fix 32 wrap around bug when handling 64 bit time values
[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/ioctl.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20
21 #include <unistd.h>
22
23 #include <libubox/uloop.h>
24
25 #include "procd.h"
26 #include "watchdog.h"
27 #include "hotplug.h"
28
29 #define HOSTNAME_PATH   "/proc/sys/kernel/hostname"
30
31 static struct blob_buf b;
32 static char *board_name;
33 static char *board_model;
34
35 static int system_info(struct ubus_context *ctx, struct ubus_object *obj,
36                         struct ubus_request_data *req, const char *method,
37                         struct blob_attr *msg)
38 {
39         struct timespec ts;
40         struct utsname uts;
41
42         blob_buf_init(&b, 0);
43         if (board_name && board_model) {
44                 blobmsg_add_string(&b, "boardname", board_name);
45                 blobmsg_add_string(&b, "boardmodel", board_model);
46         }
47         if (!uname(&uts)) {
48                 blobmsg_add_string(&b, "hostname", uts.nodename);
49                 blobmsg_add_string(&b, "machine", uts.machine);
50                 blobmsg_add_string(&b, "kernel", uts.release);
51         }
52         if (!clock_gettime(CLOCK_MONOTONIC, &ts))
53                 blobmsg_add_u32(&b, "uptime", ts.tv_sec);
54         ubus_send_reply(ctx, req, b.head);
55
56         return 0;
57 }
58
59 static int system_upgrade(struct ubus_context *ctx, struct ubus_object *obj,
60                         struct ubus_request_data *req, const char *method,
61                         struct blob_attr *msg)
62 {
63         procd_reconnect_ubus(0);
64         log_shutdown();
65         hotplug_shutdown();
66
67         return 0;
68 }
69
70 enum {
71         WDT_FREQUENCY,
72         WDT_TIMEOUT,
73         WDT_STOP,
74         __WDT_MAX
75 };
76
77 static const struct blobmsg_policy watchdog_policy[__WDT_MAX] = {
78         [WDT_FREQUENCY] = { .name = "frequency", .type = BLOBMSG_TYPE_INT32 },
79         [WDT_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
80         [WDT_STOP] = { .name = "stop", .type = BLOBMSG_TYPE_BOOL },
81 };
82
83 static int watchdog_set(struct ubus_context *ctx, struct ubus_object *obj,
84                         struct ubus_request_data *req, const char *method,
85                         struct blob_attr *msg)
86 {
87         struct blob_attr *tb[__WDT_MAX];
88         const char *status;
89
90         if (!msg)
91                 return UBUS_STATUS_INVALID_ARGUMENT;
92
93         blobmsg_parse(watchdog_policy, __WDT_MAX, tb, blob_data(msg), blob_len(msg));
94         if (tb[WDT_FREQUENCY]) {
95                 unsigned int timeout = watchdog_timeout(0);
96                 unsigned int freq = blobmsg_get_u32(tb[WDT_FREQUENCY]);
97
98                 if (freq) {
99                         if (freq > timeout / 2)
100                                 freq = timeout / 2;
101                         watchdog_frequency(freq);
102                 }
103         }
104
105         if (tb[WDT_TIMEOUT]) {
106                 unsigned int timeout = blobmsg_get_u32(tb[WDT_TIMEOUT]);
107                 unsigned int frequency = watchdog_frequency(0);
108
109                 if (timeout <= frequency)
110                         timeout = frequency * 2;
111                  watchdog_timeout(timeout);
112         }
113
114         if (tb[WDT_STOP])
115                 watchdog_set_stopped(blobmsg_get_bool(tb[WDT_STOP]));
116
117         if (watchdog_fd() < 0)
118                 status = "offline";
119         else if (watchdog_get_stopped())
120                 status = "stopped";
121         else
122                 status = "running";
123
124         blob_buf_init(&b, 0);
125         blobmsg_add_string(&b, "status", status);
126         blobmsg_add_u32(&b, "timeout", watchdog_timeout(0));
127         blobmsg_add_u32(&b, "frequency", watchdog_frequency(0));
128         ubus_send_reply(ctx, req, b.head);
129
130         return 0;
131 }
132
133 static const struct ubus_method system_methods[] = {
134         UBUS_METHOD_NOARG("info", system_info),
135         UBUS_METHOD_NOARG("upgrade", system_upgrade),
136         UBUS_METHOD("watchdog", watchdog_set, watchdog_policy),
137 };
138
139 static struct ubus_object_type system_object_type =
140         UBUS_OBJECT_TYPE("system", system_methods);
141
142 static struct ubus_object system_object = {
143         .name = "system",
144         .type = &system_object_type,
145         .methods = system_methods,
146         .n_methods = ARRAY_SIZE(system_methods),
147 };
148
149 static char* load_file_content(const char *file)
150 {
151         char buf[32];
152         int fd, r;
153
154         fd = open(file, O_RDONLY);
155         if (!fd)
156                 return NULL;
157         r = read(fd, buf, sizeof(buf) - 1);
158         close(fd);
159         if (r < 1)
160                 return NULL;
161         if (buf[r - 1] == '\n')
162                 buf[r - 1] = '\0';
163         else
164                 buf[r] = '\0';
165
166         return strdup(buf);
167 }
168
169 void ubus_init_system(struct ubus_context *ctx)
170 {
171         int ret;
172
173         if (!board_model)
174                 board_model = load_file_content("/tmp/sysinfo/model");
175         if (!board_name);
176                 board_name = load_file_content("/tmp/sysinfo/board_name");
177         ret = ubus_add_object(ctx, &system_object);
178         if (ret)
179                 ERROR("Failed to add object: %s\n", ubus_strerror(ret));
180 }