trace: use standard POSIX header for basename()
[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 #include <sys/reboot.h>
19
20 #include <unistd.h>
21 #include <getopt.h>
22 #include <libgen.h>
23
24 #include "procd.h"
25 #include "watchdog.h"
26 #include "plug/hotplug.h"
27
28 unsigned int debug;
29
30 static struct udebug ud;
31 static struct udebug_buf udb;
32 static bool udebug_enabled;
33
34 static void procd_udebug_vprintf(const char *format, va_list ap)
35 {
36 if (!udebug_enabled)
37 return;
38
39 udebug_entry_init(&udb);
40 udebug_entry_vprintf(&udb, format, ap);
41 udebug_entry_add(&udb);
42 }
43
44 void procd_udebug_printf(const char *format, ...)
45 {
46 va_list ap;
47
48 va_start(ap, format);
49 procd_udebug_vprintf(format, ap);
50 va_end(ap);
51 }
52
53 void procd_udebug_set_enabled(bool val)
54 {
55 static const struct udebug_buf_meta meta = {
56 .name = "procd_log",
57 .format = UDEBUG_FORMAT_STRING,
58 };
59
60 if (udebug_enabled == val)
61 return;
62
63 udebug_enabled = val;
64 if (!val) {
65 ulog_udebug(NULL);
66 udebug_buf_free(&udb);
67 udebug_free(&ud);
68 return;
69 }
70
71 udebug_init(&ud);
72 udebug_auto_connect(&ud, NULL);
73 udebug_buf_init(&udb, 1024, 64 * 1024);
74 udebug_buf_add(&ud, &udb, &meta);
75 ulog_udebug(&udb);
76 }
77
78
79 static int usage(const char *prog)
80 {
81 fprintf(stderr, "Usage: %s [options]\n"
82 "Options:\n"
83 " -s <path> Path to ubus socket\n"
84 " -h <path> run as hotplug daemon\n"
85 " -d <level> Enable debug messages\n"
86 " -S Print messages to stdout\n"
87 "\n", prog);
88 return 1;
89 }
90
91 int main(int argc, char **argv)
92 {
93 int ch;
94 char *dbglvl = getenv("DBGLVL");
95 int ulog_channels = ULOG_KMSG;
96
97 if (dbglvl) {
98 debug = atoi(dbglvl);
99 unsetenv("DBGLVL");
100 }
101
102 while ((ch = getopt(argc, argv, "d:s:h:S")) != -1) {
103 switch (ch) {
104 case 'h':
105 return hotplug_run(optarg);
106 case 's':
107 ubus_socket = optarg;
108 break;
109 case 'd':
110 debug = atoi(optarg);
111 break;
112 case 'S':
113 ulog_channels = ULOG_STDIO;
114 break;
115 default:
116 return usage(argv[0]);
117 }
118 }
119
120 ulog_open(ulog_channels, LOG_DAEMON, "procd");
121 ulog_threshold(LOG_DEBUG + 1);
122
123 setsid();
124 uloop_init();
125 procd_signal();
126 procd_udebug_set_enabled(true);
127 if (getpid() != 1)
128 procd_connect_ubus();
129 else
130 procd_state_next();
131 uloop_run();
132 uloop_done();
133
134 return 0;
135 }