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>
17 #include <sys/sysinfo.h>
19 #include <sys/ioctl.h>
20 #include <sys/types.h>
21 #include <sys/reboot.h>
28 #include <libubox/uloop.h>
31 #include "sysupgrade.h"
34 static struct blob_buf b
;
36 static struct ubus_context
*_ctx
;
38 static int system_board(struct ubus_context
*ctx
, struct ubus_object
*obj
,
39 struct ubus_request_data
*req
, const char *method
,
40 struct blob_attr
*msg
)
44 char *key
, *val
, *next
;
45 struct utsname utsname
;
50 if (uname(&utsname
) >= 0)
52 blobmsg_add_string(&b
, "kernel", utsname
.release
);
53 blobmsg_add_string(&b
, "hostname", utsname
.nodename
);
56 if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
58 while(fgets(line
, sizeof(line
), f
))
60 key
= strtok(line
, "\t:");
61 val
= strtok(NULL
, "\t\n");
66 if (!strcasecmp(key
, "system type") ||
67 !strcasecmp(key
, "processor") ||
68 !strcasecmp(key
, "cpu") ||
69 !strcasecmp(key
, "model name"))
71 strtoul(val
+ 2, &key
, 0);
73 if (key
== (val
+ 2) || *key
!= 0)
75 blobmsg_add_string(&b
, "system", val
+ 2);
84 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
||
85 (f
= fopen("/proc/device-tree/model", "r")) != NULL
)
87 if (fgets(line
, sizeof(line
), f
))
89 val
= strtok(line
, "\t\n");
92 blobmsg_add_string(&b
, "model", val
);
97 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
99 while(fgets(line
, sizeof(line
), f
))
101 key
= strtok(line
, "\t:");
102 val
= strtok(NULL
, "\t\n");
107 if (!strcasecmp(key
, "machine") ||
108 !strcasecmp(key
, "hardware"))
110 blobmsg_add_string(&b
, "model", val
+ 2);
118 if ((f
= fopen("/tmp/sysinfo/board_name", "r")) != NULL
)
120 if (fgets(line
, sizeof(line
), f
))
122 val
= strtok(line
, "\t\n");
125 blobmsg_add_string(&b
, "board_name", val
);
130 else if ((f
= fopen("/proc/device-tree/compatible", "r")) != NULL
)
132 if (fgets(line
, sizeof(line
), f
))
134 val
= strtok(line
, "\t\n");
139 while ((next
= strchr(next
, ',')) != NULL
)
145 blobmsg_add_string(&b
, "board_name", val
);
152 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
154 c
= blobmsg_open_table(&b
, "release");
156 while (fgets(line
, sizeof(line
), f
))
162 val
= strchr(line
, '=');
168 if (!strcasecmp(key
, "DISTRIB_ID"))
169 key
= "distribution";
170 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
172 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
174 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
176 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
178 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
183 dest
= blobmsg_alloc_string_buffer(&b
, key
, strlen(val
));
185 ERROR("Failed to allocate blob.\n");
189 while (val
&& (ch
= *(val
++)) != 0) {
193 next
= strchr(val
, ch
);
202 dest
+= strlen(dest
);
205 *(dest
++) = *(val
++);
209 blobmsg_add_string_buffer(&b
);
212 blobmsg_close_array(&b
, c
);
217 ubus_send_reply(ctx
, req
, b
.head
);
219 return UBUS_STATUS_OK
;
222 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
223 struct ubus_request_data
*req
, const char *method
,
224 struct blob_attr
*msg
)
233 return UBUS_STATUS_UNKNOWN_ERROR
;
238 if (!(tm
= localtime(&now
)))
239 return UBUS_STATUS_UNKNOWN_ERROR
;
241 blob_buf_init(&b
, 0);
243 blobmsg_add_u32(&b
, "localtime", now
+ tm
->tm_gmtoff
);
246 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
248 c
= blobmsg_open_array(&b
, "load");
249 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
250 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
251 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
252 blobmsg_close_array(&b
, c
);
254 c
= blobmsg_open_table(&b
, "memory");
255 blobmsg_add_u64(&b
, "total", info
.mem_unit
* info
.totalram
);
256 blobmsg_add_u64(&b
, "free", info
.mem_unit
* info
.freeram
);
257 blobmsg_add_u64(&b
, "shared", info
.mem_unit
* info
.sharedram
);
258 blobmsg_add_u64(&b
, "buffered", info
.mem_unit
* info
.bufferram
);
259 blobmsg_close_table(&b
, c
);
261 c
= blobmsg_open_table(&b
, "swap");
262 blobmsg_add_u64(&b
, "total", info
.mem_unit
* info
.totalswap
);
263 blobmsg_add_u64(&b
, "free", info
.mem_unit
* info
.freeswap
);
264 blobmsg_close_table(&b
, c
);
267 ubus_send_reply(ctx
, req
, b
.head
);
269 return UBUS_STATUS_OK
;
272 static int system_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
273 struct ubus_request_data
*req
, const char *method
,
274 struct blob_attr
*msg
)
276 procd_shutdown(RB_AUTOBOOT
);
288 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
289 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
290 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
291 [WDT_MAGICCLOSE
] = { .name
= "magicclose", .type
= BLOBMSG_TYPE_BOOL
},
292 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
295 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
296 struct ubus_request_data
*req
, const char *method
,
297 struct blob_attr
*msg
)
299 struct blob_attr
*tb
[__WDT_MAX
];
303 return UBUS_STATUS_INVALID_ARGUMENT
;
305 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
306 if (tb
[WDT_FREQUENCY
]) {
307 unsigned int timeout
= watchdog_timeout(0);
308 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
311 if (freq
> timeout
/ 2)
313 watchdog_frequency(freq
);
317 if (tb
[WDT_TIMEOUT
]) {
318 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
319 unsigned int frequency
= watchdog_frequency(0);
321 if (timeout
<= frequency
)
322 timeout
= frequency
* 2;
323 watchdog_timeout(timeout
);
326 if (tb
[WDT_MAGICCLOSE
])
327 watchdog_set_magicclose(blobmsg_get_bool(tb
[WDT_MAGICCLOSE
]));
330 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
332 if (watchdog_fd() == NULL
)
334 else if (watchdog_get_stopped())
339 blob_buf_init(&b
, 0);
340 blobmsg_add_string(&b
, "status", status
);
341 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
342 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
343 blobmsg_add_u8(&b
, "magicclose", watchdog_get_magicclose());
344 ubus_send_reply(ctx
, req
, b
.head
);
355 static const struct blobmsg_policy signal_policy
[__SIGNAL_MAX
] = {
356 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
357 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
360 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
361 struct ubus_request_data
*req
, const char *method
,
362 struct blob_attr
*msg
)
364 struct blob_attr
*tb
[__SIGNAL_MAX
];
367 return UBUS_STATUS_INVALID_ARGUMENT
;
369 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
370 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
371 return UBUS_STATUS_INVALID_ARGUMENT
;
373 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
385 static const struct blobmsg_policy sysupgrade_policy
[__SYSUPGRADE_MAX
] = {
386 [SYSUPGRADE_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
387 [SYSUPGRADE_PREFIX
] = { .name
= "prefix", .type
= BLOBMSG_TYPE_STRING
},
388 [SYSUPGRADE_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
391 static int sysupgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
392 struct ubus_request_data
*req
, const char *method
,
393 struct blob_attr
*msg
)
395 struct blob_attr
*tb
[__SYSUPGRADE_MAX
];
398 return UBUS_STATUS_INVALID_ARGUMENT
;
400 blobmsg_parse(sysupgrade_policy
, __SYSUPGRADE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
401 if (!tb
[SYSUPGRADE_PATH
] || !tb
[SYSUPGRADE_PREFIX
])
402 return UBUS_STATUS_INVALID_ARGUMENT
;
404 sysupgrade_exec_upgraded(blobmsg_get_string(tb
[SYSUPGRADE_PREFIX
]),
405 blobmsg_get_string(tb
[SYSUPGRADE_PATH
]),
406 tb
[SYSUPGRADE_COMMAND
] ? blobmsg_get_string(tb
[SYSUPGRADE_COMMAND
]) : NULL
);
408 /* sysupgrade_exec_upgraded() will never return unless something has gone wrong */
409 return UBUS_STATUS_UNKNOWN_ERROR
;
413 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
415 notify
= obj
->has_subscribers
;
419 static const struct ubus_method system_methods
[] = {
420 UBUS_METHOD_NOARG("board", system_board
),
421 UBUS_METHOD_NOARG("info", system_info
),
422 UBUS_METHOD_NOARG("reboot", system_reboot
),
423 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
424 UBUS_METHOD("signal", proc_signal
, signal_policy
),
425 UBUS_METHOD("sysupgrade", sysupgrade
, sysupgrade_policy
),
428 static struct ubus_object_type system_object_type
=
429 UBUS_OBJECT_TYPE("system", system_methods
);
431 static struct ubus_object system_object
= {
433 .type
= &system_object_type
,
434 .methods
= system_methods
,
435 .n_methods
= ARRAY_SIZE(system_methods
),
436 .subscribe_cb
= procd_subscribe_cb
,
440 procd_bcast_event(char *event
, struct blob_attr
*msg
)
447 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
449 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
452 void ubus_init_system(struct ubus_context
*ctx
)
457 ret
= ubus_add_object(ctx
, &system_object
);
459 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));