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 unsigned long long available
, cached
;
239 return UBUS_STATUS_UNKNOWN_ERROR
;
241 if ((f
= fopen("/proc/meminfo", "r")) == NULL
)
242 return UBUS_STATUS_UNKNOWN_ERROR
;
244 /* if linux < 3.14 MemAvailable is not in meminfo */
248 while (fgets(line
, sizeof(line
), f
))
250 key
= strtok(line
, " :");
251 val
= strtok(NULL
, " ");
256 if (!strcasecmp(key
, "MemAvailable"))
257 available
= 1024 * atoll(val
);
258 else if (!strcasecmp(key
, "Cached"))
259 cached
= 1024 * atoll(val
);
267 if (!(tm
= localtime(&now
)))
268 return UBUS_STATUS_UNKNOWN_ERROR
;
270 blob_buf_init(&b
, 0);
272 blobmsg_add_u32(&b
, "localtime", now
+ tm
->tm_gmtoff
);
275 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
277 c
= blobmsg_open_array(&b
, "load");
278 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
279 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
280 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
281 blobmsg_close_array(&b
, c
);
283 c
= blobmsg_open_table(&b
, "memory");
284 blobmsg_add_u64(&b
, "total",
285 (uint64_t)info
.mem_unit
* (uint64_t)info
.totalram
);
286 blobmsg_add_u64(&b
, "free",
287 (uint64_t)info
.mem_unit
* (uint64_t)info
.freeram
);
288 blobmsg_add_u64(&b
, "shared",
289 (uint64_t)info
.mem_unit
* (uint64_t)info
.sharedram
);
290 blobmsg_add_u64(&b
, "buffered",
291 (uint64_t)info
.mem_unit
* (uint64_t)info
.bufferram
);
292 blobmsg_add_u64(&b
, "available", available
);
293 blobmsg_add_u64(&b
, "cached", cached
);
294 blobmsg_close_table(&b
, c
);
296 c
= blobmsg_open_table(&b
, "swap");
297 blobmsg_add_u64(&b
, "total",
298 (uint64_t)info
.mem_unit
* (uint64_t)info
.totalswap
);
299 blobmsg_add_u64(&b
, "free",
300 (uint64_t)info
.mem_unit
* (uint64_t)info
.freeswap
);
301 blobmsg_close_table(&b
, c
);
304 ubus_send_reply(ctx
, req
, b
.head
);
306 return UBUS_STATUS_OK
;
309 static int system_reboot(struct ubus_context
*ctx
, struct ubus_object
*obj
,
310 struct ubus_request_data
*req
, const char *method
,
311 struct blob_attr
*msg
)
313 procd_shutdown(RB_AUTOBOOT
);
325 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
326 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
327 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
328 [WDT_MAGICCLOSE
] = { .name
= "magicclose", .type
= BLOBMSG_TYPE_BOOL
},
329 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
332 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
333 struct ubus_request_data
*req
, const char *method
,
334 struct blob_attr
*msg
)
336 struct blob_attr
*tb
[__WDT_MAX
];
340 return UBUS_STATUS_INVALID_ARGUMENT
;
342 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
343 if (tb
[WDT_FREQUENCY
]) {
344 unsigned int timeout
= tb
[WDT_TIMEOUT
] ? blobmsg_get_u32(tb
[WDT_TIMEOUT
]) :
346 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
349 if (freq
> timeout
/ 2)
351 watchdog_frequency(freq
);
355 if (tb
[WDT_TIMEOUT
]) {
356 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
357 unsigned int frequency
= watchdog_frequency(0);
359 if (timeout
<= frequency
)
360 timeout
= frequency
* 2;
361 watchdog_timeout(timeout
);
364 if (tb
[WDT_MAGICCLOSE
])
365 watchdog_set_magicclose(blobmsg_get_bool(tb
[WDT_MAGICCLOSE
]));
368 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
370 if (watchdog_fd() == NULL
)
372 else if (watchdog_get_stopped())
377 blob_buf_init(&b
, 0);
378 blobmsg_add_string(&b
, "status", status
);
379 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
380 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
381 blobmsg_add_u8(&b
, "magicclose", watchdog_get_magicclose());
382 ubus_send_reply(ctx
, req
, b
.head
);
393 static const struct blobmsg_policy signal_policy
[__SIGNAL_MAX
] = {
394 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
395 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
398 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
399 struct ubus_request_data
*req
, const char *method
,
400 struct blob_attr
*msg
)
402 struct blob_attr
*tb
[__SIGNAL_MAX
];
405 return UBUS_STATUS_INVALID_ARGUMENT
;
407 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
408 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
409 return UBUS_STATUS_INVALID_ARGUMENT
;
411 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
417 * validate_firmware_image_call - perform validation & store result in global b
419 * @file: firmware image path
421 static int validate_firmware_image_call(const char *file
)
423 const char *path
= "/usr/libexec/validate_firmware_image";
424 json_object
*jsobj
= NULL
;
441 /* Set stdin & stderr to /dev/null */
442 fd
= open("/dev/null", O_RDWR
);
449 /* Set stdout to the shared pipe */
454 execl(path
, path
, file
, NULL
);
461 tok
= json_tokener_new();
467 blob_buf_init(&b
, 0);
468 while ((len
= read(fds
[0], buf
, sizeof(buf
)))) {
469 if (len
< 0 && errno
== EINTR
)
472 jsobj
= json_tokener_parse_ex(tok
, buf
, len
);
474 if (json_tokener_get_error(tok
) == json_tokener_success
)
476 else if (json_tokener_get_error(tok
) == json_tokener_continue
)
479 fprintf(stderr
, "Failed to parse JSON: %d\n",
480 json_tokener_get_error(tok
));
487 if (json_object_get_type(jsobj
) == json_type_object
) {
488 blobmsg_add_object(&b
, jsobj
);
492 json_object_put(jsobj
);
495 json_tokener_free(tok
);
501 VALIDATE_FIRMWARE_IMAGE_PATH
,
502 __VALIDATE_FIRMWARE_IMAGE_MAX
,
505 static const struct blobmsg_policy validate_firmware_image_policy
[__VALIDATE_FIRMWARE_IMAGE_MAX
] = {
506 [VALIDATE_FIRMWARE_IMAGE_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
509 static int validate_firmware_image(struct ubus_context
*ctx
,
510 struct ubus_object
*obj
,
511 struct ubus_request_data
*req
,
512 const char *method
, struct blob_attr
*msg
)
514 struct blob_attr
*tb
[__VALIDATE_FIRMWARE_IMAGE_MAX
];
517 return UBUS_STATUS_INVALID_ARGUMENT
;
519 blobmsg_parse(validate_firmware_image_policy
, __VALIDATE_FIRMWARE_IMAGE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
520 if (!tb
[VALIDATE_FIRMWARE_IMAGE_PATH
])
521 return UBUS_STATUS_INVALID_ARGUMENT
;
523 if (validate_firmware_image_call(blobmsg_get_string(tb
[VALIDATE_FIRMWARE_IMAGE_PATH
])))
524 return UBUS_STATUS_UNKNOWN_ERROR
;
526 ubus_send_reply(ctx
, req
, b
.head
);
528 return UBUS_STATUS_OK
;
541 static const struct blobmsg_policy sysupgrade_policy
[__SYSUPGRADE_MAX
] = {
542 [SYSUPGRADE_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
543 [SYSUPGRADE_FORCE
] = { .name
= "force", .type
= BLOBMSG_TYPE_BOOL
},
544 [SYSUPGRADE_BACKUP
] = { .name
= "backup", .type
= BLOBMSG_TYPE_STRING
},
545 [SYSUPGRADE_PREFIX
] = { .name
= "prefix", .type
= BLOBMSG_TYPE_STRING
},
546 [SYSUPGRADE_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_STRING
},
547 [SYSUPGRADE_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_TABLE
},
550 static void sysupgrade_error(struct ubus_context
*ctx
,
551 struct ubus_request_data
*req
,
556 blob_buf_init(&b
, 0);
558 c
= blobmsg_open_table(&b
, "error");
559 blobmsg_add_string(&b
, "message", message
);
560 blobmsg_close_table(&b
, c
);
562 ubus_send_reply(ctx
, req
, b
.head
);
565 static int sysupgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
566 struct ubus_request_data
*req
, const char *method
,
567 struct blob_attr
*msg
)
571 VALIDATION_FORCEABLE
,
572 VALIDATION_ALLOW_BACKUP
,
575 static const struct blobmsg_policy validation_policy
[__VALIDATION_MAX
] = {
576 [VALIDATION_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_BOOL
},
577 [VALIDATION_FORCEABLE
] = { .name
= "forceable", .type
= BLOBMSG_TYPE_BOOL
},
578 [VALIDATION_ALLOW_BACKUP
] = { .name
= "allow_backup", .type
= BLOBMSG_TYPE_BOOL
},
580 struct blob_attr
*validation
[__VALIDATION_MAX
];
581 struct blob_attr
*tb
[__SYSUPGRADE_MAX
];
582 bool valid
, forceable
, allow_backup
;
585 return UBUS_STATUS_INVALID_ARGUMENT
;
587 blobmsg_parse(sysupgrade_policy
, __SYSUPGRADE_MAX
, tb
, blob_data(msg
), blob_len(msg
));
588 if (!tb
[SYSUPGRADE_PATH
] || !tb
[SYSUPGRADE_PREFIX
])
589 return UBUS_STATUS_INVALID_ARGUMENT
;
591 if (validate_firmware_image_call(blobmsg_get_string(tb
[SYSUPGRADE_PATH
]))) {
592 sysupgrade_error(ctx
, req
, "Firmware image couldn't be validated");
593 return UBUS_STATUS_UNKNOWN_ERROR
;
596 blobmsg_parse(validation_policy
, __VALIDATION_MAX
, validation
, blob_data(b
.head
), blob_len(b
.head
));
598 valid
= validation
[VALIDATION_VALID
] && blobmsg_get_bool(validation
[VALIDATION_VALID
]);
599 forceable
= validation
[VALIDATION_FORCEABLE
] && blobmsg_get_bool(validation
[VALIDATION_FORCEABLE
]);
600 allow_backup
= validation
[VALIDATION_ALLOW_BACKUP
] && blobmsg_get_bool(validation
[VALIDATION_ALLOW_BACKUP
]);
604 sysupgrade_error(ctx
, req
, "Firmware image is broken and cannot be installed");
605 return UBUS_STATUS_NOT_SUPPORTED
;
606 } else if (!tb
[SYSUPGRADE_FORCE
] || !blobmsg_get_bool(tb
[SYSUPGRADE_FORCE
])) {
607 sysupgrade_error(ctx
, req
, "Firmware image is invalid");
608 return UBUS_STATUS_NOT_SUPPORTED
;
610 } else if (!allow_backup
&& tb
[SYSUPGRADE_BACKUP
]) {
611 sysupgrade_error(ctx
, req
, "Firmware image doesn't allow preserving a backup");
612 return UBUS_STATUS_NOT_SUPPORTED
;
615 sysupgrade_exec_upgraded(blobmsg_get_string(tb
[SYSUPGRADE_PREFIX
]),
616 blobmsg_get_string(tb
[SYSUPGRADE_PATH
]),
617 tb
[SYSUPGRADE_BACKUP
] ? blobmsg_get_string(tb
[SYSUPGRADE_BACKUP
]) : NULL
,
618 tb
[SYSUPGRADE_COMMAND
] ? blobmsg_get_string(tb
[SYSUPGRADE_COMMAND
]) : NULL
,
619 tb
[SYSUPGRADE_OPTIONS
]);
621 /* sysupgrade_exec_upgraded() will never return unless something has gone wrong */
622 return UBUS_STATUS_UNKNOWN_ERROR
;
626 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
628 notify
= obj
->has_subscribers
;
632 static const struct ubus_method system_methods
[] = {
633 UBUS_METHOD_NOARG("board", system_board
),
634 UBUS_METHOD_NOARG("info", system_info
),
635 UBUS_METHOD_NOARG("reboot", system_reboot
),
636 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
637 UBUS_METHOD("signal", proc_signal
, signal_policy
),
638 UBUS_METHOD("validate_firmware_image", validate_firmware_image
, validate_firmware_image_policy
),
639 UBUS_METHOD("sysupgrade", sysupgrade
, sysupgrade_policy
),
642 static struct ubus_object_type system_object_type
=
643 UBUS_OBJECT_TYPE("system", system_methods
);
645 static struct ubus_object system_object
= {
647 .type
= &system_object_type
,
648 .methods
= system_methods
,
649 .n_methods
= ARRAY_SIZE(system_methods
),
650 .subscribe_cb
= procd_subscribe_cb
,
654 procd_bcast_event(char *event
, struct blob_attr
*msg
)
661 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
663 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
666 void ubus_init_system(struct ubus_context
*ctx
)
671 ret
= ubus_add_object(ctx
, &system_object
);
673 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));