procd: Remove unnecessary memset calls.
[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 #ifdef linux
17 #include <sys/sysinfo.h>
18 #endif
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/reboot.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27
28 #include <libubox/uloop.h>
29
30 #include "procd.h"
31 #include "sysupgrade.h"
32 #include "watchdog.h"
33
34 static struct blob_buf b;
35 static int notify;
36 static struct ubus_context *_ctx;
37
38 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
39 struct ubus_request_data *req, const char *method,
40 struct blob_attr *msg)
41 {
42 void *c;
43 char line[256];
44 char *key, *val, *next;
45 struct utsname utsname;
46 FILE *f;
47
48 blob_buf_init(&b, 0);
49
50 if (uname(&utsname) >= 0)
51 {
52 blobmsg_add_string(&b, "kernel", utsname.release);
53 blobmsg_add_string(&b, "hostname", utsname.nodename);
54 }
55
56 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
57 {
58 while(fgets(line, sizeof(line), f))
59 {
60 key = strtok(line, "\t:");
61 val = strtok(NULL, "\t\n");
62
63 if (!key || !val)
64 continue;
65
66 if (!strcasecmp(key, "system type") ||
67 !strcasecmp(key, "processor") ||
68 !strcasecmp(key, "model name"))
69 {
70 strtoul(val + 2, &key, 0);
71
72 if (key == (val + 2) || *key != 0)
73 {
74 blobmsg_add_string(&b, "system", val + 2);
75 break;
76 }
77 }
78 }
79
80 fclose(f);
81 }
82
83 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL ||
84 (f = fopen("/proc/device-tree/model", "r")) != NULL)
85 {
86 if (fgets(line, sizeof(line), f))
87 {
88 val = strtok(line, "\t\n");
89
90 if (val)
91 blobmsg_add_string(&b, "model", val);
92 }
93
94 fclose(f);
95 }
96 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
97 {
98 while(fgets(line, sizeof(line), f))
99 {
100 key = strtok(line, "\t:");
101 val = strtok(NULL, "\t\n");
102
103 if (!key || !val)
104 continue;
105
106 if (!strcasecmp(key, "machine") ||
107 !strcasecmp(key, "hardware"))
108 {
109 blobmsg_add_string(&b, "model", val + 2);
110 break;
111 }
112 }
113
114 fclose(f);
115 }
116
117 if ((f = fopen("/tmp/sysinfo/board_name", "r")) != NULL)
118 {
119 if (fgets(line, sizeof(line), f))
120 {
121 val = strtok(line, "\t\n");
122
123 if (val)
124 blobmsg_add_string(&b, "board_name", val);
125 }
126
127 fclose(f);
128 }
129 else if ((f = fopen("/proc/device-tree/compatible", "r")) != NULL)
130 {
131 if (fgets(line, sizeof(line), f))
132 {
133 val = strtok(line, "\t\n");
134
135 if (val)
136 {
137 next = val;
138 while ((next = strchr(next, ',')) != NULL)
139 {
140 *next = '-';
141 next++;
142 }
143
144 blobmsg_add_string(&b, "board_name", val);
145 }
146 }
147
148 fclose(f);
149 }
150
151 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
152 {
153 c = blobmsg_open_table(&b, "release");
154
155 while (fgets(line, sizeof(line), f))
156 {
157 char *dest;
158 char ch;
159
160 key = line;
161 val = strchr(line, '=');
162 if (!val)
163 continue;
164
165 *(val++) = 0;
166
167 if (!strcasecmp(key, "DISTRIB_ID"))
168 key = "distribution";
169 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
170 key = "version";
171 else if (!strcasecmp(key, "DISTRIB_REVISION"))
172 key = "revision";
173 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
174 key = "codename";
175 else if (!strcasecmp(key, "DISTRIB_TARGET"))
176 key = "target";
177 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
178 key = "description";
179 else
180 continue;
181
182 dest = blobmsg_alloc_string_buffer(&b, key, strlen(val));
183 if (!dest) {
184 ERROR("Failed to allocate blob.\n");
185 continue;
186 }
187
188 while (val && (ch = *(val++)) != 0) {
189 switch (ch) {
190 case '\'':
191 case '"':
192 next = strchr(val, ch);
193 if (next)
194 *next = 0;
195
196 strcpy(dest, val);
197
198 if (next)
199 val = next + 1;
200
201 dest += strlen(dest);
202 break;
203 case '\\':
204 *(dest++) = *(val++);
205 break;
206 }
207 }
208 blobmsg_add_string_buffer(&b);
209 }
210
211 blobmsg_close_array(&b, c);
212
213 fclose(f);
214 }
215
216 ubus_send_reply(ctx, req, b.head);
217
218 return UBUS_STATUS_OK;
219 }
220
221 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
222 struct ubus_request_data *req, const char *method,
223 struct blob_attr *msg)
224 {
225 time_t now;
226 struct tm *tm;
227 #ifdef linux
228 struct sysinfo info;
229 void *c;
230
231 if (sysinfo(&info))
232 return UBUS_STATUS_UNKNOWN_ERROR;
233 #endif
234
235 now = time(NULL);
236
237 if (!(tm = localtime(&now)))
238 return UBUS_STATUS_UNKNOWN_ERROR;
239
240 blob_buf_init(&b, 0);
241
242 blobmsg_add_u32(&b, "localtime", now + tm->tm_gmtoff);
243
244 #ifdef linux
245 blobmsg_add_u32(&b, "uptime", info.uptime);
246
247 c = blobmsg_open_array(&b, "load");
248 blobmsg_add_u32(&b, NULL, info.loads[0]);
249 blobmsg_add_u32(&b, NULL, info.loads[1]);
250 blobmsg_add_u32(&b, NULL, info.loads[2]);
251 blobmsg_close_array(&b, c);
252
253 c = blobmsg_open_table(&b, "memory");
254 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalram);
255 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram);
256 blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram);
257 blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram);
258 blobmsg_close_table(&b, c);
259
260 c = blobmsg_open_table(&b, "swap");
261 blobmsg_add_u64(&b, "total", info.mem_unit * info.totalswap);
262 blobmsg_add_u64(&b, "free", info.mem_unit * info.freeswap);
263 blobmsg_close_table(&b, c);
264 #endif
265
266 ubus_send_reply(ctx, req, b.head);
267
268 return UBUS_STATUS_OK;
269 }
270
271 static int system_reboot(struct ubus_context *ctx, struct ubus_object *obj,
272 struct ubus_request_data *req, const char *method,
273 struct blob_attr *msg)
274 {
275 procd_shutdown(RB_AUTOBOOT);
276 return 0;
277 }
278
279 enum {
280 WDT_FREQUENCY,
281 WDT_TIMEOUT,
282 WDT_MAGICCLOSE,
283 WDT_STOP,
284 __WDT_MAX
285 };
286
287 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
288 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
289 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
290 [WDT_MAGICCLOSE] = { .name = "magicclose", .type = BLOBMSG_TYPE_BOOL },
291 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
292 };
293
294 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
295 struct ubus_request_data *req, const char *method,
296 struct blob_attr *msg)
297 {
298 struct blob_attr *tb[__WDT_MAX];
299 const char *status;
300
301 if (!msg)
302 return UBUS_STATUS_INVALID_ARGUMENT;
303
304 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
305 if (tb[WDT_FREQUENCY]) {
306 unsigned int timeout = watchdog_timeout(0);
307 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
308
309 if (freq) {
310 if (freq > timeout / 2)
311 freq = timeout / 2;
312 watchdog_frequency(freq);
313 }
314 }
315
316 if (tb[WDT_TIMEOUT]) {
317 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
318 unsigned int frequency = watchdog_frequency(0);
319
320 if (timeout <= frequency)
321 timeout = frequency * 2;
322 watchdog_timeout(timeout);
323 }
324
325 if (tb[WDT_MAGICCLOSE])
326 watchdog_set_magicclose(blobmsg_get_bool(tb[WDT_MAGICCLOSE]));
327
328 if (tb[WDT_STOP])
329 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
330
331 if (watchdog_fd() == NULL)
332 status = "offline";
333 else if (watchdog_get_stopped())
334 status = "stopped";
335 else
336 status = "running";
337
338 blob_buf_init(&b, 0);
339 blobmsg_add_string(&b, "status", status);
340 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
341 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
342 blobmsg_add_u8(&b, "magicclose", watchdog_get_magicclose());
343 ubus_send_reply(ctx, req, b.head);
344
345 return 0;
346 }
347
348 enum {
349 SIGNAL_PID,
350 SIGNAL_NUM,
351 __SIGNAL_MAX
352 };
353
354 static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
355 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
356 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
357 };
358
359 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
360 struct ubus_request_data *req, const char *method,
361 struct blob_attr *msg)
362 {
363 struct blob_attr *tb[__SIGNAL_MAX];
364
365 if (!msg)
366 return UBUS_STATUS_INVALID_ARGUMENT;
367
368 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
369 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
370 return UBUS_STATUS_INVALID_ARGUMENT;
371
372 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
373
374 return 0;
375 }
376
377 enum {
378 SYSUPGRADE_PATH,
379 SYSUPGRADE_PREFIX,
380 SYSUPGRADE_COMMAND,
381 __SYSUPGRADE_MAX
382 };
383
384 static const struct blobmsg_policy sysupgrade_policy[__SYSUPGRADE_MAX] = {
385 [SYSUPGRADE_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
386 [SYSUPGRADE_PREFIX] = { .name = "prefix", .type = BLOBMSG_TYPE_STRING },
387 [SYSUPGRADE_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_STRING },
388 };
389
390 static int sysupgrade(struct ubus_context *ctx, struct ubus_object *obj,
391 struct ubus_request_data *req, const char *method,
392 struct blob_attr *msg)
393 {
394 struct blob_attr *tb[__SYSUPGRADE_MAX];
395
396 if (!msg)
397 return UBUS_STATUS_INVALID_ARGUMENT;
398
399 blobmsg_parse(sysupgrade_policy, __SYSUPGRADE_MAX, tb, blob_data(msg), blob_len(msg));
400 if (!tb[SYSUPGRADE_PATH] || !tb[SYSUPGRADE_PREFIX])
401 return UBUS_STATUS_INVALID_ARGUMENT;
402
403 sysupgrade_exec_upgraded(blobmsg_get_string(tb[SYSUPGRADE_PREFIX]),
404 blobmsg_get_string(tb[SYSUPGRADE_PATH]),
405 tb[SYSUPGRADE_COMMAND] ? blobmsg_get_string(tb[SYSUPGRADE_COMMAND]) : NULL);
406
407 /* sysupgrade_exec_upgraded() will never return unless something has gone wrong */
408 return UBUS_STATUS_UNKNOWN_ERROR;
409 }
410
411 static void
412 procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
413 {
414 notify = obj->has_subscribers;
415 }
416
417
418 static const struct ubus_method system_methods[] = {
419 UBUS_METHOD_NOARG("board", system_board),
420 UBUS_METHOD_NOARG("info", system_info),
421 UBUS_METHOD_NOARG("reboot", system_reboot),
422 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
423 UBUS_METHOD("signal", proc_signal, signal_policy),
424 UBUS_METHOD("sysupgrade", sysupgrade, sysupgrade_policy),
425 };
426
427 static struct ubus_object_type system_object_type =
428 UBUS_OBJECT_TYPE("system", system_methods);
429
430 static struct ubus_object system_object = {
431 .name = "system",
432 .type = &system_object_type,
433 .methods = system_methods,
434 .n_methods = ARRAY_SIZE(system_methods),
435 .subscribe_cb = procd_subscribe_cb,
436 };
437
438 void
439 procd_bcast_event(char *event, struct blob_attr *msg)
440 {
441 int ret;
442
443 if (!notify)
444 return;
445
446 ret = ubus_notify(_ctx, &system_object, event, msg, -1);
447 if (ret)
448 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
449 }
450
451 void ubus_init_system(struct ubus_context *ctx)
452 {
453 int ret;
454
455 _ctx = ctx;
456 ret = ubus_add_object(ctx, &system_object);
457 if (ret)
458 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
459 }