add a ubus handler that allows sending signals to processes
[project/procd.git] / main.c
1 /*
2  * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3  * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License version 2.1
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <sys/wait.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18
19 #include <unistd.h>
20 #include <getopt.h>
21 #include <libgen.h>
22
23 #include "procd.h"
24 #include "hotplug.h"
25 #include "watchdog.h"
26
27 static int usage(const char *prog)
28 {
29         ERROR("Usage: %s [options]\n"
30                 "Options:\n"
31                 "    -s <path>:         Path to ubus socket\n"
32                 "    -d:                Enable debug messages\n"
33                 "\n", prog);
34         return 1;
35 }
36
37
38 static int main_procd_init(int argc, char **argv)
39 {
40         procd_signal_preinit();
41         procd_early();
42         debug_init();
43         watchdog_init(1);
44         system("/sbin/kmodloader /etc/modules-boot.d/");
45         uloop_init();
46         hotplug("/etc/hotplug-preinit.json");
47         procd_preinit();
48         uloop_run();
49         return 0;
50 }
51
52 int main(int argc, char **argv)
53 {
54         int ch;
55
56         if (!strcmp(basename(*argv), "init"))
57                 return main_procd_init(argc, argv);
58
59         while ((ch = getopt(argc, argv, "ds:")) != -1) {
60                 switch (ch) {
61                 case 's':
62                         ubus_socket = optarg;
63                         break;
64                 case 'd':
65                         debug++;
66                         break;
67                 default:
68                         return usage(argv[0]);
69                 }
70         }
71         uloop_init();
72         procd_signal();
73         trigger_init();
74         if (getpid() != 1)
75                 procd_connect_ubus();
76         else
77                 procd_state_next();
78         uloop_run();
79
80         return 0;
81 }