bind the console to /dev/null if the real console fails to come up
[project/procd.git] / system.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/utsname.h>
16 #include <sys/sysinfo.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <unistd.h>
23
24 #include <libubox/uloop.h>
25
26 #include "procd.h"
27 #include "watchdog.h"
28 #include "hotplug.h"
29
30 static struct blob_buf b;
31
32 int upgrade_running = 0;
33
34 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
35                  struct ubus_request_data *req, const char *method,
36                  struct blob_attr *msg)
37 {
38         void *c;
39         char line[256];
40         char *key, *val;
41         struct utsname utsname;
42         FILE *f;
43
44         blob_buf_init(&b, 0);
45
46         if (uname(&utsname) >= 0)
47         {
48                 blobmsg_add_string(&b, "kernel", utsname.release);
49                 blobmsg_add_string(&b, "hostname", utsname.nodename);
50         }
51
52         if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
53         {
54                 while(fgets(line, sizeof(line), f))
55                 {
56                         key = strtok(line, "\t:");
57                         val = strtok(NULL, "\t\n");
58
59                         if (!key || !val)
60                                 continue;
61
62                         if (!strcasecmp(key, "system type") ||
63                             !strcasecmp(key, "processor") ||
64                             !strcasecmp(key, "model name"))
65                         {
66                                 blobmsg_add_string(&b, "system", val + 2);
67                                 break;
68                         }
69                 }
70
71                 fclose(f);
72         }
73
74         if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
75         {
76                 if (fgets(line, sizeof(line), f))
77                 {
78                         val = strtok(line, "\t\n");
79
80                         if (val)
81                                 blobmsg_add_string(&b, "model", val);
82                 }
83
84                 fclose(f);
85         }
86         else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
87         {
88                 while(fgets(line, sizeof(line), f))
89                 {
90                         key = strtok(line, "\t:");
91                         val = strtok(NULL, "\t\n");
92
93                         if (!key || !val)
94                                 continue;
95
96                         if (!strcasecmp(key, "machine") ||
97                             !strcasecmp(key, "hardware"))
98                         {
99                                 blobmsg_add_string(&b, "model", val + 2);
100                                 break;
101                         }
102                 }
103
104                 fclose(f);
105         }
106
107         if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
108         {
109                 c = blobmsg_open_table(&b, "release");
110
111                 while (fgets(line, sizeof(line), f))
112                 {
113                         key = strtok(line, "=\"");
114                         val = strtok(NULL, "\"\n");
115
116                         if (!key || !val)
117                                 continue;
118
119                         if (!strcasecmp(key, "DISTRIB_ID"))
120                                 blobmsg_add_string(&b, "distribution", val);
121                         else if (!strcasecmp(key, "DISTRIB_RELEASE"))
122                                 blobmsg_add_string(&b, "version", val);
123                         else if (!strcasecmp(key, "DISTRIB_REVISION"))
124                                 blobmsg_add_string(&b, "revision", val);
125                         else if (!strcasecmp(key, "DISTRIB_CODENAME"))
126                                 blobmsg_add_string(&b, "codename", val);
127                         else if (!strcasecmp(key, "DISTRIB_TARGET"))
128                                 blobmsg_add_string(&b, "target", val);
129                         else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
130                                 blobmsg_add_string(&b, "description", val);
131                 }
132
133                 blobmsg_close_array(&b, c);
134
135                 fclose(f);
136         }
137
138         ubus_send_reply(ctx, req, b.head);
139
140         return UBUS_STATUS_OK;
141 }
142
143 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
144                 struct ubus_request_data *req, const char *method,
145                 struct blob_attr *msg)
146 {
147         void *c;
148         time_t now;
149         struct tm *tm;
150         struct sysinfo info;
151
152         now = time(NULL);
153
154         if (!(tm = localtime(&now)))
155                 return UBUS_STATUS_UNKNOWN_ERROR;
156
157         if (sysinfo(&info))
158                 return UBUS_STATUS_UNKNOWN_ERROR;
159
160         blob_buf_init(&b, 0);
161
162         blobmsg_add_u32(&b, "uptime",    info.uptime);
163         blobmsg_add_u32(&b, "localtime", mktime(tm));
164
165         c = blobmsg_open_array(&b, "load");
166         blobmsg_add_u32(&b, NULL, info.loads[0]);
167         blobmsg_add_u32(&b, NULL, info.loads[1]);
168         blobmsg_add_u32(&b, NULL, info.loads[2]);
169         blobmsg_close_array(&b, c);
170
171         c = blobmsg_open_table(&b, "memory");
172         blobmsg_add_u32(&b, "total",    info.mem_unit * info.totalram);
173         blobmsg_add_u32(&b, "free",     info.mem_unit * info.freeram);
174         blobmsg_add_u32(&b, "shared",   info.mem_unit * info.sharedram);
175         blobmsg_add_u32(&b, "buffered", info.mem_unit * info.bufferram);
176         blobmsg_close_table(&b, c);
177
178         c = blobmsg_open_table(&b, "swap");
179         blobmsg_add_u32(&b, "total",    info.mem_unit * info.totalswap);
180         blobmsg_add_u32(&b, "free",     info.mem_unit * info.freeswap);
181         blobmsg_close_table(&b, c);
182
183         ubus_send_reply(ctx, req, b.head);
184
185         return UBUS_STATUS_OK;
186 }
187
188 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
189                         struct ubus_request_data *req, const char *method,
190                         struct blob_attr *msg)
191 {
192         procd_reconnect_ubus(0);
193         log_shutdown();
194         hotplug_shutdown();
195
196         upgrade_running = 1;
197
198         return 0;
199 }
200
201 enum {
202         WDT_FREQUENCY,
203         WDT_TIMEOUT,
204         WDT_STOP,
205         __WDT_MAX
206 };
207
208 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
209         [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
210         [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
211         [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
212 };
213
214 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
215                         struct ubus_request_data *req, const char *method,
216                         struct blob_attr *msg)
217 {
218         struct blob_attr *tb[__WDT_MAX];
219         const char *status;
220
221         if (!msg)
222                 return UBUS_STATUS_INVALID_ARGUMENT;
223
224         blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
225         if (tb[WDT_FREQUENCY]) {
226                 unsigned int timeout = watchdog_timeout(0);
227                 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
228
229                 if (freq) {
230                         if (freq > timeout / 2)
231                                 freq = timeout / 2;
232                         watchdog_frequency(freq);
233                 }
234         }
235
236         if (tb[WDT_TIMEOUT]) {
237                 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
238                 unsigned int frequency = watchdog_frequency(0);
239
240                 if (timeout <= frequency)
241                         timeout = frequency * 2;
242                  watchdog_timeout(timeout);
243         }
244
245         if (tb[WDT_STOP])
246                 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
247
248         if (watchdog_fd() < 0)
249                 status = "offline";
250         else if (watchdog_get_stopped())
251                 status = "stopped";
252         else
253                 status = "running";
254
255         blob_buf_init(&b, 0);
256         blobmsg_add_string(&b, "status", status);
257         blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
258         blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
259         ubus_send_reply(ctx, req, b.head);
260
261         return 0;
262 }
263
264 enum {
265         SIGNAL_PID,
266         SIGNAL_NUM,
267         __SIGNAL_MAX
268 };
269
270 static const struct blobmsg_policy signal_policy[__WDT_MAX] = {
271         [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
272         [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
273 };
274
275 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
276                         struct ubus_request_data *req, const char *method,
277                         struct blob_attr *msg)
278 {
279         struct blob_attr *tb[__SIGNAL_MAX];
280
281         if (!msg)
282                 return UBUS_STATUS_INVALID_ARGUMENT;
283
284         blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
285         if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
286                 return UBUS_STATUS_INVALID_ARGUMENT;
287
288         kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
289
290         return 0;
291 }
292
293 static const struct ubus_method system_methods[] = {
294         UBUS_METHOD_NOARG("board", system_board),
295         UBUS_METHOD_NOARG("info",  system_info),
296         UBUS_METHOD_NOARG("upgrade", system_upgrade),
297         UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
298         UBUS_METHOD("signal", proc_signal, signal_policy),
299 };
300
301 static struct ubus_object_type system_object_type =
302         UBUS_OBJECT_TYPE("system", system_methods);
303
304 static struct ubus_object system_object = {
305         .name = "system",
306         .type = &system_object_type,
307         .methods = system_methods,
308         .n_methods = ARRAY_SIZE(system_methods),
309 };
310
311 void ubus_init_system(struct ubus_context *ctx)
312 {
313         int ret;
314
315         ret = ubus_add_object(ctx, &system_object);
316         if (ret)
317                 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
318 }