summaryrefslogtreecommitdiff
path: root/src/x-server-local.c
blob: 7c4ab8707e37c880ff5d7023b9a584e2ca17d6fb (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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
 * Copyright (C) 2010-2011 Robert Ancell.
 * Author: Robert Ancell <robert.ancell@canonical.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 3 of the License, or (at your option) any later
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <config.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <stdlib.h>

#include "x-server-local.h"
#include "configuration.h"
#include "process.h"
#include "vt.h"

typedef struct
{
    /* X server process */
    Process *x_server_process;

    /* Command to run the X server */
    gchar *command;

    /* Display number to use */
    guint display_number;

    /* Config file to use */
    gchar *config_file;

    /* Server layout to use */
    gchar *layout;

    /* Value for -seat argument */
    gchar *xdg_seat;

    /* TRUE if TCP/IP connections are allowed */
    gboolean allow_tcp;

    /* Authority file */
    gchar *authority_file;

    /* XDMCP server to connect to */
    gchar *xdmcp_server;

    /* XDMCP port to connect to */
    guint xdmcp_port;

    /* XDMCP key to use */
    gchar *xdmcp_key;

    /* TRUE when received ready signal */
    gboolean got_signal;

    /* VT to run on */
    gint vt;
    gboolean have_vt_ref;

    /* Background to set */
    gchar *background;
} XServerLocalPrivate;

static void x_server_local_logger_iface_init (LoggerInterface *iface);

G_DEFINE_TYPE_WITH_CODE (XServerLocal, x_server_local, X_SERVER_TYPE,
                         G_ADD_PRIVATE (XServerLocal)
                         G_IMPLEMENT_INTERFACE (LOGGER_TYPE, x_server_local_logger_iface_init))

static gchar *version = NULL;
static guint version_major = 0, version_minor = 0;
static GList *display_numbers = NULL;

#define XORG_VERSION_PREFIX "X.Org X Server "

static gchar *
find_version (const gchar *line)
{
    if (!g_str_has_prefix (line, XORG_VERSION_PREFIX))
        return NULL;

    return g_strdup (line + strlen (XORG_VERSION_PREFIX));
}

static const gchar *
x_server_local_get_version (void)
{
    if (version)
        return version;

    g_autofree gchar *stderr_text = NULL;
    gint exit_status;
    if (!g_spawn_command_line_sync ("X -version", NULL, &stderr_text, &exit_status, NULL))
        return NULL;
    if (exit_status == EXIT_SUCCESS)
    {
        g_auto(GStrv) lines = g_strsplit (stderr_text, "\n", -1);
        for (int i = 0; lines[i] && !version; i++)
            version = find_version (lines[i]);
    }

    g_auto(GStrv) tokens = g_strsplit (version, ".", 3);
    guint n_tokens = g_strv_length (tokens);
    version_major = n_tokens > 0 ? atoi (tokens[0]) : 0;
    version_minor = n_tokens > 1 ? atoi (tokens[1]) : 0;

    return version;
}

static gint
x_server_local_version_compare (guint major, guint minor)
{
    x_server_local_get_version ();
    if (major == version_major)
        return version_minor - minor;
    else
        return version_major - major;
}

static gboolean
display_number_in_use (guint display_number)
{
    /* See if we know we are managing a server with that number */
    for (GList *link = display_numbers; link; link = link->next)
    {
        guint number = GPOINTER_TO_UINT (link->data);
        if (number == display_number)
            return TRUE;
    }

    /* See if an X server that we don't know of has a lock on that number */
    g_autofree gchar *path = g_strdup_printf ("/tmp/.X%d-lock", display_number);
    gboolean in_use = g_file_test (path, G_FILE_TEST_EXISTS);

    /* See if that lock file is valid, ignore it if the contents are invalid or the process doesn't exist */
    g_autofree gchar *data = NULL;
    if (in_use && g_file_get_contents (path, &data, NULL, NULL))
    {
        int pid = atoi (g_strstrip (data));

        errno = 0;
        if (pid < 0 || (kill (pid, 0) < 0 && errno == ESRCH))
            in_use = FALSE;
    }

    return in_use;
}

static guint
x_server_local_get_unused_display_number (void)
{
    guint number = config_get_integer (config_get_instance (), "LightDM", "minimum-display-number");
    while (display_number_in_use (number))
        number++;

    display_numbers = g_list_append (display_numbers, GUINT_TO_POINTER (number));

    return number;
}

static void
x_server_local_release_display_number (guint display_number)
{
    for (GList *link = display_numbers; link; link = link->next)
    {
        guint number = GPOINTER_TO_UINT (link->data);
        if (number == display_number)
        {
            display_numbers = g_list_delete_link (display_numbers, link);
            return;
        }
    }
}

XServerLocal *
x_server_local_new (void)
{
    return g_object_new (X_SERVER_LOCAL_TYPE, NULL);
}

void
x_server_local_set_command (XServerLocal *server, const gchar *command)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    g_free (priv->command);
    priv->command = g_strdup (command);
}

void
x_server_local_set_vt (XServerLocal *server, gint vt)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);

    g_return_if_fail (server != NULL);

    if (priv->have_vt_ref)
        vt_unref (priv->vt);
    priv->have_vt_ref = FALSE;
    priv->vt = vt;
    if (vt > 0)
    {
        vt_ref (vt);
        priv->have_vt_ref = TRUE;
    }
}

void
x_server_local_set_config (XServerLocal *server, const gchar *path)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    g_free (priv->config_file);
    priv->config_file = g_strdup (path);
}

void
x_server_local_set_layout (XServerLocal *server, const gchar *layout)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    g_free (priv->layout);
    priv->layout = g_strdup (layout);
}

void
x_server_local_set_xdg_seat (XServerLocal *server, const gchar *xdg_seat)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    g_free (priv->xdg_seat);
    priv->xdg_seat = g_strdup (xdg_seat);
}

void
x_server_local_set_allow_tcp (XServerLocal *server, gboolean allow_tcp)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    priv->allow_tcp = allow_tcp;
}

void
x_server_local_set_xdmcp_server (XServerLocal *server, const gchar *hostname)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    g_free (priv->xdmcp_server);
    priv->xdmcp_server = g_strdup (hostname);
}

const gchar *
x_server_local_get_xdmcp_server (XServerLocal *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_val_if_fail (server != NULL, 0);
    return priv->xdmcp_server;
}

void
x_server_local_set_xdmcp_port (XServerLocal *server, guint port)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    priv->xdmcp_port = port;
}

guint
x_server_local_get_xdmcp_port (XServerLocal *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_val_if_fail (server != NULL, 0);
    return priv->xdmcp_port;
}

void
x_server_local_set_xdmcp_key (XServerLocal *server, const gchar *key)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    g_free (priv->xdmcp_key);
    priv->xdmcp_key = g_strdup (key);
    x_server_set_authority (X_SERVER (server), NULL);
}

void
x_server_local_set_background (XServerLocal *server, const gchar *background)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_if_fail (server != NULL);
    g_free (priv->background);
    priv->background = g_strdup (background);
}

static guint
x_server_local_get_display_number (XServer *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (X_SERVER_LOCAL (server));
    return priv->display_number;
}

static gint
x_server_local_get_vt (DisplayServer *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (X_SERVER_LOCAL (server));
    return priv->vt;
}

const gchar *
x_server_local_get_authority_file_path (XServerLocal *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    g_return_val_if_fail (server != NULL, 0);
    return priv->authority_file;
}

static gchar *
get_absolute_command (const gchar *command)
{
    g_auto(GStrv) tokens = g_strsplit (command, " ", 2);
    g_autofree gchar *absolute_binary = g_find_program_in_path (tokens[0]);
    gchar *absolute_command = NULL;
    if (absolute_binary)
    {
        if (tokens[1])
            absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
        else
            absolute_command = g_strdup (absolute_binary);
    }

    return absolute_command;
}

static void
x_server_local_run (Process *process, gpointer user_data)
{
    /* Make input non-blocking */
    int fd = open ("/dev/null", O_RDONLY);
    dup2 (fd, STDIN_FILENO);
    close (fd);

    /* Set SIGUSR1 to ignore so the X server can indicate it when it is ready */
    signal (SIGUSR1, SIG_IGN);
}

static ProcessRunFunc
x_server_local_get_run_function (XServerLocal *server)
{
    return x_server_local_run;
}

static gboolean
x_server_local_get_log_stdout (XServerLocal *server)
{
    return TRUE;
}

static void
got_signal_cb (Process *process, int signum, XServerLocal *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);

    if (signum == SIGUSR1 && !priv->got_signal)
    {
        priv->got_signal = TRUE;
        l_debug (server, "Got signal from X server :%d", priv->display_number);

        // FIXME: Check return value
        DISPLAY_SERVER_CLASS (x_server_local_parent_class)->start (DISPLAY_SERVER (server));
    }
}

static void
stopped_cb (Process *process, XServerLocal *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);

    l_debug (server, "X server stopped");

    /* Release VT and display number for re-use */
    if (priv->have_vt_ref)
    {
        vt_unref (priv->vt);
        priv->have_vt_ref = FALSE;
    }
    x_server_local_release_display_number (priv->display_number);

    if (x_server_get_authority (X_SERVER (server)) && priv->authority_file)
    {
        l_debug (server, "Removing X server authority %s", priv->authority_file);

        g_unlink (priv->authority_file);

        g_free (priv->authority_file);
        priv->authority_file = NULL;
    }

    DISPLAY_SERVER_CLASS (x_server_local_parent_class)->stop (DISPLAY_SERVER (server));
}

static void
write_authority_file (XServerLocal *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);

    XAuthority *authority = x_server_get_authority (X_SERVER (server));
    if (!authority)
        return;

    /* Get file to write to if have authority */
    if (!priv->authority_file)
    {
        g_autofree gchar *run_dir = NULL;
        g_autofree gchar *dir = NULL;

        run_dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
        dir = g_build_filename (run_dir, "root", NULL);
        if (g_mkdir_with_parents (dir, S_IRWXU) < 0)
            l_warning (server, "Failed to make authority directory %s: %s", dir, strerror (errno));

        priv->authority_file = g_build_filename (dir, x_server_get_address (X_SERVER (server)), NULL);
    }

    l_debug (server, "Writing X server authority to %s", priv->authority_file);

    g_autoptr(GError) error = NULL;
    x_authority_write (authority, XAUTH_WRITE_MODE_REPLACE, priv->authority_file, &error);
    if (error)
        l_warning (server, "Failed to write authority: %s", error->message);
}

static gboolean
x_server_local_start (DisplayServer *display_server)
{
    XServerLocal *server = X_SERVER_LOCAL (display_server);
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);

    g_return_val_if_fail (priv->x_server_process == NULL, FALSE);

    priv->got_signal = FALSE;

    g_return_val_if_fail (priv->command != NULL, FALSE);

    ProcessRunFunc run_cb = X_SERVER_LOCAL_GET_CLASS (server)->get_run_function (server);
    priv->x_server_process = process_new (run_cb, server);
    process_set_clear_environment (priv->x_server_process, TRUE);
    g_signal_connect (priv->x_server_process, PROCESS_SIGNAL_GOT_SIGNAL, G_CALLBACK (got_signal_cb), server);
    g_signal_connect (priv->x_server_process, PROCESS_SIGNAL_STOPPED, G_CALLBACK (stopped_cb), server);

    /* Setup logging */
    g_autofree gchar *filename = g_strdup_printf ("x-%d.log", x_server_get_display_number (X_SERVER (server)));
    g_autofree gchar *dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
    g_autofree gchar *log_file = g_build_filename (dir, filename, NULL);
    gboolean backup_logs = config_get_boolean (config_get_instance (), "LightDM", "backup-logs");
    process_set_log_file (priv->x_server_process, log_file, X_SERVER_LOCAL_GET_CLASS (server)->get_log_stdout (server), backup_logs ? LOG_MODE_BACKUP_AND_TRUNCATE : LOG_MODE_APPEND);
    l_debug (display_server, "Logging to %s", log_file);

    g_autofree gchar *absolute_command = get_absolute_command (priv->command);
    if (!absolute_command)
    {
        l_debug (display_server, "Can't launch X server %s, not found in path", priv->command);
        stopped_cb (priv->x_server_process, X_SERVER_LOCAL (server));
        return FALSE;
    }
    g_autoptr(GString) command = g_string_new (absolute_command);

    g_string_append_printf (command, " :%d", priv->display_number);

    if (priv->config_file)
        g_string_append_printf (command, " -config %s", priv->config_file);

    if (priv->layout)
        g_string_append_printf (command, " -layout %s", priv->layout);

    if (priv->xdg_seat)
        g_string_append_printf (command, " -seat %s", priv->xdg_seat);

    write_authority_file (server);
    if (priv->authority_file)
        g_string_append_printf (command, " -auth %s", priv->authority_file);

    /* Connect to a remote server using XDMCP */
    if (priv->xdmcp_server != NULL)
    {
        if (priv->xdmcp_port != 0)
            g_string_append_printf (command, " -port %d", priv->xdmcp_port);
        g_string_append_printf (command, " -query %s", priv->xdmcp_server);
        if (priv->xdmcp_key)
            g_string_append_printf (command, " -cookie %s", priv->xdmcp_key);
    }
    else if (priv->allow_tcp)
    {
        if (x_server_local_version_compare (1, 17) >= 0)
            g_string_append (command, " -listen tcp");
    }
    else
        g_string_append (command, " -nolisten tcp");

    if (priv->vt >= 0)
        g_string_append_printf (command, " vt%d -novtswitch", priv->vt);

    if (priv->background)
        g_string_append_printf (command, " -background %s", priv->background);

    /* Allow sub-classes to add arguments */
    if (X_SERVER_LOCAL_GET_CLASS (server)->add_args)
        X_SERVER_LOCAL_GET_CLASS (server)->add_args (server, command);

    process_set_command (priv->x_server_process, command->str);

    l_debug (display_server, "Launching X Server");

    /* If running inside another display then pass through those variables */
    if (g_getenv ("DISPLAY"))
    {
        process_set_env (priv->x_server_process, "DISPLAY", g_getenv ("DISPLAY"));
        if (g_getenv ("XAUTHORITY"))
            process_set_env (priv->x_server_process, "XAUTHORITY", g_getenv ("XAUTHORITY"));
        else
        {
            g_autofree gchar *path = g_build_filename (g_get_home_dir (), ".Xauthority", NULL);
            process_set_env (priv->x_server_process, "XAUTHORITY", path);
        }
    }

    /* Pass through library variables */
    if (g_getenv ("LD_PRELOAD"))
        process_set_env (priv->x_server_process, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
    if (g_getenv ("LD_LIBRARY_PATH"))
        process_set_env (priv->x_server_process, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
    if (g_getenv ("PATH"))
        process_set_env (priv->x_server_process, "PATH", g_getenv ("PATH"));

    /* Variable required for regression tests */
    if (g_getenv ("LIGHTDM_TEST_ROOT"))
        process_set_env (priv->x_server_process, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));

    gboolean result = process_start (priv->x_server_process, FALSE);
    if (result)
        l_debug (display_server, "Waiting for ready signal from X server :%d", priv->display_number);
    else
        stopped_cb (priv->x_server_process, X_SERVER_LOCAL (server));

    return result;
}

static void
x_server_local_stop (DisplayServer *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (X_SERVER_LOCAL (server));
    process_stop (priv->x_server_process);
}

static void
x_server_local_init (XServerLocal *server)
{
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    priv->vt = -1;
    priv->command = g_strdup ("X");
    priv->display_number = x_server_local_get_unused_display_number ();
}

static void
x_server_local_finalize (GObject *object)
{
    XServerLocal *self = X_SERVER_LOCAL (object);
    XServerLocalPrivate *priv = x_server_local_get_instance_private (self);

    if (priv->x_server_process)
        g_signal_handlers_disconnect_matched (priv->x_server_process, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, self);
    g_clear_object (&priv->x_server_process);
    g_clear_pointer (&priv->command, g_free);
    g_clear_pointer (&priv->config_file, g_free);
    g_clear_pointer (&priv->layout, g_free);
    g_clear_pointer (&priv->xdg_seat, g_free);
    g_clear_pointer (&priv->xdmcp_server, g_free);
    g_clear_pointer (&priv->xdmcp_key, g_free);
    g_clear_pointer (&priv->authority_file, g_free);
    if (priv->have_vt_ref)
        vt_unref (priv->vt);
    g_clear_pointer (&priv->background, g_free);

    G_OBJECT_CLASS (x_server_local_parent_class)->finalize (object);
}

static void
x_server_local_class_init (XServerLocalClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
    XServerClass *x_server_class = X_SERVER_CLASS (klass);
    DisplayServerClass *display_server_class = DISPLAY_SERVER_CLASS (klass);

    klass->get_run_function = x_server_local_get_run_function;
    klass->get_log_stdout = x_server_local_get_log_stdout;
    x_server_class->get_display_number = x_server_local_get_display_number;
    display_server_class->get_vt = x_server_local_get_vt;
    display_server_class->start = klass->start = x_server_local_start;
    display_server_class->stop = x_server_local_stop;
    object_class->finalize = x_server_local_finalize;
}

static gint
x_server_local_real_logprefix (Logger *self, gchar *buf, gulong buflen)
{
    XServerLocal *server = X_SERVER_LOCAL (self);
    XServerLocalPrivate *priv = x_server_local_get_instance_private (server);
    return g_snprintf (buf, buflen, "XServer %d: ", priv->display_number);
}

static void
x_server_local_logger_iface_init (LoggerInterface *iface)
{
    iface->logprefix = &x_server_local_real_logprefix;
}