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