cleanup debug level handover
[project/procd.git] / procd.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 "watchdog.h"
25 #include "plug/hotplug.h"
26
27 unsigned int debug;
28
29 static int usage(const char *prog)
30 {
31         ERROR("Usage: %s [options]\n"
32                 "Options:\n"
33                 "\t-s <path>\tPath to ubus socket\n"
34                 "\t-h <path>\trun as hotplug daemon\n"
35                 "\t-d <level>\tEnable debug messages\n"
36                 "\n", prog);
37         return 1;
38 }
39
40 int main(int argc, char **argv)
41 {
42         int ch;
43         char *dbglvl = getenv("DBGLVL");
44
45         if (dbglvl) {
46                 debug = atoi(dbglvl);
47                 unsetenv("DBGLVL");
48         }
49
50         while ((ch = getopt(argc, argv, "d:s:h:")) != -1) {
51                 switch (ch) {
52                 case 'h':
53                         return hotplug_run(optarg);
54                 case 's':
55                         ubus_socket = optarg;
56                         break;
57                 case 'd':
58                         debug = atoi(optarg);
59                         break;
60                 default:
61                         return usage(argv[0]);
62                 }
63         }
64         uloop_init();
65         procd_signal();
66         trigger_init();
67         if (getpid() != 1)
68                 procd_connect_ubus();
69         else
70                 procd_state_next();
71         uloop_run();
72
73         return 0;
74 }