4158
|
1 /*
|
|
2 * session management for Gaim
|
|
3 *
|
|
4 * Copyright (C) 2002, Robert McQueen <robot101@debian.org> but
|
|
5 * much code shamelessly cribbed from GsmClient (C) 2001 Havoc
|
|
6 * Pennington, which is in turn inspired by various other pieces
|
|
7 * of code including GnomeClient (C) 1998 Carsten Schaar, Tom
|
|
8 * Tromey, and twm session code (C) 1998 The Open Group.
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 *
|
|
24 */
|
|
25
|
|
26 #ifdef HAVE_CONFIG_H
|
|
27 #include <config.h>
|
|
28 #endif
|
|
29
|
4210
|
30 #include <string.h>
|
|
31
|
4158
|
32 #include "gaim.h"
|
|
33
|
|
34 #ifdef USE_SM
|
|
35
|
|
36 #include <X11/ICE/ICElib.h>
|
|
37 #include <X11/SM/SMlib.h>
|
|
38 #include <unistd.h>
|
|
39 #include <fcntl.h>
|
|
40 #define ERROR_LENGTH 512
|
|
41
|
|
42 static IceIOErrorHandler ice_installed_io_error_handler;
|
|
43 static SmcConn session = NULL;
|
|
44 static gchar *myself = NULL;
|
|
45 static gboolean had_first_save = FALSE;
|
|
46 gboolean session_managed = FALSE;
|
|
47
|
|
48 /* ICE belt'n'braces stuff */
|
|
49
|
|
50 static gboolean ice_process_messages(GIOChannel *channel, GIOCondition condition,
|
|
51 gpointer data) {
|
|
52 IceConn connection = (IceConn)data;
|
|
53 IceProcessMessagesStatus status;
|
|
54
|
|
55 /* please don't block... please! */
|
|
56 status = IceProcessMessages(connection, NULL, NULL);
|
|
57
|
|
58 if (status == IceProcessMessagesIOError) {
|
|
59 debug_printf("Session Management: ICE IO error, closing connection... ");
|
|
60
|
|
61 /* IO error, please disconnect */
|
|
62 IceSetShutdownNegotiation(connection, False);
|
|
63 IceCloseConnection(connection);
|
|
64
|
|
65 debug_printf("done\n");
|
|
66
|
|
67 /* cancel the handler */
|
|
68 return FALSE;
|
|
69 }
|
|
70
|
|
71 /* live to see another day */
|
|
72 return TRUE;
|
|
73 }
|
|
74
|
|
75 static void ice_connection_watch(IceConn connection, IcePointer client_data,
|
|
76 Bool opening, IcePointer *watch_data) {
|
|
77 guint input_id;
|
|
78
|
|
79 if (opening) {
|
|
80 GIOChannel *channel;
|
|
81
|
|
82 debug_printf("Session Management: handling new ICE connection... ");
|
|
83
|
|
84 /* ensure ICE connection is not passed to child processes */
|
|
85 fcntl(IceConnectionNumber(connection), F_SETFD, FD_CLOEXEC);
|
|
86
|
|
87 /* get glib to watch the connection for us */
|
|
88 channel = g_io_channel_unix_new(IceConnectionNumber(connection));
|
|
89 input_id = g_io_add_watch(channel, G_IO_IN | G_IO_ERR,
|
|
90 ice_process_messages, connection);
|
|
91 g_io_channel_unref(channel);
|
|
92
|
|
93 /* store the input ID as a pointer for when it closes */
|
|
94 *watch_data = (IcePointer)GUINT_TO_POINTER(input_id);
|
|
95 } else {
|
|
96 debug_printf("Session Management: handling closed ICE connection... ");
|
|
97
|
|
98 /* get the input ID back and stop watching it */
|
|
99 input_id = GPOINTER_TO_UINT((gpointer) *watch_data);
|
|
100 g_source_remove(input_id);
|
|
101 }
|
|
102
|
|
103 debug_printf("done\n");
|
|
104 }
|
|
105
|
|
106 /* We call any handler installed before (or after) ice_init but
|
|
107 * avoid calling the default libICE handler which does an exit().
|
|
108 *
|
|
109 * This means we do nothing by default, which is probably correct,
|
|
110 * the connection will get closed by libICE
|
|
111 */
|
|
112
|
|
113 static void ice_io_error_handler(IceConn connection) {
|
|
114 debug_printf("Session Management: handling ICE IO error... ");
|
|
115
|
|
116 if (ice_installed_io_error_handler)
|
|
117 (*ice_installed_io_error_handler)(connection);
|
|
118
|
|
119 debug_printf("done\n");
|
|
120 }
|
|
121
|
|
122 static void ice_init() {
|
|
123 IceIOErrorHandler default_handler;
|
|
124
|
|
125 ice_installed_io_error_handler = IceSetIOErrorHandler(NULL);
|
|
126 default_handler = IceSetIOErrorHandler(ice_io_error_handler);
|
|
127
|
|
128 if (ice_installed_io_error_handler == default_handler)
|
|
129 ice_installed_io_error_handler = NULL;
|
|
130
|
|
131 IceAddConnectionWatch(ice_connection_watch, NULL);
|
|
132
|
|
133 debug_printf("Session Management: ICE initialised\n");
|
|
134 }
|
|
135
|
4281
|
136 /* my magic utility function */
|
|
137
|
|
138 static gchar **session_make_command(gchar *client_id) {
|
|
139 gint i = 2;
|
|
140 gint j = 0;
|
|
141 gchar **ret;
|
|
142
|
|
143 if (client_id) i += 2;
|
|
144 if (opt_rcfile_arg) i += 2;
|
|
145
|
|
146 ret = g_new(gchar *, i);
|
|
147 ret[j++] = g_strdup(myself);
|
|
148
|
|
149 if (client_id) {
|
|
150 ret[j++] = g_strdup("--session");
|
|
151 ret[j++] = g_strdup(client_id);
|
|
152 }
|
|
153
|
|
154 if (opt_rcfile_arg) {
|
|
155 ret[j++] = g_strdup("--file");
|
|
156 ret[j++] = g_strdup(opt_rcfile_arg);
|
|
157 }
|
|
158
|
|
159 ret[j++] = NULL;
|
|
160
|
|
161 return ret;
|
|
162 }
|
|
163
|
4158
|
164 /* SM callback handlers */
|
|
165
|
|
166 void session_save_yourself(SmcConn conn, SmPointer data, int save_type,
|
|
167 Bool shutdown, int interact_style, Bool fast) {
|
|
168 if (had_first_save == FALSE && save_type == SmSaveLocal &&
|
|
169 interact_style == SmInteractStyleNone && !shutdown &&
|
|
170 !fast) {
|
|
171 /* this is just a dry run, spit it back */
|
|
172 debug_printf("Session Management: received first save_yourself\n");
|
|
173 SmcSaveYourselfDone(conn, True);
|
|
174 had_first_save = TRUE;
|
|
175 return;
|
|
176 }
|
|
177
|
|
178 /* tum ti tum... don't add anything else here without *
|
|
179 * reading SMlib.PS from an X.org ftp server near you */
|
|
180
|
|
181 debug_printf("Session Management: received save_yourself\n");
|
|
182
|
|
183 if (save_type == SmSaveGlobal || save_type == SmSaveBoth) {
|
|
184 /* may as well do something ... */
|
|
185 save_prefs();
|
|
186 }
|
|
187
|
|
188 SmcSaveYourselfDone(conn, True);
|
|
189 }
|
|
190
|
|
191 void session_die(SmcConn conn, SmPointer data) {
|
|
192 debug_printf("Session Management: received die\n");
|
|
193 do_quit();
|
|
194 }
|
|
195
|
|
196 void session_save_complete(SmcConn conn, SmPointer data) {
|
|
197 debug_printf("Session Management: received save_complete\n");
|
|
198 }
|
|
199
|
|
200 void session_shutdown_cancelled(SmcConn conn, SmPointer data) {
|
|
201 debug_printf("Session Management: received shutdown_cancelled\n");
|
|
202 }
|
|
203
|
|
204 /* utility functions stolen from Gnome-client */
|
|
205
|
|
206 static void session_set_value(SmcConn conn, gchar *name, char *type,
|
|
207 int num_vals, SmPropValue *vals) {
|
|
208 SmProp *proplist[1];
|
|
209 SmProp prop;
|
|
210
|
|
211 g_return_if_fail(conn);
|
|
212
|
|
213 prop.name = name;
|
|
214 prop.type = type;
|
|
215 prop.num_vals = num_vals;
|
|
216 prop.vals = vals;
|
|
217
|
|
218 proplist[0] = ∝
|
|
219 SmcSetProperties(conn, 1, proplist);
|
|
220 }
|
|
221
|
|
222 static void session_set_string(SmcConn conn, gchar *name, gchar *value) {
|
|
223 SmPropValue val;
|
|
224
|
|
225 g_return_if_fail(name);
|
|
226
|
|
227 val.length = strlen (value)+1;
|
|
228 val.value = value;
|
|
229
|
|
230 session_set_value(conn, name, SmARRAY8, 1, &val);
|
|
231 }
|
|
232
|
|
233 static void session_set_gchar(SmcConn conn, gchar *name, gchar value) {
|
|
234 SmPropValue val;
|
|
235
|
|
236 g_return_if_fail(name);
|
|
237
|
|
238 val.length = 1;
|
|
239 val.value = &value;
|
|
240
|
|
241 session_set_value(conn, name, SmCARD8, 1, &val);
|
|
242 }
|
|
243
|
|
244 static void session_set_array(SmcConn conn, gchar *name, gchar *array[]) {
|
|
245 gint argc;
|
|
246 gchar **ptr;
|
|
247 gint i;
|
|
248
|
|
249 SmPropValue *vals;
|
|
250
|
|
251 g_return_if_fail (name);
|
|
252
|
|
253 /* We count the number of elements in our array. */
|
|
254 for (ptr = array, argc = 0; *ptr ; ptr++, argc++) /* LOOP */;
|
|
255
|
|
256 /* Now initialize the 'vals' array. */
|
|
257 vals = g_new (SmPropValue, argc);
|
|
258 for (ptr = array, i = 0 ; i < argc ; ptr++, i++) {
|
|
259 vals[i].length = strlen (*ptr);
|
|
260 vals[i].value = *ptr;
|
|
261 }
|
|
262
|
|
263 session_set_value(conn, name, SmLISTofARRAY8, argc, vals);
|
|
264
|
|
265 g_free (vals);
|
|
266 }
|
|
267
|
|
268 #endif /* USE_SM */
|
|
269
|
|
270 /* setup functions */
|
|
271
|
|
272 void session_init(gchar *argv0, gchar *previous_id) {
|
|
273 #ifdef USE_SM
|
|
274 SmcCallbacks callbacks;
|
|
275 gchar *client_id = NULL;
|
|
276 gchar error[ERROR_LENGTH] = "";
|
|
277 gchar *tmp = NULL;
|
4281
|
278 gchar **cmd = NULL;
|
4158
|
279
|
|
280 if (session != NULL) {
|
|
281 /* session is already established, what the hell is going on? */
|
|
282 debug_printf("Session Management: duplicated call to session_init!\n");
|
|
283 return;
|
|
284 }
|
|
285
|
|
286 if (g_getenv("SESSION_MANAGER") == NULL) {
|
|
287 debug_printf("Session Management: no SESSION_MANAGER found, aborting\n");
|
|
288 return;
|
|
289 }
|
|
290
|
|
291 ice_init();
|
|
292
|
|
293 callbacks.save_yourself.callback = session_save_yourself;
|
|
294 callbacks.die.callback = session_die;
|
|
295 callbacks.save_complete.callback = session_save_complete;
|
|
296 callbacks.shutdown_cancelled.callback = session_shutdown_cancelled;
|
|
297
|
|
298 callbacks.save_yourself.client_data = NULL;
|
|
299 callbacks.die.client_data = NULL;
|
|
300 callbacks.save_complete.client_data = NULL;
|
|
301 callbacks.shutdown_cancelled.client_data = NULL;
|
|
302
|
|
303 debug_printf("Session Management: connecting with previous ID %s\n", previous_id);
|
|
304
|
|
305 session = SmcOpenConnection(NULL, "session", SmProtoMajor, SmProtoMinor, SmcSaveYourselfProcMask |
|
|
306 SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask,
|
|
307 &callbacks, previous_id, &client_id, ERROR_LENGTH, error);
|
|
308
|
|
309 if (session == NULL) {
|
|
310 if (error[0] != '\0') {
|
|
311 debug_printf("Session Management: connection failed with error: %s\n", error);
|
|
312 } else {
|
|
313 debug_printf("Session Management: connection failed with unknown error\n");
|
|
314 }
|
|
315 return;
|
|
316 }
|
|
317
|
|
318 tmp = SmcVendor(session);
|
|
319 debug_printf("Session Management: connected to manager (%s) with client ID %s\n", tmp, client_id);
|
|
320 g_free(tmp);
|
|
321
|
|
322 session_managed = TRUE;
|
|
323 gdk_set_sm_client_id(client_id);
|
|
324
|
4265
|
325 tmp = g_get_current_dir();
|
|
326 session_set_string(session, SmCurrentDirectory, tmp);
|
|
327 g_free(tmp);
|
|
328
|
4210
|
329 tmp = g_strdup_printf("%d", (int) getpid());
|
4158
|
330 session_set_string(session, SmProcessID, tmp);
|
|
331 g_free(tmp);
|
|
332
|
4210
|
333 tmp = g_strdup(g_get_user_name());
|
4158
|
334 session_set_string(session, SmUserID, tmp);
|
|
335 g_free(tmp);
|
|
336
|
|
337 session_set_gchar(session, SmRestartStyleHint, (gchar) SmRestartIfRunning);
|
|
338 session_set_string(session, SmProgram, g_get_prgname());
|
|
339
|
|
340 myself = g_strdup(argv0);
|
|
341 debug_printf("Session Management: using %s as command\n", myself);
|
|
342
|
4281
|
343 cmd = session_make_command(NULL);
|
4158
|
344 session_set_array(session, SmCloneCommand, cmd);
|
4281
|
345 g_strfreev(cmd);
|
4158
|
346
|
4265
|
347 /* this is currently useless, but gnome-session warns 'the following applications will not
|
|
348 save their current status' bla bla if we don't have it and the user checks 'Save Session'
|
|
349 when they log out */
|
4281
|
350 cmd = g_new(gchar *, 2);
|
|
351 cmd[0] = g_strdup("/bin/true");
|
|
352 cmd[1] = NULL;
|
4158
|
353 session_set_array(session, SmDiscardCommand, cmd);
|
4281
|
354 g_strfreev(cmd);
|
4158
|
355
|
4281
|
356 cmd = session_make_command(client_id);
|
4158
|
357 session_set_array(session, SmRestartCommand, cmd);
|
4281
|
358 g_strfreev(cmd);
|
|
359
|
4158
|
360 g_free(client_id);
|
|
361 #endif /* USE_SM */
|
|
362 }
|
|
363
|
|
364 void session_end() {
|
|
365 #ifdef USE_SM
|
|
366 if (session == NULL) /* no session to close */
|
|
367 return;
|
|
368
|
|
369 SmcCloseConnection(session, 0, NULL);
|
|
370
|
|
371 debug_printf("Session Management: connection closed\n");
|
|
372 #endif /* USE_SM */
|
|
373 }
|