make the service running trigger be queued directly after the service was startetd
[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
31 int upgrade_running = 0;
32
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)
36 {
37         void *c;
38         char line[256];
39         char *key, *val;
40         struct utsname utsname;
41         FILE *f;
42
43         blob_buf_init(&b, 0);
44
45         if (uname(&utsname) >= 0)
46         {
47                 blobmsg_add_string(&b, "kernel", utsname.release);
48                 blobmsg_add_string(&b, "hostname", utsname.nodename);
49         }
50
51         if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
52         {
53                 while(fgets(line, sizeof(line), f))
54                 {
55                         key = strtok(line, "\t:");
56                         val = strtok(NULL, "\t\n");
57
58                         if (!key || !val)
59                                 continue;
60
61                         if (!strcasecmp(key, "system type") ||
62                             !strcasecmp(key, "processor") ||
63                             !strcasecmp(key, "model name"))
64                         {
65                                 blobmsg_add_string(&b, "system", val + 2);
66                                 break;
67                         }
68                 }
69
70                 fclose(f);
71         }
72
73         if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
74         {
75                 if (fgets(line, sizeof(line), f))
76                 {
77                         val = strtok(line, "\t\n");
78
79                         if (val)
80                                 blobmsg_add_string(&b, "model", val);
81                 }
82
83                 fclose(f);
84         }
85         else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
86         {
87                 while(fgets(line, sizeof(line), f))
88                 {
89                         key = strtok(line, "\t:");
90                         val = strtok(NULL, "\t\n");
91
92                         if (!key || !val)
93                                 continue;
94
95                         if (!strcasecmp(key, "machine") ||
96                             !strcasecmp(key, "hardware"))
97                         {
98                                 blobmsg_add_string(&b, "model", val + 2);
99                                 break;
100                         }
101                 }
102
103                 fclose(f);
104         }
105
106         if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
107         {
108                 c = blobmsg_open_table(&b, "release");
109
110                 while (fgets(line, sizeof(line), f))
111                 {
112                         key = strtok(line, "=\"");
113                         val = strtok(NULL, "\"\n");
114
115                         if (!key || !val)
116                                 continue;
117
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);
130                 }
131
132                 blobmsg_close_array(&b, c);
133
134                 fclose(f);
135         }
136
137         ubus_send_reply(ctx, req, b.head);
138
139         return UBUS_STATUS_OK;
140 }
141
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)
145 {
146         void *c;
147         time_t now;
148         struct tm *tm;
149         struct sysinfo info;
150
151         now = time(NULL);
152
153         if (!(tm = localtime(&now)))
154                 return UBUS_STATUS_UNKNOWN_ERROR;
155
156         if (sysinfo(&info))
157                 return UBUS_STATUS_UNKNOWN_ERROR;
158
159         blob_buf_init(&b, 0);
160
161         blobmsg_add_u32(&b, "uptime",    info.uptime);
162         blobmsg_add_u32(&b, "localtime", mktime(tm));
163
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);
169
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);
176
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);
181
182         ubus_send_reply(ctx, req, b.head);
183
184         return UBUS_STATUS_OK;
185 }
186
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)
190 {
191         upgrade_running = 1;
192         return 0;
193 }
194
195 enum {
196         WDT_FREQUENCY,
197         WDT_TIMEOUT,
198         WDT_STOP,
199         __WDT_MAX
200 };
201
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 },
206 };
207
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)
211 {
212         struct blob_attr *tb[__WDT_MAX];
213         const char *status;
214
215         if (!msg)
216                 return UBUS_STATUS_INVALID_ARGUMENT;
217
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]);
222
223                 if (freq) {
224                         if (freq > timeout / 2)
225                                 freq = timeout / 2;
226                         watchdog_frequency(freq);
227                 }
228         }
229
230         if (tb[WDT_TIMEOUT]) {
231                 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
232                 unsigned int frequency = watchdog_frequency(0);
233
234                 if (timeout <= frequency)
235                         timeout = frequency * 2;
236                  watchdog_timeout(timeout);
237         }
238
239         if (tb[WDT_STOP])
240                 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
241
242         if (watchdog_fd() < 0)
243                 status = "offline";
244         else if (watchdog_get_stopped())
245                 status = "stopped";
246         else
247                 status = "running";
248
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);
254
255         return 0;
256 }
257
258 enum {
259         SIGNAL_PID,
260         SIGNAL_NUM,
261         __SIGNAL_MAX
262 };
263
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 },
267 };
268
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)
272 {
273         struct blob_attr *tb[__SIGNAL_MAX];
274
275         if (!msg)
276                 return UBUS_STATUS_INVALID_ARGUMENT;
277
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;
281
282         kill(blobmsg_get_u32(tb[SIGNAL_PID]), blobmsg_get_u32(tb[SIGNAL_NUM]));
283
284         return 0;
285 }
286
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),
293 };
294
295 static struct ubus_object_type system_object_type =
296         UBUS_OBJECT_TYPE("system", system_methods);
297
298 static struct ubus_object system_object = {
299         .name = "system",
300         .type = &system_object_type,
301         .methods = system_methods,
302         .n_methods = ARRAY_SIZE(system_methods),
303 };
304
305 void ubus_init_system(struct ubus_context *ctx)
306 {
307         int ret;
308
309         ret = ubus_add_object(ctx, &system_object);
310         if (ret)
311                 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
312 }