Back to index

lightdm  1.3.2
lightdm.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-2011 Robert Ancell.
00003  * Author: Robert Ancell <robert.ancell@canonical.com>
00004  * 
00005  * This program is free software: you can redistribute it and/or modify it under
00006  * the terms of the GNU General Public License as published by the Free Software
00007  * Foundation, either version 3 of the License, or (at your option) any later
00008  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
00009  * license.
00010  */
00011 
00012 #include <config.h>
00013 
00014 #include <stdlib.h>
00015 #include <stdio.h>
00016 #include <sys/stat.h>
00017 #include <glib.h>
00018 #include <glib/gi18n.h>
00019 #include <unistd.h>
00020 #include <fcntl.h>
00021 #include <sys/stat.h>
00022 #include <sys/mman.h>
00023 
00024 #include "configuration.h"
00025 #include "display-manager.h"
00026 #include "xdmcp-server.h"
00027 #include "vnc-server.h"
00028 #include "seat-xdmcp-session.h"
00029 #include "seat-xvnc.h"
00030 #include "xserver.h"
00031 #include "process.h"
00032 #include "session-child.h"
00033 
00034 static gchar *config_path = NULL;
00035 static GMainLoop *loop = NULL;
00036 static GTimer *log_timer;
00037 static int log_fd = -1;
00038 static gboolean debug = FALSE;
00039 
00040 static DisplayManager *display_manager = NULL;
00041 static XDMCPServer *xdmcp_server = NULL;
00042 static VNCServer *vnc_server = NULL;
00043 static guint bus_id = 0;
00044 static GDBusConnection *bus = NULL;
00045 static guint bus_id;
00046 static GDBusNodeInfo *seat_info;
00047 static GHashTable *seat_bus_entries = NULL;
00048 static guint seat_index = 0;
00049 static GDBusNodeInfo *session_info;
00050 static GHashTable *session_bus_entries = NULL;
00051 static guint session_index = 0;
00052 static gint exit_code = EXIT_SUCCESS;
00053 
00054 typedef struct
00055 {
00056     gchar *path;
00057     gchar *parent_path;
00058     gchar *removed_signal;
00059     guint bus_id;
00060 } BusEntry;
00061 
00062 #define LIGHTDM_BUS_NAME "org.freedesktop.DisplayManager"
00063 
00064 static void
00065 log_cb (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer data)
00066 {
00067     const gchar *prefix;
00068     gchar *text;
00069 
00070     switch (log_level & G_LOG_LEVEL_MASK)
00071     {
00072     case G_LOG_LEVEL_ERROR:
00073         prefix = "ERROR:";
00074         break;
00075     case G_LOG_LEVEL_CRITICAL:
00076         prefix = "CRITICAL:";
00077         break;
00078     case G_LOG_LEVEL_WARNING:
00079         prefix = "WARNING:";
00080         break;
00081     case G_LOG_LEVEL_MESSAGE:
00082         prefix = "MESSAGE:";
00083         break;
00084     case G_LOG_LEVEL_INFO:
00085         prefix = "INFO:";
00086         break;
00087     case G_LOG_LEVEL_DEBUG:
00088         prefix = "DEBUG:";
00089         break;
00090     default:
00091         prefix = "LOG:";
00092         break;
00093     }
00094 
00095     text = g_strdup_printf ("[%+.2fs] %s %s\n", g_timer_elapsed (log_timer, NULL), prefix, message);
00096 
00097     /* Log everything to a file */
00098     if (log_fd >= 0)
00099     {
00100         ssize_t n_written;
00101         n_written = write (log_fd, text, strlen (text));
00102         if (n_written < 0)
00103             ; /* Check result so compiler doesn't warn about it */
00104     }
00105 
00106     /* Log to stderr if requested */
00107     if (debug)
00108         g_printerr ("%s", text);
00109     else
00110         g_log_default_handler (log_domain, log_level, message, data);
00111 
00112     g_free (text);
00113 }
00114 
00115 static void
00116 log_init (void)
00117 {
00118     gchar *log_dir, *path;
00119 
00120     log_timer = g_timer_new ();
00121 
00122     /* Log to a file */
00123     log_dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
00124     path = g_build_filename (log_dir, "lightdm.log", NULL);
00125     g_free (log_dir);
00126 
00127     log_fd = open (path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
00128     fcntl (log_fd, F_SETFD, FD_CLOEXEC);
00129     g_log_set_default_handler (log_cb, NULL);
00130 
00131     g_debug ("Logging to %s", path);
00132     g_free (path);
00133 }
00134 
00135 static void
00136 signal_cb (Process *process, int signum)
00137 {
00138     g_debug ("Caught %s signal, shutting down", g_strsignal (signum));
00139     display_manager_stop (display_manager);
00140     // FIXME: Stop XDMCP server
00141 }
00142 
00143 static gboolean
00144 exit_cb (gpointer data)
00145 {
00146     /* Clean up display manager */
00147     g_object_unref (display_manager);
00148     display_manager = NULL;
00149 
00150     /* Remove D-Bus interface */
00151     g_bus_unown_name (bus_id);
00152     if (seat_bus_entries)
00153         g_hash_table_unref (seat_bus_entries);
00154     if (session_bus_entries)
00155         g_hash_table_unref (session_bus_entries);
00156 
00157     g_debug ("Exiting with return value %d", exit_code);
00158     exit (exit_code);
00159 }
00160 
00161 static void
00162 display_manager_stopped_cb (DisplayManager *display_manager)
00163 {
00164     g_debug ("Stopping daemon");
00165     g_idle_add (exit_cb, NULL);
00166 }
00167 
00168 static GVariant *
00169 handle_display_manager_get_property (GDBusConnection       *connection,
00170                                      const gchar           *sender,
00171                                      const gchar           *object_path,
00172                                      const gchar           *interface_name,
00173                                      const gchar           *property_name,
00174                                      GError               **error,
00175                                      gpointer               user_data)
00176 {
00177     GVariant *result = NULL;
00178 
00179     if (g_strcmp0 (property_name, "Seats") == 0)
00180     {
00181         GVariantBuilder *builder;
00182         GHashTableIter iter;
00183         gpointer value;
00184 
00185         builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
00186         g_hash_table_iter_init (&iter, seat_bus_entries);
00187         while (g_hash_table_iter_next (&iter, NULL, &value))
00188         {
00189             BusEntry *entry = value;
00190             g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
00191         }
00192         result = g_variant_builder_end (builder);
00193         g_variant_builder_unref (builder);
00194     }
00195     else if (g_strcmp0 (property_name, "Sessions") == 0)
00196     {
00197         GVariantBuilder *builder;
00198         GHashTableIter iter;
00199         gpointer value;
00200 
00201         builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
00202         g_hash_table_iter_init (&iter, session_bus_entries);
00203         while (g_hash_table_iter_next (&iter, NULL, &value))
00204         {
00205             BusEntry *entry = value;
00206             g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
00207         }
00208         result = g_variant_builder_end (builder);
00209         g_variant_builder_unref (builder);
00210     }
00211 
00212     return result;
00213 }
00214 
00215 static void
00216 set_seat_properties (Seat *seat, const gchar *config_section)
00217 {
00218     gchar **keys;
00219     gint i;
00220 
00221     keys = config_get_keys (config_get_instance (), "SeatDefaults");
00222     for (i = 0; keys[i]; i++)
00223     {
00224         gchar *value = config_get_string (config_get_instance (), "SeatDefaults", keys[i]);
00225         seat_set_property (seat, keys[i], value);
00226         g_free (value);
00227     }
00228     g_strfreev (keys);
00229 
00230     if (config_section)
00231     {
00232         keys = config_get_keys (config_get_instance (), config_section);
00233         for (i = 0; keys[i]; i++)
00234         {
00235             gchar *value = config_get_string (config_get_instance (), config_section, keys[i]);
00236             seat_set_property (seat, keys[i], value);
00237             g_free (value);
00238         }
00239         g_strfreev (keys);
00240     }
00241 }
00242 
00243 static void
00244 handle_display_manager_call (GDBusConnection       *connection,
00245                              const gchar           *sender,
00246                              const gchar           *object_path,
00247                              const gchar           *interface_name,
00248                              const gchar           *method_name,
00249                              GVariant              *parameters,
00250                              GDBusMethodInvocation *invocation,
00251                              gpointer               user_data)
00252 {
00253     if (g_strcmp0 (method_name, "AddSeat") == 0)
00254     {
00255         gchar *type;
00256         GVariantIter *property_iter;
00257         gchar *name, *value;
00258         Seat *seat;
00259 
00260         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa(ss))")))
00261             return;
00262 
00263         g_variant_get (parameters, "(&sa(ss))", &type, &property_iter);
00264 
00265         g_debug ("Adding seat of type %s", type);
00266 
00267         seat = seat_new (type);
00268         if (seat)
00269         {
00270             set_seat_properties (seat, NULL);
00271             while (g_variant_iter_loop (property_iter, "(&s&s)", &name, &value))
00272                 seat_set_property (seat, name, value);
00273         }
00274         g_variant_iter_free (property_iter);
00275 
00276         if (!seat)
00277         {
00278             // FIXME: Need to make proper error
00279             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create seat of type %s", type);
00280             return;
00281         }
00282 
00283         if (display_manager_add_seat (display_manager, seat))
00284         {
00285             BusEntry *entry;
00286 
00287             entry = g_hash_table_lookup (seat_bus_entries, seat);
00288             g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
00289         }
00290         else// FIXME: Need to make proper error
00291             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
00292         g_object_unref (seat);
00293     }
00294     else if (g_strcmp0 (method_name, "AddLocalXSeat") == 0)
00295     {
00296         gint display_number;
00297         Seat *seat;
00298 
00299         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(i)")))
00300             return;
00301 
00302         g_variant_get (parameters, "(i)", &display_number);
00303 
00304         g_debug ("Adding local X seat :%d", display_number);
00305 
00306         seat = seat_new ("xremote");
00307         if (seat)
00308         {
00309             gchar *display_number_string;
00310 
00311             set_seat_properties (seat, NULL);
00312             display_number_string = g_strdup_printf ("%d", display_number);
00313             seat_set_property (seat, "xserver-display-number", display_number_string);
00314             g_free (display_number_string);
00315         }
00316 
00317         if (!seat)
00318         {
00319             // FIXME: Need to make proper error
00320             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Unable to create local X seat");
00321             return;
00322         }
00323 
00324         if (display_manager_add_seat (display_manager, seat))
00325         {
00326             BusEntry *entry;
00327 
00328             entry = g_hash_table_lookup (seat_bus_entries, seat);
00329             g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", entry->path));
00330         }
00331         else// FIXME: Need to make proper error
00332             g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_FAILED, "Failed to start seat");
00333         g_object_unref (seat);
00334     }
00335 }
00336 
00337 static GVariant *
00338 handle_seat_get_property (GDBusConnection       *connection,
00339                           const gchar           *sender,
00340                           const gchar           *object_path,
00341                           const gchar           *interface_name,
00342                           const gchar           *property_name,
00343                           GError               **error,
00344                           gpointer               user_data)
00345 {
00346     Seat *seat = user_data;
00347     GVariant *result = NULL;
00348 
00349     if (g_strcmp0 (property_name, "CanSwitch") == 0)
00350         result = g_variant_new_boolean (seat_get_can_switch (seat));
00351     if (g_strcmp0 (property_name, "HasGuestAccount") == 0)
00352         result = g_variant_new_boolean (seat_get_allow_guest (seat));
00353     else if (g_strcmp0 (property_name, "Sessions") == 0)
00354     {
00355         GVariantBuilder *builder;
00356         GList *link;
00357 
00358         builder = g_variant_builder_new (G_VARIANT_TYPE ("ao"));
00359         for (link = seat_get_displays (seat); link; link = link->next)
00360         {
00361             Display *display = link->data;
00362             BusEntry *entry;
00363             entry = g_hash_table_lookup (session_bus_entries, display_get_session (display));
00364             if (entry)
00365                 g_variant_builder_add_value (builder, g_variant_new_object_path (entry->path));
00366         }
00367         result = g_variant_builder_end (builder);
00368         g_variant_builder_unref (builder);
00369     }
00370   
00371     return result;
00372 }
00373 
00374 static void
00375 handle_seat_call (GDBusConnection       *connection,
00376                   const gchar           *sender,
00377                   const gchar           *object_path,
00378                   const gchar           *interface_name,
00379                   const gchar           *method_name,
00380                   GVariant              *parameters,
00381                   GDBusMethodInvocation *invocation,
00382                   gpointer               user_data)
00383 {
00384     Seat *seat = user_data;
00385   
00386     if (g_strcmp0 (method_name, "SwitchToGreeter") == 0)
00387     {
00388         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("()")))
00389             return;
00390 
00391         seat_switch_to_greeter (seat);
00392         g_dbus_method_invocation_return_value (invocation, NULL);
00393     }
00394     else if (g_strcmp0 (method_name, "SwitchToUser") == 0)
00395     {
00396         const gchar *username, *session_name;
00397 
00398         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(ss)")))
00399             return;
00400 
00401         g_variant_get (parameters, "(&s&s)", &username, &session_name);
00402         if (strcmp (session_name, "") == 0)
00403             session_name = NULL;
00404 
00405         seat_switch_to_user (seat, username, session_name);
00406         g_dbus_method_invocation_return_value (invocation, NULL);
00407     }
00408     else if (g_strcmp0 (method_name, "SwitchToGuest") == 0)
00409     {
00410         const gchar *session_name;
00411 
00412         if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(s)")))
00413             return;
00414 
00415         g_variant_get (parameters, "(&s)", &session_name);
00416         if (strcmp (session_name, "") == 0)
00417             session_name = NULL;
00418 
00419         seat_switch_to_guest (seat, session_name);
00420         g_dbus_method_invocation_return_value (invocation, NULL);
00421     }
00422     else if (g_strcmp0 (method_name, "Lock") == 0)
00423     {
00424         /* FIXME: Should only allow locks if have a session on this seat */
00425         seat_lock (seat, NULL);
00426         g_dbus_method_invocation_return_value (invocation, NULL);
00427     }
00428 }
00429 
00430 static Seat *
00431 get_seat_for_session (Session *session)
00432 {
00433     GList *seat_link;
00434 
00435     for (seat_link = display_manager_get_seats (display_manager); seat_link; seat_link = seat_link->next)
00436     {
00437         Seat *seat = seat_link->data;
00438         GList *display_link;
00439 
00440         for (display_link = seat_get_displays (seat); display_link; display_link = display_link->next)
00441         {
00442             Display *display = display_link->data;
00443 
00444             if (display_get_session (display) == session)
00445                 return seat;
00446         }
00447     } 
00448 
00449     return NULL;
00450 }
00451 
00452 static GVariant *
00453 handle_session_get_property (GDBusConnection       *connection,
00454                              const gchar           *sender,
00455                              const gchar           *object_path,
00456                              const gchar           *interface_name,
00457                              const gchar           *property_name,
00458                              GError               **error,
00459                              gpointer               user_data)
00460 {
00461     Session *session = user_data;
00462     BusEntry *entry;
00463 
00464     entry = g_hash_table_lookup (session_bus_entries, session);
00465     if (g_strcmp0 (property_name, "Seat") == 0)
00466         return g_variant_new_object_path (entry ? entry->parent_path : "");
00467     else if (g_strcmp0 (property_name, "UserName") == 0)
00468         return g_variant_new_string (session_get_username (session));
00469 
00470     return NULL;
00471 }
00472 
00473 static void
00474 handle_session_call (GDBusConnection       *connection,
00475                      const gchar           *sender,
00476                      const gchar           *object_path,
00477                      const gchar           *interface_name,
00478                      const gchar           *method_name,
00479                      GVariant              *parameters,
00480                      GDBusMethodInvocation *invocation,
00481                      gpointer               user_data)
00482 {
00483     Session *session = user_data;
00484 
00485     if (g_strcmp0 (method_name, "Lock") == 0)
00486     {
00487         Seat *seat;
00488 
00489         seat = get_seat_for_session (session);
00490         /* FIXME: Should only allow locks if have a session on this seat */
00491         seat_lock (seat, session_get_username (session));
00492         g_dbus_method_invocation_return_value (invocation, NULL);
00493     }
00494 }
00495 
00496 static BusEntry *
00497 bus_entry_new (const gchar *path, const gchar *parent_path, const gchar *removed_signal)
00498 {
00499     BusEntry *entry;
00500 
00501     entry = g_malloc0 (sizeof (BusEntry));
00502     entry->path = g_strdup (path);
00503     entry->parent_path = g_strdup (parent_path);
00504     entry->removed_signal = g_strdup (removed_signal);
00505 
00506     return entry;
00507 }
00508 
00509 static void
00510 bus_entry_free (gpointer data)
00511 {
00512     BusEntry *entry = data;
00513 
00514     g_dbus_connection_unregister_object (bus, entry->bus_id);
00515 
00516     g_dbus_connection_emit_signal (bus,
00517                                    NULL,
00518                                    "/org/freedesktop/DisplayManager",
00519                                    "org.freedesktop.DisplayManager",
00520                                    entry->removed_signal,
00521                                    g_variant_new ("(o)", entry->path),
00522                                    NULL);
00523 
00524     g_free (entry->path);
00525     g_free (entry->parent_path);
00526     g_free (entry->removed_signal);
00527     g_free (entry);
00528 }
00529 
00530 static gboolean
00531 start_session_cb (Display *display, Seat *seat)
00532 {
00533     Session *session;
00534     BusEntry *seat_entry;
00535     gchar *path;
00536 
00537     session = display_get_session (display);
00538 
00539     /* Set environment variables when session runs */
00540     seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
00541     session_set_env (session, "XDG_SEAT_PATH", seat_entry->path);
00542     path = g_strdup_printf ("/org/freedesktop/DisplayManager/Session%d", session_index);
00543     session_index++;
00544     session_set_env (session, "XDG_SESSION_PATH", path);
00545     g_object_set_data_full (G_OBJECT (session), "XDG_SESSION_PATH", path, g_free);
00546 
00547     return FALSE;
00548 }
00549 
00550 static void
00551 session_stopped_cb (Session *session, Seat *seat)
00552 {
00553     g_signal_handlers_disconnect_matched (session, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, seat);
00554     g_hash_table_remove (session_bus_entries, session);
00555 }
00556 
00557 static gboolean
00558 session_started_cb (Display *display, Seat *seat)
00559 {
00560     static const GDBusInterfaceVTable session_vtable =
00561     {
00562         handle_session_call,
00563         handle_session_get_property
00564     };
00565     Session *session;
00566     BusEntry *seat_entry, *entry;
00567 
00568     session = display_get_session (display);
00569 
00570     g_signal_connect (session, "stopped", G_CALLBACK (session_stopped_cb), seat);
00571 
00572     seat_entry = g_hash_table_lookup (seat_bus_entries, seat);
00573     entry = bus_entry_new (g_object_get_data (G_OBJECT (session), "XDG_SESSION_PATH"), seat_entry ? seat_entry->path : NULL, "SessionRemoved");
00574     g_hash_table_insert (session_bus_entries, g_object_ref (session), entry);
00575 
00576     g_debug ("Registering session with bus path %s", entry->path);
00577 
00578     entry->bus_id = g_dbus_connection_register_object (bus,
00579                                                        entry->path,
00580                                                        session_info->interfaces[0],
00581                                                        &session_vtable,
00582                                                        g_object_ref (session), g_object_unref,
00583                                                        NULL);
00584     g_dbus_connection_emit_signal (bus,
00585                                    NULL,
00586                                    "/org/freedesktop/DisplayManager",
00587                                    "org.freedesktop.DisplayManager",
00588                                    "SessionAdded",
00589                                    g_variant_new ("(o)", entry->path),
00590                                    NULL);
00591 
00592     return FALSE;
00593 }
00594 
00595 static void
00596 display_added_cb (Seat *seat, Display *display)
00597 {
00598     g_signal_connect (display, "start-session", G_CALLBACK (start_session_cb), seat);
00599     g_signal_connect_after (display, "start-session", G_CALLBACK (session_started_cb), seat);
00600 }
00601 
00602 static void
00603 seat_added_cb (DisplayManager *display_manager, Seat *seat)
00604 {
00605     static const GDBusInterfaceVTable seat_vtable =
00606     {
00607         handle_seat_call,
00608         handle_seat_get_property
00609     };
00610     GList *link;
00611     gchar *path;
00612     BusEntry *entry;
00613 
00614     g_signal_connect (seat, "display-added", G_CALLBACK (display_added_cb), NULL);
00615     for (link = seat_get_displays (seat); link; link = link->next)
00616         display_added_cb (seat, (Display *) link->data);
00617 
00618     path = g_strdup_printf ("/org/freedesktop/DisplayManager/Seat%d", seat_index);
00619     seat_index++;
00620 
00621     entry = bus_entry_new (path, NULL, "SeatRemoved");
00622     g_free (path);
00623     g_hash_table_insert (seat_bus_entries, g_object_ref (seat), entry);
00624 
00625     g_debug ("Registering seat with bus path %s", entry->path);
00626 
00627     entry->bus_id = g_dbus_connection_register_object (bus,
00628                                                        entry->path,
00629                                                        seat_info->interfaces[0],
00630                                                        &seat_vtable,
00631                                                        g_object_ref (seat), g_object_unref,
00632                                                        NULL);
00633     g_dbus_connection_emit_signal (bus,
00634                                    NULL,
00635                                    "/org/freedesktop/DisplayManager",
00636                                    "org.freedesktop.DisplayManager",
00637                                    "SeatAdded",
00638                                    g_variant_new ("(o)", entry->path),
00639                                    NULL);
00640 }
00641 
00642 static void
00643 seat_removed_cb (DisplayManager *display_manager, Seat *seat)
00644 {
00645     gboolean exit_on_failure;
00646 
00647     exit_on_failure = seat_get_boolean_property (seat, "exit-on-failure");
00648     g_hash_table_remove (seat_bus_entries, seat);
00649 
00650     if (exit_on_failure)
00651     {
00652         g_debug ("Required seat has stopped");
00653         exit_code = EXIT_FAILURE;
00654         display_manager_stop (display_manager);
00655     }
00656 }
00657 
00658 static void
00659 bus_acquired_cb (GDBusConnection *connection,
00660                  const gchar     *name,
00661                  gpointer         user_data)
00662 {
00663     const gchar *display_manager_interface =
00664         "<node>"
00665         "  <interface name='org.freedesktop.DisplayManager'>"
00666         "    <property name='Seats' type='ao' access='read'/>"
00667         "    <property name='Sessions' type='ao' access='read'/>"
00668         "    <method name='AddSeat'>"
00669         "      <arg name='type' direction='in' type='s'/>"
00670         "      <arg name='properties' direction='in' type='a(ss)'/>"
00671         "      <arg name='seat' direction='out' type='o'/>"
00672         "    </method>"
00673         "    <method name='AddLocalXSeat'>"
00674         "      <arg name='display-number' direction='in' type='i'/>"
00675         "      <arg name='seat' direction='out' type='o'/>"
00676         "    </method>"
00677         "    <signal name='SeatAdded'>"
00678         "      <arg name='seat' type='o'/>"
00679         "    </signal>"
00680         "    <signal name='SeatRemoved'>"
00681         "      <arg name='seat' type='o'/>"
00682         "    </signal>"
00683         "    <signal name='SessionAdded'>"
00684         "      <arg name='session' type='o'/>"
00685         "    </signal>"
00686         "    <signal name='SessionRemoved'>"
00687         "      <arg name='session' type='o'/>"
00688         "    </signal>"
00689         "  </interface>"
00690         "</node>";
00691     static const GDBusInterfaceVTable display_manager_vtable =
00692     {
00693         handle_display_manager_call,
00694         handle_display_manager_get_property
00695     };
00696     const gchar *seat_interface =
00697         "<node>"
00698         "  <interface name='org.freedesktop.DisplayManager.Seat'>"
00699         "    <property name='CanSwitch' type='b' access='read'/>"
00700         "    <property name='HasGuestAccount' type='b' access='read'/>"
00701         "    <property name='Sessions' type='ao' access='read'/>"
00702         "    <method name='SwitchToGreeter'/>"
00703         "    <method name='SwitchToUser'>"
00704         "      <arg name='username' direction='in' type='s'/>"
00705         "      <arg name='session-name' direction='in' type='s'/>"
00706         "    </method>"
00707         "    <method name='SwitchToGuest'>"
00708         "      <arg name='session-name' direction='in' type='s'/>"
00709         "    </method>"
00710         "    <method name='Lock'/>"    
00711         "  </interface>"
00712         "</node>";
00713     const gchar *session_interface =
00714         "<node>"
00715         "  <interface name='org.freedesktop.DisplayManager.Session'>"
00716         "    <property name='Seat' type='o' access='read'/>"
00717         "    <property name='UserName' type='s' access='read'/>"
00718         "    <method name='Lock'/>"
00719         "  </interface>"
00720         "</node>";
00721     GDBusNodeInfo *display_manager_info;
00722     GList *link;
00723 
00724     g_debug ("Acquired bus name %s", name);
00725 
00726     bus = connection;
00727 
00728     display_manager_info = g_dbus_node_info_new_for_xml (display_manager_interface, NULL);
00729     g_assert (display_manager_info != NULL);
00730     seat_info = g_dbus_node_info_new_for_xml (seat_interface, NULL);
00731     g_assert (seat_info != NULL);
00732     session_info = g_dbus_node_info_new_for_xml (session_interface, NULL);
00733     g_assert (session_info != NULL);
00734 
00735     bus_id = g_dbus_connection_register_object (connection,
00736                                                 "/org/freedesktop/DisplayManager",
00737                                                 display_manager_info->interfaces[0],
00738                                                 &display_manager_vtable,
00739                                                 NULL, NULL,
00740                                                 NULL);
00741     g_dbus_node_info_unref  (display_manager_info);
00742 
00743     seat_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, bus_entry_free);
00744     session_bus_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, bus_entry_free);
00745 
00746     g_signal_connect (display_manager, "seat-added", G_CALLBACK (seat_added_cb), NULL);
00747     g_signal_connect (display_manager, "seat-removed", G_CALLBACK (seat_removed_cb), NULL);
00748     for (link = display_manager_get_seats (display_manager); link; link = link->next)
00749         seat_added_cb (display_manager, (Seat *) link->data);
00750 }
00751 
00752 static void
00753 name_lost_cb (GDBusConnection *connection,
00754               const gchar *name,
00755               gpointer user_data)
00756 {
00757     if (connection)
00758         g_printerr ("Failed to use bus name " LIGHTDM_BUS_NAME ", do you have appropriate permissions?\n");
00759     else
00760         g_printerr ("Failed to get D-Bus connection\n");
00761 
00762     exit (EXIT_FAILURE);
00763 }
00764 
00765 static gchar *
00766 path_make_absolute (gchar *path)
00767 {
00768     gchar *cwd, *abs_path;
00769   
00770     if (!path)
00771         return NULL;
00772 
00773     if (g_path_is_absolute (path))
00774         return path;
00775 
00776     cwd = g_get_current_dir ();
00777     abs_path = g_build_filename (cwd, path, NULL);
00778     g_free (path);
00779 
00780     return abs_path;
00781 }
00782 
00783 static gboolean
00784 xdmcp_session_cb (XDMCPServer *server, XDMCPSession *session)
00785 {
00786     SeatXDMCPSession *seat;
00787     gboolean result;
00788 
00789     seat = seat_xdmcp_session_new (session);
00790     set_seat_properties (SEAT (seat), NULL);
00791     result = display_manager_add_seat (display_manager, SEAT (seat));
00792     g_object_unref (seat);
00793 
00794     return result;
00795 }
00796 
00797 static void
00798 vnc_connection_cb (VNCServer *server, GSocket *connection)
00799 {
00800     SeatXVNC *seat;
00801 
00802     seat = seat_xvnc_new (connection);
00803     set_seat_properties (SEAT (seat), NULL);
00804     display_manager_add_seat (display_manager, SEAT (seat));
00805     g_object_unref (seat);
00806 }
00807 
00808 int
00809 main (int argc, char **argv)
00810 {
00811     FILE *pid_file;
00812     GOptionContext *option_context;
00813     gboolean result;
00814     gchar **groups, **i, *dir;
00815     gint n_seats = 0;
00816     gboolean explicit_config = FALSE;
00817     gboolean test_mode = FALSE;
00818     gchar *pid_path = "/var/run/lightdm.pid";
00819     gchar *xsessions_dir = NULL;
00820     gchar *xgreeters_dir = NULL;
00821     gchar *config_dir;
00822     gchar *log_dir = NULL;
00823     gchar *run_dir = NULL;
00824     gchar *cache_dir = NULL;
00825     gchar *default_log_dir = g_strdup (LOG_DIR);
00826     gchar *default_run_dir = g_strdup (RUN_DIR);
00827     gchar *default_cache_dir = g_strdup (CACHE_DIR);
00828     gboolean show_version = FALSE;
00829     GOptionEntry options[] = 
00830     {
00831         { "config", 'c', 0, G_OPTION_ARG_STRING, &config_path,
00832           /* Help string for command line --config flag */
00833           N_("Use configuration file"), "FILE" },
00834         { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug,
00835           /* Help string for command line --debug flag */
00836           N_("Print debugging messages"), NULL },
00837         { "test-mode", 0, 0, G_OPTION_ARG_NONE, &test_mode,
00838           /* Help string for command line --test-mode flag */
00839           N_("Run as unprivileged user, skipping things that require root access"), NULL },
00840         { "pid-file", 0, 0, G_OPTION_ARG_STRING, &pid_path,
00841           /* Help string for command line --pid-file flag */
00842           N_("File to write PID into"), "FILE" },
00843         { "xsessions-dir", 0, 0, G_OPTION_ARG_STRING, &xsessions_dir,
00844           /* Help string for command line --xsessions-dir flag */
00845           N_("Directory to load X sessions from"), "DIRECTORY" },
00846         { "xgreeters-dir", 0, 0, G_OPTION_ARG_STRING, &xgreeters_dir,
00847           /* Help string for command line --xgreeters-dir flag */
00848           N_("Directory to load X greeters from"), "DIRECTORY" },
00849         { "log-dir", 0, 0, G_OPTION_ARG_STRING, &log_dir,
00850           /* Help string for command line --log-dir flag */
00851           N_("Directory to write logs to"), "DIRECTORY" },
00852         { "run-dir", 0, 0, G_OPTION_ARG_STRING, &run_dir,
00853           /* Help string for command line --run-dir flag */
00854           N_("Directory to store running state"), "DIRECTORY" },
00855         { "cache-dir", 0, 0, G_OPTION_ARG_STRING, &cache_dir,
00856           /* Help string for command line --cache-dir flag */
00857           N_("Directory to cache information"), "DIRECTORY" },
00858         { "version", 'v', 0, G_OPTION_ARG_NONE, &show_version,
00859           /* Help string for command line --version flag */
00860           N_("Show release version"), NULL },
00861         { NULL }
00862     };
00863     GError *error = NULL;
00864 
00865     /* When lightdm starts sessions it needs to run itself in a new mode */
00866     if (argc >= 2 && strcmp (argv[1], "--session-child") == 0)
00867         return session_child_run (argc, argv);
00868 
00869     g_type_init ();
00870     loop = g_main_loop_new (NULL, FALSE);
00871 
00872     g_signal_connect (process_get_current (), "got-signal", G_CALLBACK (signal_cb), NULL);
00873 
00874     option_context = g_option_context_new (/* Arguments and description for --help test */
00875                                            _("- Display Manager"));
00876     g_option_context_add_main_entries (option_context, options, GETTEXT_PACKAGE);
00877     result = g_option_context_parse (option_context, &argc, &argv, &error);
00878     if (error)
00879         g_printerr ("%s\n", error->message);
00880     g_clear_error (&error);
00881     g_option_context_free (option_context);
00882     if (!result)
00883     {
00884         g_printerr (/* Text printed out when an unknown command-line argument provided */
00885                     _("Run '%s --help' to see a full list of available command line options."), argv[0]);
00886         g_printerr ("\n");
00887         return EXIT_FAILURE;
00888     }
00889 
00890     if (show_version)
00891     {
00892         /* NOTE: Is not translated so can be easily parsed */
00893         g_printerr ("lightdm %s\n", VERSION);
00894         return EXIT_SUCCESS;
00895     }
00896 
00897     if (config_path)
00898     {
00899         config_dir = g_path_get_basename (config_path);
00900         config_dir = path_make_absolute (config_dir);
00901         explicit_config = TRUE;
00902     }
00903     else
00904     {
00905         config_dir = g_strdup (CONFIG_DIR);
00906         config_path = g_build_filename (config_dir, "lightdm.conf", NULL);
00907     }
00908     config_set_string (config_get_instance (), "LightDM", "config-directory", config_dir);
00909     g_free (config_dir);
00910 
00911     if (!test_mode && getuid () != 0)
00912     {
00913         g_printerr ("Only root can run Light Display Manager.  To run as a regular user for testing run with the --test-mode flag.\n");
00914         return EXIT_FAILURE;
00915     }
00916 
00917     /* If running inside an X server use Xephyr for display */
00918     if (getenv ("DISPLAY") && getuid () != 0)
00919     {
00920         gchar *xserver_path;
00921 
00922         xserver_path = g_find_program_in_path ("Xephyr");
00923         if (!xserver_path)
00924         {
00925             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");
00926             return EXIT_FAILURE;
00927         }
00928         g_free (xserver_path);
00929     }
00930 
00931     /* Make sure the system binary directory (where the greeters are installed) is in the path */
00932     if (test_mode)
00933     {
00934         const gchar *path = g_getenv ("PATH");
00935         gchar *new_path;
00936 
00937         if (path)
00938             new_path = g_strdup_printf ("%s:%s", path, SBIN_DIR);
00939         else
00940             new_path = g_strdup (SBIN_DIR);
00941         g_setenv ("PATH", new_path, TRUE);
00942         g_free (new_path);
00943     }
00944 
00945     /* Write PID file */
00946     pid_file = fopen (pid_path, "w");
00947     if (pid_file)
00948     {
00949         fprintf (pid_file, "%d\n", getpid ());
00950         fclose (pid_file);
00951     }
00952 
00953     /* Always use absolute directories as child processes may run from different locations */
00954     xsessions_dir = path_make_absolute (xsessions_dir);
00955     xgreeters_dir = path_make_absolute (xgreeters_dir);
00956 
00957     /* If not running as root write output to directories we control */
00958     if (getuid () != 0)
00959     {
00960         g_free (default_log_dir);
00961         default_log_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "log", NULL);
00962         g_free (default_run_dir);
00963         default_run_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "run", NULL);
00964         g_free (default_cache_dir);
00965         default_cache_dir = g_build_filename (g_get_user_cache_dir (), "lightdm", "cache", NULL);
00966     }
00967 
00968     /* Load config file */
00969     if (!config_load_from_file (config_get_instance (), config_path, &error))
00970     {
00971         gboolean is_empty;
00972 
00973         is_empty = error && g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
00974       
00975         if (explicit_config || !is_empty)      
00976         {
00977             if (error)
00978                 g_printerr ("Failed to load configuration from %s: %s\n", config_path, error->message);
00979             exit (EXIT_FAILURE);
00980         }
00981     }
00982     g_clear_error (&error);
00983 
00984     /* Set default values */
00985     if (!config_has_key (config_get_instance (), "LightDM", "start-default-seat"))
00986         config_set_boolean (config_get_instance (), "LightDM", "start-default-seat", TRUE);
00987     if (!config_has_key (config_get_instance (), "LightDM", "minimum-vt"))
00988         config_set_integer (config_get_instance (), "LightDM", "minimum-vt", 7);
00989     if (!config_has_key (config_get_instance (), "LightDM", "guest-account-script"))
00990         config_set_string (config_get_instance (), "LightDM", "guest-account-script", "guest-account");
00991     if (!config_has_key (config_get_instance (), "LightDM", "greeter-user"))
00992         config_set_string (config_get_instance (), "LightDM", "greeter-user", GREETER_USER);
00993     if (!config_has_key (config_get_instance (), "LightDM", "lock-memory"))
00994         config_set_boolean (config_get_instance (), "LightDM", "lock-memory", TRUE);
00995     if (!config_has_key (config_get_instance (), "SeatDefaults", "type"))
00996         config_set_string (config_get_instance (), "SeatDefaults", "type", "xlocal");
00997     if (!config_has_key (config_get_instance (), "SeatDefaults", "xserver-command"))
00998         config_set_string (config_get_instance (), "SeatDefaults", "xserver-command", "X");
00999     if (!config_has_key (config_get_instance (), "SeatDefaults", "start-session"))
01000         config_set_boolean (config_get_instance (), "SeatDefaults", "start-session", TRUE);
01001     if (!config_has_key (config_get_instance (), "SeatDefaults", "allow-guest"))
01002         config_set_boolean (config_get_instance (), "SeatDefaults", "allow-guest", TRUE);
01003     if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-allow-guest"))
01004         config_set_boolean (config_get_instance (), "SeatDefaults", "greeter-allow-guest", TRUE);
01005     if (!config_has_key (config_get_instance (), "SeatDefaults", "greeter-session"))
01006         config_set_string (config_get_instance (), "SeatDefaults", "greeter-session", GREETER_SESSION);
01007     if (!config_has_key (config_get_instance (), "SeatDefaults", "user-session"))
01008         config_set_string (config_get_instance (), "SeatDefaults", "user-session", USER_SESSION);
01009     if (!config_has_key (config_get_instance (), "SeatDefaults", "session-wrapper"))
01010         config_set_string (config_get_instance (), "SeatDefaults", "session-wrapper", "lightdm-session");
01011     if (!config_has_key (config_get_instance (), "LightDM", "log-directory"))
01012         config_set_string (config_get_instance (), "LightDM", "log-directory", default_log_dir);
01013     g_free (default_log_dir);
01014     if (!config_has_key (config_get_instance (), "LightDM", "run-directory"))
01015         config_set_string (config_get_instance (), "LightDM", "run-directory", default_run_dir);
01016     g_free (default_run_dir);
01017     if (!config_has_key (config_get_instance (), "LightDM", "cache-directory"))
01018         config_set_string (config_get_instance (), "LightDM", "cache-directory", default_cache_dir);
01019     g_free (default_cache_dir);
01020     if (!config_has_key (config_get_instance (), "LightDM", "xsessions-directory"))
01021         config_set_string (config_get_instance (), "LightDM", "xsessions-directory", XSESSIONS_DIR);
01022     if (!config_has_key (config_get_instance (), "LightDM", "xgreeters-directory"))
01023         config_set_string (config_get_instance (), "LightDM", "xgreeters-directory", XGREETERS_DIR);
01024 
01025     /* Override defaults */
01026     if (log_dir)
01027         config_set_string (config_get_instance (), "LightDM", "log-directory", log_dir);
01028     g_free (log_dir);
01029     if (run_dir)
01030         config_set_string (config_get_instance (), "LightDM", "run-directory", run_dir);
01031     g_free (run_dir);
01032     if (cache_dir)
01033         config_set_string (config_get_instance (), "LightDM", "cache-directory", cache_dir);
01034     g_free (cache_dir);
01035     if (xsessions_dir)
01036         config_set_string (config_get_instance (), "LightDM", "xsessions-directory", xsessions_dir);
01037     g_free (xsessions_dir);
01038     if (xgreeters_dir)
01039         config_set_string (config_get_instance (), "LightDM", "xgreeters-directory", xgreeters_dir);
01040     g_free (xgreeters_dir);
01041 
01042     /* Create run and cache directories */
01043     dir = config_get_string (config_get_instance (), "LightDM", "log-directory");
01044     g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH);
01045     g_free (dir);
01046     dir = config_get_string (config_get_instance (), "LightDM", "run-directory");
01047     g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH);
01048     g_free (dir);
01049     dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
01050     g_mkdir_with_parents (dir, S_IRWXU | S_IXGRP | S_IXOTH);
01051     g_free (dir);
01052 
01053     log_init ();
01054 
01055     g_debug ("Starting Light Display Manager %s, UID=%i PID=%i", VERSION, getuid (), getpid ());
01056 
01057     g_debug ("Loaded configuration from %s", config_path);
01058     g_free (config_path);
01059 
01060     g_debug ("Using D-Bus name %s", LIGHTDM_BUS_NAME);
01061     bus_id = g_bus_own_name (getuid () == 0 ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
01062                              LIGHTDM_BUS_NAME,
01063                              G_BUS_NAME_OWNER_FLAGS_NONE,
01064                              bus_acquired_cb,
01065                              NULL,
01066                              name_lost_cb,
01067                              NULL,
01068                              NULL);
01069 
01070     if (config_get_boolean (config_get_instance (), "LightDM", "lock-memory"))
01071     {
01072         /* Protect memory from being paged to disk, as we deal with passwords */
01073         mlockall (MCL_CURRENT | MCL_FUTURE);
01074     }
01075 
01076     if (getuid () != 0)
01077         g_debug ("Running in user mode");
01078     if (getenv ("DISPLAY"))
01079         g_debug ("Using Xephyr for X servers");
01080 
01081     display_manager = display_manager_new ();
01082     g_signal_connect (display_manager, "stopped", G_CALLBACK (display_manager_stopped_cb), NULL);
01083 
01084     /* Load the static display entries */
01085     groups = config_get_groups (config_get_instance ());
01086     for (i = groups; *i; i++)
01087     {
01088         gchar *config_section = *i;
01089         gchar *type;
01090         Seat *seat;
01091 
01092         if (!g_str_has_prefix (config_section, "Seat:"))
01093             continue;
01094 
01095         g_debug ("Loading seat %s", config_section);
01096         type = config_get_string (config_get_instance (), config_section, "type");
01097         if (!type)
01098             type = config_get_string (config_get_instance (), "SeatDefaults", "type");
01099         seat = seat_new (type);
01100         g_free (type);
01101         if (seat)
01102         {
01103             set_seat_properties (seat, config_section);
01104             display_manager_add_seat (display_manager, seat);
01105             g_object_unref (seat);
01106             n_seats++;
01107         }
01108         else
01109             g_warning ("Failed to create seat %s", config_section);
01110     }
01111     g_strfreev (groups);
01112 
01113     /* If no seats start a default one */
01114     if (n_seats == 0 && config_get_boolean (config_get_instance (), "LightDM", "start-default-seat"))
01115     {
01116         gchar *type;
01117         Seat *seat;
01118 
01119         g_debug ("Adding default seat");
01120 
01121         type = config_get_string (config_get_instance (), "SeatDefaults", "type");
01122         seat = seat_new (type);
01123         g_free (type);
01124         if (seat)
01125         {
01126             set_seat_properties (seat, NULL);
01127             seat_set_property (seat, "exit-on-failure", "true");
01128             display_manager_add_seat (display_manager, seat);
01129             g_object_unref (seat);
01130         }
01131         else
01132             g_warning ("Failed to create default seat");
01133     }
01134 
01135     display_manager_start (display_manager);
01136 
01137     /* Start the XDMCP server */
01138     if (config_get_boolean (config_get_instance (), "XDMCPServer", "enabled"))
01139     {
01140         gchar *key_name, *key = NULL;
01141 
01142         xdmcp_server = xdmcp_server_new ();
01143         if (config_has_key (config_get_instance (), "XDMCPServer", "port"))
01144         {
01145             gint port;
01146             port = config_get_integer (config_get_instance (), "XDMCPServer", "port");
01147             if (port > 0)
01148                 xdmcp_server_set_port (xdmcp_server, port);
01149         }
01150         g_signal_connect (xdmcp_server, "new-session", G_CALLBACK (xdmcp_session_cb), NULL);
01151 
01152         key_name = config_get_string (config_get_instance (), "XDMCPServer", "key");
01153         if (key_name)
01154         {
01155             gchar *dir, *path;
01156             GKeyFile *keys;
01157             gboolean result;
01158             GError *error = NULL;
01159 
01160             dir = config_get_string (config_get_instance (), "LightDM", "config-directory");
01161             path = g_build_filename (dir, "keys.conf", NULL);
01162             g_free (dir);
01163 
01164             keys = g_key_file_new ();
01165             result = g_key_file_load_from_file (keys, path, G_KEY_FILE_NONE, &error);
01166             if (error)
01167                 g_debug ("Error getting key %s", error->message);
01168             g_clear_error (&error);
01169 
01170             if (result)
01171             {
01172                 if (g_key_file_has_key (keys, "keyring", key_name, NULL))
01173                     key = g_key_file_get_string (keys, "keyring", key_name, NULL);
01174                 else
01175                     g_debug ("Key %s not defined", key_name);
01176             }
01177             g_free (path);
01178             g_key_file_free (keys);
01179         }
01180         if (key)
01181             xdmcp_server_set_key (xdmcp_server, key);
01182         g_free (key_name);
01183         g_free (key);
01184 
01185         g_debug ("Starting XDMCP server on UDP/IP port %d", xdmcp_server_get_port (xdmcp_server));
01186         xdmcp_server_start (xdmcp_server);
01187     }
01188 
01189     /* Start the VNC server */
01190     if (config_get_boolean (config_get_instance (), "VNCServer", "enabled"))
01191     {
01192         gchar *path;
01193 
01194         path = g_find_program_in_path ("Xvnc");
01195         if (path)
01196         {
01197             vnc_server = vnc_server_new ();
01198             if (config_has_key (config_get_instance (), "VNCServer", "port"))
01199             {
01200                 gint port;
01201                 port = config_get_integer (config_get_instance (), "VNCServer", "port");
01202                 if (port > 0)
01203                     vnc_server_set_port (vnc_server, port);
01204             }
01205             g_signal_connect (vnc_server, "new-connection", G_CALLBACK (vnc_connection_cb), NULL);
01206 
01207             g_debug ("Starting VNC server on TCP/IP port %d", vnc_server_get_port (vnc_server));
01208             vnc_server_start (vnc_server);
01209 
01210             g_free (path);
01211         }
01212         else
01213             g_warning ("Can't start VNC server, Xvn is not in the path");
01214     }
01215 
01216     g_main_loop_run (loop);
01217 
01218     return EXIT_SUCCESS;
01219 }