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