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"
34 static gchar *config_path = NULL;
35 static GMainLoop *loop = NULL;
36 static GTimer *log_timer;
37 static int log_fd = -1;
38 static gboolean debug = FALSE;
40 static DisplayManager *display_manager = NULL;
41 static XDMCPServer *xdmcp_server = NULL;
42 static VNCServer *vnc_server = NULL;
43 static guint bus_id = 0;
44 static GDBusConnection *bus = NULL;
46 static GDBusNodeInfo *seat_info;
47 static GHashTable *seat_bus_entries = NULL;
48 static guint seat_index = 0;
49 static GDBusNodeInfo *session_info;
50 static GHashTable *session_bus_entries = NULL;
51 static guint session_index = 0;
52 static gint exit_code = EXIT_SUCCESS;
58 gchar *removed_signal;
62 #define LIGHTDM_BUS_NAME "org.freedesktop.DisplayManager"
65 log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer data)
70 switch (log_level & G_LOG_LEVEL_MASK)
72 case G_LOG_LEVEL_ERROR:
75 case G_LOG_LEVEL_CRITICAL:
78 case G_LOG_LEVEL_WARNING:
81 case G_LOG_LEVEL_MESSAGE:
84 case G_LOG_LEVEL_INFO:
87 case G_LOG_LEVEL_DEBUG:
95 text = g_strdup_printf ("[%+.2fs] %s %s\n", g_timer_elapsed (log_timer, NULL), prefix, message);
97 /* Log everything to a file */
101 n_written = write (log_fd, text, strlen (text));
103 ; /* Check result so compiler doesn't warn about it */
106 /* Log to stderr if requested */
108 g_printerr ("%s", text);
110 g_log_default_handler (log_domain, log_level, message, data);
118 gchar *log_dir, *path, *old_path;
120 log_timer = g_timer_new ();
123 log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
124 path = g_build_filename (log_dir, "lightdm.log", NULL);
127 /* Move old file out of the way */
128 old_path = g_strdup_printf ("%s.old", path);
129 rename (path, old_path);
132 /* Create new file and log to it */
133 log_fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
134 fcntl (log_fd, F_SETFD, FD_CLOEXEC);
135 g_log_set_default_handler (log_cb, NULL);
137 g_debug ("Logging to %s", path);
142 set_seat_properties (Seat *seat, const gchar *config_section)
147 keys = config_get_keys (config_get_instance (), "SeatDefaults");
148 for (i = 0; keys[i]; i++)
150 gchar *value = config_get_string (config_get_instance (), "SeatDefaults", keys[i]);
151 seat_set_property (seat, keys[i], value);
158 keys = config_get_keys (config_get_instance (), config_section);
159 for (i = 0; keys[i]; i++)
161 gchar *value = config_get_string (config_get_instance (), config_section, keys[i]);
162 seat_set_property (seat, keys[i], value);
170 signal_cb (Process *process, int signum)
172 g_debug ("Caught %s signal, shutting down", g_strsignal (signum));
173 display_manager_stop (display_manager);
174 // FIXME: Stop XDMCP server
178 display_manager_stopped_cb (DisplayManager *display_manager)
180 g_debug ("Stopping daemon");
181 g_main_loop_quit (loop);
185 display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat)
190 gboolean make_seats = FALSE;
191 Seat *next_seat = NULL;
193 /* If we have fallback types registered for the seat, let's try them
194 before giving up. This code does not expect a type name to appear twice
195 in the type list. It is a configuration error to do so. */
196 type = seat_get_type_name (seat);
197 types = seat_get_string_list_property (seat, "type");
198 for (iter = types; iter && *iter; iter++)
200 if (g_strcmp0 (type, *iter) == 0)
202 else if (make_seats) {
203 next_seat = seat_new (*iter);
212 const gchar *seat_name;
213 gchar *config_section = NULL;
215 seat_name = seat_get_string_property (seat, "seat-name");
217 config_section = g_strdup_printf ("Seat:%s", seat_name);
218 set_seat_properties (next_seat, config_section);
219 g_free (config_section);
221 // We set this manually on default seat. Let's port it over if needed.
222 if (seat_get_boolean_property (seat, "exit-on-failure"))
223 seat_set_property (next_seat, "exit-on-failure", "true");
225 display_manager_add_seat (display_manager, next_seat);
226 g_object_unref (next_seat);
228 else if (seat_get_boolean_property (seat, "exit-on-failure"))
230 g_debug ("Required seat has stopped");
231 exit_code = EXIT_FAILURE;
232 display_manager_stop (display_manager);
237 handle_display_manager_get_property (GDBusConnection *connection,
239 const gchar *object_path,
240 const gchar *interface_name,
241 const gchar *property_name,
245 GVariant *result = NULL;
247 if (g_strcmp0 (property_name, "Seats") == 0)
249 GVariantBuilder *builder;
253 builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
254 g_hash_table_iter_init (&iter, seat_bus_entries);
255 while (g_hash_table_iter_next (&iter, NULL, &value))
257 BusEntry *entry = value;
258 g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
260 result = g_variant_builder_end (builder);
261 g_variant_builder_unref (builder);
263 else if (g_strcmp0 (property_name, "Sessions") == 0)
265 GVariantBuilder *builder;
269 builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
270 g_hash_table_iter_init (&iter, session_bus_entries);
271 while (g_hash_table_iter_next (&iter, NULL, &value))
273 BusEntry *entry = value;
274 g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
276 result = g_variant_builder_end (builder);
277 g_variant_builder_unref (builder);
284 handle_display_manager_call (GDBusConnection *connection,
286 const gchar *object_path,
287 const gchar *interface_name,
288 const gchar *method_name,
289 GVariant *parameters,
290 GDBusMethodInvocation *invocation,
293 if (g_strcmp0 (method_name, "AddSeat") == 0)
296 GVariantIter *property_iter;
300 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa(ss))")))
303 g_variant_get (parameters, "(&sa(ss))", &type, &property_iter);
305 g_debug ("Adding seat of type %s", type);
307 seat = seat_new (type);
310 set_seat_properties (seat, NULL);
311 while (g_variant_iter_loop (property_iter, "(&s&s)", &name, &value))
312 seat_set_property (seat, name, value);
314 g_variant_iter_free (property_iter);
318 // FIXME: Need to make proper error
319 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create seat of type %s", type);
323 if (display_manager_add_seat (display_manager, seat))
327 entry = g_hash_table_lookup (seat_bus_entries, seat);
328 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
330 else// FIXME: Need to make proper error
331 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
332 g_object_unref (seat);
334 else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
339 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
342 g_variant_get (parameters, "(i)", &display_number);
344 g_debug ("Adding local X seat :%d", display_number);
346 seat = seat_new ("xremote");
349 gchar *display_number_string;
351 set_seat_properties (seat, NULL);
352 display_number_string = g_strdup_printf ("%d", display_number);
353 seat_set_property (seat, "xserver-display-number", display_number_string);
354 g_free (display_number_string);
359 // FIXME: Need to make proper error
360 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create local X seat");
364 if (display_manager_add_seat (display_manager, seat))
368 entry = g_hash_table_lookup (seat_bus_entries, seat);
369 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
371 else// FIXME: Need to make proper error
372 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
373 g_object_unref (seat);
378 handle_seat_get_property (GDBusConnection *connection,
380 const gchar *object_path,
381 const gchar *interface_name,
382 const gchar *property_name,
386 Seat *seat = user_data;
387 GVariant *result = NULL;
389 if (g_strcmp0 (property_name, "CanSwitch") == 0)
390 result = g_variant_new_boolean (seat_get_can_switch (seat));
391 if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
392 result = g_variant_new_boolean (seat_get_allow_guest (seat));
393 else if (g_strcmp0 (property_name, "Sessions") == 0)
395 GVariantBuilder *builder;
398 builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
399 for (link = seat_get_sessions (seat); link; link = link->next)
401 Session *session = link->data;
403 entry = g_hash_table_lookup (session_bus_entries, session);
405 g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
407 result = g_variant_builder_end (builder);
408 g_variant_builder_unref (builder);
415 handle_seat_call (GDBusConnection *connection,
417 const gchar *object_path,
418 const gchar *interface_name,
419 const gchar *method_name,
420 GVariant *parameters,
421 GDBusMethodInvocation *invocation,
424 Seat *seat = user_data;
426 if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
428 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
431 seat_switch_to_greeter (seat);
432 g_dbus_method_invocation_return_value (invocation, NULL);
434 else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
436 const gchar *username, *session_name;
438 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
441 g_variant_get (parameters, "(&s&s)", &username, &session_name);
442 if (strcmp (session_name, "") == 0)
445 seat_switch_to_user (seat, username, session_name);
446 g_dbus_method_invocation_return_value (invocation, NULL);
448 else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
450 const gchar *session_name;
452 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
455 g_variant_get (parameters, "(&s)", &session_name);
456 if (strcmp (session_name, "") == 0)
459 seat_switch_to_guest (seat, session_name);
460 g_dbus_method_invocation_return_value (invocation, NULL);
462 else if (g_strcmp0 (method_name, "Lock") == 0)
464 /* FIXME: Should only allow locks if have a session on this seat */
465 seat_lock (seat, NULL);
466 g_dbus_method_invocation_return_value (invocation, NULL);
471 get_seat_for_session (Session *session)
475 for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
477 Seat *seat = seat_link->data;
480 for (session_link = seat_get_sessions (seat); session_link; session_link = session_link->next)
482 Session *s = session_link->data;
493 handle_session_get_property (GDBusConnection *connection,
495 const gchar *object_path,
496 const gchar *interface_name,
497 const gchar *property_name,
501 Session *session = user_data;
504 entry = g_hash_table_lookup (session_bus_entries, session);
505 if (g_strcmp0 (property_name, "Seat") == 0)
506 return g_variant_new_object_path (entry ? entry->parent_path : "");
507 else if (g_strcmp0 (property_name, "UserName") == 0)
508 return g_variant_new_string (session_get_username (session));
514 handle_session_call (GDBusConnection *connection,
516 const gchar *object_path,
517 const gchar *interface_name,
518 const gchar *method_name,
519 GVariant *parameters,
520 GDBusMethodInvocation *invocation,
523 Session *session = user_data;
525 if (g_strcmp0 (method_name, "Lock") == 0)
529 seat = get_seat_for_session (session);
530 /* FIXME: Should only allow locks if have a session on this seat */
531 seat_lock (seat, session_get_username (session));
532 g_dbus_method_invocation_return_value (invocation, NULL);
537 bus_entry_new (const gchar *path, const gchar *parent_path, const gchar *removed_signal)
541 entry = g_malloc0 (sizeof (BusEntry));
542 entry->path = g_strdup (path);
543 entry->parent_path = g_strdup (parent_path);
544 entry->removed_signal = g_strdup (removed_signal);
550 bus_entry_free (gpointer data)
552 BusEntry *entry = data;
553 GError *error = NULL;
555 g_dbus_connection_unregister_object (bus, entry->bus_id);
557 if (!g_dbus_connection_emit_signal (bus,
559 "/org/freedesktop/DisplayManager",
560 "org.freedesktop.DisplayManager",
561 entry->removed_signal,
562 g_variant_new ("(o)", entry->path),
564 g_warning ("Failed to emit %s signal: %s", entry->removed_signal, error->message);
565 g_clear_error (&error);
567 g_free (entry->path);
568 g_free (entry->parent_path);
569 g_free (entry->removed_signal);
574 running_user_session_cb (Seat *seat, Session *session)
576 static const GDBusInterfaceVTable session_vtable =
579 handle_session_get_property
581 BusEntry *seat_entry, *entry;
583 GError *error = NULL;
585 /* Set environment variables when session runs */
586 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
587 session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
588 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
590 session_set_env (session, "XDG_SESSION_PATH", path);
591 g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
593 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
594 entry = bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL, "SessionRemoved");
595 g_hash_table_insert (session_bus_entries, g_object_ref (session), entry);
597 g_debug ("Registering session with bus path %s", entry->path);
599 entry->bus_id = g_dbus_connection_register_object (bus,
601 session_info->interfaces[0],
603 g_object_ref (session), g_object_unref,
605 if (entry->bus_id == 0)
606 g_warning ("Failed to register user session: %s", error->message);
607 g_clear_error (&error);
609 if (!g_dbus_connection_emit_signal (bus,
611 "/org/freedesktop/DisplayManager",
612 "org.freedesktop.DisplayManager",
614 g_variant_new ("(o)", entry->path),
616 g_warning ("Failed to emit SessionAdded signal: %s", error->message);
617 g_clear_error (&error);
621 session_removed_cb (Seat *seat, Session *session)
623 g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
624 g_hash_table_remove (session_bus_entries, session);
628 seat_added_cb (DisplayManager *display_manager, Seat *seat)
630 static const GDBusInterfaceVTable seat_vtable =
633 handle_seat_get_property
637 GError *error = NULL;
639 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
642 entry = bus_entry_new (path, NULL, "SeatRemoved");
644 g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
646 g_debug ("Registering seat with bus path %s", entry->path);
648 entry->bus_id = g_dbus_connection_register_object (bus,
650 seat_info->interfaces[0],
652 g_object_ref (seat), g_object_unref,
654 if (entry->bus_id == 0)
655 g_warning ("Failed to register seat: %s", error->message);
656 g_clear_error (&error);
658 if (!g_dbus_connection_emit_signal (bus,
660 "/org/freedesktop/DisplayManager",
661 "org.freedesktop.DisplayManager",
663 g_variant_new ("(o)", entry->path),
665 g_warning ("Failed to emit SeatAdded signal: %s", error->message);
666 g_clear_error (&error);
668 g_signal_connect (seat, "running-user-session", G_CALLBACK (running_user_session_cb), NULL);
669 g_signal_connect (seat, "session-removed", G_CALLBACK (session_removed_cb), NULL);
673 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
675 g_hash_table_remove (seat_bus_entries, seat);
679 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
681 SeatXDMCPSession *seat;
684 seat = seat_xdmcp_session_new (session);
685 set_seat_properties (SEAT (seat), NULL);
686 result = display_manager_add_seat (display_manager, SEAT (seat));
687 g_object_unref (seat);
693 vnc_connection_cb (VNCServer *server, GSocket *connection)
697 seat = seat_xvnc_new (connection);
698 set_seat_properties (SEAT (seat), NULL);
699 display_manager_add_seat (display_manager, SEAT (seat));
700 g_object_unref (seat);
704 bus_acquired_cb (GDBusConnection *connection,
708 const gchar *display_manager_interface =
710 " <interface name='org.freedesktop.DisplayManager'>"
711 " <property name='Seats' type='ao' access='read'/>"
712 " <property name='Sessions' type='ao' access='read'/>"
713 " <method name='AddSeat'>"
714 " <arg name='type' direction='in' type='s'/>"
715 " <arg name='properties' direction='in' type='a(ss)'/>"
716 " <arg name='seat' direction='out' type='o'/>"
718 " <method name='AddLocalXSeat'>"
719 " <arg name='display-number' direction='in' type='i'/>"
720 " <arg name='seat' direction='out' type='o'/>"
722 " <signal name='SeatAdded'>"
723 " <arg name='seat' type='o'/>"
725 " <signal name='SeatRemoved'>"
726 " <arg name='seat' type='o'/>"
728 " <signal name='SessionAdded'>"
729 " <arg name='session' type='o'/>"
731 " <signal name='SessionRemoved'>"
732 " <arg name='session' type='o'/>"
736 static const GDBusInterfaceVTable display_manager_vtable =
738 handle_display_manager_call,
739 handle_display_manager_get_property
741 const gchar *seat_interface =
743 " <interface name='org.freedesktop.DisplayManager.Seat'>"
744 " <property name='CanSwitch' type='b' access='read'/>"
745 " <property name='HasGuestAccount' type='b' access='read'/>"
746 " <property name='Sessions' type='ao' access='read'/>"
747 " <method name='SwitchToGreeter'/>"
748 " <method name='SwitchToUser'>"
749 " <arg name='username' direction='in' type='s'/>"
750 " <arg name='session-name' direction='in' type='s'/>"
752 " <method name='SwitchToGuest'>"
753 " <arg name='session-name' direction='in' type='s'/>"
755 " <method name='Lock'/>"
758 const gchar *session_interface =
760 " <interface name='org.freedesktop.DisplayManager.Session'>"
761 " <property name='Seat' type='o' access='read'/>"
762 " <property name='UserName' type='s' access='read'/>"
763 " <method name='Lock'/>"
766 GDBusNodeInfo *display_manager_info;
768 GError *error = NULL;
770 g_debug ("Acquired bus name %s", name);
774 display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
775 g_assert (display_manager_info != NULL);
776 seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
777 g_assert (seat_info != NULL);
778 session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
779 g_assert (session_info != NULL);
781 bus_id = g_dbus_connection_register_object (connection,
782 "/org/freedesktop/DisplayManager",
783 display_manager_info->interfaces[0],
784 &display_manager_vtable,
788 g_warning ("Failed to register display manager: %s", error->message);
789 g_clear_error (&error);
790 g_dbus_node_info_unref (display_manager_info);
792 seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, bus_entry_free);
793 session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, bus_entry_free);
795 g_signal_connect (display_manager, "seat-added", G_CALLBACK (seat_added_cb), NULL);
796 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (seat_removed_cb), NULL);
797 for (link = display_manager_get_seats (display_manager); link; link = link->next)
798 seat_added_cb (display_manager, (Seat *) link->data);
800 display_manager_start (display_manager);
802 /* Start the XDMCP server */
803 if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
805 gchar *key_name, *key = NULL;
807 xdmcp_server = xdmcp_server_new ();
808 if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
811 port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
813 xdmcp_server_set_port (xdmcp_server, port);
815 g_signal_connect (xdmcp_server, "new-session", G_CALLBACK (xdmcp_session_cb), NULL);
817 key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
823 GError *error = NULL;
825 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
826 path = g_build_filename (dir, "keys.conf", NULL);
829 keys = g_key_file_new ();
830 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
832 g_debug ("Error getting key %s", error->message);
833 g_clear_error (&error);
837 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
838 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
840 g_debug ("Key %s not defined", key_name);
843 g_key_file_free (keys);
846 xdmcp_server_set_key (xdmcp_server, key);
850 g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
851 xdmcp_server_start (xdmcp_server);
854 /* Start the VNC server */
855 if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
859 path = g_find_program_in_path ("Xvnc");
862 vnc_server = vnc_server_new ();
863 if (config_has_key (config_get_instance (), "VNCServer", "port"))
866 port = config_get_integer (config_get_instance (), "VNCServer", "port");
868 vnc_server_set_port (vnc_server, port);
870 g_signal_connect (vnc_server, "new-connection", G_CALLBACK (vnc_connection_cb), NULL);
872 g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
873 vnc_server_start (vnc_server);
878 g_warning ("Can't start VNC server, Xvnc is not in the path");
883 name_lost_cb (GDBusConnection *connection,
888 g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
890 g_printerr ("Failed to get D-Bus connection\n");
896 path_make_absolute (gchar *path)
898 gchar *cwd, *abs_path;
903 if (g_path_is_absolute (path))
906 cwd = g_get_current_dir ();
907 abs_path = g_build_filename (cwd, path, NULL);
914 compare_strings (gconstpointer a, gconstpointer b)
916 return strcmp (a, b);
920 load_config_directory (const gchar *path, GList **messages)
923 GList *files = NULL, *link;
924 GError *error = NULL;
926 /* Find configuration files */
927 dir = g_dir_open (path, 0, &error);
928 if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
929 g_printerr ("Failed to open configuration directory %s: %s\n", path, error->message);
930 g_clear_error (&error);
934 while ((name = g_dir_read_name (dir)))
935 files = g_list_append (files, g_strdup (name));
939 /* Sort alphabetically and load onto existing configuration */
940 files = g_list_sort (files, compare_strings);
941 for (link = files; link; link = link->next)
943 gchar *filename = link->data;
946 conf_path = g_build_filename (path, filename, NULL);
947 if (g_str_has_suffix (filename, ".conf"))
949 *messages = g_list_append (*messages, g_strdup_printf ("Loading configuration from %s", conf_path));
950 config_load_from_file (config_get_instance (), conf_path, &error);
951 if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
952 g_printerr ("Failed to load configuration from %s: %s\n", filename, error->message);
953 g_clear_error (&error);
956 g_debug ("Ignoring configuration file %s, it does not have .conf suffix", conf_path);
959 g_list_free_full (files, g_free);
963 main (int argc, char **argv)
966 GOptionContext *option_context;
968 gchar **groups, **i, *dir;
970 gboolean explicit_config = FALSE;
971 gboolean test_mode = FALSE;
972 gchar *pid_path = "/var/run/lightdm.pid";
973 gchar *config_dir, *config_d_dir = NULL;
974 gchar *log_dir = NULL;
975 gchar *run_dir = NULL;
976 gchar *cache_dir = NULL;
977 gchar *default_log_dir = g_strdup (LOG_DIR);
978 gchar *default_run_dir = g_strdup (RUN_DIR);
979 gchar *default_cache_dir = g_strdup (CACHE_DIR);
980 gboolean show_version = FALSE;
981 GList *link, *messages = NULL;
982 GOptionEntry options[] =
984 { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
985 /* Help string for command line --config flag */
986 N_("Use configuration file"), "FILE" },
987 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
988 /* Help string for command line --debug flag */
989 N_("Print debugging messages"), NULL },
990 { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
991 /* Help string for command line --test-mode flag */
992 N_("Run as unprivileged user, skipping things that require root access"), NULL },
993 { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
994 /* Help string for command line --pid-file flag */
995 N_("File to write PID into"), "FILE" },
996 { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
997 /* Help string for command line --log-dir flag */
998 N_("Directory to write logs to"), "DIRECTORY" },
999 { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
1000 /* Help string for command line --run-dir flag */
1001 N_("Directory to store running state"), "DIRECTORY" },
1002 { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
1003 /* Help string for command line --cache-dir flag */
1004 N_("Directory to cache information"), "DIRECTORY" },
1005 { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
1006 /* Help string for command line --version flag */
1007 N_("Show release version"), NULL },
1010 GError *error = NULL;
1012 /* When lightdm starts sessions it needs to run itself in a new mode */
1013 if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
1014 return session_child_run (argc, argv);
1016 #if !defined(GLIB_VERSION_2_36)
1019 loop = g_main_loop_new (NULL, FALSE);
1021 messages = g_list_append (messages, g_strdup_printf ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ()));
1023 g_signal_connect (process_get_current (), "got-signal", G_CALLBACK (signal_cb), NULL);
1025 option_context = g_option_context_new (/* Arguments and description for --help test */
1026 _("- Display Manager"));
1027 g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
1028 result = g_option_context_parse (option_context, &argc, &argv, &error);
1030 g_printerr ("%s\n", error->message);
1031 g_clear_error (&error);
1032 g_option_context_free (option_context);
1035 g_printerr (/* Text printed out when an unknown command-line argument provided */
1036 _("Run '%s --help' to see a full list of available command line options."), argv[0]);
1038 return EXIT_FAILURE;
1043 /* NOTE: Is not translated so can be easily parsed */
1044 g_printerr ("lightdm %s\n", VERSION);
1045 return EXIT_SUCCESS;
1050 config_dir = g_path_get_basename (config_path);
1051 config_dir = path_make_absolute (config_dir);
1052 explicit_config = TRUE;
1056 config_dir = g_strdup (CONFIG_DIR);
1057 config_d_dir = g_build_filename (config_dir, "lightdm.conf.d", NULL);
1058 config_path = g_build_filename (config_dir, "lightdm.conf", NULL);
1060 config_set_string (config_get_instance (), "LightDM", "config-directory", config_dir);
1061 g_free (config_dir);
1063 if (!test_mode && getuid () != 0)
1065 g_printerr ("Only root can run Light Display Manager. To run as a regular user for testing run with the --test-mode flag.\n");
1066 return EXIT_FAILURE;
1069 /* If running inside an X server use Xephyr for display */
1070 if (getenv ("DISPLAY") && getuid () != 0)
1072 gchar *x_server_path;
1074 x_server_path = g_find_program_in_path ("Xephyr");
1077 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");
1078 return EXIT_FAILURE;
1080 g_free (x_server_path);
1083 /* Make sure the system binary directory (where the greeters are installed) is in the path */
1086 const gchar *path = g_getenv ("PATH");
1090 new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
1092 new_path = g_strdup (SBIN_DIR);
1093 g_setenv ("PATH", new_path, TRUE);
1097 /* Write PID file */
1098 pid_file = fopen (pid_path, "w");
1101 fprintf (pid_file, "%d\n", getpid ());
1105 /* If not running as root write output to directories we control */
1108 g_free (default_log_dir);
1109 default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
1110 g_free (default_run_dir);
1111 default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
1112 g_free (default_cache_dir);
1113 default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
1116 /* Load config file(s) */
1117 load_config_directory (SYSTEM_CONFIG_DIR, &messages);
1119 load_config_directory (config_d_dir, &messages);
1120 g_free (config_d_dir);
1121 messages = g_list_append (messages, g_strdup_printf ("Loading configuration from %s", config_path));
1122 if (!config_load_from_file (config_get_instance (), config_path, &error))
1126 is_empty = error && g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1128 if (explicit_config || !is_empty)
1131 g_printerr ("Failed to load configuration from %s: %s\n", config_path, error->message);
1132 exit (EXIT_FAILURE);
1135 g_clear_error (&error);
1136 g_free (config_path);
1138 /* Set default values */
1139 if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
1140 config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
1141 if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
1142 config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
1143 if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
1144 config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
1145 if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
1146 config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
1147 if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
1148 config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
1149 if (!config_has_key (config_get_instance (), "SeatDefaults", "type"))
1150 config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal");
1151 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-command"))
1152 config_set_string (config_get_instance (), "SeatDefaults", "xserver-command", "X");
1153 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-share"))
1154 config_set_boolean (config_get_instance (), "SeatDefaults", "xserver-share", TRUE);
1155 if (!config_has_key (config_get_instance (), "SeatDefaults", "unity-compositor-command"))
1156 config_set_string (config_get_instance (), "SeatDefaults", "unity-compositor-command", "unity-system-compositor");
1157 if (!config_has_key (config_get_instance (), "SeatDefaults", "start-session"))
1158 config_set_boolean (config_get_instance (), "SeatDefaults", "start-session", TRUE);
1159 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-guest"))
1160 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-guest", TRUE);
1161 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-allow-guest"))
1162 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-allow-guest", TRUE);
1163 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-show-remote-login"))
1164 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-show-remote-login", TRUE);
1165 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-session"))
1166 config_set_string (config_get_instance (), "SeatDefaults", "greeter-session", GREETER_SESSION);
1167 if (!config_has_key (config_get_instance (), "SeatDefaults", "user-session"))
1168 config_set_string (config_get_instance (), "SeatDefaults", "user-session", USER_SESSION);
1169 if (!config_has_key (config_get_instance (), "SeatDefaults", "session-wrapper"))
1170 config_set_string (config_get_instance (), "SeatDefaults", "session-wrapper", "lightdm-session");
1171 if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
1172 config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
1173 g_free (default_log_dir);
1174 if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
1175 config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
1176 g_free (default_run_dir);
1177 if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
1178 config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
1179 g_free (default_cache_dir);
1180 if (!config_has_key (config_get_instance (), "LightDM", "sessions-directory"))
1181 config_set_string (config_get_instance (), "LightDM", "sessions-directory", SESSIONS_DIR);
1182 if (!config_has_key (config_get_instance (), "LightDM", "remote-sessions-directory"))
1183 config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR);
1184 if (!config_has_key (config_get_instance (), "LightDM", "greeters-directory"))
1185 config_set_string (config_get_instance (), "LightDM", "greeters-directory", GREETERS_DIR);
1187 /* Override defaults */
1189 config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
1192 config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
1195 config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
1198 /* Create run and cache directories */
1199 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
1200 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1201 g_warning ("Failed to make log directory %s: %s", dir, strerror (errno));
1203 dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
1204 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1205 g_warning ("Failed to make run directory %s: %s", dir, strerror (errno));
1207 dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
1208 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1209 g_warning ("Failed to make cache directory %s: %s", dir, strerror (errno));
1214 /* Show queued messages once logging is complete */
1215 for (link = messages; link; link = link->next)
1216 g_debug ("%s", (gchar *)link->data);
1217 g_list_free_full (messages, g_free);
1219 g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
1220 bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
1222 G_BUS_NAME_OWNER_FLAGS_NONE,
1230 g_debug ("Running in user mode");
1231 if (getenv ("DISPLAY"))
1232 g_debug ("Using Xephyr for X servers");
1234 display_manager = display_manager_new ();
1235 g_signal_connect (display_manager, "stopped", G_CALLBACK (display_manager_stopped_cb), NULL);
1236 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (display_manager_seat_removed_cb), NULL);
1238 /* Load the static display entries */
1239 groups = config_get_groups (config_get_instance ());
1240 for (i = groups; *i; i++)
1242 gchar *config_section = *i;
1246 const gchar *const seatpfx = "Seat:";
1248 if (!g_str_has_prefix (config_section, seatpfx))
1251 g_debug ("Loading seat %s", config_section);
1252 types = config_get_string_list (config_get_instance (), config_section, "type");
1254 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1255 for (type = types; type && *type; type++)
1257 seat = seat_new (*type);
1264 const gsize seatpfxlen = strlen(seatpfx);
1265 gchar *seatname = config_section + seatpfxlen;
1267 seat_set_property (seat, "seat-name", seatname);
1269 set_seat_properties (seat, config_section);
1270 display_manager_add_seat (display_manager, seat);
1271 g_object_unref (seat);
1275 g_warning ("Failed to create seat %s", config_section);
1277 g_strfreev (groups);
1279 /* If no seats start a default one */
1280 if (n_seats == 0 && config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1286 g_debug ("Adding default seat");
1288 types = config_get_string_list (config_get_instance (), "SeatDefaults", "type");
1289 for (type = types; type && *type; type++)
1291 seat = seat_new (*type);
1298 set_seat_properties (seat, NULL);
1299 seat_set_property (seat, "exit-on-failure", "true");
1300 if (!display_manager_add_seat (display_manager, seat))
1301 return EXIT_FAILURE;
1302 g_object_unref (seat);
1306 g_warning ("Failed to create default seat");
1307 return EXIT_FAILURE;
1311 g_main_loop_run (loop);
1313 /* Clean up display manager */
1314 g_object_unref (display_manager);
1315 display_manager = NULL;
1317 /* Remove D-Bus interface */
1318 g_bus_unown_name (bus_id);
1319 if (seat_bus_entries)
1320 g_hash_table_unref (seat_bus_entries);
1321 if (session_bus_entries)
1322 g_hash_table_unref (session_bus_entries);
1324 g_debug ("Exiting with return value %d", exit_code);