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