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 <json-c/json_tokener.h>
29 #include <libubox/blobmsg_json.h>
30 #include <libubox/uloop.h>
33 #include "sysupgrade.h"
36 static struct blob_buf b
;
38 static struct ubus_context
*_ctx
;
40 static int system_board(struct ubus_context
*ctx
, struct ubus_object
*obj
,
41 struct ubus_request_data
*req
, const char *method
,
42 struct blob_attr
*msg
)
46 char *key
, *val
, *next
;
47 struct utsname utsname
;
52 if (uname(&utsname
) >= 0)
54 blobmsg_add_string(&b
, "kernel", utsname
.release
);
55 blobmsg_add_string(&b
, "hostname", utsname
.nodename
);
58 if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
60 while(fgets(line
, sizeof(line
), f
))
62 key
= strtok(line
, "\t:");
63 val
= strtok(NULL
, "\t\n");
68 if (!strcasecmp(key
, "system type") ||
69 !strcasecmp(key
, "processor") ||
70 !strcasecmp(key
, "cpu") ||
71 !strcasecmp(key
, "model name"))
73 strtoul(val
+ 2, &key
, 0);
75 if (key
== (val
+ 2) || *key
!= 0)
77 blobmsg_add_string(&b
, "system", val
+ 2);
86 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
||
87 (f
= fopen("/proc/device-tree/model", "r")) != NULL
)
89 if (fgets(line
, sizeof(line
), f
))
91 val
= strtok(line
, "\t\n");
94 blobmsg_add_string(&b
, "model", val
);
99 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
101 while(fgets(line
, sizeof(line
), f
))
103 key
= strtok(line
, "\t:");
104 val
= strtok(NULL
, "\t\n");
109 if (!strcasecmp(key
, "machine") ||
110 !strcasecmp(key
, "hardware"))
112 blobmsg_add_string(&b
, "model", val
+ 2);
120 if ((f
= fopen("/tmp/sysinfo/board_name", "r")) != NULL
)
122 if (fgets(line
, sizeof(line
), f
))
124 val
= strtok(line
, "\t\n");
127 blobmsg_add_string(&b
, "board_name", val
);
132 else if ((f
= fopen("/proc/device-tree/compatible", "r")) != NULL
)
134 if (fgets(line
, sizeof(line
), f
))
136 val
= strtok(line
, "\t\n");
141 while ((next
= strchr(next
, ',')) != NULL
)
147 blobmsg_add_string(&b
, "board_name", val
);
154 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
156 c
= blobmsg_open_table(&b
, "release");
158 while (fgets(line
, sizeof(line
), f
))
164 val
= strchr(line
, '=');
170 if (!strcasecmp(key
, "DISTRIB_ID"))
171 key
= "distribution";
172 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
174 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
176 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
178 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
180 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
185 dest
= blobmsg_alloc_string_buffer(&b
, key
, strlen(val
));
187 ERROR("Failed to allocate blob.\n");
191 while (val
&& (ch
= *(val
++)) != 0) {
195 next
= strchr(val
, ch
);
204 dest
+= strlen(dest
);
207 *(dest
++) = *(val
++);
211 blobmsg_add_string_buffer(&b
);
214 blobmsg_close_array(&b
, c
);
219 ubus_send_reply(ctx
, req
, b
.head
);
221 return UBUS_STATUS_OK
;
224 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
225 struct ubus_request_data
*req
, const char *method
,
226 struct blob_attr
*msg
)
235 return UBUS_STATUS_UNKNOWN_ERROR
;
240 if (!(tm
= localtime(&now
)))
241 return UBUS_STATUS_UNKNOWN_ERROR
;
243 blob_buf_init(&b
, 0);
245 blobmsg_add_u32(&b
, "localtime", now
+ tm
->tm_gmtoff
);
248 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
250 c
= blobmsg_open_array(&b
, "load");
251 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
252 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
253 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
254 blobmsg_close_array(&b
, c
);
256 c
= blobmsg_open_table(&b
, "memory");
257 blobmsg_add_u64(&b
, "total",
258 (uint64_t)info
.mem_unit
* (uint64_t)info
.totalram
);
259 blobmsg_add_u64(&b
, "free",
260 (uint64_t)info
.mem_unit
* (uint64_t)info
.freeram
);
261 blobmsg_add_u64(&b
, "shared",
262 (uint64_t)info
.mem_unit
* (uint64_t)info
.sharedram
);
263 blobmsg_add_u64(&b
, "buffered",
264 (uint64_t)info
.mem_unit
* (uint64_t)info
.bufferram
);
265 blobmsg_close_table(&b
, c
);
267 c
= blobmsg_open_table(&b
, "swap");
268 blobmsg_add_u64(&b
, "total",
269 (uint64_t)info
.mem_unit
* (uint64_t)info
.totalswap
);
270 blobmsg_add_u64(&b
, "free",
271 (uint64_t)info
.mem_unit
* (uint64_t)info
.freeswap
);
272 blobmsg_close_table(&b
, c
);
275 ubus_send_reply(ctx
, req
, b
.head
);
277 return UBUS_STATUS_OK
;
280 static int system_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
281 struct ubus_request_data
*req
, const char *method
,
282 struct blob_attr
*msg
)
284 procd_shutdown(RB_AUTOBOOT
);
296 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
297 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
298 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
299 [WDT_MAGICCLOSE
] = { .name
= "magicclose", .type
= BLOBMSG_TYPE_BOOL
},
300 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
303 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
304 struct ubus_request_data
*req
, const char *method
,
305 struct blob_attr
*msg
)
307 struct blob_attr
*tb
[__WDT_MAX
];
311 return UBUS_STATUS_INVALID_ARGUMENT
;
313 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
314 if (tb
[WDT_FREQUENCY
]) {
315 unsigned int timeout
= tb
[WDT_TIMEOUT
] ? blobmsg_get_u32(tb
[WDT_TIMEOUT
]) :
317 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
320 if (freq
> timeout
/ 2)
322 watchdog_frequency(freq
);
326 if (tb
[WDT_TIMEOUT
]) {
327 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
328 unsigned int frequency
= watchdog_frequency(0);
330 if (timeout
<= frequency
)
331 timeout
= frequency
* 2;
332 watchdog_timeout(timeout
);
335 if (tb
[WDT_MAGICCLOSE
])
336 watchdog_set_magicclose(blobmsg_get_bool(tb
[WDT_MAGICCLOSE
]));
339 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
341 if (watchdog_fd() == NULL
)
343 else if (watchdog_get_stopped())
348 blob_buf_init(&b
, 0);
349 blobmsg_add_string(&b
, "status", status
);
350 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
351 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
352 blobmsg_add_u8(&b
, "magicclose", watchdog_get_magicclose());
353 ubus_send_reply(ctx
, req
, b
.head
);
364 static const struct blobmsg_policy signal_policy
[__SIGNAL_MAX
] = {
365 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
366 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
369 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
370 struct ubus_request_data
*req
, const char *method
,
371 struct blob_attr
*msg
)
373 struct blob_attr
*tb
[__SIGNAL_MAX
];
376 return UBUS_STATUS_INVALID_ARGUMENT
;
378 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
379 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
380 return UBUS_STATUS_INVALID_ARGUMENT
;
382 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
388 * validate_firmware_image_call - perform validation & store result in global b
390 * @file: firmware image path
392 static int validate_firmware_image_call(const char *file
)
394 const char *path
= "/usr/libexec/validate_firmware_image";
395 json_object
*jsobj
= NULL
;
410 /* Set stdin & stderr to /dev/null */
411 fd
= open("/dev/null", O_RDWR
);
418 /* Set stdout to the shared pipe */
423 execl(path
, path
, file
, NULL
);
429 tok
= json_tokener_new();
436 blob_buf_init(&b
, 0);
437 while ((len
= read(fds
[0], buf
, sizeof(buf
)))) {
438 jsobj
= json_tokener_parse_ex(tok
, buf
, len
);
440 if (json_tokener_get_error(tok
) == json_tokener_success
)
442 else if (json_tokener_get_error(tok
) == json_tokener_continue
)
445 fprintf(stderr
, "Failed to parse JSON: %d\n",
446 json_tokener_get_error(tok
));
454 if (json_object_get_type(jsobj
) == json_type_object
) {
455 blobmsg_add_object(&b
, jsobj
);
459 json_object_put(jsobj
);
462 json_tokener_free(tok
);
468 VALIDATE_FIRMWARE_IMAGE_PATH
,
469 __VALIDATE_FIRMWARE_IMAGE_MAX
,
472 static const struct blobmsg_policy validate_firmware_image_policy
[__VALIDATE_FIRMWARE_IMAGE_MAX
] = {
473 [VALIDATE_FIRMWARE_IMAGE_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
476 static int validate_firmware_image(struct ubus_context
*ctx
,
477 struct ubus_object
*obj
,
478 struct ubus_request_data
*req
,
479 const char *method
, struct blob_attr
*msg
)
481 struct blob_attr
*tb
[__VALIDATE_FIRMWARE_IMAGE_MAX
];
484 return UBUS_STATUS_INVALID_ARGUMENT
;
486 blobmsg_parse(validate_firmware_image_policy
, __VALIDATE_FIRMWARE_IMAGE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
487 if (!tb
[VALIDATE_FIRMWARE_IMAGE_PATH
])
488 return UBUS_STATUS_INVALID_ARGUMENT
;
490 if (validate_firmware_image_call(blobmsg_get_string(tb
[VALIDATE_FIRMWARE_IMAGE_PATH
])))
491 return UBUS_STATUS_UNKNOWN_ERROR
;
493 ubus_send_reply(ctx
, req
, b
.head
);
495 return UBUS_STATUS_OK
;
508 static const struct blobmsg_policy sysupgrade_policy
[__SYSUPGRADE_MAX
] = {
509 [SYSUPGRADE_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
510 [SYSUPGRADE_FORCE
] = { .name
= "force", .type
= BLOBMSG_TYPE_BOOL
},
511 [SYSUPGRADE_BACKUP
] = { .name
= "backup", .type
= BLOBMSG_TYPE_STRING
},
512 [SYSUPGRADE_PREFIX
] = { .name
= "prefix", .type
= BLOBMSG_TYPE_STRING
},
513 [SYSUPGRADE_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
514 [SYSUPGRADE_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_TABLE
},
517 static void sysupgrade_error(struct ubus_context
*ctx
,
518 struct ubus_request_data
*req
,
523 blob_buf_init(&b
, 0);
525 c
= blobmsg_open_table(&b
, "error");
526 blobmsg_add_string(&b
, "message", message
);
527 blobmsg_close_table(&b
, c
);
529 ubus_send_reply(ctx
, req
, b
.head
);
532 static int sysupgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
533 struct ubus_request_data
*req
, const char *method
,
534 struct blob_attr
*msg
)
538 VALIDATION_FORCEABLE
,
539 VALIDATION_ALLOW_BACKUP
,
542 static const struct blobmsg_policy validation_policy
[__VALIDATION_MAX
] = {
543 [VALIDATION_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_BOOL
},
544 [VALIDATION_FORCEABLE
] = { .name
= "forceable", .type
= BLOBMSG_TYPE_BOOL
},
545 [VALIDATION_ALLOW_BACKUP
] = { .name
= "allow_backup", .type
= BLOBMSG_TYPE_BOOL
},
547 struct blob_attr
*validation
[__VALIDATION_MAX
];
548 struct blob_attr
*tb
[__SYSUPGRADE_MAX
];
549 bool valid
, forceable
, allow_backup
;
552 return UBUS_STATUS_INVALID_ARGUMENT
;
554 blobmsg_parse(sysupgrade_policy
, __SYSUPGRADE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
555 if (!tb
[SYSUPGRADE_PATH
] || !tb
[SYSUPGRADE_PREFIX
])
556 return UBUS_STATUS_INVALID_ARGUMENT
;
558 if (validate_firmware_image_call(blobmsg_get_string(tb
[SYSUPGRADE_PATH
]))) {
559 sysupgrade_error(ctx
, req
, "Firmware image couldn't be validated");
560 return UBUS_STATUS_UNKNOWN_ERROR
;
563 blobmsg_parse(validation_policy
, __VALIDATION_MAX
, validation
, blob_data(b
.head
), blob_len(b
.head
));
565 valid
= validation
[VALIDATION_VALID
] && blobmsg_get_bool(validation
[VALIDATION_VALID
]);
566 forceable
= validation
[VALIDATION_FORCEABLE
] && blobmsg_get_bool(validation
[VALIDATION_FORCEABLE
]);
567 allow_backup
= validation
[VALIDATION_ALLOW_BACKUP
] && blobmsg_get_bool(validation
[VALIDATION_ALLOW_BACKUP
]);
571 sysupgrade_error(ctx
, req
, "Firmware image is broken and cannot be installed");
572 return UBUS_STATUS_NOT_SUPPORTED
;
573 } else if (!tb
[SYSUPGRADE_FORCE
] || !blobmsg_get_bool(tb
[SYSUPGRADE_FORCE
])) {
574 sysupgrade_error(ctx
, req
, "Firmware image is invalid");
575 return UBUS_STATUS_NOT_SUPPORTED
;
577 } else if (!allow_backup
&& tb
[SYSUPGRADE_BACKUP
]) {
578 sysupgrade_error(ctx
, req
, "Firmware image doesn't allow preserving a backup");
579 return UBUS_STATUS_NOT_SUPPORTED
;
582 sysupgrade_exec_upgraded(blobmsg_get_string(tb
[SYSUPGRADE_PREFIX
]),
583 blobmsg_get_string(tb
[SYSUPGRADE_PATH
]),
584 tb
[SYSUPGRADE_BACKUP
] ? blobmsg_get_string(tb
[SYSUPGRADE_BACKUP
]) : NULL
,
585 tb
[SYSUPGRADE_COMMAND
] ? blobmsg_get_string(tb
[SYSUPGRADE_COMMAND
]) : NULL
,
586 tb
[SYSUPGRADE_OPTIONS
]);
588 /* sysupgrade_exec_upgraded() will never return unless something has gone wrong */
589 return UBUS_STATUS_UNKNOWN_ERROR
;
593 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
595 notify
= obj
->has_subscribers
;
599 static const struct ubus_method system_methods
[] = {
600 UBUS_METHOD_NOARG("board", system_board
),
601 UBUS_METHOD_NOARG("info", system_info
),
602 UBUS_METHOD_NOARG("reboot", system_reboot
),
603 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
604 UBUS_METHOD("signal", proc_signal
, signal_policy
),
605 UBUS_METHOD("validate_firmware_image", validate_firmware_image
, validate_firmware_image_policy
),
606 UBUS_METHOD("sysupgrade", sysupgrade
, sysupgrade_policy
),
609 static struct ubus_object_type system_object_type
=
610 UBUS_OBJECT_TYPE("system", system_methods
);
612 static struct ubus_object system_object
= {
614 .type
= &system_object_type
,
615 .methods
= system_methods
,
616 .n_methods
= ARRAY_SIZE(system_methods
),
617 .subscribe_cb
= procd_subscribe_cb
,
621 procd_bcast_event(char *event
, struct blob_attr
*msg
)
628 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
630 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
633 void ubus_init_system(struct ubus_context
*ctx
)
638 ret
= ubus_add_object(ctx
, &system_object
);
640 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));