2 * Copyright (C) 2010-2011 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
18 #include <glib/gi18n.h>
24 #include "configuration.h"
25 #include "display-manager.h"
26 #include "xdmcp-server.h"
27 #include "vnc-server.h"
28 #include "seat-xdmcp-session.h"
29 #include "seat-xvnc.h"
32 #include "session-child.h"
33 #include "shared-data-manager.h"
34 #include "user-list.h"
37 static gchar *config_path = NULL;
38 static GMainLoop *loop = NULL;
39 static GTimer *log_timer;
40 static int log_fd = -1;
41 static gboolean debug = FALSE;
43 static DisplayManager *display_manager = NULL;
44 static XDMCPServer *xdmcp_server = NULL;
45 static VNCServer *vnc_server = NULL;
46 static guint bus_id = 0;
47 static GDBusConnection *bus = NULL;
48 static guint reg_id = 0;
49 static GDBusNodeInfo *seat_info;
50 static GHashTable *seat_bus_entries = NULL;
51 static guint seat_index = 0;
52 static GDBusNodeInfo *session_info;
53 static GHashTable *session_bus_entries = NULL;
54 static guint session_index = 0;
55 static gint exit_code = EXIT_SUCCESS;
69 #define LIGHTDM_BUS_NAME "org.freedesktop.DisplayManager"
72 log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer data)
77 switch (log_level & G_LOG_LEVEL_MASK)
79 case G_LOG_LEVEL_ERROR:
82 case G_LOG_LEVEL_CRITICAL:
85 case G_LOG_LEVEL_WARNING:
88 case G_LOG_LEVEL_MESSAGE:
91 case G_LOG_LEVEL_INFO:
94 case G_LOG_LEVEL_DEBUG:
102 text = g_strdup_printf ("[%+.2fs] %s %s\n", g_timer_elapsed (log_timer, NULL), prefix, message);
104 /* Log everything to a file */
108 n_written = write (log_fd, text, strlen (text));
110 ; /* Check result so compiler doesn't warn about it */
113 /* Log to stderr if requested */
115 g_printerr ("%s", text);
117 g_log_default_handler (log_domain, log_level, message, data);
125 gchar *log_dir, *path, *old_path;
127 log_timer = g_timer_new ();
130 log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
131 path = g_build_filename (log_dir, "lightdm.log", NULL);
134 /* Move old file out of the way */
135 old_path = g_strdup_printf ("%s.old", path);
136 rename (path, old_path);
139 /* Create new file and log to it */
140 log_fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
141 fcntl (log_fd, F_SETFD, FD_CLOEXEC);
142 g_log_set_default_handler (log_cb, NULL);
144 g_debug ("Logging to %s", path);
149 set_seat_properties (Seat *seat, const gchar *config_section)
154 keys = config_get_keys (config_get_instance (), "SeatDefaults");
155 for (i = 0; keys[i]; i++)
157 gchar *value = config_get_string (config_get_instance (), "SeatDefaults", keys[i]);
158 seat_set_property (seat, keys[i], value);
165 keys = config_get_keys (config_get_instance (), config_section);
166 for (i = 0; keys[i]; i++)
168 gchar *value = config_get_string (config_get_instance (), config_section, keys[i]);
169 seat_set_property (seat, keys[i], value);
177 signal_cb (Process *process, int signum)
179 g_debug ("Caught %s signal, shutting down", g_strsignal (signum));
180 display_manager_stop (display_manager);
181 // FIXME: Stop XDMCP server
185 display_manager_stopped_cb (DisplayManager *display_manager)
187 g_debug ("Stopping daemon");
188 g_main_loop_quit (loop);
192 display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat)
196 Seat *next_seat = NULL;
199 /* If we have fallback types registered for the seat, let's try them
201 types = seat_get_string_list_property (seat, "type");
202 next_types = g_string_new ("");
203 for (iter = types; iter && *iter; iter++)
206 continue; // skip first one, that is our current seat type
210 next_seat = seat_new (*iter);
211 g_string_assign (next_types, *iter);
215 // Build up list of types to try next time
216 g_string_append_c (next_types, ';');
217 g_string_append (next_types, *iter);
224 const gchar *seat_name;
225 gchar *config_section = NULL;
227 seat_name = seat_get_string_property (seat, "seat-name");
229 config_section = g_strdup_printf ("Seat:%s", seat_name);
230 set_seat_properties (next_seat, config_section);
231 g_free (config_section);
233 // We set this manually on default seat. Let's port it over if needed.
234 if (seat_get_boolean_property (seat, "exit-on-failure"))
235 seat_set_property (next_seat, "exit-on-failure", "true");
237 seat_set_property (next_seat, "type", next_types->str);
239 display_manager_add_seat (display_manager, next_seat);
240 g_object_unref (next_seat);
242 else if (seat_get_boolean_property (seat, "exit-on-failure"))
244 g_debug ("Required seat has stopped");
245 exit_code = EXIT_FAILURE;
246 display_manager_stop (display_manager);
249 g_string_free (next_types, TRUE);
255 GVariantBuilder builder;
259 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
260 g_hash_table_iter_init (&iter, seat_bus_entries);
261 while (g_hash_table_iter_next (&iter, NULL, &value))
263 SeatBusEntry *entry = value;
264 g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
267 return g_variant_builder_end (&builder);
271 get_session_list (const gchar *seat_path)
273 GVariantBuilder builder;
277 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
279 g_hash_table_iter_init (&iter, session_bus_entries);
280 while (g_hash_table_iter_next (&iter, NULL, &value))
282 SessionBusEntry *entry = value;
283 if (seat_path == NULL || strcmp (entry->seat_path, seat_path) == 0)
284 g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
287 return g_variant_builder_end (&builder);
291 handle_display_manager_get_property (GDBusConnection *connection,
293 const gchar *object_path,
294 const gchar *interface_name,
295 const gchar *property_name,
299 if (g_strcmp0 (property_name, "Seats") == 0)
300 return get_seat_list ();
301 else if (g_strcmp0 (property_name, "Sessions") == 0)
302 return get_session_list (NULL);
308 handle_display_manager_call (GDBusConnection *connection,
310 const gchar *object_path,
311 const gchar *interface_name,
312 const gchar *method_name,
313 GVariant *parameters,
314 GDBusMethodInvocation *invocation,
317 if (g_strcmp0 (method_name, "AddSeat") == 0)
320 GVariantIter *property_iter;
324 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa(ss))")))
326 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
330 g_variant_get (parameters, "(&sa(ss))", &type, &property_iter);
332 g_debug ("Adding seat of type %s", type);
334 seat = seat_new (type);
337 set_seat_properties (seat, NULL);
338 while (g_variant_iter_loop (property_iter, "(&s&s)", &name, &value))
339 seat_set_property (seat, name, value);
341 g_variant_iter_free (property_iter);
345 // FIXME: Need to make proper error
346 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create seat of type %s", type);
350 if (display_manager_add_seat (display_manager, seat))
354 entry = g_hash_table_lookup (seat_bus_entries, seat);
355 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
357 else// FIXME: Need to make proper error
358 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
359 g_object_unref (seat);
361 else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
366 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
368 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
372 g_variant_get (parameters, "(i)", &display_number);
374 g_debug ("Adding local X seat :%d", display_number);
376 seat = seat_new ("xremote");
379 gchar *display_number_string;
381 set_seat_properties (seat, NULL);
382 display_number_string = g_strdup_printf ("%d", display_number);
383 seat_set_property (seat, "xserver-display-number", display_number_string);
384 g_free (display_number_string);
389 // FIXME: Need to make proper error
390 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create local X seat");
394 if (display_manager_add_seat (display_manager, seat))
398 entry = g_hash_table_lookup (seat_bus_entries, seat);
399 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
401 else// FIXME: Need to make proper error
402 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
403 g_object_unref (seat);
406 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
410 handle_seat_get_property (GDBusConnection *connection,
412 const gchar *object_path,
413 const gchar *interface_name,
414 const gchar *property_name,
418 Seat *seat = user_data;
420 if (g_strcmp0 (property_name, "CanSwitch") == 0)
421 return g_variant_new_boolean (seat_get_can_switch (seat));
422 if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
423 return g_variant_new_boolean (seat_get_allow_guest (seat));
424 else if (g_strcmp0 (property_name, "Sessions") == 0)
428 entry = g_hash_table_lookup (seat_bus_entries, seat);
429 return get_session_list (entry->path);
436 handle_seat_call (GDBusConnection *connection,
438 const gchar *object_path,
439 const gchar *interface_name,
440 const gchar *method_name,
441 GVariant *parameters,
442 GDBusMethodInvocation *invocation,
445 Seat *seat = user_data;
447 if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
449 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
450 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
452 if (seat_switch_to_greeter (seat))
453 g_dbus_method_invocation_return_value (invocation, NULL);
454 else// FIXME: Need to make proper error
455 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to greeter");
457 else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
459 const gchar *username, *session_name;
461 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
462 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
464 g_variant_get (parameters, "(&s&s)", &username, &session_name);
465 if (strcmp (session_name, "") == 0)
468 if (seat_switch_to_user (seat, username, session_name))
469 g_dbus_method_invocation_return_value (invocation, NULL);
470 else// FIXME: Need to make proper error
471 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to user");
473 else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
475 const gchar *session_name;
477 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
478 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
480 g_variant_get (parameters, "(&s)", &session_name);
481 if (strcmp (session_name, "") == 0)
484 if (seat_switch_to_guest (seat, session_name))
485 g_dbus_method_invocation_return_value (invocation, NULL);
486 else// FIXME: Need to make proper error
487 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to guest");
489 else if (g_strcmp0 (method_name, "Lock") == 0)
491 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
492 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
494 /* FIXME: Should only allow locks if have a session on this seat */
495 if (seat_lock (seat, NULL))
496 g_dbus_method_invocation_return_value (invocation, NULL);
497 else// FIXME: Need to make proper error
498 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to lock seat");
501 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
505 get_seat_for_session (Session *session)
509 for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
511 Seat *seat = seat_link->data;
514 for (session_link = seat_get_sessions (seat); session_link; session_link = session_link->next)
516 Session *s = session_link->data;
527 handle_session_get_property (GDBusConnection *connection,
529 const gchar *object_path,
530 const gchar *interface_name,
531 const gchar *property_name,
535 Session *session = user_data;
536 SessionBusEntry *entry;
538 entry = g_hash_table_lookup (session_bus_entries, session);
539 if (g_strcmp0 (property_name, "Seat") == 0)
540 return g_variant_new_object_path (entry ? entry->seat_path : "");
541 else if (g_strcmp0 (property_name, "UserName") == 0)
542 return g_variant_new_string (session_get_username (session));
548 handle_session_call (GDBusConnection *connection,
550 const gchar *object_path,
551 const gchar *interface_name,
552 const gchar *method_name,
553 GVariant *parameters,
554 GDBusMethodInvocation *invocation,
557 Session *session = user_data;
559 if (g_strcmp0 (method_name, "Lock") == 0)
563 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
564 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
566 seat = get_seat_for_session (session);
567 /* FIXME: Should only allow locks if have a session on this seat */
568 seat_lock (seat, session_get_username (session));
569 g_dbus_method_invocation_return_value (invocation, NULL);
572 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
575 static SeatBusEntry *
576 seat_bus_entry_new (const gchar *path)
580 entry = g_malloc0 (sizeof (SeatBusEntry));
581 entry->path = g_strdup (path);
586 static SessionBusEntry *
587 session_bus_entry_new (const gchar *path, const gchar *seat_path)
589 SessionBusEntry *entry;
591 entry = g_malloc0 (sizeof (SessionBusEntry));
592 entry->path = g_strdup (path);
593 entry->seat_path = g_strdup (seat_path);
599 emit_object_value_changed (GDBusConnection *bus, const gchar *path, const gchar *interface_name, const gchar *property_name, GVariant *property_value)
601 GVariantBuilder builder;
602 GError *error = NULL;
604 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
605 g_variant_builder_add (&builder, "{sv}", property_name, property_value);
607 if (!g_dbus_connection_emit_signal (bus,
610 "org.freedesktop.DBus.Properties",
612 g_variant_new ("(sa{sv}as)", interface_name, &builder, NULL),
614 g_warning ("Failed to emit PropertiesChanged signal: %s", error->message);
615 g_clear_error (&error);
619 emit_object_signal (GDBusConnection *bus, const gchar *path, const gchar *signal_name, const gchar *object_path)
621 GError *error = NULL;
623 if (!g_dbus_connection_emit_signal (bus,
626 "org.freedesktop.DisplayManager",
628 g_variant_new ("(o)", object_path),
630 g_warning ("Failed to emit %s signal on %s: %s", signal_name, path, error->message);
631 g_clear_error (&error);
635 seat_bus_entry_free (gpointer data)
637 SeatBusEntry *entry = data;
639 g_dbus_connection_unregister_object (bus, entry->bus_id);
641 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
642 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatRemoved", entry->path);
644 g_free (entry->path);
649 session_bus_entry_free (gpointer data)
651 SessionBusEntry *entry = data;
653 g_dbus_connection_unregister_object (bus, entry->bus_id);
655 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
656 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionRemoved", entry->path);
658 emit_object_value_changed (bus, entry->seat_path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (entry->seat_path));
659 emit_object_signal (bus, entry->seat_path, "SessionRemoved", entry->path);
661 g_free (entry->path);
662 g_free (entry->seat_path);
667 running_user_session_cb (Seat *seat, Session *session)
669 static const GDBusInterfaceVTable session_vtable =
672 handle_session_get_property
674 SeatBusEntry *seat_entry;
675 SessionBusEntry *session_entry;
677 GError *error = NULL;
679 /* Set environment variables when session runs */
680 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
681 session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
682 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
684 session_set_env (session, "XDG_SESSION_PATH", path);
685 g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
687 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
688 session_entry = session_bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL);
689 g_hash_table_insert (session_bus_entries, g_object_ref (session), session_entry);
691 g_debug ("Registering session with bus path %s", session_entry->path);
693 session_entry->bus_id = g_dbus_connection_register_object (bus,
695 session_info->interfaces[0],
697 g_object_ref (session), g_object_unref,
699 if (session_entry->bus_id == 0)
700 g_warning ("Failed to register user session: %s", error->message);
701 g_clear_error (&error);
703 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
704 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionAdded", session_entry->path);
706 emit_object_value_changed (bus, seat_entry->path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (session_entry->seat_path));
707 emit_object_signal (bus, seat_entry->path, "SessionAdded", session_entry->path);
711 session_removed_cb (Seat *seat, Session *session)
713 g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
714 g_hash_table_remove (session_bus_entries, session);
718 seat_added_cb (DisplayManager *display_manager, Seat *seat)
720 static const GDBusInterfaceVTable seat_vtable =
723 handle_seat_get_property
727 GError *error = NULL;
729 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
732 entry = seat_bus_entry_new (path);
734 g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
736 g_debug ("Registering seat with bus path %s", entry->path);
738 entry->bus_id = g_dbus_connection_register_object (bus,
740 seat_info->interfaces[0],
742 g_object_ref (seat), g_object_unref,
744 if (entry->bus_id == 0)
745 g_warning ("Failed to register seat: %s", error->message);
746 g_clear_error (&error);
748 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
749 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatAdded", entry->path);
751 g_signal_connect (seat, "running-user-session", G_CALLBACK (running_user_session_cb), NULL);
752 g_signal_connect (seat, "session-removed", G_CALLBACK (session_removed_cb), NULL);
756 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
758 g_hash_table_remove (seat_bus_entries, seat);
762 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
764 SeatXDMCPSession *seat;
767 seat = seat_xdmcp_session_new (session);
768 set_seat_properties (SEAT (seat), NULL);
769 result = display_manager_add_seat (display_manager, SEAT (seat));
770 g_object_unref (seat);
776 vnc_connection_cb (VNCServer *server, GSocket *connection)
780 seat = seat_xvnc_new (connection);
781 set_seat_properties (SEAT (seat), NULL);
782 display_manager_add_seat (display_manager, SEAT (seat));
783 g_object_unref (seat);
787 bus_acquired_cb (GDBusConnection *connection,
791 const gchar *display_manager_interface =
793 " <interface name='org.freedesktop.DisplayManager'>"
794 " <property name='Seats' type='ao' access='read'/>"
795 " <property name='Sessions' type='ao' access='read'/>"
796 " <method name='AddSeat'>"
797 " <arg name='type' direction='in' type='s'/>"
798 " <arg name='properties' direction='in' type='a(ss)'/>"
799 " <arg name='seat' direction='out' type='o'/>"
801 " <method name='AddLocalXSeat'>"
802 " <arg name='display-number' direction='in' type='i'/>"
803 " <arg name='seat' direction='out' type='o'/>"
805 " <signal name='SeatAdded'>"
806 " <arg name='seat' type='o'/>"
808 " <signal name='SeatRemoved'>"
809 " <arg name='seat' type='o'/>"
811 " <signal name='SessionAdded'>"
812 " <arg name='session' type='o'/>"
814 " <signal name='SessionRemoved'>"
815 " <arg name='session' type='o'/>"
819 static const GDBusInterfaceVTable display_manager_vtable =
821 handle_display_manager_call,
822 handle_display_manager_get_property
824 const gchar *seat_interface =
826 " <interface name='org.freedesktop.DisplayManager.Seat'>"
827 " <property name='CanSwitch' type='b' access='read'/>"
828 " <property name='HasGuestAccount' type='b' access='read'/>"
829 " <property name='Sessions' type='ao' access='read'/>"
830 " <method name='SwitchToGreeter'/>"
831 " <method name='SwitchToUser'>"
832 " <arg name='username' direction='in' type='s'/>"
833 " <arg name='session-name' direction='in' type='s'/>"
835 " <method name='SwitchToGuest'>"
836 " <arg name='session-name' direction='in' type='s'/>"
838 " <method name='Lock'/>"
839 " <signal name='SessionAdded'>"
840 " <arg name='session' type='o'/>"
842 " <signal name='SessionRemoved'>"
843 " <arg name='session' type='o'/>"
847 const gchar *session_interface =
849 " <interface name='org.freedesktop.DisplayManager.Session'>"
850 " <property name='Seat' type='o' access='read'/>"
851 " <property name='UserName' type='s' access='read'/>"
852 " <method name='Lock'/>"
855 GDBusNodeInfo *display_manager_info;
857 GError *error = NULL;
859 g_debug ("Acquired bus name %s", name);
863 display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
864 g_assert (display_manager_info != NULL);
865 seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
866 g_assert (seat_info != NULL);
867 session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
868 g_assert (session_info != NULL);
870 reg_id = g_dbus_connection_register_object (connection,
871 "/org/freedesktop/DisplayManager",
872 display_manager_info->interfaces[0],
873 &display_manager_vtable,
877 g_warning ("Failed to register display manager: %s", error->message);
878 g_clear_error (&error);
879 g_dbus_node_info_unref (display_manager_info);
881 seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, seat_bus_entry_free);
882 session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, session_bus_entry_free);
884 g_signal_connect (display_manager, "seat-added", G_CALLBACK (seat_added_cb), NULL);
885 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (seat_removed_cb), NULL);
886 for (link = display_manager_get_seats (display_manager); link; link = link->next)
887 seat_added_cb (display_manager, (Seat *) link->data);
889 display_manager_start (display_manager);
891 /* Start the XDMCP server */
892 if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
894 gchar *key_name, *key = NULL;
896 xdmcp_server = xdmcp_server_new ();
897 if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
900 port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
902 xdmcp_server_set_port (xdmcp_server, port);
904 g_signal_connect (xdmcp_server, "new-session", G_CALLBACK (xdmcp_session_cb), NULL);
906 key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
912 GError *error = NULL;
914 path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
916 keys = g_key_file_new ();
917 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
919 g_debug ("Error getting key %s", error->message);
920 g_clear_error (&error);
924 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
925 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
927 g_debug ("Key %s not defined", key_name);
930 g_key_file_free (keys);
933 xdmcp_server_set_key (xdmcp_server, key);
937 g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
938 xdmcp_server_start (xdmcp_server);
941 /* Start the VNC server */
942 if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
946 path = g_find_program_in_path ("Xvnc");
949 vnc_server = vnc_server_new ();
950 if (config_has_key (config_get_instance (), "VNCServer", "port"))
953 port = config_get_integer (config_get_instance (), "VNCServer", "port");
955 vnc_server_set_port (vnc_server, port);
957 g_signal_connect (vnc_server, "new-connection", G_CALLBACK (vnc_connection_cb), NULL);
959 g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
960 vnc_server_start (vnc_server);
965 g_warning ("Can't start VNC server, Xvnc is not in the path");
970 name_lost_cb (GDBusConnection *connection,
975 g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
977 g_printerr ("Failed to get D-Bus connection\n");
983 add_login1_seat (Login1Seat *login1_seat)
985 const gchar *seat_name = login1_seat_get_id (login1_seat);
987 gchar *config_section = NULL;
988 gchar **types = NULL, **type;
990 gboolean is_seat0, started = FALSE;
992 g_debug ("New seat added from logind: %s", seat_name);
993 is_seat0 = strcmp (seat_name, "seat0") == 0;
995 groups = config_get_groups (config_get_instance ());
996 for (i = groups; !config_section && *i; i++)
998 if (g_str_has_prefix (*i, "Seat:") &&
999 g_str_has_suffix (*i, seat_name))
1001 config_section = g_strdup (*i);
1005 g_strfreev (groups);
1009 g_debug ("Loading properties from config section %s", config_section);
1010 types = config_get_string_list (config_get_instance (), config_section, "type");
1014 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1015 for (type = types; !seat && type && *type; type++)
1016 seat = seat_new (*type);
1021 set_seat_properties (seat, NULL);
1023 if (!login1_seat_get_can_multi_session (login1_seat))
1025 g_debug ("Seat %s has property CanMultiSession=no", seat_name);
1026 seat_set_property (seat, "allow-user-switching", "false");
1030 set_seat_properties (seat, config_section);
1032 seat_set_property (seat, "seat-name", seat_name);
1033 seat_set_property (seat, "xdg-seat", seat_name);
1036 seat_set_property (seat, "exit-on-failure", "true");
1039 g_debug ("Unable to create seat: %s", seat_name);
1043 started = display_manager_add_seat (display_manager, seat);
1045 g_debug ("Failed to start seat: %s", seat_name);
1048 g_free (config_section);
1049 g_object_unref (seat);
1055 login1_service_seat_added_cb (Login1Service *service, Login1Seat *login1_seat)
1057 add_login1_seat (login1_seat);
1061 login1_service_seat_removed_cb (Login1Service *service, Login1Seat *login1_seat)
1065 g_debug ("Seat removed from logind: %s", login1_seat_get_id (login1_seat));
1066 seat = display_manager_get_seat (display_manager, login1_seat_get_id (login1_seat));
1072 main (int argc, char **argv)
1075 GOptionContext *option_context;
1077 gchar **groups, **i, *dir;
1078 gboolean test_mode = FALSE;
1079 gchar *pid_path = "/var/run/lightdm.pid";
1080 gchar *log_dir = NULL;
1081 gchar *run_dir = NULL;
1082 gchar *cache_dir = NULL;
1083 gchar *default_log_dir = g_strdup (LOG_DIR);
1084 gchar *default_run_dir = g_strdup (RUN_DIR);
1085 gchar *default_cache_dir = g_strdup (CACHE_DIR);
1086 gboolean show_config = FALSE, show_version = FALSE;
1087 GList *link, *messages = NULL;
1088 GOptionEntry options[] =
1090 { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
1091 /* Help string for command line --config flag */
1092 N_("Use configuration file"), "FILE" },
1093 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
1094 /* Help string for command line --debug flag */
1095 N_("Print debugging messages"), NULL },
1096 { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
1097 /* Help string for command line --test-mode flag */
1098 N_("Run as unprivileged user, skipping things that require root access"), NULL },
1099 { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
1100 /* Help string for command line --pid-file flag */
1101 N_("File to write PID into"), "FILE" },
1102 { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
1103 /* Help string for command line --log-dir flag */
1104 N_("Directory to write logs to"), "DIRECTORY" },
1105 { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
1106 /* Help string for command line --run-dir flag */
1107 N_("Directory to store running state"), "DIRECTORY" },
1108 { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
1109 /* Help string for command line --cache-dir flag */
1110 N_("Directory to cache information"), "DIRECTORY" },
1111 { "show-config", 0, 0, G_OPTION_ARG_NONE, &show_config,
1112 /* Help string for command line --show-config flag */
1113 N_("Show combined configuration"), NULL },
1114 { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
1115 /* Help string for command line --version flag */
1116 N_("Show release version"), NULL },
1119 GError *error = NULL;
1121 /* When lightdm starts sessions it needs to run itself in a new mode */
1122 if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
1123 return session_child_run (argc, argv);
1125 #if !defined(GLIB_VERSION_2_36)
1128 loop = g_main_loop_new (NULL, FALSE);
1130 messages = g_list_append (messages, g_strdup_printf ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ()));
1132 g_signal_connect (process_get_current (), "got-signal", G_CALLBACK (signal_cb), NULL);
1134 option_context = g_option_context_new (/* Arguments and description for --help test */
1135 _("- Display Manager"));
1136 g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
1137 result = g_option_context_parse (option_context, &argc, &argv, &error);
1139 g_printerr ("%s\n", error->message);
1140 g_clear_error (&error);
1141 g_option_context_free (option_context);
1144 g_printerr (/* Text printed out when an unknown command-line argument provided */
1145 _("Run '%s --help' to see a full list of available command line options."), argv[0]);
1147 return EXIT_FAILURE;
1150 /* Show combined configuration if user requested it */
1153 GList *sources, *link;
1154 gchar **groups, *last_source, *empty_source;
1155 GHashTable *source_ids;
1158 if (!config_load_from_standard_locations (config_get_instance (), config_path, NULL))
1159 return EXIT_FAILURE;
1161 /* Number sources */
1162 sources = config_get_sources (config_get_instance ());
1163 source_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1165 for (i = 0, link = sources; link; i++, link = link->next)
1171 id = g_strdup_printf ("%c", 'A' + i);
1173 id = g_strdup_printf ("%d", i);
1174 g_hash_table_insert (source_ids, g_strdup (path), id);
1177 empty_source = g_strdup (last_source);
1178 for (i = 0; empty_source[i] != '\0'; i++)
1179 empty_source[i] = ' ';
1181 /* Print out keys */
1182 groups = config_get_groups (config_get_instance ());
1183 for (i = 0; groups[i]; i++)
1190 g_printerr ("%s [%s]\n", empty_source, groups[i]);
1192 keys = config_get_keys (config_get_instance (), groups[i]);
1193 for (j = 0; keys[j]; j++)
1195 const gchar *source, *id;
1198 source = config_get_source (config_get_instance (), groups[i], keys[j]);
1199 id = source ? g_hash_table_lookup (source_ids, source) : empty_source;
1200 value = config_get_string (config_get_instance (), groups[i], keys[j]);
1201 g_printerr ("%s %s=%s\n", id, keys[j], value);
1207 g_strfreev (groups);
1209 /* Show mapping from source number to path */
1211 g_printerr ("Sources:\n");
1212 for (link = sources; link; link = link->next)
1214 const gchar *path = link->data;
1215 const gchar *source;
1217 source = g_hash_table_lookup (source_ids, path);
1218 g_printerr ("%s %s\n", source, path);
1221 g_hash_table_destroy (source_ids);
1223 return EXIT_SUCCESS;
1228 /* NOTE: Is not translated so can be easily parsed */
1229 g_printerr ("lightdm %s\n", VERSION);
1230 return EXIT_SUCCESS;
1233 if (!test_mode && getuid () != 0)
1235 g_printerr ("Only root can run Light Display Manager. To run as a regular user for testing run with the --test-mode flag.\n");
1236 return EXIT_FAILURE;
1239 /* If running inside an X server use Xephyr for display */
1240 if (getenv ("DISPLAY") && getuid () != 0)
1242 gchar *x_server_path;
1244 x_server_path = g_find_program_in_path ("Xephyr");
1247 g_printerr ("Running inside an X server requires Xephyr to be installed but it cannot be found. Please install it or update your PATH environment variable.\n");
1248 return EXIT_FAILURE;
1250 g_free (x_server_path);
1253 /* Make sure the system binary directory (where the greeters are installed) is in the path */
1256 const gchar *path = g_getenv ("PATH");
1260 new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
1262 new_path = g_strdup (SBIN_DIR);
1263 g_setenv ("PATH", new_path, TRUE);
1267 /* Write PID file */
1268 pid_file = fopen (pid_path, "w");
1271 fprintf (pid_file, "%d\n", getpid ());
1275 /* If not running as root write output to directories we control */
1278 g_free (default_log_dir);
1279 default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
1280 g_free (default_run_dir);
1281 default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
1282 g_free (default_cache_dir);
1283 default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
1286 /* Load config file(s) */
1287 if (!config_load_from_standard_locations (config_get_instance (), config_path, &messages))
1288 exit (EXIT_FAILURE);
1289 g_free (config_path);
1291 /* Set default values */
1292 if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
1293 config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
1294 if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
1295 config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
1296 if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
1297 config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
1298 if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
1299 config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
1300 if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
1301 config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
1302 if (!config_has_key (config_get_instance (), "SeatDefaults", "type"))
1303 config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal");
1304 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-service"))
1305 config_set_string (config_get_instance (), "SeatDefaults", "pam-service", "lightdm");
1306 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-autologin-service"))
1307 config_set_string (config_get_instance (), "SeatDefaults", "pam-autologin-service", "lightdm-autologin");
1308 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-greeter-service"))
1309 config_set_string (config_get_instance (), "SeatDefaults", "pam-greeter-service", "lightdm-greeter");
1310 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-command"))
1311 config_set_string (config_get_instance (), "SeatDefaults", "xserver-command", "X");
1312 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-share"))
1313 config_set_boolean (config_get_instance (), "SeatDefaults", "xserver-share", TRUE);
1314 if (!config_has_key (config_get_instance (), "SeatDefaults", "unity-compositor-command"))
1315 config_set_string (config_get_instance (), "SeatDefaults", "unity-compositor-command", "unity-system-compositor");
1316 if (!config_has_key (config_get_instance (), "SeatDefaults", "start-session"))
1317 config_set_boolean (config_get_instance (), "SeatDefaults", "start-session", TRUE);
1318 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-user-switching"))
1319 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-user-switching", TRUE);
1320 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-guest"))
1321 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-guest", TRUE);
1322 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-allow-guest"))
1323 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-allow-guest", TRUE);
1324 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-show-remote-login"))
1325 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-show-remote-login", TRUE);
1326 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-session"))
1327 config_set_string (config_get_instance (), "SeatDefaults", "greeter-session", GREETER_SESSION);
1328 if (!config_has_key (config_get_instance (), "SeatDefaults", "user-session"))
1329 config_set_string (config_get_instance (), "SeatDefaults", "user-session", USER_SESSION);
1330 if (!config_has_key (config_get_instance (), "SeatDefaults", "session-wrapper"))
1331 config_set_string (config_get_instance (), "SeatDefaults", "session-wrapper", "lightdm-session");
1332 if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
1333 config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
1334 g_free (default_log_dir);
1335 if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
1336 config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
1337 g_free (default_run_dir);
1338 if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
1339 config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
1340 g_free (default_cache_dir);
1341 if (!config_has_key (config_get_instance (), "LightDM", "sessions-directory"))
1342 config_set_string (config_get_instance (), "LightDM", "sessions-directory", SESSIONS_DIR);
1343 if (!config_has_key (config_get_instance (), "LightDM", "remote-sessions-directory"))
1344 config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR);
1345 if (!config_has_key (config_get_instance (), "LightDM", "greeters-directory"))
1346 config_set_string (config_get_instance (), "LightDM", "greeters-directory", GREETERS_DIR);
1348 /* Override defaults */
1350 config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
1353 config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
1356 config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
1359 /* Create run and cache directories */
1360 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
1361 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1362 g_warning ("Failed to make log directory %s: %s", dir, strerror (errno));
1364 dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
1365 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1366 g_warning ("Failed to make run directory %s: %s", dir, strerror (errno));
1368 dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
1369 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1370 g_warning ("Failed to make cache directory %s: %s", dir, strerror (errno));
1375 /* Show queued messages once logging is complete */
1376 for (link = messages; link; link = link->next)
1377 g_debug ("%s", (gchar *)link->data);
1378 g_list_free_full (messages, g_free);
1380 g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
1381 bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
1383 G_BUS_NAME_OWNER_FLAGS_NONE,
1391 g_debug ("Running in user mode");
1392 if (getenv ("DISPLAY"))
1393 g_debug ("Using Xephyr for X servers");
1395 display_manager = display_manager_new ();
1396 g_signal_connect (display_manager, "stopped", G_CALLBACK (display_manager_stopped_cb), NULL);
1397 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (display_manager_seat_removed_cb), NULL);
1399 shared_data_manager_start (shared_data_manager_get_instance ());
1401 /* Connect to logind */
1402 if (login1_service_connect (login1_service_get_instance ()))
1404 /* Load dynamic seats from logind */
1405 g_debug ("Monitoring logind for seats");
1407 if (config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1409 g_signal_connect (login1_service_get_instance (), "seat-added", G_CALLBACK (login1_service_seat_added_cb), NULL);
1410 g_signal_connect (login1_service_get_instance (), "seat-removed", G_CALLBACK (login1_service_seat_removed_cb), NULL);
1412 for (link = login1_service_get_seats (login1_service_get_instance ()); link; link = link->next)
1414 Login1Seat *seat = link->data;
1415 if (!add_login1_seat (seat))
1416 return EXIT_FAILURE;
1424 /* Load the static display entries */
1425 groups = config_get_groups (config_get_instance ());
1426 for (i = groups; *i; i++)
1428 gchar *config_section = *i;
1432 const gchar *const seatpfx = "Seat:";
1434 if (!g_str_has_prefix (config_section, seatpfx))
1437 g_debug ("Loading seat %s", config_section);
1438 types = config_get_string_list (config_get_instance (), config_section, "type");
1440 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1441 for (type = types; type && *type; type++)
1443 seat = seat_new (*type);
1451 gchar *seat_name = config_section + strlen (seatpfx);
1453 seat_set_property (seat, "seat-name", seat_name);
1455 set_seat_properties (seat, config_section);
1456 display_manager_add_seat (display_manager, seat);
1457 g_object_unref (seat);
1461 g_warning ("Failed to create seat %s", config_section);
1463 g_strfreev (groups);
1465 /* If no seats start a default one */
1466 if (n_seats == 0 && config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1472 g_debug ("Adding default seat");
1474 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1475 for (type = types; type && *type; type++)
1477 seat = seat_new (*type);
1484 set_seat_properties (seat, NULL);
1485 seat_set_property (seat, "exit-on-failure", "true");
1486 if (!display_manager_add_seat (display_manager, seat))
1487 return EXIT_FAILURE;
1488 g_object_unref (seat);
1492 g_warning ("Failed to create default seat");
1493 return EXIT_FAILURE;
1498 g_main_loop_run (loop);
1500 /* Clean up shared data manager */
1501 shared_data_manager_cleanup ();
1503 /* Clean up user list */
1504 common_user_list_cleanup ();
1506 /* Clean up display manager */
1507 g_object_unref (display_manager);
1508 display_manager = NULL;
1510 /* Remove D-Bus interface */
1511 g_dbus_connection_unregister_object (bus, reg_id);
1512 g_bus_unown_name (bus_id);
1513 if (seat_bus_entries)
1514 g_hash_table_unref (seat_bus_entries);
1515 if (session_bus_entries)
1516 g_hash_table_unref (session_bus_entries);
1518 g_debug ("Exiting with return value %d", exit_code);