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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
|
/*=============================================================================
pm_system
===============================================================================
This is the pm_system() family of subroutines.
pm_system() is just like Standard C Library system(), except that you can
supply routines for it to run to generate the Standard Input for the
executed shell command and to accept the Standard Output from it.
system(), by contrast, always sets up the current Standard Input and
Standard Output as the Standard Input and Standard Output of the shell
command.
pm_system_lp() and pm_system_vp() are similar, but exec an OS-level program
(i.e. exec a program) rather than run a shell command.
By Bryan Henderson, San Jose CA 2002.12.14.
Contributed to the public domain.
=============================================================================*/
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
#define _BSD_SOURCE /* Make SIGWINCH defined on OpenBSD */
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#include "netpbm/pm_c_util.h"
#include "netpbm/mallocvar.h"
#include "netpbm/nstring.h"
#include "pm.h"
#include "pm_system.h"
#define STDIN 0
#define STDOUT 1
static void
closeUninheritableFds(int const keepFdA,
int const keepFdB) {
/*----------------------------------------------------------------------------
Close all the file descriptors that we declare uninheritable -- files Parent
has open that Child has no business accessing.
Closing an extra file descriptor is essential to allow the file to close
when Parent closes it.
It is also essential to prevent the system from messing with the position of
the file as the child process exits. If the file descriptor is backing a
stream (FILE *), some process-exit code seeks the file to the current stream
position (from the readahead position), but having the file descriptor
closed defeats that.
We define uninheritable as less than 64 and not Standard Input, Output,
or Error, or 'keepFdA' or 'keepFdB'.
-----------------------------------------------------------------------------*/
int fd;
for (fd = 0; fd < 64; ++fd) {
if (false) {
} else if (fd == keepFdA) {
} else if (fd == keepFdB) {
} else if (fd == STDIN_FILENO) {
} else if (fd == STDOUT_FILENO) {
} else if (fd == STDERR_FILENO) {
} else {
close(fd);
}
}
}
static void
execProgram(const char * const progName,
const char ** const argArray,
int const stdinFd,
int const stdoutFd) {
/*----------------------------------------------------------------------------
Exec the program 'progName' with arguments argArray[], with 'stdinFd' as
its Standard Input and 'stdoutFd' as its Standard Output.
But if the exec fails, leave all file descriptors as we found them.
Note that stdinFd or stdoutFd may actually be Standard Input and
Standard Output already.
-----------------------------------------------------------------------------*/
int stdinSaveFd, stdoutSaveFd;
int rc;
int execErrno;
/* Make stdinFd Standard Input.
Make stdoutFd Standard Output.
*/
if (stdinFd != STDIN) {
stdinSaveFd = dup(STDIN);
close(STDIN);
dup2(stdinFd, STDIN);
close(stdinFd);
}
if (stdoutFd != STDOUT) {
stdoutSaveFd = dup(STDOUT);
close(STDOUT);
dup2(stdoutFd, STDOUT);
close(stdoutFd);
}
rc = execvp(progName, (char **)argArray);
execErrno = errno;
if (stdinFd != STDIN) {
close(STDIN);
dup2(stdinSaveFd, STDIN);
close(stdinSaveFd);
}
if (stdoutFd != STDOUT) {
close(STDOUT);
dup2(stdoutSaveFd, STDOUT);
close(stdoutSaveFd);
}
if (rc < 0)
pm_error("Unable to exec '%s' "
"(i.e. the program did not run at all). "
"execvp() errno=%d (%s)",
progName, execErrno, strerror(execErrno));
else
pm_error("INTERNAL ERROR. execvp() returns, but does not fail.");
}
static void
createPipeFeeder(void pipeFeederRtn(int, void *),
void * const feederParm,
int * const fdP,
pid_t * const pidP) {
/*----------------------------------------------------------------------------
Create a process and a pipe. Have the process run program
'pipeFeederRtn' to fill the pipe and return the file descriptor of the
other end of the pipe as *fdP.
-----------------------------------------------------------------------------*/
int pipeToFeed[2];
pid_t rc;
pm_pipe(pipeToFeed);
rc = fork();
if (rc < 0) {
pm_error("fork() of stdin feeder failed. errno=%d (%s)",
errno, strerror(errno));
} else if (rc == 0) {
/* This is the child -- the stdin feeder process */
close(pipeToFeed[0]);
closeUninheritableFds(pipeToFeed[1], pipeToFeed[1]);
(*pipeFeederRtn)(pipeToFeed[1], feederParm);
exit(0);
} else {
/* This is the parent */
pid_t const feederPid = rc;
close(pipeToFeed[1]);
*fdP = pipeToFeed[0];
*pidP = feederPid;
}
}
static void
spawnProcessor(const char * const progName,
const char ** const argArray,
int const stdinFd,
int * const stdoutFdP,
pid_t * const pidP) {
/*----------------------------------------------------------------------------
Create a process to run program 'progName' with arguments
argArray[] (terminated by NULL element). Pass file descriptor
'stdinFd' to the process as Standard Input.
if 'stdoutFdP' is NULL, have that process write its Standard Output to
the current process' Standard Output.
If 'stdoutFdP' is non-NULL, set up a pipe and pass it to the new
process as Standard Output. Return as *stdoutFdP the file
descriptor of the other end of that pipe, from which Caller can
suck the program's Standard Output.
-----------------------------------------------------------------------------*/
bool const pipeStdout = !!stdoutFdP;
int stdoutpipe[2];
pid_t rc;
if (pipeStdout)
pm_pipe(stdoutpipe);
rc = fork();
if (rc < 0) {
pm_error("fork() of processor process failed. errno=%d (%s)",
errno, strerror(errno));
} else if (rc == 0) {
/* The program child */
int stdoutFd;
if (pipeStdout) {
close(stdoutpipe[0]);
stdoutFd = stdoutpipe[1];
} else
stdoutFd = STDOUT;
closeUninheritableFds(stdinFd, stdoutFd);
execProgram(progName, argArray, stdinFd, stdoutFd);
close(stdinFd);
close(stdoutpipe[1]);
pm_error("INTERNAL ERROR: execProgram() returns.");
} else {
/* The parent */
pid_t const processorpid = rc;
if (pipeStdout) {
close(stdoutpipe[1]);
*stdoutFdP = stdoutpipe[0];
}
*pidP = processorpid;
}
}
static const char *
signalName(unsigned int const signalClass) {
/* There are various signal classes that are not universally defined,
so we make a half-hearted attempt to determine whether they are and
not try to recognize the ones that aren't. We do this by testing
whether a macro is defined with the signal class name. That could give
a false negative, because the signal class name isn't necessarily
defined as a macro, but it's a really, really small problem to miss
one of these signal classes here, so we don't bother with all the work
it would take to do it right.
OpenBSD does not have SIGWINCH and SIGIO in 2013. Everyone else seems
to have them. OpenBSD does have them if the code is not declared as
X/open code (i.e. OpenBSD seems to interpret _XOPEN_SOURCE backward -
it removes features rather than adds them).
*/
switch (signalClass) {
case SIGHUP: /* POSIX.1 */
return "SIGHUP";
case SIGINT: /* POSIX.1 */
return "SIGINT";
case SIGQUIT: /* POSIX.1 */
return "SIGQUIT";
case SIGILL: /* POSIX.1 */
return "SIGILL";
case SIGTRAP:
return "SIGTRAP";
case SIGABRT: /* POSIX.1 */
return "SIGABRT";
case SIGBUS:
return "SIGBUS";
case SIGFPE: /* POSIX.1 */
return "SIGFPE";
case SIGKILL: /* POSIX.1 */
return "SIGKILL";
case SIGUSR1: /* POSIX.1 */
return "SIGUSR1";
case SIGSEGV: /* POSIX.1 */
return "SIGSEGV";
case SIGUSR2: /* POSIX.1 */
return "SIGUSR2";
case SIGPIPE: /* POSIX.1 */
return "SIGPIPE";
case SIGALRM: /* POSIX.1 */
return "SIGALRM";
case SIGTERM: /* POSIX.1 */
return "SIGTERM";
case SIGCHLD: /* POSIX.1 */
return "SIGCHLD";
case SIGCONT: /* POSIX.1 */
return "SIGCONT";
case SIGSTOP: /* POSIX.1 */
return "SIGSTOP";
case SIGTSTP: /* POSIX.1 */
return "SIGTSTP";
case SIGTTIN: /* POSIX.1 */
return "SIGTTIN";
case SIGTTOU: /* POSIX.1 */
return "SIGTTOU";
#ifdef SIGURG
/* SCO Openserver 5.0.7/3.2 does not have SIGURG */
case SIGURG:
return "SIGURG";
#endif
case SIGXCPU:
return "SIGXCPU";
case SIGXFSZ:
return "SIGXFSZ";
case SIGVTALRM:
return "SIGVTALRM";
case SIGPROF:
return "SIGPROF";
#ifdef SIGWINCH
case SIGWINCH:
return "SIGWINCH";
#endif
#ifdef SIGIO
/* SCO Openserver 5.0.7/3.2 does not have SIGIO */
case SIGIO:
return "SIGIO";
#endif
#ifdef SIGPWR
case SIGPWR:
return "SIGPWR";
#endif
case SIGSYS:
return "SIGSYS";
default:
return "???";
}
}
const char *
pm_termStatusDesc(int const termStatusArg) {
/*----------------------------------------------------------------------------
English description of process termination status 'termStatus'.
-----------------------------------------------------------------------------*/
const char * retval;
/* WIFEXITED, etc. do not work with a constant argument in older GNU C
library. Compilation fails with "attempt to assign read-only
location". This is because The GNU C library has some magic to allow
for a BSD 'union wait' (instead of int) argument to WIFEXITED. The
magic involves defining a variable with 'typeof' the argument and
assigning to that variable.
To work around this, we make sure the argument is not constant.
*/
int termStatus = termStatusArg;
if (WIFEXITED(termStatus)) {
int const exitStatus = WEXITSTATUS(termStatus);
if (exitStatus == 0)
pm_asprintf(&retval, "Process exited normally");
else
pm_asprintf(&retval,
"Process exited with abnormal exit status %u. ",
exitStatus);
} else if (WIFSIGNALED(termStatus)) {
pm_asprintf(&retval, "Process was killed by a Class %u (%s) signal.",
WTERMSIG(termStatus),
signalName(WTERMSIG(termStatus)));
} else {
pm_asprintf(&retval, "Process died, but its termination status "
"0x%x doesn't make sense", termStatus);
}
return retval;
}
static void
cleanupFeederProcess(pid_t const feederPid) {
int status;
waitpid(feederPid, &status, 0);
if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGPIPE)
pm_message("WARNING: "
"Standard Input feeder process was terminated by a "
"SIGPIPE signal because the program closed its "
"Standard Input before the Standard Input feeder was "
"through feeding it.");
else
pm_message("WARNING: "
"Standard Input feeder was terminated by a Signal %d.",
WTERMSIG(status));
}
else if (WIFEXITED(status)) {
if (WEXITSTATUS(status) != 0)
pm_message("WARNING: "
"Standard Input feeder process ended abnormally. "
"exit status = %d", WEXITSTATUS(status));
} else
pm_message("WARNING: "
"Unrecognized process completion status from "
"Standard Input feeder: %d", status);
}
void
pm_system2_vp(const char * const progName,
const char ** const argArray,
void stdinFeeder(int, void *),
void * const feederParm,
void stdoutAccepter(int, void *),
void * const accepterParm,
int * const termStatusP) {
/*----------------------------------------------------------------------------
Run a program in a child process. Feed its Standard Input with a
pipe, which is fed by the routine 'stdinFeeder' with parameter
'feederParm'. Process its Standard Output with the routine
'stdoutAccepter' with parameter 'accepterParm'.
But if 'stdinFeeder' is NULL, just feed the program our own Standard
Input. And if 'stdoutFeeder' is NULL, just send its Standard Output
to our own Standard Output.
Run the program 'progName' with arguments argArray[] (terminated by NULL
element). That includes arg0.
Return as *termStatusP the termination status of the processor process
(the one running the program named 'progName').
-----------------------------------------------------------------------------*/
/* If 'stdinFeeder' is non-NULL, we create a child process to run
'stdinFeeder' and create a pipe from that process as the
program's Standard Input.
We create another child process to run the program.
If 'stdoutFeeder' is non-NULL, we create a pipe between the
program process and the current process and have the program
write its Standard Output to that pipe. The current process
runs 'stdoutAccepter' to read the data from that pipe.
But if 'stdoutFeeder' is NULL, we just tell the program process
to write to the current process' Standard Output.
So there are two processes when stdinFeeder is NULL and three when
stdinFeeder is non-null.
*/
int progStdinFd;
/* File descriptor that the processor program will get as Standard
Input
*/
bool weCreatedStdinFd;
/* This program created (opened) file descriptor 'progStdinFd',
as opposed to inheriting it.
*/
pid_t feederPid;
pid_t processorPid;
int termStatus;
if (stdinFeeder) {
createPipeFeeder(stdinFeeder, feederParm, &progStdinFd, &feederPid);
weCreatedStdinFd = true;
} else {
progStdinFd = STDIN;
weCreatedStdinFd = false;
feederPid = 0;
}
if (stdoutAccepter) {
int progStdoutFd;
/* Make a child process to run the program and pipe back to us its
Standard Output
*/
spawnProcessor(progName, argArray, progStdinFd,
&progStdoutFd, &processorPid);
/* Dispose of the stdout from that child */
(*stdoutAccepter)(progStdoutFd, accepterParm);
close(progStdoutFd);
} else {
/* Run a child process for the program that sends its Standard Output
to our Standard Output
*/
spawnProcessor(progName, argArray, progStdinFd, NULL, &processorPid);
}
if (weCreatedStdinFd) {
/* The child process has cloned our 'progStdinFd'; we have no
more use for our copy.
*/
close(progStdinFd);
}
waitpid(processorPid, &termStatus, 0);
if (feederPid)
cleanupFeederProcess(feederPid);
*termStatusP = termStatus;
}
void
pm_system2_lp(const char * const progName,
void stdinFeeder(int, void *),
void * const feederParm,
void stdoutAccepter(int, void *),
void * const accepterParm,
int * const termStatusP,
...) {
/*----------------------------------------------------------------------------
Same as pm_system_vp() except with arguments as variable arguments
instead of an array.
N.B. the first variable argument is the program's arg 0; the last
variable argument must be NULL.
-----------------------------------------------------------------------------*/
va_list args;
bool endOfArgs;
const char ** argArray;
unsigned int n;
va_start(args, termStatusP);
endOfArgs = FALSE;
argArray = NULL;
for (endOfArgs = FALSE, argArray = NULL, n = 0;
!endOfArgs;
) {
const char * const arg = va_arg(args, const char *);
REALLOCARRAY(argArray, n+1);
argArray[n++] = arg;
if (!arg)
endOfArgs = TRUE;
}
va_end(args);
pm_system2_vp(progName, argArray,
stdinFeeder, feederParm, stdoutAccepter, accepterParm,
termStatusP);
free(argArray);
}
void
pm_system2(void stdinFeeder(int, void *),
void * const feederParm,
void stdoutAccepter(int, void *),
void * const accepterParm,
const char * const shellCommand,
int * const termStatusP) {
/*----------------------------------------------------------------------------
Run a shell and have it run command 'shellCommand'. Feed its
Standard Input with a pipe, which is fed by the routine
'stdinFeeder' with parameter 'feederParm'. Process its Standard
Output with the routine 'stdoutAccepter' with parameter 'accepterParm'.
But if 'stdinFeeder' is NULL, just feed the shell our own Standard
Input. And if 'stdoutFeeder' is NULL, just send its Standard Output
to our own Standard Output.
Return as *termStatusP the termination status of the processor process
(the one running the program named 'progName').
-----------------------------------------------------------------------------*/
pm_system2_lp("/bin/sh",
stdinFeeder, feederParm, stdoutAccepter, accepterParm,
termStatusP,
"sh", "-c", shellCommand, NULL);
}
void
pm_system_vp(const char * const progName,
const char ** const argArray,
void stdinFeeder(int, void *),
void * const feederParm,
void stdoutAccepter(int, void *),
void * const accepterParm) {
/*----------------------------------------------------------------------------
Same as pm_system2_vp(), except instead of returning the termination
status, we just issue a message (pm_message) describing it.
-----------------------------------------------------------------------------*/
int termStatus;
pm_system2_vp(progName, argArray,
stdinFeeder, feederParm,
stdoutAccepter, accepterParm,
&termStatus);
if (termStatus != 0) {
const char * const msg = pm_termStatusDesc(termStatus);
pm_message("%s", msg);
pm_strfree(msg);
}
}
void
pm_system_lp(const char * const progName,
void stdinFeeder(int, void *),
void * const feederParm,
void stdoutAccepter(int, void *),
void * const accepterParm,
...) {
/*----------------------------------------------------------------------------
Same as pm_system_vp() except with arguments as variable arguments
instead of an array.
N.B. the first variable argument is the program's arg 0; the last
variable argument must be NULL.
-----------------------------------------------------------------------------*/
va_list args;
bool endOfArgs;
const char ** argArray;
unsigned int n;
va_start(args, accepterParm);
endOfArgs = FALSE;
argArray = NULL;
for (endOfArgs = FALSE, argArray = NULL, n = 0;
!endOfArgs;
) {
const char * const arg = va_arg(args, const char *);
REALLOCARRAY(argArray, n+1);
argArray[n++] = arg;
if (!arg)
endOfArgs = TRUE;
}
va_end(args);
pm_system_vp(progName, argArray,
stdinFeeder, feederParm, stdoutAccepter, accepterParm);
free(argArray);
}
void
pm_system(void stdinFeeder(int, void *),
void * const feederParm,
void stdoutAccepter(int, void *),
void * const accepterParm,
const char * const shellCommand) {
/*----------------------------------------------------------------------------
Same as pm_system2(), except instead of returning the termination status,
we just issue a message (pm_message) describing it.
-----------------------------------------------------------------------------*/
int termStatus;
pm_system2(stdinFeeder, feederParm, stdoutAccepter, accepterParm,
shellCommand,
&termStatus);
if (termStatus != 0) {
const char * const msg = pm_termStatusDesc(termStatus);
pm_message("%s", msg);
pm_strfree(msg);
}
}
void
pm_feed_null(int const pipeToFeedFd,
void * const feederParm) {
}
void
pm_accept_null(int const pipetosuckFd,
void * const accepterParm ) {
size_t const bufferSize = 4096;
unsigned char * buffer;
MALLOCARRAY(buffer, bufferSize);
if (buffer) {
bool eof;
for (eof = false; !eof; ) {
ssize_t rc;
rc = read(pipetosuckFd, buffer, bufferSize);
if (rc < 0) {
/* No way to report the problem; just say we're done */
eof = true;
} else if (rc == 0)
/* eof */
eof = true;
}
free(buffer);
}
close(pipetosuckFd);
}
void
pm_feed_from_memory(int const pipeToFeedFd,
void * const feederParm) {
pm_bufferDesc * const inputBufferP = feederParm;
FILE * const outFileP = fdopen(pipeToFeedFd, "w");
size_t bytesTransferred;
/* The following signals (and normally kills) the process with
SIGPIPE if the pipe does not take all 'size' bytes.
*/
bytesTransferred =
fwrite(inputBufferP->buffer, 1, inputBufferP->size, outFileP);
if (inputBufferP->bytesTransferredP)
*(inputBufferP->bytesTransferredP) = bytesTransferred;
fclose(outFileP);
}
void
pm_accept_to_memory(int const pipetosuckFd,
void * const accepterParm ) {
pm_bufferDesc * const outputBufferP = accepterParm;
FILE * const inFileP = fdopen(pipetosuckFd, "r");
size_t bytesTransferred;
bytesTransferred =
fread(outputBufferP->buffer, 1, outputBufferP->size, inFileP);
fclose(inFileP);
if (outputBufferP->bytesTransferredP)
*(outputBufferP->bytesTransferredP) = bytesTransferred;
}
|