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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2007 David Zeuthen <davidz@redhat.com>
* Copyright (C) 2008-2009 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <gio/gio.h>
#include <glib.h>
#include <glib-unix.h>
#include <glib/gi18n-lib.h>
#include <glib-object.h>
#include <locale.h>
#include "up-daemon.h"
#include "up-kbd-backlight.h"
#include "up-wakeups.h"
#define DEVKIT_POWER_SERVICE_NAME "org.freedesktop.UPower"
typedef struct UpState {
UpKbdBacklight *kbd_backlight;
UpWakeups *wakeups;
UpDaemon *daemon;
GMainLoop *loop;
} UpState;
static void
up_state_free (UpState *state)
{
up_daemon_shutdown (state->daemon);
g_clear_object (&state->kbd_backlight);
g_clear_object (&state->wakeups);
g_clear_object (&state->daemon);
g_clear_pointer (&state->loop, g_main_loop_unref);
g_free (state);
}
static UpState *
up_state_new (void)
{
UpState *state = g_new0 (UpState, 1);
state->kbd_backlight = up_kbd_backlight_new ();
state->wakeups = up_wakeups_new ();
state->daemon = up_daemon_new ();
state->loop = g_main_loop_new (NULL, FALSE);
return state;
}
/**
* up_main_bus_acquired:
**/
static void
up_main_bus_acquired (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
UpState *state = user_data;
up_kbd_backlight_register (state->kbd_backlight, connection);
up_wakeups_register (state->wakeups, connection);
if (!up_daemon_startup (state->daemon, connection)) {
g_warning ("Could not startup; bailing out");
g_main_loop_quit (state->loop);
}
}
/**
* up_main_name_lost:
**/
static void
up_main_name_lost (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
UpState *state = user_data;
g_debug ("name lost, exiting");
g_main_loop_quit (state->loop);
}
/**
* up_main_sigint_cb:
**/
static gboolean
up_main_sigint_cb (gpointer user_data)
{
UpState *state = user_data;
g_debug ("Handling SIGINT");
g_main_loop_quit (state->loop);
return FALSE;
}
/**
* up_main_timed_exit_cb:
*
* Exits the main loop, which is helpful for valgrinding.
**/
static gboolean
up_main_timed_exit_cb (UpState *state)
{
g_main_loop_quit (state->loop);
return FALSE;
}
/**
* up_main_log_ignore_cb:
**/
static void
up_main_log_ignore_cb (const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer user_data)
{
}
/**
* up_main_log_handler_cb:
**/
static void
up_main_log_handler_cb (const gchar *log_domain, GLogLevelFlags log_level,
const gchar *message, gpointer user_data)
{
gchar str_time[255];
time_t the_time;
/* header always in green */
time (&the_time);
strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));
g_print ("%c[%dmTI:%s\t", 0x1B, 32, str_time);
/* critical is also in red */
if (log_level == G_LOG_LEVEL_CRITICAL ||
log_level == G_LOG_LEVEL_WARNING ||
log_level == G_LOG_LEVEL_ERROR) {
g_print ("%c[%dm%s\n%c[%dm", 0x1B, 31, message, 0x1B, 0);
} else {
/* debug in blue */
g_print ("%c[%dm%s\n%c[%dm", 0x1B, 34, message, 0x1B, 0);
}
}
/**
* main:
**/
gint
main (gint argc, gchar **argv)
{
GError *error = NULL;
GOptionContext *context;
gboolean timed_exit = FALSE;
gboolean immediate_exit = FALSE;
guint timer_id = 0;
gboolean verbose = FALSE;
UpState *state;
GBusNameOwnerFlags bus_flags;
gboolean replace = FALSE;
const GOptionEntry options[] = {
{ "timed-exit", '\0', 0, G_OPTION_ARG_NONE, &timed_exit,
/* TRANSLATORS: exit after we've started up, used for user profiling */
_("Exit after a small delay"), NULL },
{ "immediate-exit", '\0', 0, G_OPTION_ARG_NONE, &immediate_exit,
/* TRANSLATORS: exit straight away, used for automatic profiling */
_("Exit after the engine has loaded"), NULL },
{ "replace", 'r', 0, G_OPTION_ARG_NONE, &replace,
_("Replace the old daemon"), NULL},
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
_("Show extra debugging information"), NULL },
{ NULL}
};
#if !defined(GLIB_VERSION_2_36)
g_type_init ();
#endif
setlocale(LC_ALL, "");
context = g_option_context_new ("upower daemon");
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error)) {
g_warning ("Failed to parse command-line options: %s", error->message);
g_error_free (error);
return 1;
}
g_option_context_free (context);
/* verbose? */
if (verbose) {
g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR |
G_LOG_LEVEL_CRITICAL);
g_log_set_handler (G_LOG_DOMAIN,
G_LOG_LEVEL_ERROR |
G_LOG_LEVEL_CRITICAL |
G_LOG_LEVEL_DEBUG |
G_LOG_LEVEL_WARNING,
up_main_log_handler_cb, NULL);
g_log_set_handler ("UPower-Linux",
G_LOG_LEVEL_ERROR |
G_LOG_LEVEL_CRITICAL |
G_LOG_LEVEL_DEBUG |
G_LOG_LEVEL_WARNING,
up_main_log_handler_cb, NULL);
} else {
/* hide all debugging */
g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR);
g_log_set_handler (G_LOG_DOMAIN,
G_LOG_LEVEL_DEBUG,
up_main_log_ignore_cb,
NULL);
g_log_set_handler ("UPower-Linux",
G_LOG_LEVEL_DEBUG,
up_main_log_ignore_cb,
NULL);
}
/* initialize state */
state = up_state_new ();
/* do stuff on ctrl-c */
g_unix_signal_add_full (G_PRIORITY_DEFAULT,
SIGINT,
up_main_sigint_cb,
state,
NULL);
/* acquire name */
bus_flags = G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT;
if (replace)
bus_flags |= G_BUS_NAME_OWNER_FLAGS_REPLACE;
g_bus_own_name (G_BUS_TYPE_SYSTEM,
DEVKIT_POWER_SERVICE_NAME,
bus_flags,
up_main_bus_acquired,
NULL,
up_main_name_lost,
state, NULL);
g_debug ("Starting upowerd version %s", PACKAGE_VERSION);
/* only timeout and close the mainloop if we have specified it on the command line */
if (timed_exit) {
timer_id = g_timeout_add_seconds (30, (GSourceFunc) up_main_timed_exit_cb, state);
g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb");
}
/* immediatly exit */
if (immediate_exit) {
g_timeout_add (50, (GSourceFunc) up_main_timed_exit_cb, state);
g_source_set_name_by_id (timer_id, "[upower] up_main_timed_exit_cb");
}
/* wait for input or timeout */
g_main_loop_run (state->loop);
up_state_free (state);
return 0;
}
|