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>
27 #include <libubox/uloop.h>
32 static struct blob_buf b
;
34 static struct ubus_context
*_ctx
;
36 int upgrade_running
= 0;
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", mktime(tm
));
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_upgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
238 struct ubus_request_data
*req
, const char *method
,
239 struct blob_attr
*msg
)
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
]));
342 static const struct blobmsg_policy nand_policy
[__NAND_MAX
] = {
343 [NAND_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
347 procd_spawn_upgraded(char *path
)
349 char *wdt_fd
= watchdog_fd();
350 char *argv
[] = { "/tmp/upgraded", NULL
, NULL
};
354 DEBUG(2, "Exec to upgraded now\n");
356 watchdog_no_cloexec();
357 setenv("WDTFD", wdt_fd
, 1);
359 execvp(argv
[0], argv
);
362 static int nand_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
363 struct ubus_request_data
*req
, const char *method
,
364 struct blob_attr
*msg
)
366 struct blob_attr
*tb
[__NAND_MAX
];
369 return UBUS_STATUS_INVALID_ARGUMENT
;
371 blobmsg_parse(nand_policy
, __NAND_MAX
, tb
, blob_data(msg
), blob_len(msg
));
373 return UBUS_STATUS_INVALID_ARGUMENT
;
375 procd_spawn_upgraded(blobmsg_get_string(tb
[NAND_PATH
]));
376 fprintf(stderr
, "Yikees, something went wrong. no /sbin/upgraded ?\n");
381 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
383 notify
= obj
->has_subscribers
;
387 static const struct ubus_method system_methods
[] = {
388 UBUS_METHOD_NOARG("board", system_board
),
389 UBUS_METHOD_NOARG("info", system_info
),
390 UBUS_METHOD_NOARG("upgrade", system_upgrade
),
391 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
392 UBUS_METHOD("signal", proc_signal
, signal_policy
),
394 /* must remain at the end as it ia not always loaded */
395 UBUS_METHOD("nandupgrade", nand_set
, nand_policy
),
398 static struct ubus_object_type system_object_type
=
399 UBUS_OBJECT_TYPE("system", system_methods
);
401 static struct ubus_object system_object
= {
403 .type
= &system_object_type
,
404 .methods
= system_methods
,
405 .n_methods
= ARRAY_SIZE(system_methods
),
406 .subscribe_cb
= procd_subscribe_cb
,
410 procd_bcast_event(char *event
, struct blob_attr
*msg
)
417 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
419 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
422 void ubus_init_system(struct ubus_context
*ctx
)
427 if (stat("/sbin/upgraded", &s
))
428 system_object
.n_methods
-= 1;
431 ret
= ubus_add_object(ctx
, &system_object
);
433 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));