]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
Merge in X module renaming changes
[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 #include <glib/gstdio.h>
16
17 #include "seat-unity.h"
18 #include "configuration.h"
19 #include "x-server-local.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 session */
75     Session *active_session;
76     DisplayServer *active_display_server;
77 };
78
79 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
80
81 static gboolean
82 seat_unity_get_start_local_sessions (Seat *seat)
83 {
84     return !seat_get_string_property (seat, "xdmcp-manager");
85 }
86
87 static void
88 seat_unity_setup (Seat *seat)
89 {
90     seat_set_can_switch (seat, TRUE);
91     SEAT_CLASS (seat_unity_parent_class)->setup (seat);
92 }
93
94 static void
95 compositor_stopped_cb (Process *process, SeatUnity *seat)
96 {
97     if (seat->priv->compositor_timeout != 0)
98         g_source_remove (seat->priv->compositor_timeout);
99     seat->priv->compositor_timeout = 0;
100
101     /* Finished with the VT */
102     vt_unref (seat->priv->vt);
103     seat->priv->vt = -1;
104
105     if (seat_get_is_stopping (SEAT (seat)))
106     {
107         SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
108         return;
109     }
110
111     /* If stopped before it was ready, then revert to VT mode */
112     if (!seat->priv->compositor_ready)
113     {
114         g_debug ("Compositor failed to start, switching to VT mode");
115         seat->priv->use_vt_switching = TRUE;
116         SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
117         return;
118     }
119
120     g_debug ("Stopping Unity seat, compositor terminated");
121
122     if (seat->priv->stopping_plymouth)
123     {
124         g_debug ("Stopping Plymouth, compositor failed to start");
125         plymouth_quit (FALSE);
126         seat->priv->stopping_plymouth = FALSE;
127     }
128
129     seat_stop (SEAT (seat));
130 }
131
132 static void
133 compositor_run_cb (Process *process, SeatUnity *seat)
134 {
135     int fd;
136
137     /* Make input non-blocking */
138     fd = open ("/dev/null", O_RDONLY);
139     dup2 (fd, STDIN_FILENO);
140     close (fd);
141
142     /* Redirect output to logfile */
143     if (seat->priv->log_file)
144     {
145          int fd;
146
147          fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
148          if (fd < 0)
149              g_warning ("Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
150          else
151          {
152              dup2 (fd, STDOUT_FILENO);
153              dup2 (fd, STDERR_FILENO);
154              close (fd);
155          }
156     }
157
158     if (seat->priv->stopping_plymouth)
159     {      
160         seat->priv->stopping_plymouth = FALSE;
161         plymouth_quit (TRUE);
162     }  
163 }
164
165 static void
166 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
167 {
168     guint8 *data;
169     gsize data_length = 4 + payload_length;
170
171     data = g_malloc (data_length);
172     data[0] = id >> 8;
173     data[1] = id & 0xFF;
174     data[2] = payload_length >> 8;
175     data[3] = payload_length & 0xFF;
176     memcpy (data + 4, payload, payload_length);
177
178     errno = 0;
179     if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
180         g_warning ("Failed to write to compositor: %s", strerror (errno));
181 }
182
183 static gboolean
184 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
185 {
186     SeatUnity *seat = data;
187     gsize n_to_read = 0;
188     guint16 id, payload_length;
189     /*guint8 *payload;*/
190   
191     if (condition == G_IO_HUP)
192     {
193         g_debug ("Compositor closed communication channel");
194         return FALSE;
195     }
196
197     /* Work out how much required for a message */
198     if (seat->priv->read_buffer_n_used < 4)
199         n_to_read = 4 - seat->priv->read_buffer_n_used;
200     else
201     {
202         payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
203         n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
204     }
205
206     /* Read from compositor */
207     if (n_to_read > 0)
208     {
209         gsize n_total, n_read = 0;
210         GIOStatus status;
211         GError *error = NULL;
212
213         n_total = seat->priv->read_buffer_n_used + n_to_read;
214         if (seat->priv->read_buffer_length < n_total)
215             seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
216
217         status = g_io_channel_read_chars (source,
218                                           (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
219                                           n_to_read,
220                                           &n_read,
221                                           &error);
222         if (error)
223             g_warning ("Failed to read from compositor: %s", error->message);
224         if (status != G_IO_STATUS_NORMAL)
225             return TRUE;
226         g_clear_error (&error);
227         seat->priv->read_buffer_n_used += n_read;
228     }
229
230     /* Read header */
231     if (seat->priv->read_buffer_n_used < 4)
232          return TRUE;
233     id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
234     payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
235
236     /* Read payload */
237     if (seat->priv->read_buffer_n_used < 4 + payload_length)
238         return TRUE;
239     /*payload = seat->priv->read_buffer + 4;*/
240
241     switch (id)
242     {
243     case USC_MESSAGE_PING:
244         g_debug ("PING!");
245         write_message (seat, USC_MESSAGE_PONG, NULL, 0);
246         break;
247     case USC_MESSAGE_PONG:
248         g_debug ("PONG!");
249         break;
250     case USC_MESSAGE_READY:
251         g_debug ("READY");
252         if (!seat->priv->compositor_ready)
253         {
254             seat->priv->compositor_ready = TRUE;
255             g_debug ("Compositor ready");
256             g_source_remove (seat->priv->compositor_timeout);
257             seat->priv->compositor_timeout = 0;
258             SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
259         }
260         break;
261     case USC_MESSAGE_SESSION_CONNECTED:
262         g_debug ("SESSION CONNECTED");
263         break;
264     default:
265         g_warning ("Ingoring unknown message %d with %d octets from system compositor", id, payload_length);
266         break;
267     }
268
269     /* Clear buffer */
270     seat->priv->read_buffer_n_used = 0;
271
272     return TRUE;
273 }
274
275 static gchar *
276 get_absolute_command (const gchar *command)
277 {
278     gchar **tokens;
279     gchar *absolute_binary, *absolute_command = NULL;
280
281     tokens = g_strsplit (command, " ", 2);
282
283     absolute_binary = g_find_program_in_path (tokens[0]);
284     if (absolute_binary)
285     {
286         if (tokens[1])
287             absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
288         else
289             absolute_command = g_strdup (absolute_binary);
290         g_free (absolute_binary);
291     }
292     else
293         absolute_command = g_strdup (command);
294
295     g_strfreev (tokens);
296
297     return absolute_command;
298 }
299
300 static gboolean
301 compositor_timeout_cb (gpointer data)
302 {
303     SeatUnity *seat = data;
304
305     /* Stop the compositor - it is not working */
306     process_stop (seat->priv->compositor_process);
307
308     return TRUE;
309 }
310
311 static gboolean
312 seat_unity_start (Seat *seat)
313 {
314     const gchar *compositor_command;
315     gchar *command, *absolute_command, *dir;
316     gboolean result;
317     int timeout;
318
319     /* Replace Plymouth if it is running */
320     if (plymouth_get_is_active () && plymouth_has_active_vt ())
321     {
322         gint active_vt = vt_get_active ();
323         if (active_vt >= vt_get_min ())
324         {
325             g_debug ("Compositor will replace Plymouth");
326             SEAT_UNITY (seat)->priv->stopping_plymouth = TRUE;
327             SEAT_UNITY (seat)->priv->vt = active_vt;
328             plymouth_deactivate ();
329         }
330         else
331             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 ());
332     }
333     if (SEAT_UNITY (seat)->priv->vt < 0)
334         SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
335     if (SEAT_UNITY (seat)->priv->vt < 0)
336     {
337         g_debug ("Failed to get a VT to run on");
338         return FALSE;
339     }
340     vt_ref (SEAT_UNITY (seat)->priv->vt);
341
342     /* Create pipes to talk to compositor */
343     if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
344     {
345         g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
346         return FALSE;
347     }
348
349     /* Don't allow the daemon end of the pipes to be accessed in the compositor */
350     fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
351     fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
352
353     /* Listen for messages from the compositor */
354     SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
355     g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
356
357     /* Setup logging */
358     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
359     SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
360     g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
361     g_free (dir);
362
363     SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
364     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
365     compositor_command = seat_get_string_property (seat, "unity-compositor-command");
366     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);
367
368     absolute_command = get_absolute_command (command);
369     g_free (command);
370
371     /* Start the compositor */
372     process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
373     g_free (absolute_command);
374     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
375     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
376     result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
377
378     /* Close compostor ends of the pipes */
379     close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
380     SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
381     close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
382     SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
383
384     if (!result)
385         return FALSE;
386
387     /* Connect to the compositor */
388     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
389     if (timeout <= 0)
390         timeout = 60;
391     g_debug ("Waiting for system compositor for %ds", timeout);
392     SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
393
394     return TRUE;
395 }
396
397 static DisplayServer *
398 create_x_server (Seat *seat)
399 {
400     XServerLocal *x_server;
401     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
402     gboolean allow_tcp;
403     gint port = 0;
404     gchar *id;
405
406     g_debug ("Starting X server on Unity compositor");
407
408     x_server = x_server_local_new ();
409
410     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
411     {
412         id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
413         SEAT_UNITY (seat)->priv->next_id++;
414         x_server_local_set_mir_id (x_server, id);
415         x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
416         g_free (id);
417     }
418
419     command = seat_get_string_property (seat, "xserver-command");
420     if (command)
421         x_server_local_set_command (x_server, command);
422
423     layout = seat_get_string_property (seat, "xserver-layout");
424     if (layout)
425         x_server_local_set_layout (x_server, layout);
426
427     config_file = seat_get_string_property (seat, "xserver-config");
428     if (config_file)
429         x_server_local_set_config (x_server, config_file);
430
431     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
432     x_server_local_set_allow_tcp (x_server, allow_tcp);
433
434     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
435     if (xdmcp_manager)
436         x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
437
438     port = seat_get_integer_property (seat, "xdmcp-port");
439     if (port > 0)
440         x_server_local_set_xdmcp_port (x_server, port);
441
442     key_name = seat_get_string_property (seat, "xdmcp-key");
443     if (key_name)
444     {
445         gchar *dir, *path;
446         GKeyFile *keys;
447         gboolean result;
448         GError *error = NULL;
449
450         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
451         path = g_build_filename (dir, "keys.conf", NULL);
452         g_free (dir);
453
454         keys = g_key_file_new ();
455         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
456         if (error)
457             g_debug ("Error getting key %s", error->message);
458         g_clear_error (&error);
459
460         if (result)
461         {
462             gchar *key = NULL;
463
464             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
465                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
466             else
467                 g_debug ("Key %s not defined", key_name);
468
469             if (key)
470                 x_server_local_set_xdmcp_key (x_server, key);
471             g_free (key);
472         }
473
474         g_free (path);
475         g_key_file_free (keys);
476     }
477
478     return DISPLAY_SERVER (x_server);
479 }
480
481 static DisplayServer *
482 seat_unity_create_display_server (Seat *seat, const gchar *session_type)
483 {  
484     if (strcmp (session_type, "x") == 0)
485         return create_x_server (seat);
486     else if (strcmp (session_type, "mir") == 0)
487         return DISPLAY_SERVER (mir_server_new ());
488     else
489     {
490         g_warning ("Can't create unsupported display server '%s'", session_type);
491         return NULL;
492     }
493 }
494
495 static Greeter *
496 seat_unity_create_greeter_session (Seat *seat)
497 {
498     Greeter *greeter_session;
499
500     greeter_session = greeter_new ();
501     session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
502
503     return greeter_session;
504 }
505
506 static Session *
507 seat_unity_create_session (Seat *seat)
508 {
509     Session *session;
510
511     session = session_new ();
512     session_set_env (session, "XDG_SEAT", "seat0");
513
514     return session;
515 }
516
517 static void
518 seat_unity_set_active_session (Seat *seat, Session *session)
519 {
520     DisplayServer *display_server;
521
522     /* If no compositor, have to use VT switching */
523     if (SEAT_UNITY (seat)->priv->use_vt_switching)
524     {
525         gint vt = display_server_get_vt (session_get_display_server (session));
526         if (vt >= 0)
527             vt_set_active (vt);
528
529         SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
530         return;
531     }
532
533     if (session == SEAT_UNITY (seat)->priv->active_session)
534         return;
535     SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
536
537     display_server = session_get_display_server (session);
538     if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
539     {
540         const gchar *id;
541
542         SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
543         id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
544
545         g_debug ("Switching to Mir session %s", id);
546         write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
547     }
548
549     SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
550 }
551
552 static Session *
553 seat_unity_get_active_session (Seat *seat)
554 {
555     if (SEAT_UNITY (seat)->priv->use_vt_switching)
556     {
557         gint vt;
558         GList *link;
559         vt = vt_get_active ();
560         if (vt < 0)
561             return NULL;
562
563         for (link = seat_get_sessions (seat); link; link = link->next)
564         {
565             Session *session = link->data;
566             if (display_server_get_vt (session_get_display_server (session)) == vt)
567                 return session;
568         }
569
570         return NULL;
571     }
572
573     return SEAT_UNITY (seat)->priv->active_session;
574 }
575
576 static void
577 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
578 {
579     const gchar *path;
580     XServerLocal *x_server;
581
582     x_server = X_SERVER_LOCAL (display_server);
583     path = x_server_local_get_authority_file_path (x_server);
584     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
585     process_set_env (script, "XAUTHORITY", path);
586
587     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
588 }
589
590 static void
591 seat_unity_stop (Seat *seat)
592 {
593     /* Stop the compositor first */
594     if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
595     {
596         process_stop (SEAT_UNITY (seat)->priv->compositor_process);
597         return;
598     }
599
600     SEAT_CLASS (seat_unity_parent_class)->stop (seat);
601 }
602
603 static void
604 seat_unity_init (SeatUnity *seat)
605 {
606     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
607     seat->priv->vt = -1;
608     seat->priv->compositor_process = process_new ();
609 }
610
611 static void
612 seat_unity_finalize (GObject *object)
613 {
614     SeatUnity *seat = SEAT_UNITY (object);
615
616     if (seat->priv->vt >= 0)
617         vt_unref (seat->priv->vt);
618     g_free (seat->priv->log_file);
619     g_free (seat->priv->mir_socket_filename);
620     close (seat->priv->to_compositor_pipe[0]);
621     close (seat->priv->to_compositor_pipe[1]);
622     close (seat->priv->from_compositor_pipe[0]);
623     close (seat->priv->from_compositor_pipe[1]);
624     g_io_channel_unref (seat->priv->from_compositor_channel);
625     g_free (seat->priv->read_buffer);
626     g_object_unref (seat->priv->compositor_process);
627     if (seat->priv->active_session)
628         g_object_unref (seat->priv->active_session);
629     if (seat->priv->active_display_server)
630         g_object_unref (seat->priv->active_display_server);
631
632     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
633 }
634
635 static void
636 seat_unity_class_init (SeatUnityClass *klass)
637 {
638     GObjectClass *object_class = G_OBJECT_CLASS (klass);
639     SeatClass *seat_class = SEAT_CLASS (klass);
640
641     object_class->finalize = seat_unity_finalize;
642     seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
643     seat_class->setup = seat_unity_setup;
644     seat_class->start = seat_unity_start;
645     seat_class->create_display_server = seat_unity_create_display_server;
646     seat_class->create_greeter_session = seat_unity_create_greeter_session;
647     seat_class->create_session = seat_unity_create_session;
648     seat_class->set_active_session = seat_unity_set_active_session;
649     seat_class->get_active_session = seat_unity_get_active_session;
650     seat_class->run_script = seat_unity_run_script;
651     seat_class->stop = seat_unity_stop;
652
653     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
654 }