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