Home | History | Annotate | Line # | Download | only in mail
popen.c revision 1.16
      1  1.16  christos /*	$NetBSD: popen.c,v 1.16 2003/03/29 21:41:04 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.7     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.4  christos #if 0
     39   1.4  christos static char sccsid[] = "@(#)popen.c	8.1 (Berkeley) 6/6/93";
     40   1.4  christos #else
     41  1.16  christos __RCSID("$NetBSD: popen.c,v 1.16 2003/03/29 21:41:04 christos Exp $");
     42   1.4  christos #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include "rcv.h"
     46  1.16  christos #include <sys/wait.h>
     47  1.16  christos #include <fcntl.h>
     48  1.16  christos #include <errno.h>
     49  1.16  christos #include <stdarg.h>
     50   1.3   deraadt #include "extern.h"
     51   1.1       cgd 
     52   1.1       cgd #define READ 0
     53   1.1       cgd #define WRITE 1
     54   1.1       cgd 
     55   1.1       cgd struct fp {
     56   1.1       cgd 	FILE *fp;
     57   1.1       cgd 	int pipe;
     58  1.16  christos 	pid_t pid;
     59   1.1       cgd 	struct fp *link;
     60   1.1       cgd };
     61   1.1       cgd static struct fp *fp_head;
     62   1.1       cgd 
     63   1.3   deraadt struct child {
     64  1.16  christos 	pid_t pid;
     65   1.3   deraadt 	char done;
     66   1.3   deraadt 	char free;
     67   1.9  christos 	int status;
     68   1.3   deraadt 	struct child *link;
     69   1.3   deraadt };
     70  1.16  christos static struct child *child, *child_freelist = NULL;
     71  1.16  christos 
     72  1.16  christos static struct child *findchild(pid_t, int);
     73  1.10       wiz static void delchild(struct child *);
     74  1.16  christos static pid_t file_pid(FILE *);
     75  1.16  christos static pid_t start_commandv(char *, sigset_t *, int, int, va_list);
     76   1.3   deraadt 
     77   1.1       cgd FILE *
     78  1.11       wiz Fopen(char *fn, char *mode)
     79   1.1       cgd {
     80   1.1       cgd 	FILE *fp;
     81   1.1       cgd 
     82  1.11       wiz 	if ((fp = fopen(fn, mode)) != NULL) {
     83   1.3   deraadt 		register_file(fp, 0, 0);
     84  1.13       wiz 		(void)fcntl(fileno(fp), F_SETFD, 1);
     85   1.3   deraadt 	}
     86   1.1       cgd 	return fp;
     87   1.1       cgd }
     88   1.1       cgd 
     89   1.1       cgd FILE *
     90  1.10       wiz Fdopen(int fd, char *mode)
     91   1.1       cgd {
     92   1.1       cgd 	FILE *fp;
     93   1.1       cgd 
     94   1.3   deraadt 	if ((fp = fdopen(fd, mode)) != NULL) {
     95   1.3   deraadt 		register_file(fp, 0, 0);
     96  1.13       wiz 		(void)fcntl(fileno(fp), F_SETFD, 1);
     97   1.3   deraadt 	}
     98   1.1       cgd 	return fp;
     99   1.1       cgd }
    100   1.1       cgd 
    101   1.3   deraadt int
    102  1.10       wiz Fclose(FILE *fp)
    103   1.1       cgd {
    104  1.16  christos 
    105   1.1       cgd 	unregister_file(fp);
    106   1.1       cgd 	return fclose(fp);
    107   1.1       cgd }
    108   1.1       cgd 
    109   1.1       cgd FILE *
    110  1.10       wiz Popen(char *cmd, char *mode)
    111   1.1       cgd {
    112   1.1       cgd 	int p[2];
    113   1.1       cgd 	int myside, hisside, fd0, fd1;
    114  1.16  christos 	pid_t pid;
    115   1.4  christos 	sigset_t nset;
    116   1.1       cgd 	FILE *fp;
    117   1.1       cgd 
    118   1.1       cgd 	if (pipe(p) < 0)
    119   1.1       cgd 		return NULL;
    120  1.13       wiz 	(void)fcntl(p[READ], F_SETFD, 1);
    121  1.13       wiz 	(void)fcntl(p[WRITE], F_SETFD, 1);
    122   1.1       cgd 	if (*mode == 'r') {
    123   1.1       cgd 		myside = p[READ];
    124  1.16  christos 		hisside = fd0 = fd1 = p[WRITE];
    125   1.1       cgd 	} else {
    126   1.1       cgd 		myside = p[WRITE];
    127   1.1       cgd 		hisside = fd0 = p[READ];
    128   1.1       cgd 		fd1 = -1;
    129   1.1       cgd 	}
    130   1.4  christos 	sigemptyset(&nset);
    131  1.16  christos 	pid = start_command(value("SHELL"), &nset, fd0, fd1, "-c", cmd, NULL);
    132  1.16  christos 	if (pid < 0) {
    133  1.16  christos 		(void)close(p[READ]);
    134  1.16  christos 		(void)close(p[WRITE]);
    135   1.1       cgd 		return NULL;
    136   1.1       cgd 	}
    137  1.13       wiz 	(void)close(hisside);
    138   1.1       cgd 	if ((fp = fdopen(myside, mode)) != NULL)
    139   1.3   deraadt 		register_file(fp, 1, pid);
    140   1.1       cgd 	return fp;
    141   1.1       cgd }
    142   1.1       cgd 
    143   1.3   deraadt int
    144  1.10       wiz Pclose(FILE *ptr)
    145   1.1       cgd {
    146   1.1       cgd 	int i;
    147   1.4  christos 	sigset_t nset, oset;
    148   1.1       cgd 
    149   1.3   deraadt 	i = file_pid(ptr);
    150   1.1       cgd 	unregister_file(ptr);
    151  1.13       wiz 	(void)fclose(ptr);
    152   1.4  christos 	sigemptyset(&nset);
    153   1.4  christos 	sigaddset(&nset, SIGINT);
    154   1.4  christos 	sigaddset(&nset, SIGHUP);
    155   1.4  christos 	sigprocmask(SIG_BLOCK, &nset, &oset);
    156   1.3   deraadt 	i = wait_child(i);
    157   1.4  christos 	sigprocmask(SIG_SETMASK, &oset, NULL);
    158   1.1       cgd 	return i;
    159   1.1       cgd }
    160   1.1       cgd 
    161   1.3   deraadt void
    162  1.10       wiz close_all_files(void)
    163   1.1       cgd {
    164   1.1       cgd 
    165   1.1       cgd 	while (fp_head)
    166   1.1       cgd 		if (fp_head->pipe)
    167  1.13       wiz 			(void)Pclose(fp_head->fp);
    168   1.1       cgd 		else
    169  1.13       wiz 			(void)Fclose(fp_head->fp);
    170   1.1       cgd }
    171   1.1       cgd 
    172   1.3   deraadt void
    173  1.16  christos register_file(FILE *fp, int pipefd, pid_t pid)
    174   1.1       cgd {
    175   1.1       cgd 	struct fp *fpp;
    176   1.1       cgd 
    177  1.16  christos 	if ((fpp = (struct fp *)malloc(sizeof(*fpp))) == NULL)
    178   1.7     lukem 		errx(1, "Out of memory");
    179   1.1       cgd 	fpp->fp = fp;
    180  1.11       wiz 	fpp->pipe = pipefd;
    181   1.3   deraadt 	fpp->pid = pid;
    182   1.1       cgd 	fpp->link = fp_head;
    183   1.1       cgd 	fp_head = fpp;
    184   1.1       cgd }
    185   1.1       cgd 
    186   1.3   deraadt void
    187  1.10       wiz unregister_file(FILE *fp)
    188   1.1       cgd {
    189   1.1       cgd 	struct fp **pp, *p;
    190   1.1       cgd 
    191   1.4  christos 	for (pp = &fp_head; (p = *pp) != NULL; pp = &p->link)
    192   1.1       cgd 		if (p->fp == fp) {
    193   1.1       cgd 			*pp = p->link;
    194  1.16  christos 			(void)free(p);
    195   1.1       cgd 			return;
    196   1.1       cgd 		}
    197   1.7     lukem 	errx(1, "Invalid file pointer");
    198   1.3   deraadt }
    199   1.3   deraadt 
    200  1.16  christos static pid_t
    201  1.10       wiz file_pid(FILE *fp)
    202   1.3   deraadt {
    203   1.3   deraadt 	struct fp *p;
    204   1.3   deraadt 
    205   1.3   deraadt 	for (p = fp_head; p; p = p->link)
    206   1.3   deraadt 		if (p->fp == fp)
    207  1.16  christos 			return p->pid;
    208   1.7     lukem 	errx(1, "Invalid file pointer");
    209   1.3   deraadt 	/*NOTREACHED*/
    210   1.1       cgd }
    211   1.1       cgd 
    212   1.1       cgd /*
    213   1.1       cgd  * Run a command without a shell, with optional arguments and splicing
    214  1.16  christos  * of stdin (-1 means none) and stdout.  The command name can be a sequence
    215  1.16  christos  * of words.
    216   1.1       cgd  * Signals must be handled by the caller.
    217  1.16  christos  * "nset" contains the signals to ignore in the new process.
    218  1.16  christos  * SIGINT is enabled unless it's in "nset".
    219   1.1       cgd  */
    220  1.16  christos static pid_t
    221  1.16  christos start_commandv(char *cmd, sigset_t *nset, int infd, int outfd, va_list args)
    222   1.1       cgd {
    223  1.16  christos 	pid_t pid;
    224   1.1       cgd 
    225  1.16  christos 	if ((pid = fork()) < 0) {
    226  1.14       wiz 		warn("fork");
    227   1.1       cgd 		return -1;
    228   1.1       cgd 	}
    229   1.1       cgd 	if (pid == 0) {
    230   1.1       cgd 		char *argv[100];
    231  1.16  christos 		int i = getrawlist(cmd, argv, sizeof(argv)/ sizeof(*argv));
    232   1.1       cgd 
    233  1.16  christos 		while ((argv[i++] = va_arg(args, char *)))
    234  1.16  christos 			;
    235  1.16  christos 		argv[i] = NULL;
    236  1.16  christos 		prepare_child(nset, infd, outfd);
    237   1.1       cgd 		execvp(argv[0], argv);
    238  1.14       wiz 		warn("%s", argv[0]);
    239   1.1       cgd 		_exit(1);
    240   1.1       cgd 	}
    241   1.1       cgd 	return pid;
    242   1.1       cgd }
    243   1.1       cgd 
    244  1.16  christos int
    245  1.16  christos run_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
    246  1.16  christos {
    247  1.16  christos 	pid_t pid;
    248  1.16  christos 	va_list args;
    249  1.16  christos 
    250  1.16  christos 	va_start(args, outfd);
    251  1.16  christos 	pid = start_commandv(cmd, nset, infd, outfd, args);
    252  1.16  christos 	va_end(args);
    253  1.16  christos 	if (pid < 0)
    254  1.16  christos 		return -1;
    255  1.16  christos 	return wait_command(pid);
    256  1.16  christos }
    257  1.16  christos 
    258  1.16  christos int
    259  1.16  christos start_command(char *cmd, sigset_t *nset, int infd, int outfd, ...)
    260  1.16  christos {
    261  1.16  christos 	va_list args;
    262  1.16  christos 	int r;
    263  1.16  christos 
    264  1.16  christos 	va_start(args, outfd);
    265  1.16  christos 	r = start_commandv(cmd, nset, infd, outfd, args);
    266  1.16  christos 	va_end(args);
    267  1.16  christos 	return r;
    268  1.16  christos }
    269  1.16  christos 
    270   1.3   deraadt void
    271  1.10       wiz prepare_child(sigset_t *nset, int infd, int outfd)
    272   1.1       cgd {
    273   1.1       cgd 	int i;
    274   1.8   mycroft 	sigset_t eset;
    275   1.1       cgd 
    276   1.3   deraadt 	/*
    277   1.3   deraadt 	 * All file descriptors other than 0, 1, and 2 are supposed to be
    278   1.3   deraadt 	 * close-on-exec.
    279   1.3   deraadt 	 */
    280  1.16  christos 	if (infd > 0) {
    281   1.1       cgd 		dup2(infd, 0);
    282  1.16  christos 	} else if (infd != 0) {
    283  1.16  christos 		/* we don't want the child stealing my stdin input */
    284  1.16  christos 		close(0);
    285  1.16  christos 		open(_PATH_DEVNULL, O_RDONLY, 0);
    286  1.16  christos 	}
    287  1.16  christos 	if (outfd >= 0 && outfd != 1)
    288   1.1       cgd 		dup2(outfd, 1);
    289  1.16  christos 	if (nset == NULL)
    290  1.16  christos 		return;
    291  1.16  christos 	if (nset != NULL) {
    292  1.16  christos 		for (i = 1; i < NSIG; i++)
    293  1.16  christos 			if (sigismember(nset, i))
    294  1.16  christos 				(void)signal(i, SIG_IGN);
    295  1.16  christos 	}
    296   1.5  christos 	if (nset == NULL || !sigismember(nset, SIGINT))
    297  1.13       wiz 		(void)signal(SIGINT, SIG_DFL);
    298   1.8   mycroft 	sigemptyset(&eset);
    299  1.13       wiz 	(void)sigprocmask(SIG_SETMASK, &eset, NULL);
    300   1.1       cgd }
    301   1.1       cgd 
    302   1.3   deraadt int
    303  1.16  christos wait_command(pid_t pid)
    304   1.1       cgd {
    305   1.1       cgd 
    306   1.1       cgd 	if (wait_child(pid) < 0) {
    307  1.16  christos 		puts("Fatal error in process.");
    308   1.1       cgd 		return -1;
    309   1.1       cgd 	}
    310   1.1       cgd 	return 0;
    311   1.1       cgd }
    312   1.1       cgd 
    313   1.3   deraadt static struct child *
    314  1.16  christos findchild(pid_t pid, int dont_alloc)
    315   1.1       cgd {
    316   1.7     lukem 	struct child **cpp;
    317   1.1       cgd 
    318   1.1       cgd 	for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
    319   1.1       cgd 	     cpp = &(*cpp)->link)
    320   1.1       cgd 			;
    321   1.1       cgd 	if (*cpp == NULL) {
    322  1.16  christos 		if (dont_alloc)
    323  1.16  christos 			return NULL;
    324  1.16  christos 		if (child_freelist) {
    325  1.16  christos 			*cpp = child_freelist;
    326  1.16  christos 			child_freelist = (*cpp)->link;
    327  1.16  christos 		} else {
    328  1.16  christos 			*cpp = (struct child *)malloc(sizeof(struct child));
    329  1.16  christos 			if (*cpp == NULL)
    330  1.16  christos 				errx(1, "Out of memory");
    331  1.16  christos 		}
    332   1.1       cgd 		(*cpp)->pid = pid;
    333   1.1       cgd 		(*cpp)->done = (*cpp)->free = 0;
    334   1.1       cgd 		(*cpp)->link = NULL;
    335   1.1       cgd 	}
    336   1.1       cgd 	return *cpp;
    337   1.1       cgd }
    338   1.1       cgd 
    339   1.3   deraadt static void
    340  1.10       wiz delchild(struct child *cp)
    341   1.1       cgd {
    342   1.7     lukem 	struct child **cpp;
    343   1.1       cgd 
    344   1.1       cgd 	for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
    345   1.1       cgd 		;
    346   1.1       cgd 	*cpp = cp->link;
    347  1.16  christos 	cp->link = child_freelist;
    348  1.16  christos 	child_freelist = cp;
    349   1.1       cgd }
    350   1.1       cgd 
    351   1.1       cgd void
    352  1.10       wiz sigchild(int signo)
    353   1.1       cgd {
    354  1.16  christos 	pid_t pid;
    355   1.9  christos 	int status;
    356   1.7     lukem 	struct child *cp;
    357  1.16  christos 	int save_errno = errno;
    358   1.1       cgd 
    359  1.16  christos 	while ((pid =
    360  1.16  christos 	    waitpid((pid_t)-1, &status, WNOHANG)) > 0) {
    361  1.16  christos 		cp = findchild(pid, 1);
    362  1.16  christos 		if (!cp)
    363  1.16  christos 			continue;
    364   1.1       cgd 		if (cp->free)
    365   1.1       cgd 			delchild(cp);
    366   1.1       cgd 		else {
    367   1.1       cgd 			cp->done = 1;
    368   1.1       cgd 			cp->status = status;
    369   1.1       cgd 		}
    370   1.1       cgd 	}
    371  1.16  christos 	errno = save_errno;
    372   1.1       cgd }
    373   1.1       cgd 
    374   1.9  christos int wait_status;
    375   1.1       cgd 
    376   1.1       cgd /*
    377   1.1       cgd  * Wait for a specific child to die.
    378   1.1       cgd  */
    379   1.3   deraadt int
    380  1.16  christos wait_child(pid_t pid)
    381   1.1       cgd {
    382  1.16  christos 	struct child *cp;
    383   1.4  christos 	sigset_t nset, oset;
    384  1.16  christos 	pid_t rv = 0;
    385  1.16  christos 
    386   1.4  christos 	sigemptyset(&nset);
    387   1.4  christos 	sigaddset(&nset, SIGCHLD);
    388   1.4  christos 	sigprocmask(SIG_BLOCK, &nset, &oset);
    389  1.16  christos 	/*
    390  1.16  christos 	 * If we have not already waited on the pid (via sigchild)
    391  1.16  christos 	 * wait on it now.  Otherwise, use the wait status stashed
    392  1.16  christos 	 * by sigchild.
    393  1.16  christos 	 */
    394  1.16  christos 	cp = findchild(pid, 1);
    395  1.16  christos 	if (cp == NULL || !cp->done)
    396  1.16  christos 		rv = waitpid(pid, &wait_status, 0);
    397  1.16  christos 	else
    398  1.16  christos 		wait_status = cp->status;
    399  1.16  christos 	if (cp != NULL)
    400  1.16  christos 		delchild(cp);
    401   1.4  christos 	sigprocmask(SIG_SETMASK, &oset, NULL);
    402  1.16  christos 	if (rv == -1 || (WIFEXITED(wait_status) && WEXITSTATUS(wait_status)))
    403  1.16  christos 		return -1;
    404  1.16  christos 	else
    405  1.16  christos 		return 0;
    406   1.1       cgd }
    407   1.1       cgd 
    408   1.1       cgd /*
    409   1.1       cgd  * Mark a child as don't care.
    410   1.1       cgd  */
    411   1.3   deraadt void
    412  1.16  christos free_child(pid_t pid)
    413   1.1       cgd {
    414  1.16  christos 	struct child *cp;
    415   1.4  christos 	sigset_t nset, oset;
    416  1.16  christos 
    417   1.4  christos 	sigemptyset(&nset);
    418   1.4  christos 	sigaddset(&nset, SIGCHLD);
    419   1.4  christos 	sigprocmask(SIG_BLOCK, &nset, &oset);
    420  1.16  christos 	if ((cp = findchild(pid, 0)) != NULL) {
    421  1.16  christos 		if (cp->done)
    422  1.16  christos 			delchild(cp);
    423  1.16  christos 		else
    424  1.16  christos 			cp->free = 1;
    425  1.16  christos 	}
    426   1.4  christos 	sigprocmask(SIG_SETMASK, &oset, NULL);
    427   1.1       cgd }
    428