[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 | |
---|
| 21 | #include "config.h" |
---|
| 22 | |
---|
| 23 | #include <stdlib.h> |
---|
| 24 | #include <stdio.h> |
---|
| 25 | #include <unistd.h> |
---|
| 26 | #include <errno.h> |
---|
| 27 | #include <string.h> |
---|
| 28 | #include <sys/types.h> |
---|
| 29 | #include <sys/stat.h> |
---|
| 30 | #include <fcntl.h> |
---|
| 31 | #include <signal.h> |
---|
| 32 | #include <locale.h> |
---|
| 33 | |
---|
| 34 | #include <glib.h> |
---|
| 35 | #include <glib/gi18n.h> |
---|
| 36 | #include <glib-object.h> |
---|
| 37 | |
---|
| 38 | #define DBUS_API_SUBJECT_TO_CHANGE |
---|
| 39 | #include <dbus/dbus-glib.h> |
---|
| 40 | #include <dbus/dbus-glib-lowlevel.h> |
---|
| 41 | |
---|
| 42 | #include "gdm-signal-handler.h" |
---|
| 43 | #include "gdm-log.h" |
---|
| 44 | #include "gdm-common.h" |
---|
| 45 | #include "gdm-simple-slave.h" |
---|
| 46 | #include "gdm-settings.h" |
---|
| 47 | #include "gdm-settings-direct.h" |
---|
| 48 | |
---|
| 49 | #include "gdm-settings-client.h" |
---|
| 50 | |
---|
| 51 | static GdmSettings *settings = NULL; |
---|
| 52 | static int gdm_return_code = 0; |
---|
| 53 | |
---|
| 54 | static DBusGConnection * |
---|
| 55 | get_system_bus (void) |
---|
| 56 | { |
---|
| 57 | GError *error; |
---|
| 58 | DBusGConnection *bus; |
---|
| 59 | DBusConnection *connection; |
---|
| 60 | |
---|
| 61 | error = NULL; |
---|
| 62 | bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); |
---|
| 63 | if (bus == NULL) { |
---|
| 64 | g_warning ("Couldn't connect to system bus: %s", |
---|
| 65 | error->message); |
---|
| 66 | g_error_free (error); |
---|
| 67 | goto out; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | connection = dbus_g_connection_get_connection (bus); |
---|
| 71 | dbus_connection_set_exit_on_disconnect (connection, FALSE); |
---|
| 72 | |
---|
| 73 | out: |
---|
| 74 | return bus; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | static gboolean |
---|
| 78 | signal_cb (int signo, |
---|
| 79 | gpointer data) |
---|
| 80 | { |
---|
| 81 | int ret; |
---|
| 82 | |
---|
| 83 | g_debug ("Got callback for signal %d", signo); |
---|
| 84 | |
---|
| 85 | ret = TRUE; |
---|
| 86 | |
---|
| 87 | switch (signo) { |
---|
| 88 | case SIGSEGV: |
---|
| 89 | case SIGBUS: |
---|
| 90 | case SIGILL: |
---|
| 91 | case SIGABRT: |
---|
| 92 | g_debug ("Caught signal %d.", signo); |
---|
| 93 | |
---|
| 94 | ret = FALSE; |
---|
| 95 | break; |
---|
| 96 | |
---|
| 97 | case SIGFPE: |
---|
| 98 | case SIGPIPE: |
---|
| 99 | /* let the fatal signals interrupt us */ |
---|
| 100 | g_debug ("Caught signal %d, shutting down abnormally.", signo); |
---|
| 101 | ret = FALSE; |
---|
| 102 | |
---|
| 103 | break; |
---|
| 104 | |
---|
| 105 | case SIGINT: |
---|
| 106 | case SIGTERM: |
---|
| 107 | /* let the fatal signals interrupt us */ |
---|
| 108 | g_debug ("Caught signal %d, shutting down normally.", signo); |
---|
| 109 | ret = FALSE; |
---|
| 110 | |
---|
| 111 | break; |
---|
| 112 | |
---|
| 113 | case SIGHUP: |
---|
| 114 | g_debug ("Got HUP signal"); |
---|
| 115 | /* FIXME: |
---|
| 116 | * Reread config stuff like system config files, VPN service files, etc |
---|
| 117 | */ |
---|
| 118 | ret = TRUE; |
---|
| 119 | |
---|
| 120 | break; |
---|
| 121 | |
---|
| 122 | case SIGUSR1: |
---|
| 123 | g_debug ("Got USR1 signal"); |
---|
| 124 | /* we get this from xorg - can't use for anything else */ |
---|
| 125 | ret = TRUE; |
---|
| 126 | |
---|
| 127 | break; |
---|
| 128 | |
---|
| 129 | case SIGUSR2: |
---|
| 130 | g_debug ("Got USR2 signal"); |
---|
| 131 | ret = TRUE; |
---|
| 132 | |
---|
| 133 | gdm_log_toggle_debug (); |
---|
| 134 | |
---|
| 135 | break; |
---|
| 136 | |
---|
| 137 | default: |
---|
| 138 | g_debug ("Caught unhandled signal %d", signo); |
---|
| 139 | ret = TRUE; |
---|
| 140 | |
---|
| 141 | break; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | return ret; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | static void |
---|
| 148 | on_slave_stopped (GdmSlave *slave, |
---|
| 149 | GMainLoop *main_loop) |
---|
| 150 | { |
---|
| 151 | g_debug ("slave finished"); |
---|
| 152 | gdm_return_code = 0; |
---|
| 153 | g_main_loop_quit (main_loop); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | static gboolean |
---|
| 157 | is_debug_set (gboolean arg) |
---|
| 158 | { |
---|
| 159 | /* enable debugging for unstable builds */ |
---|
| 160 | if (gdm_is_version_unstable ()) { |
---|
| 161 | return TRUE; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | return arg; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | int |
---|
| 168 | main (int argc, |
---|
| 169 | char **argv) |
---|
| 170 | { |
---|
| 171 | GMainLoop *main_loop; |
---|
| 172 | GOptionContext *context; |
---|
| 173 | DBusGConnection *connection; |
---|
| 174 | GdmSlave *slave; |
---|
| 175 | static char *display_id = NULL; |
---|
| 176 | static gboolean debug = FALSE; |
---|
| 177 | GdmSignalHandler *signal_handler; |
---|
| 178 | static GOptionEntry entries [] = { |
---|
| 179 | { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging code"), NULL }, |
---|
| 180 | { "display-id", 0, 0, G_OPTION_ARG_STRING, &display_id, N_("Display ID"), N_("id") }, |
---|
| 181 | { NULL } |
---|
| 182 | }; |
---|
| 183 | |
---|
| 184 | bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); |
---|
| 185 | textdomain (GETTEXT_PACKAGE); |
---|
| 186 | setlocale (LC_ALL, ""); |
---|
| 187 | |
---|
| 188 | gdm_set_fatal_warnings_if_unstable (); |
---|
| 189 | |
---|
| 190 | g_type_init (); |
---|
| 191 | |
---|
| 192 | context = g_option_context_new (_("GNOME Display Manager Slave")); |
---|
| 193 | g_option_context_add_main_entries (context, entries, NULL); |
---|
| 194 | |
---|
| 195 | g_option_context_parse (context, &argc, &argv, NULL); |
---|
| 196 | g_option_context_free (context); |
---|
| 197 | |
---|
| 198 | /* For debugging */ |
---|
| 199 | /*sleep (10);*/ |
---|
| 200 | |
---|
| 201 | connection = get_system_bus (); |
---|
| 202 | if (connection == NULL) { |
---|
| 203 | goto out; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | gdm_log_init (); |
---|
| 207 | |
---|
| 208 | settings = gdm_settings_new (); |
---|
| 209 | if (settings == NULL) { |
---|
| 210 | g_warning ("Unable to initialize settings"); |
---|
| 211 | goto out; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | if (! gdm_settings_direct_init (settings, GDMCONFDIR "/gdm.schemas", "/")) { |
---|
| 215 | g_warning ("Unable to initialize settings"); |
---|
| 216 | goto out; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | gdm_log_set_debug (is_debug_set (debug)); |
---|
| 220 | |
---|
| 221 | if (display_id == NULL) { |
---|
| 222 | g_critical ("No display ID set"); |
---|
| 223 | exit (1); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | if (! gdm_settings_client_init (GDMCONFDIR "/gdm.schemas", "/")) { |
---|
| 227 | g_critical ("Unable to initialize settings client"); |
---|
| 228 | exit (1); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | main_loop = g_main_loop_new (NULL, FALSE); |
---|
| 232 | |
---|
| 233 | signal_handler = gdm_signal_handler_new (); |
---|
| 234 | gdm_signal_handler_set_fatal_func (signal_handler, |
---|
| 235 | (GDestroyNotify)g_main_loop_quit, |
---|
| 236 | main_loop); |
---|
| 237 | gdm_signal_handler_add (signal_handler, SIGTERM, signal_cb, NULL); |
---|
| 238 | gdm_signal_handler_add (signal_handler, SIGINT, signal_cb, NULL); |
---|
| 239 | gdm_signal_handler_add (signal_handler, SIGILL, signal_cb, NULL); |
---|
| 240 | gdm_signal_handler_add (signal_handler, SIGBUS, signal_cb, NULL); |
---|
| 241 | gdm_signal_handler_add (signal_handler, SIGFPE, signal_cb, NULL); |
---|
| 242 | gdm_signal_handler_add (signal_handler, SIGHUP, signal_cb, NULL); |
---|
| 243 | gdm_signal_handler_add (signal_handler, SIGSEGV, signal_cb, NULL); |
---|
| 244 | gdm_signal_handler_add (signal_handler, SIGABRT, signal_cb, NULL); |
---|
| 245 | gdm_signal_handler_add (signal_handler, SIGUSR1, signal_cb, NULL); |
---|
| 246 | gdm_signal_handler_add (signal_handler, SIGUSR2, signal_cb, NULL); |
---|
| 247 | |
---|
| 248 | slave = gdm_simple_slave_new (display_id); |
---|
| 249 | if (slave == NULL) { |
---|
| 250 | goto out; |
---|
| 251 | } |
---|
| 252 | g_signal_connect (slave, |
---|
| 253 | "stopped", |
---|
| 254 | G_CALLBACK (on_slave_stopped), |
---|
| 255 | main_loop); |
---|
| 256 | gdm_slave_start (slave); |
---|
| 257 | |
---|
| 258 | g_main_loop_run (main_loop); |
---|
| 259 | |
---|
| 260 | if (slave != NULL) { |
---|
| 261 | g_object_unref (slave); |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | if (signal_handler != NULL) { |
---|
| 265 | g_object_unref (signal_handler); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | g_main_loop_unref (main_loop); |
---|
| 269 | |
---|
| 270 | out: |
---|
| 271 | |
---|
| 272 | g_debug ("Slave finished"); |
---|
| 273 | |
---|
| 274 | return gdm_return_code; |
---|
| 275 | } |
---|