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