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