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 get_config_sections (const gchar *seat_name)
152 GList *config_sections = NULL;
154 config_sections = g_list_append (config_sections, g_strdup ("SeatDefaults"));
157 return config_sections;
159 groups = config_get_groups (config_get_instance ());
160 for (i = groups; *i; i++)
162 if (g_str_has_prefix (*i, "Seat:"))
164 const gchar *seat_name_glob = *i + strlen ("Seat:");
165 if (g_pattern_match_simple (seat_name_glob, seat_name))
166 config_sections = g_list_append (config_sections, g_strdup (*i));
171 return config_sections;
175 set_seat_properties (Seat *seat, const gchar *seat_name)
177 GList *sections, *link;
181 sections = get_config_sections (seat_name);
182 for (link = sections; link; link = link->next)
184 const gchar *section = link->data;
185 keys = config_get_keys (config_get_instance (), section);
186 for (i = 0; keys && keys[i]; i++)
188 gchar *value = config_get_string (config_get_instance (), section, keys[i]);
189 seat_set_property (seat, keys[i], value);
194 g_list_free_full (sections, g_free);
198 signal_cb (Process *process, int signum)
200 g_debug ("Caught %s signal, shutting down", g_strsignal (signum));
201 display_manager_stop (display_manager);
202 // FIXME: Stop XDMCP server
206 display_manager_stopped_cb (DisplayManager *display_manager)
208 g_debug ("Stopping daemon");
209 g_main_loop_quit (loop);
213 display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat)
217 Seat *next_seat = NULL;
220 /* If we have fallback types registered for the seat, let's try them
222 types = seat_get_string_list_property (seat, "type");
223 next_types = g_string_new ("");
224 for (iter = types; iter && *iter; iter++)
227 continue; // skip first one, that is our current seat type
231 next_seat = seat_new (*iter, seat_get_name (seat));
232 g_string_assign (next_types, *iter);
236 // Build up list of types to try next time
237 g_string_append_c (next_types, ';');
238 g_string_append (next_types, *iter);
245 set_seat_properties (next_seat, seat_get_name (seat));
247 // We set this manually on default seat. Let's port it over if needed.
248 if (seat_get_boolean_property (seat, "exit-on-failure"))
249 seat_set_property (next_seat, "exit-on-failure", "true");
251 seat_set_property (next_seat, "type", next_types->str);
253 display_manager_add_seat (display_manager, next_seat);
254 g_object_unref (next_seat);
256 else if (seat_get_boolean_property (seat, "exit-on-failure"))
258 g_debug ("Required seat has stopped");
259 exit_code = EXIT_FAILURE;
260 display_manager_stop (display_manager);
263 g_string_free (next_types, TRUE);
269 GVariantBuilder builder;
273 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
274 g_hash_table_iter_init (&iter, seat_bus_entries);
275 while (g_hash_table_iter_next (&iter, NULL, &value))
277 SeatBusEntry *entry = value;
278 g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
281 return g_variant_builder_end (&builder);
285 get_session_list (const gchar *seat_path)
287 GVariantBuilder builder;
291 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
293 g_hash_table_iter_init (&iter, session_bus_entries);
294 while (g_hash_table_iter_next (&iter, NULL, &value))
296 SessionBusEntry *entry = value;
297 if (seat_path == NULL || strcmp (entry->seat_path, seat_path) == 0)
298 g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
301 return g_variant_builder_end (&builder);
305 handle_display_manager_get_property (GDBusConnection *connection,
307 const gchar *object_path,
308 const gchar *interface_name,
309 const gchar *property_name,
313 if (g_strcmp0 (property_name, "Seats") == 0)
314 return get_seat_list ();
315 else if (g_strcmp0 (property_name, "Sessions") == 0)
316 return get_session_list (NULL);
322 handle_display_manager_call (GDBusConnection *connection,
324 const gchar *object_path,
325 const gchar *interface_name,
326 const gchar *method_name,
327 GVariant *parameters,
328 GDBusMethodInvocation *invocation,
331 if (g_strcmp0 (method_name, "AddSeat") == 0)
332 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "AddSeat is deprecated");
333 else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
338 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
340 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
344 g_variant_get (parameters, "(i)", &display_number);
346 g_debug ("Adding local X seat :%d", display_number);
348 seat = seat_new ("xremote", "xremote0"); // FIXME: What to use for a name?
351 gchar *display_number_string;
353 set_seat_properties (seat, NULL);
354 display_number_string = g_strdup_printf ("%d", display_number);
355 seat_set_property (seat, "xserver-display-number", display_number_string);
356 g_free (display_number_string);
361 // FIXME: Need to make proper error
362 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create local X seat");
366 if (display_manager_add_seat (display_manager, seat))
370 entry = g_hash_table_lookup (seat_bus_entries, seat);
371 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
373 else// FIXME: Need to make proper error
374 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
375 g_object_unref (seat);
378 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
382 handle_seat_get_property (GDBusConnection *connection,
384 const gchar *object_path,
385 const gchar *interface_name,
386 const gchar *property_name,
390 Seat *seat = user_data;
392 if (g_strcmp0 (property_name, "CanSwitch") == 0)
393 return g_variant_new_boolean (seat_get_can_switch (seat));
394 if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
395 return g_variant_new_boolean (seat_get_allow_guest (seat));
396 else if (g_strcmp0 (property_name, "Sessions") == 0)
400 entry = g_hash_table_lookup (seat_bus_entries, seat);
401 return get_session_list (entry->path);
408 handle_seat_call (GDBusConnection *connection,
410 const gchar *object_path,
411 const gchar *interface_name,
412 const gchar *method_name,
413 GVariant *parameters,
414 GDBusMethodInvocation *invocation,
417 Seat *seat = user_data;
419 if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
421 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
422 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
424 if (seat_switch_to_greeter (seat))
425 g_dbus_method_invocation_return_value (invocation, NULL);
426 else// FIXME: Need to make proper error
427 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to greeter");
429 else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
431 const gchar *username, *session_name;
433 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
434 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
436 g_variant_get (parameters, "(&s&s)", &username, &session_name);
437 if (strcmp (session_name, "") == 0)
440 if (seat_switch_to_user (seat, username, session_name))
441 g_dbus_method_invocation_return_value (invocation, NULL);
442 else// FIXME: Need to make proper error
443 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to user");
445 else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
447 const gchar *session_name;
449 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
450 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
452 g_variant_get (parameters, "(&s)", &session_name);
453 if (strcmp (session_name, "") == 0)
456 if (seat_switch_to_guest (seat, session_name))
457 g_dbus_method_invocation_return_value (invocation, NULL);
458 else// FIXME: Need to make proper error
459 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to guest");
461 else if (g_strcmp0 (method_name, "Lock") == 0)
463 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
464 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
466 /* FIXME: Should only allow locks if have a session on this seat */
467 if (seat_lock (seat, NULL))
468 g_dbus_method_invocation_return_value (invocation, NULL);
469 else// FIXME: Need to make proper error
470 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to lock seat");
473 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
477 get_seat_for_session (Session *session)
481 for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
483 Seat *seat = seat_link->data;
486 for (session_link = seat_get_sessions (seat); session_link; session_link = session_link->next)
488 Session *s = session_link->data;
499 handle_session_get_property (GDBusConnection *connection,
501 const gchar *object_path,
502 const gchar *interface_name,
503 const gchar *property_name,
507 Session *session = user_data;
508 SessionBusEntry *entry;
510 entry = g_hash_table_lookup (session_bus_entries, session);
511 if (g_strcmp0 (property_name, "Seat") == 0)
512 return g_variant_new_object_path (entry ? entry->seat_path : "");
513 else if (g_strcmp0 (property_name, "UserName") == 0)
514 return g_variant_new_string (session_get_username (session));
520 handle_session_call (GDBusConnection *connection,
522 const gchar *object_path,
523 const gchar *interface_name,
524 const gchar *method_name,
525 GVariant *parameters,
526 GDBusMethodInvocation *invocation,
529 Session *session = user_data;
531 if (g_strcmp0 (method_name, "Lock") == 0)
535 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
536 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
538 seat = get_seat_for_session (session);
539 /* FIXME: Should only allow locks if have a session on this seat */
540 seat_lock (seat, session_get_username (session));
541 g_dbus_method_invocation_return_value (invocation, NULL);
544 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
547 static SeatBusEntry *
548 seat_bus_entry_new (const gchar *path)
552 entry = g_malloc0 (sizeof (SeatBusEntry));
553 entry->path = g_strdup (path);
558 static SessionBusEntry *
559 session_bus_entry_new (const gchar *path, const gchar *seat_path)
561 SessionBusEntry *entry;
563 entry = g_malloc0 (sizeof (SessionBusEntry));
564 entry->path = g_strdup (path);
565 entry->seat_path = g_strdup (seat_path);
571 emit_object_value_changed (GDBusConnection *bus, const gchar *path, const gchar *interface_name, const gchar *property_name, GVariant *property_value)
573 GVariantBuilder builder;
574 GError *error = NULL;
576 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
577 g_variant_builder_add (&builder, "{sv}", property_name, property_value);
579 if (!g_dbus_connection_emit_signal (bus,
582 "org.freedesktop.DBus.Properties",
584 g_variant_new ("(sa{sv}as)", interface_name, &builder, NULL),
586 g_warning ("Failed to emit PropertiesChanged signal: %s", error->message);
587 g_clear_error (&error);
591 emit_object_signal (GDBusConnection *bus, const gchar *path, const gchar *signal_name, const gchar *object_path)
593 GError *error = NULL;
595 if (!g_dbus_connection_emit_signal (bus,
598 "org.freedesktop.DisplayManager",
600 g_variant_new ("(o)", object_path),
602 g_warning ("Failed to emit %s signal on %s: %s", signal_name, path, error->message);
603 g_clear_error (&error);
607 seat_bus_entry_free (gpointer data)
609 SeatBusEntry *entry = data;
611 g_dbus_connection_unregister_object (bus, entry->bus_id);
613 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
614 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatRemoved", entry->path);
616 g_free (entry->path);
621 session_bus_entry_free (gpointer data)
623 SessionBusEntry *entry = data;
625 g_dbus_connection_unregister_object (bus, entry->bus_id);
627 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
628 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionRemoved", entry->path);
630 emit_object_value_changed (bus, entry->seat_path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (entry->seat_path));
631 emit_object_signal (bus, entry->seat_path, "SessionRemoved", entry->path);
633 g_free (entry->path);
634 g_free (entry->seat_path);
639 running_user_session_cb (Seat *seat, Session *session)
641 static const GDBusInterfaceVTable session_vtable =
644 handle_session_get_property
646 SeatBusEntry *seat_entry;
647 SessionBusEntry *session_entry;
649 GError *error = NULL;
651 /* Set environment variables when session runs */
652 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
653 session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
654 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
656 session_set_env (session, "XDG_SESSION_PATH", path);
657 g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
659 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
660 session_entry = session_bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL);
661 g_hash_table_insert (session_bus_entries, g_object_ref (session), session_entry);
663 g_debug ("Registering session with bus path %s", session_entry->path);
665 session_entry->bus_id = g_dbus_connection_register_object (bus,
667 session_info->interfaces[0],
669 g_object_ref (session), g_object_unref,
671 if (session_entry->bus_id == 0)
672 g_warning ("Failed to register user session: %s", error->message);
673 g_clear_error (&error);
675 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
676 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionAdded", session_entry->path);
678 emit_object_value_changed (bus, seat_entry->path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (session_entry->seat_path));
679 emit_object_signal (bus, seat_entry->path, "SessionAdded", session_entry->path);
683 session_removed_cb (Seat *seat, Session *session)
685 g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
686 g_hash_table_remove (session_bus_entries, session);
690 seat_added_cb (DisplayManager *display_manager, Seat *seat)
692 static const GDBusInterfaceVTable seat_vtable =
695 handle_seat_get_property
699 GError *error = NULL;
701 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
704 entry = seat_bus_entry_new (path);
706 g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
708 g_debug ("Registering seat with bus path %s", entry->path);
710 entry->bus_id = g_dbus_connection_register_object (bus,
712 seat_info->interfaces[0],
714 g_object_ref (seat), g_object_unref,
716 if (entry->bus_id == 0)
717 g_warning ("Failed to register seat: %s", error->message);
718 g_clear_error (&error);
720 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
721 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatAdded", entry->path);
723 g_signal_connect (seat, "running-user-session", G_CALLBACK (running_user_session_cb), NULL);
724 g_signal_connect (seat, "session-removed", G_CALLBACK (session_removed_cb), NULL);
728 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
730 g_hash_table_remove (seat_bus_entries, seat);
734 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
736 SeatXDMCPSession *seat;
739 seat = seat_xdmcp_session_new (session);
740 set_seat_properties (SEAT (seat), NULL);
741 result = display_manager_add_seat (display_manager, SEAT (seat));
742 g_object_unref (seat);
748 vnc_connection_cb (VNCServer *server, GSocket *connection)
752 seat = seat_xvnc_new (connection);
753 set_seat_properties (SEAT (seat), NULL);
754 display_manager_add_seat (display_manager, SEAT (seat));
755 g_object_unref (seat);
759 bus_acquired_cb (GDBusConnection *connection,
763 const gchar *display_manager_interface =
765 " <interface name='org.freedesktop.DisplayManager'>"
766 " <property name='Seats' type='ao' access='read'/>"
767 " <property name='Sessions' type='ao' access='read'/>"
768 " <method name='AddSeat'>"
769 " <arg name='type' direction='in' type='s'/>"
770 " <arg name='properties' direction='in' type='a(ss)'/>"
771 " <arg name='seat' direction='out' type='o'/>"
773 " <method name='AddLocalXSeat'>"
774 " <arg name='display-number' direction='in' type='i'/>"
775 " <arg name='seat' direction='out' type='o'/>"
777 " <signal name='SeatAdded'>"
778 " <arg name='seat' type='o'/>"
780 " <signal name='SeatRemoved'>"
781 " <arg name='seat' type='o'/>"
783 " <signal name='SessionAdded'>"
784 " <arg name='session' type='o'/>"
786 " <signal name='SessionRemoved'>"
787 " <arg name='session' type='o'/>"
791 static const GDBusInterfaceVTable display_manager_vtable =
793 handle_display_manager_call,
794 handle_display_manager_get_property
796 const gchar *seat_interface =
798 " <interface name='org.freedesktop.DisplayManager.Seat'>"
799 " <property name='CanSwitch' type='b' access='read'/>"
800 " <property name='HasGuestAccount' type='b' access='read'/>"
801 " <property name='Sessions' type='ao' access='read'/>"
802 " <method name='SwitchToGreeter'/>"
803 " <method name='SwitchToUser'>"
804 " <arg name='username' direction='in' type='s'/>"
805 " <arg name='session-name' direction='in' type='s'/>"
807 " <method name='SwitchToGuest'>"
808 " <arg name='session-name' direction='in' type='s'/>"
810 " <method name='Lock'/>"
811 " <signal name='SessionAdded'>"
812 " <arg name='session' type='o'/>"
814 " <signal name='SessionRemoved'>"
815 " <arg name='session' type='o'/>"
819 const gchar *session_interface =
821 " <interface name='org.freedesktop.DisplayManager.Session'>"
822 " <property name='Seat' type='o' access='read'/>"
823 " <property name='UserName' type='s' access='read'/>"
824 " <method name='Lock'/>"
827 GDBusNodeInfo *display_manager_info;
829 GError *error = NULL;
831 g_debug ("Acquired bus name %s", name);
835 display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
836 g_assert (display_manager_info != NULL);
837 seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
838 g_assert (seat_info != NULL);
839 session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
840 g_assert (session_info != NULL);
842 reg_id = g_dbus_connection_register_object (connection,
843 "/org/freedesktop/DisplayManager",
844 display_manager_info->interfaces[0],
845 &display_manager_vtable,
849 g_warning ("Failed to register display manager: %s", error->message);
850 g_clear_error (&error);
851 g_dbus_node_info_unref (display_manager_info);
853 seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, seat_bus_entry_free);
854 session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, session_bus_entry_free);
856 g_signal_connect (display_manager, "seat-added", G_CALLBACK (seat_added_cb), NULL);
857 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (seat_removed_cb), NULL);
858 for (link = display_manager_get_seats (display_manager); link; link = link->next)
859 seat_added_cb (display_manager, (Seat *) link->data);
861 display_manager_start (display_manager);
863 /* Start the XDMCP server */
864 if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
866 gchar *key_name, *key = NULL;
868 xdmcp_server = xdmcp_server_new ();
869 if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
872 port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
874 xdmcp_server_set_port (xdmcp_server, port);
876 g_signal_connect (xdmcp_server, "new-session", G_CALLBACK (xdmcp_session_cb), NULL);
878 key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
884 GError *error = NULL;
886 path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
888 keys = g_key_file_new ();
889 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
891 g_debug ("Error getting key %s", error->message);
892 g_clear_error (&error);
896 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
897 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
899 g_debug ("Key %s not defined", key_name);
902 g_key_file_free (keys);
905 xdmcp_server_set_key (xdmcp_server, key);
909 g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
910 xdmcp_server_start (xdmcp_server);
913 /* Start the VNC server */
914 if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
918 path = g_find_program_in_path ("Xvnc");
921 vnc_server = vnc_server_new ();
922 if (config_has_key (config_get_instance (), "VNCServer", "port"))
925 port = config_get_integer (config_get_instance (), "VNCServer", "port");
927 vnc_server_set_port (vnc_server, port);
929 g_signal_connect (vnc_server, "new-connection", G_CALLBACK (vnc_connection_cb), NULL);
931 g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
932 vnc_server_start (vnc_server);
937 g_warning ("Can't start VNC server, Xvnc is not in the path");
942 name_lost_cb (GDBusConnection *connection,
947 g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
949 g_printerr ("Failed to get D-Bus connection\n");
955 add_login1_seat (Login1Seat *login1_seat)
957 const gchar *seat_name = login1_seat_get_id (login1_seat);
958 gchar **types = NULL, **type;
959 GList *config_sections = NULL, *link;
961 gboolean is_seat0, started = FALSE;
963 g_debug ("New seat added from logind: %s", seat_name);
964 is_seat0 = strcmp (seat_name, "seat0") == 0;
966 config_sections = get_config_sections (seat_name);
967 for (link = g_list_last (config_sections); link; link = link->prev)
969 gchar *config_section = link->data;
970 g_debug ("Loading properties from config section %s", config_section);
971 types = config_get_string_list (config_get_instance (), config_section, "type");
975 g_list_free_full (config_sections, g_free);
977 for (type = types; !seat && type && *type; type++)
978 seat = seat_new (*type, seat_name);
983 set_seat_properties (seat, seat_name);
985 if (!login1_seat_get_can_multi_session (login1_seat))
987 g_debug ("Seat %s has property CanMultiSession=no", seat_name);
988 seat_set_property (seat, "allow-user-switching", "false");
992 seat_set_property (seat, "exit-on-failure", "true");
995 g_debug ("Unable to create seat: %s", seat_name);
999 started = display_manager_add_seat (display_manager, seat);
1001 g_debug ("Failed to start seat: %s", seat_name);
1004 g_object_unref (seat);
1010 remove_login1_seat (Login1Seat *login1_seat)
1014 seat = display_manager_get_seat (display_manager, login1_seat_get_id (login1_seat));
1019 static gboolean update_login1_seat (Login1Seat *login1_seat);
1022 seat_stopped_cb (Seat *seat, Login1Seat *login1_seat)
1024 update_login1_seat (login1_seat);
1025 g_signal_handlers_disconnect_matched (seat, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, login1_seat);
1029 update_login1_seat (Login1Seat *login1_seat)
1031 if (login1_seat_get_can_graphical (login1_seat))
1035 /* Wait for existing seat to stop or ignore if we already have a valid seat */
1036 seat = display_manager_get_seat (display_manager, login1_seat_get_id (login1_seat));
1039 if (seat_get_is_stopping (seat))
1040 g_signal_connect (seat, "stopped", G_CALLBACK (seat_stopped_cb), login1_seat);
1044 return add_login1_seat (login1_seat);
1048 remove_login1_seat (login1_seat);
1054 login1_can_graphical_changed_cb (Login1Seat *login1_seat)
1056 g_debug ("Seat %s changes graphical state to %s", login1_seat_get_id (login1_seat), login1_seat_get_can_graphical (login1_seat) ? "true" : "false");
1057 update_login1_seat (login1_seat);
1061 login1_service_seat_added_cb (Login1Service *service, Login1Seat *login1_seat)
1063 g_signal_connect (login1_seat, "can-graphical-changed", G_CALLBACK (login1_can_graphical_changed_cb), NULL);
1064 login1_can_graphical_changed_cb (login1_seat);
1068 login1_service_seat_removed_cb (Login1Service *service, Login1Seat *login1_seat)
1070 g_signal_handlers_disconnect_matched (login1_seat, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, login1_can_graphical_changed_cb, NULL);
1072 g_debug ("Seat removed from logind: %s", login1_seat_get_id (login1_seat));
1073 remove_login1_seat (login1_seat);
1077 main (int argc, char **argv)
1080 GOptionContext *option_context;
1083 gboolean test_mode = FALSE;
1084 gchar *pid_path = "/var/run/lightdm.pid";
1085 gchar *log_dir = NULL;
1086 gchar *run_dir = NULL;
1087 gchar *cache_dir = NULL;
1088 gchar *default_log_dir = g_strdup (LOG_DIR);
1089 gchar *default_run_dir = g_strdup (RUN_DIR);
1090 gchar *default_cache_dir = g_strdup (CACHE_DIR);
1091 gboolean show_config = FALSE, show_version = FALSE;
1092 GList *link, *messages = NULL;
1093 GOptionEntry options[] =
1095 { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
1096 /* Help string for command line --config flag */
1097 N_("Use configuration file"), "FILE" },
1098 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
1099 /* Help string for command line --debug flag */
1100 N_("Print debugging messages"), NULL },
1101 { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
1102 /* Help string for command line --test-mode flag */
1103 N_("Run as unprivileged user, skipping things that require root access"), NULL },
1104 { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
1105 /* Help string for command line --pid-file flag */
1106 N_("File to write PID into"), "FILE" },
1107 { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
1108 /* Help string for command line --log-dir flag */
1109 N_("Directory to write logs to"), "DIRECTORY" },
1110 { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
1111 /* Help string for command line --run-dir flag */
1112 N_("Directory to store running state"), "DIRECTORY" },
1113 { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
1114 /* Help string for command line --cache-dir flag */
1115 N_("Directory to cache information"), "DIRECTORY" },
1116 { "show-config", 0, 0, G_OPTION_ARG_NONE, &show_config,
1117 /* Help string for command line --show-config flag */
1118 N_("Show combined configuration"), NULL },
1119 { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
1120 /* Help string for command line --version flag */
1121 N_("Show release version"), NULL },
1124 GError *error = NULL;
1126 /* When lightdm starts sessions it needs to run itself in a new mode */
1127 if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
1128 return session_child_run (argc, argv);
1130 #if !defined(GLIB_VERSION_2_36)
1133 loop = g_main_loop_new (NULL, FALSE);
1135 messages = g_list_append (messages, g_strdup_printf ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ()));
1137 g_signal_connect (process_get_current (), "got-signal", G_CALLBACK (signal_cb), NULL);
1139 option_context = g_option_context_new (/* Arguments and description for --help test */
1140 _("- Display Manager"));
1141 g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
1142 result = g_option_context_parse (option_context, &argc, &argv, &error);
1144 g_printerr ("%s\n", error->message);
1145 g_clear_error (&error);
1146 g_option_context_free (option_context);
1149 g_printerr (/* Text printed out when an unknown command-line argument provided */
1150 _("Run '%s --help' to see a full list of available command line options."), argv[0]);
1152 return EXIT_FAILURE;
1155 /* Show combined configuration if user requested it */
1158 GList *sources, *link;
1159 gchar **groups, *last_source, *empty_source;
1160 GHashTable *source_ids;
1163 if (!config_load_from_standard_locations (config_get_instance (), config_path, NULL))
1164 return EXIT_FAILURE;
1166 /* Number sources */
1167 sources = config_get_sources (config_get_instance ());
1168 source_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1170 for (i = 0, link = sources; link; i++, link = link->next)
1176 id = g_strdup_printf ("%c", 'A' + i);
1178 id = g_strdup_printf ("%d", i);
1179 g_hash_table_insert (source_ids, g_strdup (path), id);
1182 empty_source = g_strdup (last_source);
1183 for (i = 0; empty_source[i] != '\0'; i++)
1184 empty_source[i] = ' ';
1186 /* Print out keys */
1187 groups = config_get_groups (config_get_instance ());
1188 for (i = 0; groups[i]; i++)
1195 g_printerr ("%s [%s]\n", empty_source, groups[i]);
1197 keys = config_get_keys (config_get_instance (), groups[i]);
1198 for (j = 0; keys && keys[j]; j++)
1200 const gchar *source, *id;
1203 source = config_get_source (config_get_instance (), groups[i], keys[j]);
1204 id = source ? g_hash_table_lookup (source_ids, source) : empty_source;
1205 value = config_get_string (config_get_instance (), groups[i], keys[j]);
1206 g_printerr ("%s %s=%s\n", id, keys[j], value);
1212 g_strfreev (groups);
1214 /* Show mapping from source number to path */
1216 g_printerr ("Sources:\n");
1217 for (link = sources; link; link = link->next)
1219 const gchar *path = link->data;
1220 const gchar *source;
1222 source = g_hash_table_lookup (source_ids, path);
1223 g_printerr ("%s %s\n", source, path);
1226 g_hash_table_destroy (source_ids);
1228 return EXIT_SUCCESS;
1233 /* NOTE: Is not translated so can be easily parsed */
1234 g_printerr ("lightdm %s\n", VERSION);
1235 return EXIT_SUCCESS;
1238 if (!test_mode && getuid () != 0)
1240 g_printerr ("Only root can run Light Display Manager. To run as a regular user for testing run with the --test-mode flag.\n");
1241 return EXIT_FAILURE;
1244 /* If running inside an X server use Xephyr for display */
1245 if (getenv ("DISPLAY") && getuid () != 0)
1247 gchar *x_server_path;
1249 x_server_path = g_find_program_in_path ("Xephyr");
1252 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");
1253 return EXIT_FAILURE;
1255 g_free (x_server_path);
1258 /* Make sure the system binary directory (where the greeters are installed) is in the path */
1261 const gchar *path = g_getenv ("PATH");
1265 new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
1267 new_path = g_strdup (SBIN_DIR);
1268 g_setenv ("PATH", new_path, TRUE);
1272 /* Write PID file */
1273 pid_file = fopen (pid_path, "w");
1276 fprintf (pid_file, "%d\n", getpid ());
1280 /* If not running as root write output to directories we control */
1283 g_free (default_log_dir);
1284 default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
1285 g_free (default_run_dir);
1286 default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
1287 g_free (default_cache_dir);
1288 default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
1291 /* Load config file(s) */
1292 if (!config_load_from_standard_locations (config_get_instance (), config_path, &messages))
1293 exit (EXIT_FAILURE);
1294 g_free (config_path);
1296 /* Set default values */
1297 if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
1298 config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
1299 if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
1300 config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
1301 if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
1302 config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
1303 if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
1304 config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
1305 if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
1306 config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
1307 if (!config_has_key (config_get_instance (), "SeatDefaults", "type"))
1308 config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal");
1309 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-service"))
1310 config_set_string (config_get_instance (), "SeatDefaults", "pam-service", "lightdm");
1311 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-autologin-service"))
1312 config_set_string (config_get_instance (), "SeatDefaults", "pam-autologin-service", "lightdm-autologin");
1313 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-greeter-service"))
1314 config_set_string (config_get_instance (), "SeatDefaults", "pam-greeter-service", "lightdm-greeter");
1315 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-command"))
1316 config_set_string (config_get_instance (), "SeatDefaults", "xserver-command", "X");
1317 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-share"))
1318 config_set_boolean (config_get_instance (), "SeatDefaults", "xserver-share", TRUE);
1319 if (!config_has_key (config_get_instance (), "SeatDefaults", "unity-compositor-command"))
1320 config_set_string (config_get_instance (), "SeatDefaults", "unity-compositor-command", "unity-system-compositor");
1321 if (!config_has_key (config_get_instance (), "SeatDefaults", "start-session"))
1322 config_set_boolean (config_get_instance (), "SeatDefaults", "start-session", TRUE);
1323 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-user-switching"))
1324 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-user-switching", TRUE);
1325 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-guest"))
1326 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-guest", TRUE);
1327 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-allow-guest"))
1328 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-allow-guest", TRUE);
1329 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-show-remote-login"))
1330 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-show-remote-login", TRUE);
1331 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-session"))
1332 config_set_string (config_get_instance (), "SeatDefaults", "greeter-session", GREETER_SESSION);
1333 if (!config_has_key (config_get_instance (), "SeatDefaults", "user-session"))
1334 config_set_string (config_get_instance (), "SeatDefaults", "user-session", USER_SESSION);
1335 if (!config_has_key (config_get_instance (), "SeatDefaults", "session-wrapper"))
1336 config_set_string (config_get_instance (), "SeatDefaults", "session-wrapper", "lightdm-session");
1337 if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
1338 config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
1339 g_free (default_log_dir);
1340 if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
1341 config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
1342 g_free (default_run_dir);
1343 if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
1344 config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
1345 g_free (default_cache_dir);
1346 if (!config_has_key (config_get_instance (), "LightDM", "sessions-directory"))
1347 config_set_string (config_get_instance (), "LightDM", "sessions-directory", SESSIONS_DIR);
1348 if (!config_has_key (config_get_instance (), "LightDM", "remote-sessions-directory"))
1349 config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR);
1350 if (!config_has_key (config_get_instance (), "LightDM", "greeters-directory"))
1351 config_set_string (config_get_instance (), "LightDM", "greeters-directory", GREETERS_DIR);
1353 /* Override defaults */
1355 config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
1358 config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
1361 config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
1364 /* Create run and cache directories */
1365 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
1366 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1367 g_warning ("Failed to make log directory %s: %s", dir, strerror (errno));
1369 dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
1370 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1371 g_warning ("Failed to make run directory %s: %s", dir, strerror (errno));
1373 dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
1374 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1375 g_warning ("Failed to make cache directory %s: %s", dir, strerror (errno));
1380 /* Show queued messages once logging is complete */
1381 for (link = messages; link; link = link->next)
1382 g_debug ("%s", (gchar *)link->data);
1383 g_list_free_full (messages, g_free);
1385 g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
1386 bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
1388 G_BUS_NAME_OWNER_FLAGS_NONE,
1396 g_debug ("Running in user mode");
1397 if (getenv ("DISPLAY"))
1398 g_debug ("Using Xephyr for X servers");
1400 display_manager = display_manager_new ();
1401 g_signal_connect (display_manager, "stopped", G_CALLBACK (display_manager_stopped_cb), NULL);
1402 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (display_manager_seat_removed_cb), NULL);
1404 shared_data_manager_start (shared_data_manager_get_instance ());
1406 /* Connect to logind */
1407 if (login1_service_connect (login1_service_get_instance ()))
1409 /* Load dynamic seats from logind */
1410 g_debug ("Monitoring logind for seats");
1412 if (config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1414 g_signal_connect (login1_service_get_instance (), "seat-added", G_CALLBACK (login1_service_seat_added_cb), NULL);
1415 g_signal_connect (login1_service_get_instance (), "seat-removed", G_CALLBACK (login1_service_seat_removed_cb), NULL);
1417 for (link = login1_service_get_seats (login1_service_get_instance ()); link; link = link->next)
1419 Login1Seat *login1_seat = link->data;
1420 g_signal_connect (login1_seat, "can-graphical-changed", G_CALLBACK (login1_can_graphical_changed_cb), NULL);
1421 if (!update_login1_seat (login1_seat))
1422 return EXIT_FAILURE;
1428 if (config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1434 g_debug ("Adding default seat");
1436 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1437 for (type = types; type && *type; type++)
1439 seat = seat_new (*type, "seat0");
1446 set_seat_properties (seat, NULL);
1447 seat_set_property (seat, "exit-on-failure", "true");
1448 if (!display_manager_add_seat (display_manager, seat))
1449 return EXIT_FAILURE;
1450 g_object_unref (seat);
1454 g_warning ("Failed to create default seat");
1455 return EXIT_FAILURE;
1460 g_main_loop_run (loop);
1462 /* Clean up shared data manager */
1463 shared_data_manager_cleanup ();
1465 /* Clean up user list */
1466 common_user_list_cleanup ();
1468 /* Clean up display manager */
1469 g_object_unref (display_manager);
1470 display_manager = NULL;
1472 /* Remove D-Bus interface */
1473 g_dbus_connection_unregister_object (bus, reg_id);
1474 g_bus_unown_name (bus_id);
1475 if (seat_bus_entries)
1476 g_hash_table_unref (seat_bus_entries);
1477 if (session_bus_entries)
1478 g_hash_table_unref (session_bus_entries);
1480 g_debug ("Exiting with return value %d", exit_code);