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