~behda/+junk/udisks2.original

« back to all changes in this revision

Viewing changes to src/main.c

  • Committer: behda
  • Date: 2014-05-24 15:15:11 UTC
  • Revision ID: pauvitk@gmail.com-20140524151511-3vtr0uubjewx3z2j
Initial commit of source code and Debian packaging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
 
2
 *
 
3
 * Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
 
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
#include <glib/gi18n.h>
 
23
 
 
24
#include <gio/gio.h>
 
25
#include <glib-unix.h>
 
26
 
 
27
#include "udiskslogging.h"
 
28
#include "udisksdaemontypes.h"
 
29
#include "udisksdaemon.h"
 
30
 
 
31
/* ---------------------------------------------------------------------------------------------------- */
 
32
 
 
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[] =
 
38
{
 
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},
 
42
  {NULL }
 
43
};
 
44
 
 
45
static UDisksDaemon *the_daemon = NULL;
 
46
 
 
47
static void
 
48
on_bus_acquired (GDBusConnection *connection,
 
49
                 const gchar     *name,
 
50
                 gpointer         user_data)
 
51
{
 
52
  the_daemon = udisks_daemon_new (connection);
 
53
  udisks_debug ("Connected to the system bus");
 
54
}
 
55
 
 
56
static void
 
57
on_name_lost (GDBusConnection *connection,
 
58
              const gchar     *name,
 
59
              gpointer         user_data)
 
60
{
 
61
  if (the_daemon == NULL)
 
62
    {
 
63
      udisks_error ("Failed to connect to the system message bus");
 
64
    }
 
65
  else
 
66
    {
 
67
      udisks_info ("Lost (or failed to acquire) the name %s on the system message bus", name);
 
68
    }
 
69
  g_main_loop_quit (loop);
 
70
}
 
71
 
 
72
static void
 
73
on_name_acquired (GDBusConnection *connection,
 
74
                  const gchar     *name,
 
75
                  gpointer         user_data)
 
76
{
 
77
  udisks_notice ("Acquired the name %s on the system message bus", name);
 
78
}
 
79
 
 
80
static gboolean
 
81
on_sigint (gpointer user_data)
 
82
{
 
83
  udisks_info ("Caught SIGINT. Initiating shutdown");
 
84
  g_main_loop_quit (loop);
 
85
  return FALSE;
 
86
}
 
87
 
 
88
int
 
89
main (int    argc,
 
90
      char **argv)
 
91
{
 
92
  GError *error;
 
93
  GOptionContext *opt_context;
 
94
  gint ret;
 
95
  guint name_owner_id;
 
96
  guint sigint_id;
 
97
 
 
98
  ret = 1;
 
99
  loop = NULL;
 
100
  opt_context = NULL;
 
101
  name_owner_id = 0;
 
102
  sigint_id = 0;
 
103
 
 
104
  g_type_init ();
 
105
 
 
106
  /* avoid gvfs (http://bugzilla.gnome.org/show_bug.cgi?id=526454) */
 
107
  if (!g_setenv ("GIO_USE_VFS", "local", TRUE))
 
108
    {
 
109
      g_printerr ("Error setting GIO_USE_GVFS\n");
 
110
      goto out;
 
111
    }
 
112
 
 
113
  opt_context = g_option_context_new ("udisks storage daemon");
 
114
  g_option_context_add_main_entries (opt_context, opt_entries, NULL);
 
115
  error = NULL;
 
116
  if (!g_option_context_parse (opt_context, &argc, &argv, &error))
 
117
    {
 
118
      g_printerr ("Error parsing options: %s\n", error->message);
 
119
      g_error_free (error);
 
120
      goto out;
 
121
    }
 
122
 
 
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
 
125
   */
 
126
  if (opt_no_debug)
 
127
    {
 
128
      gint dev_null_fd;
 
129
      dev_null_fd = open ("/dev/null", O_RDWR);
 
130
      if (dev_null_fd >= 0)
 
131
        {
 
132
          dup2 (dev_null_fd, STDIN_FILENO);
 
133
          dup2 (dev_null_fd, STDOUT_FILENO);
 
134
          dup2 (dev_null_fd, STDERR_FILENO);
 
135
          close (dev_null_fd);
 
136
        }
 
137
      else
 
138
        {
 
139
          udisks_warning ("Error opening /dev/null: %m");
 
140
        }
 
141
    }
 
142
 
 
143
  if (g_getenv ("PATH") == NULL)
 
144
    g_setenv ("PATH", "/usr/bin:/bin:/usr/sbin:/sbin", TRUE);
 
145
 
 
146
  udisks_notice ("udisks daemon version %s starting", PACKAGE_VERSION);
 
147
 
 
148
  loop = g_main_loop_new (NULL, FALSE);
 
149
 
 
150
  sigint_id = 0;
 
151
  if (!opt_no_sigint)
 
152
    {
 
153
      sigint_id = g_unix_signal_add_full (G_PRIORITY_DEFAULT,
 
154
                                          SIGINT,
 
155
                                          on_sigint,
 
156
                                          NULL,  /* user_data */
 
157
                                          NULL); /* GDestroyNotify */
 
158
    }
 
159
 
 
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),
 
164
                                  on_bus_acquired,
 
165
                                  on_name_acquired,
 
166
                                  on_name_lost,
 
167
                                  NULL,
 
168
                                  NULL);
 
169
 
 
170
 
 
171
  udisks_debug ("Entering main event loop");
 
172
 
 
173
  g_main_loop_run (loop);
 
174
 
 
175
  ret = 0;
 
176
 
 
177
 out:
 
178
  if (sigint_id > 0)
 
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);
 
184
  if (loop != NULL)
 
185
    g_main_loop_unref (loop);
 
186
  if (opt_context != NULL)
 
187
    g_option_context_free (opt_context);
 
188
 
 
189
  udisks_notice ("udisks daemon version %s exiting", PACKAGE_VERSION);
 
190
 
 
191
  return ret;
 
192
}