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