-
Notifications
You must be signed in to change notification settings - Fork 15
/
pipe.c
275 lines (242 loc) · 7.01 KB
/
pipe.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2015, Louis Opter <[email protected]>
//
// This file is part of lighstd.
//
// lighstd is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// lighstd 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.
//
// You should have received a copy of the GNU General Public License
// along with lighstd. If not, see <http://www.gnu.org/licenses/>.
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <event2/buffer.h>
#include <event2/event.h>
#include "daemon.h"
#include "jsmn.h"
#include "jsonrpc.h"
#include "client.h"
#include "pipe.h"
#include "lightsd.h"
struct lgtd_command_pipe_list lgtd_command_pipes =
SLIST_HEAD_INITIALIZER(&lgtd_command_pipes);
static void
_lgtd_command_pipe_close(struct lgtd_command_pipe *pipe)
{
assert(pipe);
event_del(pipe->read_ev);
if (pipe->fd != -1) {
close(pipe->fd);
}
SLIST_REMOVE(&lgtd_command_pipes, pipe, lgtd_command_pipe, link);
evbuffer_free(pipe->read_buf);
event_free(pipe->read_ev);
free(pipe->client.jsmn_tokens);
free(pipe);
}
static void
lgtd_command_pipe_close(struct lgtd_command_pipe *pipe)
{
const char *path = pipe->path;
_lgtd_command_pipe_close(pipe);
unlink(path);
lgtd_info("closed command pipe %s", path);
}
static void lgtd_command_pipe_reset(struct lgtd_command_pipe *);
static void
lgtd_command_pipe_read_callback(evutil_socket_t socket, short events, void *ctx)
{
assert(ctx);
assert(socket != -1);
(void)socket;
(void)events;
struct lgtd_command_pipe *pipe = ctx;
bool drain = false;
for (int nbytes = evbuffer_read(pipe->read_buf, pipe->fd, -1);
nbytes;
nbytes = evbuffer_read(pipe->read_buf, pipe->fd, -1)) {
if (nbytes == -1) {
if (errno == EINTR) {
continue;
}
if (errno != EAGAIN) {
lgtd_warn("read error on command pipe %s", pipe->path);
break;
}
return; // EAGAIN, go back to the event loop
}
if (!drain) {
jsmntok_t *tokens = NULL;
int ntokens = 0;
jsmn_parser jsmn_ctx;
next_request:
(void)0;
const char *buf = (char *)evbuffer_pullup(pipe->read_buf, -1);
ssize_t bufsz = evbuffer_get_length(pipe->read_buf);
parse_after_realloc:
jsmn_init(&jsmn_ctx);
int rv = jsmn_parse(&jsmn_ctx, buf, bufsz, tokens, ntokens);
switch (rv) {
case JSMN_ERROR_NOMEM:
case JSMN_ERROR_INVAL:
lgtd_warnx("pipe %s: request too big or invalid: %s", pipe->path, buf);
// ignore what's left
drain = true;
break;
case JSMN_ERROR_PART:
case 0:
if (bufsz >= LGTD_CLIENT_MAX_REQUEST_BUF_SIZE) {
lgtd_warnx("pipe %s: request too big", pipe->path);
drain = true;
}
break;
default:
ntokens = rv;
if (tokens) {
pipe->client.json = buf;
lgtd_jsonrpc_dispatch_request(&pipe->client, ntokens);
pipe->client.json = NULL;
int request_size = tokens[0].end;
tokens = NULL;
evbuffer_drain(pipe->read_buf, request_size);
if (request_size >= bufsz) {
break;
}
} else {
pipe->client.jsmn_tokens = reallocarray(
pipe->client.jsmn_tokens, ntokens, sizeof(*tokens)
);
tokens = pipe->client.jsmn_tokens;
goto parse_after_realloc;
}
goto next_request;
}
}
if (drain) {
ssize_t bufsz = evbuffer_get_length(pipe->read_buf);
evbuffer_drain(pipe->read_buf, bufsz);
drain = false;
}
}
lgtd_command_pipe_reset(pipe);
}
static bool
_lgtd_command_pipe_open(const char *path)
{
assert(path);
struct lgtd_command_pipe *pipe;
SLIST_FOREACH(pipe, &lgtd_command_pipes, link) {
if (!strcmp(pipe->path, path)) {
return true;
}
}
if (!lgtd_daemon_makedirs(path)) {
return false;
}
pipe = calloc(1, sizeof(*pipe));
if (!pipe) {
lgtd_warn("can't open command pipe %s", path);
return false;
}
struct stat sb;
if (stat(path, &sb) == -1) {
if (errno != ENOENT) {
goto error;
}
} else if (!S_ISFIFO(sb.st_mode)) {
errno = EEXIST;
goto error;
}
pipe->path = path;
pipe->fd = -1;
mode_t mode = S_IWUSR|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IWGRP;
if (mkfifo(path, mode)) {
if (errno != EEXIST) {
goto error;
}
}
if (chmod(path, mode)) {
goto error;
}
pipe->fd = open(path, O_RDONLY|O_NONBLOCK);
if (pipe->fd == -1) {
goto error;
}
if (evutil_make_socket_nonblocking(pipe->fd) == -1) {
goto error;
}
pipe->read_ev = event_new(
lgtd_ev_base,
pipe->fd,
EV_READ|EV_PERSIST,
lgtd_command_pipe_read_callback,
pipe
);
if (!pipe->read_ev) {
goto error;
}
pipe->read_buf = evbuffer_new();
if (!pipe->read_buf) {
goto error;
}
if (event_add(pipe->read_ev, NULL)) {
goto error;
}
SLIST_INSERT_HEAD(&lgtd_command_pipes, pipe, link);
return true;
error:
lgtd_warn("can't open command pipe %s", path);
if (pipe->read_buf) {
evbuffer_free(pipe->read_buf);
}
if (pipe->read_ev) {
event_free(pipe->read_ev);
}
if (pipe->fd != -1) {
close(pipe->fd);
}
free(pipe);
return false;
}
static void
lgtd_command_pipe_reset(struct lgtd_command_pipe *pipe)
{
const char *path = pipe->path;
// we could optimize a bit to avoid re-allocations here:
_lgtd_command_pipe_close(pipe);
if (!_lgtd_command_pipe_open(path)) {
lgtd_warn("can't re-open pipe %s", path);
}
}
bool
lgtd_command_pipe_open(const char *path)
{
if (_lgtd_command_pipe_open(path)) {
lgtd_info("command pipe ready at %s", path);
return true;
}
return false;
}
void
lgtd_command_pipe_close_all(void)
{
while (!SLIST_EMPTY(&lgtd_command_pipes)) {
lgtd_command_pipe_close(SLIST_FIRST(&lgtd_command_pipes));
}
}