]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/lightdm.c
Deprecate [SeatDefaults] in favour of new globbing support for seat config sections
[sojka/lightdm.git] / src / lightdm.c
1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
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
9  * license.
10  */
11
12 #include <config.h>
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <sys/stat.h>
17 #include <glib.h>
18 #include <glib/gi18n.h>
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <errno.h>
23
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"
30 #include "x-server.h"
31 #include "process.h"
32 #include "session-child.h"
33 #include "shared-data-manager.h"
34 #include "user-list.h"
35 #include "login1.h"
36
37 static gchar *config_path = NULL;
38 static GMainLoop *loop = NULL;
39 static GTimer *log_timer;
40 static int log_fd = -1;
41 static gboolean debug = FALSE;
42
43 static DisplayManager *display_manager = NULL;
44 static XDMCPServer *xdmcp_server = NULL;
45 static VNCServer *vnc_server = NULL;
46 static guint bus_id = 0;
47 static GDBusConnection *bus = NULL;
48 static guint reg_id = 0;
49 static GDBusNodeInfo *seat_info;
50 static GHashTable *seat_bus_entries = NULL;
51 static guint seat_index = 0;
52 static GDBusNodeInfo *session_info;
53 static GHashTable *session_bus_entries = NULL;
54 static guint session_index = 0;
55 static gint exit_code = EXIT_SUCCESS;
56
57 typedef struct
58 {
59     gchar *path;
60     guint bus_id;
61 } SeatBusEntry;
62 typedef struct
63 {
64     gchar *path;
65     gchar *seat_path;
66     guint bus_id;
67 } SessionBusEntry;
68
69 #define LIGHTDM_BUS_NAME "org.freedesktop.DisplayManager"
70
71 static gboolean update_login1_seat (Login1Seat *login1_seat);
72
73 static void
74 log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer data)
75 {
76     const gchar *prefix;
77     gchar *text;
78
79     switch (log_level & G_LOG_LEVEL_MASK)
80     {
81     case G_LOG_LEVEL_ERROR:
82         prefix = "ERROR:";
83         break;
84     case G_LOG_LEVEL_CRITICAL:
85         prefix = "CRITICAL:";
86         break;
87     case G_LOG_LEVEL_WARNING:
88         prefix = "WARNING:";
89         break;
90     case G_LOG_LEVEL_MESSAGE:
91         prefix = "MESSAGE:";
92         break;
93     case G_LOG_LEVEL_INFO:
94         prefix = "INFO:";
95         break;
96     case G_LOG_LEVEL_DEBUG:
97         prefix = "DEBUG:";
98         break;
99     default:
100         prefix = "LOG:";
101         break;
102     }
103
104     text = g_strdup_printf ("[%+.2fs] %s %s\n", g_timer_elapsed (log_timer, NULL), prefix, message);
105
106     /* Log everything to a file */
107     if (log_fd >= 0)
108     {
109         ssize_t n_written;
110         n_written = write (log_fd, text, strlen (text));
111         if (n_written < 0)
112             ; /* Check result so compiler doesn't warn about it */
113     }
114
115     /* Log to stderr if requested */
116     if (debug)
117         g_printerr ("%s", text);
118     else
119         g_log_default_handler (log_domain, log_level, message, data);
120
121     g_free (text);
122 }
123
124 static void
125 log_init (void)
126 {
127     gchar *log_dir, *path, *old_path;
128
129     log_timer = g_timer_new ();
130
131     /* Log to a file */
132     log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
133     path = g_build_filename (log_dir, "lightdm.log", NULL);
134     g_free (log_dir);
135
136     /* Move old file out of the way */
137     old_path = g_strdup_printf ("%s.old", path);
138     rename (path, old_path);
139     g_free (old_path);
140
141     /* Create new file and log to it */
142     log_fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
143     fcntl (log_fd, F_SETFD, FD_CLOEXEC);
144     g_log_set_default_handler (log_cb, NULL);
145
146     g_debug ("Logging to %s", path);
147     g_free (path);
148 }
149
150 static GList*
151 get_config_sections (const gchar *seat_name)
152 {
153     gchar **groups, **i;
154     GList *config_sections = NULL;
155
156     groups = config_get_groups (config_get_instance ());
157     for (i = groups; *i; i++)
158     {
159         if (g_str_has_prefix (*i, "Seat:"))
160         {
161             const gchar *seat_name_glob = *i + strlen ("Seat:");
162             if (g_pattern_match_simple (seat_name_glob, seat_name ? seat_name : ""))
163                 config_sections = g_list_append (config_sections, g_strdup (*i));
164         }
165     }
166     g_strfreev (groups);
167
168     return config_sections;
169 }
170
171 static void
172 set_seat_properties (Seat *seat, const gchar *seat_name)
173 {
174     GList *sections, *link;
175     gchar **keys;
176     gint i;
177
178     sections = get_config_sections (seat_name);
179     for (link = sections; link; link = link->next)
180     {
181         const gchar *section = link->data;
182         g_debug ("Loading properties from config section %s", section);
183         keys = config_get_keys (config_get_instance (), section);
184         for (i = 0; keys && keys[i]; i++)
185         {
186             gchar *value = config_get_string (config_get_instance (), section, keys[i]);
187             seat_set_property (seat, keys[i], value);
188             g_free (value);
189         }
190         g_strfreev (keys);
191     }
192     g_list_free_full (sections, g_free);
193 }
194
195 static void
196 signal_cb (Process *process, int signum)
197 {
198     g_debug ("Caught %s signal, shutting down", g_strsignal (signum));
199     display_manager_stop (display_manager);
200     // FIXME: Stop XDMCP server
201 }
202
203 static void
204 display_manager_stopped_cb (DisplayManager *display_manager)
205 {
206     g_debug ("Stopping daemon");
207     g_main_loop_quit (loop);
208 }
209
210 static void
211 display_manager_seat_removed_cb (DisplayManager *display_manager, Seat *seat)
212 {
213     gchar **types;
214     gchar **iter;
215     Seat *next_seat = NULL;
216     GString *next_types;
217
218     /* If we have fallback types registered for the seat, let's try them
219        before giving up. */
220     types = seat_get_string_list_property (seat, "type");
221     next_types = g_string_new ("");
222     for (iter = types; iter && *iter; iter++)
223     {
224         if (iter == types)
225             continue; // skip first one, that is our current seat type
226
227         if (!next_seat)
228         {
229             next_seat = seat_new (*iter, seat_get_name (seat));
230             g_string_assign (next_types, *iter);
231         }
232         else
233         {
234             // Build up list of types to try next time
235             g_string_append_c (next_types, ';');
236             g_string_append (next_types, *iter);
237         }
238     }
239     g_strfreev (types);
240
241     if (next_seat)
242     {
243         set_seat_properties (next_seat, seat_get_name (seat));
244
245         // We set this manually on default seat.  Let's port it over if needed.
246         if (seat_get_boolean_property (seat, "exit-on-failure"))
247             seat_set_property (next_seat, "exit-on-failure", "true");
248
249         seat_set_property (next_seat, "type", next_types->str);
250
251         display_manager_add_seat (display_manager, next_seat);
252         g_object_unref (next_seat);
253     }
254     else if (seat_get_boolean_property (seat, "exit-on-failure"))
255     {
256         g_debug ("Required seat has stopped");
257         exit_code = EXIT_FAILURE;
258         display_manager_stop (display_manager);
259     }
260
261     g_string_free (next_types, TRUE);
262 }
263
264 static GVariant *
265 get_seat_list (void)
266 {
267     GVariantBuilder builder;
268     GHashTableIter iter;
269     gpointer value;
270
271     g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
272     g_hash_table_iter_init (&iter, seat_bus_entries);
273     while (g_hash_table_iter_next (&iter, NULL, &value))
274     {
275         SeatBusEntry *entry = value;
276         g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
277     }
278
279     return g_variant_builder_end (&builder);
280 }
281
282 static GVariant *
283 get_session_list (const gchar *seat_path)
284 {
285     GVariantBuilder builder;
286     GHashTableIter iter;
287     gpointer value;
288
289     g_variant_builder_init (&builder, G_VARIANT_TYPE ("ao"));
290
291     g_hash_table_iter_init (&iter, session_bus_entries);
292     while (g_hash_table_iter_next (&iter, NULL, &value))
293     {
294         SessionBusEntry *entry = value;
295         if (seat_path == NULL || strcmp (entry->seat_path, seat_path) == 0)
296             g_variant_builder_add_value (&builder, g_variant_new_object_path (entry->path));
297     }
298
299     return g_variant_builder_end (&builder);
300 }
301
302 static GVariant *
303 handle_display_manager_get_property (GDBusConnection       *connection,
304                                      const gchar           *sender,
305                                      const gchar           *object_path,
306                                      const gchar           *interface_name,
307                                      const gchar           *property_name,
308                                      GError               **error,
309                                      gpointer               user_data)
310 {
311     if (g_strcmp0 (property_name, "Seats") == 0)
312         return get_seat_list ();
313     else if (g_strcmp0 (property_name, "Sessions") == 0)
314         return get_session_list (NULL);
315
316     return NULL;
317 }
318
319 static void
320 handle_display_manager_call (GDBusConnection       *connection,
321                              const gchar           *sender,
322                              const gchar           *object_path,
323                              const gchar           *interface_name,
324                              const gchar           *method_name,
325                              GVariant              *parameters,
326                              GDBusMethodInvocation *invocation,
327                              gpointer               user_data)
328 {
329     if (g_strcmp0 (method_name, "AddSeat") == 0)
330         g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "AddSeat is deprecated");
331     else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
332     {
333         gint display_number;
334         Seat *seat;
335
336         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
337         {
338             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
339             return;
340         }
341
342         g_variant_get (parameters, "(i)", &display_number);
343
344         g_debug ("Adding local X seat :%d", display_number);
345
346         seat = seat_new ("xremote", "xremote0"); // FIXME: What to use for a name?
347         if (seat)
348         {
349             gchar *display_number_string;
350
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);
355         }
356
357         if (!seat)
358         {
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");
361             return;
362         }
363
364         if (display_manager_add_seat (display_manager, seat))
365         {
366             SeatBusEntry *entry;
367
368             entry = g_hash_table_lookup (seat_bus_entries, seat);
369             g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
370         }
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);
374     }
375     else
376         g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
377 }
378
379 static GVariant *
380 handle_seat_get_property (GDBusConnection       *connection,
381                           const gchar           *sender,
382                           const gchar           *object_path,
383                           const gchar           *interface_name,
384                           const gchar           *property_name,
385                           GError               **error,
386                           gpointer               user_data)
387 {
388     Seat *seat = user_data;
389
390     if (g_strcmp0 (property_name, "CanSwitch") == 0)
391         return g_variant_new_boolean (seat_get_can_switch (seat));
392     if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
393         return g_variant_new_boolean (seat_get_allow_guest (seat));
394     else if (g_strcmp0 (property_name, "Sessions") == 0)
395     {
396         SeatBusEntry *entry;
397
398         entry = g_hash_table_lookup (seat_bus_entries, seat);
399         return get_session_list (entry->path);
400     }
401
402     return NULL;
403 }
404
405 static void
406 handle_seat_call (GDBusConnection       *connection,
407                   const gchar           *sender,
408                   const gchar           *object_path,
409                   const gchar           *interface_name,
410                   const gchar           *method_name,
411                   GVariant              *parameters,
412                   GDBusMethodInvocation *invocation,
413                   gpointer               user_data)
414 {
415     Seat *seat = user_data;
416
417     if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
418     {
419         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
420             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
421
422         if (seat_switch_to_greeter (seat))
423             g_dbus_method_invocation_return_value (invocation, NULL);
424         else// FIXME: Need to make proper error
425             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to greeter");
426     }
427     else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
428     {
429         const gchar *username, *session_name;
430
431         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
432             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
433
434         g_variant_get (parameters, "(&s&s)", &username, &session_name);
435         if (strcmp (session_name, "") == 0)
436             session_name = NULL;
437
438         if (seat_switch_to_user (seat, username, session_name))
439             g_dbus_method_invocation_return_value (invocation, NULL);
440         else// FIXME: Need to make proper error
441             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to user");
442     }
443     else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
444     {
445         const gchar *session_name;
446
447         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
448             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
449
450         g_variant_get (parameters, "(&s)", &session_name);
451         if (strcmp (session_name, "") == 0)
452             session_name = NULL;
453
454         if (seat_switch_to_guest (seat, session_name))
455             g_dbus_method_invocation_return_value (invocation, NULL);
456         else// FIXME: Need to make proper error
457             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to switch to guest");
458     }
459     else if (g_strcmp0 (method_name, "Lock") == 0)
460     {
461         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
462             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
463
464         /* FIXME: Should only allow locks if have a session on this seat */
465         if (seat_lock (seat, NULL))
466             g_dbus_method_invocation_return_value (invocation, NULL);
467         else// FIXME: Need to make proper error
468             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to lock seat");
469     }
470     else
471         g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
472 }
473
474 static Seat *
475 get_seat_for_session (Session *session)
476 {
477     GList *seat_link;
478
479     for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
480     {
481         Seat *seat = seat_link->data;
482         GList *session_link;
483
484         for (session_link = seat_get_sessions (seat); session_link; session_link = session_link->next)
485         {
486             Session *s = session_link->data;
487
488             if (s == session)
489                 return seat;
490         }
491     }
492
493     return NULL;
494 }
495
496 static GVariant *
497 handle_session_get_property (GDBusConnection       *connection,
498                              const gchar           *sender,
499                              const gchar           *object_path,
500                              const gchar           *interface_name,
501                              const gchar           *property_name,
502                              GError               **error,
503                              gpointer               user_data)
504 {
505     Session *session = user_data;
506     SessionBusEntry *entry;
507
508     entry = g_hash_table_lookup (session_bus_entries, session);
509     if (g_strcmp0 (property_name, "Seat") == 0)
510         return g_variant_new_object_path (entry ? entry->seat_path : "");
511     else if (g_strcmp0 (property_name, "UserName") == 0)
512         return g_variant_new_string (session_get_username (session));
513
514     return NULL;
515 }
516
517 static void
518 handle_session_call (GDBusConnection       *connection,
519                      const gchar           *sender,
520                      const gchar           *object_path,
521                      const gchar           *interface_name,
522                      const gchar           *method_name,
523                      GVariant              *parameters,
524                      GDBusMethodInvocation *invocation,
525                      gpointer               user_data)
526 {
527     Session *session = user_data;
528
529     if (g_strcmp0 (method_name, "Lock") == 0)
530     {
531         Seat *seat;
532
533         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
534             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "Invalid arguments");
535
536         seat = get_seat_for_session (session);
537         /* FIXME: Should only allow locks if have a session on this seat */
538         seat_lock (seat, session_get_username (session));
539         g_dbus_method_invocation_return_value (invocation, NULL);
540     }
541     else
542         g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD, "Unknown method");
543 }
544
545 static SeatBusEntry *
546 seat_bus_entry_new (const gchar *path)
547 {
548     SeatBusEntry *entry;
549
550     entry = g_malloc0 (sizeof (SeatBusEntry));
551     entry->path = g_strdup (path);
552
553     return entry;
554 }
555
556 static SessionBusEntry *
557 session_bus_entry_new (const gchar *path, const gchar *seat_path)
558 {
559     SessionBusEntry *entry;
560
561     entry = g_malloc0 (sizeof (SessionBusEntry));
562     entry->path = g_strdup (path);
563     entry->seat_path = g_strdup (seat_path);
564
565     return entry;
566 }
567
568 static void
569 emit_object_value_changed (GDBusConnection *bus, const gchar *path, const gchar *interface_name, const gchar *property_name, GVariant *property_value)
570 {
571     GVariantBuilder builder;
572     GError *error = NULL;
573
574     g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
575     g_variant_builder_add (&builder, "{sv}", property_name, property_value);
576
577     if (!g_dbus_connection_emit_signal (bus,
578                                         NULL,
579                                         path,
580                                         "org.freedesktop.DBus.Properties",
581                                         "PropertiesChanged",
582                                         g_variant_new ("(sa{sv}as)", interface_name, &builder, NULL),
583                                         &error))
584         g_warning ("Failed to emit PropertiesChanged signal: %s", error->message);
585     g_clear_error (&error);
586 }
587
588 static void
589 emit_object_signal (GDBusConnection *bus, const gchar *path, const gchar *signal_name, const gchar *object_path)
590 {
591     GError *error = NULL;
592
593     if (!g_dbus_connection_emit_signal (bus,
594                                         NULL,
595                                         path,
596                                         "org.freedesktop.DisplayManager",
597                                         signal_name,
598                                         g_variant_new ("(o)", object_path),
599                                         &error))
600         g_warning ("Failed to emit %s signal on %s: %s", signal_name, path, error->message);
601     g_clear_error (&error);
602 }
603
604 static void
605 seat_bus_entry_free (gpointer data)
606 {
607     SeatBusEntry *entry = data;
608
609     g_free (entry->path);
610     g_free (entry);
611 }
612
613 static void
614 session_bus_entry_free (gpointer data)
615 {
616     SessionBusEntry *entry = data;
617
618     g_free (entry->path);
619     g_free (entry->seat_path);
620     g_free (entry);
621 }
622
623 static void
624 running_user_session_cb (Seat *seat, Session *session)
625 {
626     static const GDBusInterfaceVTable session_vtable =
627     {
628         handle_session_call,
629         handle_session_get_property
630     };
631     SeatBusEntry *seat_entry;
632     SessionBusEntry *session_entry;
633     gchar *path;
634     GError *error = NULL;
635
636     /* Set environment variables when session runs */
637     seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
638     session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
639     path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
640     session_index++;
641     session_set_env (session, "XDG_SESSION_PATH", path);
642     g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
643
644     session_entry = session_bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL);
645     g_hash_table_insert (session_bus_entries, g_object_ref (session), session_entry);
646
647     g_debug ("Registering session with bus path %s", session_entry->path);
648
649     session_entry->bus_id = g_dbus_connection_register_object (bus,
650                                                                session_entry->path,
651                                                                session_info->interfaces[0],
652                                                                &session_vtable,
653                                                                g_object_ref (session), g_object_unref,
654                                                                &error);
655     if (session_entry->bus_id == 0)
656         g_warning ("Failed to register user session: %s", error->message);
657     g_clear_error (&error);
658
659     emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
660     emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionAdded", session_entry->path);
661
662     emit_object_value_changed (bus, seat_entry->path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (session_entry->seat_path));
663     emit_object_signal (bus, seat_entry->path, "SessionAdded", session_entry->path);
664 }
665
666 static void
667 session_removed_cb (Seat *seat, Session *session)
668 {
669     SessionBusEntry *entry;
670     gchar *seat_path = NULL;
671
672     g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
673
674     entry = g_hash_table_lookup (session_bus_entries, session);
675     if (entry)
676     {
677         g_dbus_connection_unregister_object (bus, entry->bus_id);
678         emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SessionRemoved", entry->path);
679         emit_object_signal (bus, entry->seat_path, "SessionRemoved", entry->path);
680         seat_path = g_strdup (entry->seat_path);
681     }
682
683     g_hash_table_remove (session_bus_entries, session);
684
685     if (seat_path)
686     {
687         emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Sessions", get_session_list (NULL));
688         emit_object_value_changed (bus, seat_path, "org.freedesktop.DisplayManager.Seat", "Sessions", get_session_list (seat_path));
689         g_free (seat_path);
690     }
691 }
692
693 static void
694 seat_added_cb (DisplayManager *display_manager, Seat *seat)
695 {
696     static const GDBusInterfaceVTable seat_vtable =
697     {
698         handle_seat_call,
699         handle_seat_get_property
700     };
701     gchar *path;
702     SeatBusEntry *entry;
703     GError *error = NULL;
704
705     path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
706     seat_index++;
707
708     entry = seat_bus_entry_new (path);
709     g_free (path);
710     g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
711
712     g_debug ("Registering seat with bus path %s", entry->path);
713
714     entry->bus_id = g_dbus_connection_register_object (bus,
715                                                        entry->path,
716                                                        seat_info->interfaces[0],
717                                                        &seat_vtable,
718                                                        g_object_ref (seat), g_object_unref,
719                                                        &error);
720     if (entry->bus_id == 0)
721         g_warning ("Failed to register seat: %s", error->message);
722     g_clear_error (&error);
723
724     emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
725     emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatAdded", entry->path);
726
727     g_signal_connect (seat, SEAT_SIGNAL_RUNNING_USER_SESSION, G_CALLBACK (running_user_session_cb), NULL);
728     g_signal_connect (seat, SEAT_SIGNAL_SESSION_REMOVED, G_CALLBACK (session_removed_cb), NULL);
729 }
730
731 static void
732 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
733 {
734     SeatBusEntry *entry;
735
736     entry = g_hash_table_lookup (seat_bus_entries, seat);
737     if (entry)
738     {
739         g_dbus_connection_unregister_object (bus, entry->bus_id);
740         emit_object_signal (bus, "/org/freedesktop/DisplayManager", "SeatRemoved", entry->path);
741     }
742
743     g_hash_table_remove (seat_bus_entries, seat);
744   
745     emit_object_value_changed (bus, "/org/freedesktop/DisplayManager", "org.freedesktop.DisplayManager", "Seats", get_seat_list ());
746 }
747
748 static gboolean
749 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
750 {
751     SeatXDMCPSession *seat;
752     gboolean result;
753
754     seat = seat_xdmcp_session_new (session);
755     set_seat_properties (SEAT (seat), NULL);
756     result = display_manager_add_seat (display_manager, SEAT (seat));
757     g_object_unref (seat);
758
759     return result;
760 }
761
762 static void
763 vnc_connection_cb (VNCServer *server, GSocket *connection)
764 {
765     SeatXVNC *seat;
766
767     seat = seat_xvnc_new (connection);
768     set_seat_properties (SEAT (seat), NULL);
769     display_manager_add_seat (display_manager, SEAT (seat));
770     g_object_unref (seat);
771 }
772
773 static void
774 bus_acquired_cb (GDBusConnection *connection,
775                  const gchar     *name,
776                  gpointer         user_data)
777 {
778     const gchar *display_manager_interface =
779         "<node>"
780         "  <interface name='org.freedesktop.DisplayManager'>"
781         "    <property name='Seats' type='ao' access='read'/>"
782         "    <property name='Sessions' type='ao' access='read'/>"
783         "    <method name='AddSeat'>"
784         "      <arg name='type' direction='in' type='s'/>"
785         "      <arg name='properties' direction='in' type='a(ss)'/>"
786         "      <arg name='seat' direction='out' type='o'/>"
787         "    </method>"
788         "    <method name='AddLocalXSeat'>"
789         "      <arg name='display-number' direction='in' type='i'/>"
790         "      <arg name='seat' direction='out' type='o'/>"
791         "    </method>"
792         "    <signal name='SeatAdded'>"
793         "      <arg name='seat' type='o'/>"
794         "    </signal>"
795         "    <signal name='SeatRemoved'>"
796         "      <arg name='seat' type='o'/>"
797         "    </signal>"
798         "    <signal name='SessionAdded'>"
799         "      <arg name='session' type='o'/>"
800         "    </signal>"
801         "    <signal name='SessionRemoved'>"
802         "      <arg name='session' type='o'/>"
803         "    </signal>"
804         "  </interface>"
805         "</node>";
806     static const GDBusInterfaceVTable display_manager_vtable =
807     {
808         handle_display_manager_call,
809         handle_display_manager_get_property
810     };
811     const gchar *seat_interface =
812         "<node>"
813         "  <interface name='org.freedesktop.DisplayManager.Seat'>"
814         "    <property name='CanSwitch' type='b' access='read'/>"
815         "    <property name='HasGuestAccount' type='b' access='read'/>"
816         "    <property name='Sessions' type='ao' access='read'/>"
817         "    <method name='SwitchToGreeter'/>"
818         "    <method name='SwitchToUser'>"
819         "      <arg name='username' direction='in' type='s'/>"
820         "      <arg name='session-name' direction='in' type='s'/>"
821         "    </method>"
822         "    <method name='SwitchToGuest'>"
823         "      <arg name='session-name' direction='in' type='s'/>"
824         "    </method>"
825         "    <method name='Lock'/>"
826         "    <signal name='SessionAdded'>"
827         "      <arg name='session' type='o'/>"
828         "    </signal>"
829         "    <signal name='SessionRemoved'>"
830         "      <arg name='session' type='o'/>"
831         "    </signal>"
832         "  </interface>"
833         "</node>";
834     const gchar *session_interface =
835         "<node>"
836         "  <interface name='org.freedesktop.DisplayManager.Session'>"
837         "    <property name='Seat' type='o' access='read'/>"
838         "    <property name='UserName' type='s' access='read'/>"
839         "    <method name='Lock'/>"
840         "  </interface>"
841         "</node>";
842     GDBusNodeInfo *display_manager_info;
843     GList *link;
844     GError *error = NULL;
845
846     g_debug ("Acquired bus name %s", name);
847
848     bus = connection;
849
850     display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
851     g_assert (display_manager_info != NULL);
852     seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
853     g_assert (seat_info != NULL);
854     session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
855     g_assert (session_info != NULL);
856
857     reg_id = g_dbus_connection_register_object (connection,
858                                                 "/org/freedesktop/DisplayManager",
859                                                 display_manager_info->interfaces[0],
860                                                 &display_manager_vtable,
861                                                 NULL, NULL,
862                                                 &error);
863     if (reg_id == 0)
864         g_warning ("Failed to register display manager: %s", error->message);
865     g_clear_error (&error);
866     g_dbus_node_info_unref (display_manager_info);
867
868     seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, seat_bus_entry_free);
869     session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, session_bus_entry_free);
870
871     g_signal_connect (display_manager, DISPLAY_MANAGER_SIGNAL_SEAT_ADDED, G_CALLBACK (seat_added_cb), NULL);
872     g_signal_connect (display_manager, DISPLAY_MANAGER_SIGNAL_SEAT_REMOVED, G_CALLBACK (seat_removed_cb), NULL);
873     for (link = display_manager_get_seats (display_manager); link; link = link->next)
874         seat_added_cb (display_manager, (Seat *) link->data);
875
876     display_manager_start (display_manager);
877
878     /* Start the XDMCP server */
879     if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
880     {
881         gchar *key_name, *key = NULL;
882
883         xdmcp_server = xdmcp_server_new ();
884         if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
885         {
886             gint port;
887             port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
888             if (port > 0)
889                 xdmcp_server_set_port (xdmcp_server, port);
890         }
891         g_signal_connect (xdmcp_server, XDMCP_SERVER_SIGNAL_NEW_SESSION, G_CALLBACK (xdmcp_session_cb), NULL);
892
893         key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
894         if (key_name)
895         {
896             gchar *path;
897             GKeyFile *keys;
898             gboolean result;
899             GError *error = NULL;
900
901             path = g_build_filename (config_get_directory (config_get_instance ()), "keys.conf", NULL);
902
903             keys = g_key_file_new ();
904             result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
905             if (error)
906                 g_debug ("Error getting key %s", error->message);
907             g_clear_error (&error);
908
909             if (result)
910             {
911                 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
912                     key = g_key_file_get_string (keys, "keyring", key_name, NULL);
913                 else
914                     g_debug ("Key %s not defined", key_name);
915             }
916             g_free (path);
917             g_key_file_free (keys);
918         }
919         if (key)
920             xdmcp_server_set_key (xdmcp_server, key);
921         g_free (key_name);
922         g_free (key);
923
924         g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
925         xdmcp_server_start (xdmcp_server);
926     }
927
928     /* Start the VNC server */
929     if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
930     {
931         gchar *path;
932
933         path = g_find_program_in_path ("Xvnc");
934         if (path)
935         {
936             vnc_server = vnc_server_new ();
937             if (config_has_key (config_get_instance (), "VNCServer", "port"))
938             {
939                 gint port;
940                 port = config_get_integer (config_get_instance (), "VNCServer", "port");
941                 if (port > 0)
942                     vnc_server_set_port (vnc_server, port);
943             }
944             g_signal_connect (vnc_server, VNC_SERVER_SIGNAL_NEW_CONNECTION, G_CALLBACK (vnc_connection_cb), NULL);
945
946             g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
947             vnc_server_start (vnc_server);
948
949             g_free (path);
950         }
951         else
952             g_warning ("Can't start VNC server, Xvnc is not in the path");
953     }
954 }
955
956 static void
957 name_lost_cb (GDBusConnection *connection,
958               const gchar *name,
959               gpointer user_data)
960 {
961     if (connection)
962         g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
963     else
964         g_printerr ("Failed to get D-Bus connection\n");
965
966     exit (EXIT_FAILURE);
967 }
968
969 static gboolean
970 add_login1_seat (Login1Seat *login1_seat)
971 {
972     const gchar *seat_name = login1_seat_get_id (login1_seat);
973     gchar **types = NULL, **type;
974     GList *config_sections = NULL, *link;
975     Seat *seat = NULL;
976     gboolean is_seat0, started = FALSE;
977
978     g_debug ("New seat added from logind: %s", seat_name);
979     is_seat0 = strcmp (seat_name, "seat0") == 0;
980
981     config_sections = get_config_sections (seat_name);
982     for (link = g_list_last (config_sections); link; link = link->prev)
983     {
984         gchar *config_section = link->data;
985         types = config_get_string_list (config_get_instance (), config_section, "type");
986         if (types)
987             break;
988     }
989     g_list_free_full (config_sections, g_free);
990
991     for (type = types; !seat && type && *type; type++)
992         seat = seat_new (*type, seat_name);
993     g_strfreev (types);
994
995     if (seat)
996     {
997         set_seat_properties (seat, seat_name);
998
999         if (!login1_seat_get_can_multi_session (login1_seat))
1000         {
1001             g_debug ("Seat %s has property CanMultiSession=no", seat_name);
1002             /* XXX: uncomment this line after bug #1371250 is closed.
1003             seat_set_property (seat, "allow-user-switching", "false"); */
1004         }
1005
1006         if (is_seat0)
1007             seat_set_property (seat, "exit-on-failure", "true");
1008     }
1009     else
1010         g_debug ("Unable to create seat: %s", seat_name);
1011
1012     if (seat)
1013     {
1014         started = display_manager_add_seat (display_manager, seat);
1015         if (!started)
1016             g_debug ("Failed to start seat: %s", seat_name);
1017     }
1018
1019     g_object_unref (seat);
1020
1021     return started;
1022 }
1023
1024 static void
1025 remove_login1_seat (Login1Seat *login1_seat)
1026 {
1027     Seat *seat;
1028
1029     seat = display_manager_get_seat (display_manager, login1_seat_get_id (login1_seat));
1030     if (seat)
1031         seat_stop (seat);
1032 }
1033
1034 static void
1035 seat_stopped_cb (Seat *seat, Login1Seat *login1_seat)
1036 {
1037     update_login1_seat (login1_seat);
1038     g_signal_handlers_disconnect_matched (seat, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, login1_seat);
1039 }
1040
1041 static gboolean
1042 update_login1_seat (Login1Seat *login1_seat)
1043 {
1044     if (!config_get_boolean (config_get_instance (), "LightDM", "logind-check-graphical") ||
1045         login1_seat_get_can_graphical (login1_seat))
1046     {
1047         Seat *seat;
1048
1049         /* Wait for existing seat to stop or ignore if we already have a valid seat */
1050         seat = display_manager_get_seat (display_manager, login1_seat_get_id (login1_seat));
1051         if (seat)
1052         {
1053             if (seat_get_is_stopping (seat))
1054                 g_signal_connect (seat, SEAT_SIGNAL_STOPPED, G_CALLBACK (seat_stopped_cb), login1_seat);
1055             return TRUE;
1056         }
1057
1058         return add_login1_seat (login1_seat);
1059     }
1060     else
1061     {
1062         remove_login1_seat (login1_seat);
1063         return TRUE;
1064     }
1065 }
1066
1067 static void
1068 login1_can_graphical_changed_cb (Login1Seat *login1_seat)
1069 {
1070     g_debug ("Seat %s changes graphical state to %s", login1_seat_get_id (login1_seat), login1_seat_get_can_graphical (login1_seat) ? "true" : "false");
1071     update_login1_seat (login1_seat);
1072 }
1073
1074 static void
1075 login1_active_session_changed_cb (Login1Seat *login1_seat, const gchar *login1_session_id)
1076 {
1077     g_debug ("Seat %s changes active session to %s", login1_seat_get_id (login1_seat), login1_session_id);
1078
1079     Seat *seat;
1080     seat = display_manager_get_seat (display_manager, login1_seat_get_id (login1_seat));
1081
1082     if (seat)
1083     {
1084         Session *active_session;
1085         active_session = seat_get_expected_active_session (seat);
1086
1087         if (g_strcmp0 (login1_session_id, session_get_login1_session_id (active_session)) == 0)
1088         {
1089             // Session is already active
1090             g_debug ("Session %s is already active", login1_session_id);
1091             return;
1092         }
1093
1094         active_session = seat_find_session_by_login1_id (seat, login1_session_id);
1095         if (active_session != NULL)
1096         {
1097             g_debug ("Activating session %s", login1_session_id);
1098             seat_set_externally_activated_session (seat, active_session);
1099             return;
1100
1101         }
1102     }
1103 }
1104
1105 static gboolean
1106 login1_add_seat (Login1Seat *login1_seat)
1107 {
1108     if (config_get_boolean (config_get_instance (), "LightDM", "logind-check-graphical"))
1109         g_signal_connect (login1_seat, "can-graphical-changed", G_CALLBACK (login1_can_graphical_changed_cb), NULL);
1110
1111     g_signal_connect (login1_seat, LOGIN1_SIGNAL_ACTIVE_SESION_CHANGED, G_CALLBACK (login1_active_session_changed_cb), NULL);
1112
1113     return update_login1_seat (login1_seat);
1114 }
1115
1116 static void
1117 login1_service_seat_added_cb (Login1Service *service, Login1Seat *login1_seat)
1118 {
1119     if (login1_seat_get_can_graphical (login1_seat))
1120         g_debug ("Seat %s added from logind", login1_seat_get_id (login1_seat));
1121     else
1122         g_debug ("Seat %s added from logind without graphical output", login1_seat_get_id (login1_seat));
1123
1124     login1_add_seat (login1_seat);
1125 }
1126
1127 static void
1128 login1_service_seat_removed_cb (Login1Service *service, Login1Seat *login1_seat)
1129 {
1130     g_debug ("Seat %s removed from logind", login1_seat_get_id (login1_seat));
1131     g_signal_handlers_disconnect_matched (login1_seat, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, login1_can_graphical_changed_cb, NULL);
1132     g_signal_handlers_disconnect_matched (login1_seat, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, login1_active_session_changed_cb, NULL);
1133     remove_login1_seat (login1_seat);
1134 }
1135
1136 int
1137 main (int argc, char **argv)
1138 {
1139     FILE *pid_file;
1140     GOptionContext *option_context;
1141     gboolean result;
1142     gchar *dir;
1143     gboolean test_mode = FALSE;
1144     gchar *pid_path = "/var/run/lightdm.pid";
1145     gchar *log_dir = NULL;
1146     gchar *run_dir = NULL;
1147     gchar *cache_dir = NULL;
1148     gchar *default_log_dir = g_strdup (LOG_DIR);
1149     gchar *default_run_dir = g_strdup (RUN_DIR);
1150     gchar *default_cache_dir = g_strdup (CACHE_DIR);
1151     gboolean show_config = FALSE, show_version = FALSE;
1152     GList *link, *messages = NULL;
1153     GOptionEntry options[] =
1154     {
1155         { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
1156           /* Help string for command line --config flag */
1157           N_("Use configuration file"), "FILE" },
1158         { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
1159           /* Help string for command line --debug flag */
1160           N_("Print debugging messages"), NULL },
1161         { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
1162           /* Help string for command line --test-mode flag */
1163           N_("Run as unprivileged user, skipping things that require root access"), NULL },
1164         { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
1165           /* Help string for command line --pid-file flag */
1166           N_("File to write PID into"), "FILE" },
1167         { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
1168           /* Help string for command line --log-dir flag */
1169           N_("Directory to write logs to"), "DIRECTORY" },
1170         { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
1171           /* Help string for command line --run-dir flag */
1172           N_("Directory to store running state"), "DIRECTORY" },
1173         { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
1174           /* Help string for command line --cache-dir flag */
1175           N_("Directory to cache information"), "DIRECTORY" },
1176         { "show-config", 0, 0, G_OPTION_ARG_NONE, &show_config,
1177           /* Help string for command line --show-config flag */
1178           N_("Show combined configuration"), NULL },
1179         { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
1180           /* Help string for command line --version flag */
1181           N_("Show release version"), NULL },
1182         { NULL }
1183     };
1184     GError *error = NULL;
1185
1186     /* When lightdm starts sessions it needs to run itself in a new mode */
1187     if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
1188         return session_child_run (argc, argv);
1189
1190 #if !defined(GLIB_VERSION_2_36)
1191     g_type_init ();
1192 #endif
1193     loop = g_main_loop_new (NULL, FALSE);
1194
1195     messages = g_list_append (messages, g_strdup_printf ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ()));
1196
1197     g_signal_connect (process_get_current (), PROCESS_SIGNAL_GOT_SIGNAL, G_CALLBACK (signal_cb), NULL);
1198
1199     option_context = g_option_context_new (/* Arguments and description for --help test */
1200                                            _("- Display Manager"));
1201     g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
1202     result = g_option_context_parse (option_context, &argc, &argv, &error);
1203     if (error)
1204         g_printerr ("%s\n", error->message);
1205     g_clear_error (&error);
1206     g_option_context_free (option_context);
1207     if (!result)
1208     {
1209         g_printerr (/* Text printed out when an unknown command-line argument provided */
1210                     _("Run '%s --help' to see a full list of available command line options."), argv[0]);
1211         g_printerr ("\n");
1212         return EXIT_FAILURE;
1213     }
1214
1215     /* Show combined configuration if user requested it */
1216     if (show_config)
1217     {
1218         GList *sources, *link;
1219         gchar **groups, *last_source, *empty_source;
1220         GHashTable *source_ids;
1221         int i;
1222
1223         if (!config_load_from_standard_locations (config_get_instance (), config_path, NULL))
1224             return EXIT_FAILURE;
1225
1226         /* Number sources */
1227         sources = config_get_sources (config_get_instance ());
1228         source_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
1229         last_source = "";
1230         for (i = 0, link = sources; link; i++, link = link->next)
1231         {
1232             gchar *path, *id;
1233
1234             path = link->data;
1235             if (i < 26)
1236                 id = g_strdup_printf ("%c", 'A' + i);
1237             else
1238                 id = g_strdup_printf ("%d", i);
1239             g_hash_table_insert (source_ids, g_strdup (path), id);
1240             last_source = id;
1241         }
1242         empty_source = g_strdup (last_source);
1243         for (i = 0; empty_source[i] != '\0'; i++)
1244             empty_source[i] = ' ';
1245
1246         /* Print out keys */
1247         groups = config_get_groups (config_get_instance ());
1248         for (i = 0; groups[i]; i++)
1249         {
1250             gchar **keys;
1251             int j;
1252
1253             if (i != 0)
1254                 g_printerr ("\n");
1255             g_printerr ("%s  [%s]\n", empty_source, groups[i]);
1256
1257             keys = config_get_keys (config_get_instance (), groups[i]);
1258             for (j = 0; keys && keys[j]; j++)
1259             {
1260                 const gchar *source, *id;
1261                 gchar *value;
1262
1263                 source = config_get_source (config_get_instance (), groups[i], keys[j]);
1264                 id = source ? g_hash_table_lookup (source_ids, source) : empty_source;
1265                 value = config_get_string (config_get_instance (), groups[i], keys[j]);
1266                 g_printerr ("%s  %s=%s\n", id, keys[j], value);
1267                 g_free (value);
1268             }
1269
1270             g_strfreev (keys);
1271         }
1272         g_strfreev (groups);
1273
1274         /* Show mapping from source number to path */
1275         g_printerr ("\n");
1276         g_printerr ("Sources:\n");
1277         for (link = sources; link; link = link->next)
1278         {
1279             const gchar *path = link->data;
1280             const gchar *source;
1281
1282             source = g_hash_table_lookup (source_ids, path);
1283             g_printerr ("%s  %s\n", source, path);
1284         }
1285
1286         g_hash_table_destroy (source_ids);
1287
1288         return EXIT_SUCCESS;
1289     }
1290
1291     if (show_version)
1292     {
1293         /* NOTE: Is not translated so can be easily parsed */
1294         g_printerr ("lightdm %s\n", VERSION);
1295         return EXIT_SUCCESS;
1296     }
1297
1298     if (!test_mode && getuid () != 0)
1299     {
1300         g_printerr ("Only root can run Light Display Manager.  To run as a regular user for testing run with the --test-mode flag.\n");
1301         return EXIT_FAILURE;
1302     }
1303
1304     /* If running inside an X server use Xephyr for display */
1305     if (getenv ("DISPLAY") && getuid () != 0)
1306     {
1307         gchar *x_server_path;
1308
1309         x_server_path = g_find_program_in_path ("Xephyr");
1310         if (!x_server_path)
1311         {
1312             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");
1313             return EXIT_FAILURE;
1314         }
1315         g_free (x_server_path);
1316     }
1317
1318     /* Make sure the system binary directory (where the greeters are installed) is in the path */
1319     if (test_mode)
1320     {
1321         const gchar *path = g_getenv ("PATH");
1322         gchar *new_path;
1323
1324         if (path)
1325             new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
1326         else
1327             new_path = g_strdup (SBIN_DIR);
1328         g_setenv ("PATH", new_path, TRUE);
1329         g_free (new_path);
1330     }
1331
1332     /* Write PID file */
1333     pid_file = fopen (pid_path, "w");
1334     if (pid_file)
1335     {
1336         fprintf (pid_file, "%d\n", getpid ());
1337         fclose (pid_file);
1338     }
1339
1340     /* If not running as root write output to directories we control */
1341     if (getuid () != 0)
1342     {
1343         g_free (default_log_dir);
1344         default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
1345         g_free (default_run_dir);
1346         default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
1347         g_free (default_cache_dir);
1348         default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
1349     }
1350
1351     /* Load config file(s) */
1352     if (!config_load_from_standard_locations (config_get_instance (), config_path, &messages))
1353         exit (EXIT_FAILURE);
1354     g_free (config_path);
1355
1356     /* Set default values */
1357     if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
1358         config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
1359     if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
1360         config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
1361     if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
1362         config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
1363     if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
1364         config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
1365     if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
1366         config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
1367     if (!config_has_key (config_get_instance (), "Seat:*", "type"))
1368         config_set_string (config_get_instance (), "Seat:*", "type", "xlocal");
1369     if (!config_has_key (config_get_instance (), "Seat:*", "pam-service"))
1370         config_set_string (config_get_instance (), "Seat:*", "pam-service", "lightdm");
1371     if (!config_has_key (config_get_instance (), "Seat:*", "pam-autologin-service"))
1372         config_set_string (config_get_instance (), "Seat:*", "pam-autologin-service", "lightdm-autologin");
1373     if (!config_has_key (config_get_instance (), "Seat:*", "pam-greeter-service"))
1374         config_set_string (config_get_instance (), "Seat:*", "pam-greeter-service", "lightdm-greeter");
1375     if (!config_has_key (config_get_instance (), "Seat:*", "xserver-command"))
1376         config_set_string (config_get_instance (), "Seat:*", "xserver-command", "X");
1377     if (!config_has_key (config_get_instance (), "Seat:*", "xserver-share"))
1378         config_set_boolean (config_get_instance (), "Seat:*", "xserver-share", TRUE);
1379     if (!config_has_key (config_get_instance (), "Seat:*", "unity-compositor-command"))
1380         config_set_string (config_get_instance (), "Seat:*", "unity-compositor-command", "unity-system-compositor");
1381     if (!config_has_key (config_get_instance (), "Seat:*", "start-session"))
1382         config_set_boolean (config_get_instance (), "Seat:*", "start-session", TRUE);
1383     if (!config_has_key (config_get_instance (), "Seat:*", "allow-user-switching"))
1384         config_set_boolean (config_get_instance (), "Seat:*", "allow-user-switching", TRUE);
1385     if (!config_has_key (config_get_instance (), "Seat:*", "allow-guest"))
1386         config_set_boolean (config_get_instance (), "Seat:*", "allow-guest", TRUE);
1387     if (!config_has_key (config_get_instance (), "Seat:*", "greeter-allow-guest"))
1388         config_set_boolean (config_get_instance (), "Seat:*", "greeter-allow-guest", TRUE);
1389     if (!config_has_key (config_get_instance (), "Seat:*", "greeter-show-remote-login"))
1390         config_set_boolean (config_get_instance (), "Seat:*", "greeter-show-remote-login", TRUE);
1391     if (!config_has_key (config_get_instance (), "Seat:*", "greeter-session"))
1392         config_set_string (config_get_instance (), "Seat:*", "greeter-session", GREETER_SESSION);
1393     if (!config_has_key (config_get_instance (), "Seat:*", "user-session"))
1394         config_set_string (config_get_instance (), "Seat:*", "user-session", USER_SESSION);
1395     if (!config_has_key (config_get_instance (), "Seat:*", "session-wrapper"))
1396         config_set_string (config_get_instance (), "Seat:*", "session-wrapper", "lightdm-session");
1397     if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
1398         config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
1399     g_free (default_log_dir);
1400     if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
1401         config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
1402     g_free (default_run_dir);
1403     if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
1404         config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
1405     g_free (default_cache_dir);
1406     if (!config_has_key (config_get_instance (), "LightDM", "sessions-directory"))
1407         config_set_string (config_get_instance (), "LightDM", "sessions-directory", SESSIONS_DIR);
1408     if (!config_has_key (config_get_instance (), "LightDM", "remote-sessions-directory"))
1409         config_set_string (config_get_instance (), "LightDM", "remote-sessions-directory", REMOTE_SESSIONS_DIR);
1410     if (!config_has_key (config_get_instance (), "LightDM", "greeters-directory"))
1411         config_set_string (config_get_instance (), "LightDM", "greeters-directory", GREETERS_DIR);
1412
1413     /* Override defaults */
1414     if (log_dir)
1415         config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
1416     g_free (log_dir);
1417     if (run_dir)
1418         config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
1419     g_free (run_dir);
1420     if (cache_dir)
1421         config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
1422     g_free (cache_dir);
1423
1424     /* Create run and cache directories */
1425     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
1426     if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1427         g_warning ("Failed to make log directory %s: %s", dir, strerror (errno));
1428     g_free (dir);
1429     dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
1430     if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1431         g_warning ("Failed to make run directory %s: %s", dir, strerror (errno));
1432     g_free (dir);
1433     dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
1434     if (g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH) < 0)
1435         g_warning ("Failed to make cache directory %s: %s", dir, strerror (errno));
1436     g_free (dir);
1437
1438     log_init ();
1439
1440     /* Show queued messages once logging is complete */
1441     for (link = messages; link; link = link->next)
1442         g_debug ("%s", (gchar *)link->data);
1443     g_list_free_full (messages, g_free);
1444
1445     g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
1446     bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
1447                              LIGHTDM_BUS_NAME,
1448                              G_BUS_NAME_OWNER_FLAGS_NONE,
1449                              bus_acquired_cb,
1450                              NULL,
1451                              name_lost_cb,
1452                              NULL,
1453                              NULL);
1454
1455     if (getuid () != 0)
1456         g_debug ("Running in user mode");
1457     if (getenv ("DISPLAY"))
1458         g_debug ("Using Xephyr for X servers");
1459
1460     display_manager = display_manager_new ();
1461     g_signal_connect (display_manager, DISPLAY_MANAGER_SIGNAL_STOPPED, G_CALLBACK (display_manager_stopped_cb), NULL);
1462     g_signal_connect (display_manager, DISPLAY_MANAGER_SIGNAL_SEAT_REMOVED, G_CALLBACK (display_manager_seat_removed_cb), NULL);
1463
1464     shared_data_manager_start (shared_data_manager_get_instance ());
1465
1466     /* Connect to logind */
1467     if (login1_service_connect (login1_service_get_instance ()))
1468     {
1469         /* Load dynamic seats from logind */
1470         g_debug ("Monitoring logind for seats");
1471
1472         if (config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1473         {
1474             g_signal_connect (login1_service_get_instance (), LOGIN1_SERVICE_SIGNAL_SEAT_ADDED, G_CALLBACK (login1_service_seat_added_cb), NULL);
1475             g_signal_connect (login1_service_get_instance (), LOGIN1_SERVICE_SIGNAL_SEAT_REMOVED, G_CALLBACK (login1_service_seat_removed_cb), NULL);
1476
1477             for (link = login1_service_get_seats (login1_service_get_instance ()); link; link = link->next)
1478             {
1479                 Login1Seat *login1_seat = link->data;
1480                 if (!login1_add_seat (login1_seat))
1481                     return EXIT_FAILURE;
1482             }
1483         }
1484     }
1485     else
1486     {
1487         if (config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
1488         {
1489             gchar **types;
1490             gchar **type;
1491             Seat *seat = NULL;
1492
1493             g_debug ("Adding default seat");
1494
1495             types = config_get_string_list (config_get_instance (), "Seat:*", "type");
1496             for (type = types; type && *type; type++)
1497             {
1498                 seat = seat_new (*type, "seat0");
1499                 if (seat)
1500                     break;
1501             }
1502             g_strfreev (types);
1503             if (seat)
1504             {
1505                 set_seat_properties (seat, NULL);
1506                 seat_set_property (seat, "exit-on-failure", "true");
1507                 if (!display_manager_add_seat (display_manager, seat))
1508                     return EXIT_FAILURE;
1509                 g_object_unref (seat);
1510             }
1511             else
1512             {
1513                 g_warning ("Failed to create default seat");
1514                 return EXIT_FAILURE;
1515             }
1516         }
1517     }
1518
1519     g_main_loop_run (loop);
1520
1521     /* Clean up shared data manager */
1522     shared_data_manager_cleanup ();
1523
1524     /* Clean up user list */
1525     common_user_list_cleanup ();
1526
1527     /* Clean up display manager */
1528     g_object_unref (display_manager);
1529     display_manager = NULL;
1530
1531     /* Remove D-Bus interface */
1532     g_dbus_connection_unregister_object (bus, reg_id);
1533     g_bus_unown_name (bus_id);
1534     if (seat_bus_entries)
1535         g_hash_table_unref (seat_bus_entries);
1536     if (session_bus_entries)
1537         g_hash_table_unref (session_bus_entries);
1538
1539     g_debug ("Exiting with return value %d", exit_code);
1540     return exit_code;
1541 }