-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathustatus.c
164 lines (134 loc) · 3.69 KB
/
ustatus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (C) 2017 John Crispin <john@phrozen.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2.1
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/reboot.h>
#include <unistd.h>
#include <getopt.h>
#include <libgen.h>
#include <libubox/uloop.h>
#include <libubox/utils.h>
#include <libubus.h>
#include <libubox/blobmsg_json.h>
static struct ubus_auto_conn conn;
static struct ubus_subscriber subscriber;
static int verbose = 1;
static const struct watch_list {
const char *path;
int wildcard;
} watch_list[] = {
{
.path = "service",
}, {
.path = "dnsmasq",
}, {
.path = "network.interface",
}, {
.path = "network.status",
}, {
.path = "hostapd.wlan",
.wildcard = 1,
},
};
enum {
EVENT_ID,
EVENT_PATH,
__EVENT_MAX
};
static const struct blobmsg_policy status_policy[__EVENT_MAX] = {
[EVENT_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
[EVENT_PATH] = { .name = "path", .type = BLOBMSG_TYPE_STRING },
};
static int
watch_match(const char *path)
{
int i;
for (i = 0; i < ARRAY_SIZE(watch_list); i++) {
int len = strlen(watch_list[i].path);
if (watch_list[i].wildcard && strncmp(path, watch_list[i].path, len))
continue;
if (!watch_list[i].wildcard && strcmp(path, watch_list[i].path))
continue;
return 0;
}
return -1;
}
static int
watch_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
if (verbose) {
char *str;
str = blobmsg_format_json(msg, true);
fprintf(stdout, "Received ubus notify '%s': %s\n", method, str);
free(str);
}
return 0;
}
static void
handle_status(struct ubus_context *ctx, struct ubus_event_handler *ev,
const char *type, struct blob_attr *msg)
{
struct blob_attr *tb[__EVENT_MAX];
const char *path;
uint32_t id;
if (strcmp("ubus.object.add", type))
return;
if (verbose) {
char *str;
str = blobmsg_format_json(msg, true);
fprintf(stdout, "Received ubus notify '%s': %s\n", type, str);
free(str);
}
blobmsg_parse(status_policy, __EVENT_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[EVENT_ID] || !tb[EVENT_PATH])
return;
path = blobmsg_get_string(tb[EVENT_PATH]);
id = blobmsg_get_u32(tb[EVENT_ID]);
if (!watch_match(path) && !ubus_subscribe(ctx, &subscriber, id))
fprintf(stdout, "Subscribe to %s (%u)\n", path, id);
}
static struct ubus_event_handler status_handler = { .cb = handle_status };
static void
receive_list_result(struct ubus_context *ctx, struct ubus_object_data *obj,
void *priv)
{
char *path = strdup(obj->path);
if (!watch_match(path) && !ubus_subscribe(ctx, &subscriber, obj->id))
fprintf(stdout, "Subscribe to %s (%u)\n", path, obj->id);
free(path);
}
static void
ubus_connect_handler(struct ubus_context *ctx)
{
fprintf(stderr, "connected to ubus\n");
ubus_register_event_handler(ctx, &status_handler, "ubus.object.add");
ubus_register_event_handler(ctx, &status_handler, "ubus.object.remove");
subscriber.cb = watch_notify_cb;
if (ubus_register_subscriber(ctx, &subscriber))
fprintf(stderr, "failed to register ubus subscriber\n");
ubus_lookup(ctx, NULL, receive_list_result, NULL);
}
int
main(int argc, char **argv)
{
uloop_init();
conn.cb = ubus_connect_handler;
ubus_auto_connect(&conn);
uloop_run();
uloop_done();
ubus_auto_shutdown(&conn);
return 0;
}