LCOV - code coverage report
Current view: top level - daemon/dbus - gkd-dbus-environment.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.0 % 28 21
Test Date: 2024-04-08 13:24:42 Functions: 75.0 % 4 3

            Line data    Source code
       1              : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
       2              : /* gkd-dbus-session.c - daemon registering environment variables with session
       3              : 
       4              :    Copyright (C) 2007, 2009, Stefan Walter
       5              : 
       6              :    The Gnome Keyring Library is free software; you can redistribute it and/or
       7              :    modify it under the terms of the GNU Library General Public License as
       8              :    published by the Free Software Foundation; either version 2 of the
       9              :    License, or (at your option) any later version.
      10              : 
      11              :    The Gnome Keyring Library is distributed in the hope that it will be useful,
      12              :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14              :    Library General Public License for more details.
      15              : 
      16              :    You should have received a copy of the GNU Library General Public
      17              :    License along with the Gnome Library; see the file COPYING.LIB.  If not,
      18              :    <http://www.gnu.org/licenses/>.
      19              : 
      20              :    Author: Stef Walter <stef@memberwebs.com>
      21              : */
      22              : 
      23              : #include "config.h"
      24              : 
      25              : #include "gkd-dbus-private.h"
      26              : 
      27              : #include "daemon/gkd-util.h"
      28              : 
      29              : #include <string.h>
      30              : 
      31              : #define SERVICE_SESSION_MANAGER "org.gnome.SessionManager"
      32              : #define PATH_SESSION_MANAGER    "/org/gnome/SessionManager"
      33              : #define IFACE_SESSION_MANAGER   "org.gnome.SessionManager"
      34              : 
      35              : void
      36           27 : gkd_dbus_environment_cleanup (GDBusConnection *conn)
      37              : {
      38              :         /* Nothing to do here */
      39           27 : }
      40              : 
      41              : static void
      42           22 : setenv_request (GDBusConnection *conn, const gchar *env)
      43              : {
      44              :         const gchar *value;
      45              :         gchar *name;
      46              :         GVariant *res;
      47           22 :         GError *error = NULL;
      48              : 
      49              :         /* Find the value part of the environment variable */
      50           22 :         value = strchr (env, '=');
      51           22 :         if (!value)
      52            0 :                 return;
      53              : 
      54           22 :         name = g_strndup (env, value - env);
      55           22 :         ++value;
      56              : 
      57              :         /* Note: This call does not neccessarily need to be a sync call. However
      58              :          *       under certain conditions the process will quit immediately
      59              :          *       after emitting the call. This ensures that we wait long enough
      60              :          *       for the message to be sent out (could also be done using
      61              :          *       g_dbus_connection_flush() in the exit handler when called with
      62              :          *       --start) and also ensures that gnome-session has processed the
      63              :          *       DBus message before possibly thinking that the startup of
      64              :          *       gnome-keyring has finished and continuing with forking the
      65              :          *       shell. */
      66           22 :         res = g_dbus_connection_call_sync (conn,
      67              :                                            SERVICE_SESSION_MANAGER,
      68              :                                            PATH_SESSION_MANAGER,
      69              :                                            IFACE_SESSION_MANAGER,
      70              :                                            "Setenv",
      71              :                                            g_variant_new ("(ss)",
      72              :                                                           name,
      73              :                                                           value),
      74              :                                            NULL, G_DBUS_CALL_FLAGS_NONE,
      75              :                                            -1, NULL, &error);
      76              : 
      77           22 :         if (error != NULL) {
      78           22 :                 if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN))
      79           22 :                         g_debug ("couldn't set environment variable in session: %s", error->message);
      80              :                 else
      81            0 :                         g_message ("couldn't set environment variable in session: %s", error->message);
      82           22 :                 g_error_free (error);
      83              :         }
      84              : 
      85           22 :         g_free (name);
      86           22 :         g_clear_pointer (&res, g_variant_unref);
      87              : }
      88              : 
      89              : static void
      90            0 : on_watch_environment (gpointer data, gpointer user_data)
      91              : {
      92            0 :         GDBusConnection *conn = user_data;
      93            0 :         const gchar *env = data;
      94            0 :         setenv_request (conn, env);
      95            0 : }
      96              : 
      97              : void
      98           27 : gkd_dbus_environment_init (GDBusConnection *conn)
      99              : {
     100              :         const gchar **envp;
     101              : 
     102              :         /*
     103              :          * The list of all environment variables registered by
     104              :          * various components in the daemon.
     105              :          */
     106           27 :         envp = gkd_util_get_environment ();
     107              : 
     108           49 :         for (; *envp; ++envp)
     109           22 :                 setenv_request (conn, *envp);
     110              : 
     111           27 :         gkd_util_watch_environment (on_watch_environment, g_object_ref (conn),
     112              :                                     (GDestroyNotify) g_object_unref);
     113           27 : }
        

Generated by: LCOV version 2.0-1