-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproc_posix.h
371 lines (337 loc) · 9.41 KB
/
proc_posix.h
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#define IPC_PROC_SIGTERM SIGTERM
#define IPC_PROC_SIGKILL SIGKILL
#define IPC_PROC_BUFSIZE 1024
typedef struct {
pid_t child;
int fds[ 3 ];
int exitstatus;
char const* exitkind; /* "exit" or "signal" */
char const* inbuf;
size_t inlen; /* length of the string `inbuf` */
char outbuf[ IPC_PROC_BUFSIZE ];
char errbuf[ IPC_PROC_BUFSIZE ];
} ipc_proc_handle;
static void ipc_proc_error( char* buf, size_t len, int code ) {
if( len > 0 && strerror_r( code, buf, len ) != (int)0 ) {
strncpy( buf, "unknown error", len-1 );
buf[ len-1 ] = '\0';
}
}
static void ipc_proc_inithandle_( ipc_proc_handle* h ) {
h->child = 0;
h->fds[ 0 ] = h->fds[ 1 ] = h->fds[ 2 ] = -1;
h->exitstatus = 0;
h->exitkind = NULL;
h->inbuf = NULL;
h->inlen = 0;
}
static int ipc_proc_waitpid_( ipc_proc_handle* h, int flags ) {
int rv = 0;
if( h->exitkind != NULL )
return 0;
IPC_EINTR( rv, waitpid( h->child, &h->exitstatus, flags ) );
switch( rv ) {
case -1:
return IPC_ERR( errno );
case 0:
return 0;
default:
if( WIFEXITED( h->exitstatus ) ) {
h->exitkind = "exit";
h->exitstatus = WEXITSTATUS( h->exitstatus );
} else if( WIFSIGNALED( h->exitstatus ) ) {
h->exitkind = "signal";
h->exitstatus = WTERMSIG( h->exitstatus );
} else
h->exitkind = "exit";
}
return 0;
}
static int ipc_proc_kill( ipc_proc_handle* h, int signum ) {
ipc_proc_waitpid_( h, WNOHANG ); /* non-blocking */
if( h->exitkind == NULL ) {
if( kill( h->child, signum ) < 0 )
return IPC_ERR( errno );
}
return 0;
}
static int ipc_proc_wait( ipc_proc_handle* h, int* status,
char const** what ) {
int rv = ipc_proc_waitpid_( h, 0 ); /* blocking */
if( rv < 0 )
return IPC_ERR( rv );
*status = h->exitstatus;
*what = h->exitkind;
return 0;
}
static int ipc_proc_trywrite_( ipc_proc_handle* h, int* done ) {
ssize_t nwritten = 0;
IPC_EINTR( nwritten, write( h->fds[ 0 ], h->inbuf, h->inlen ) );
if( nwritten < 0 ) {
*done = 0;
if( errno == EAGAIN || errno == EWOULDBLOCK )
return 0;
else {
int saved_errno = errno;
close( h->fds[ 0 ] );
h->fds[ 0 ] = -1;
if( saved_errno == EPIPE )
return 0;
else
return IPC_ERR( saved_errno );
}
} else {
h->inbuf += nwritten;
h->inlen -= nwritten;
if( h->inlen > 0 )
*done = 0;
else {
h->inbuf = NULL;
*done = 1;
}
}
return 0;
}
static int ipc_proc_stdinready( ipc_proc_handle* h, int* ready ) {
int rv = 0;
if( h->fds[ 0 ] < 0 ) {
*ready = 0;
return 0;
}
if( h->inbuf == NULL ) {
*ready = 1;
return 0;
}
rv = ipc_proc_trywrite_( h, ready );
if( rv != 0 )
return IPC_ERR( rv );
return 0;
}
static int ipc_proc_closestdin( ipc_proc_handle* h ) {
if( h->fds[ 0 ] < 0 )
return IPC_ERR( EBADF );
if( close( h->fds[ 0 ] ) < 0 )
return IPC_ERR( errno );
h->fds[ 0 ] = -1;
return 0;
}
static int ipc_proc_write( ipc_proc_handle* h, char const* s,
size_t len, int* completed ) {
int rv = 0;
if( h->fds[ 0 ] < 0 )
return IPC_ERR( EBADF );
if( h->inbuf != NULL )
return IPC_ERR( EBUSY );
h->inbuf = s;
h->inlen = len;
rv = ipc_proc_trywrite_( h, completed );
if( rv != 0 )
return IPC_ERR( rv );
return 0;
}
static int ipc_proc_waitio( ipc_proc_handle* h, int* child_done,
int* stdin_done, int* stdout_done,
int* stderr_done ) {
int nfds = -1;
int rv = 0;
fd_set readset, writeset;
fd_set* readsetp = NULL;
fd_set* writesetp = NULL;
*child_done = *stdin_done = *stdout_done = *stderr_done = 0;
FD_ZERO( &readset );
FD_ZERO( &writeset );
#define ADD2SET( fdidx, set, extra ) \
do { \
if( h->fds[ fdidx ] >= 0 && (extra) ) { \
set ## p = &set; \
nfds = nfds > h->fds[ fdidx ] ? nfds : h->fds[ fdidx ]; \
FD_SET( h->fds[ fdidx ], set ## p ); \
} \
} while( 0 )
ADD2SET( 0, writeset, h->inbuf != NULL );
ADD2SET( 1, readset, 1 );
ADD2SET( 2, readset, 1 );
#undef ADD2SET
if( nfds >= 0 ) {
/* when the child dies, the select should return too */
IPC_EINTR( rv, select( nfds+1, readsetp, writesetp, NULL, NULL ) );
if( rv < 0 )
return IPC_ERR( errno );
/* check for dead child */
rv = ipc_proc_waitpid_( h, WNOHANG );
if( rv != 0 )
return IPC_ERR( rv );
if( h->exitkind != NULL )
*child_done = 1;
} else {
/* no I/O configured, so we just wait for the child to die! */
rv = ipc_proc_waitpid_( h, 0 );
if( rv != 0 )
return IPC_ERR( rv );
*child_done = 1;
}
if( writesetp != NULL && FD_ISSET( h->fds[ 0 ], writesetp ) )
*stdin_done = 1;
if( readsetp != NULL && h->fds[ 1 ] >= 0 &&
FD_ISSET( h->fds[ 1 ], readsetp ) )
*stdout_done = 1;
if( readsetp != NULL && h->fds[ 2 ] >= 0 &&
FD_ISSET( h->fds[ 2 ], readsetp ) )
*stderr_done = 1;
return 0;
}
static int ipc_proc_get_( ipc_proc_handle* h, int fdindex, char* buf,
char const** data, size_t* len ) {
ssize_t n = 0;
if( h->fds[ fdindex ] < 0 )
return IPC_ERR( EBADF );
*data = buf;
*len = 0;
IPC_EINTR( n, read( h->fds[ fdindex ], buf, IPC_PROC_BUFSIZE ) );
if( n < 0 ) {
if( errno != EAGAIN && errno != EWOULDBLOCK ) {
int saved_errno = errno;
close( h->fds[ fdindex ] );
h->fds[ fdindex ] = -1;
return IPC_ERR( saved_errno );
}
} else if( n == 0 ) { /* EOF */
close( h->fds[ fdindex ] );
h->fds[ fdindex ] = -1;
*len = 0;
} else
*len = n;
return 0;
}
static int ipc_proc_stdoutready( ipc_proc_handle* h, char const** data,
size_t* len ) {
return ipc_proc_get_( h, 1, h->outbuf, data, len );
}
static int ipc_proc_stderrready( ipc_proc_handle* h, char const** data,
size_t* len ) {
return ipc_proc_get_( h, 2, h->errbuf, data, len );
}
static int ipc_proc_spawn( ipc_proc_handle* h, char const* cmdline,
FILE* cstdin, int pstdin,
FILE* cstdout, int pstdout,
FILE* cstderr, int pstderr ) {
int fds[] = { -1, -1, -1, -1, -1, -1 };
int stdin_fd = -1, stdout_fd = -1, stderr_fd = -1;
int rv = 0;
/* macro to check that the given file descriptor is valid for select
* to use. cleans up and returns error code if not. */
#define CHECK_FD( fd ) \
do { \
int d = (fd); \
if( d < 0 || d >= FD_SETSIZE ) { \
size_t i = 0; \
for( i = 0; i < sizeof( fds )/sizeof( *fds ); ++i ) \
if( fds[ i ] >= 0 ) \
close( fds[ i ] ); \
return IPC_ERR( EMFILE ); \
} \
} while( 0 )
/* macro to check that a value is >= 0. cleans up and returns error
* code if not. */
#define CHECK_R( v ) \
do { \
if( (v) < 0 ) { \
int saved_errno = errno; \
size_t i = 0; \
for( i = 0; i < sizeof( fds )/sizeof( *fds ); ++i ) \
if( fds[ i ] >= 0 ) \
close( fds[ i ] ); \
return IPC_ERR( saved_errno ); \
} \
} while( 0 )
ipc_proc_inithandle_( h );
/* create pipes if necessary */
if( cstdin != NULL ) {
CHECK_R( stdin_fd = fileno( cstdin ) );
} else if( pstdin ) {
CHECK_R( pipe( fds ) );
IPC_EINTR( rv, fcntl( fds[ 1 ], F_SETFL, O_NONBLOCK ) );
CHECK_R( rv );
CHECK_FD( h->fds[ 0 ] = fds[ 1 ] );
stdin_fd = fds[ 0 ];
}
if( cstdout != NULL ) {
CHECK_R( stdout_fd = fileno( cstdout ) );
} else if( pstdout ) {
CHECK_R( pipe( fds+2 ) );
IPC_EINTR( rv, fcntl( fds[ 2 ], F_SETFL, O_NONBLOCK ) );
CHECK_R( rv );
CHECK_FD( h->fds[ 1 ] = fds[ 2 ] );
stdout_fd = fds[ 3 ];
}
if( cstderr != NULL ) {
CHECK_R( stderr_fd = fileno( cstderr ) );
} else if( pstderr ) {
CHECK_R( pipe( fds+4 ) );
IPC_EINTR( rv, fcntl( fds[ 4 ], F_SETFL, O_NONBLOCK ) );
CHECK_R( rv );
CHECK_FD( h->fds[ 2 ] = fds[ 4 ] );
stderr_fd = fds[ 5 ];
}
/* create child process */
CHECK_R( h->child = fork() );
if( h->child > 0 ) { /* parent */
if( fds[ 0 ] >= 0 )
close( fds[ 0 ] );
if( fds[ 3 ] >= 0 )
close( fds[ 3 ] );
if( fds[ 5 ] >= 0 )
close( fds[ 5 ] );
return 0;
}
#undef CHECK_FD
#undef CHECK_R
/* here we are the child process */
if( fds[ 1 ] >= 0 )
close( fds[ 1 ] );
if( fds[ 2 ] >= 0 )
close( fds[ 2 ] );
if( fds[ 4 ] >= 0 )
close( fds[ 4 ] );
/* setup stdin, stdout, and stderr for the child process */
if( stdin_fd >= 0 ) {
IPC_EINTR( rv, dup2( stdin_fd, STDIN_FILENO ) );
close( stdin_fd );
if( rv < 0 )
_exit( 127 );
}
if( stdout_fd >= 0 ) {
IPC_EINTR( rv, dup2( stdout_fd, STDOUT_FILENO ) );
close( stdout_fd );
if( rv < 0 )
_exit( 127 );
}
if( stderr_fd >= 0 ) {
IPC_EINTR( rv, dup2( stderr_fd, STDERR_FILENO ) );
close( stderr_fd );
if( rv < 0 )
_exit( 127 );
}
signal( SIGPIPE, SIG_DFL );
/* FIXME should we prevent file descriptors from leaking?! */
/* start the new process */
execl( "/bin/sh", "/bin/sh", "-c", cmdline, (char const*)NULL );
_exit( 127 );
return 0;
}
static int ipc_proc_prepare( void ) {
if( signal( SIGPIPE, SIG_IGN ) == SIG_ERR )
return IPC_ERR( errno );
return 0;
}