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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/* GDM - The Gnome Display Manager
* Copyright (C) 1998, 1999 Martin Kasper Petersen <mkp@SunSITE.auc.dk>
*
* 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 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* This file contains functions for controlling local X servers */
#include <config.h>
#include <gnome.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <strings.h>
#include <signal.h>
#include <errno.h>
#include <X11/Xlib.h>
#include "gdm.h"
static const gchar RCSid[]="$Id$";
extern gchar *GdmDisplayInit;
extern gchar *GdmAuthDir;
extern gchar *GdmLogDir;
extern gint GdmXdmcp;
extern gchar **gdm_arg_munch(const gchar *p);
extern void gdm_auth_secure_display(GdmDisplay *);
extern void gdm_debug(const gchar *, ...);
extern void gdm_error(const gchar *, ...);
extern gint gdm_display_manage(GdmDisplay *);
extern void gdm_putenv(gchar *);
extern void gdm_xdmcp_close();
void gdm_server_start(GdmDisplay *d);
void gdm_server_stop(GdmDisplay *d);
void gdm_server_restart(GdmDisplay *d);
void gdm_server_usr1_handler(gint);
void gdm_server_alarm_handler(gint);
GdmDisplay *gdm_server_alloc (gint id, gchar *command);
GdmDisplay *d;
sigset_t mask, omask;
void
gdm_server_start(GdmDisplay *disp)
{
struct sigaction usr1;
sigset_t usr1mask;
gchar *srvcmd=NULL;
gchar **argv=NULL;
int logfd;
d=disp;
gdm_debug("gdm_server_start: %s", d->name);
/* Catch USR1 from X server */
usr1.sa_handler = gdm_server_usr1_handler;
usr1.sa_flags = SA_RESTART|SA_RESETHAND;
sigemptyset(&usr1.sa_mask);
if(sigaction(SIGUSR1, &usr1, NULL) < 0) {
gdm_error(_("gdm_server_start: Error setting up USR1 signal handler"));
exit(SERVER_ABORT);
}
sigemptyset(&usr1mask);
sigaddset(&usr1mask, SIGUSR1);
sigprocmask(SIG_UNBLOCK, &usr1mask, NULL);
/* Log all output from spawned programs to a file */
logfd=open(g_strconcat(GdmLogDir, "/", d->name, ".log", NULL),
O_CREAT|O_TRUNC|O_APPEND|O_WRONLY, 0666);
if(logfd != -1) {
dup2(logfd, 1);
dup2(logfd, 2);
}
else
gdm_error(_("gdm_server_start: Could not open logfile for display %s!"), d->name);
/* Just in case we have an old server hanging around */
if(d->servpid) {
gdm_debug("gdm_server_start: Old server found (%d). Killing.", d->servpid);
gdm_server_stop(d);
}
/* Secure display with cookie */
gdm_auth_secure_display(d);
gdm_putenv(g_strconcat("XAUTHORITY=", d->auth, NULL));
gdm_putenv(g_strconcat("DISPLAY=", d->name, NULL));
/* Fork into two processes. Parent remains the gdm slave
* process. Child becomes the X server.
*/
switch(d->servpid=fork()) {
case 0:
/* Close the XDMCP fd inherited by the daemon process */
if(GdmXdmcp)
gdm_xdmcp_close();
/* The X server expects USR1 to be SIG_IGN */
usr1.sa_handler = SIG_IGN;
usr1.sa_flags = SA_RESTART;
sigemptyset(&usr1.sa_mask);
if(sigaction(SIGUSR1, &usr1, NULL) < 0) {
gdm_error(_("gdm_server_start: Error setting USR1 to SIG_IGN"));
exit(SERVER_ABORT);
}
srvcmd=g_strconcat(d->command, " -auth ", GdmAuthDir, \
"/", d->name, ".xauth ",
d->name, NULL);
gdm_debug("gdm_server_start: '%s'", srvcmd);
argv=gdm_arg_munch(srvcmd);
g_free(srvcmd);
setpgid(0, 0);
execv(argv[0], argv);
gdm_error(_("gdm_server_start: Xserver not found: %s"), d->command);
exit(SERVER_ABORT);
break;
case -1:
gdm_error(_("gdm_server_start: Can't fork Xserver process!"));
d->servpid=0;
break;
default:
break;
}
d->servstat=SERVER_STARTED;
/* Wait for X server to send ready signal */
pause();
}
void
gdm_server_stop(GdmDisplay *d)
{
gdm_debug("gdm_server_stop: Server for %s going down!", d->name);
kill(d->servpid, SIGTERM);
waitpid(d->servpid, 0, 0);
d->servpid=0;
d->servstat=SERVER_DEAD;
if(unlink(d->auth) == -1)
gdm_error(_("gdm_server_stop: Could not unlink auth file: %s!"), strerror(errno));
}
/* FIXME: We have a race here. What if USR1 is delivered before we enable the handler?
* This only succeeds because the X server takes ages to shut down...
* Maybe we should jump directly to display_manage and bail out on error?
* Needs thinking for sure...
*/
void
gdm_server_restart(GdmDisplay *d)
{
sigset_t usr1mask;
struct sigaction usr1, sigalarm;
gdm_debug("gdm_server_restart: Server for %s restarting!", d->name);
if(d->servpid && kill(d->servpid, 0)<0) {
gdm_debug("gdm_server_restart: Old server for %s still alive. Killing!", d->name);
gdm_server_stop(d);
gdm_server_start(d);
}
/* Create new cookie */
gdm_auth_secure_display(d);
gdm_putenv(g_strconcat("XAUTHORITY=", d->auth, NULL));
gdm_putenv(g_strconcat("DISPLAY=", d->name, NULL));
/* Catch USR1 from X server */
usr1.sa_handler = gdm_server_usr1_handler;
usr1.sa_flags = SA_RESTART|SA_RESETHAND;
sigemptyset(&usr1.sa_mask);
if(sigaction(SIGUSR1, &usr1, NULL) < 0) {
gdm_error(_("gdm_server_start: Error setting up USR1 signal handler"));
exit(SERVER_ABORT);
}
sigemptyset(&usr1mask);
sigaddset(&usr1mask, SIGUSR1);
sigprocmask(SIG_UNBLOCK, &usr1mask, NULL);
/* Reset X and force auth file reread. XCloseDisplay works on most servers but we play it safe */
gdm_debug("gdm_server_restart: Servpid=%d", d->servpid);
kill(d->servpid, SIGHUP);
d->servstat=SERVER_STARTED;
/* We add a timeout in case the X server fails to start. This
* might happen because X servers take a while to die, close their
* sockets etc. If the old X server isn't completely dead, the new
* one will fail and we'll hang here forever */
sigalarm.sa_handler = gdm_server_alarm_handler;
sigalarm.sa_flags = 0;
sigemptyset(&sigalarm.sa_mask);
if(sigaction(SIGALRM, &sigalarm, NULL) < 0) {
gdm_error(_("gdm_server_restart: Error setting up ALARM signal handler"));
return;
}
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
sigprocmask(SIG_UNBLOCK, &mask, &omask);
alarm(10);
/* Wait for X server to send ready signal */
pause();
}
void
gdm_server_alarm_handler(gint signal)
{
/* Unset alarm and try again */
alarm(0);
sigprocmask(SIG_SETMASK, &omask, NULL);
waitpid(d->servpid, 0, WNOHANG);
gdm_debug("gdm_server_alarm_handler: Temporary server failure");
gdm_server_restart(d);
}
void
gdm_server_usr1_handler(gint sig)
{
sigset_t usr1mask;
/* Unset alarm */
alarm(0);
sigprocmask(SIG_SETMASK, &omask, NULL);
gdm_debug("gdm_server_usr1_handler: Starting display %s!", d->name);
d->servstat=SERVER_RUNNING;
/* Block USR1 */
sigemptyset(&usr1mask);
sigaddset(&usr1mask, SIGUSR1);
sigprocmask(SIG_BLOCK, &usr1mask, NULL);
gdm_display_manage(d);
}
GdmDisplay *
gdm_server_alloc (gint id, gchar *command)
{
gchar dname[1024];
GdmDisplay *d = g_malloc(sizeof(GdmDisplay));
sprintf(dname, ":%d", id);
d->auth = NULL;
d->command = g_strdup(command);
d->cookie = NULL;
d->dispstat = DISPLAY_DEAD;
d->greetpid = 0;
d->id = id;
d->name = strdup(dname);
d->servpid = 0;
d->servstat = SERVER_DEAD;
d->sessionid = 0;
d->sesspid = 0;
d->slavepid = 0;
d->type = DISPLAY_LOCAL;
d->sessionid = 0;
d->acctime = 0;
d->dsp = NULL;
return (d);
}
/* EOF */
|