1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
3
* Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <glib/gi18n.h>
25
#include <glib-unix.h>
27
#include "udiskslogging.h"
28
#include "udisksdaemontypes.h"
29
#include "udisksdaemon.h"
31
/* ---------------------------------------------------------------------------------------------------- */
33
static GMainLoop *loop = NULL;
34
static gboolean opt_replace = FALSE;
35
static gboolean opt_no_debug = FALSE;
36
static gboolean opt_no_sigint = FALSE;
37
static GOptionEntry opt_entries[] =
39
{"replace", 'r', 0, G_OPTION_ARG_NONE, &opt_replace, "Replace existing daemon", NULL},
40
{"no-debug", 'n', 0, G_OPTION_ARG_NONE, &opt_no_debug, "Don't print debug information on stdout/stderr", NULL},
41
{"no-sigint", 's', 0, G_OPTION_ARG_NONE, &opt_no_sigint, "Do not handle SIGINT for controlled shutdown", NULL},
45
static UDisksDaemon *the_daemon = NULL;
48
on_bus_acquired (GDBusConnection *connection,
52
the_daemon = udisks_daemon_new (connection);
53
udisks_debug ("Connected to the system bus");
57
on_name_lost (GDBusConnection *connection,
61
if (the_daemon == NULL)
63
udisks_error ("Failed to connect to the system message bus");
67
udisks_info ("Lost (or failed to acquire) the name %s on the system message bus", name);
69
g_main_loop_quit (loop);
73
on_name_acquired (GDBusConnection *connection,
77
udisks_notice ("Acquired the name %s on the system message bus", name);
81
on_sigint (gpointer user_data)
83
udisks_info ("Caught SIGINT. Initiating shutdown");
84
g_main_loop_quit (loop);
93
GOptionContext *opt_context;
106
/* avoid gvfs (http://bugzilla.gnome.org/show_bug.cgi?id=526454) */
107
if (!g_setenv ("GIO_USE_VFS", "local", TRUE))
109
g_printerr ("Error setting GIO_USE_GVFS\n");
113
opt_context = g_option_context_new ("udisks storage daemon");
114
g_option_context_add_main_entries (opt_context, opt_entries, NULL);
116
if (!g_option_context_parse (opt_context, &argc, &argv, &error))
118
g_printerr ("Error parsing options: %s\n", error->message);
119
g_error_free (error);
123
/* TODO: this hammer is too big - it would be a lot better to configure the
124
* logging routines and avoid printf(3) overhead and so on
129
dev_null_fd = open ("/dev/null", O_RDWR);
130
if (dev_null_fd >= 0)
132
dup2 (dev_null_fd, STDIN_FILENO);
133
dup2 (dev_null_fd, STDOUT_FILENO);
134
dup2 (dev_null_fd, STDERR_FILENO);
139
udisks_warning ("Error opening /dev/null: %m");
143
if (g_getenv ("PATH") == NULL)
144
g_setenv ("PATH", "/usr/bin:/bin:/usr/sbin:/sbin", TRUE);
146
udisks_notice ("udisks daemon version %s starting", PACKAGE_VERSION);
148
loop = g_main_loop_new (NULL, FALSE);
153
sigint_id = g_unix_signal_add_full (G_PRIORITY_DEFAULT,
156
NULL, /* user_data */
157
NULL); /* GDestroyNotify */
160
name_owner_id = g_bus_own_name (G_BUS_TYPE_SYSTEM,
161
"org.freedesktop.UDisks2",
162
G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
163
(opt_replace ? G_BUS_NAME_OWNER_FLAGS_REPLACE : 0),
171
udisks_debug ("Entering main event loop");
173
g_main_loop_run (loop);
179
g_source_remove (sigint_id);
180
if (the_daemon != NULL)
181
g_object_unref (the_daemon);
182
if (name_owner_id != 0)
183
g_bus_unown_name (name_owner_id);
185
g_main_loop_unref (loop);
186
if (opt_context != NULL)
187
g_option_context_free (opt_context);
189
udisks_notice ("udisks daemon version %s exiting", PACKAGE_VERSION);