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))")))
326 g_variant_get (parameters, "(&sa(ss))", &type, &property_iter);
328 g_debug ("Adding seat of type %s", type);
330 seat = seat_new (type);
333 set_seat_properties (seat, NULL);
334 while (g_variant_iter_loop (property_iter, "(&s&s)", &name, &value))
335 seat_set_property (seat, name, value);
337 g_variant_iter_free (property_iter);
341 // FIXME: Need to make proper error
342 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create seat of type %s", type);
346 if (display_manager_add_seat (display_manager, seat))
350 entry = g_hash_table_lookup (seat_bus_entries, seat);
351 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
353 else// FIXME: Need to make proper error
354 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
355 g_object_unref (seat);
357 else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
362 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
365 g_variant_get (parameters, "(i)", &display_number);
367 g_debug ("Adding local X seat :%d", display_number);
369 seat = seat_new ("xremote");
372 gchar *display_number_string;
374 set_seat_properties (seat, NULL);
375 display_number_string = g_strdup_printf ("%d", display_number);
376 seat_set_property (seat, "xserver-display-number", display_number_string);
377 g_free (display_number_string);
382 // FIXME: Need to make proper error
383 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create local X seat");
387 if (display_manager_add_seat (display_manager, seat))
391 entry = g_hash_table_lookup (seat_bus_entries, seat);
392 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
394 else// FIXME: Need to make proper error
395 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
396 g_object_unref (seat);
401 handle_seat_get_property (GDBusConnection *connection,
403 const gchar *object_path,
404 const gchar *interface_name,
405 const gchar *property_name,
409 Seat *seat = user_data;
411 if (g_strcmp0 (property_name, "CanSwitch") == 0)
412 return g_variant_new_boolean (seat_get_can_switch (seat));
413 if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
414 return g_variant_new_boolean (seat_get_allow_guest (seat));
415 else if (g_strcmp0 (property_name, "Sessions") == 0)
419 entry = g_hash_table_lookup (seat_bus_entries, seat);
420 return get_session_list (entry->path);
427 handle_seat_call (GDBusConnection *connection,
429 const gchar *object_path,
430 const gchar *interface_name,
431 const gchar *method_name,
432 GVariant *parameters,
433 GDBusMethodInvocation *invocation,
436 Seat *seat = user_data;
438 if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
440 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
443 seat_switch_to_greeter (seat);
444 g_dbus_method_invocation_return_value (invocation, NULL);
446 else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
448 const gchar *username, *session_name;
450 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
453 g_variant_get (parameters, "(&s&s)", &username, &session_name);
454 if (strcmp (session_name, "") == 0)
457 seat_switch_to_user (seat, username, session_name);
458 g_dbus_method_invocation_return_value (invocation, NULL);
460 else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
462 const gchar *session_name;
464 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
467 g_variant_get (parameters, "(&s)", &session_name);
468 if (strcmp (session_name, "") == 0)
471 seat_switch_to_guest (seat, session_name);
472 g_dbus_method_invocation_return_value (invocation, NULL);
474 else if (g_strcmp0 (method_name, "Lock") == 0)
476 /* FIXME: Should only allow locks if have a session on this seat */
477 seat_lock (seat, NULL);
478 g_dbus_method_invocation_return_value (invocation, NULL);
483 get_seat_for_session (Session *session)
487 for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
489 Seat *seat = seat_link->data;
492 for (session_link = seat_get_sessions (seat); session_link; session_link = session_link->next)
494 Session *s = session_link->data;
505 handle_session_get_property (GDBusConnection *connection,
507 const gchar *object_path,
508 const gchar *interface_name,
509 const gchar *property_name,
513 Session *session = user_data;
514 SessionBusEntry *entry;
516 entry = g_hash_table_lookup (session_bus_entries, session);
517 if (g_strcmp0 (property_name, "Seat") == 0)
518 return g_variant_new_object_path (entry ? entry->seat_path : "");
519 else if (g_strcmp0 (property_name, "UserName") == 0)
520 return g_variant_new_string (session_get_username (session));
526 handle_session_call (GDBusConnection *connection,
528 const gchar *object_path,
529 const gchar *interface_name,
530 const gchar *method_name,
531 GVariant *parameters,
532 GDBusMethodInvocation *invocation,
535 Session *session = user_data;
537 if (g_strcmp0 (method_name, "Lock") == 0)
541 seat = get_seat_for_session (session);
542 /* FIXME: Should only allow locks if have a session on this seat */
543 seat_lock (seat, session_get_username (session));
544 g_dbus_method_invocation_return_value (invocation, NULL);
548 static SeatBusEntry *
549 seat_bus_entry_new (const gchar *path)
553 entry = g_malloc0 (sizeof (SeatBusEntry));
554 entry->path = g_strdup (path);
559 static SessionBusEntry *
560 session_bus_entry_new (const gchar *path, const gchar *seat_path)
562 SessionBusEntry *entry;
564 entry = g_malloc0 (sizeof (SessionBusEntry));
565 entry->path = g_strdup (path);
566 entry->seat_path = g_strdup (seat_path);
572 emit_object_value_changed (GDBusConnection *bus, const gchar *path, const gchar *interface_name, const gchar *property_name, GVariant *property_value)
574 GVariantBuilder builder;
575 GError *error = NULL;
577 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
578 g_variant_builder_add (&builder, "{sv}", property_name, property_value);
580 if (!g_dbus_connection_emit_signal (bus,
583 "org.freedesktop.DBus.Properties",
585 g_variant_new ("(sa{sv}as)", interface_name, &builder, NULL),
587 g_warning ("Failed to emit PropertiesChanged signal: %s", error->message);
588 g_clear_error (&error);
592 emit_object_signal (GDBusConnection *bus, const gchar *path, const gchar *signal_name, const gchar *object_path)
594 GError *error = NULL;
596 if (!g_dbus_connection_emit_signal (bus,
599 "org.freedesktop.DisplayManager",
601 g_variant_new ("(o)", object_path),
603 g_warning ("Failed to emit %s signal on %s: %s", signal_name, path, error->message);
604 g_clear_error (&error);
608 seat_bus_entry_free (gpointer data)
610 SeatBusEntry *entry = data;
612 g_dbus_connection_unregister_object (bus, entry->bus_id);
614 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
615 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatRemoved", entry->path);
617 g_free (entry->path);
622 session_bus_entry_free (gpointer data)
624 SessionBusEntry *entry = data;
626 g_dbus_connection_unregister_object (bus, entry->bus_id);
628 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
629 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionRemoved", entry->path);
631 emit_object_value_changed (bus, entry->seat_path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (entry->seat_path));
632 emit_object_signal (bus, entry->seat_path, "SessionRemoved", entry->path);
634 g_free (entry->path);
635 g_free (entry->seat_path);
640 running_user_session_cb (Seat *seat, Session *session)
642 static const GDBusInterfaceVTable session_vtable =
645 handle_session_get_property
647 SeatBusEntry *seat_entry;
648 SessionBusEntry *session_entry;
650 GError *error = NULL;
652 /* Set environment variables when session runs */
653 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
654 session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
655 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
657 session_set_env (session, "XDG_SESSION_PATH", path);
658 g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
660 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
661 session_entry = session_bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL);
662 g_hash_table_insert (session_bus_entries, g_object_ref (session), session_entry);
664 g_debug ("Registering session with bus path %s", session_entry->path);
666 session_entry->bus_id = g_dbus_connection_register_object (bus,
668 session_info->interfaces[0],
670 g_object_ref (session), g_object_unref,
672 if (session_entry->bus_id == 0)
673 g_warning ("Failed to register user session: %s", error->message);
674 g_clear_error (&error);
676 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
677 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionAdded", session_entry->path);
679 emit_object_value_changed (bus, seat_entry->path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (session_entry->seat_path));
680 emit_object_signal (bus, seat_entry->path, "SessionAdded", session_entry->path);
684 session_removed_cb (Seat *seat, Session *session)
686 g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
687 g_hash_table_remove (session_bus_entries, session);
691 seat_added_cb (DisplayManager *display_manager, Seat *seat)
693 static const GDBusInterfaceVTable seat_vtable =
696 handle_seat_get_property
700 GError *error = NULL;
702 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
705 entry = seat_bus_entry_new (path);
707 g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
709 g_debug ("Registering seat with bus path %s", entry->path);
711 entry->bus_id = g_dbus_connection_register_object (bus,
713 seat_info->interfaces[0],
715 g_object_ref (seat), g_object_unref,
717 if (entry->bus_id == 0)
718 g_warning ("Failed to register seat: %s", error->message);
719 g_clear_error (&error);
721 emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
722 emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatAdded", entry->path);
724 g_signal_connect (seat, "running-user-session", G_CALLBACK (running_user_session_cb), NULL);
725 g_signal_connect (seat, "session-removed", G_CALLBACK (session_removed_cb), NULL);
729 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
731 g_hash_table_remove (seat_bus_entries, seat);
735 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
737 SeatXDMCPSession *seat;
740 seat = seat_xdmcp_session_new (session);
741 set_seat_properties (SEAT (seat), NULL);
742 result = display_manager_add_seat (display_manager, SEAT (seat));
743 g_object_unref (seat);
749 vnc_connection_cb (VNCServer *server, GSocket *connection)
753 seat = seat_xvnc_new (connection);
754 set_seat_properties (SEAT (seat), NULL);
755 display_manager_add_seat (display_manager, SEAT (seat));
756 g_object_unref (seat);
760 bus_acquired_cb (GDBusConnection *connection,
764 const gchar *display_manager_interface =
766 " <interface name='org.freedesktop.DisplayManager'>"
767 " <property name='Seats' type='ao' access='read'/>"
768 " <property name='Sessions' type='ao' access='read'/>"
769 " <method name='AddSeat'>"
770 " <arg name='type' direction='in' type='s'/>"
771 " <arg name='properties' direction='in' type='a(ss)'/>"
772 " <arg name='seat' direction='out' type='o'/>"
774 " <method name='AddLocalXSeat'>"
775 " <arg name='display-number' direction='in' type='i'/>"
776 " <arg name='seat' direction='out' type='o'/>"
778 " <signal name='SeatAdded'>"
779 " <arg name='seat' type='o'/>"
781 " <signal name='SeatRemoved'>"
782 " <arg name='seat' type='o'/>"
784 " <signal name='SessionAdded'>"
785 " <arg name='session' type='o'/>"
787 " <signal name='SessionRemoved'>"
788 " <arg name='session' type='o'/>"
792 static const GDBusInterfaceVTable display_manager_vtable =
794 handle_display_manager_call,
795 handle_display_manager_get_property
797 const gchar *seat_interface =
799 " <interface name='org.freedesktop.DisplayManager.Seat'>"
800 " <property name='CanSwitch' type='b' access='read'/>"
801 " <property name='HasGuestAccount' type='b' access='read'/>"
802 " <property name='Sessions' type='ao' access='read'/>"
803 " <method name='SwitchToGreeter'/>"
804 " <method name='SwitchToUser'>"
805 " <arg name='username' direction='in' type='s'/>"
806 " <arg name='session-name' direction='in' type='s'/>"
808 " <method name='SwitchToGuest'>"
809 " <arg name='session-name' direction='in' type='s'/>"
811 " <method name='Lock'/>"
812 " <signal name='SessionAdded'>"
813 " <arg name='session' type='o'/>"
815 " <signal name='SessionRemoved'>"
816 " <arg name='session' type='o'/>"
820 const gchar *session_interface =
822 " <interface name='org.freedesktop.DisplayManager.Session'>"
823 " <property name='Seat' type='o' access='read'/>"
824 " <property name='UserName' type='s' access='read'/>"
825 " <method name='Lock'/>"
828 GDBusNodeInfo *display_manager_info;
830 GError *error = NULL;
832 g_debug ("Acquired bus name %s", name);
836 display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
837 g_assert (display_manager_info != NULL);
838 seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
839 g_assert (seat_info != NULL);
840 session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
841 g_assert (session_info != NULL);
843 reg_id = g_dbus_connection_register_object (connection,
844 "/org/freedesktop/DisplayManager",
845 display_manager_info->interfaces[0],
846 &display_manager_vtable,
850 g_warning ("Failed to register display manager: %s", error->message);
851 g_clear_error (&error);
852 g_dbus_node_info_unref (display_manager_info);
854 seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, seat_bus_entry_free);
855 session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, session_bus_entry_free);
857 g_signal_connect (display_manager, "seat-added", G_CALLBACK (seat_added_cb), NULL);
858 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (seat_removed_cb), NULL);
859 for (link = display_manager_get_seats (display_manager); link; link = link->next)
860 seat_added_cb (display_manager, (Seat *) link->data);
862 display_manager_start (display_manager);
864 /* Start the XDMCP server */
865 if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
867 gchar *key_name, *key = NULL;
869 xdmcp_server = xdmcp_server_new ();
870 if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
873 port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
875 xdmcp_server_set_port (xdmcp_server, port);
877 g_signal_connect (xdmcp_server, "new-session", G_CALLBACK (xdmcp_session_cb), NULL);
879 key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
885 GError *error = NULL;
887 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
888 path = g_build_filename (dir, "keys.conf", NULL);
891 keys = g_key_file_new ();
892 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
894 g_debug ("Error getting key %s", error->message);
895 g_clear_error (&error);
899 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
900 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
902 g_debug ("Key %s not defined", key_name);
905 g_key_file_free (keys);
908 xdmcp_server_set_key (xdmcp_server, key);
912 g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
913 xdmcp_server_start (xdmcp_server);
916 /* Start the VNC server */
917 if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
921 path = g_find_program_in_path ("Xvnc");
924 vnc_server = vnc_server_new ();
925 if (config_has_key (config_get_instance (), "VNCServer", "port"))
928 port = config_get_integer (config_get_instance (), "VNCServer", "port");
930 vnc_server_set_port (vnc_server, port);
932 g_signal_connect (vnc_server, "new-connection", G_CALLBACK (vnc_connection_cb), NULL);
934 g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
935 vnc_server_start (vnc_server);
940 g_warning ("Can't start VNC server, Xvnc is not in the path");
945 name_lost_cb (GDBusConnection *connection,
950 g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
952 g_printerr ("Failed to get D-Bus connection\n");
958 main (int argc, char **argv)
961 GOptionContext *option_context;
963 gchar **groups, **i, *dir;
965 gboolean test_mode = FALSE;
966 gchar *pid_path = "/var/run/lightdm.pid";
967 gchar *log_dir = NULL;
968 gchar *run_dir = NULL;
969 gchar *cache_dir = NULL;
970 gchar *default_log_dir = g_strdup (LOG_DIR);
971 gchar *default_run_dir = g_strdup (RUN_DIR);
972 gchar *default_cache_dir = g_strdup (CACHE_DIR);
973 gboolean show_version = FALSE;
974 GList *link, *messages = NULL;
975 GOptionEntry options[] =
977 { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
978 /* Help string for command line --config flag */
979 N_("Use configuration file"), "FILE" },
980 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
981 /* Help string for command line --debug flag */
982 N_("Print debugging messages"), NULL },
983 { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
984 /* Help string for command line --test-mode flag */
985 N_("Run as unprivileged user, skipping things that require root access"), NULL },
986 { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
987 /* Help string for command line --pid-file flag */
988 N_("File to write PID into"), "FILE" },
989 { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
990 /* Help string for command line --log-dir flag */
991 N_("Directory to write logs to"), "DIRECTORY" },
992 { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
993 /* Help string for command line --run-dir flag */
994 N_("Directory to store running state"), "DIRECTORY" },
995 { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
996 /* Help string for command line --cache-dir flag */
997 N_("Directory to cache information"), "DIRECTORY" },
998 { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
999 /* Help string for command line --version flag */
1000 N_("Show release version"), NULL },
1003 GError *error = NULL;
1005 /* When lightdm starts sessions it needs to run itself in a new mode */
1006 if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
1007 return session_child_run (argc, argv);
1009 #if !defined(GLIB_VERSION_2_36)
1012 loop = g_main_loop_new (NULL, FALSE);
1014 messages = g_list_append (messages, g_strdup_printf ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ()));
1016 g_signal_connect (process_get_current (), "got-signal", G_CALLBACK (signal_cb), NULL);
1018 option_context = g_option_context_new (/* Arguments and description for --help test */
1019 _("- Display Manager"));
1020 g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
1021 result = g_option_context_parse (option_context, &argc, &argv, &error);
1023 g_printerr ("%s\n", error->message);
1024 g_clear_error (&error);
1025 g_option_context_free (option_context);
1028 g_printerr (/* Text printed out when an unknown command-line argument provided */
1029 _("Run '%s --help' to see a full list of available command line options."), argv[0]);
1031 return EXIT_FAILURE;
1036 /* NOTE: Is not translated so can be easily parsed */
1037 g_printerr ("lightdm %s\n", VERSION);
1038 return EXIT_SUCCESS;
1041 if (!test_mode && getuid () != 0)
1043 g_printerr ("Only root can run Light Display Manager. To run as a regular user for testing run with the --test-mode flag.\n");
1044 return EXIT_FAILURE;
1047 /* If running inside an X server use Xephyr for display */
1048 if (getenv ("DISPLAY") && getuid () != 0)
1050 gchar *x_server_path;
1052 x_server_path = g_find_program_in_path ("Xephyr");
1055 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");
1056 return EXIT_FAILURE;
1058 g_free (x_server_path);
1061 /* Make sure the system binary directory (where the greeters are installed) is in the path */
1064 const gchar *path = g_getenv ("PATH");
1068 new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
1070 new_path = g_strdup (SBIN_DIR);
1071 g_setenv ("PATH", new_path, TRUE);
1075 /* Write PID file */
1076 pid_file = fopen (pid_path, "w");
1079 fprintf (pid_file, "%d\n", getpid ());
1083 /* If not running as root write output to directories we control */
1086 g_free (default_log_dir);
1087 default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
1088 g_free (default_run_dir);
1089 default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
1090 g_free (default_cache_dir);
1091 default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
1094 /* Load config file(s) */
1095 if (!config_load_from_standard_locations (config_get_instance (), config_path, &messages))
1096 exit (EXIT_FAILURE);
1097 g_free (config_path);
1099 /* Set default values */
1100 if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
1101 config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
1102 if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
1103 config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
1104 if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
1105 config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
1106 if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
1107 config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
1108 if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
1109 config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
1110 if (!config_has_key (config_get_instance (), "SeatDefaults", "type"))
1111 config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal");
1112 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-service"))
1113 config_set_string (config_get_instance (), "SeatDefaults", "pam-service", "lightdm");
1114 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-autologin-service"))
1115 config_set_string (config_get_instance (), "SeatDefaults", "pam-autologin-service", "lightdm-autologin");
1116 if (!config_has_key (config_get_instance (), "SeatDefaults", "pam-greeter-service"))
1117 config_set_string (config_get_instance (), "SeatDefaults", "pam-greeter-service", "lightdm-greeter");
1118 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-command"))
1119 config_set_string (config_get_instance (), "SeatDefaults", "xserver-command", "X");
1120 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-share"))
1121 config_set_boolean (config_get_instance (), "SeatDefaults", "xserver-share", TRUE);
1122 if (!config_has_key (config_get_instance (), "SeatDefaults", "unity-compositor-command"))
1123 config_set_string (config_get_instance (), "SeatDefaults", "unity-compositor-command", "unity-system-compositor");
1124 if (!config_has_key (config_get_instance (), "SeatDefaults", "start-session"))
1125 config_set_boolean (config_get_instance (), "SeatDefaults", "start-session", TRUE);
1126 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-guest"))
1127 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-guest", TRUE);
1128 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-allow-guest"))
1129 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-allow-guest", TRUE);
1130 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-show-remote-login"))
1131 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-show-remote-login", TRUE);
1132 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-session"))
1133 config_set_string (config_get_instance (), "SeatDefaults", "greeter-session", GREETER_SESSION);
1134 if (!config_has_key (config_get_instance (), "SeatDefaults", "user-session"))
1135 config_set_string (config_get_instance (), "SeatDefaults", "user-session", USER_SESSION);
1136 if (!config_has_key (config_get_instance (), "SeatDefaults", "session-wrapper"))
1137 config_set_string (config_get_instance (), "SeatDefaults", "session-wrapper", "lightdm-session");
1138 if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
1139 config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
1140 g_free (default_log_dir);
1141 if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
1142 config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
1143 g_free (default_run_dir);
1144 if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
1145 config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
1146 g_free (default_cache_dir);
1147 if (!config_has_key (config_get_instance (), "LightDM", "sessions-directory"))
1148 config_set_string (config_get_instance (), "LightDM", "sessions-directory", SESSIONS_DIR);
1149 if (!config_has_key (config_get_instance (), "LightDM", "remote-sessions-directory"))
1150 config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR);
1151 if (!config_has_key (config_get_instance (), "LightDM", "greeters-directory"))
1152 config_set_string (config_get_instance (), "LightDM", "greeters-directory", GREETERS_DIR);
1154 /* Override defaults */
1156 config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
1159 config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
1162 config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
1165 /* Create run and cache directories */
1166 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
1167 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1168 g_warning ("Failed to make log directory %s: %s", dir, strerror (errno));
1170 dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
1171 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1172 g_warning ("Failed to make run directory %s: %s", dir, strerror (errno));
1174 dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
1175 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1176 g_warning ("Failed to make cache directory %s: %s", dir, strerror (errno));
1181 /* Show queued messages once logging is complete */
1182 for (link = messages; link; link = link->next)
1183 g_debug ("%s", (gchar *)link->data);
1184 g_list_free_full (messages, g_free);
1186 g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
1187 bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
1189 G_BUS_NAME_OWNER_FLAGS_NONE,
1197 g_debug ("Running in user mode");
1198 if (getenv ("DISPLAY"))
1199 g_debug ("Using Xephyr for X servers");
1201 display_manager = display_manager_new ();
1202 g_signal_connect (display_manager, "stopped", G_CALLBACK (display_manager_stopped_cb), NULL);
1203 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (display_manager_seat_removed_cb), NULL);
1205 shared_data_manager_start (shared_data_manager_get_instance ());
1207 /* Load the static display entries */
1208 groups = config_get_groups (config_get_instance ());
1209 for (i = groups; *i; i++)
1211 gchar *config_section = *i;
1215 const gchar *const seatpfx = "Seat:";
1217 if (!g_str_has_prefix (config_section, seatpfx))
1220 g_debug ("Loading seat %s", config_section);
1221 types = config_get_string_list (config_get_instance (), config_section, "type");
1223 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1224 for (type = types; type && *type; type++)
1226 seat = seat_new (*type);
1233 const gsize seatpfxlen = strlen(seatpfx);
1234 gchar *seatname = config_section + seatpfxlen;
1236 seat_set_property (seat, "seat-name", seatname);
1238 set_seat_properties (seat, config_section);
1239 display_manager_add_seat (display_manager, seat);
1240 g_object_unref (seat);
1244 g_warning ("Failed to create seat %s", config_section);
1246 g_strfreev (groups);
1248 /* If no seats start a default one */
1249 if (n_seats == 0 && config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1255 g_debug ("Adding default seat");
1257 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1258 for (type = types; type && *type; type++)
1260 seat = seat_new (*type);
1267 set_seat_properties (seat, NULL);
1268 seat_set_property (seat, "exit-on-failure", "true");
1269 if (!display_manager_add_seat (display_manager, seat))
1270 return EXIT_FAILURE;
1271 g_object_unref (seat);
1275 g_warning ("Failed to create default seat");
1276 return EXIT_FAILURE;
1280 g_main_loop_run (loop);
1282 /* Clean up shared data manager */
1283 shared_data_manager_cleanup ();
1285 /* Clean up user list */
1286 common_user_list_cleanup ();
1288 /* Clean up display manager */
1289 g_object_unref (display_manager);
1290 display_manager = NULL;
1292 /* Remove D-Bus interface */
1293 g_dbus_connection_unregister_object (bus, reg_id);
1294 g_bus_unown_name (bus_id);
1295 if (seat_bus_entries)
1296 g_hash_table_unref (seat_bus_entries);
1297 if (session_bus_entries)
1298 g_hash_table_unref (session_bus_entries);
1300 g_debug ("Exiting with return value %d", exit_code);