fix copy paste error
[project/procd.git] / system.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #include <sys/utsname.h>
16 #include <sys/sysinfo.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <unistd.h>
23
24 #include <libubox/uloop.h>
25
26 #include "procd.h"
27 #include "watchdog.h"
28
29 static struct blob_buf b;
30 static int notify;
31 static struct ubus_context *_ctx;
32
33 int upgrade_running = 0;
34
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)
38 {
39 void *c;
40 char line[256];
41 char *key, *val;
42 struct utsname utsname;
43 FILE *f;
44
45 blob_buf_init(&b, 0);
46
47 if (uname(&utsname) >= 0)
48 {
49 blobmsg_add_string(&b, "kernel", utsname.release);
50 blobmsg_add_string(&b, "hostname", utsname.nodename);
51 }
52
53 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
54 {
55 while(fgets(line, sizeof(line), f))
56 {
57 key = strtok(line, "\t:");
58 val = strtok(NULL, "\t\n");
59
60 if (!key || !val)
61 continue;
62
63 if (!strcasecmp(key, "system type") ||
64 !strcasecmp(key, "processor") ||
65 !strcasecmp(key, "model name"))
66 {
67 blobmsg_add_string(&b, "system", val + 2);
68 break;
69 }
70 }
71
72 fclose(f);
73 }
74
75 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
76 {
77 if (fgets(line, sizeof(line), f))
78 {
79 val = strtok(line, "\t\n");
80
81 if (val)
82 blobmsg_add_string(&b, "model", val);
83 }
84
85 fclose(f);
86 }
87 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
88 {
89 while(fgets(line, sizeof(line), f))
90 {
91 key = strtok(line, "\t:");
92 val = strtok(NULL, "\t\n");
93
94 if (!key || !val)
95 continue;
96
97 if (!strcasecmp(key, "machine") ||
98 !strcasecmp(key, "hardware"))
99 {
100 blobmsg_add_string(&b, "model", val + 2);
101 break;
102 }
103 }
104
105 fclose(f);
106 }
107
108 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
109 {
110 c = blobmsg_open_table(&b, "release");
111
112 while (fgets(line, sizeof(line), f))
113 {
114 key = strtok(line, "=\"");
115 val = strtok(NULL, "\"\n");
116
117 if (!key || !val)
118 continue;
119
120 if (!strcasecmp(key, "DISTRIB_ID"))
121 blobmsg_add_string(&b, "distribution", val);
122 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
123 blobmsg_add_string(&b, "version", val);
124 else if (!strcasecmp(key, "DISTRIB_REVISION"))
125 blobmsg_add_string(&b, "revision", val);
126 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
127 blobmsg_add_string(&b, "codename", val);
128 else if (!strcasecmp(key, "DISTRIB_TARGET"))
129 blobmsg_add_string(&b, "target", val);
130 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
131 blobmsg_add_string(&b, "description", val);
132 }
133
134 blobmsg_close_array(&b, c);
135
136 fclose(f);
137 }
138
139 ubus_send_reply(ctx, req, b.head);
140
141 return UBUS_STATUS_OK;
142 }
143
144 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
145 struct ubus_request_data *req, const char *method,
146 struct blob_attr *msg)
147 {
148 void *c;
149 time_t now;
150 struct tm *tm;
151 struct sysinfo info;
152
153 now = time(NULL);
154
155 if (!(tm = localtime(&now)))
156 return UBUS_STATUS_UNKNOWN_ERROR;
157
158 if (sysinfo(&info))
159 return UBUS_STATUS_UNKNOWN_ERROR;
160
161 blob_buf_init(&b, 0);
162
163 blobmsg_add_u32(&b, "uptime", info.uptime);
164 blobmsg_add_u32(&b, "localtime", mktime(tm));
165
166 c = blobmsg_open_array(&b, "load");
167 blobmsg_add_u32(&b, NULL, info.loads[0]);
168 blobmsg_add_u32(&b, NULL, info.loads[1]);
169 blobmsg_add_u32(&b, NULL, info.loads[2]);
170 blobmsg_close_array(&b, c);
171
172 c = blobmsg_open_table(&b, "memory");
173 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalram);
174 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeram);
175 blobmsg_add_u32(&b, "shared", info.mem_unit * info.sharedram);
176 blobmsg_add_u32(&b, "buffered", info.mem_unit * info.bufferram);
177 blobmsg_close_table(&b, c);
178
179 c = blobmsg_open_table(&b, "swap");
180 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalswap);
181 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeswap);
182 blobmsg_close_table(&b, c);
183
184 ubus_send_reply(ctx, req, b.head);
185
186 return UBUS_STATUS_OK;
187 }
188
189 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
190 struct ubus_request_data *req, const char *method,
191 struct blob_attr *msg)
192 {
193 upgrade_running = 1;
194 return 0;
195 }
196
197 enum {
198 WDT_FREQUENCY,
199 WDT_TIMEOUT,
200 WDT_STOP,
201 __WDT_MAX
202 };
203
204 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
205 [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
206 [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
207 [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
208 };
209
210 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
211 struct ubus_request_data *req, const char *method,
212 struct blob_attr *msg)
213 {
214 struct blob_attr *tb[__WDT_MAX];
215 const char *status;
216
217 if (!msg)
218 return UBUS_STATUS_INVALID_ARGUMENT;
219
220 blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
221 if (tb[WDT_FREQUENCY]) {
222 unsigned int timeout = watchdog_timeout(0);
223 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
224
225 if (freq) {
226 if (freq > timeout / 2)
227 freq = timeout / 2;
228 watchdog_frequency(freq);
229 }
230 }
231
232 if (tb[WDT_TIMEOUT]) {
233 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
234 unsigned int frequency = watchdog_frequency(0);
235
236 if (timeout <= frequency)
237 timeout = frequency * 2;
238 watchdog_timeout(timeout);
239 }
240
241 if (tb[WDT_STOP])
242 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
243
244 if (watchdog_fd() < 0)
245 status = "offline";
246 else if (watchdog_get_stopped())
247 status = "stopped";
248 else
249 status = "running";
250
251 blob_buf_init(&b, 0);
252 blobmsg_add_string(&b, "status", status);
253 blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
254 blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
255 ubus_send_reply(ctx, req, b.head);
256
257 return 0;
258 }
259
260 enum {
261 SIGNAL_PID,
262 SIGNAL_NUM,
263 __SIGNAL_MAX
264 };
265
266 static const struct blobmsg_policy signal_policy[__SIGNAL_MAX] = {
267 [SIGNAL_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
268 [SIGNAL_NUM] = { .name = "signum", .type = BLOBMSG_TYPE_INT32 },
269 };
270
271 static int proc_signal(struct ubus_context *ctx, struct ubus_object *obj,
272 struct ubus_request_data *req, const char *method,
273 struct blob_attr *msg)
274 {
275 struct blob_attr *tb[__SIGNAL_MAX];
276
277 if (!msg)
278 return UBUS_STATUS_INVALID_ARGUMENT;
279
280 blobmsg_parse(signal_policy, __SIGNAL_MAX, tb, blob_data(msg), blob_len(msg));
281 if (!tb[SIGNAL_PID || !tb[SIGNAL_NUM]])
282 return UBUS_STATUS_INVALID_ARGUMENT;
283
284 kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
285
286 return 0;
287 }
288
289 static void
290 procd_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
291 {
292 notify = obj->has_subscribers;
293 }
294
295 static const struct ubus_method system_methods[] = {
296 UBUS_METHOD_NOARG("board", system_board),
297 UBUS_METHOD_NOARG("info", system_info),
298 UBUS_METHOD_NOARG("upgrade", system_upgrade),
299 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
300 UBUS_METHOD("signal", proc_signal, signal_policy),
301 };
302
303 static struct ubus_object_type system_object_type =
304 UBUS_OBJECT_TYPE("system", system_methods);
305
306 static struct ubus_object system_object = {
307 .name = "system",
308 .type = &system_object_type,
309 .methods = system_methods,
310 .n_methods = ARRAY_SIZE(system_methods),
311 .subscribe_cb = procd_subscribe_cb,
312 };
313
314 void
315 procd_bcast_event(char *event, struct blob_attr *msg)
316 {
317 int ret;
318
319 if (!notify)
320 return;
321
322 ret = ubus_notify(_ctx, &system_object, event, msg, -1);
323 if (ret)
324 fprintf(stderr, "Failed to notify log: %s\n", ubus_strerror(ret));
325 }
326
327 void ubus_init_system(struct ubus_context *ctx)
328 {
329 int ret;
330
331 _ctx = ctx;
332 ret = ubus_add_object(ctx, &system_object);
333 if (ret)
334 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
335 }