2 * Copyright (C) 2012-2013 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
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
15 #include <glib/gstdio.h>
17 #include "seat-unity.h"
18 #include "configuration.h"
19 #include "x-server-local.h"
20 #include "mir-server.h"
28 USC_MESSAGE_READY = 2,
29 USC_MESSAGE_SESSION_CONNECTED = 3,
30 USC_MESSAGE_SET_ACTIVE_SESSION = 4,
31 USC_MESSAGE_SET_NEXT_SESSION = 5,
34 struct SeatUnityPrivate
36 /* VT we are running on */
42 /* Filename of Mir socket */
43 gchar *mir_socket_filename;
45 /* Pipes to communicate with compositor */
46 int to_compositor_pipe[2];
47 int from_compositor_pipe[2];
49 /* IO channel listening on for messages from the compositor */
50 GIOChannel *from_compositor_channel;
51 guint from_compositor_watch;
53 /* TRUE when the compositor indicates it is ready */
54 gboolean compositor_ready;
56 /* Buffer reading from channel */
58 gsize read_buffer_length;
59 gsize read_buffer_n_used;
61 /* Compositor process */
62 Process *compositor_process;
64 /* Timeout when waiting for compositor to start */
65 guint compositor_timeout;
67 /* Next Mir ID to use for a compositor client */
70 /* TRUE if using VT switching fallback */
71 gboolean use_vt_switching;
73 /* The currently visible session */
74 Session *active_session;
75 DisplayServer *active_display_server;
78 G_DEFINE_TYPE (SeatUnity, seat_unity, SEAT_TYPE);
81 seat_unity_get_start_local_sessions (Seat *seat)
83 return !seat_get_string_property (seat, "xdmcp-manager");
87 seat_unity_setup (Seat *seat)
89 seat_set_can_switch (seat, TRUE);
90 SEAT_CLASS (seat_unity_parent_class)->setup (seat);
94 compositor_stopped_cb (Process *process, SeatUnity *seat)
96 if (seat->priv->compositor_timeout != 0)
97 g_source_remove (seat->priv->compositor_timeout);
98 seat->priv->compositor_timeout = 0;
100 /* Finished with the VT */
101 vt_unref (seat->priv->vt);
104 if (seat_get_is_stopping (SEAT (seat)))
106 SEAT_CLASS (seat_unity_parent_class)->stop (SEAT (seat));
110 /* If stopped before it was ready, then revert to VT mode */
111 if (!seat->priv->compositor_ready)
113 l_debug (seat, "Compositor failed to start, switching to VT mode");
114 seat->priv->use_vt_switching = TRUE;
115 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
119 l_debug (seat, "Stopping Unity seat, compositor terminated");
121 seat_stop (SEAT (seat));
125 compositor_run_cb (Process *process, SeatUnity *seat)
129 /* Make input non-blocking */
130 fd = open ("/dev/null", O_RDONLY);
131 dup2 (fd, STDIN_FILENO);
134 /* Redirect output to logfile */
135 if (seat->priv->log_file)
140 /* Move old file out of the way */
141 old_filename = g_strdup_printf ("%s.old", seat->priv->log_file);
142 rename (seat->priv->log_file, old_filename);
143 g_free (old_filename);
145 /* Create new file and log to it */
146 fd = g_open (seat->priv->log_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
148 l_warning (seat, "Failed to open log file %s: %s", seat->priv->log_file, g_strerror (errno));
151 dup2 (fd, STDOUT_FILENO);
152 dup2 (fd, STDERR_FILENO);
159 write_message (SeatUnity *seat, guint16 id, const guint8 *payload, guint16 payload_length)
162 gsize data_length = 4 + payload_length;
164 data = g_malloc (data_length);
167 data[2] = payload_length >> 8;
168 data[3] = payload_length & 0xFF;
169 memcpy (data + 4, payload, payload_length);
172 if (write (seat->priv->to_compositor_pipe[1], data, data_length) != data_length)
173 l_warning (seat, "Failed to write to compositor: %s", strerror (errno));
177 read_cb (GIOChannel *source, GIOCondition condition, gpointer data)
179 SeatUnity *seat = data;
181 guint16 id, payload_length;
184 if (condition == G_IO_HUP)
186 l_debug (seat, "Compositor closed communication channel");
190 /* Work out how much required for a message */
191 if (seat->priv->read_buffer_n_used < 4)
192 n_to_read = 4 - seat->priv->read_buffer_n_used;
195 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
196 n_to_read = 4 + payload_length - seat->priv->read_buffer_n_used;
199 /* Read from compositor */
202 gsize n_total, n_read = 0;
204 GError *error = NULL;
206 n_total = seat->priv->read_buffer_n_used + n_to_read;
207 if (seat->priv->read_buffer_length < n_total)
208 seat->priv->read_buffer = g_realloc (seat->priv->read_buffer, n_total);
210 status = g_io_channel_read_chars (source,
211 (gchar *)seat->priv->read_buffer + seat->priv->read_buffer_n_used,
216 l_warning (seat, "Failed to read from compositor: %s", error->message);
217 if (status != G_IO_STATUS_NORMAL)
219 g_clear_error (&error);
220 seat->priv->read_buffer_n_used += n_read;
224 if (seat->priv->read_buffer_n_used < 4)
226 id = seat->priv->read_buffer[0] << 8 | seat->priv->read_buffer[1];
227 payload_length = seat->priv->read_buffer[2] << 8 | seat->priv->read_buffer[3];
230 if (seat->priv->read_buffer_n_used < 4 + payload_length)
232 /*payload = seat->priv->read_buffer + 4;*/
236 case USC_MESSAGE_PING:
237 l_debug (seat, "PING!");
238 write_message (seat, USC_MESSAGE_PONG, NULL, 0);
240 case USC_MESSAGE_PONG:
241 l_debug (seat, "PONG!");
243 case USC_MESSAGE_READY:
244 l_debug (seat, "READY");
245 if (!seat->priv->compositor_ready)
247 seat->priv->compositor_ready = TRUE;
248 l_debug (seat, "Compositor ready");
249 g_source_remove (seat->priv->compositor_timeout);
250 seat->priv->compositor_timeout = 0;
251 SEAT_CLASS (seat_unity_parent_class)->start (SEAT (seat));
254 case USC_MESSAGE_SESSION_CONNECTED:
255 l_debug (seat, "SESSION CONNECTED");
258 l_warning (seat, "Ignoring unknown message %d with %d octets from system compositor", id, payload_length);
263 seat->priv->read_buffer_n_used = 0;
269 get_absolute_command (const gchar *command)
272 gchar *absolute_binary, *absolute_command = NULL;
274 tokens = g_strsplit (command, " ", 2);
276 absolute_binary = g_find_program_in_path (tokens[0]);
280 absolute_command = g_strjoin (" ", absolute_binary, tokens[1], NULL);
282 absolute_command = g_strdup (absolute_binary);
283 g_free (absolute_binary);
286 absolute_command = g_strdup (command);
290 return absolute_command;
294 compositor_timeout_cb (gpointer data)
296 SeatUnity *seat = data;
298 /* Stop the compositor - it is not working */
299 process_stop (seat->priv->compositor_process);
305 seat_unity_start (Seat *seat)
307 const gchar *compositor_command;
308 gchar *command, *absolute_command, *dir, *value;
312 /* Replace Plymouth if it is running */
313 if (plymouth_get_is_active () && plymouth_has_active_vt ())
315 gint active_vt = vt_get_active ();
316 if (active_vt >= vt_get_min ())
318 SEAT_UNITY (seat)->priv->vt = active_vt;
319 plymouth_quit (TRUE);
322 l_debug (seat, "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 ());
324 if (plymouth_get_is_active ())
325 plymouth_quit (FALSE);
326 if (SEAT_UNITY (seat)->priv->vt < 0)
327 SEAT_UNITY (seat)->priv->vt = vt_get_unused ();
328 if (SEAT_UNITY (seat)->priv->vt < 0)
330 l_debug (seat, "Failed to get a VT to run on");
333 vt_ref (SEAT_UNITY (seat)->priv->vt);
335 /* Create pipes to talk to compositor */
336 if (pipe (SEAT_UNITY (seat)->priv->to_compositor_pipe) < 0 || pipe (SEAT_UNITY (seat)->priv->from_compositor_pipe) < 0)
338 l_debug (seat, "Failed to create compositor pipes: %s", g_strerror (errno));
342 /* Don't allow the daemon end of the pipes to be accessed in the compositor */
343 fcntl (SEAT_UNITY (seat)->priv->to_compositor_pipe[1], F_SETFD, FD_CLOEXEC);
344 fcntl (SEAT_UNITY (seat)->priv->from_compositor_pipe[0], F_SETFD, FD_CLOEXEC);
346 /* Listen for messages from the compositor */
347 SEAT_UNITY (seat)->priv->from_compositor_channel = g_io_channel_unix_new (SEAT_UNITY (seat)->priv->from_compositor_pipe[0]);
348 SEAT_UNITY (seat)->priv->from_compositor_watch = g_io_add_watch (SEAT_UNITY (seat)->priv->from_compositor_channel, G_IO_IN | G_IO_HUP, read_cb, seat);
351 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
352 SEAT_UNITY (seat)->priv->log_file = g_build_filename (dir, "unity-system-compositor.log", NULL);
353 l_debug (seat, "Logging to %s", SEAT_UNITY (seat)->priv->log_file);
356 /* Setup environment */
357 process_set_clear_environment (SEAT_UNITY (seat)->priv->compositor_process, TRUE);
358 process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "XDG_SEAT", "seat0");
359 value = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->vt);
360 process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "XDG_VTNR", value);
362 /* Variable required for regression tests */
363 if (g_getenv ("LIGHTDM_TEST_ROOT"))
365 process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LIGHTDM_TEST_ROOT", g_getenv ("LIGHTDM_TEST_ROOT"));
366 process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LD_PRELOAD", g_getenv ("LD_PRELOAD"));
367 process_set_env (SEAT_UNITY (seat)->priv->compositor_process, "LD_LIBRARY_PATH", g_getenv ("LD_LIBRARY_PATH"));
370 SEAT_UNITY (seat)->priv->mir_socket_filename = g_strdup ("/tmp/mir_socket"); // FIXME: Use this socket by default as XMir is hardcoded to this
371 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
372 compositor_command = seat_get_string_property (seat, "unity-compositor-command");
373 command = g_strdup_printf ("%s --file '%s' --from-dm-fd %d --to-dm-fd %d --vt %d", compositor_command, SEAT_UNITY (seat)->priv->mir_socket_filename, SEAT_UNITY (seat)->priv->to_compositor_pipe[0], SEAT_UNITY (seat)->priv->from_compositor_pipe[1], SEAT_UNITY (seat)->priv->vt);
375 absolute_command = get_absolute_command (command);
378 /* Start the compositor */
379 process_set_command (SEAT_UNITY (seat)->priv->compositor_process, absolute_command);
380 g_free (absolute_command);
381 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "stopped", G_CALLBACK (compositor_stopped_cb), seat);
382 g_signal_connect (SEAT_UNITY (seat)->priv->compositor_process, "run", G_CALLBACK (compositor_run_cb), seat);
383 result = process_start (SEAT_UNITY (seat)->priv->compositor_process, FALSE);
385 /* Close compostor ends of the pipes */
386 close (SEAT_UNITY (seat)->priv->to_compositor_pipe[0]);
387 SEAT_UNITY (seat)->priv->to_compositor_pipe[0] = 0;
388 close (SEAT_UNITY (seat)->priv->from_compositor_pipe[1]);
389 SEAT_UNITY (seat)->priv->from_compositor_pipe[1] = 0;
394 /* Connect to the compositor */
395 timeout = seat_get_integer_property (seat, "unity-compositor-timeout");
398 l_debug (seat, "Waiting for system compositor for %ds", timeout);
399 SEAT_UNITY (seat)->priv->compositor_timeout = g_timeout_add (timeout * 1000, compositor_timeout_cb, seat);
404 static DisplayServer *
405 create_x_server (Seat *seat)
407 XServerLocal *x_server;
408 const gchar *command = NULL, *layout = NULL, *config_file = NULL, *xdmcp_manager = NULL, *key_name = NULL, *xdg_seat = NULL;
412 l_debug (seat, "Starting X server on Unity compositor");
414 x_server = x_server_local_new ();
416 command = seat_get_string_property (seat, "xserver-command");
418 x_server_local_set_command (x_server, command);
420 if (SEAT_UNITY (seat)->priv->use_vt_switching)
421 x_server_local_set_vt (x_server, vt_get_unused ());
426 id = g_strdup_printf ("%d", SEAT_UNITY (seat)->priv->next_id);
427 SEAT_UNITY (seat)->priv->next_id++;
428 x_server_local_set_mir_id (x_server, id);
429 x_server_local_set_mir_socket (x_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
433 layout = seat_get_string_property (seat, "xserver-layout");
435 x_server_local_set_layout (x_server, layout);
437 xdg_seat = seat_get_string_property (seat, "xdg-seat");
439 x_server_local_set_xdg_seat (x_server, xdg_seat);
441 config_file = seat_get_string_property (seat, "xserver-config");
443 x_server_local_set_config (x_server, config_file);
445 allow_tcp = seat_get_boolean_property (seat, "xserver-allow-tcp");
446 x_server_local_set_allow_tcp (x_server, allow_tcp);
448 xdmcp_manager = seat_get_string_property (seat, "xdmcp-manager");
450 x_server_local_set_xdmcp_server (x_server, xdmcp_manager);
452 port = seat_get_integer_property (seat, "xdmcp-port");
454 x_server_local_set_xdmcp_port (x_server, port);
456 key_name = seat_get_string_property (seat, "xdmcp-key");
462 GError *error = NULL;
464 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
465 path = g_build_filename (dir, "keys.conf", NULL);
468 keys = g_key_file_new ();
469 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
471 l_debug (seat, "Error getting key %s", error->message);
472 g_clear_error (&error);
478 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
479 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
481 l_debug (seat, "Key %s not defined", key_name);
484 x_server_local_set_xdmcp_key (x_server, key);
489 g_key_file_free (keys);
492 return DISPLAY_SERVER (x_server);
495 static DisplayServer *
496 create_mir_server (Seat *seat, gboolean is_greeter)
498 MirServer *mir_server;
500 mir_server = mir_server_new ();
501 mir_server_set_parent_socket (mir_server, SEAT_UNITY (seat)->priv->mir_socket_filename);
503 if (SEAT_UNITY (seat)->priv->use_vt_switching)
504 mir_server_set_vt (mir_server, vt_get_unused ());
509 // If we're a greeter, mark our ID that way, so USC can treat it specially
510 id = g_strdup_printf ("%s%d", is_greeter ? "greeter-" : "", SEAT_UNITY (seat)->priv->next_id);
511 mir_server_set_id (mir_server, id);
514 // Now set display name for logging and internal use
515 id = g_strdup_printf ("mir-%d", SEAT_UNITY (seat)->priv->next_id);
516 display_server_set_name (DISPLAY_SERVER (mir_server), id);
519 SEAT_UNITY (seat)->priv->next_id++;
522 return DISPLAY_SERVER (mir_server);
525 static DisplayServer *
526 seat_unity_create_display_server (Seat *seat, const gchar *session_type, gboolean is_greeter)
528 if (strcmp (session_type, "x") == 0)
529 return create_x_server (seat);
530 else if (strcmp (session_type, "mir") == 0)
531 return create_mir_server (seat, is_greeter);
534 l_warning (seat, "Can't create unsupported display server '%s'", session_type);
540 seat_unity_create_greeter_session (Seat *seat)
542 Greeter *greeter_session;
543 const gchar *xdg_seat;
546 greeter_session = SEAT_CLASS (seat_unity_parent_class)->create_greeter_session (seat);
547 xdg_seat = seat_get_string_property (seat, "xdg-seat");
550 l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
551 session_set_env (SESSION (greeter_session), "XDG_SEAT", xdg_seat);
553 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
554 vt = SEAT_UNITY (seat)->priv->vt;
558 gchar *value = g_strdup_printf ("%d", vt);
559 l_debug (seat, "Setting XDG_VTNR=%s", value);
560 session_set_env (SESSION (greeter_session), "XDG_VTNR", value);
564 l_debug (seat, "Not setting XDG_VTNR");
566 return greeter_session;
570 seat_unity_create_session (Seat *seat)
573 const gchar *xdg_seat;
576 session = SEAT_CLASS (seat_unity_parent_class)->create_session (seat);
577 xdg_seat = seat_get_string_property (seat, "xdg-seat");
580 l_debug (seat, "Setting XDG_SEAT=%s", xdg_seat);
581 session_set_env (session, "XDG_SEAT", xdg_seat);
583 if (!SEAT_UNITY (seat)->priv->use_vt_switching)
584 vt = SEAT_UNITY (seat)->priv->vt;
588 gchar *value = g_strdup_printf ("%d", vt);
589 l_debug (seat, "Setting XDG_VTNR=%s", value);
590 session_set_env (SESSION (session), "XDG_VTNR", value);
594 l_debug (seat, "Not setting XDG_VTNR");
600 seat_unity_set_active_session (Seat *seat, Session *session)
602 DisplayServer *display_server;
604 /* If no compositor, have to use VT switching */
605 if (SEAT_UNITY (seat)->priv->use_vt_switching)
607 gint vt = display_server_get_vt (session_get_display_server (session));
611 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
615 if (session == SEAT_UNITY (seat)->priv->active_session)
617 SEAT_UNITY (seat)->priv->active_session = g_object_ref (session);
619 display_server = session_get_display_server (session);
620 if (SEAT_UNITY (seat)->priv->active_display_server != display_server)
622 const gchar *id = NULL;
624 SEAT_UNITY (seat)->priv->active_display_server = g_object_ref (display_server);
626 if (IS_X_SERVER_LOCAL (display_server))
627 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
628 else if (IS_MIR_SERVER (display_server))
629 id = mir_server_get_id (MIR_SERVER (display_server));
633 l_debug (seat, "Switching to Mir session %s", id);
634 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_ACTIVE_SESSION, (const guint8 *) id, strlen (id));
637 l_warning (seat, "Failed to work out session ID");
640 SEAT_CLASS (seat_unity_parent_class)->set_active_session (seat, session);
644 seat_unity_get_active_session (Seat *seat)
646 if (SEAT_UNITY (seat)->priv->use_vt_switching)
650 vt = vt_get_active ();
654 for (link = seat_get_sessions (seat); link; link = link->next)
656 Session *session = link->data;
657 if (display_server_get_vt (session_get_display_server (session)) == vt)
664 return SEAT_UNITY (seat)->priv->active_session;
668 seat_unity_set_next_session (Seat *seat, Session *session)
670 DisplayServer *display_server;
671 const gchar *id = NULL;
676 /* If no compositor, don't worry about it */
677 if (SEAT_UNITY (seat)->priv->use_vt_switching)
680 display_server = session_get_display_server (session);
682 if (IS_X_SERVER_LOCAL (display_server))
683 id = x_server_local_get_mir_id (X_SERVER_LOCAL (display_server));
684 else if (IS_MIR_SERVER (display_server))
685 id = mir_server_get_id (MIR_SERVER (display_server));
689 l_debug (seat, "Marking Mir session %s as the next session", id);
690 write_message (SEAT_UNITY (seat), USC_MESSAGE_SET_NEXT_SESSION, (const guint8 *) id, strlen (id));
694 l_debug (seat, "Failed to work out session ID to mark");
697 SEAT_CLASS (seat_unity_parent_class)->set_next_session (seat, session);
701 seat_unity_run_script (Seat *seat, DisplayServer *display_server, Process *script)
704 XServerLocal *x_server;
706 x_server = X_SERVER_LOCAL (display_server);
707 path = x_server_local_get_authority_file_path (x_server);
708 process_set_env (script, "DISPLAY", x_server_get_address (X_SERVER (x_server)));
709 process_set_env (script, "XAUTHORITY", path);
711 SEAT_CLASS (seat_unity_parent_class)->run_script (seat, display_server, script);
715 seat_unity_stop (Seat *seat)
717 /* Stop the compositor first */
718 if (process_get_is_running (SEAT_UNITY (seat)->priv->compositor_process))
720 process_stop (SEAT_UNITY (seat)->priv->compositor_process);
724 SEAT_CLASS (seat_unity_parent_class)->stop (seat);
728 seat_unity_init (SeatUnity *seat)
730 seat->priv = G_TYPE_INSTANCE_GET_PRIVATE (seat, SEAT_UNITY_TYPE, SeatUnityPrivate);
732 seat->priv->compositor_process = process_new ();
736 seat_unity_finalize (GObject *object)
738 SeatUnity *seat = SEAT_UNITY (object);
740 if (seat->priv->vt >= 0)
741 vt_unref (seat->priv->vt);
742 g_free (seat->priv->log_file);
743 g_free (seat->priv->mir_socket_filename);
744 close (seat->priv->to_compositor_pipe[0]);
745 close (seat->priv->to_compositor_pipe[1]);
746 close (seat->priv->from_compositor_pipe[0]);
747 close (seat->priv->from_compositor_pipe[1]);
748 g_io_channel_unref (seat->priv->from_compositor_channel);
749 g_source_remove (seat->priv->from_compositor_watch);
750 g_free (seat->priv->read_buffer);
751 g_object_unref (seat->priv->compositor_process);
752 if (seat->priv->active_session)
753 g_object_unref (seat->priv->active_session);
754 if (seat->priv->active_display_server)
755 g_object_unref (seat->priv->active_display_server);
757 G_OBJECT_CLASS (seat_unity_parent_class)->finalize (object);
761 seat_unity_class_init (SeatUnityClass *klass)
763 GObjectClass *object_class = G_OBJECT_CLASS (klass);
764 SeatClass *seat_class = SEAT_CLASS (klass);
766 object_class->finalize = seat_unity_finalize;
767 seat_class->get_start_local_sessions = seat_unity_get_start_local_sessions;
768 seat_class->setup = seat_unity_setup;
769 seat_class->start = seat_unity_start;
770 seat_class->create_display_server = seat_unity_create_display_server;
771 seat_class->create_greeter_session = seat_unity_create_greeter_session;
772 seat_class->create_session = seat_unity_create_session;
773 seat_class->set_active_session = seat_unity_set_active_session;
774 seat_class->get_active_session = seat_unity_get_active_session;
775 seat_class->set_next_session = seat_unity_set_next_session;
776 seat_class->run_script = seat_unity_run_script;
777 seat_class->stop = seat_unity_stop;
779 g_type_class_add_private (klass, sizeof (SeatUnityPrivate));