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