summaryrefslogtreecommitdiff
path: root/hw/dmx/dmxlaunch.c
blob: b10db894aac0f72f26ae0cb712fc246f7dd48d04 (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * Copyright © 2005 Novell, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Novell, Inc. not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior permission.
 * Novell, Inc. makes no representations about the suitability of this
 * software for any purpose. It is provided "as is" without express or
 * implied warranty.
 *
 * NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
 * NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Authors: David Reveman <davidr@novell.com>
 *          Matthias Hopf <mhopf@suse.de>
 */

#include "dmx.h"
#include "dmxlaunch.h"
#include "dmxinit.h"
#include "opaque.h"

#include <X11/Xauth.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <libgen.h>
#include <fcntl.h>

typedef void (*sighandler_t) (int);

#define XBE_DIE_TIMEOUT 3
#define XBE_DEV_RANDOM  "/dev/urandom"

static char xbeAuthBuf[256];
static char *xbeAuthTempl = "/tmp/.Xdmx-auth-XXXXXX";
static char *xbeAuth      = NULL;

static char *xbeProgs[] = { "/var/X11R6/bin/Xbackend", "/usr/bin/X" };
static char *xbeProg    = NULL;

static char xbeDisplayBuf[256];
static char *xbeDisplay      = NULL;
static int  xbeDisplayOffset = 53;

static pid_t   xbePid = 0;
static int     receivedUsr1 = 0;
static jmp_buf jumpbuf;

static char **xbeArgv = 0;
static int  nXbeArgv  = 0;

static int
dmxAddXbeArguments (char **argv,
		    int  n)
{
    char **newArgv;
    int  i;

    newArgv = xrealloc (xbeArgv, sizeof (char *) * (nXbeArgv + n));
    if (!newArgv)
	return 0;

    for (i = 0; i < n; i++)
	newArgv[nXbeArgv + i] = argv[i];

    xbeArgv   = newArgv;
    nXbeArgv += n;

    return n;
}

static void
sigAlarm (int sig)
{
    ErrorF ("%s won't die, killing it\n", basename (xbeProg));

    kill (xbePid, SIGKILL);
    if (xbePid)
	while (waitpid (xbePid, NULL, 0) == -1 && errno == EINTR);
}

void
dmxAbortDisplay (void)
{
    sighandler_t oldSigAlarm;
    unsigned int oldAlarm;
    int          status = 0;
    char	 *name;

    if (!xbePid)
	return;

    name = basename (xbeProg);

    oldAlarm    = alarm (0);
    oldSigAlarm = signal (SIGALRM, sigAlarm);

    kill (xbePid, SIGTERM);

    alarm (XBE_DIE_TIMEOUT);
    while (waitpid (xbePid, &status, 0) == -1 && errno == EINTR);
    alarm (0);

    signal (SIGALRM, oldSigAlarm);
    alarm (oldAlarm);

    if (WIFEXITED (status))
    {
	if (WEXITSTATUS (status))
	    ErrorF ("%s died, exit status %d\n", name, WEXITSTATUS (status));
    }
    else if (WIFSIGNALED (status))
	ErrorF ("%s died, signal %d\n", name, WTERMSIG (status));
    else
	ErrorF ("%s died, dubious exit\n", name);

    if (xbeAuth)
	unlink (xbeAuth);
}

static void
sigUsr1Waiting (int sig)
{
    signal (sig, sigUsr1Waiting);
    receivedUsr1++;
}

static void
sigUsr1Jump (int sig)
{

#ifdef HAVE_SIGPROCMASK
    sigset_t set;
#endif

    signal (sig, sigUsr1Waiting);

#ifdef HAVE_SIGPROCMASK
    sigemptyset (&set);
    sigaddset (&set, SIGUSR1);
    sigprocmask (SIG_UNBLOCK, &set, NULL);
#endif

    longjmp (jumpbuf, 1);
}

#define AUTH_DATA_LEN 16 /* bytes of authorization data */

static Bool
dmxSetupAuth (char *name, int authFd)
{
    Xauth   auth;
    int	    randomFd, i;
    ssize_t bytes, size;
    char    authHost[256];
    char    authData[AUTH_DATA_LEN];
    char    realProg[PATH_MAX], buf[PATH_MAX];
    FILE    *file;
    int     virtualFb = FALSE;

    auth.family = FamilyLocal;

    gethostname (authHost, sizeof (authHost));

    auth.address	= authHost;
    auth.address_length = strlen (authHost);

    auth.number	= strrchr (xbeDisplay, ':');
    if (!auth.number)
    {
	ErrorF ("Bad back-end X display name: %s\n", xbeDisplay);
	return FALSE;
    }

    auth.number++;

    auth.number_length = strlen (auth.number);
    if (!auth.number_length)
    {
	ErrorF ("Bad back-end X display name: %s\n", xbeDisplay);
	return FALSE;
    }

    auth.name	     = "MIT-MAGIC-COOKIE-1";
    auth.name_length = strlen (auth.name);

    randomFd = open (XBE_DEV_RANDOM, O_RDONLY);
    if (randomFd == -1)
    {
	ErrorF ("Failed to open " XBE_DEV_RANDOM "\n");
	return FALSE;
    }

    bytes = 0;
    do {
	size = read (randomFd, authData + bytes, AUTH_DATA_LEN - bytes);
	if (size <= 0)
	    break;

	bytes += size;
    } while (bytes != AUTH_DATA_LEN);

    close (randomFd);

    if (bytes != AUTH_DATA_LEN)
    {
	ErrorF ("Failed to read %d random bytes from " XBE_DEV_RANDOM "\n",
		AUTH_DATA_LEN);
	return FALSE;
    }

    auth.data	     = authData;
    auth.data_length = AUTH_DATA_LEN;

    file = fdopen (authFd, "w");
    if (!file)
    {
	ErrorF ("Failed to open authorization file: %s\n", name);
	close (authFd);
	return FALSE;
    }

    XauWriteAuth (file, &auth);
    fclose (file);

    strcpy (realProg, xbeProg);

#define MAX_SYMLINKS 32

    for (i = 0; i < MAX_SYMLINKS; i++)
    {
	size = readlink (realProg, buf, sizeof (buf) - 1);
	if (size == -1)
	    break;
	
	memcpy (realProg, buf, size);
	realProg[size] = '\0';
    }

    /* a bit hackish but very useful */
    if (strcmp (basename (realProg), "Xvfb")  == 0 ||
	strcmp (basename (realProg), "Xfake") == 0)
	virtualFb = TRUE;
    
    dmxAddScreen (basename (realProg),
		  xbeDisplay,
		  auth.name,
		  auth.name_length,
		  auth.data,
		  auth.data_length,
		  virtualFb);

    return TRUE;
}

Bool
dmxLaunchDisplay (int argc, char *argv[], int index, char *vt)
{
    sighandler_t oldSigUsr1;
    pid_t	 pid;
    char	 *name;
    char	 *auth[] = { "-auth", xbeAuthBuf };
    char	 *defs[] = { "-terminate", "-nolisten", "tcp" };
    char	 *endArg = NULL;
    int		 authFd;
    int		 mask;

    if (xbePid)
	return TRUE;

    strcpy (xbeAuthBuf, xbeAuthTempl);
    mask = umask (0077);
    authFd = mkstemp (xbeAuthBuf);
    umask (mask);
    if (authFd == -1)
	FatalError ("Failed to generate unique authorization file\n");

    xbeAuth = xbeAuthBuf;

    if (index && index < argc)
    {
        xbeProg = argv[index];
    }
    else
    {
        struct stat buf;
	int         i;

	for (i = 0; i < sizeof (xbeProgs) / sizeof (char *); i++)
	{
	    if (stat (xbeProgs[i], &buf) == 0)
	    {
		xbeProg = xbeProgs[i];
		break;
	    }
	}

	if (!xbeProg)
            FatalError ("Can't find X server executable\n");
    }

    if (!dmxAddXbeArguments (&xbeProg, 1))
	return FALSE;

    if (!dmxAddXbeArguments (auth, sizeof (auth) / sizeof (char *)))
	return FALSE;

    xbeDisplay = xbeDisplayBuf;
    sprintf (xbeDisplay, ":%d", xbeDisplayOffset + atoi (display));
    
    if (index)
    {
	int i;

	for (i = index + 1; i < argc; i++)
	{
	    if (*argv[i] == ':')
	    {
		xbeDisplay = argv[i];
		break;
	    }
	}

	if (i >= argc)
	    if (!dmxAddXbeArguments (&xbeDisplay, 1))
		return FALSE;

        if (argc > index)
            if (!dmxAddXbeArguments (&argv[index + 1], argc - index))
                return FALSE;
    }
    else
    {
	if (!dmxAddXbeArguments (&xbeDisplay, 1))
	    return FALSE;

        if (!dmxAddXbeArguments (defs, sizeof (defs) / sizeof (char *)))
            return FALSE;
    }

    if (vt)
	if (!dmxAddXbeArguments (&vt, 1))
	    return FALSE;

    if (!dmxAddXbeArguments (&endArg, 1))
	return FALSE;

    name = basename (xbeProg);

    if (!dmxSetupAuth (xbeAuth, authFd))
	FatalError ("Failed to set up authorization: %s\n", xbeAuth);

    oldSigUsr1 = signal (SIGUSR1, sigUsr1Waiting);

    pid = fork ();

    switch (pid) {
    case -1:
	perror ("fork");
	FatalError ("fork");
	break;
    case 0:
	signal (SIGUSR1, SIG_IGN);
	execv (xbeArgv[0], xbeArgv);
	perror (xbeArgv[0]);
	exit (2);
	break;
    default:
	xbePid = pid;
	break;
    }

    for (;;)
    {
	int status;

	signal (SIGUSR1, sigUsr1Waiting);
	if (setjmp (jumpbuf))
	    break;

	signal (SIGUSR1, sigUsr1Jump);
	if (receivedUsr1)
	    break;

	if (waitpid (xbePid, &status, 0) != -1)
	{
	    if (WIFEXITED (status))
	    {
                FatalError ("%s died, exit status %d\n", name,
                            WEXITSTATUS (status));
	    }
	    else if (WIFSIGNALED (status))
		FatalError ("%s died, signal %d\n", name, WTERMSIG (status));
	    else
		FatalError ("%s died, dubious exit\n", name);
	}
    }

    signal (SIGUSR1, oldSigUsr1);

    return TRUE;
}