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