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