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