mirrored from http://xwax.org/devel/xwax.git/
-
Notifications
You must be signed in to change notification settings - Fork 34
/
external.c
309 lines (248 loc) · 6.17 KB
/
external.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (C) 2024 Mark Hills <[email protected]>
*
* This file is part of "xwax".
*
* "xwax" is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 3 as
* published by the Free Software Foundation.
*
* "xwax" 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 this program; if not, see <https://www.gnu.org/licenses/>.
*
*/
#define _DEFAULT_SOURCE /* vfork() */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "debug.h"
#include "external.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))
/*
* Fork a child process, attaching stdout to the given pipe
*
* Return: -1 on error, or pid on success
* Post: on success, *fd is file handle for reading
*/
static pid_t do_fork(int pp[2], const char *path, char *argv[])
{
pid_t pid;
pid = vfork();
if (pid == -1) {
perror("vfork");
return -1;
}
if (pid == 0) { /* child */
if (close(pp[0]) != 0)
abort();
if (dup2(pp[1], STDOUT_FILENO) == -1) {
perror("dup2");
_exit(EXIT_FAILURE); /* vfork() was used */
}
if (close(pp[1]) != 0)
abort();
if (execv(path, argv) == -1) {
perror(path);
_exit(EXIT_FAILURE); /* vfork() was used */
}
abort(); /* execv() does not return */
}
if (close(pp[1]) != 0)
abort();
return pid;
}
/*
* Wrapper on do_fork which uses va_list
*
* The caller passes in the pipe for use, rather us handing one
* back. This is because if the caller wishes to have a non-blocking
* pipe, then the cleanup is messy if the process has already been
* forked.
*/
static pid_t vext(int pp[2], const char *path, char *arg, va_list ap)
{
char *args[16];
size_t n;
args[0] = arg;
n = 1;
/* Convert to an array; there's no va_list variant of exec() */
for (;;) {
char *x;
x = va_arg(ap, char*);
assert(n < ARRAY_SIZE(args));
args[n++] = x;
if (x == NULL)
break;
}
return do_fork(pp, path, args);
}
/*
* Fork a child process with stdout connected to this process
* via a pipe
*
* Return: PID on success, otherwise -1
* Post: on success, *fd is file descriptor for reading
*/
pid_t fork_pipe(int *fd, const char *path, char *arg, ...)
{
int pp[2];
pid_t r;
va_list va;
if (pipe(pp) == -1) {
perror("pipe");
return -1;
}
va_start(va, arg);
r = vext(pp, path, arg, va);
va_end(va);
if (r == -1) {
if (close(pp[0]) != 0)
abort();
if (close(pp[1]) != 0)
abort();
}
*fd = pp[0];
return r;
}
/*
* Make the given file descriptor non-blocking
*
* Return: 0 on success, otherwise -1
* Post: if 0 is returned, file descriptor is non-blocking
*/
static int make_non_blocking(int fd)
{
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl");
return -1;
}
return 0;
}
/*
* Fork a child process with stdout connected to this process
* via a non-blocking pipe
*
* Return: PID on success, otherwise -1
* Post: on success, *fd is non-blocking file descriptor for reading
*/
pid_t fork_pipe_nb(int *fd, const char *path, char *arg, ...)
{
int pp[2];
pid_t r;
va_list va;
if (pipe(pp) == -1) {
perror("pipe");
return -1;
}
if (make_non_blocking(pp[0]) == -1)
goto fail;
va_start(va, arg);
r = vext(pp, path, arg, va);
va_end(va);
assert(r != 0);
if (r < 0)
goto fail;
*fd = pp[0];
return r;
fail:
if (close(pp[0]) != 0)
abort();
if (close(pp[1]) != 0)
abort();
return -1;
}
void rb_reset(struct rb *rb)
{
rb->len = 0;
}
bool rb_is_full(const struct rb *rb)
{
return (rb->len == sizeof rb->buf);
}
/*
* Read, within reasonable limits (ie. memory or time)
* from the fd into the buffer
*
* Return: -1 on error, 0 on EOF, otherwise the number of bytes added
*/
static ssize_t top_up(struct rb *rb, int fd)
{
size_t remain;
ssize_t z;
assert(rb->len < sizeof rb->buf);
remain = sizeof(rb->buf) - rb->len;
z = read(fd, rb->buf + rb->len, remain);
if (z == -1)
return -1;
rb->len += z;
return z;
}
/*
* Pop the front of the buffer to end-of-line
*
* Return: 0 if not found, -1 if not enough memory,
* otherwise string length (incl. terminator)
* Post: if return is > 0, q points to alloc'd string
*/
static ssize_t pop(struct rb *rb, char **q)
{
const char *x;
char *s;
size_t len;
x = memchr(rb->buf, '\n', rb->len);
if (!x) {
debug("pop %p exhausted", rb);
return 0;
}
len = x - rb->buf;
debug("pop %p got %u", rb, len);
s = strndup(rb->buf, len);
if (!s) {
debug("strndup: %s", strerror(errno));
return -1;
}
*q = s;
/* Simple compact of the buffer. If this is a bottleneck of any
* kind (unlikely) then a circular buffer should be used */
memmove(rb->buf, x + 1, rb->len - len - 1);
rb->len = rb->len - len - 1;
return len + 1;
}
/*
* Read a terminated string from the given file descriptor via
* the buffer.
*
* Handles non-blocking file descriptors too. If fd is non-blocking,
* then the semantics are the same as a non-blocking read() --
* ie. EAGAIN may be returned as an error.
*
* Return: 0 on EOF, or -1 on error
* Post: if -1 is returned, errno is set accordingly
*/
ssize_t get_line(int fd, struct rb *rb, char **string)
{
ssize_t y, z;
y = top_up(rb, fd);
if (y < 0)
return y;
z = pop(rb, string);
if (z != 0)
return z;
if (rb_is_full(rb))
errno = ENOBUFS;
else if (y > 0)
errno = EAGAIN;
else
return 0; /* true EOF: no more data and empty buffer */
return -1;
}