1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* gkd-dbus-session.c - daemon registering environment variables with session
Copyright (C) 2007, 2009, Stefan Walter
The Gnome Keyring Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Keyring Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
<http://www.gnu.org/licenses/>.
Author: Stef Walter <stef@memberwebs.com>
*/
#include "config.h"
#include "gkd-dbus-private.h"
#include "gkd-util.h"
#include <dbus/dbus.h>
#include <string.h>
#define SERVICE_SESSION_MANAGER "org.gnome.SessionManager"
#define PATH_SESSION_MANAGER "/org/gnome/SessionManager"
#define IFACE_SESSION_MANAGER "org.gnome.SessionManager"
void
gkd_dbus_environment_cleanup (DBusConnection *conn)
{
/* Nothing to do here */
}
static void
on_setenv_reply (DBusPendingCall *pending, void *user_data)
{
DBusMessage *reply;
DBusError derr = DBUS_ERROR_INIT;
reply = dbus_pending_call_steal_reply (pending);
g_return_if_fail (reply);
if (dbus_set_error_from_message (&derr, reply)) {
if (dbus_error_has_name (&derr, "org.gnome.SessionManager.NotInInitialization") ||
dbus_error_has_name (&derr, DBUS_ERROR_SERVICE_UNKNOWN))
g_debug ("couldn't set environment variable in session: %s", derr.message);
else
g_message ("couldn't set environment variable in session: %s", derr.message);
dbus_error_free (&derr);
}
dbus_message_unref (reply);
}
static void
setenv_request (DBusConnection *conn, const gchar *env)
{
DBusPendingCall *pending = NULL;
DBusError derr = DBUS_ERROR_INIT;
DBusMessage *msg;
const gchar *value;
gchar *name;
/* Find the value part of the environment variable */
value = strchr (env, '=');
if (!value)
return;
name = g_strndup (env, value - env);
++value;
msg = dbus_message_new_method_call (SERVICE_SESSION_MANAGER,
PATH_SESSION_MANAGER,
IFACE_SESSION_MANAGER,
"Setenv");
g_return_if_fail (msg);
if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &value,
DBUS_TYPE_INVALID))
g_return_if_reached ();
g_free (name);
value = name = NULL;
/* Send message and get a handle for a reply */
dbus_connection_send_with_reply (conn, msg, &pending, -1);
dbus_message_unref (msg);
if (pending) {
dbus_pending_call_set_notify (pending, on_setenv_reply, NULL, NULL);
dbus_pending_call_unref (pending);
} else {
g_warning ("couldn't send dbus message: %s",
derr.message ? derr.message : "");
dbus_error_free (&derr);
}
}
static void
on_watch_environment (gpointer data, gpointer user_data)
{
DBusConnection *conn = user_data;
const gchar *env = data;
setenv_request (conn, env);
}
void
gkd_dbus_environment_init (DBusConnection *conn)
{
const gchar **envp;
/*
* The list of all environment variables registered by
* various components in the daemon.
*/
envp = gkd_util_get_environment ();
for (; *envp; ++envp)
setenv_request (conn, *envp);
gkd_util_watch_environment (on_watch_environment, dbus_connection_ref (conn),
(GDestroyNotify)dbus_connection_unref);
}
|