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>
30 #define HOSTNAME_PATH "/proc/sys/kernel/hostname"
32 static struct blob_buf b
;
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
)
41 struct utsname utsname
;
46 if (uname(&utsname
) >= 0)
48 blobmsg_add_string(&b
, "kernel", utsname
.release
);
49 blobmsg_add_string(&b
, "hostname", utsname
.nodename
);
52 if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
54 while(fgets(line
, sizeof(line
), f
))
56 key
= strtok(line
, "\t:");
57 val
= strtok(NULL
, "\t\n");
62 if (!strcasecmp(key
, "system type") ||
63 !strcasecmp(key
, "processor") ||
64 !strcasecmp(key
, "model name"))
66 blobmsg_add_string(&b
, "system", val
+ 2);
74 if ((f
= fopen("/tmp/sysinfo/model", "r")) != NULL
)
76 if (fgets(line
, sizeof(line
), f
))
78 val
= strtok(line
, "\t\n");
81 blobmsg_add_string(&b
, "model", val
);
86 else if ((f
= fopen("/proc/cpuinfo", "r")) != NULL
)
88 while(fgets(line
, sizeof(line
), f
))
90 key
= strtok(line
, "\t:");
91 val
= strtok(NULL
, "\t\n");
96 if (!strcasecmp(key
, "machine") ||
97 !strcasecmp(key
, "hardware"))
99 blobmsg_add_string(&b
, "model", val
+ 2);
107 if ((f
= fopen("/etc/openwrt_release", "r")) != NULL
)
109 c
= blobmsg_open_table(&b
, "release");
111 while (fgets(line
, sizeof(line
), f
))
113 key
= strtok(line
, "=\"");
114 val
= strtok(NULL
, "\"\n");
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
);
133 blobmsg_close_array(&b
, c
);
138 ubus_send_reply(ctx
, req
, b
.head
);
140 return UBUS_STATUS_OK
;
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
)
154 if (!(tm
= localtime(&now
)))
155 return UBUS_STATUS_UNKNOWN_ERROR
;
158 return UBUS_STATUS_UNKNOWN_ERROR
;
160 blob_buf_init(&b
, 0);
162 blobmsg_add_u32(&b
, "uptime", info
.uptime
);
163 blobmsg_add_u32(&b
, "localtime", mktime(tm
));
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
);
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
);
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
);
183 ubus_send_reply(ctx
, req
, b
.head
);
185 return UBUS_STATUS_OK
;
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
)
192 procd_reconnect_ubus(0);
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
},
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
)
216 struct blob_attr
*tb
[__WDT_MAX
];
220 return UBUS_STATUS_INVALID_ARGUMENT
;
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
]);
228 if (freq
> timeout
/ 2)
230 watchdog_frequency(freq
);
234 if (tb
[WDT_TIMEOUT
]) {
235 unsigned int timeout
= blobmsg_get_u32(tb
[WDT_TIMEOUT
]);
236 unsigned int frequency
= watchdog_frequency(0);
238 if (timeout
<= frequency
)
239 timeout
= frequency
* 2;
240 watchdog_timeout(timeout
);
244 watchdog_set_stopped(blobmsg_get_bool(tb
[WDT_STOP
]));
246 if (watchdog_fd() < 0)
248 else if (watchdog_get_stopped())
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
);
268 static const struct blobmsg_policy event_policy
[__WDT_MAX
] = {
269 [EVENT_TYPE
] = { .name
= "frequency", .type
= BLOBMSG_TYPE_INT32
},
270 [EVENT_DATA
] = { .name
= "timeout", .type
= BLOBMSG_TYPE_INT32
},
273 static int system_event(struct ubus_context
*ctx
, struct ubus_object
*obj
,
274 struct ubus_request_data
*req
, const char *method
,
275 struct blob_attr
*msg
)
277 struct blob_attr
*tb
[__EVENT_MAX
];
280 return UBUS_STATUS_INVALID_ARGUMENT
;
282 blobmsg_parse(event_policy
, __EVENT_MAX
, tb
, blob_data(msg
), blob_len(msg
));
284 return UBUS_STATUS_INVALID_ARGUMENT
;
286 fprintf(stderr
, "%s\n", blobmsg_get_string(tb
[EVENT_TYPE
]));
291 static const struct ubus_method system_methods
[] = {
292 UBUS_METHOD_NOARG("board", system_board
),
293 UBUS_METHOD_NOARG("info", system_info
),
294 UBUS_METHOD_NOARG("upgrade", system_upgrade
),
295 UBUS_METHOD("watchdog", watchdog_set
, watchdog_policy
),
296 UBUS_METHOD("event", system_event
, event_policy
),
299 static struct ubus_object_type system_object_type
=
300 UBUS_OBJECT_TYPE("system", system_methods
);
302 static struct ubus_object system_object
= {
304 .type
= &system_object_type
,
305 .methods
= system_methods
,
306 .n_methods
= ARRAY_SIZE(system_methods
),
309 void ubus_init_system(struct ubus_context
*ctx
)
313 ret
= ubus_add_object(ctx
, &system_object
);
315 ERROR("Failed to add object: %s\n", ubus_strerror(ret
));