-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
msm-gnome.c
273 lines (222 loc) · 6.98 KB
/
msm-gnome.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright (c) 2004-2005 Benedikt Meurer <benny@xfce.org>
* 2013 Stefano Karapetsas <stefano@karapetsas.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* Most parts of this file where taken from xfce4-session and
* gnome-session.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gio/gio.h>
#include "msm-gnome.h"
#define GSM_SCHEMA "org.mate.session"
#define GSM_GNOME_COMPAT_STARTUP_KEY "gnome-compat-startup"
#define GNOME_KEYRING_DAEMON "gnome-keyring-daemon"
static gboolean gnome_compat_started = FALSE;
static int keyring_lifetime_pipe[2];
static pid_t gnome_keyring_daemon_pid = 0;
static Window gnome_smproxy_window = None;
static void
child_setup (gpointer user_data)
{
gint open_max;
gint fd;
char *fd_str;
open_max = sysconf (_SC_OPEN_MAX);
for (fd = 3; fd < open_max; fd++)
{
if (fd != keyring_lifetime_pipe[0])
fcntl (fd, F_SETFD, FD_CLOEXEC);
}
fd_str = g_strdup_printf ("%d", keyring_lifetime_pipe[0]);
g_setenv ("GNOME_KEYRING_LIFETIME_FD", fd_str, TRUE);
g_free (fd_str);
}
static void
gnome_keyring_daemon_startup (void)
{
GError *error = NULL;
gchar *sout;
gchar **lines;
gsize lineno;
gint status;
glong pid;
gchar *end;
gchar *argv[3];
gchar *p;
gchar *name;
const gchar *value;
/* Pipe to slave keyring lifetime to */
if (pipe (keyring_lifetime_pipe))
{
g_warning ("Failed to set up pipe for gnome-keyring: %s", strerror (errno));
return;
}
error = NULL;
argv[0] = GNOME_KEYRING_DAEMON;
argv[1] = "--start";
argv[2] = NULL;
g_spawn_sync (NULL, argv, NULL,
G_SPAWN_SEARCH_PATH | G_SPAWN_LEAVE_DESCRIPTORS_OPEN,
child_setup, NULL,
&sout, NULL, &status, &error);
close (keyring_lifetime_pipe[0]);
/* We leave keyring_lifetime_pipe[1] open for the lifetime of the session,
in order to slave the keyring daemon lifecycle to the session. */
if (error != NULL)
{
g_printerr ("Failed to run gnome-keyring-daemon: %s\n",
error->message);
g_error_free (error);
}
else
{
if (WIFEXITED (status) && WEXITSTATUS (status) == 0 && sout != NULL)
{
lines = g_strsplit (sout, "\n", 0);
for (lineno = 0; lines[lineno] != NULL; lineno++)
{
p = strchr (lines[lineno], '=');
if (p == NULL)
continue;
name = g_strndup (lines[lineno], p - lines[lineno]);
value = p + 1;
g_setenv (name, value, TRUE);
if (g_strcmp0 (name, "GNOME_KEYRING_PID") == 0)
{
pid = strtol (value, &end, 10);
if (end != value)
gnome_keyring_daemon_pid = pid;
}
g_free (name);
}
g_strfreev (lines);
}
else
{
/* daemon failed for some reason */
g_printerr ("gnome-keyring-daemon failed to start correctly, "
"exit code: %d\n", WEXITSTATUS (status));
}
g_free (sout);
}
}
static void
gnome_keyring_daemon_shutdown (void)
{
if (gnome_keyring_daemon_pid != 0)
{
kill (gnome_keyring_daemon_pid, SIGTERM);
gnome_keyring_daemon_pid = 0;
}
}
static void
msm_compat_gnome_smproxy_startup (void)
{
Atom gnome_sm_proxy;
Display *dpy;
Window root;
GdkDisplay *gdkdisplay;
gdkdisplay = gdk_display_get_default ();
gdk_x11_display_error_trap_push (gdkdisplay);
/* Set GNOME_SM_PROXY property, since some apps (like OOo) seem to require
* it to behave properly. Thanks to Jasper/Francois for reporting this.
* This has another advantage, since it prevents people from running
* gnome-smproxy in xfce4, which would cause trouble otherwise.
*/
dpy = GDK_DISPLAY_XDISPLAY(gdkdisplay);
root = RootWindow (dpy, 0);
if (gnome_smproxy_window != None)
XDestroyWindow (dpy, gnome_smproxy_window);
gnome_sm_proxy = XInternAtom (dpy, "GNOME_SM_PROXY", False);
gnome_smproxy_window = XCreateSimpleWindow (dpy, root, 1, 1, 1, 1, 0, 0, 0);
XChangeProperty (dpy, gnome_smproxy_window, gnome_sm_proxy,
XA_CARDINAL, 32, PropModeReplace,
(unsigned char *) (void *) &gnome_smproxy_window, 1);
XChangeProperty (dpy, root, gnome_sm_proxy,
XA_CARDINAL, 32, PropModeReplace,
(unsigned char *) (void *) &gnome_smproxy_window, 1);
XSync (dpy, False);
gdk_x11_display_error_trap_pop_ignored (gdkdisplay);
}
static void
msm_compat_gnome_smproxy_shutdown (void)
{
GdkDisplay *gdkdisplay;
gdkdisplay = gdk_display_get_default ();
gdk_x11_display_error_trap_push (gdkdisplay);
if (gnome_smproxy_window != None)
{
XDestroyWindow (GDK_DISPLAY_XDISPLAY(gdkdisplay), gnome_smproxy_window);
XSync (GDK_DISPLAY_XDISPLAY(gdkdisplay), False);
gnome_smproxy_window = None;
}
gdk_x11_display_error_trap_pop_ignored (gdkdisplay);
}
void
msm_gnome_start (void)
{
GSettings* settings;
gchar **array;
if (gnome_compat_started == TRUE)
return;
settings = g_settings_new (GSM_SCHEMA);
array = g_settings_get_strv (settings, GSM_GNOME_COMPAT_STARTUP_KEY);
if (array) {
guint i;
for (i = 0; array[i]; i++) {
if (strcmp (array[i], "smproxy") == 0) {
g_debug ("MsmGnome: starting smproxy");
msm_compat_gnome_smproxy_startup ();
gnome_compat_started = TRUE;
} else if (strcmp (array[i], "keyring") == 0) {
g_debug ("MsmGnome: starting keyring");
gnome_keyring_daemon_startup ();
gnome_compat_started = TRUE;
} else {
g_debug ("MsmGnome: ignoring unknown component \"%s\"", array[i]);
}
}
g_strfreev (array);
} else {
g_debug ("MsmGnome: No components found to start");
}
g_object_unref (settings);
}
void
msm_gnome_stop (void)
{
if (gnome_compat_started == FALSE)
return;
g_debug ("MsmGnome: stopping");
/* shutdown the keyring daemon */
gnome_keyring_daemon_shutdown ();
msm_compat_gnome_smproxy_shutdown ();
gnome_compat_started = FALSE;
}