Home | History | Annotate | Line # | Download | only in mail
popen.c revision 1.25
      1  1.25  christos /*	$NetBSD: popen.c,v 1.25 2009/04/10 13:08:25 christos Exp $	*/
      2   1.4  christos 
      3   1.1       cgd /*
      4   1.3   deraadt  * Copyright (c) 1980, 1993
      5   1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.7     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.4  christos #if 0
     35   1.4  christos static char sccsid[] = "@(#)popen.c	8.1 (Berkeley) 6/6/93";
     36   1.4  christos #else
     37  1.25  christos __RCSID("$NetBSD: popen.c,v 1.25 2009/04/10 13:08:25 christos Exp $");
     38   1.4  christos #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41  1.20  christos #include <errno.h>
     42  1.16  christos #include <fcntl.h>
     43  1.16  christos #include <stdarg.h>
     44  1.20  christos #include <util.h>
     45  1.20  christos #include <sys/wait.h>
     46  1.20  christos 
     47  1.20  christos #include "rcv.h"
     48   1.3   deraadt #include "extern.h"
     49  1.25  christos #include "sig.h"
     50   1.1       cgd 
     51   1.1       cgd #define READ 0
     52   1.1       cgd #define WRITE 1
     53   1.1       cgd 
     54   1.1       cgd struct fp {
     55   1.1       cgd 	FILE *fp;
     56   1.1       cgd 	int pipe;
     57  1.16  christos 	pid_t pid;
     58   1.1       cgd 	struct fp *link;
     59   1.1       cgd };
     60   1.1       cgd static struct fp *fp_head;
     61   1.1       cgd 
     62   1.3   deraadt struct child {
     63  1.16  christos 	pid_t pid;
     64   1.3   deraadt 	char done;
     65   1.3   deraadt 	char free;
     66   1.9  christos 	int status;
     67   1.3   deraadt 	struct child *link;
     68   1.3   deraadt };
     69  1.16  christos static struct child *child, *child_freelist = NULL;
     70  1.16  christos 
     71   1.3   deraadt 
     72  1.23  christos #if 0	/* XXX - debugging stuff.  This should go away eventually! */
     73  1.22  christos static void
     74  1.22  christos show_one_file(FILE *fo, struct fp *fpp)
     75  1.22  christos {
     76  1.22  christos 	(void)fprintf(fo, ">>> fp: %p,  pipe: %d,  pid: %d,  link: %p\n",
     77  1.22  christos 	    fpp->fp, fpp->pipe, fpp->pid, fpp->link);
     78  1.22  christos }
     79  1.22  christos 
     80  1.22  christos void show_all_files(FILE *fo);
     81  1.22  christos __unused
     82  1.22  christos PUBLIC void
     83  1.22  christos show_all_files(FILE *fo)
     84  1.22  christos {
     85  1.22  christos 	struct fp *fpp;
     86  1.23  christos 
     87  1.22  christos 	(void)fprintf(fo, ">> FILES\n");
     88  1.22  christos 	for (fpp = fp_head; fpp; fpp = fpp->link)
     89  1.22  christos 		show_one_file(fo, fpp);
     90  1.22  christos 	(void)fprintf(fo, ">> -------\n");
     91  1.22  christos 	(void)fflush(fo);
     92  1.22  christos }
     93  1.22  christos #endif /* end debugging stuff */
     94  1.22  christos 
     95  1.22  christos 
     96  1.22  christos static void
     97  1.22  christos unregister_file(FILE *fp)
     98  1.22  christos {
     99  1.22  christos 	struct fp **pp, *p;
    100  1.22  christos 
    101  1.22  christos 	for (pp = &fp_head; (p = *pp) != NULL; pp = &p->link)
    102  1.22  christos 		if (p->fp == fp) {
    103  1.22  christos 			*pp = p->link;
    104  1.22  christos 			(void)free(p);
    105  1.22  christos 			return;
    106  1.22  christos 		}
    107  1.22  christos 	errx(1, "Invalid file pointer");
    108  1.22  christos }
    109  1.22  christos 
    110  1.22  christos PUBLIC void
    111  1.22  christos register_file(FILE *fp, int pipefd, pid_t pid)
    112  1.22  christos {
    113  1.22  christos 	struct fp *fpp;
    114  1.22  christos 
    115  1.23  christos 	fpp = emalloc(sizeof(*fpp));
    116  1.22  christos 	fpp->fp = fp;
    117  1.22  christos 	fpp->pipe = pipefd;
    118  1.22  christos 	fpp->pid = pid;
    119  1.22  christos 	fpp->link = fp_head;
    120  1.22  christos 	fp_head = fpp;
    121  1.22  christos }
    122  1.22  christos 
    123  1.22  christos PUBLIC FILE *
    124  1.18  christos Fopen(const char *fn, const char *mode)
    125   1.1       cgd {
    126   1.1       cgd 	FILE *fp;
    127   1.1       cgd 
    128  1.11       wiz 	if ((fp = fopen(fn, mode)) != NULL) {
    129   1.3   deraadt 		register_file(fp, 0, 0);
    130  1.20  christos 		(void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
    131   1.3   deraadt 	}
    132   1.1       cgd 	return fp;
    133   1.1       cgd }
    134   1.1       cgd 
    135  1.22  christos PUBLIC FILE *
    136  1.18  christos Fdopen(int fd, const char *mode)
    137   1.1       cgd {
    138   1.1       cgd 	FILE *fp;
    139   1.1       cgd 
    140   1.3   deraadt 	if ((fp = fdopen(fd, mode)) != NULL) {
    141   1.3   deraadt 		register_file(fp, 0, 0);
    142  1.20  christos 		(void)fcntl(fileno(fp), F_SETFD, FD_CLOEXEC);
    143   1.3   deraadt 	}
    144   1.1       cgd 	return fp;
    145   1.1       cgd }
    146   1.1       cgd 
    147  1.22  christos PUBLIC int
    148  1.10       wiz Fclose(FILE *fp)
    149   1.1       cgd {
    150  1.25  christos 
    151  1.25  christos 	if (fp == NULL)
    152  1.25  christos 		return 0;
    153  1.25  christos 
    154   1.1       cgd 	unregister_file(fp);
    155   1.1       cgd 	return fclose(fp);
    156   1.1       cgd }
    157   1.1       cgd 
    158  1.22  christos PUBLIC void
    159  1.22  christos prepare_child(sigset_t *nset, int infd, int outfd)
    160  1.22  christos {
    161  1.22  christos 	int i;
    162  1.22  christos 	sigset_t eset;
    163  1.22  christos 
    164  1.22  christos 	/*
    165  1.22  christos 	 * All file descriptors other than 0, 1, and 2 are supposed to be
    166  1.22  christos 	 * close-on-exec.
    167  1.22  christos 	 */
    168  1.22  christos 	if (infd > 0) {
    169  1.22  christos 		(void)dup2(infd, 0);
    170  1.22  christos 	} else if (infd != 0) {
    171  1.22  christos 		/* we don't want the child stealing my stdin input */
    172  1.22  christos 		(void)close(0);
    173  1.22  christos 		(void)open(_PATH_DEVNULL, O_RDONLY, 0);
    174  1.22  christos 	}
    175  1.22  christos 	if (outfd >= 0 && outfd != 1)
    176  1.22  christos 		(void)dup2(outfd, 1);
    177  1.25  christos 
    178  1.22  christos 	if (nset != NULL) {
    179  1.25  christos 		for (i = 1; i < NSIG; i++) {
    180  1.22  christos 			if (sigismember(nset, i))
    181  1.22  christos 				(void)signal(i, SIG_IGN);
    182  1.25  christos 		}
    183  1.25  christos 		if (!sigismember(nset, SIGINT))
    184  1.25  christos 			(void)signal(SIGINT, SIG_DFL);
    185  1.25  christos 		(void)sigemptyset(&eset);
    186  1.25  christos 		(void)sigprocmask(SIG_SETMASK, &eset, NULL);
    187  1.22  christos 	}
    188  1.22  christos }
    189  1.22  christos 
    190  1.22  christos /*
    191  1.22  christos  * Run a command without a shell, with optional arguments and splicing
    192  1.22  christos  * of stdin (-1 means none) and stdout.  The command name can be a sequence
    193  1.22  christos  * of words.
    194  1.22  christos  * Signals must be handled by the caller.
    195  1.22  christos  * "nset" contains the signals to ignore in the new process.
    196  1.22  christos  * SIGINT is enabled unless it's in "nset".
    197  1.22  christos  */
    198  1.22  christos static pid_t
    199  1.22  christos start_commandv(const char *cmd, sigset_t *nset, int infd, int outfd,
    200  1.22  christos     va_list args)
    201  1.22  christos {
    202  1.22  christos 	pid_t pid;
    203  1.22  christos 
    204  1.25  christos 	sig_check();
    205  1.22  christos 	if ((pid = fork()) < 0) {
    206  1.22  christos 		warn("fork");
    207  1.22  christos 		return -1;
    208  1.22  christos 	}
    209  1.22  christos 	if (pid == 0) {
    210  1.22  christos 		char *argv[100];
    211  1.25  christos 		size_t i;
    212  1.22  christos 
    213  1.25  christos 		i = getrawlist(cmd, argv, (int)__arraycount(argv));
    214  1.25  christos 		while (i < __arraycount(argv) - 1 &&
    215  1.25  christos 		    (argv[i++] = va_arg(args, char *)) != NULL)
    216  1.22  christos 			continue;
    217  1.22  christos 		argv[i] = NULL;
    218  1.22  christos 		prepare_child(nset, infd, outfd);
    219  1.22  christos 		(void)execvp(argv[0], argv);
    220  1.22  christos 		warn("%s", argv[0]);
    221  1.22  christos 		_exit(1);
    222  1.22  christos 	}
    223  1.22  christos 	return pid;
    224  1.22  christos }
    225  1.22  christos 
    226  1.22  christos PUBLIC int
    227  1.22  christos start_command(const char *cmd, sigset_t *nset, int infd, int outfd, ...)
    228  1.22  christos {
    229  1.22  christos 	va_list args;
    230  1.22  christos 	int r;
    231  1.22  christos 
    232  1.22  christos 	va_start(args, outfd);
    233  1.22  christos 	r = start_commandv(cmd, nset, infd, outfd, args);
    234  1.22  christos 	va_end(args);
    235  1.22  christos 	return r;
    236  1.22  christos }
    237  1.22  christos 
    238  1.22  christos PUBLIC FILE *
    239  1.18  christos Popen(const char *cmd, const char *mode)
    240   1.1       cgd {
    241   1.1       cgd 	int p[2];
    242   1.1       cgd 	int myside, hisside, fd0, fd1;
    243  1.16  christos 	pid_t pid;
    244   1.4  christos 	sigset_t nset;
    245   1.1       cgd 	FILE *fp;
    246  1.20  christos 	char *shellcmd;
    247   1.1       cgd 
    248   1.1       cgd 	if (pipe(p) < 0)
    249   1.1       cgd 		return NULL;
    250  1.20  christos 	(void)fcntl(p[READ], F_SETFD, FD_CLOEXEC);
    251  1.20  christos 	(void)fcntl(p[WRITE], F_SETFD, FD_CLOEXEC);
    252   1.1       cgd 	if (*mode == 'r') {
    253   1.1       cgd 		myside = p[READ];
    254  1.16  christos 		hisside = fd0 = fd1 = p[WRITE];
    255   1.1       cgd 	} else {
    256   1.1       cgd 		myside = p[WRITE];
    257   1.1       cgd 		hisside = fd0 = p[READ];
    258   1.1       cgd 		fd1 = -1;
    259   1.1       cgd 	}
    260  1.19  christos 	(void)sigemptyset(&nset);
    261  1.22  christos 	if ((shellcmd = value(ENAME_SHELL)) == NULL)
    262  1.20  christos 		shellcmd = __UNCONST(_PATH_CSHELL);
    263  1.20  christos 	pid = start_command(shellcmd, &nset, fd0, fd1, "-c", cmd, NULL);
    264  1.16  christos 	if (pid < 0) {
    265  1.16  christos 		(void)close(p[READ]);
    266  1.16  christos 		(void)close(p[WRITE]);
    267   1.1       cgd 		return NULL;
    268   1.1       cgd 	}
    269  1.13       wiz 	(void)close(hisside);
    270   1.1       cgd 	if ((fp = fdopen(myside, mode)) != NULL)
    271   1.3   deraadt 		register_file(fp, 1, pid);
    272   1.1       cgd 	return fp;
    273   1.1       cgd }
    274   1.1       cgd 
    275  1.22  christos static struct child *
    276  1.22  christos findchild(pid_t pid, int dont_alloc)
    277  1.22  christos {
    278  1.22  christos 	struct child **cpp;
    279  1.22  christos 
    280  1.22  christos 	for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
    281  1.22  christos 	     cpp = &(*cpp)->link)
    282  1.22  christos 			continue;
    283  1.22  christos 	if (*cpp == NULL) {
    284  1.22  christos 		if (dont_alloc)
    285  1.22  christos 			return NULL;
    286  1.22  christos 		if (child_freelist) {
    287  1.22  christos 			*cpp = child_freelist;
    288  1.22  christos 			child_freelist = (*cpp)->link;
    289  1.22  christos 		} else
    290  1.24  christos 			*cpp = emalloc(sizeof(**cpp));
    291  1.22  christos 
    292  1.22  christos 		(*cpp)->pid = pid;
    293  1.22  christos 		(*cpp)->done = (*cpp)->free = 0;
    294  1.22  christos 		(*cpp)->link = NULL;
    295  1.22  christos 	}
    296  1.22  christos 	return *cpp;
    297  1.22  christos }
    298  1.22  christos 
    299  1.22  christos static void
    300  1.22  christos delchild(struct child *cp)
    301  1.22  christos {
    302  1.22  christos 	struct child **cpp;
    303  1.22  christos 
    304  1.22  christos 	for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
    305  1.22  christos 		continue;
    306  1.22  christos 	*cpp = cp->link;
    307  1.22  christos 	cp->link = child_freelist;
    308  1.22  christos 	child_freelist = cp;
    309  1.22  christos }
    310  1.22  christos 
    311  1.22  christos /*
    312  1.22  christos  * Wait for a specific child to die.
    313  1.22  christos  */
    314  1.22  christos PUBLIC int
    315  1.22  christos wait_child(pid_t pid)
    316  1.22  christos {
    317  1.22  christos 	struct child *cp;
    318  1.22  christos 	sigset_t nset, oset;
    319  1.22  christos 	pid_t rv = 0;
    320  1.22  christos 
    321  1.22  christos 	(void)sigemptyset(&nset);
    322  1.22  christos 	(void)sigaddset(&nset, SIGCHLD);
    323  1.22  christos 	(void)sigprocmask(SIG_BLOCK, &nset, &oset);
    324  1.22  christos 	/*
    325  1.22  christos 	 * If we have not already waited on the pid (via sigchild)
    326  1.22  christos 	 * wait on it now.  Otherwise, use the wait status stashed
    327  1.22  christos 	 * by sigchild.
    328  1.22  christos 	 */
    329  1.22  christos 	cp = findchild(pid, 1);
    330  1.22  christos 	if (cp == NULL || !cp->done)
    331  1.22  christos 		rv = waitpid(pid, &wait_status, 0);
    332  1.22  christos 	else
    333  1.22  christos 		wait_status = cp->status;
    334  1.22  christos 	if (cp != NULL)
    335  1.22  christos 		delchild(cp);
    336  1.22  christos 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    337  1.22  christos 	if (rv == -1 || (WIFEXITED(wait_status) && WEXITSTATUS(wait_status)))
    338  1.22  christos 		return -1;
    339  1.22  christos 	else
    340  1.22  christos 		return 0;
    341  1.22  christos }
    342  1.22  christos 
    343  1.22  christos static pid_t
    344  1.22  christos file_pid(FILE *fp)
    345  1.22  christos {
    346  1.22  christos 	struct fp *p;
    347  1.22  christos 
    348  1.22  christos 	for (p = fp_head; p; p = p->link)
    349  1.22  christos 		if (p->fp == fp)
    350  1.22  christos 			return p->pid;
    351  1.22  christos 	errx(1, "Invalid file pointer");
    352  1.22  christos 	/*NOTREACHED*/
    353  1.22  christos }
    354  1.22  christos 
    355  1.22  christos PUBLIC int
    356  1.10       wiz Pclose(FILE *ptr)
    357   1.1       cgd {
    358   1.1       cgd 	int i;
    359   1.4  christos 	sigset_t nset, oset;
    360   1.1       cgd 
    361  1.25  christos 	if (ptr == NULL)
    362  1.25  christos 		return 0;
    363  1.25  christos 
    364   1.3   deraadt 	i = file_pid(ptr);
    365   1.1       cgd 	unregister_file(ptr);
    366  1.13       wiz 	(void)fclose(ptr);
    367  1.19  christos 	(void)sigemptyset(&nset);
    368  1.19  christos 	(void)sigaddset(&nset, SIGINT);
    369  1.19  christos 	(void)sigaddset(&nset, SIGHUP);
    370  1.19  christos 	(void)sigprocmask(SIG_BLOCK, &nset, &oset);
    371   1.3   deraadt 	i = wait_child(i);
    372  1.19  christos 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    373   1.1       cgd 	return i;
    374   1.1       cgd }
    375   1.1       cgd 
    376  1.22  christos PUBLIC void
    377  1.10       wiz close_all_files(void)
    378   1.1       cgd {
    379  1.20  christos 	while (fp_head)
    380  1.20  christos 		if (fp_head->pipe)
    381  1.20  christos 			(void)Pclose(fp_head->fp);
    382  1.20  christos 		else
    383  1.20  christos 			(void)Fclose(fp_head->fp);
    384  1.20  christos }
    385  1.20  christos 
    386  1.22  christos PUBLIC FILE *
    387  1.20  christos last_registered_file(int last_pipe)
    388  1.20  christos {
    389  1.20  christos 	struct fp *fpp;
    390  1.23  christos 
    391  1.20  christos 	if (last_pipe == 0)
    392  1.20  christos 		return fp_head ? fp_head->fp : NULL;
    393  1.20  christos 
    394  1.20  christos 	for (fpp = fp_head; fpp; fpp = fpp->link)
    395  1.20  christos 		if (fpp->pipe)
    396  1.20  christos 			return fpp->fp;
    397  1.20  christos 	return NULL;
    398  1.20  christos }
    399  1.20  christos 
    400  1.22  christos PUBLIC void
    401  1.20  christos close_top_files(FILE *fp_stop)
    402  1.20  christos {
    403  1.20  christos 	while (fp_head && fp_head->fp != fp_stop)
    404   1.1       cgd 		if (fp_head->pipe)
    405  1.13       wiz 			(void)Pclose(fp_head->fp);
    406   1.1       cgd 		else
    407  1.13       wiz 			(void)Fclose(fp_head->fp);
    408   1.1       cgd }
    409   1.1       cgd 
    410  1.22  christos #ifdef MIME_SUPPORT
    411  1.22  christos PUBLIC void
    412  1.20  christos flush_files(FILE *fo, int only_pipes)
    413  1.20  christos {
    414  1.20  christos 	struct fp *fpp;
    415  1.23  christos 
    416  1.20  christos 	if (fo)
    417  1.20  christos 		(void)fflush(fo);
    418  1.20  christos 
    419  1.20  christos 	for (fpp = fp_head; fpp; fpp = fpp->link)
    420  1.20  christos 		if (!only_pipes || fpp->pipe)
    421  1.20  christos 			(void)fflush(fpp->fp);
    422  1.20  christos 
    423  1.20  christos 	(void)fflush(stdout);
    424  1.20  christos }
    425  1.22  christos #endif /* MIME_SUPPORT */
    426  1.20  christos 
    427  1.22  christos static int
    428  1.22  christos wait_command(pid_t pid)
    429   1.1       cgd {
    430   1.1       cgd 
    431  1.22  christos 	if (wait_child(pid) < 0) {
    432  1.22  christos 		(void)puts("Fatal error in process.");
    433   1.1       cgd 		return -1;
    434   1.1       cgd 	}
    435  1.22  christos 	return 0;
    436   1.1       cgd }
    437   1.1       cgd 
    438  1.22  christos PUBLIC int
    439  1.18  christos run_command(const char *cmd, sigset_t *nset, int infd, int outfd, ...)
    440  1.16  christos {
    441  1.16  christos 	pid_t pid;
    442  1.16  christos 	va_list args;
    443  1.25  christos 	int rval;
    444  1.16  christos 
    445  1.25  christos #ifdef BROKEN_EXEC_TTY_RESTORE
    446  1.25  christos 	struct termios ttybuf;
    447  1.25  christos 	int tcrval;
    448  1.25  christos 	/*
    449  1.25  christos 	 * XXX - grab the tty settings as currently they can get
    450  1.25  christos 	 * trashed by emacs-21 when suspending with bash-3.2.25 as the
    451  1.25  christos 	 * shell.
    452  1.25  christos 	 *
    453  1.25  christos 	 * 1) from the mail editor, start "emacs -nw" (21.4)
    454  1.25  christos 	 * 2) suspend emacs to the shell (bash 3.2.25)
    455  1.25  christos 	 * 3) resume emacs
    456  1.25  christos 	 * 4) exit emacs back to the mail editor
    457  1.25  christos 	 * 5) discover the tty is screwed: the mail editor is no
    458  1.25  christos 	 *    longer receiving characters
    459  1.25  christos 	 *
    460  1.25  christos 	 * - This occurs on both i386 and amd64.
    461  1.25  christos 	 * - This did _NOT_ occur before 4.99.10.
    462  1.25  christos 	 * - This does _NOT_ occur if the editor is vi(1) or if the shell
    463  1.25  christos 	 *   is /bin/sh.
    464  1.25  christos 	 * - This _DOES_ happen with the old mail(1) from 2006-01-01 (long
    465  1.25  christos 	 *   before my changes).
    466  1.25  christos 	 *
    467  1.25  christos 	 * This is the commit that introduced this "feature":
    468  1.25  christos 	 * http://mail-index.netbsd.org/source-changes/2007/02/09/0020.html
    469  1.25  christos 	 */
    470  1.25  christos 	if ((tcrval = tcgetattr(fileno(stdin), &ttybuf)) == -1)
    471  1.25  christos 		warn("tcgetattr");
    472  1.25  christos #endif
    473  1.16  christos 	va_start(args, outfd);
    474  1.16  christos 	pid = start_commandv(cmd, nset, infd, outfd, args);
    475  1.16  christos 	va_end(args);
    476  1.16  christos 	if (pid < 0)
    477  1.16  christos 		return -1;
    478  1.25  christos 	rval = wait_command(pid);
    479  1.25  christos #ifdef BROKEN_EXEC_TTY_RESTORE
    480  1.25  christos 	if (tcrval != -1 && tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf) == -1)
    481  1.25  christos 		warn("tcsetattr");
    482  1.25  christos #endif
    483  1.25  christos 	return rval;
    484  1.25  christos 
    485  1.16  christos }
    486  1.16  christos 
    487  1.19  christos /*ARGSUSED*/
    488  1.22  christos PUBLIC void
    489  1.20  christos sigchild(int signo __unused)
    490   1.1       cgd {
    491  1.16  christos 	pid_t pid;
    492   1.9  christos 	int status;
    493   1.7     lukem 	struct child *cp;
    494  1.25  christos 	int save_errno;
    495   1.1       cgd 
    496  1.25  christos 	save_errno = errno;
    497  1.25  christos 	while ((pid = waitpid((pid_t)-1, &status, WNOHANG)) > 0) {
    498  1.25  christos 		cp = findchild(pid, 1);	/* async-signal-safe: we don't alloc */
    499  1.16  christos 		if (!cp)
    500  1.16  christos 			continue;
    501   1.1       cgd 		if (cp->free)
    502  1.25  christos 			delchild(cp);	/* async-signal-safe: list changes */
    503   1.1       cgd 		else {
    504   1.1       cgd 			cp->done = 1;
    505   1.1       cgd 			cp->status = status;
    506   1.1       cgd 		}
    507   1.1       cgd 	}
    508  1.16  christos 	errno = save_errno;
    509   1.1       cgd }
    510   1.1       cgd 
    511   1.1       cgd /*
    512   1.1       cgd  * Mark a child as don't care.
    513   1.1       cgd  */
    514  1.22  christos PUBLIC void
    515  1.16  christos free_child(pid_t pid)
    516   1.1       cgd {
    517  1.16  christos 	struct child *cp;
    518   1.4  christos 	sigset_t nset, oset;
    519  1.16  christos 
    520  1.19  christos 	(void)sigemptyset(&nset);
    521  1.19  christos 	(void)sigaddset(&nset, SIGCHLD);
    522  1.19  christos 	(void)sigprocmask(SIG_BLOCK, &nset, &oset);
    523  1.16  christos 	if ((cp = findchild(pid, 0)) != NULL) {
    524  1.16  christos 		if (cp->done)
    525  1.16  christos 			delchild(cp);
    526  1.16  christos 		else
    527  1.16  christos 			cp->free = 1;
    528  1.16  christos 	}
    529  1.19  christos 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
    530   1.1       cgd }
    531