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