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 int upgrade_running
= 0;
33 static int system_board(struct ubus_context
*ctx
, struct ubus_object
*obj
,
34 struct ubus_request_data
*req
, const char *method
,
35 struct blob_attr
*msg
)
40 struct utsname utsname
;
45 if (uname(&utsname
) >= 0)
47 blobmsg_add_string(&b
, "kernel", utsname
.release
);
48 blobmsg_add_string(&b
, "hostname", utsname
.nodename
);
51 if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
53 while(fgets(line
, sizeof(line
), f
))
55 key
= strtok(line
, "\t:");
56 val
= strtok(NULL
, "\t\n");
61 if (!strcasecmp(key
, "system type") ||
62 !strcasecmp(key
, "processor") ||
63 !strcasecmp(key
, "model name"))
65 blobmsg_add_string(&b
, "system", val
+ 2);
73 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
)
75 if (fgets(line
, sizeof(line
), f
))
77 val
= strtok(line
, "\t\n");
80 blobmsg_add_string(&b
, "model", val
);
85 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
87 while(fgets(line
, sizeof(line
), f
))
89 key
= strtok(line
, "\t:");
90 val
= strtok(NULL
, "\t\n");
95 if (!strcasecmp(key
, "machine") ||
96 !strcasecmp(key
, "hardware"))
98 blobmsg_add_string(&b
, "model", val
+ 2);
106 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
108 c
= blobmsg_open_table(&b
, "release");
110 while (fgets(line
, sizeof(line
), f
))
112 key
= strtok(line
, "=\"");
113 val
= strtok(NULL
, "\"\n");
118 if (!strcasecmp(key
, "DISTRIB_ID"))
119 blobmsg_add_string(&b
, "distribution", val
);
120 else if (!strcasecmp(key
, "DISTRIB_RELEASE"))
121 blobmsg_add_string(&b
, "version", val
);
122 else if (!strcasecmp(key
, "DISTRIB_REVISION"))
123 blobmsg_add_string(&b
, "revision", val
);
124 else if (!strcasecmp(key
, "DISTRIB_CODENAME"))
125 blobmsg_add_string(&b
, "codename", val
);
126 else if (!strcasecmp(key
, "DISTRIB_TARGET"))
127 blobmsg_add_string(&b
, "target", val
);
128 else if (!strcasecmp(key
, "DISTRIB_DESCRIPTION"))
129 blobmsg_add_string(&b
, "description", val
);
132 blobmsg_close_array(&b
, c
);
137 ubus_send_reply(ctx
, req
, b
.head
);
139 return UBUS_STATUS_OK
;
142 static int system_info(struct ubus_context
*ctx
, struct ubus_object
*obj
,
143 struct ubus_request_data
*req
, const char *method
,
144 struct blob_attr
*msg
)
153 if (!(tm
= localtime(&now
)))
154 return UBUS_STATUS_UNKNOWN_ERROR
;
157 return UBUS_STATUS_UNKNOWN_ERROR
;
159 blob_buf_init(&b
, 0);
161 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
162 blobmsg_add_u32(&b
, "localtime", mktime(tm
));
164 c
= blobmsg_open_array(&b
, "load");
165 blobmsg_add_u32(&b
, NULL
, info
.loads
[0]);
166 blobmsg_add_u32(&b
, NULL
, info
.loads
[1]);
167 blobmsg_add_u32(&b
, NULL
, info
.loads
[2]);
168 blobmsg_close_array(&b
, c
);
170 c
= blobmsg_open_table(&b
, "memory");
171 blobmsg_add_u32(&b
, "total", info
.mem_unit
* info
.totalram
);
172 blobmsg_add_u32(&b
, "free", info
.mem_unit
* info
.freeram
);
173 blobmsg_add_u32(&b
, "shared", info
.mem_unit
* info
.sharedram
);
174 blobmsg_add_u32(&b
, "buffered", info
.mem_unit
* info
.bufferram
);
175 blobmsg_close_table(&b
, c
);
177 c
= blobmsg_open_table(&b
, "swap");
178 blobmsg_add_u32(&b
, "total", info
.mem_unit
* info
.totalswap
);
179 blobmsg_add_u32(&b
, "free", info
.mem_unit
* info
.freeswap
);
180 blobmsg_close_table(&b
, c
);
182 ubus_send_reply(ctx
, req
, b
.head
);
184 return UBUS_STATUS_OK
;
187 static int system_upgrade(struct ubus_context
*ctx
, struct ubus_object
*obj
,
188 struct ubus_request_data
*req
, const char *method
,
189 struct blob_attr
*msg
)
202 static const struct blobmsg_policy watchdog_policy
[__WDT_MAX
] = {
203 [WDT_FREQUENCY
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
204 [WDT_TIMEOUT
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
205 [WDT_STOP
] = { .name
= "stop", .type
= BLOBMSG_TYPE_BOOL
},
208 static int watchdog_set(struct ubus_context
*ctx
, struct ubus_object
*obj
,
209 struct ubus_request_data
*req
, const char *method
,
210 struct blob_attr
*msg
)
212 struct blob_attr
*tb
[__WDT_MAX
];
216 return UBUS_STATUS_INVALID_ARGUMENT
;
218 blobmsg_parse(watchdog_policy
, __WDT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
219 if (tb
[WDT_FREQUENCY
]) {
220 unsigned int timeout
= watchdog_timeout(0);
221 unsigned int freq
= blobmsg_get_u32(tb
[WDT_FREQUENCY
]);
224 if (freq
> timeout
/ 2)
226 watchdog_frequency(freq
);
230 if (tb
[WDT_TIMEOUT
]) {
231 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
232 unsigned int frequency
= watchdog_frequency(0);
234 if (timeout
<= frequency
)
235 timeout
= frequency
* 2;
236 watchdog_timeout(timeout
);
240 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
242 if (watchdog_fd() < 0)
244 else if (watchdog_get_stopped())
249 blob_buf_init(&b
, 0);
250 blobmsg_add_string(&b
, "status", status
);
251 blobmsg_add_u32(&b
, "timeout", watchdog_timeout(0));
252 blobmsg_add_u32(&b
, "frequency", watchdog_frequency(0));
253 ubus_send_reply(ctx
, req
, b
.head
);
264 static const struct blobmsg_policy signal_policy
[__WDT_MAX
] = {
265 [SIGNAL_PID
] = { .name
= "pid", .type
= BLOBMSG_TYPE_INT32
},
266 [SIGNAL_NUM
] = { .name
= "signum", .type
= BLOBMSG_TYPE_INT32
},
269 static int proc_signal(struct ubus_context
*ctx
, struct ubus_object
*obj
,
270 struct ubus_request_data
*req
, const char *method
,
271 struct blob_attr
*msg
)
273 struct blob_attr
*tb
[__SIGNAL_MAX
];
276 return UBUS_STATUS_INVALID_ARGUMENT
;
278 blobmsg_parse(signal_policy
, __SIGNAL_MAX
, tb
, blob_data(msg
), blob_len(msg
));
279 if (!tb
[SIGNAL_PID
|| !tb
[SIGNAL_NUM
]])
280 return UBUS_STATUS_INVALID_ARGUMENT
;
282 kill(blobmsg_get_u32(tb
[SIGNAL_PID
]), blobmsg_get_u32(tb
[SIGNAL_NUM
]));
287 static const struct ubus_method system_methods
[] = {
288 UBUS_METHOD_NOARG("board", system_board
),
289 UBUS_METHOD_NOARG("info", system_info
),
290 UBUS_METHOD_NOARG("upgrade", system_upgrade
),
291 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
292 UBUS_METHOD("signal", proc_signal
, signal_policy
),
295 static struct ubus_object_type system_object_type
=
296 UBUS_OBJECT_TYPE("system", system_methods
);
298 static struct ubus_object system_object
= {
300 .type
= &system_object_type
,
301 .methods
= system_methods
,
302 .n_methods
= ARRAY_SIZE(system_methods
),
305 void ubus_init_system(struct ubus_context
*ctx
)
309 ret
= ubus_add_object(ctx
, &system_object
);
311 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));