1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2006-2007 William Jon McCann <mccann@jhu.edu>
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.
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.
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.
29
#include <sys/types.h>
34
#include <glib/gi18n.h>
35
#include <glib-object.h>
37
#include <dbus/dbus-glib.h>
38
#include <dbus/dbus-glib-lowlevel.h>
40
#include "ck-sysdeps.h"
41
#include "ck-manager.h"
44
#define CK_DBUS_NAME "org.freedesktop.ConsoleKit"
47
timed_exit_cb (GMainLoop *loop)
49
g_main_loop_quit (loop);
54
get_bus_proxy (DBusGConnection *connection)
56
DBusGProxy *bus_proxy;
58
bus_proxy = dbus_g_proxy_new_for_name (connection,
66
acquire_name_on_proxy (DBusGProxy *bus_proxy)
75
if (bus_proxy == NULL) {
80
res = dbus_g_proxy_call (bus_proxy,
83
G_TYPE_STRING, CK_DBUS_NAME,
90
g_warning ("Failed to acquire %s: %s", CK_DBUS_NAME, error->message);
93
g_warning ("Failed to acquire %s", CK_DBUS_NAME);
98
if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
100
g_warning ("Failed to acquire %s: %s", CK_DBUS_NAME, error->message);
101
g_error_free (error);
103
g_warning ("Failed to acquire %s", CK_DBUS_NAME);
114
static DBusGConnection *
115
get_system_bus (void)
118
DBusGConnection *bus;
119
DBusConnection *connection;
122
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
124
g_warning ("Couldn't connect to system bus: %s",
126
g_error_free (error);
130
connection = dbus_g_connection_get_connection (bus);
131
dbus_connection_set_exit_on_disconnect (connection, FALSE);
138
bus_proxy_destroyed_cb (DBusGProxy *bus_proxy,
141
g_debug ("Disconnected from D-Bus");
142
g_main_loop_quit (loop);
148
unlink (CONSOLE_KIT_PID_FILE);
151
/* copied from nautilus */
152
static int debug_log_pipes[2];
155
debug_log_io_cb (GIOChannel *io,
156
GIOCondition condition,
161
while (read (debug_log_pipes[0], &a, 1) != 1)
164
ck_log_toggle_debug ();
170
sigusr1_handler (int sig)
172
while (write (debug_log_pipes[1], "a", 1) != 1)
177
setup_debug_log_signals (void)
182
if (pipe (debug_log_pipes) == -1) {
183
g_error ("Could not create pipe() for debug log");
186
io = g_io_channel_unix_new (debug_log_pipes[0]);
187
g_io_add_watch (io, G_IO_IN, debug_log_io_cb, NULL);
189
sa.sa_handler = sigusr1_handler;
190
sigemptyset (&sa.sa_mask);
192
sigaction (SIGUSR1, &sa, NULL);
196
setup_debug_log (gboolean debug)
199
ck_log_set_debug (debug);
200
setup_debug_log_signals ();
204
create_pid_file (void)
212
/* remove old pid file */
213
unlink (CONSOLE_KIT_PID_FILE);
215
dirname = g_path_get_dirname (CONSOLE_KIT_PID_FILE);
217
res = g_mkdir_with_parents (dirname,
218
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
220
g_warning ("Unable to create directory %s (%s)",
226
/* make a new pid file */
227
if ((pf = open (CONSOLE_KIT_PID_FILE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644)) > 0) {
228
snprintf (pid, sizeof (pid), "%lu\n", (long unsigned) getpid ());
229
written = write (pf, pid, strlen (pid));
231
g_atexit (delete_pid);
233
g_warning ("Unable to write pid file %s: %s",
234
CONSOLE_KIT_PID_FILE,
245
GOptionContext *context;
246
DBusGProxy *bus_proxy;
247
DBusGConnection *connection;
251
static gboolean debug = FALSE;
252
static gboolean no_daemon = FALSE;
253
static gboolean do_timed_exit = FALSE;
254
static GOptionEntry entries [] = {
255
{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging code"), NULL },
256
{ "no-daemon", 0, 0, G_OPTION_ARG_NONE, &no_daemon, N_("Don't become a daemon"), NULL },
257
{ "timed-exit", 0, 0, G_OPTION_ARG_NONE, &do_timed_exit, N_("Exit after a time - for debugging"), NULL },
263
if (! g_thread_supported ()) {
264
g_thread_init (NULL);
266
dbus_g_thread_init ();
269
if (! ck_is_root_user ()) {
270
g_warning ("Must be run as root");
275
g_setenv ("G_DEBUG", "fatal_criticals", FALSE);
276
g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
279
context = g_option_context_new (_("Console kit daemon"));
280
g_option_context_add_main_entries (context, entries, NULL);
282
res = g_option_context_parse (context, &argc, &argv, &error);
283
g_option_context_free (context);
285
g_warning ("%s", error->message);
286
g_error_free (error);
290
if (! no_daemon && daemon (0, 0)) {
291
g_error ("Could not daemonize: %s", g_strerror (errno));
294
setup_debug_log (debug);
296
g_debug ("initializing console-kit-daemon %s", VERSION);
298
connection = get_system_bus ();
299
if (connection == NULL) {
303
manager = ck_manager_new ();
305
if (manager == NULL) {
309
bus_proxy = get_bus_proxy (connection);
310
if (bus_proxy == NULL) {
311
g_warning ("Could not construct bus_proxy object; bailing out");
315
if (! acquire_name_on_proxy (bus_proxy) ) {
316
g_warning ("Could not acquire name; bailing out");
322
loop = g_main_loop_new (NULL, FALSE);
324
g_signal_connect (bus_proxy,
326
G_CALLBACK (bus_proxy_destroyed_cb),
330
g_timeout_add (1000 * 30, (GSourceFunc) timed_exit_cb, loop);
333
g_main_loop_run (loop);
335
if (manager != NULL) {
336
g_object_unref (manager);
339
g_main_loop_unref (loop);