dont list services that have no instances
[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 static struct blob_buf b;
31
32 static int system_board(struct ubus_context *ctx, struct ubus_object *obj,
33 struct ubus_request_data *req, const char *method,
34 struct blob_attr *msg)
35 {
36 void *c;
37 char line[256];
38 char *key, *val;
39 struct utsname utsname;
40 FILE *f;
41
42 blob_buf_init(&b, 0);
43
44 if (uname(&utsname) >= 0)
45 {
46 blobmsg_add_string(&b, "kernel", utsname.release);
47 blobmsg_add_string(&b, "hostname", utsname.nodename);
48 }
49
50 if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
51 {
52 while(fgets(line, sizeof(line), f))
53 {
54 key = strtok(line, "\t:");
55 val = strtok(NULL, "\t\n");
56
57 if (!key || !val)
58 continue;
59
60 if (!strcasecmp(key, "system type") ||
61 !strcasecmp(key, "processor") ||
62 !strcasecmp(key, "model name"))
63 {
64 blobmsg_add_string(&b, "system", val + 2);
65 break;
66 }
67 }
68
69 fclose(f);
70 }
71
72 if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL)
73 {
74 if (fgets(line, sizeof(line), f))
75 {
76 val = strtok(line, "\t\n");
77
78 if (val)
79 blobmsg_add_string(&b, "model", val);
80 }
81
82 fclose(f);
83 }
84 else if ((f = fopen("/proc/cpuinfo", "r")) != NULL)
85 {
86 while(fgets(line, sizeof(line), f))
87 {
88 key = strtok(line, "\t:");
89 val = strtok(NULL, "\t\n");
90
91 if (!key || !val)
92 continue;
93
94 if (!strcasecmp(key, "machine") ||
95 !strcasecmp(key, "hardware"))
96 {
97 blobmsg_add_string(&b, "model", val + 2);
98 break;
99 }
100 }
101
102 fclose(f);
103 }
104
105 if ((f = fopen("/etc/openwrt_release", "r")) != NULL)
106 {
107 c = blobmsg_open_table(&b, "release");
108
109 while (fgets(line, sizeof(line), f))
110 {
111 key = strtok(line, "=\"");
112 val = strtok(NULL, "\"\n");
113
114 if (!key || !val)
115 continue;
116
117 if (!strcasecmp(key, "DISTRIB_ID"))
118 blobmsg_add_string(&b, "distribution", val);
119 else if (!strcasecmp(key, "DISTRIB_RELEASE"))
120 blobmsg_add_string(&b, "version", val);
121 else if (!strcasecmp(key, "DISTRIB_REVISION"))
122 blobmsg_add_string(&b, "revision", val);
123 else if (!strcasecmp(key, "DISTRIB_CODENAME"))
124 blobmsg_add_string(&b, "codename", val);
125 else if (!strcasecmp(key, "DISTRIB_TARGET"))
126 blobmsg_add_string(&b, "target", val);
127 else if (!strcasecmp(key, "DISTRIB_DESCRIPTION"))
128 blobmsg_add_string(&b, "description", val);
129 }
130
131 blobmsg_close_array(&b, c);
132
133 fclose(f);
134 }
135
136 ubus_send_reply(ctx, req, b.head);
137
138 return UBUS_STATUS_OK;
139 }
140
141 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
142 struct ubus_request_data *req, const char *method,
143 struct blob_attr *msg)
144 {
145 void *c;
146 time_t now;
147 struct tm *tm;
148 struct sysinfo info;
149
150 now = time(NULL);
151
152 if (!(tm = localtime(&now)))
153 return UBUS_STATUS_UNKNOWN_ERROR;
154
155 if (sysinfo(&info))
156 return UBUS_STATUS_UNKNOWN_ERROR;
157
158 blob_buf_init(&b, 0);
159
160 blobmsg_add_u32(&b, "uptime", info.uptime);
161 blobmsg_add_u32(&b, "localtime", mktime(tm));
162
163 c = blobmsg_open_array(&b, "load");
164 blobmsg_add_u32(&b, NULL, info.loads[0]);
165 blobmsg_add_u32(&b, NULL, info.loads[1]);
166 blobmsg_add_u32(&b, NULL, info.loads[2]);
167 blobmsg_close_array(&b, c);
168
169 c = blobmsg_open_table(&b, "memory");
170 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalram);
171 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeram);
172 blobmsg_add_u32(&b, "shared", info.mem_unit * info.sharedram);
173 blobmsg_add_u32(&b, "buffered", info.mem_unit * info.bufferram);
174 blobmsg_close_table(&b, c);
175
176 c = blobmsg_open_table(&b, "swap");
177 blobmsg_add_u32(&b, "total", info.mem_unit * info.totalswap);
178 blobmsg_add_u32(&b, "free", info.mem_unit * info.freeswap);
179 blobmsg_close_table(&b, c);
180
181 ubus_send_reply(ctx, req, b.head);
182
183 return UBUS_STATUS_OK;
184 }
185
186 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
187 struct ubus_request_data *req, const char *method,
188 struct blob_attr *msg)
189 {
190 procd_reconnect_ubus(0);
191 log_shutdown();
192 hotplug_shutdown();
193
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 static const struct ubus_method system_methods[] = {
261 UBUS_METHOD_NOARG("board", system_board),
262 UBUS_METHOD_NOARG("info", system_info),
263 UBUS_METHOD_NOARG("upgrade", system_upgrade),
264 UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
265 };
266
267 static struct ubus_object_type system_object_type =
268 UBUS_OBJECT_TYPE("system", system_methods);
269
270 static struct ubus_object system_object = {
271 .name = "system",
272 .type = &system_object_type,
273 .methods = system_methods,
274 .n_methods = ARRAY_SIZE(system_methods),
275 };
276
277 void ubus_init_system(struct ubus_context *ctx)
278 {
279 int ret;
280
281 ret = ubus_add_object(ctx, &system_object);
282 if (ret)
283 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
284 }