]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
Half baked code before merge with trunk
[sojka/lightdm.git] / src / seat-unity.c
1 /*
2  * Copyright (C) 2012-2013 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11
12 #include <string.h>
13 #include <fcntl.h>
14 #include <errno.h>
15
16 #include "seat-unity.h"
17 #include "configuration.h"
18 #include "xserver-local.h"
19 #include "xsession.h"
20 #include "mir-server.h"
21 #include "vt.h"
22 #include "plymouth.h"
23
24 typedef enum
25 {
26    USC_MESSAGE_PING = 0,
27    USC_MESSAGE_PONG = 1,
28    USC_MESSAGE_READY = 2,
29    USC_MESSAGE_SESSION_CONNECTED = 3,
30    USC_MESSAGE_SET_ACTIVE_SESSION = 4
31 } USCMessageID;
32
33 struct SeatUnityPrivate
34 {
35     /* VT we are running on */
36     gint vt;
37
38     /* TRUE if waiting for X server to start before stopping Plymouth */
39     gboolean stopping_plymouth;
40
41     /* File to log to */
42     gchar *log_file;
43
44     /* Filename of Mir socket */
45     gchar *mir_socket_filename;
46
47     /* Pipes to communicate with compositor */
48     int to_compositor_pipe[2];
49     int from_compositor_pipe[2];
50
51     /* IO channel listening on for messages from the compositor */
52     GIOChannel *from_compositor_channel;
53
54     /* TRUE when the compositor indicates it is ready */
55     gboolean compositor_ready;
56
57     /* Buffer reading from channel */
58     guint8 *read_buffer;
59     gsize read_buffer_length;
60     gsize read_buffer_n_used;
61
62     /* Compositor process */
63     Process *compositor_process;
64
65     /* Timeout when waiting for compositor to start */
66     guint compositor_timeout;
67
68     /* Next Mir ID to use for a compositor client */
69     gint next_id;
70
71     /* TRUE if using VT switching fallback */
72     gboolean use_vt_switching;
73
74     /* The currently visible display */
75     Display *active_display;
76 };
77
78 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
79
80 static void
81 seat_unity_setup (Seat *seat)
82 {
83     seat_set_can_switch (seat, TRUE);
84     SEAT_CLASS (seat_unity_parent_class)->setup (seat);
85 }
86
87 static void
88 compositor_stopped_cb (Process *process, SeatUnity *seat)
89 {
90     if (seat->priv->compositor_timeout != 0)
91         g_source_remove (seat->priv->compositor_timeout);
92     seat->priv->compositor_timeout = 0;
93
94     if (seat_get_is_stopping (SEAT (seat)))
95     {
96         SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
97         return;
98     }
99
100     /* If stopped before it was ready, then revert to VT mode */
101     if (!seat->priv->compositor_ready)
102     {
103         g_debug ("Compositor failed to start, switching to VT mode");
104         seat->priv->use_vt_switching = TRUE;
105         SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
106         return;
107     }
108
109     g_debug ("Stopping Unity seat, compositor terminated");
110
111     if (seat->priv->stopping_plymouth)
112     {
113         g_debug ("Stopping Plymouth, compositor failed to start");
114         plymouth_quit (FALSE);
115         seat->priv->stopping_plymouth = FALSE;
116     }
117
118     seat_stop (SEAT (seat));
119 }
120
121 static void
122 compositor_run_cb (Process *process, SeatUnity *seat)
123 {
124     int fd;
125
126     /* Make input non-blocking */
127     fd = open ("/dev/null", O_RDONLY);
128     dup2 (fd, STDIN_FILENO);
129     close (fd);
130
131     /* Redirect output to logfile */
132     if (seat->priv->log_file)
133     {
134          int fd;
135
136          fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
137          if (fd < 0)
138              g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
139          else
140          {
141              dup2 (fd, STDOUT_FILENO);
142              dup2 (fd, STDERR_FILENO);
143              close (fd);
144          }
145     }
146 }
147
148 static void
149 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
150 {
151     guint8 *data;
152     gsize data_length = 4 + payload_length;
153
154     data = g_malloc (data_length);
155     data[0] = id >> 8;
156     data[1] = id & 0xFF;
157     data[2] = payload_length >> 8;
158     data[3] = payload_length & 0xFF;
159     memcpy (data + 4, payload, payload_length);
160
161     errno = 0;
162     if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
163         g_warning ("Failed to write to compositor: %s", strerror (errno));
164 }
165
166 static gboolean
167 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
168 {
169     SeatUnity *seat = data;
170     gsize n_to_read = 0;
171     guint16 id, payload_length;
172     guint8 *payload;
173   
174     if (condition == G_IO_HUP)
175     {
176         g_debug ("Compositor closed communication channel");
177         return FALSE;
178     }
179
180     /* Work out how much required for a message */
181     if (seat->priv->read_buffer_n_used < 4)
182         n_to_read = 4 - seat->priv->read_buffer_n_used;
183     else
184     {
185         payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
186         n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
187     }
188
189     /* Read from compositor */
190     if (n_to_read > 0)
191     {
192         gsize n_total, n_read = 0;
193         GIOStatus status;
194         GError *error = NULL;
195
196         n_total = seat->priv->read_buffer_n_used + n_to_read;
197         if (seat->priv->read_buffer_length < n_total)
198             seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
199
200         status = g_io_channel_read_chars (source,
201                                           seat->priv->read_buffer + seat->priv->read_buffer_n_used,
202                                           n_to_read,
203                                           &n_read,
204                                           &error);
205         if (error)
206             g_warning ("Failed to read from compositor: %s", error->message);
207         if (status != G_IO_STATUS_NORMAL)
208             return TRUE;
209         g_clear_error (&error);
210         seat->priv->read_buffer_n_used += n_read;
211     }
212
213     /* Read header */
214     if (seat->priv->read_buffer_n_used < 4)
215          return TRUE;
216     id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
217     payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
218
219     /* Read payload */
220     if (seat->priv->read_buffer_n_used < 4 + payload_length)
221         return TRUE;
222     payload = seat->priv->read_buffer + 4;
223
224     switch (id)
225     {
226     case USC_MESSAGE_PING:
227         g_debug ("PING!");
228         write_message (seat, USC_MESSAGE_PONG, NULL, 0);
229         break;
230     case USC_MESSAGE_PONG:
231         g_debug ("PONG!");
232         break;
233     case USC_MESSAGE_READY:
234         g_debug ("READY");
235         if (!seat->priv->compositor_ready)
236         {
237             seat->priv->compositor_ready = TRUE;
238             g_debug ("Compositor ready");
239             g_source_remove (seat->priv->compositor_timeout);
240             seat->priv->compositor_timeout = 0;
241             SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
242         }
243         break;
244     case USC_MESSAGE_SESSION_CONNECTED:
245         g_debug ("SESSION CONNECTED");
246         break;
247     default:
248         g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
249         break;
250     }
251
252     /* Clear buffer */
253     seat->priv->read_buffer_n_used = 0;
254
255     return TRUE;
256 }
257
258 static gchar *
259 get_absolute_command (const gchar *command)
260 {
261     gchar **tokens;
262     gchar *absolute_binary, *absolute_command = NULL;
263
264     tokens = g_strsplit (command, " ", 2);
265
266     absolute_binary = g_find_program_in_path (tokens[0]);
267     if (absolute_binary)
268     {
269         if (tokens[1])
270             absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
271         else
272             absolute_command = g_strdup (absolute_binary);
273         g_free (absolute_binary);
274     }
275     else
276         absolute_command = g_strdup (command);
277
278     g_strfreev (tokens);
279
280     return absolute_command;
281 }
282
283 static gboolean
284 compositor_timeout_cb (gpointer data)
285 {
286     SeatUnity *seat = data;
287
288     /* Stop the compositor - it is not working */
289     process_stop (seat->priv->compositor_process);
290
291     return TRUE;
292 }
293
294 static gboolean
295 seat_unity_start (Seat *seat)
296 {
297     const gchar *compositor_command;
298     gchar *command, *absolute_command, *dir;
299     gboolean result;
300     int timeout;
301
302     /* Replace Plymouth if it is running */
303     if (plymouth_get_is_active () && plymouth_has_active_vt ())
304     {
305         gint active_vt = vt_get_active ();
306         if (active_vt >= vt_get_min ())
307         {
308             g_debug ("Compositor will replace Plymouth");
309             SEAT_UNITY (seat)->priv->vt = active_vt;
310             plymouth_deactivate ();
311         }
312         else
313             g_debug ("Plymouth is running on VT %d, but this is less than the configured minimum of %d so not replacing it", active_vt, vt_get_min ());
314     }
315     if (SEAT_UNITY (seat)->priv->vt < 0)
316         SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
317     if (SEAT_UNITY (seat)->priv->vt < 0)
318     {
319         g_debug ("Failed to get a VT to run on");
320         return FALSE;
321     }
322     vt_ref (SEAT_UNITY (seat)->priv->vt);
323
324     /* Create pipes to talk to compositor */
325     if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
326     {
327         g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
328         return FALSE;
329     }
330
331     /* Don't allow the daemon end of the pipes to be accessed in the compositor */
332     fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
333     fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
334
335     /* Listen for messages from the compositor */
336     SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
337     g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
338
339     /* Setup logging */
340     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
341     SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
342     g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
343     g_free (dir);
344
345     SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
346     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
347     compositor_command = seat_get_string_property (seat, "unity-compositor-command");
348     command = g_strdup_printf ("%s --from-dm-fd %d --to-dm-fd %d --vt %d", compositor_command, SEAT_UNITY (seat)->priv->to_compositor_pipe[0], SEAT_UNITY (seat)->priv->from_compositor_pipe[1], SEAT_UNITY (seat)->priv->vt);
349
350     absolute_command = get_absolute_command (command);
351     g_free (command);
352
353     /* Start the compositor */
354     process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
355     g_free (absolute_command);
356     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
357     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
358     result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
359
360     /* Close compostor ends of the pipes */
361     close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
362     SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
363     close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
364     SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
365
366     if (!result)
367         return FALSE;
368
369     /* Connect to the compositor */
370     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
371     if (timeout <= 0)
372         timeout = 60;
373     g_debug ("Waiting for system compositor for %ds", timeout);
374     SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
375
376     return TRUE;
377 }
378
379 static DisplayServer *
380 create_x_server (Seat *seat)
381 {
382     XServerLocal *xserver;
383     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
384     gboolean allow_tcp;
385     gint port = 0;
386     gchar *id;
387
388     g_debug ("Starting X server on Unity compositor");
389
390     xserver = xserver_local_new ();
391
392     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
393     {
394         id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
395         SEAT_UNITY (seat)->priv->next_id++;
396         xserver_local_set_mir_id (xserver, id);
397         xserver_local_set_mir_socket (xserver, SEAT_UNITY (seat)->priv->mir_socket_filename);
398         g_free (id);
399     }
400
401     command = seat_get_string_property (seat, "xserver-command");
402     if (command)
403         xserver_local_set_command (xserver, command);
404
405     layout = seat_get_string_property (seat, "xserver-layout");
406     if (layout)
407         xserver_local_set_layout (xserver, layout);
408
409     config_file = seat_get_string_property (seat, "xserver-config");
410     if (config_file)
411         xserver_local_set_config (xserver, config_file);
412
413     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
414     xserver_local_set_allow_tcp (xserver, allow_tcp);
415
416     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
417     if (xdmcp_manager)
418         xserver_local_set_xdmcp_server (xserver, xdmcp_manager);
419
420     port = seat_get_integer_property (seat, "xdmcp-port");
421     if (port > 0)
422         xserver_local_set_xdmcp_port (xserver, port);
423
424     key_name = seat_get_string_property (seat, "xdmcp-key");
425     if (key_name)
426     {
427         gchar *dir, *path;
428         GKeyFile *keys;
429         gboolean result;
430         GError *error = NULL;
431
432         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
433         path = g_build_filename (dir, "keys.conf", NULL);
434         g_free (dir);
435
436         keys = g_key_file_new ();
437         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
438         if (error)
439             g_debug ("Error getting key %s", error->message);
440         g_clear_error (&error);
441
442         if (result)
443         {
444             gchar *key = NULL;
445
446             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
447                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
448             else
449                 g_debug ("Key %s not defined", key_name);
450
451             if (key)
452                 xserver_local_set_xdmcp_key (xserver, key);
453             g_free (key);
454         }
455
456         g_free (path);
457         g_key_file_free (keys);
458     }
459
460     return DISPLAY_SERVER (xserver);
461 }
462
463 static DisplayServer *
464 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
465 {
466     if (strcmp (session_type, "x") == 0)
467         return create_x_server (seat);
468     else if (strcmp (session_type, "mir") == 0)
469         return DISPLAY_SERVER (mir_server_new ());
470     else
471     {
472         g_warning ("Can't create unsupported display server '%s'", session_type);
473         return NULL;
474     }
475 }
476
477 static Session *
478 seat_unity_create_session (Seat *seat, Display *display)
479 {
480     XServerLocal *xserver;
481     XSession *session;
482     int vt_number;
483     gchar *t;
484
485     xserver = XSERVER_LOCAL (display_get_display_server (display));
486
487     if (SEAT_UNITY (seat)->priv->use_vt_switching)
488         vt_number = xserver_local_get_vt (xserver);
489     else
490         vt_number = SEAT_UNITY (seat)->priv->vt;
491
492     session = xsession_new ();
493     t = g_strdup_printf ("/dev/tty%d", vt_number);
494     session_set_tty (SESSION (session), t);
495     g_free (t);
496
497     /* Set variables for logind */
498     session_set_env (SESSION (session), "XDG_SEAT", "seat0");
499     t = g_strdup_printf ("%d", vt_number);
500     session_set_env (SESSION (session), "XDG_VTNR", t);
501     g_free (t);
502
503     return SESSION (session);
504 }
505
506 static void
507 seat_unity_set_active_display (Seat *seat, Display *display)
508 {
509     XServerLocal *xserver;
510     const gchar *id;
511
512     /* If no compositor, have to use VT switching */
513     if (SEAT_UNITY (seat)->priv->use_vt_switching)
514     {
515         gint vt = xserver_local_get_vt (XSERVER_LOCAL (display_get_display_server (display)));
516         if (vt >= 0)
517             vt_set_active (vt);
518
519         SEAT_CLASS (seat_unity_parent_class)->set_active_display (seat, display);
520         return;
521     }
522
523     if (display == SEAT_UNITY (seat)->priv->active_display)
524         return;
525     SEAT_UNITY (seat)->priv->active_display = display;
526
527     xserver = XSERVER_LOCAL (display_get_display_server (display));
528     id = xserver_local_get_mir_id (xserver);
529
530     g_debug ("Switching to Mir session %s", id);
531     write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, id, strlen (id));
532
533     SEAT_CLASS (seat_unity_parent_class)->set_active_display (seat, display);
534 }
535
536 static Display *
537 seat_unity_get_active_display (Seat *seat)
538 {
539     if (SEAT_UNITY (seat)->priv->use_vt_switching)
540     {
541         gint vt;
542         GList *link;
543         vt = vt_get_active ();
544         if (vt < 0)
545             return NULL;
546
547         for (link = seat_get_displays (seat); link; link = link->next)
548         {
549             Display *display = link->data;
550             XServerLocal *xserver;
551
552             xserver = XSERVER_LOCAL (display_get_display_server (display));
553             if (xserver_local_get_vt (xserver) == vt)
554                 return display;
555         }
556
557         return NULL;
558     }
559
560     return SEAT_UNITY (seat)->priv->active_display;
561 }
562
563 static void
564 seat_unity_run_script (Seat *seat, Display *display, Process *script)
565 {
566     const gchar *path;
567     XServerLocal *xserver;
568
569     xserver = XSERVER_LOCAL (display_get_display_server (display));
570     path = xserver_local_get_authority_file_path (xserver);
571     process_set_env (script, "DISPLAY", xserver_get_address (XSERVER (xserver)));
572     process_set_env (script, "XAUTHORITY", path);
573
574     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display, script);
575 }
576
577 static void
578 seat_unity_stop (Seat *seat)
579 {
580     /* Stop the compositor first */
581     if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
582     {
583         process_stop (SEAT_UNITY (seat)->priv->compositor_process);
584         return;
585     }
586
587     SEAT_CLASS (seat_unity_parent_class)->stop (seat);
588 }
589
590 static void
591 seat_unity_display_removed (Seat *seat, Display *display)
592 {
593     if (seat_get_is_stopping (seat))
594         return;
595
596     /* If this is the only display and it failed to start then stop this seat */
597     if (g_list_length (seat_get_displays (seat)) == 0 && !display_get_is_ready (display))
598     {
599         g_debug ("Stopping Unity seat, failed to start a display");
600         seat_stop (seat);
601         return;
602     }
603
604     /* Show a new greeter */
605     if (display == seat_get_active_display (seat))
606     {
607         g_debug ("Active display stopped, switching to greeter");
608         seat_switch_to_greeter (seat);
609     }
610 }
611
612 static void
613 seat_unity_init (SeatUnity *seat)
614 {
615     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
616     seat->priv->vt = -1;
617     seat->priv->compositor_process = process_new ();
618 }
619
620 static void
621 seat_unity_finalize (GObject *object)
622 {
623     SeatUnity *seat = SEAT_UNITY (object);
624
625     if (seat->priv->vt >= 0)
626         vt_unref (seat->priv->vt);
627     g_free (seat->priv->log_file);
628     g_free (seat->priv->mir_socket_filename);
629     close (seat->priv->to_compositor_pipe[0]);
630     close (seat->priv->to_compositor_pipe[1]);
631     close (seat->priv->from_compositor_pipe[0]);
632     close (seat->priv->from_compositor_pipe[1]);
633     g_io_channel_unref (seat->priv->from_compositor_channel);
634     g_free (seat->priv->read_buffer);
635     g_object_unref (seat->priv->compositor_process);
636
637     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
638 }
639
640 static void
641 seat_unity_class_init (SeatUnityClass *klass)
642 {
643     GObjectClass *object_class = G_OBJECT_CLASS (klass);
644     SeatClass *seat_class = SEAT_CLASS (klass);
645
646     object_class->finalize = seat_unity_finalize;
647     seat_class->setup = seat_unity_setup;
648     seat_class->start = seat_unity_start;
649     seat_class->create_display_server = seat_unity_create_display_server;
650     seat_class->create_session = seat_unity_create_session;
651     seat_class->set_active_display = seat_unity_set_active_display;
652     seat_class->get_active_display = seat_unity_get_active_display;
653     seat_class->run_script = seat_unity_run_script;
654     seat_class->stop = seat_unity_stop;
655     seat_class->display_removed = seat_unity_display_removed;
656
657     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
658 }