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