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