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
, "model name"))
70 strtoul(val
+ 2, &key
, 0);
72 if (key
== (val
+ 2) || *key
!= 0)
74 blobmsg_add_string(&b
, "system", val
+ 2);
83 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
||
84 (f
= fopen("/proc/device-tree/model", "r")) != NULL
)
86 if (fgets(line
, sizeof(line
), f
))
88 val
= strtok(line
, "\t\n");
91 blobmsg_add_string(&b
, "model", val
);
96 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
98 while(fgets(line
, sizeof(line
), f
))
100 key
= strtok(line
, "\t:");
101 val
= strtok(NULL
, "\t\n");
106 if (!strcasecmp(key
, "machine") ||
107 !strcasecmp(key
, "hardware"))
109 blobmsg_add_string(&b
, "model", val
+ 2);
117 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
119 c
= blobmsg_open_table(&b
, "release");
121 while (fgets(line
, sizeof(line
), f
))
127 val
= strchr(line
, '=');
133 if (!strcasecmp(key
, "DISTRIB_ID"))
134 key
= "distribution";
135 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
137 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
139 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
141 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
143 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
148 dest
= blobmsg_alloc_string_buffer(&b
, key
, strlen(val
));
150 ERROR("Failed to allocate blob.\n");
154 while (val
&& (ch
= *(val
++)) != 0) {
158 next
= strchr(val
, ch
);
167 dest
+= strlen(dest
);
170 *(dest
++) = *(val
++);
174 blobmsg_add_string_buffer(&b
);
177 blobmsg_close_array(&b
, c
);
182 ubus_send_reply(ctx
, req
, b
.head
);
184 return UBUS_STATUS_OK
;
187 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
188 struct ubus_request_data
*req
, const char *method
,
189 struct blob_attr
*msg
)
198 return UBUS_STATUS_UNKNOWN_ERROR
;
203 if (!(tm
= localtime(&now
)))
204 return UBUS_STATUS_UNKNOWN_ERROR
;
206 blob_buf_init(&b
, 0);
208 blobmsg_add_u32(&b
, "localtime", now
+ tm
->tm_gmtoff
);
211 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
213 c
= blobmsg_open_array(&b
, "load");
214 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
215 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
216 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
217 blobmsg_close_array(&b
, c
);
219 c
= blobmsg_open_table(&b
, "memory");
220 blobmsg_add_u64(&b
, "total", info
.mem_unit
* info
.totalram
);
221 blobmsg_add_u64(&b
, "free", info
.mem_unit
* info
.freeram
);
222 blobmsg_add_u64(&b
, "shared", info
.mem_unit
* info
.sharedram
);
223 blobmsg_add_u64(&b
, "buffered", info
.mem_unit
* info
.bufferram
);
224 blobmsg_close_table(&b
, c
);
226 c
= blobmsg_open_table(&b
, "swap");
227 blobmsg_add_u64(&b
, "total", info
.mem_unit
* info
.totalswap
);
228 blobmsg_add_u64(&b
, "free", info
.mem_unit
* info
.freeswap
);
229 blobmsg_close_table(&b
, c
);
232 ubus_send_reply(ctx
, req
, b
.head
);
234 return UBUS_STATUS_OK
;
237 static int system_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
238 struct ubus_request_data
*req
, const char *method
,
239 struct blob_attr
*msg
)
241 procd_shutdown(RB_AUTOBOOT
);
252 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
253 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
254 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
255 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
258 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
259 struct ubus_request_data
*req
, const char *method
,
260 struct blob_attr
*msg
)
262 struct blob_attr
*tb
[__WDT_MAX
];
266 return UBUS_STATUS_INVALID_ARGUMENT
;
268 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
269 if (tb
[WDT_FREQUENCY
]) {
270 unsigned int timeout
= watchdog_timeout(0);
271 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
274 if (freq
> timeout
/ 2)
276 watchdog_frequency(freq
);
280 if (tb
[WDT_TIMEOUT
]) {
281 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
282 unsigned int frequency
= watchdog_frequency(0);
284 if (timeout
<= frequency
)
285 timeout
= frequency
* 2;
286 watchdog_timeout(timeout
);
290 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
292 if (watchdog_fd() == NULL
)
294 else if (watchdog_get_stopped())
299 blob_buf_init(&b
, 0);
300 blobmsg_add_string(&b
, "status", status
);
301 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
302 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
303 ubus_send_reply(ctx
, req
, b
.head
);
314 static const struct blobmsg_policy signal_policy
[__SIGNAL_MAX
] = {
315 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
316 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
319 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
320 struct ubus_request_data
*req
, const char *method
,
321 struct blob_attr
*msg
)
323 struct blob_attr
*tb
[__SIGNAL_MAX
];
326 return UBUS_STATUS_INVALID_ARGUMENT
;
328 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
329 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
330 return UBUS_STATUS_INVALID_ARGUMENT
;
332 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
344 static const struct blobmsg_policy sysupgrade_policy
[__SYSUPGRADE_MAX
] = {
345 [SYSUPGRADE_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
346 [SYSUPGRADE_PREFIX
] = { .name
= "prefix", .type
= BLOBMSG_TYPE_STRING
},
347 [SYSUPGRADE_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
350 static int sysupgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
351 struct ubus_request_data
*req
, const char *method
,
352 struct blob_attr
*msg
)
354 struct blob_attr
*tb
[__SYSUPGRADE_MAX
];
357 return UBUS_STATUS_INVALID_ARGUMENT
;
359 blobmsg_parse(sysupgrade_policy
, __SYSUPGRADE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
360 if (!tb
[SYSUPGRADE_PATH
] || !tb
[SYSUPGRADE_PREFIX
])
361 return UBUS_STATUS_INVALID_ARGUMENT
;
363 sysupgrade_exec_upgraded(blobmsg_get_string(tb
[SYSUPGRADE_PREFIX
]),
364 blobmsg_get_string(tb
[SYSUPGRADE_PATH
]),
365 tb
[SYSUPGRADE_COMMAND
] ? blobmsg_get_string(tb
[SYSUPGRADE_COMMAND
]) : NULL
);
370 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
372 notify
= obj
->has_subscribers
;
376 static const struct ubus_method system_methods
[] = {
377 UBUS_METHOD_NOARG("board", system_board
),
378 UBUS_METHOD_NOARG("info", system_info
),
379 UBUS_METHOD_NOARG("reboot", system_reboot
),
380 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
381 UBUS_METHOD("signal", proc_signal
, signal_policy
),
382 UBUS_METHOD("sysupgrade", sysupgrade
, sysupgrade_policy
),
385 static struct ubus_object_type system_object_type
=
386 UBUS_OBJECT_TYPE("system", system_methods
);
388 static struct ubus_object system_object
= {
390 .type
= &system_object_type
,
391 .methods
= system_methods
,
392 .n_methods
= ARRAY_SIZE(system_methods
),
393 .subscribe_cb
= procd_subscribe_cb
,
397 procd_bcast_event(char *event
, struct blob_attr
*msg
)
404 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
406 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
409 void ubus_init_system(struct ubus_context
*ctx
)
414 ret
= ubus_add_object(ctx
, &system_object
);
416 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));