2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/utsname.h>
16 #include <sys/sysinfo.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
24 #include <libubox/uloop.h>
29 static struct blob_buf b;
31 static struct ubus_context *_ctx;
33 int upgrade_running = 0;
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)
42 struct utsname utsname;
47 if (uname(&utsname) >= 0)
49 blobmsg_add_string(&b, "kernel", utsname.release);
50 blobmsg_add_string(&b, "hostname", utsname.nodename);
53 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
55 while(fgets(line, sizeof(line), f))
57 key = strtok(line, "\t:");
58 val = strtok(NULL, "\t\n");
63 if (!strcasecmp(key, "system type") ||
64 !strcasecmp(key, "processor") ||
65 !strcasecmp(key, "model name"))
67 blobmsg_add_string(&b, "system", val + 2);
75 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
77 if (fgets(line, sizeof(line), f))
79 val = strtok(line, "\t\n");
82 blobmsg_add_string(&b, "model", val);
87 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
89 while(fgets(line, sizeof(line), f))
91 key = strtok(line, "\t:");
92 val = strtok(NULL, "\t\n");
97 if (!strcasecmp(key, "machine") ||
98 !strcasecmp(key, "hardware"))
100 blobmsg_add_string(&b, "model", val + 2);
108 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
110 c = blobmsg_open_table(&b, "release");
112 while (fgets(line, sizeof(line), f))
114 key = strtok(line, "=\"");
115 val = strtok(NULL, "\"\n");
120 if (!strcasecmp(key, "DISTRIB_ID"))
121 blobmsg_add_string(&b, "distribution", val);
122 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
123 blobmsg_add_string(&b, "version", val);
124 else if (!strcasecmp(key, "DISTRIB_REVISION"))
125 blobmsg_add_string(&b, "revision", val);
126 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
127 blobmsg_add_string(&b, "codename", val);
128 else if (!strcasecmp(key, "DISTRIB_TARGET"))
129 blobmsg_add_string(&b, "target", val);
130 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
131 blobmsg_add_string(&b, "description", val);
134 blobmsg_close_array(&b, c);
139 ubus_send_reply(ctx, req, b.head);
141 return UBUS_STATUS_OK;
144 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
145 struct ubus_request_data *req, const char *method,
146 struct blob_attr *msg)
155 if (!(tm = localtime(&now)))
156 return UBUS_STATUS_UNKNOWN_ERROR;
159 return UBUS_STATUS_UNKNOWN_ERROR;
161 blob_buf_init(&b, 0);
163 blobmsg_add_u32(&b, "uptime", info.uptime);
164 blobmsg_add_u32(&b, "localtime", mktime(tm));
166 c = blobmsg_open_array(&b, "load");
167 blobmsg_add_u32(&b, NULL, info.loads[0]);
168 blobmsg_add_u32(&b, NULL, info.loads[1]);
169 blobmsg_add_u32(&b, NULL, info.loads[2]);
170 blobmsg_close_array(&b, c);
172 c = blobmsg_open_table(&b, "memory");
173 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalram);
174 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeram);
175 blobmsg_add_u32(&b, "shared", info.mem_unit * info.sharedram);
176 blobmsg_add_u32(&b, "buffered", info.mem_unit * info.bufferram);
177 blobmsg_close_table(&b, c);
179 c = blobmsg_open_table(&b, "swap");
180 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalswap);
181 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeswap);
182 blobmsg_close_table(&b, c);
184 ubus_send_reply(ctx, req, b.head);
186 return UBUS_STATUS_OK;
189 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
190 struct ubus_request_data *req, const char *method,
191 struct blob_attr *msg)
204 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
205 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
206 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
207 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
210 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
211 struct ubus_request_data *req, const char *method,
212 struct blob_attr *msg)
214 struct blob_attr *tb[__WDT_MAX];
218 return UBUS_STATUS_INVALID_ARGUMENT;
220 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
221 if (tb[WDT_FREQUENCY]) {
222 unsigned int timeout = watchdog_timeout(0);
223 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
226 if (freq > timeout / 2)
228 watchdog_frequency(freq);
232 if (tb[WDT_TIMEOUT]) {
233 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
234 unsigned int frequency = watchdog_frequency(0);
236 if (timeout <= frequency)
237 timeout = frequency * 2;
238 watchdog_timeout(timeout);
242 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
244 if (watchdog_fd() < 0)
246 else if (watchdog_get_stopped())
251 blob_buf_init(&b, 0);
252 blobmsg_add_string(&b, "status", status);
253 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
254 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
255 ubus_send_reply(ctx, req, b.head);
266 static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
267 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
268 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
271 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
272 struct ubus_request_data *req, const char *method,
273 struct blob_attr *msg)
275 struct blob_attr *tb[__SIGNAL_MAX];
278 return UBUS_STATUS_INVALID_ARGUMENT;
280 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
281 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
282 return UBUS_STATUS_INVALID_ARGUMENT;
284 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
294 static const struct blobmsg_policy nand_policy[__NAND_MAX] = {
295 [NAND_FOLDER] = { .name = "folder", .type = BLOBMSG_TYPE_STRING },
299 procd_spawn_upgraded(char *folder)
301 char *wdt_fd = watchdog_fd();
302 char *argv[] = { "/tmp/upgraded", NULL, NULL};
306 DEBUG(2, "Exec to upgraded now\n");
308 watchdog_no_cloexec();
309 setenv("WDTFD", wdt_fd, 1);
311 execvp(argv[0], argv);
314 static int nand_set(struct ubus_context *ctx, struct ubus_object *obj,
315 struct ubus_request_data *req, const char *method,
316 struct blob_attr *msg)
318 struct blob_attr *tb[__NAND_MAX];
321 return UBUS_STATUS_INVALID_ARGUMENT;
323 blobmsg_parse(nand_policy, __NAND_MAX, tb, blob_data(msg), blob_len(msg));
324 if (!tb[NAND_FOLDER])
325 return UBUS_STATUS_INVALID_ARGUMENT;
327 procd_spawn_upgraded(blobmsg_get_string(tb[NAND_FOLDER]));
328 fprintf(stderr, "Yikees, something went wrong. no /sbin/upgraded ?\n");
333 procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
335 notify = obj->has_subscribers;
339 static const struct ubus_method system_methods[] = {
340 UBUS_METHOD_NOARG("board", system_board),
341 UBUS_METHOD_NOARG("info", system_info),
342 UBUS_METHOD_NOARG("upgrade", system_upgrade),
343 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
344 UBUS_METHOD("signal", proc_signal, signal_policy),
346 /* must remain at the end as it ia not always loaded */
347 UBUS_METHOD("nandupgrade", nand_set, nand_policy),
350 static struct ubus_object_type system_object_type =
351 UBUS_OBJECT_TYPE("system", system_methods);
353 static struct ubus_object system_object = {
355 .type = &system_object_type,
356 .methods = system_methods,
357 .n_methods = ARRAY_SIZE(system_methods),
358 .subscribe_cb = procd_subscribe_cb,
362 procd_bcast_event(char *event, struct blob_attr *msg)
369 ret = ubus_notify(_ctx, &system_object, event, msg, -1);
371 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
374 void ubus_init_system(struct ubus_context *ctx)
379 if (stat("/sbin/upgraded", &s))
380 system_object.n_methods -= 1;
383 ret = ubus_add_object(ctx, &system_object);
385 ERROR("Failed to add object: %s\n", ubus_strerror(ret));