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>
25 #include <libubox/uloop.h>
30 static struct blob_buf b
;
32 static struct ubus_context
*_ctx
;
34 int upgrade_running
= 0;
36 static int system_board(struct ubus_context
*ctx
, struct ubus_object
*obj
,
37 struct ubus_request_data
*req
, const char *method
,
38 struct blob_attr
*msg
)
42 char *key
, *val
, *next
;
43 struct utsname utsname
;
48 if (uname(&utsname
) >= 0)
50 blobmsg_add_string(&b
, "kernel", utsname
.release
);
51 blobmsg_add_string(&b
, "hostname", utsname
.nodename
);
54 if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
56 while(fgets(line
, sizeof(line
), f
))
58 key
= strtok(line
, "\t:");
59 val
= strtok(NULL
, "\t\n");
64 if (!strcasecmp(key
, "system type") ||
65 !strcasecmp(key
, "processor") ||
66 !strcasecmp(key
, "model name"))
68 strtoul(val
+ 2, &key
, 0);
70 if (key
== (val
+ 2) || *key
!= 0)
72 blobmsg_add_string(&b
, "system", val
+ 2);
81 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
)
83 if (fgets(line
, sizeof(line
), f
))
85 val
= strtok(line
, "\t\n");
88 blobmsg_add_string(&b
, "model", val
);
93 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
95 while(fgets(line
, sizeof(line
), f
))
97 key
= strtok(line
, "\t:");
98 val
= strtok(NULL
, "\t\n");
103 if (!strcasecmp(key
, "machine") ||
104 !strcasecmp(key
, "hardware"))
106 blobmsg_add_string(&b
, "model", val
+ 2);
114 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
116 c
= blobmsg_open_table(&b
, "release");
118 while (fgets(line
, sizeof(line
), f
))
124 val
= strchr(line
, '=');
130 if (!strcasecmp(key
, "DISTRIB_ID"))
131 key
= "distribution";
132 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
134 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
136 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
138 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
140 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
145 dest
= blobmsg_alloc_string_buffer(&b
, key
, strlen(val
));
146 while (val
&& (ch
= *(val
++)) != 0) {
150 next
= strchr(val
, ch
);
159 dest
+= strlen(dest
);
162 *(dest
++) = *(val
++);
166 blobmsg_add_string_buffer(&b
);
169 blobmsg_close_array(&b
, c
);
174 ubus_send_reply(ctx
, req
, b
.head
);
176 return UBUS_STATUS_OK
;
179 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
180 struct ubus_request_data
*req
, const char *method
,
181 struct blob_attr
*msg
)
190 if (!(tm
= localtime(&now
)))
191 return UBUS_STATUS_UNKNOWN_ERROR
;
194 return UBUS_STATUS_UNKNOWN_ERROR
;
196 blob_buf_init(&b
, 0);
198 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
199 blobmsg_add_u32(&b
, "localtime", mktime(tm
));
201 c
= blobmsg_open_array(&b
, "load");
202 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
203 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
204 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
205 blobmsg_close_array(&b
, c
);
207 c
= blobmsg_open_table(&b
, "memory");
208 blobmsg_add_u32(&b
, "total", info
.mem_unit
* info
.totalram
);
209 blobmsg_add_u32(&b
, "free", info
.mem_unit
* info
.freeram
);
210 blobmsg_add_u32(&b
, "shared", info
.mem_unit
* info
.sharedram
);
211 blobmsg_add_u32(&b
, "buffered", info
.mem_unit
* info
.bufferram
);
212 blobmsg_close_table(&b
, c
);
214 c
= blobmsg_open_table(&b
, "swap");
215 blobmsg_add_u32(&b
, "total", info
.mem_unit
* info
.totalswap
);
216 blobmsg_add_u32(&b
, "free", info
.mem_unit
* info
.freeswap
);
217 blobmsg_close_table(&b
, c
);
219 ubus_send_reply(ctx
, req
, b
.head
);
221 return UBUS_STATUS_OK
;
224 static int system_upgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
225 struct ubus_request_data
*req
, const char *method
,
226 struct blob_attr
*msg
)
239 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
240 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
241 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
242 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
245 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
246 struct ubus_request_data
*req
, const char *method
,
247 struct blob_attr
*msg
)
249 struct blob_attr
*tb
[__WDT_MAX
];
253 return UBUS_STATUS_INVALID_ARGUMENT
;
255 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
256 if (tb
[WDT_FREQUENCY
]) {
257 unsigned int timeout
= watchdog_timeout(0);
258 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
261 if (freq
> timeout
/ 2)
263 watchdog_frequency(freq
);
267 if (tb
[WDT_TIMEOUT
]) {
268 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
269 unsigned int frequency
= watchdog_frequency(0);
271 if (timeout
<= frequency
)
272 timeout
= frequency
* 2;
273 watchdog_timeout(timeout
);
277 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
279 if (watchdog_fd() < 0)
281 else if (watchdog_get_stopped())
286 blob_buf_init(&b
, 0);
287 blobmsg_add_string(&b
, "status", status
);
288 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
289 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
290 ubus_send_reply(ctx
, req
, b
.head
);
301 static const struct blobmsg_policy signal_policy
[__SIGNAL_MAX
] = {
302 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
303 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
306 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
307 struct ubus_request_data
*req
, const char *method
,
308 struct blob_attr
*msg
)
310 struct blob_attr
*tb
[__SIGNAL_MAX
];
313 return UBUS_STATUS_INVALID_ARGUMENT
;
315 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
316 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
317 return UBUS_STATUS_INVALID_ARGUMENT
;
319 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
329 static const struct blobmsg_policy nand_policy
[__NAND_MAX
] = {
330 [NAND_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
334 procd_spawn_upgraded(char *path
)
336 char *wdt_fd
= watchdog_fd();
337 char *argv
[] = { "/tmp/upgraded", NULL
, NULL
};
341 DEBUG(2, "Exec to upgraded now\n");
343 watchdog_no_cloexec();
344 setenv("WDTFD", wdt_fd
, 1);
346 execvp(argv
[0], argv
);
349 static int nand_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
350 struct ubus_request_data
*req
, const char *method
,
351 struct blob_attr
*msg
)
353 struct blob_attr
*tb
[__NAND_MAX
];
356 return UBUS_STATUS_INVALID_ARGUMENT
;
358 blobmsg_parse(nand_policy
, __NAND_MAX
, tb
, blob_data(msg
), blob_len(msg
));
360 return UBUS_STATUS_INVALID_ARGUMENT
;
362 procd_spawn_upgraded(blobmsg_get_string(tb
[NAND_PATH
]));
363 fprintf(stderr
, "Yikees, something went wrong. no /sbin/upgraded ?\n");
368 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
370 notify
= obj
->has_subscribers
;
374 static const struct ubus_method system_methods
[] = {
375 UBUS_METHOD_NOARG("board", system_board
),
376 UBUS_METHOD_NOARG("info", system_info
),
377 UBUS_METHOD_NOARG("upgrade", system_upgrade
),
378 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
379 UBUS_METHOD("signal", proc_signal
, signal_policy
),
381 /* must remain at the end as it ia not always loaded */
382 UBUS_METHOD("nandupgrade", nand_set
, nand_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 if (stat("/sbin/upgraded", &s
))
415 system_object
.n_methods
-= 1;
418 ret
= ubus_add_object(ctx
, &system_object
);
420 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));