add a rcS helper
[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
18 #include <unistd.h>
19 #include <getopt.h>
20 #include <libgen.h>
21
22 #include "procd.h"
23 #include "hotplug.h"
24 #include "watchdog.h"
25
26 static int usage(const char *prog)
27 {
28 ERROR("Usage: %s [options]\n"
29 "Options:\n"
30 " -s <path>: Path to ubus socket\n"
31 " -d: Enable debug messages\n"
32 "\n", prog);
33 return 1;
34 }
35
36
37 static int main_procd_init(int argc, char **argv)
38 {
39 procd_signal_preinit();
40 procd_early();
41 debug_init();
42 watchdog_init();
43 uloop_init();
44 hotplug("/etc/hotplug-preinit.json");
45 procd_preinit();
46 uloop_run();
47 return 0;
48 }
49
50 int main(int argc, char **argv)
51 {
52 int ch;
53
54 if (!strcmp(basename(*argv), "init"))
55 return main_procd_init(argc, argv);
56
57 while ((ch = getopt(argc, argv, "ds:")) != -1) {
58 switch (ch) {
59 case 's':
60 ubus_socket = optarg;
61 break;
62 case 'd':
63 debug++;
64 break;
65 default:
66 return usage(argv[0]);
67 }
68 }
69 uloop_init();
70 procd_signal();
71 if (getpid() != 1)
72 procd_connect_ubus();
73 else
74 procd_state_next();
75 uloop_run();
76
77 return 0;
78 }