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