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