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