[134] | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- |
---|
| 2 | * |
---|
| 3 | * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> |
---|
| 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #include "config.h" |
---|
| 21 | |
---|
| 22 | #include <stdlib.h> |
---|
| 23 | #include <locale.h> |
---|
| 24 | #include <unistd.h> |
---|
| 25 | #include <signal.h> |
---|
| 26 | #include <string.h> |
---|
| 27 | |
---|
| 28 | #include <glib.h> |
---|
| 29 | #include <glib/gi18n.h> |
---|
| 30 | #include <gdk/gdkx.h> |
---|
| 31 | #include <gtk/gtk.h> |
---|
| 32 | #include <gst/gst.h> |
---|
| 33 | #include <gconf/gconf-client.h> |
---|
| 34 | |
---|
| 35 | #include <dbus/dbus-glib.h> |
---|
| 36 | |
---|
| 37 | #include "gdm-log.h" |
---|
| 38 | #include "gdm-common.h" |
---|
| 39 | #include "gdm-signal-handler.h" |
---|
| 40 | #include "gdm-settings-client.h" |
---|
| 41 | #include "gdm-settings-keys.h" |
---|
| 42 | #include "gdm-profile.h" |
---|
| 43 | |
---|
| 44 | #include "gdm-greeter-session.h" |
---|
| 45 | |
---|
| 46 | #define DEBUG_KEY "/apps/gdm/simple-greeter/debug" |
---|
| 47 | |
---|
| 48 | #define SM_DBUS_NAME "org.gnome.SessionManager" |
---|
| 49 | #define SM_DBUS_PATH "/org/gnome/SessionManager" |
---|
| 50 | #define SM_DBUS_INTERFACE "org.gnome.SessionManager" |
---|
| 51 | |
---|
| 52 | #define SM_CLIENT_DBUS_INTERFACE "org.gnome.SessionManager.ClientPrivate" |
---|
| 53 | |
---|
| 54 | static DBusGConnection *bus_connection = NULL; |
---|
| 55 | static DBusGProxy *sm_proxy = NULL; |
---|
| 56 | static char *client_id = NULL; |
---|
| 57 | static DBusGProxy *client_proxy = NULL; |
---|
| 58 | |
---|
| 59 | static gboolean |
---|
| 60 | is_debug_set (void) |
---|
| 61 | { |
---|
| 62 | GConfClient *client; |
---|
| 63 | gboolean is; |
---|
| 64 | |
---|
| 65 | /* enable debugging for unstable builds */ |
---|
| 66 | if (gdm_is_version_unstable ()) { |
---|
| 67 | return TRUE; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | client = gconf_client_get_default (); |
---|
| 71 | is = gconf_client_get_bool (client, DEBUG_KEY, NULL); |
---|
| 72 | g_object_unref (client); |
---|
| 73 | |
---|
| 74 | return is; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | static gboolean |
---|
| 79 | signal_cb (int signo, |
---|
| 80 | gpointer data) |
---|
| 81 | { |
---|
| 82 | int ret; |
---|
| 83 | |
---|
| 84 | g_debug ("Got callback for signal %d", signo); |
---|
| 85 | |
---|
| 86 | ret = TRUE; |
---|
| 87 | |
---|
| 88 | switch (signo) { |
---|
| 89 | case SIGFPE: |
---|
| 90 | case SIGPIPE: |
---|
| 91 | /* let the fatal signals interrupt us */ |
---|
| 92 | g_debug ("Caught signal %d, shutting down abnormally.", signo); |
---|
| 93 | ret = FALSE; |
---|
| 94 | |
---|
| 95 | break; |
---|
| 96 | |
---|
| 97 | case SIGINT: |
---|
| 98 | case SIGTERM: |
---|
| 99 | /* let the fatal signals interrupt us */ |
---|
| 100 | g_debug ("Caught signal %d, shutting down normally.", signo); |
---|
| 101 | ret = FALSE; |
---|
| 102 | |
---|
| 103 | break; |
---|
| 104 | |
---|
| 105 | case SIGHUP: |
---|
| 106 | g_debug ("Got HUP signal"); |
---|
| 107 | /* FIXME: |
---|
| 108 | * Reread config stuff like system config files, VPN service files, etc |
---|
| 109 | */ |
---|
| 110 | ret = TRUE; |
---|
| 111 | |
---|
| 112 | break; |
---|
| 113 | |
---|
| 114 | case SIGUSR1: |
---|
| 115 | g_debug ("Got USR1 signal"); |
---|
| 116 | /* FIXME: |
---|
| 117 | * Play with log levels or something |
---|
| 118 | */ |
---|
| 119 | ret = TRUE; |
---|
| 120 | |
---|
| 121 | gdm_log_toggle_debug (); |
---|
| 122 | |
---|
| 123 | break; |
---|
| 124 | |
---|
| 125 | default: |
---|
| 126 | g_debug ("Caught unhandled signal %d", signo); |
---|
| 127 | ret = TRUE; |
---|
| 128 | |
---|
| 129 | break; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return ret; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | static gboolean |
---|
| 136 | session_manager_connect (void) |
---|
| 137 | { |
---|
| 138 | |
---|
| 139 | if (bus_connection == NULL) { |
---|
| 140 | GError *error; |
---|
| 141 | |
---|
| 142 | error = NULL; |
---|
| 143 | bus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); |
---|
| 144 | if (bus_connection == NULL) { |
---|
| 145 | g_message ("Failed to connect to the session bus: %s", |
---|
| 146 | error->message); |
---|
| 147 | g_error_free (error); |
---|
| 148 | exit (1); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | sm_proxy = dbus_g_proxy_new_for_name (bus_connection, |
---|
| 153 | SM_DBUS_NAME, |
---|
| 154 | SM_DBUS_PATH, |
---|
| 155 | SM_DBUS_INTERFACE); |
---|
| 156 | return (sm_proxy != NULL); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | static void |
---|
| 160 | stop_cb (gpointer data) |
---|
| 161 | { |
---|
| 162 | gtk_main_quit (); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | static gboolean |
---|
| 166 | end_session_response (gboolean is_okay, const gchar *reason) |
---|
| 167 | { |
---|
| 168 | gboolean ret; |
---|
| 169 | GError *error = NULL; |
---|
| 170 | |
---|
| 171 | ret = dbus_g_proxy_call (client_proxy, "EndSessionResponse", |
---|
| 172 | &error, |
---|
| 173 | G_TYPE_BOOLEAN, is_okay, |
---|
| 174 | G_TYPE_STRING, reason, |
---|
| 175 | G_TYPE_INVALID, |
---|
| 176 | G_TYPE_INVALID); |
---|
| 177 | |
---|
| 178 | if (!ret) { |
---|
| 179 | g_warning ("Failed to send session response %s", error->message); |
---|
| 180 | g_error_free (error); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | return ret; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | static void |
---|
| 187 | query_end_session_cb (guint flags, gpointer data) |
---|
| 188 | { |
---|
| 189 | end_session_response (TRUE, NULL); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | static void |
---|
| 193 | end_session_cb (guint flags, gpointer data) |
---|
| 194 | { |
---|
| 195 | end_session_response (TRUE, NULL); |
---|
| 196 | gtk_main_quit (); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | static gboolean |
---|
| 200 | register_client (void) |
---|
| 201 | { |
---|
| 202 | GError *error; |
---|
| 203 | gboolean res; |
---|
| 204 | const char *startup_id; |
---|
| 205 | const char *app_id; |
---|
| 206 | |
---|
| 207 | startup_id = g_getenv ("DESKTOP_AUTOSTART_ID"); |
---|
| 208 | app_id = "gdm-simple-greeter.desktop"; |
---|
| 209 | |
---|
| 210 | error = NULL; |
---|
| 211 | res = dbus_g_proxy_call (sm_proxy, |
---|
| 212 | "RegisterClient", |
---|
| 213 | &error, |
---|
| 214 | G_TYPE_STRING, app_id, |
---|
| 215 | G_TYPE_STRING, startup_id, |
---|
| 216 | G_TYPE_INVALID, |
---|
| 217 | DBUS_TYPE_G_OBJECT_PATH, &client_id, |
---|
| 218 | G_TYPE_INVALID); |
---|
| 219 | if (! res) { |
---|
| 220 | g_warning ("Failed to register client: %s", error->message); |
---|
| 221 | g_error_free (error); |
---|
| 222 | return FALSE; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | g_debug ("Client registered with session manager: %s", client_id); |
---|
| 226 | client_proxy = dbus_g_proxy_new_for_name (bus_connection, |
---|
| 227 | SM_DBUS_NAME, |
---|
| 228 | client_id, |
---|
| 229 | SM_CLIENT_DBUS_INTERFACE); |
---|
| 230 | |
---|
| 231 | dbus_g_proxy_add_signal (client_proxy, "Stop", G_TYPE_INVALID); |
---|
| 232 | dbus_g_proxy_connect_signal (client_proxy, "Stop", |
---|
| 233 | G_CALLBACK (stop_cb), NULL, NULL); |
---|
| 234 | |
---|
| 235 | dbus_g_proxy_add_signal (client_proxy, "QueryEndSession", G_TYPE_UINT, G_TYPE_INVALID); |
---|
| 236 | dbus_g_proxy_connect_signal (client_proxy, "QueryEndSession", |
---|
| 237 | G_CALLBACK (query_end_session_cb), NULL, NULL); |
---|
| 238 | |
---|
| 239 | dbus_g_proxy_add_signal (client_proxy, "EndSession", G_TYPE_UINT, G_TYPE_INVALID); |
---|
| 240 | dbus_g_proxy_connect_signal (client_proxy, "EndSession", |
---|
| 241 | G_CALLBACK (end_session_cb), NULL, NULL); |
---|
| 242 | |
---|
| 243 | g_unsetenv ("DESKTOP_AUTOSTART_ID"); |
---|
| 244 | |
---|
| 245 | return TRUE; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | int |
---|
| 249 | main (int argc, char *argv[]) |
---|
| 250 | { |
---|
| 251 | GError *error; |
---|
| 252 | GdmGreeterSession *session; |
---|
| 253 | gboolean res; |
---|
| 254 | GdmSignalHandler *signal_handler; |
---|
| 255 | |
---|
| 256 | bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); |
---|
| 257 | bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
---|
| 258 | textdomain (GETTEXT_PACKAGE); |
---|
| 259 | |
---|
| 260 | setlocale (LC_ALL, ""); |
---|
| 261 | |
---|
| 262 | gdm_set_fatal_warnings_if_unstable (); |
---|
| 263 | |
---|
| 264 | g_type_init (); |
---|
| 265 | |
---|
| 266 | gdm_profile_start ("Initializing settings client"); |
---|
| 267 | if (! gdm_settings_client_init (GDMCONFDIR "/gdm.schemas", "/")) { |
---|
| 268 | g_critical ("Unable to initialize settings client"); |
---|
| 269 | exit (1); |
---|
| 270 | } |
---|
| 271 | gdm_profile_end ("Initializing settings client"); |
---|
| 272 | |
---|
| 273 | g_debug ("Greeter session pid=%d display=%s xauthority=%s", |
---|
| 274 | (int)getpid (), |
---|
| 275 | g_getenv ("DISPLAY"), |
---|
| 276 | g_getenv ("XAUTHORITY")); |
---|
| 277 | |
---|
| 278 | /* FIXME: For testing to make it easier to attach gdb */ |
---|
| 279 | /*sleep (15);*/ |
---|
| 280 | |
---|
| 281 | gdm_log_init (); |
---|
| 282 | gdm_log_set_debug (is_debug_set ()); |
---|
| 283 | |
---|
| 284 | gtk_init (&argc, &argv); |
---|
| 285 | gst_init (&argc, &argv); |
---|
| 286 | |
---|
| 287 | signal_handler = gdm_signal_handler_new (); |
---|
| 288 | gdm_signal_handler_add_fatal (signal_handler); |
---|
| 289 | gdm_signal_handler_add (signal_handler, SIGTERM, signal_cb, NULL); |
---|
| 290 | gdm_signal_handler_add (signal_handler, SIGINT, signal_cb, NULL); |
---|
| 291 | gdm_signal_handler_add (signal_handler, SIGFPE, signal_cb, NULL); |
---|
| 292 | gdm_signal_handler_add (signal_handler, SIGHUP, signal_cb, NULL); |
---|
| 293 | gdm_signal_handler_add (signal_handler, SIGUSR1, signal_cb, NULL); |
---|
| 294 | |
---|
| 295 | gdm_profile_start ("Creating new greeter session"); |
---|
| 296 | session = gdm_greeter_session_new (); |
---|
| 297 | if (session == NULL) { |
---|
| 298 | g_critical ("Unable to create greeter session"); |
---|
| 299 | exit (1); |
---|
| 300 | } |
---|
| 301 | gdm_profile_end ("Creating new greeter session"); |
---|
| 302 | |
---|
| 303 | error = NULL; |
---|
| 304 | res = gdm_greeter_session_start (session, &error); |
---|
| 305 | if (! res) { |
---|
| 306 | g_warning ("Unable to start greeter session: %s", error->message); |
---|
| 307 | g_error_free (error); |
---|
| 308 | exit (1); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | res = session_manager_connect (); |
---|
| 312 | if (! res) { |
---|
| 313 | g_warning ("Unable to connect to session manager"); |
---|
| 314 | exit (1); |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | res = register_client (); |
---|
| 318 | if (! res) { |
---|
| 319 | g_warning ("Unable to register client with session manager"); |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | gtk_main (); |
---|
| 323 | |
---|
| 324 | if (session != NULL) { |
---|
| 325 | g_object_unref (session); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | return 0; |
---|
| 329 | } |
---|