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