]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/seat-unity.c
Hold the VT reference inside the X server.
[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 "vt.h"
21 #include "plymouth.h"
22
23 typedef enum
24 {
25    USC_MESSAGE_PING = 0,
26    USC_MESSAGE_PONG = 1,
27    USC_MESSAGE_READY = 2,
28    USC_MESSAGE_SESSION_CONNECTED = 3,
29    USC_MESSAGE_SET_ACTIVE_SESSION = 4
30 } USCMessageID;
31
32 struct SeatUnityPrivate
33 {
34     /* VT we are running on */
35     gint vt;
36
37     /* File to log to */
38     gchar *log_file;
39
40     /* Filename of Mir socket */
41     gchar *mir_socket_filename;
42
43     /* Pipes to communicate with compositor */
44     int to_compositor_pipe[2];
45     int from_compositor_pipe[2];
46
47     /* IO channel listening on for messages from the compositor */
48     GIOChannel *from_compositor_channel;
49
50     /* TRUE when the compositor indicates it is ready */
51     gboolean compositor_ready;
52
53     /* Buffer reading from channel */
54     guint8 *read_buffer;
55     gsize read_buffer_length;
56     gsize read_buffer_n_used;
57
58     /* Compositor process */
59     Process *compositor_process;
60
61     /* Timeout when waiting for compositor to start */
62     guint compositor_timeout;
63
64     /* Next Mir ID to use for a compositor client */
65     gint next_id;
66
67     /* TRUE if using VT switching fallback */
68     gboolean use_vt_switching;
69
70     /* The currently visible session */
71     Session *active_session;
72     DisplayServer *active_display_server;
73 };
74
75 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
76
77 static gboolean
78 seat_unity_get_start_local_sessions (Seat *seat)
79 {
80     return !seat_get_string_property (seat, "xdmcp-manager");
81 }
82
83 static void
84 seat_unity_setup (Seat *seat)
85 {
86     seat_set_can_switch (seat, TRUE);
87     SEAT_CLASS (seat_unity_parent_class)->setup (seat);
88 }
89
90 static void
91 compositor_stopped_cb (Process *process, SeatUnity *seat)
92 {
93     if (seat->priv->compositor_timeout != 0)
94         g_source_remove (seat->priv->compositor_timeout);
95     seat->priv->compositor_timeout = 0;
96
97     /* Finished with the VT */
98     vt_unref (seat->priv->vt);
99     seat->priv->vt = -1;
100
101     if (seat_get_is_stopping (SEAT (seat)))
102     {
103         SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
104         return;
105     }
106
107     /* If stopped before it was ready, then revert to VT mode */
108     if (!seat->priv->compositor_ready)
109     {
110         g_debug ("Compositor failed to start, switching to VT mode");
111         seat->priv->use_vt_switching = TRUE;
112         SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
113         return;
114     }
115
116     g_debug ("Stopping Unity seat, compositor terminated");
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                                           (gchar *)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             SEAT_UNITY (seat)->priv->vt = active_vt;
309             plymouth_quit (TRUE);
310         }
311         else
312             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 ());
313     }
314     if (plymouth_get_is_active ())
315         plymouth_quit (FALSE);
316     if (SEAT_UNITY (seat)->priv->vt < 0)
317         SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
318     if (SEAT_UNITY (seat)->priv->vt < 0)
319     {
320         g_debug ("Failed to get a VT to run on");
321         return FALSE;
322     }
323     vt_ref (SEAT_UNITY (seat)->priv->vt);
324
325     /* Create pipes to talk to compositor */
326     if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
327     {
328         g_debug ("Failed to create compositor pipes: %s", g_strerror (errno));
329         return FALSE;
330     }
331
332     /* Don't allow the daemon end of the pipes to be accessed in the compositor */
333     fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
334     fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
335
336     /* Listen for messages from the compositor */
337     SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
338     g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
339
340     /* Setup logging */
341     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
342     SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
343     g_debug ("Logging to %s", SEAT_UNITY (seat)->priv->log_file);
344     g_free (dir);
345
346     SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
347     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
348     compositor_command = seat_get_string_property (seat, "unity-compositor-command");
349     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);
350
351     absolute_command = get_absolute_command (command);
352     g_free (command);
353
354     /* Start the compositor */
355     process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
356     g_free (absolute_command);
357     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
358     g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
359     result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
360
361     /* Close compostor ends of the pipes */
362     close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
363     SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
364     close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
365     SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
366
367     if (!result)
368         return FALSE;
369
370     /* Connect to the compositor */
371     timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
372     if (timeout <= 0)
373         timeout = 60;
374     g_debug ("Waiting for system compositor for %ds", timeout);
375     SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
376
377     return TRUE;
378 }
379
380 static DisplayServer *
381 seat_unity_create_display_server (Seat *seat)
382 {
383     XServerLocal *x_server;
384     const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL;
385     gboolean allow_tcp;
386     gint port = 0;
387     gchar *id;
388
389     g_debug ("Starting X server on Unity compositor");
390
391     x_server = x_server_local_new ();
392
393     if (!SEAT_UNITY (seat)->priv->use_vt_switching)
394     {
395         id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
396         SEAT_UNITY (seat)->priv->next_id++;
397         x_server_local_set_mir_id (x_server, id);
398         x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
399         g_free (id);
400     }
401
402     command = seat_get_string_property (seat, "xserver-command");
403     if (command)
404         x_server_local_set_command (x_server, command);
405
406     if (SEAT_UNITY (seat)->priv->use_vt_switching)
407         x_server_local_set_vt (x_server, vt_get_unused ());
408
409     layout = seat_get_string_property (seat, "xserver-layout");
410     if (layout)
411         x_server_local_set_layout (x_server, layout);
412
413     config_file = seat_get_string_property (seat, "xserver-config");
414     if (config_file)
415         x_server_local_set_config (x_server, config_file);
416
417     allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
418     x_server_local_set_allow_tcp (x_server, allow_tcp);
419
420     xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
421     if (xdmcp_manager)
422         x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
423
424     port = seat_get_integer_property (seat, "xdmcp-port");
425     if (port > 0)
426         x_server_local_set_xdmcp_port (x_server, port);
427
428     key_name = seat_get_string_property (seat, "xdmcp-key");
429     if (key_name)
430     {
431         gchar *dir, *path;
432         GKeyFile *keys;
433         gboolean result;
434         GError *error = NULL;
435
436         dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
437         path = g_build_filename (dir, "keys.conf", NULL);
438         g_free (dir);
439
440         keys = g_key_file_new ();
441         result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
442         if (error)
443             g_debug ("Error getting key %s", error->message);
444         g_clear_error (&error);
445
446         if (result)
447         {
448             gchar *key = NULL;
449
450             if (g_key_file_has_key (keys, "keyring", key_name, NULL))
451                 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
452             else
453                 g_debug ("Key %s not defined", key_name);
454
455             if (key)
456                 x_server_local_set_xdmcp_key (x_server, key);
457             g_free (key);
458         }
459
460         g_free (path);
461         g_key_file_free (keys);
462     }
463
464     return DISPLAY_SERVER (x_server);
465 }
466
467 static Greeter *
468 seat_unity_create_greeter_session (Seat *seat)
469 {
470     Greeter *greeter_session;
471
472     greeter_session = greeter_new ();
473     session_set_env (SESSION (greeter_session), "XDG_SEAT", "seat0");
474
475     return greeter_session;
476 }
477
478 static Session *
479 seat_unity_create_session (Seat *seat)
480 {
481     Session *session;
482
483     session = session_new ();
484     session_set_env (session, "XDG_SEAT", "seat0");
485
486     return session;
487 }
488
489 static void
490 seat_unity_set_active_session (Seat *seat, Session *session)
491 {
492     DisplayServer *display_server;
493
494     /* If no compositor, have to use VT switching */
495     if (SEAT_UNITY (seat)->priv->use_vt_switching)
496     {
497         gint vt = display_server_get_vt (session_get_display_server (session));
498         if (vt >= 0)
499             vt_set_active (vt);
500
501         SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
502         return;
503     }
504
505     if (session == SEAT_UNITY (seat)->priv->active_session)
506         return;
507     SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
508
509     display_server = session_get_display_server (session);
510     if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
511     {
512         const gchar *id;
513
514         SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
515         id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
516
517         g_debug ("Switching to Mir session %s", id);
518         write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
519     }
520
521     SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
522 }
523
524 static Session *
525 seat_unity_get_active_session (Seat *seat)
526 {
527     if (SEAT_UNITY (seat)->priv->use_vt_switching)
528     {
529         gint vt;
530         GList *link;
531         vt = vt_get_active ();
532         if (vt < 0)
533             return NULL;
534
535         for (link = seat_get_sessions (seat); link; link = link->next)
536         {
537             Session *session = link->data;
538             if (display_server_get_vt (session_get_display_server (session)) == vt)
539                 return session;
540         }
541
542         return NULL;
543     }
544
545     return SEAT_UNITY (seat)->priv->active_session;
546 }
547
548 static void
549 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
550 {
551     const gchar *path;
552     XServerLocal *x_server;
553
554     x_server = X_SERVER_LOCAL (display_server);
555     path = x_server_local_get_authority_file_path (x_server);
556     process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
557     process_set_env (script, "XAUTHORITY", path);
558
559     SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
560 }
561
562 static void
563 seat_unity_stop (Seat *seat)
564 {
565     /* Stop the compositor first */
566     if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
567     {
568         process_stop (SEAT_UNITY (seat)->priv->compositor_process);
569         return;
570     }
571
572     SEAT_CLASS (seat_unity_parent_class)->stop (seat);
573 }
574
575 static void
576 seat_unity_init (SeatUnity *seat)
577 {
578     seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
579     seat->priv->vt = -1;
580     seat->priv->compositor_process = process_new ();
581 }
582
583 static void
584 seat_unity_finalize (GObject *object)
585 {
586     SeatUnity *seat = SEAT_UNITY (object);
587
588     if (seat->priv->vt >= 0)
589         vt_unref (seat->priv->vt);
590     g_free (seat->priv->log_file);
591     g_free (seat->priv->mir_socket_filename);
592     close (seat->priv->to_compositor_pipe[0]);
593     close (seat->priv->to_compositor_pipe[1]);
594     close (seat->priv->from_compositor_pipe[0]);
595     close (seat->priv->from_compositor_pipe[1]);
596     g_io_channel_unref (seat->priv->from_compositor_channel);
597     g_free (seat->priv->read_buffer);
598     g_object_unref (seat->priv->compositor_process);
599     if (seat->priv->active_session)
600         g_object_unref (seat->priv->active_session);
601     if (seat->priv->active_display_server)
602         g_object_unref (seat->priv->active_display_server);
603
604     G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
605 }
606
607 static void
608 seat_unity_class_init (SeatUnityClass *klass)
609 {
610     GObjectClass *object_class = G_OBJECT_CLASS (klass);
611     SeatClass *seat_class = SEAT_CLASS (klass);
612
613     object_class->finalize = seat_unity_finalize;
614     seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
615     seat_class->setup = seat_unity_setup;
616     seat_class->start = seat_unity_start;
617     seat_class->create_display_server = seat_unity_create_display_server;
618     seat_class->create_greeter_session = seat_unity_create_greeter_session;
619     seat_class->create_session = seat_unity_create_session;
620     seat_class->set_active_session = seat_unity_set_active_session;
621     seat_class->get_active_session = seat_unity_get_active_session;
622     seat_class->run_script = seat_unity_run_script;
623     seat_class->stop = seat_unity_stop;
624
625     g_type_class_add_private (klass, sizeof (SeatUnityPrivate));
626 }