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>
16 #include <sys/sysinfo.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
24 #include <libubox/uloop.h>
29 static struct blob_buf b
;
31 static struct ubus_context
*_ctx
;
33 int upgrade_running
= 0;
35 static int system_board(struct ubus_context
*ctx
, struct ubus_object
*obj
,
36 struct ubus_request_data
*req
, const char *method
,
37 struct blob_attr
*msg
)
41 char *key
, *val
, *next
;
42 struct utsname utsname
;
47 if (uname(&utsname
) >= 0)
49 blobmsg_add_string(&b
, "kernel", utsname
.release
);
50 blobmsg_add_string(&b
, "hostname", utsname
.nodename
);
53 if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
55 while(fgets(line
, sizeof(line
), f
))
57 key
= strtok(line
, "\t:");
58 val
= strtok(NULL
, "\t\n");
63 if (!strcasecmp(key
, "system type") ||
64 !strcasecmp(key
, "processor") ||
65 !strcasecmp(key
, "model name"))
67 blobmsg_add_string(&b
, "system", val
+ 2);
75 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
)
77 if (fgets(line
, sizeof(line
), f
))
79 val
= strtok(line
, "\t\n");
82 blobmsg_add_string(&b
, "model", val
);
87 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
89 while(fgets(line
, sizeof(line
), f
))
91 key
= strtok(line
, "\t:");
92 val
= strtok(NULL
, "\t\n");
97 if (!strcasecmp(key
, "machine") ||
98 !strcasecmp(key
, "hardware"))
100 blobmsg_add_string(&b
, "model", val
+ 2);
108 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
110 c
= blobmsg_open_table(&b
, "release");
112 while (fgets(line
, sizeof(line
), f
))
118 val
= strchr(line
, '=');
124 if (!strcasecmp(key
, "DISTRIB_ID"))
125 key
= "distribution";
126 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
128 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
130 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
132 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
134 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
139 dest
= blobmsg_alloc_string_buffer(&b
, key
, strlen(val
));
140 while (val
&& (ch
= *(val
++)) != 0) {
144 next
= strchr(val
, ch
);
153 dest
+= strlen(dest
);
156 *(dest
++) = *(val
++);
160 blobmsg_add_string_buffer(&b
);
163 blobmsg_close_array(&b
, c
);
168 ubus_send_reply(ctx
, req
, b
.head
);
170 return UBUS_STATUS_OK
;
173 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
174 struct ubus_request_data
*req
, const char *method
,
175 struct blob_attr
*msg
)
184 if (!(tm
= localtime(&now
)))
185 return UBUS_STATUS_UNKNOWN_ERROR
;
188 return UBUS_STATUS_UNKNOWN_ERROR
;
190 blob_buf_init(&b
, 0);
192 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
193 blobmsg_add_u32(&b
, "localtime", mktime(tm
));
195 c
= blobmsg_open_array(&b
, "load");
196 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
197 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
198 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
199 blobmsg_close_array(&b
, c
);
201 c
= blobmsg_open_table(&b
, "memory");
202 blobmsg_add_u32(&b
, "total", info
.mem_unit
* info
.totalram
);
203 blobmsg_add_u32(&b
, "free", info
.mem_unit
* info
.freeram
);
204 blobmsg_add_u32(&b
, "shared", info
.mem_unit
* info
.sharedram
);
205 blobmsg_add_u32(&b
, "buffered", info
.mem_unit
* info
.bufferram
);
206 blobmsg_close_table(&b
, c
);
208 c
= blobmsg_open_table(&b
, "swap");
209 blobmsg_add_u32(&b
, "total", info
.mem_unit
* info
.totalswap
);
210 blobmsg_add_u32(&b
, "free", info
.mem_unit
* info
.freeswap
);
211 blobmsg_close_table(&b
, c
);
213 ubus_send_reply(ctx
, req
, b
.head
);
215 return UBUS_STATUS_OK
;
218 static int system_upgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
219 struct ubus_request_data
*req
, const char *method
,
220 struct blob_attr
*msg
)
233 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
234 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
235 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
236 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
239 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
240 struct ubus_request_data
*req
, const char *method
,
241 struct blob_attr
*msg
)
243 struct blob_attr
*tb
[__WDT_MAX
];
247 return UBUS_STATUS_INVALID_ARGUMENT
;
249 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
250 if (tb
[WDT_FREQUENCY
]) {
251 unsigned int timeout
= watchdog_timeout(0);
252 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
255 if (freq
> timeout
/ 2)
257 watchdog_frequency(freq
);
261 if (tb
[WDT_TIMEOUT
]) {
262 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
263 unsigned int frequency
= watchdog_frequency(0);
265 if (timeout
<= frequency
)
266 timeout
= frequency
* 2;
267 watchdog_timeout(timeout
);
271 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
273 if (watchdog_fd() < 0)
275 else if (watchdog_get_stopped())
280 blob_buf_init(&b
, 0);
281 blobmsg_add_string(&b
, "status", status
);
282 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
283 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
284 ubus_send_reply(ctx
, req
, b
.head
);
295 static const struct blobmsg_policy signal_policy
[__SIGNAL_MAX
] = {
296 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
297 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
300 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
301 struct ubus_request_data
*req
, const char *method
,
302 struct blob_attr
*msg
)
304 struct blob_attr
*tb
[__SIGNAL_MAX
];
307 return UBUS_STATUS_INVALID_ARGUMENT
;
309 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
310 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
311 return UBUS_STATUS_INVALID_ARGUMENT
;
313 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
323 static const struct blobmsg_policy nand_policy
[__NAND_MAX
] = {
324 [NAND_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
328 procd_spawn_upgraded(char *path
)
330 char *wdt_fd
= watchdog_fd();
331 char *argv
[] = { "/tmp/upgraded", NULL
, NULL
};
335 DEBUG(2, "Exec to upgraded now\n");
337 watchdog_no_cloexec();
338 setenv("WDTFD", wdt_fd
, 1);
340 execvp(argv
[0], argv
);
343 static int nand_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
344 struct ubus_request_data
*req
, const char *method
,
345 struct blob_attr
*msg
)
347 struct blob_attr
*tb
[__NAND_MAX
];
350 return UBUS_STATUS_INVALID_ARGUMENT
;
352 blobmsg_parse(nand_policy
, __NAND_MAX
, tb
, blob_data(msg
), blob_len(msg
));
354 return UBUS_STATUS_INVALID_ARGUMENT
;
356 procd_spawn_upgraded(blobmsg_get_string(tb
[NAND_PATH
]));
357 fprintf(stderr
, "Yikees, something went wrong. no /sbin/upgraded ?\n");
362 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
364 notify
= obj
->has_subscribers
;
368 static const struct ubus_method system_methods
[] = {
369 UBUS_METHOD_NOARG("board", system_board
),
370 UBUS_METHOD_NOARG("info", system_info
),
371 UBUS_METHOD_NOARG("upgrade", system_upgrade
),
372 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
373 UBUS_METHOD("signal", proc_signal
, signal_policy
),
375 /* must remain at the end as it ia not always loaded */
376 UBUS_METHOD("nandupgrade", nand_set
, nand_policy
),
379 static struct ubus_object_type system_object_type
=
380 UBUS_OBJECT_TYPE("system", system_methods
);
382 static struct ubus_object system_object
= {
384 .type
= &system_object_type
,
385 .methods
= system_methods
,
386 .n_methods
= ARRAY_SIZE(system_methods
),
387 .subscribe_cb
= procd_subscribe_cb
,
391 procd_bcast_event(char *event
, struct blob_attr
*msg
)
398 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
400 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
403 void ubus_init_system(struct ubus_context
*ctx
)
408 if (stat("/sbin/upgraded", &s
))
409 system_object
.n_methods
-= 1;
412 ret
= ubus_add_object(ctx
, &system_object
);
414 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));