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;
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 log_fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
128 fcntl (log_fd, F_SETFD, FD_CLOEXEC);
129 g_log_set_default_handler (log_cb, NULL);
131 g_debug ("Logging to %s", path);
136 signal_cb (Process *process, int signum)
138 g_debug ("Caught %s signal, shutting down", g_strsignal (signum));
139 display_manager_stop (display_manager);
140 // FIXME: Stop XDMCP server
144 display_manager_stopped_cb (DisplayManager *display_manager)
146 g_debug ("Stopping daemon");
147 g_main_loop_quit (loop);
151 display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat)
153 if (seat_get_boolean_property (seat, "exit-on-failure"))
155 g_debug ("Required seat has stopped");
156 exit_code = EXIT_FAILURE;
157 display_manager_stop (display_manager);
162 handle_display_manager_get_property (GDBusConnection *connection,
164 const gchar *object_path,
165 const gchar *interface_name,
166 const gchar *property_name,
170 GVariant *result = NULL;
172 if (g_strcmp0 (property_name, "Seats") == 0)
174 GVariantBuilder *builder;
178 builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
179 g_hash_table_iter_init (&iter, seat_bus_entries);
180 while (g_hash_table_iter_next (&iter, NULL, &value))
182 BusEntry *entry = value;
183 g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
185 result = g_variant_builder_end (builder);
186 g_variant_builder_unref (builder);
188 else if (g_strcmp0 (property_name, "Sessions") == 0)
190 GVariantBuilder *builder;
194 builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
195 g_hash_table_iter_init (&iter, session_bus_entries);
196 while (g_hash_table_iter_next (&iter, NULL, &value))
198 BusEntry *entry = value;
199 g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
201 result = g_variant_builder_end (builder);
202 g_variant_builder_unref (builder);
209 set_seat_properties (Seat *seat, const gchar *config_section)
214 keys = config_get_keys (config_get_instance (), "SeatDefaults");
215 for (i = 0; keys[i]; i++)
217 gchar *value = config_get_string (config_get_instance (), "SeatDefaults", keys[i]);
218 seat_set_property (seat, keys[i], value);
225 keys = config_get_keys (config_get_instance (), config_section);
226 for (i = 0; keys[i]; i++)
228 gchar *value = config_get_string (config_get_instance (), config_section, keys[i]);
229 seat_set_property (seat, keys[i], value);
237 handle_display_manager_call (GDBusConnection *connection,
239 const gchar *object_path,
240 const gchar *interface_name,
241 const gchar *method_name,
242 GVariant *parameters,
243 GDBusMethodInvocation *invocation,
246 if (g_strcmp0 (method_name, "AddSeat") == 0)
249 GVariantIter *property_iter;
253 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa(ss))")))
256 g_variant_get (parameters, "(&sa(ss))", &type, &property_iter);
258 g_debug ("Adding seat of type %s", type);
260 seat = seat_new (type);
263 set_seat_properties (seat, NULL);
264 while (g_variant_iter_loop (property_iter, "(&s&s)", &name, &value))
265 seat_set_property (seat, name, value);
267 g_variant_iter_free (property_iter);
271 // FIXME: Need to make proper error
272 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create seat of type %s", type);
276 if (display_manager_add_seat (display_manager, seat))
280 entry = g_hash_table_lookup (seat_bus_entries, seat);
281 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
283 else// FIXME: Need to make proper error
284 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
285 g_object_unref (seat);
287 else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
292 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
295 g_variant_get (parameters, "(i)", &display_number);
297 g_debug ("Adding local X seat :%d", display_number);
299 seat = seat_new ("xremote");
302 gchar *display_number_string;
304 set_seat_properties (seat, NULL);
305 display_number_string = g_strdup_printf ("%d", display_number);
306 seat_set_property (seat, "xserver-display-number", display_number_string);
307 g_free (display_number_string);
312 // FIXME: Need to make proper error
313 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create local X seat");
317 if (display_manager_add_seat (display_manager, seat))
321 entry = g_hash_table_lookup (seat_bus_entries, seat);
322 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
324 else// FIXME: Need to make proper error
325 g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
326 g_object_unref (seat);
331 handle_seat_get_property (GDBusConnection *connection,
333 const gchar *object_path,
334 const gchar *interface_name,
335 const gchar *property_name,
339 Seat *seat = user_data;
340 GVariant *result = NULL;
342 if (g_strcmp0 (property_name, "CanSwitch") == 0)
343 result = g_variant_new_boolean (seat_get_can_switch (seat));
344 if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
345 result = g_variant_new_boolean (seat_get_allow_guest (seat));
346 else if (g_strcmp0 (property_name, "Sessions") == 0)
348 GVariantBuilder *builder;
351 builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
352 for (link = seat_get_displays (seat); link; link = link->next)
354 Display *display = link->data;
356 entry = g_hash_table_lookup (session_bus_entries, display_get_session (display));
358 g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
360 result = g_variant_builder_end (builder);
361 g_variant_builder_unref (builder);
368 handle_seat_call (GDBusConnection *connection,
370 const gchar *object_path,
371 const gchar *interface_name,
372 const gchar *method_name,
373 GVariant *parameters,
374 GDBusMethodInvocation *invocation,
377 Seat *seat = user_data;
379 if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
381 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
384 seat_switch_to_greeter (seat);
385 g_dbus_method_invocation_return_value (invocation, NULL);
387 else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
389 const gchar *username, *session_name;
391 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
394 g_variant_get (parameters, "(&s&s)", &username, &session_name);
395 if (strcmp (session_name, "") == 0)
398 seat_switch_to_user (seat, username, session_name);
399 g_dbus_method_invocation_return_value (invocation, NULL);
401 else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
403 const gchar *session_name;
405 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
408 g_variant_get (parameters, "(&s)", &session_name);
409 if (strcmp (session_name, "") == 0)
412 seat_switch_to_guest (seat, session_name);
413 g_dbus_method_invocation_return_value (invocation, NULL);
415 else if (g_strcmp0 (method_name, "Lock") == 0)
417 /* FIXME: Should only allow locks if have a session on this seat */
418 seat_lock (seat, NULL);
419 g_dbus_method_invocation_return_value (invocation, NULL);
424 get_seat_for_session (Session *session)
428 for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
430 Seat *seat = seat_link->data;
433 for (display_link = seat_get_displays (seat); display_link; display_link = display_link->next)
435 Display *display = display_link->data;
437 if (display_get_session (display) == session)
446 handle_session_get_property (GDBusConnection *connection,
448 const gchar *object_path,
449 const gchar *interface_name,
450 const gchar *property_name,
454 Session *session = user_data;
457 entry = g_hash_table_lookup (session_bus_entries, session);
458 if (g_strcmp0 (property_name, "Seat") == 0)
459 return g_variant_new_object_path (entry ? entry->parent_path : "");
460 else if (g_strcmp0 (property_name, "UserName") == 0)
461 return g_variant_new_string (session_get_username (session));
467 handle_session_call (GDBusConnection *connection,
469 const gchar *object_path,
470 const gchar *interface_name,
471 const gchar *method_name,
472 GVariant *parameters,
473 GDBusMethodInvocation *invocation,
476 Session *session = user_data;
478 if (g_strcmp0 (method_name, "Lock") == 0)
482 seat = get_seat_for_session (session);
483 /* FIXME: Should only allow locks if have a session on this seat */
484 seat_lock (seat, session_get_username (session));
485 g_dbus_method_invocation_return_value (invocation, NULL);
490 bus_entry_new (const gchar *path, const gchar *parent_path, const gchar *removed_signal)
494 entry = g_malloc0 (sizeof (BusEntry));
495 entry->path = g_strdup (path);
496 entry->parent_path = g_strdup (parent_path);
497 entry->removed_signal = g_strdup (removed_signal);
503 bus_entry_free (gpointer data)
505 BusEntry *entry = data;
507 g_dbus_connection_unregister_object (bus, entry->bus_id);
509 g_dbus_connection_emit_signal (bus,
511 "/org/freedesktop/DisplayManager",
512 "org.freedesktop.DisplayManager",
513 entry->removed_signal,
514 g_variant_new ("(o)", entry->path),
517 g_free (entry->path);
518 g_free (entry->parent_path);
519 g_free (entry->removed_signal);
524 start_session_cb (Display *display, Seat *seat)
527 BusEntry *seat_entry;
530 session = display_get_session (display);
532 /* Set environment variables when session runs */
533 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
534 session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
535 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
537 session_set_env (session, "XDG_SESSION_PATH", path);
538 g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
544 session_stopped_cb (Session *session, Seat *seat)
546 g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
547 g_hash_table_remove (session_bus_entries, session);
551 session_started_cb (Display *display, Seat *seat)
553 static const GDBusInterfaceVTable session_vtable =
556 handle_session_get_property
559 BusEntry *seat_entry, *entry;
561 session = display_get_session (display);
563 g_signal_connect (session, "stopped", G_CALLBACK (session_stopped_cb), seat);
565 seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
566 entry = bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL, "SessionRemoved");
567 g_hash_table_insert (session_bus_entries, g_object_ref (session), entry);
569 g_debug ("Registering session with bus path %s", entry->path);
571 entry->bus_id = g_dbus_connection_register_object (bus,
573 session_info->interfaces[0],
575 g_object_ref (session), g_object_unref,
577 g_dbus_connection_emit_signal (bus,
579 "/org/freedesktop/DisplayManager",
580 "org.freedesktop.DisplayManager",
582 g_variant_new ("(o)", entry->path),
589 display_added_cb (Seat *seat, Display *display)
591 g_signal_connect (display, "start-session", G_CALLBACK (start_session_cb), seat);
592 g_signal_connect_after (display, "start-session", G_CALLBACK (session_started_cb), seat);
596 seat_added_cb (DisplayManager *display_manager, Seat *seat)
598 static const GDBusInterfaceVTable seat_vtable =
601 handle_seat_get_property
607 g_signal_connect (seat, "display-added", G_CALLBACK (display_added_cb), NULL);
608 for (link = seat_get_displays (seat); link; link = link->next)
609 display_added_cb (seat, (Display *) link->data);
611 path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
614 entry = bus_entry_new (path, NULL, "SeatRemoved");
616 g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
618 g_debug ("Registering seat with bus path %s", entry->path);
620 entry->bus_id = g_dbus_connection_register_object (bus,
622 seat_info->interfaces[0],
624 g_object_ref (seat), g_object_unref,
626 g_dbus_connection_emit_signal (bus,
628 "/org/freedesktop/DisplayManager",
629 "org.freedesktop.DisplayManager",
631 g_variant_new ("(o)", entry->path),
636 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
638 g_hash_table_remove (seat_bus_entries, seat);
642 bus_acquired_cb (GDBusConnection *connection,
646 const gchar *display_manager_interface =
648 " <interface name='org.freedesktop.DisplayManager'>"
649 " <property name='Seats' type='ao' access='read'/>"
650 " <property name='Sessions' type='ao' access='read'/>"
651 " <method name='AddSeat'>"
652 " <arg name='type' direction='in' type='s'/>"
653 " <arg name='properties' direction='in' type='a(ss)'/>"
654 " <arg name='seat' direction='out' type='o'/>"
656 " <method name='AddLocalXSeat'>"
657 " <arg name='display-number' direction='in' type='i'/>"
658 " <arg name='seat' direction='out' type='o'/>"
660 " <signal name='SeatAdded'>"
661 " <arg name='seat' type='o'/>"
663 " <signal name='SeatRemoved'>"
664 " <arg name='seat' type='o'/>"
666 " <signal name='SessionAdded'>"
667 " <arg name='session' type='o'/>"
669 " <signal name='SessionRemoved'>"
670 " <arg name='session' type='o'/>"
674 static const GDBusInterfaceVTable display_manager_vtable =
676 handle_display_manager_call,
677 handle_display_manager_get_property
679 const gchar *seat_interface =
681 " <interface name='org.freedesktop.DisplayManager.Seat'>"
682 " <property name='CanSwitch' type='b' access='read'/>"
683 " <property name='HasGuestAccount' type='b' access='read'/>"
684 " <property name='Sessions' type='ao' access='read'/>"
685 " <method name='SwitchToGreeter'/>"
686 " <method name='SwitchToUser'>"
687 " <arg name='username' direction='in' type='s'/>"
688 " <arg name='session-name' direction='in' type='s'/>"
690 " <method name='SwitchToGuest'>"
691 " <arg name='session-name' direction='in' type='s'/>"
693 " <method name='Lock'/>"
696 const gchar *session_interface =
698 " <interface name='org.freedesktop.DisplayManager.Session'>"
699 " <property name='Seat' type='o' access='read'/>"
700 " <property name='UserName' type='s' access='read'/>"
701 " <method name='Lock'/>"
704 GDBusNodeInfo *display_manager_info;
707 g_debug ("Acquired bus name %s", name);
711 display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
712 g_assert (display_manager_info != NULL);
713 seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
714 g_assert (seat_info != NULL);
715 session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
716 g_assert (session_info != NULL);
718 bus_id = g_dbus_connection_register_object (connection,
719 "/org/freedesktop/DisplayManager",
720 display_manager_info->interfaces[0],
721 &display_manager_vtable,
724 g_dbus_node_info_unref (display_manager_info);
726 seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, bus_entry_free);
727 session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, bus_entry_free);
729 g_signal_connect (display_manager, "seat-added", G_CALLBACK (seat_added_cb), NULL);
730 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (seat_removed_cb), NULL);
731 for (link = display_manager_get_seats (display_manager); link; link = link->next)
732 seat_added_cb (display_manager, (Seat *) link->data);
736 name_lost_cb (GDBusConnection *connection,
741 g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
743 g_printerr ("Failed to get D-Bus connection\n");
749 path_make_absolute (gchar *path)
751 gchar *cwd, *abs_path;
756 if (g_path_is_absolute (path))
759 cwd = g_get_current_dir ();
760 abs_path = g_build_filename (cwd, path, NULL);
767 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
769 SeatXDMCPSession *seat;
772 seat = seat_xdmcp_session_new (session);
773 set_seat_properties (SEAT (seat), NULL);
774 result = display_manager_add_seat (display_manager, SEAT (seat));
775 g_object_unref (seat);
781 vnc_connection_cb (VNCServer *server, GSocket *connection)
785 seat = seat_xvnc_new (connection);
786 set_seat_properties (SEAT (seat), NULL);
787 display_manager_add_seat (display_manager, SEAT (seat));
788 g_object_unref (seat);
792 compare_strings (gconstpointer a, gconstpointer b)
794 return strcmp (a, b);
798 main (int argc, char **argv)
801 GOptionContext *option_context;
803 gchar **groups, **i, *dir;
805 gboolean explicit_config = FALSE;
806 gboolean test_mode = FALSE;
807 gchar *pid_path = "/var/run/lightdm.pid";
808 gchar *xsessions_dir = NULL;
809 gchar *remote_sessions_dir = NULL;
810 gchar *xgreeters_dir = NULL;
811 gchar *config_dir, *config_d_dir = NULL;
812 gchar *log_dir = NULL;
813 gchar *run_dir = NULL;
814 gchar *cache_dir = NULL;
815 gchar *default_log_dir = g_strdup (LOG_DIR);
816 gchar *default_run_dir = g_strdup (RUN_DIR);
817 gchar *default_cache_dir = g_strdup (CACHE_DIR);
818 gboolean show_version = FALSE;
819 GList *link, *messages = NULL;
820 GOptionEntry options[] =
822 { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
823 /* Help string for command line --config flag */
824 N_("Use configuration file"), "FILE" },
825 { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
826 /* Help string for command line --debug flag */
827 N_("Print debugging messages"), NULL },
828 { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
829 /* Help string for command line --test-mode flag */
830 N_("Run as unprivileged user, skipping things that require root access"), NULL },
831 { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
832 /* Help string for command line --pid-file flag */
833 N_("File to write PID into"), "FILE" },
834 { "xsessions-dir", 0, 0, G_OPTION_ARG_STRING, &xsessions_dir,
835 /* Help string for command line --xsessions-dir flag */
836 N_("Directory to load X sessions from"), "DIRECTORY" },
837 { "remote-sessions-dir", 0, 0, G_OPTION_ARG_STRING, &remote_sessions_dir,
838 /* Help string for command line --remote-sessions-dir flag */
839 N_("Directory to load remote sessions from"), "DIRECTORY" },
840 { "xgreeters-dir", 0, 0, G_OPTION_ARG_STRING, &xgreeters_dir,
841 /* Help string for command line --xgreeters-dir flag */
842 N_("Directory to load X greeters from"), "DIRECTORY" },
843 { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
844 /* Help string for command line --log-dir flag */
845 N_("Directory to write logs to"), "DIRECTORY" },
846 { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
847 /* Help string for command line --run-dir flag */
848 N_("Directory to store running state"), "DIRECTORY" },
849 { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
850 /* Help string for command line --cache-dir flag */
851 N_("Directory to cache information"), "DIRECTORY" },
852 { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
853 /* Help string for command line --version flag */
854 N_("Show release version"), NULL },
857 GError *error = NULL;
859 /* When lightdm starts sessions it needs to run itself in a new mode */
860 if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
861 return session_child_run (argc, argv);
863 #if !defined(GLIB_VERSION_2_36)
866 loop = g_main_loop_new (NULL, FALSE);
868 messages = g_list_append (messages, g_strdup_printf ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ()));
870 g_signal_connect (process_get_current (), "got-signal", G_CALLBACK (signal_cb), NULL);
872 option_context = g_option_context_new (/* Arguments and description for --help test */
873 _("- Display Manager"));
874 g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
875 result = g_option_context_parse (option_context, &argc, &argv, &error);
877 g_printerr ("%s\n", error->message);
878 g_clear_error (&error);
879 g_option_context_free (option_context);
882 g_printerr (/* Text printed out when an unknown command-line argument provided */
883 _("Run '%s --help' to see a full list of available command line options."), argv[0]);
890 /* NOTE: Is not translated so can be easily parsed */
891 g_printerr ("lightdm %s\n", VERSION);
897 config_dir = g_path_get_basename (config_path);
898 config_dir = path_make_absolute (config_dir);
899 explicit_config = TRUE;
903 config_dir = g_strdup (CONFIG_DIR);
904 config_d_dir = g_build_filename (config_dir, "lightdm.conf.d", NULL);
905 config_path = g_build_filename (config_dir, "lightdm.conf", NULL);
907 config_set_string (config_get_instance (), "LightDM", "config-directory", config_dir);
910 if (!test_mode && getuid () != 0)
912 g_printerr ("Only root can run Light Display Manager. To run as a regular user for testing run with the --test-mode flag.\n");
916 /* If running inside an X server use Xephyr for display */
917 if (getenv ("DISPLAY") && getuid () != 0)
921 xserver_path = g_find_program_in_path ("Xephyr");
924 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");
927 g_free (xserver_path);
930 /* Make sure the system binary directory (where the greeters are installed) is in the path */
933 const gchar *path = g_getenv ("PATH");
937 new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
939 new_path = g_strdup (SBIN_DIR);
940 g_setenv ("PATH", new_path, TRUE);
945 pid_file = fopen (pid_path, "w");
948 fprintf (pid_file, "%d\n", getpid ());
952 /* Always use absolute directories as child processes may run from different locations */
953 xsessions_dir = path_make_absolute (xsessions_dir);
954 remote_sessions_dir = path_make_absolute (remote_sessions_dir);
955 xgreeters_dir = path_make_absolute (xgreeters_dir);
957 /* If not running as root write output to directories we control */
960 g_free (default_log_dir);
961 default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
962 g_free (default_run_dir);
963 default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
964 g_free (default_cache_dir);
965 default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
968 /* Load config file(s) */
969 messages = g_list_append (messages, g_strdup_printf ("Loading configuration from %s", config_path));
970 if (!config_load_from_file (config_get_instance (), config_path, &error))
974 is_empty = error && g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
976 if (explicit_config || !is_empty)
979 g_printerr ("Failed to load configuration from %s: %s\n", config_path, error->message);
983 g_clear_error (&error);
984 g_free (config_path);
991 /* Find configuration files */
992 dir = g_dir_open (config_d_dir, 0, &error);
993 if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
994 g_printerr ("Failed to open configuration directory %s: %s\n", config_d_dir, error->message);
995 g_clear_error (&error);
999 while ((name = g_dir_read_name (dir)))
1000 files = g_list_append (files, g_strdup (name));
1004 /* Sort alphabetically and load onto existing configuration */
1005 files = g_list_sort (files, compare_strings);
1006 for (link = files; link; link = link->next)
1008 gchar *filename = link->data;
1011 path = g_build_filename (config_d_dir, filename, NULL);
1012 if (g_str_has_suffix (filename, ".conf"))
1014 messages = g_list_append (messages, g_strdup_printf ("Loading configuration from %s", path));
1015 config_load_from_file (config_get_instance (), path, &error);
1016 if (error && !g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
1017 g_printerr ("Failed to load configuration from %s: %s\n", filename, error->message);
1018 g_clear_error (&error);
1021 g_debug ("Ignoring configuration file %s, it does not have .conf suffix", path);
1024 g_list_free_full (files, g_free);
1026 g_free (config_d_dir);
1028 /* Set default values */
1029 if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
1030 config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
1031 if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
1032 config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
1033 if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
1034 config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
1035 if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
1036 config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
1037 if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
1038 config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
1039 if (!config_has_key (config_get_instance (), "SeatDefaults", "type"))
1040 config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal");
1041 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-command"))
1042 config_set_string (config_get_instance (), "SeatDefaults", "xserver-command", "X");
1043 if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-share"))
1044 config_set_boolean (config_get_instance (), "SeatDefaults", "xserver-share", TRUE);
1045 if (!config_has_key (config_get_instance (), "SeatDefaults", "unity-compositor-command"))
1046 config_set_string (config_get_instance (), "SeatDefaults", "unity-compositor-command", "unity-system-compositor");
1047 if (!config_has_key (config_get_instance (), "SeatDefaults", "start-session"))
1048 config_set_boolean (config_get_instance (), "SeatDefaults", "start-session", TRUE);
1049 if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-guest"))
1050 config_set_boolean (config_get_instance (), "SeatDefaults", "allow-guest", TRUE);
1051 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-allow-guest"))
1052 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-allow-guest", TRUE);
1053 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-show-remote-login"))
1054 config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-show-remote-login", TRUE);
1055 if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-session"))
1056 config_set_string (config_get_instance (), "SeatDefaults", "greeter-session", GREETER_SESSION);
1057 if (!config_has_key (config_get_instance (), "SeatDefaults", "user-session"))
1058 config_set_string (config_get_instance (), "SeatDefaults", "user-session", USER_SESSION);
1059 if (!config_has_key (config_get_instance (), "SeatDefaults", "session-wrapper"))
1060 config_set_string (config_get_instance (), "SeatDefaults", "session-wrapper", "lightdm-session");
1061 if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
1062 config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
1063 g_free (default_log_dir);
1064 if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
1065 config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
1066 g_free (default_run_dir);
1067 if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
1068 config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
1069 g_free (default_cache_dir);
1070 if (!config_has_key (config_get_instance (), "LightDM", "xsessions-directory"))
1071 config_set_string (config_get_instance (), "LightDM", "xsessions-directory", XSESSIONS_DIR);
1072 if (!config_has_key (config_get_instance (), "LightDM", "remote-sessions-directory"))
1073 config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR);
1074 if (!config_has_key (config_get_instance (), "LightDM", "xgreeters-directory"))
1075 config_set_string (config_get_instance (), "LightDM", "xgreeters-directory", XGREETERS_DIR);
1077 /* Override defaults */
1079 config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
1082 config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
1085 config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
1088 config_set_string (config_get_instance (), "LightDM", "xsessions-directory", xsessions_dir);
1089 g_free (xsessions_dir);
1090 if (remote_sessions_dir)
1091 config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", remote_sessions_dir);
1092 g_free (remote_sessions_dir);
1094 config_set_string (config_get_instance (), "LightDM", "xgreeters-directory", xgreeters_dir);
1095 g_free (xgreeters_dir);
1097 /* Create run and cache directories */
1098 dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
1099 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1100 g_warning ("Failed to make log directory %s: %s", dir, strerror (errno));
1102 dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
1103 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1104 g_warning ("Failed to make run directory %s: %s", dir, strerror (errno));
1106 dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
1107 if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1108 g_warning ("Failed to make cache directory %s: %s", dir, strerror (errno));
1113 /* Show queued messages once logging is complete */
1114 for (link = messages; link; link = link->next)
1115 g_debug ("%s", link->data);
1116 g_list_free_full (messages, g_free);
1118 g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
1119 bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
1121 G_BUS_NAME_OWNER_FLAGS_NONE,
1129 g_debug ("Running in user mode");
1130 if (getenv ("DISPLAY"))
1131 g_debug ("Using Xephyr for X servers");
1133 display_manager = display_manager_new ();
1134 g_signal_connect (display_manager, "stopped", G_CALLBACK (display_manager_stopped_cb), NULL);
1135 g_signal_connect (display_manager, "seat-removed", G_CALLBACK (display_manager_seat_removed_cb), NULL);
1137 /* Load the static display entries */
1138 groups = config_get_groups (config_get_instance ());
1139 for (i = groups; *i; i++)
1141 gchar *config_section = *i;
1145 if (!g_str_has_prefix (config_section, "Seat:"))
1148 g_debug ("Loading seat %s", config_section);
1149 type = config_get_string (config_get_instance (), config_section, "type");
1151 type = config_get_string (config_get_instance (), "SeatDefaults", "type");
1152 seat = seat_new (type);
1156 set_seat_properties (seat, config_section);
1157 display_manager_add_seat (display_manager, seat);
1158 g_object_unref (seat);
1162 g_warning ("Failed to create seat %s", config_section);
1164 g_strfreev (groups);
1166 /* If no seats start a default one */
1167 if (n_seats == 0 && config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1172 g_debug ("Adding default seat");
1174 type = config_get_string (config_get_instance (), "SeatDefaults", "type");
1175 seat = seat_new (type);
1179 set_seat_properties (seat, NULL);
1180 seat_set_property (seat, "exit-on-failure", "true");
1181 display_manager_add_seat (display_manager, seat);
1182 g_object_unref (seat);
1186 g_warning ("Failed to create default seat %s", type);
1187 return EXIT_FAILURE;
1191 display_manager_start (display_manager);
1193 /* Start the XDMCP server */
1194 if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
1196 gchar *key_name, *key = NULL;
1198 xdmcp_server = xdmcp_server_new ();
1199 if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
1202 port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
1204 xdmcp_server_set_port (xdmcp_server, port);
1206 g_signal_connect (xdmcp_server, "new-session", G_CALLBACK (xdmcp_session_cb), NULL);
1208 key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
1214 GError *error = NULL;
1216 dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
1217 path = g_build_filename (dir, "keys.conf", NULL);
1220 keys = g_key_file_new ();
1221 result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
1223 g_debug ("Error getting key %s", error->message);
1224 g_clear_error (&error);
1228 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
1229 key = g_key_file_get_string (keys, "keyring", key_name, NULL);
1231 g_debug ("Key %s not defined", key_name);
1234 g_key_file_free (keys);
1237 xdmcp_server_set_key (xdmcp_server, key);
1241 g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
1242 xdmcp_server_start (xdmcp_server);
1245 /* Start the VNC server */
1246 if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
1250 path = g_find_program_in_path ("Xvnc");
1253 vnc_server = vnc_server_new ();
1254 if (config_has_key (config_get_instance (), "VNCServer", "port"))
1257 port = config_get_integer (config_get_instance (), "VNCServer", "port");
1259 vnc_server_set_port (vnc_server, port);
1261 g_signal_connect (vnc_server, "new-connection", G_CALLBACK (vnc_connection_cb), NULL);
1263 g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
1264 vnc_server_start (vnc_server);
1269 g_warning ("Can't start VNC server, Xvn is not in the path");
1272 g_main_loop_run (loop);
1274 /* Clean up display manager */
1275 g_object_unref (display_manager);
1276 display_manager = NULL;
1278 /* Remove D-Bus interface */
1279 g_bus_unown_name (bus_id);
1280 if (seat_bus_entries)
1281 g_hash_table_unref (seat_bus_entries);
1282 if (session_bus_entries)
1283 g_hash_table_unref (session_bus_entries);
1285 g_debug ("Exiting with return value %d", exit_code);