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