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