1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* Copyright (C) 2006 William Jon McCann <mccann@jhu.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib-object.h>
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "ck-manager.h"
#include "ck-debug.h"
#define CK_DBUS_NAME "org.freedesktop.ConsoleKit"
static void bus_proxy_destroyed_cb (DBusGProxy *bus_proxy,
CkManager *manager);
static gboolean
timed_exit_cb (GMainLoop *loop)
{
g_main_loop_quit (loop);
return FALSE;
}
static DBusGProxy *
get_bus_proxy (DBusGConnection *connection)
{
DBusGProxy *bus_proxy;
GError *error;
guint result;
gboolean res;
bus_proxy = dbus_g_proxy_new_for_name (connection,
DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
DBUS_INTERFACE_DBUS);
if (bus_proxy == NULL) {
goto out;
}
error = NULL;
res = dbus_g_proxy_call (bus_proxy,
"RequestName",
&error,
G_TYPE_STRING, CK_DBUS_NAME,
G_TYPE_UINT, 0,
G_TYPE_INVALID,
G_TYPE_UINT, &result,
G_TYPE_INVALID);
if (! res) {
if (error != NULL) {
g_warning ("Failed to acquire %s: %s", CK_DBUS_NAME, error->message);
g_error_free (error);
} else {
g_warning ("Failed to acquire %s", CK_DBUS_NAME);
}
goto out;
}
if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
if (error != NULL) {
g_warning ("Failed to acquire %s: %s", CK_DBUS_NAME, error->message);
g_error_free (error);
} else {
g_warning ("Failed to acquire %s", CK_DBUS_NAME);
}
goto out;
}
out:
return bus_proxy;
}
static DBusGConnection *
get_system_bus (void)
{
GError *error;
DBusGConnection *bus;
DBusConnection *connection;
error = NULL;
bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
if (bus == NULL) {
g_warning ("Couldn't connect to system bus: %s",
error->message);
g_error_free (error);
goto out;
}
connection = dbus_g_connection_get_connection (bus);
dbus_connection_set_exit_on_disconnect (connection, FALSE);
out:
return bus;
}
static gboolean
bus_reconnect (CkManager *manager)
{
DBusGConnection *bus;
DBusGProxy *bus_proxy;
gboolean ret;
ret = TRUE;
bus = get_system_bus ();
if (bus == NULL) {
goto out;
}
bus_proxy = get_bus_proxy (bus);
if (bus_proxy == NULL) {
g_warning ("Could not construct bus_proxy object; will retry");
goto out;
}
manager = ck_manager_new ();
if (manager == NULL) {
g_warning ("Could not construct manager object; will retry");
exit (1);
}
g_signal_connect (bus_proxy,
"destroy",
G_CALLBACK (bus_proxy_destroyed_cb),
manager);
ck_debug ("Successfully reconnected to D-Bus");
ret = FALSE;
out:
return ret;
}
static void
bus_proxy_destroyed_cb (DBusGProxy *bus_proxy,
CkManager *manager)
{
ck_debug ("Disconnected from D-Bus");
g_object_unref (manager);
manager = NULL;
g_timeout_add (3000, (GSourceFunc)bus_reconnect, manager);
}
int
main (int argc,
char **argv)
{
GMainLoop *loop;
CkManager *manager;
GOptionContext *context;
DBusGProxy *bus_proxy;
DBusGConnection *connection;
int ret;
static gboolean debug = FALSE;
static gboolean no_daemon = FALSE;
static gboolean do_timed_exit = FALSE;
static GOptionEntry entries [] = {
{ "debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Enable debugging code"), NULL },
{ "no-daemon", 0, 0, G_OPTION_ARG_NONE, &no_daemon, N_("Don't become a daemon"), NULL },
{ "timed-exit", 0, 0, G_OPTION_ARG_NONE, &do_timed_exit, N_("Exit after a time - for debugging"), NULL },
{ NULL }
};
ret = 1;
g_type_init ();
g_thread_init (NULL);
dbus_g_thread_init ();
context = g_option_context_new (_("Console kit daemon"));
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_parse (context, &argc, &argv, NULL);
g_option_context_free (context);
connection = get_system_bus ();
if (connection == NULL) {
goto out;
}
bus_proxy = get_bus_proxy (connection);
if (bus_proxy == NULL) {
g_warning ("Could not construct bus_proxy object; bailing out");
goto out;
}
/* debug to a file if in deamon mode */
ck_debug_init (debug, ! no_daemon);
ck_debug ("initializing console-kit-daemon %s", VERSION);
/* Don't close stdout and stderr for now */
if (! no_daemon && daemon (0, 1)) {
g_error ("Could not daemonize: %s", g_strerror (errno));
}
manager = ck_manager_new ();
if (manager == NULL) {
goto out;
}
g_signal_connect (bus_proxy,
"destroy",
G_CALLBACK (bus_proxy_destroyed_cb),
manager);
loop = g_main_loop_new (NULL, FALSE);
if (do_timed_exit) {
g_timeout_add (3000 * 60, (GSourceFunc) timed_exit_cb, loop);
}
g_main_loop_run (loop);
g_object_unref (manager);
ret = 0;
out:
return ret;
}
|