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