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