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"
36 static gchar *config_path = NULL;
37 static GMainLoop *loop = NULL;
38 static GTimer *log_timer;
39 static int log_fd = -1;
40 static gboolean debug = FALSE;
42 static DisplayManager *display_manager = NULL;
43 static XDMCPServer *xdmcp_server = NULL;
44 static VNCServer *vnc_server = NULL;
45 static guint bus_id = 0;
46 static GDBusConnection *bus = NULL;
47 static guint reg_id = 0;
48 static GDBusNodeInfo *seat_info;
49 static GHashTable *seat_bus_entries = NULL;
50 static guint seat_index = 0;
51 static GDBusNodeInfo *session_info;
52 static GHashTable *session_bus_entries = NULL;
53 static guint session_index = 0;
54 static gint exit_code = EXIT_SUCCESS;
68 #define LIGHTDM_BUS_NAME "org.freedesktop.DisplayManager"
71 log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer data)
76 switch (log_level & G_LOG_LEVEL_MASK)
78 case G_LOG_LEVEL_ERROR:
81 case G_LOG_LEVEL_CRITICAL:
84 case G_LOG_LEVEL_WARNING:
87 case G_LOG_LEVEL_MESSAGE:
90 case G_LOG_LEVEL_INFO:
93 case G_LOG_LEVEL_DEBUG:
101 text = g_strdup_printf ("[%+.2fs] %s %s\n", g_timer_elapsed (log_timer, NULL), prefix, message);
103 /* Log everything to a file */
107 n_written = write (log_fd, text, strlen (text));
109 ; /* Check result so compiler doesn't warn about it */
112 /* Log to stderr if requested */
114 g_printerr ("%s", text);
116 g_log_default_handler (log_domain, log_level, message, data);
124 gchar *log_dir, *path, *old_path;
126 log_timer = g_timer_new ();
129 log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
130 path = g_build_filename (log_dir, "lightdm.log", NULL);
133 /* Move old file out of the way */
134 old_path = g_strdup_printf ("%s.old", path);
135 rename (path, old_path);
138 /* Create new file and log to it */
139 log_fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
140 fcntl (log_fd, F_SETFD, FD_CLOEXEC);
141 g_log_set_default_handler (log_cb, NULL);
143 g_debug ("Logging to %s", path);
148 set_seat_properties (Seat *seat, const gchar *config_section)
153 keys = config_get_keys (config_get_instance (), "SeatDefaults");
154 for (i = 0; keys[i]; i++)
156 gchar *value = config_get_string (config_get_instance (), "SeatDefaults", keys[i]);
157 seat_set_property (seat, keys[i], value);
164 keys = config_get_keys (config_get_instance (), config_section);
165 for (i = 0; keys[i]; i++)
167 gchar *value = config_get_string (config_get_instance (), config_section, keys[i]);
168 seat_set_property (seat, keys[i], value);
176 signal_cb (Process *process, int signum)
178 g_debug ("Caught %s signal, shutting down", g_strsignal (signum));
179 display_manager_stop (display_manager);
180 // FIXME: Stop XDMCP server
184 display_manager_stopped_cb (DisplayManager *display_manager)
186 g_debug ("Stopping daemon");
187 g_main_loop_quit (loop);
191 display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat)
195 Seat *next_seat = NULL;
198 /* If we have fallback types registered for the seat, let's try them
200 types = seat_get_string_list_property (seat, "type");
201 next_types = g_string_new ("");
202 for (iter = types; iter && *iter; iter++)
205 continue; // skip first one, that is our current seat type
209 next_seat = seat_new (*iter);
210 g_string_assign (next_types, *iter);
214 // Build up list of types to try next time
215 g_string_append_c (next_types, ';');
216 g_string_append (next_types, *iter);
223 const gchar *seat_name;
224 gchar *config_section = NULL;
226 seat_name = seat_get_string_property (seat, "seat-name");
228 config_section = g_strdup_printf ("Seat:%s", seat_name);
229 set_seat_properties (next_seat, config_section);
230 g_free (config_section);
232 // We set this manually on default seat. Let's port it over if needed.
233 if (seat_get_boolean_property (seat, "exit-on-failure"))
234 seat_set_property (next_seat, "exit-on-failure", "true");
236 seat_set_property (next_seat, "type", next_types->str);
238 display_manager_add_seat (display_manager, next_seat);
239 g_object_unref (next_seat);
241 else if (seat_get_boolean_property (seat, "exit-on-failure"))
243 g_debug ("Required seat has stopped");
244 exit_code = EXIT_FAILURE;
245 display_manager_stop (display_manager);
248 g_string_free (next_types, TRUE);
254 GVariantBuilder builder;
258 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
259 g_hash_table_iter_init (&iter, seat_bus_entries);
260 while (g_hash_table_iter_next (&iter, NULL, &value))
262 SeatBusEntry *entry = value;
263 g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
266 return g_variant_builder_end (&builder);
270 get_session_list (const gchar *seat_path)
272 GVariantBuilder builder;
276 g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
278 g_hash_table_iter_init (&iter, session_bus_entries);
279 while (g_hash_table_iter_next (&iter, NULL, &value))
281 SessionBusEntry *entry = value;
282 if (seat_path == NULL || strcmp (entry->seat_path, seat_path) == 0)
283 g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
286 return g_variant_builder_end (&builder);
290 handle_display_manager_get_property (GDBusConnection *connection,
292 const gchar *object_path,
293 const gchar *interface_name,
294 const gchar *property_name,
298 if (g_strcmp0 (property_name, "Seats") == 0)
299 return get_seat_list ();
300 else if (g_strcmp0 (property_name, "Sessions") == 0)
301 return get_session_list (NULL);
307 handle_display_manager_call (GDBusConnection *connection,
309 const gchar *object_path,
310 const gchar *interface_name,
311 const gchar *method_name,
312 GVariant *parameters,
313 GDBusMethodInvocation *invocation,
316 if (g_strcmp0 (method_name, "AddSeat") == 0)
319 GVariantIter *property_iter;
323 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa(ss))")))
325 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
329 g_variant_get (parameters, "(&sa(ss))", &type, &property_iter);
331 g_debug ("Adding seat of type %s", type);
333 seat = seat_new (type);
336 set_seat_properties (seat, NULL);
337 while (g_variant_iter_loop (property_iter, "(&s&s)", &name, &value))
338 seat_set_property (seat, name, value);
340 g_variant_iter_free (property_iter);
344 // FIXME: Need to make proper error
345 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create seat of type %s", type);
349 if (display_manager_add_seat (display_manager, seat))
353 entry = g_hash_table_lookup (seat_bus_entries, seat);
354 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
356 else// FIXME: Need to make proper error
357 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
358 g_object_unref (seat);
360 else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
365 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
367 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
371 g_variant_get (parameters, "(i)", &display_number);
373 g_debug ("Adding local X seat :%d", display_number);
375 seat = seat_new ("xremote");
378 gchar *display_number_string;
380 set_seat_properties (seat, NULL);
381 display_number_string = g_strdup_printf ("%d", display_number);
382 seat_set_property (seat, "xserver-display-number", display_number_string);
383 g_free (display_number_string);
388 // FIXME: Need to make proper error
389 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create local X seat");
393 if (display_manager_add_seat (display_manager, seat))
397 entry = g_hash_table_lookup (seat_bus_entries, seat);
398 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
400 else// FIXME: Need to make proper error
401 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
402 g_object_unref (seat);
405 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
409 handle_seat_get_property (GDBusConnection *connection,
411 const gchar *object_path,
412 const gchar *interface_name,
413 const gchar *property_name,
417 Seat *seat = user_data;
419 if (g_strcmp0 (property_name, "CanSwitch") == 0)
420 return g_variant_new_boolean (seat_get_can_switch (seat));
421 if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
422 return g_variant_new_boolean (seat_get_allow_guest (seat));
423 else if (g_strcmp0 (property_name, "Sessions") == 0)
427 entry = g_hash_table_lookup (seat_bus_entries, seat);
428 return get_session_list (entry->path);
435 handle_seat_call (GDBusConnection *connection,
437 const gchar *object_path,
438 const gchar *interface_name,
439 const gchar *method_name,
440 GVariant *parameters,
441 GDBusMethodInvocation *invocation,
444 Seat *seat = user_data;
446 if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
448 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
449 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
451 if (seat_switch_to_greeter (seat))
452 g_dbus_method_invocation_return_value (invocation, NULL);
453 else// FIXME: Need to make proper error
454 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to greeter");
456 else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
458 const gchar *username, *session_name;
460 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
461 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
463 g_variant_get (parameters, "(&s&s)", &username, &session_name);
464 if (strcmp (session_name, "") == 0)
467 if (seat_switch_to_user (seat, username, session_name))
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 switch to user");
472 else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
474 const gchar *session_name;
476 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
477 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
479 g_variant_get (parameters, "(&s)", &session_name);
480 if (strcmp (session_name, "") == 0)
483 if (seat_switch_to_guest (seat, session_name))
484 g_dbus_method_invocation_return_value (invocation, NULL);
485 else// FIXME: Need to make proper error
486 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to guest");
488 else if (g_strcmp0 (method_name, "Lock") == 0)
490 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
491 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
493 /* FIXME: Should only allow locks if have a session on this seat */
494 if (seat_lock (seat, NULL))
495 g_dbus_method_invocation_return_value (invocation, NULL);
496 else// FIXME: Need to make proper error
497 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to lock seat");
500 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
504 get_seat_for_session (Session *session)
508 for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
510 Seat *seat = seat_link->data;
513 for (session_link = seat_get_sessions (seat); session_link; session_link = session_link->next)
515 Session *s = session_link->data;
526 handle_session_get_property (GDBusConnection *connection,
528 const gchar *object_path,
529 const gchar *interface_name,
530 const gchar *property_name,
534 Session *session = user_data;
535 SessionBusEntry *entry;
537 entry = g_hash_table_lookup (session_bus_entries, session);
538 if (g_strcmp0 (property_name, "Seat") == 0)
539 return g_variant_new_object_path (entry ? entry->seat_path : "");
540 else if (g_strcmp0 (property_name, "UserName") == 0)
541 return g_variant_new_string (session_get_username (session));
547 handle_session_call (GDBusConnection *connection,
549 const gchar *object_path,
550 const gchar *interface_name,
551 const gchar *method_name,
552 GVariant *parameters,
553 GDBusMethodInvocation *invocation,
556 Session *session = user_data;
558 if (g_strcmp0 (method_name, "Lock") == 0)
562 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
563 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
565 seat = get_seat_for_session (session);
566 /* FIXME: Should only allow locks if have a session on this seat */
567 seat_lock (seat, session_get_username (session));
568 g_dbus_method_invocation_return_value (invocation, NULL);
571 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
574 static SeatBusEntry *
575 seat_bus_entry_new (const gchar *path)
579 entry = g_malloc0 (sizeof (SeatBusEntry));
580 entry->path = g_strdup (path);
585 static SessionBusEntry *
586 session_bus_entry_new (const gchar *path, const gchar *seat_path)
588 SessionBusEntry *entry;
590 entry = g_malloc0 (sizeof (SessionBusEntry));
591 entry->path = g_strdup (path);
592 entry->seat_path = g_strdup (seat_path);
598 emit_object_value_changed (GDBusConnection *bus, const gchar *path, const gchar *interface_name, const gchar *property_name, GVariant *property_value)
600 GVariantBuilder builder;
601 GError *error = NULL;
603 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
604 g_variant_builder_add (&builder, "{sv}", property_name, property_value);
606 if (!g_dbus_connection_emit_signal (bus,
609 "org.freedesktop.DBus.Properties",
611 g_variant_new ("(sa{sv}as)", interface_name, &builder, NULL),
613 g_warning ("Failed to emit PropertiesChanged signal: %s", error->message);
614 g_clear_error (&error);
618 emit_object_signal (GDBusConnection *bus, const gchar *path, const gchar *signal_name, const gchar *object_path)
620 GError *error = NULL;
622 if (!g_dbus_connection_emit_signal (bus,
625 "org.freedesktop.DisplayManager",
627 g_variant_new ("(o)", object_path),
629 g_warning ("Failed to emit %s signal on %s: %s", signal_name, path, error->message);
630 g_clear_error (&error);
634 seat_bus_entry_free (gpointer data)
636 SeatBusEntry *entry = data;
638 g_dbus_connection_unregister_object (bus, entry->bus_id);
640 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
641 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatRemoved", entry->path);
643 g_free (entry->path);
648 session_bus_entry_free (gpointer data)
650 SessionBusEntry *entry = data;
652 g_dbus_connection_unregister_object (bus, entry->bus_id);
654 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
655 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionRemoved", entry->path);
657 emit_object_value_changed (bus, entry->seat_path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (entry->seat_path));
658 emit_object_signal (bus, entry->seat_path, "SessionRemoved", entry->path);
660 g_free (entry->path);
661 g_free (entry->seat_path);
666 running_user_session_cb (Seat *seat, Session *session)
668 static const GDBusInterfaceVTable session_vtable =
671 handle_session_get_property
673 SeatBusEntry *seat_entry;
674 SessionBusEntry *session_entry;
676 GError *error = NULL;
678 /* Set environment variables when session runs */
679 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
680 session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
681 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
683 session_set_env (session, "XDG_SESSION_PATH", path);
684 g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
686 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
687 session_entry = session_bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL);
688 g_hash_table_insert (session_bus_entries, g_object_ref (session), session_entry);
690 g_debug ("Registering session with bus path %s", session_entry->path);
692 session_entry->bus_id = g_dbus_connection_register_object (bus,
694 session_info->interfaces[0],
696 g_object_ref (session), g_object_unref,
698 if (session_entry->bus_id == 0)
699 g_warning ("Failed to register user session: %s", error->message);
700 g_clear_error (&error);
702 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
703 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionAdded", session_entry->path);
705 emit_object_value_changed (bus, seat_entry->path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (session_entry->seat_path));
706 emit_object_signal (bus, seat_entry->path, "SessionAdded", session_entry->path);
710 session_removed_cb (Seat *seat, Session *session)
712 g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
713 g_hash_table_remove (session_bus_entries, session);
717 seat_added_cb (DisplayManager *display_manager, Seat *seat)
719 static const GDBusInterfaceVTable seat_vtable =
722 handle_seat_get_property
726 GError *error = NULL;
728 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
731 entry = seat_bus_entry_new (path);
733 g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
735 g_debug ("Registering seat with bus path %s", entry->path);
737 entry->bus_id = g_dbus_connection_register_object (bus,
739 seat_info->interfaces[0],
741 g_object_ref (seat), g_object_unref,
743 if (entry->bus_id == 0)
744 g_warning ("Failed to register seat: %s", error->message);
745 g_clear_error (&error);
747 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
748 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatAdded", entry->path);
750 g_signal_connect (seat, "running-user-session", G_CALLBACK (running_user_session_cb), NULL);
751 g_signal_connect (seat, "session-removed", G_CALLBACK (session_removed_cb), NULL);
755 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
757 g_hash_table_remove (seat_bus_entries, seat);
761 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
763 SeatXDMCPSession *seat;
766 seat = seat_xdmcp_session_new (session);
767 set_seat_properties (SEAT (seat), NULL);
768 result = display_manager_add_seat (display_manager, SEAT (seat));
769 g_object_unref (seat);
775 vnc_connection_cb (VNCServer *server, GSocket *connection)
779 seat = seat_xvnc_new (connection);
780 set_seat_properties (SEAT (seat), NULL);
781 display_manager_add_seat (display_manager, SEAT (seat));
782 g_object_unref (seat);
786 bus_acquired_cb (GDBusConnection *connection,
790 const gchar *display_manager_interface =
792 " <interface name='org.freedesktop.DisplayManager'>"
793 " <property name='Seats' type='ao' access='read'/>"
794 " <property name='Sessions' type='ao' access='read'/>"
795 " <method name='AddSeat'>"
796 " <arg name='type' direction='in' type='s'/>"
797 " <arg name='properties' direction='in' type='a(ss)'/>"
798 " <arg name='seat' direction='out' type='o'/>"
800 " <method name='AddLocalXSeat'>"
801 " <arg name='display-number' direction='in' type='i'/>"
802 " <arg name='seat' direction='out' type='o'/>"
804 " <signal name='SeatAdded'>"
805 " <arg name='seat' type='o'/>"
807 " <signal name='SeatRemoved'>"
808 " <arg name='seat' type='o'/>"
810 " <signal name='SessionAdded'>"
811 " <arg name='session' type='o'/>"
813 " <signal name='SessionRemoved'>"
814 " <arg name='session' type='o'/>"
818 static const GDBusInterfaceVTable display_manager_vtable =
820 handle_display_manager_call,
821 handle_display_manager_get_property
823 const gchar *seat_interface =
825 " <interface name='org.freedesktop.DisplayManager.Seat'>"
826 " <property name='CanSwitch' type='b' access='read'/>"
827 " <property name='HasGuestAccount' type='b' access='read'/>"
828 " <property name='Sessions' type='ao' access='read'/>"
829 " <method name='SwitchToGreeter'/>"
830 " <method name='SwitchToUser'>"
831 " <arg name='username' direction='in' type='s'/>"
832 " <arg name='session-name' direction='in' type='s'/>"
834 " <method name='SwitchToGuest'>"
835 " <arg name='session-name' direction='in' type='s'/>"
837 " <method name='Lock'/>"
838 " <signal name='SessionAdded'>"
839 " <arg name='session' type='o'/>"
841 " <signal name='SessionRemoved'>"
842 " <arg name='session' type='o'/>"
846 const gchar *session_interface =
848 " <interface name='org.freedesktop.DisplayManager.Session'>"
849 " <property name='Seat' type='o' access='read'/>"
850 " <property name='UserName' type='s' access='read'/>"
851 " <method name='Lock'/>"
854 GDBusNodeInfo *display_manager_info;
856 GError *error = NULL;
858 g_debug ("Acquired bus name %s", name);
862 display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
863 g_assert (display_manager_info != NULL);
864 seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
865 g_assert (seat_info != NULL);
866 session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
867 g_assert (session_info != NULL);
869 reg_id = g_dbus_connection_register_object (connection,
870 "/org/freedesktop/DisplayManager",
871 display_manager_info->interfaces[0],
872 &display_manager_vtable,
876 g_warning ("Failed to register display manager: %s", error->message);
877 g_clear_error (&error);
878 g_dbus_node_info_unref (display_manager_info);
880 seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, seat_bus_entry_free);
881 session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, session_bus_entry_free);
883 g_signal_connect (display_manager, "seat-added", G_CALLBACK (seat_added_cb), NULL);
884 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (seat_removed_cb), NULL);
885 for (link = display_manager_get_seats (display_manager); link; link = link->next)
886 seat_added_cb (display_manager, (Seat *) link->data);
888 display_manager_start (display_manager);
890 /* Start the XDMCP server */
891 if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
893 gchar *key_name, *key = NULL;
895 xdmcp_server = xdmcp_server_new ();
896 if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
899 port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
901 xdmcp_server_set_port (xdmcp_server, port);
903 g_signal_connect (xdmcp_server, "new-session", G_CALLBACK (xdmcp_session_cb), NULL);
905 key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
911 GError *error = NULL;
913 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
914 path = g_build_filename (dir, "keys.conf", NULL);
917 keys = g_key_file_new ();
918 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
920 g_debug ("Error getting key %s", error->message);
921 g_clear_error (&error);
925 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
926 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
928 g_debug ("Key %s not defined", key_name);
931 g_key_file_free (keys);
934 xdmcp_server_set_key (xdmcp_server, key);
938 g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
939 xdmcp_server_start (xdmcp_server);
942 /* Start the VNC server */
943 if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
947 path = g_find_program_in_path ("Xvnc");
950 vnc_server = vnc_server_new ();
951 if (config_has_key (config_get_instance (), "VNCServer", "port"))
954 port = config_get_integer (config_get_instance (), "VNCServer", "port");
956 vnc_server_set_port (vnc_server, port);
958 g_signal_connect (vnc_server, "new-connection", G_CALLBACK (vnc_connection_cb), NULL);
960 g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
961 vnc_server_start (vnc_server);
966 g_warning ("Can't start VNC server, Xvnc is not in the path");
971 name_lost_cb (GDBusConnection *connection,
976 g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
978 g_printerr ("Failed to get D-Bus connection\n");
984 main (int argc, char **argv)
987 GOptionContext *option_context;
989 gchar **groups, **i, *dir;
991 gboolean test_mode = FALSE;
992 gchar *pid_path = "/var/run/lightdm.pid";
993 gchar *log_dir = NULL;
994 gchar *run_dir = NULL;
995 gchar *cache_dir = NULL;
996 gchar *default_log_dir = g_strdup (LOG_DIR);
997 gchar *default_run_dir = g_strdup (RUN_DIR);
998 gchar *default_cache_dir = g_strdup (CACHE_DIR);
999 gboolean show_version = FALSE;
1000 GList *link, *messages = NULL;
1001 GOptionEntry options[] =
1003 { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
1004 /* Help string for command line --config flag */
1005 N_("Use configuration file"), "FILE" },
1006 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
1007 /* Help string for command line --debug flag */
1008 N_("Print debugging messages"), NULL },
1009 { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
1010 /* Help string for command line --test-mode flag */
1011 N_("Run as unprivileged user, skipping things that require root access"), NULL },
1012 { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
1013 /* Help string for command line --pid-file flag */
1014 N_("File to write PID into"), "FILE" },
1015 { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
1016 /* Help string for command line --log-dir flag */
1017 N_("Directory to write logs to"), "DIRECTORY" },
1018 { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
1019 /* Help string for command line --run-dir flag */
1020 N_("Directory to store running state"), "DIRECTORY" },
1021 { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
1022 /* Help string for command line --cache-dir flag */
1023 N_("Directory to cache information"), "DIRECTORY" },
1024 { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
1025 /* Help string for command line --version flag */
1026 N_("Show release version"), NULL },
1029 GError *error = NULL;
1031 /* When lightdm starts sessions it needs to run itself in a new mode */
1032 if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
1033 return session_child_run (argc, argv);
1035 #if !defined(GLIB_VERSION_2_36)
1038 loop = g_main_loop_new (NULL, FALSE);
1040 messages = g_list_append (messages, g_strdup_printf ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ()));
1042 g_signal_connect (process_get_current (), "got-signal", G_CALLBACK (signal_cb), NULL);
1044 option_context = g_option_context_new (/* Arguments and description for --help test */
1045 _("- Display Manager"));
1046 g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
1047 result = g_option_context_parse (option_context, &argc, &argv, &error);
1049 g_printerr ("%s\n", error->message);
1050 g_clear_error (&error);
1051 g_option_context_free (option_context);
1054 g_printerr (/* Text printed out when an unknown command-line argument provided */
1055 _("Run '%s --help' to see a full list of available command line options."), argv[0]);
1057 return EXIT_FAILURE;
1062 /* NOTE: Is not translated so can be easily parsed */
1063 g_printerr ("lightdm %s\n", VERSION);
1064 return EXIT_SUCCESS;
1067 if (!test_mode && getuid () != 0)
1069 g_printerr ("Only root can run Light Display Manager. To run as a regular user for testing run with the --test-mode flag.\n");
1070 return EXIT_FAILURE;
1073 /* If running inside an X server use Xephyr for display */
1074 if (getenv ("DISPLAY") && getuid () != 0)
1076 gchar *x_server_path;
1078 x_server_path = g_find_program_in_path ("Xephyr");
1081 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");
1082 return EXIT_FAILURE;
1084 g_free (x_server_path);
1087 /* Make sure the system binary directory (where the greeters are installed) is in the path */
1090 const gchar *path = g_getenv ("PATH");
1094 new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
1096 new_path = g_strdup (SBIN_DIR);
1097 g_setenv ("PATH", new_path, TRUE);
1101 /* Write PID file */
1102 pid_file = fopen (pid_path, "w");
1105 fprintf (pid_file, "%d\n", getpid ());
1109 /* If not running as root write output to directories we control */
1112 g_free (default_log_dir);
1113 default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
1114 g_free (default_run_dir);
1115 default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
1116 g_free (default_cache_dir);
1117 default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
1120 /* Load config file(s) */
1121 if (!config_load_from_standard_locations (config_get_instance (), config_path, &messages))
1122 exit (EXIT_FAILURE);
1123 g_free (config_path);
1125 /* Set default values */
1126 if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
1127 config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
1128 if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
1129 config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
1130 if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
1131 config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
1132 if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
1133 config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
1134 if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
1135 config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
1136 if (!config_has_key (config_get_instance (), "SeatDefaults", "type"))
1137 config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal");
1138 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-service"))
1139 config_set_string (config_get_instance (), "SeatDefaults", "pam-service", "lightdm");
1140 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-autologin-service"))
1141 config_set_string (config_get_instance (), "SeatDefaults", "pam-autologin-service", "lightdm-autologin");
1142 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-greeter-service"))
1143 config_set_string (config_get_instance (), "SeatDefaults", "pam-greeter-service", "lightdm-greeter");
1144 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-command"))
1145 config_set_string (config_get_instance (), "SeatDefaults", "xserver-command", "X");
1146 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-share"))
1147 config_set_boolean (config_get_instance (), "SeatDefaults", "xserver-share", TRUE);
1148 if (!config_has_key (config_get_instance (), "SeatDefaults", "unity-compositor-command"))
1149 config_set_string (config_get_instance (), "SeatDefaults", "unity-compositor-command", "unity-system-compositor");
1150 if (!config_has_key (config_get_instance (), "SeatDefaults", "start-session"))
1151 config_set_boolean (config_get_instance (), "SeatDefaults", "start-session", TRUE);
1152 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-user-switching"))
1153 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-user-switching", TRUE);
1154 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-guest"))
1155 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-guest", TRUE);
1156 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-allow-guest"))
1157 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-allow-guest", TRUE);
1158 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-show-remote-login"))
1159 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-show-remote-login", TRUE);
1160 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-session"))
1161 config_set_string (config_get_instance (), "SeatDefaults", "greeter-session", GREETER_SESSION);
1162 if (!config_has_key (config_get_instance (), "SeatDefaults", "user-session"))
1163 config_set_string (config_get_instance (), "SeatDefaults", "user-session", USER_SESSION);
1164 if (!config_has_key (config_get_instance (), "SeatDefaults", "session-wrapper"))
1165 config_set_string (config_get_instance (), "SeatDefaults", "session-wrapper", "lightdm-session");
1166 if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
1167 config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
1168 g_free (default_log_dir);
1169 if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
1170 config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
1171 g_free (default_run_dir);
1172 if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
1173 config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
1174 g_free (default_cache_dir);
1175 if (!config_has_key (config_get_instance (), "LightDM", "sessions-directory"))
1176 config_set_string (config_get_instance (), "LightDM", "sessions-directory", SESSIONS_DIR);
1177 if (!config_has_key (config_get_instance (), "LightDM", "remote-sessions-directory"))
1178 config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR);
1179 if (!config_has_key (config_get_instance (), "LightDM", "greeters-directory"))
1180 config_set_string (config_get_instance (), "LightDM", "greeters-directory", GREETERS_DIR);
1182 /* Override defaults */
1184 config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
1187 config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
1190 config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
1193 /* Create run and cache directories */
1194 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
1195 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1196 g_warning ("Failed to make log directory %s: %s", dir, strerror (errno));
1198 dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
1199 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1200 g_warning ("Failed to make run directory %s: %s", dir, strerror (errno));
1202 dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
1203 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1204 g_warning ("Failed to make cache directory %s: %s", dir, strerror (errno));
1209 /* Show queued messages once logging is complete */
1210 for (link = messages; link; link = link->next)
1211 g_debug ("%s", (gchar *)link->data);
1212 g_list_free_full (messages, g_free);
1214 g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
1215 bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
1217 G_BUS_NAME_OWNER_FLAGS_NONE,
1225 g_debug ("Running in user mode");
1226 if (getenv ("DISPLAY"))
1227 g_debug ("Using Xephyr for X servers");
1229 display_manager = display_manager_new ();
1230 g_signal_connect (display_manager, "stopped", G_CALLBACK (display_manager_stopped_cb), NULL);
1231 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (display_manager_seat_removed_cb), NULL);
1233 shared_data_manager_start (shared_data_manager_get_instance ());
1235 /* Load the static display entries */
1236 groups = config_get_groups (config_get_instance ());
1237 for (i = groups; *i; i++)
1239 gchar *config_section = *i;
1243 const gchar *const seatpfx = "Seat:";
1245 if (!g_str_has_prefix (config_section, seatpfx))
1248 g_debug ("Loading seat %s", config_section);
1249 types = config_get_string_list (config_get_instance (), config_section, "type");
1251 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1252 for (type = types; type && *type; type++)
1254 seat = seat_new (*type);
1261 const gsize seatpfxlen = strlen(seatpfx);
1262 gchar *seatname = config_section + seatpfxlen;
1264 seat_set_property (seat, "seat-name", seatname);
1266 set_seat_properties (seat, config_section);
1267 display_manager_add_seat (display_manager, seat);
1268 g_object_unref (seat);
1272 g_warning ("Failed to create seat %s", config_section);
1274 g_strfreev (groups);
1276 /* If no seats start a default one */
1277 if (n_seats == 0 && config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1283 g_debug ("Adding default seat");
1285 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1286 for (type = types; type && *type; type++)
1288 seat = seat_new (*type);
1295 set_seat_properties (seat, NULL);
1296 seat_set_property (seat, "exit-on-failure", "true");
1297 if (!display_manager_add_seat (display_manager, seat))
1298 return EXIT_FAILURE;
1299 g_object_unref (seat);
1303 g_warning ("Failed to create default seat");
1304 return EXIT_FAILURE;
1308 g_main_loop_run (loop);
1310 /* Clean up shared data manager */
1311 shared_data_manager_cleanup ();
1313 /* Clean up user list */
1314 common_user_list_cleanup ();
1316 /* Clean up display manager */
1317 g_object_unref (display_manager);
1318 display_manager = NULL;
1320 /* Remove D-Bus interface */
1321 g_dbus_connection_unregister_object (bus, reg_id);
1322 g_bus_unown_name (bus_id);
1323 if (seat_bus_entries)
1324 g_hash_table_unref (seat_bus_entries);
1325 if (session_bus_entries)
1326 g_hash_table_unref (session_bus_entries);
1328 g_debug ("Exiting with return value %d", exit_code);