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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/***************************************************************************
* CVSID: $Id$
*
* hald.c : main startup for HAL daemon
*
* Copyright (C) 2003 David Zeuthen, <david@fubar.dk>
* Copyright (C) 2005 Danny Kukawka, <danny.kukawka@web.de>
*
* Licensed under the Academic Free License version 2.1
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <pwd.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <grp.h>
#include <syslog.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
/*#include "master_slave.h"*/
#include "logger.h"
#include "hald.h"
#include "device_store.h"
#include "device_info.h"
#include "osspec.h"
#include "hald_dbus.h"
#include "util.h"
#include "hald_runner.h"
#include "util_helper.h"
static void delete_pid(void)
{
unlink(HALD_PID_FILE);
}
/**
* @defgroup HalDaemon HAL daemon
* @brief The HAL daemon manages persistent device objects available through
* a D-BUS network API
*/
static HalDeviceStore *global_device_list = NULL;
static HalDeviceStore *temporary_device_list = NULL;
static void
gdl_store_changed (HalDeviceStore *store, HalDevice *device,
gboolean is_added, gpointer user_data)
{
if (is_added) {
GSList *addons;
HAL_INFO (("Added device to GDL; udi=%s", hal_device_get_udi(device)));
if ((addons = hal_device_property_get_strlist (device, "info.addons")) != NULL) {
GSList *i;
for (i = addons; i != NULL; i = g_slist_next (i)) {
const gchar *command_line;
gchar *extra_env[2] = {"HALD_ACTION=addon", NULL};
command_line = (const gchar *) i->data;
hald_runner_start (device, command_line, extra_env);
HAL_INFO (("Started addon %s for udi %s",
command_line, hal_device_get_udi(device)));
}
}
} else {
HAL_INFO (("Removed device from GDL; udi=%s", hal_device_get_udi(device)));
hald_runner_kill_device(device);
}
/*hal_device_print (device);*/
if (is_added)
manager_send_signal_device_added (device);
else
manager_send_signal_device_removed (device);
}
static void
gdl_property_changed (HalDeviceStore *store, HalDevice *device,
const char *key, gboolean added, gboolean removed,
gpointer user_data)
{
device_send_signal_property_modified (device, key, removed, added);
/* only execute the callouts if the property _changed_ */
if (added == FALSE && removed == FALSE)
/*hal_callout_property (device, key)*/;
}
static void
gdl_capability_added (HalDeviceStore *store, HalDevice *device,
const char *capability, gpointer user_data)
{
manager_send_signal_new_capability (device, capability);
/*hal_callout_capability (device, capability, TRUE)*/;
}
HalDeviceStore *
hald_get_gdl (void)
{
if (global_device_list == NULL) {
global_device_list = hal_device_store_new ();
g_signal_connect (global_device_list,
"store_changed",
G_CALLBACK (gdl_store_changed), NULL);
g_signal_connect (global_device_list,
"device_property_changed",
G_CALLBACK (gdl_property_changed), NULL);
g_signal_connect (global_device_list,
"device_capability_added",
G_CALLBACK (gdl_capability_added), NULL);
}
return global_device_list;
}
HalDeviceStore *
hald_get_tdl (void)
{
if (temporary_device_list == NULL) {
temporary_device_list = hal_device_store_new ();
}
return temporary_device_list;
}
/**
* @defgroup MainDaemon Basic functions
* @ingroup HalDaemon
* @brief Basic functions in the HAL daemon
* @{
*/
/** Print out program usage.
*
*/
static void
usage ()
{
fprintf (stderr, "\n" "usage : hald [--daemon=yes|no] [--verbose=yes|no] [--help]\n");
fprintf (stderr,
"\n"
" --daemon=yes|no Become a daemon\n"
" --verbose=yes|no Print out debug (overrides HALD_VERBOSE)\n"
" --use-syslog Print out debug messages to syslog instead of stderr.\n"
" Use this option to get debug messages if HAL runs as\n"
" daemon.\n"
" --help Show this information and exit\n"
" --version Output version information and exit"
"\n"
"The HAL daemon detects devices present in the system and provides the\n"
"org.freedesktop.Hal service through the system-wide message bus provided\n"
"by D-BUS.\n"
"\n"
"For more information visit http://freedesktop.org/Software/hal\n"
"\n");
}
/** If #TRUE, we will daemonize */
static dbus_bool_t opt_become_daemon = TRUE;
/** If #TRUE, we will spew out debug */
dbus_bool_t hald_is_verbose = FALSE;
dbus_bool_t hald_use_syslog = FALSE;
static int sigterm_unix_signal_pipe_fds[2];
static GIOChannel *sigterm_iochn;
static void
handle_sigterm (int value)
{
ssize_t written;
static char marker[1] = {'S'};
/* write a 'S' character to the other end to tell about
* the signal. Note that 'the other end' is a GIOChannel thingy
* that is only called from the mainloop - thus this is how we
* defer this since UNIX signal handlers are evil
*
* Oh, and write(2) is indeed reentrant */
written = write (sigterm_unix_signal_pipe_fds[1], marker, 1);
}
static gboolean
sigterm_iochn_data (GIOChannel *source,
GIOCondition condition,
gpointer user_data)
{
GError *err = NULL;
gchar data[1];
gsize bytes_read;
/* Empty the pipe */
if (G_IO_STATUS_NORMAL !=
g_io_channel_read_chars (source, data, 1, &bytes_read, &err)) {
HAL_ERROR (("Error emptying sigterm pipe: %s",
err->message));
g_error_free (err);
goto out;
}
HAL_INFO (("Caught SIGTERM, initiating shutdown"));
hald_runner_kill_all();
exit (0);
out:
return TRUE;
}
/** This is set to #TRUE if we are probing and #FALSE otherwise */
dbus_bool_t hald_is_initialising;
static int startup_daemonize_pipe[2];
/*--------------------------------------------------------------------------------------------------*/
static gboolean child_died = FALSE;
static void
handle_sigchld (int value)
{
child_died = TRUE;
}
static int
parent_wait_for_child (int child_fd, pid_t child_pid)
{
fd_set rfds;
fd_set efds;
struct timeval tv;
int retval;
int ret;
signal(SIGCHLD, handle_sigchld);
/* wait for either
*
* o Child writes something to the child_fd; means that device
* probing is completed and the parent should exit with success
*
* o Child is killed (segfault etc.); means that parent should exit
* with failure
*
* o Timeout; means that we should kill the child and exit with
* failure
*
*/
FD_ZERO(&rfds);
FD_SET(child_fd, &rfds);
FD_ZERO(&efds);
FD_SET(child_fd, &efds);
/* Wait up to 250 seconds for device probing */
tv.tv_sec = 250;
tv.tv_usec = 0;
retval = select (child_fd + 1, &rfds, NULL, &efds, &tv);
if (child_died) {
/* written from handle_sigchld */
ret = 1;
goto out;
}
if (retval > 0) {
/* means child wrote to socket or closed it; all good */
ret = 0;
goto out;
}
/* assume timeout; kill child */
kill (child_pid, SIGTERM);
ret = 2;
out:
return ret;
}
/*--------------------------------------------------------------------------------------------------*/
/** Entry point for HAL daemon
*
* @param argc Number of arguments
* @param argv Array of arguments
* @return Exit code
*/
int
main (int argc, char *argv[])
{
GMainLoop *loop;
guint sigterm_iochn_listener_source_id;
char *path;
char newpath[512];
openlog ("hald", LOG_PID, LOG_DAEMON);
g_type_init ();
if (getenv ("HALD_VERBOSE"))
hald_is_verbose = TRUE;
else
hald_is_verbose = FALSE;
/* our helpers are installed into libexec, so adjust out $PATH
* to include this at the end (since we want to overide in
* run-hald.sh and friends)
*/
path = getenv ("PATH");
if (path != NULL) {
g_strlcpy (newpath, path, sizeof (newpath));
g_strlcat (newpath, ":", sizeof (newpath));
} else {
/* No PATH was set */
newpath[0] = '\0';
}
g_strlcat (newpath, PACKAGE_LIBEXEC_DIR, sizeof (newpath));
g_strlcat (newpath, ":", sizeof (newpath));
g_strlcat (newpath, PACKAGE_SCRIPT_DIR, sizeof (newpath));
setenv ("PATH", newpath, TRUE);
while (1) {
int c;
int option_index = 0;
const char *opt;
static struct option long_options[] = {
{"daemon", 1, NULL, 0},
{"verbose", 1, NULL, 0},
{"use-syslog", 0, NULL, 0},
{"help", 0, NULL, 0},
{"version", 0, NULL, 0},
{NULL, 0, NULL, 0}
};
c = getopt_long (argc, argv, "",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
opt = long_options[option_index].name;
if (strcmp (opt, "help") == 0) {
usage ();
return 0;
} else if (strcmp (opt, "version") == 0) {
fprintf (stderr, "HAL package version: " PACKAGE_VERSION "\n");
return 0;
} else if (strcmp (opt, "daemon") == 0) {
if (strcmp ("yes", optarg) == 0) {
opt_become_daemon = TRUE;
} else if (strcmp ("no", optarg) == 0) {
opt_become_daemon = FALSE;
} else {
usage ();
return 1;
}
} else if (strcmp (opt, "verbose") == 0) {
if (strcmp ("yes", optarg) == 0) {
hald_is_verbose = TRUE;
} else if (strcmp ("no", optarg) == 0) {
hald_is_verbose = FALSE;
} else {
usage ();
return 1;
}
} else if (strcmp (opt, "use-syslog") == 0) {
hald_use_syslog = TRUE;
}
break;
default:
usage ();
return 1;
break;
}
}
if (hald_is_verbose)
logger_enable ();
else
logger_disable ();
if (hald_use_syslog)
logger_enable_syslog ();
else
logger_disable_syslog ();
/* will fork into two; only the child will return here if we are successful */
/*master_slave_setup ();
sleep (100000000);*/
loop = g_main_loop_new (NULL, FALSE);
HAL_INFO ((PACKAGE_STRING));
if (opt_become_daemon) {
int child_pid;
int dev_null_fd;
int pf;
ssize_t written;
char pid[9];
HAL_INFO (("Will daemonize"));
HAL_INFO (("Becoming a daemon"));
if (pipe (startup_daemonize_pipe) != 0) {
fprintf (stderr, "Could not setup pipe: %s\n", strerror(errno));
exit (1);
}
if (chdir ("/") < 0) {
fprintf (stderr, "Could not chdir to /: %s\n", strerror(errno));
exit (1);
}
child_pid = fork ();
switch (child_pid) {
case -1:
fprintf (stderr, "Cannot fork(): %s\n", strerror(errno));
break;
case 0:
/* child */
dev_null_fd = open ("/dev/null", O_RDWR);
/* ignore if we can't open /dev/null */
if (dev_null_fd >= 0) {
/* attach /dev/null to stdout, stdin, stderr */
dup2 (dev_null_fd, 0);
dup2 (dev_null_fd, 1);
dup2 (dev_null_fd, 2);
close (dev_null_fd);
}
umask (022);
break;
default:
/* parent, block until child writes */
exit (parent_wait_for_child (startup_daemonize_pipe[0], child_pid));
break;
}
/* Create session */
setsid ();
/* remove old pid file */
unlink (HALD_PID_FILE);
/* Make a new one */
if ((pf= open (HALD_PID_FILE, O_WRONLY|O_CREAT|O_TRUNC|O_EXCL, 0644)) > 0) {
snprintf (pid, sizeof(pid), "%lu\n", (long unsigned) getpid ());
written = write (pf, pid, strlen(pid));
close (pf);
atexit (delete_pid);
}
} else {
HAL_INFO (("Will not daemonize"));
}
/* we need to do stuff when we are expected to terminate, thus
* this involves looking for SIGTERM; UNIX signal handlers are
* evil though, so set up a pipe to transmit the signal.
*/
/* create pipe */
if (pipe (sigterm_unix_signal_pipe_fds) != 0) {
DIE (("Could not setup pipe, errno=%d", errno));
}
/* setup glib handler - 0 is for reading, 1 is for writing */
sigterm_iochn = g_io_channel_unix_new (sigterm_unix_signal_pipe_fds[0]);
if (sigterm_iochn == NULL)
DIE (("Could not create GIOChannel"));
/* get callback when there is data to read */
sigterm_iochn_listener_source_id = g_io_add_watch (
sigterm_iochn, G_IO_IN, sigterm_iochn_data, NULL);
/* Finally, setup unix signal handler for TERM */
signal (SIGTERM, handle_sigterm);
/* set up the local dbus server */
if (!hald_dbus_local_server_init ())
return 1;
/* Start the runner helper daemon */
if (!hald_runner_start_runner ()) {
return 1;
}
drop_privileges(0);
/* initialize operating system specific parts */
osspec_init ();
hald_is_initialising = TRUE;
/* detect devices */
osspec_probe ();
/* run the main loop and serve clients */
g_main_loop_run (loop);
return 0;
}
#ifdef HALD_MEMLEAK_DBG
extern int dbg_hal_device_object_delta;
/* useful for valgrinding; see below */
static gboolean
my_shutdown (gpointer data)
{
HalDeviceStore *gdl;
printf ("Num devices in TDL: %d\n", g_slist_length ((hald_get_tdl ())->devices));
printf ("Num devices in GDL: %d\n", g_slist_length ((hald_get_gdl ())->devices));
gdl = hald_get_gdl ();
next:
if (g_slist_length (gdl->devices) > 0) {
HalDevice *d = HAL_DEVICE(gdl->devices->data);
hal_device_store_remove (gdl, d);
g_object_unref (d);
goto next;
}
printf ("hal_device_object_delta = %d (should be zero)\n", dbg_hal_device_object_delta);
exit (1);
}
#endif
void
osspec_probe_done (void)
{
ssize_t written;
char buf[1] = {0};
HAL_INFO (("Device probing completed"));
if (!hald_dbus_init ()) {
hald_runner_kill_all();
exit (1);
}
/* tell parent to exit */
written = write (startup_daemonize_pipe[1], buf, sizeof (buf));
close (startup_daemonize_pipe[0]);
close (startup_daemonize_pipe[1]);
hald_is_initialising = FALSE;
#ifdef HALD_MEMLEAK_DBG
g_timeout_add ((HALD_MEMLEAK_DBG) * 1000,
my_shutdown,
NULL);
#endif
}
/** @} */
|