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
||
82 (f
= fopen("/proc/device-tree/model", "r")) != NULL
)
84 if (fgets(line
, sizeof(line
), f
))
86 val
= strtok(line
, "\t\n");
89 blobmsg_add_string(&b
, "model", val
);
94 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
96 while(fgets(line
, sizeof(line
), f
))
98 key
= strtok(line
, "\t:");
99 val
= strtok(NULL
, "\t\n");
104 if (!strcasecmp(key
, "machine") ||
105 !strcasecmp(key
, "hardware"))
107 blobmsg_add_string(&b
, "model", val
+ 2);
115 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
117 c
= blobmsg_open_table(&b
, "release");
119 while (fgets(line
, sizeof(line
), f
))
125 val
= strchr(line
, '=');
131 if (!strcasecmp(key
, "DISTRIB_ID"))
132 key
= "distribution";
133 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
135 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
137 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
139 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
141 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
146 dest
= blobmsg_alloc_string_buffer(&b
, key
, strlen(val
));
148 ERROR("Failed to allocate blob.\n");
152 while (val
&& (ch
= *(val
++)) != 0) {
156 next
= strchr(val
, ch
);
165 dest
+= strlen(dest
);
168 *(dest
++) = *(val
++);
172 blobmsg_add_string_buffer(&b
);
175 blobmsg_close_array(&b
, c
);
180 ubus_send_reply(ctx
, req
, b
.head
);
182 return UBUS_STATUS_OK
;
185 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
186 struct ubus_request_data
*req
, const char *method
,
187 struct blob_attr
*msg
)
196 if (!(tm
= localtime(&now
)))
197 return UBUS_STATUS_UNKNOWN_ERROR
;
200 return UBUS_STATUS_UNKNOWN_ERROR
;
202 blob_buf_init(&b
, 0);
204 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
205 blobmsg_add_u32(&b
, "localtime", mktime(tm
));
207 c
= blobmsg_open_array(&b
, "load");
208 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
209 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
210 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
211 blobmsg_close_array(&b
, c
);
213 c
= blobmsg_open_table(&b
, "memory");
214 blobmsg_add_u64(&b
, "total", info
.mem_unit
* info
.totalram
);
215 blobmsg_add_u64(&b
, "free", info
.mem_unit
* info
.freeram
);
216 blobmsg_add_u64(&b
, "shared", info
.mem_unit
* info
.sharedram
);
217 blobmsg_add_u64(&b
, "buffered", info
.mem_unit
* info
.bufferram
);
218 blobmsg_close_table(&b
, c
);
220 c
= blobmsg_open_table(&b
, "swap");
221 blobmsg_add_u64(&b
, "total", info
.mem_unit
* info
.totalswap
);
222 blobmsg_add_u64(&b
, "free", info
.mem_unit
* info
.freeswap
);
223 blobmsg_close_table(&b
, c
);
225 ubus_send_reply(ctx
, req
, b
.head
);
227 return UBUS_STATUS_OK
;
230 static int system_upgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
231 struct ubus_request_data
*req
, const char *method
,
232 struct blob_attr
*msg
)
245 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
246 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
247 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
248 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
251 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
252 struct ubus_request_data
*req
, const char *method
,
253 struct blob_attr
*msg
)
255 struct blob_attr
*tb
[__WDT_MAX
];
259 return UBUS_STATUS_INVALID_ARGUMENT
;
261 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
262 if (tb
[WDT_FREQUENCY
]) {
263 unsigned int timeout
= watchdog_timeout(0);
264 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
267 if (freq
> timeout
/ 2)
269 watchdog_frequency(freq
);
273 if (tb
[WDT_TIMEOUT
]) {
274 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
275 unsigned int frequency
= watchdog_frequency(0);
277 if (timeout
<= frequency
)
278 timeout
= frequency
* 2;
279 watchdog_timeout(timeout
);
283 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
285 if (watchdog_fd() < 0)
287 else if (watchdog_get_stopped())
292 blob_buf_init(&b
, 0);
293 blobmsg_add_string(&b
, "status", status
);
294 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
295 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
296 ubus_send_reply(ctx
, req
, b
.head
);
307 static const struct blobmsg_policy signal_policy
[__SIGNAL_MAX
] = {
308 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
309 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
312 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
313 struct ubus_request_data
*req
, const char *method
,
314 struct blob_attr
*msg
)
316 struct blob_attr
*tb
[__SIGNAL_MAX
];
319 return UBUS_STATUS_INVALID_ARGUMENT
;
321 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
322 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
323 return UBUS_STATUS_INVALID_ARGUMENT
;
325 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
335 static const struct blobmsg_policy nand_policy
[__NAND_MAX
] = {
336 [NAND_PATH
] = { .name
= "path", .type
= BLOBMSG_TYPE_STRING
},
340 procd_spawn_upgraded(char *path
)
342 char *wdt_fd
= watchdog_fd();
343 char *argv
[] = { "/tmp/upgraded", NULL
, NULL
};
347 DEBUG(2, "Exec to upgraded now\n");
349 watchdog_no_cloexec();
350 setenv("WDTFD", wdt_fd
, 1);
352 execvp(argv
[0], argv
);
355 static int nand_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
356 struct ubus_request_data
*req
, const char *method
,
357 struct blob_attr
*msg
)
359 struct blob_attr
*tb
[__NAND_MAX
];
362 return UBUS_STATUS_INVALID_ARGUMENT
;
364 blobmsg_parse(nand_policy
, __NAND_MAX
, tb
, blob_data(msg
), blob_len(msg
));
366 return UBUS_STATUS_INVALID_ARGUMENT
;
368 procd_spawn_upgraded(blobmsg_get_string(tb
[NAND_PATH
]));
369 fprintf(stderr
, "Yikees, something went wrong. no /sbin/upgraded ?\n");
374 procd_subscribe_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
)
376 notify
= obj
->has_subscribers
;
380 static const struct ubus_method system_methods
[] = {
381 UBUS_METHOD_NOARG("board", system_board
),
382 UBUS_METHOD_NOARG("info", system_info
),
383 UBUS_METHOD_NOARG("upgrade", system_upgrade
),
384 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
385 UBUS_METHOD("signal", proc_signal
, signal_policy
),
387 /* must remain at the end as it ia not always loaded */
388 UBUS_METHOD("nandupgrade", nand_set
, nand_policy
),
391 static struct ubus_object_type system_object_type
=
392 UBUS_OBJECT_TYPE("system", system_methods
);
394 static struct ubus_object system_object
= {
396 .type
= &system_object_type
,
397 .methods
= system_methods
,
398 .n_methods
= ARRAY_SIZE(system_methods
),
399 .subscribe_cb
= procd_subscribe_cb
,
403 procd_bcast_event(char *event
, struct blob_attr
*msg
)
410 ret
= ubus_notify(_ctx
, &system_object
, event
, msg
, -1);
412 fprintf(stderr
, "Failed to notify log: %s\n", ubus_strerror(ret
));
415 void ubus_init_system(struct ubus_context
*ctx
)
420 if (stat("/sbin/upgraded", &s
))
421 system_object
.n_methods
-= 1;
424 ret
= ubus_add_object(ctx
, &system_object
);
426 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));