Home | History | Annotate | Line # | Download | only in mail
popen.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1980 Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)popen.c	5.16 (Berkeley) 4/1/91";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd #include "rcv.h"
     39  1.1  cgd #include <sys/signal.h>
     40  1.1  cgd #include <sys/wait.h>
     41  1.1  cgd 
     42  1.1  cgd #define READ 0
     43  1.1  cgd #define WRITE 1
     44  1.1  cgd static int *pid;
     45  1.1  cgd 
     46  1.1  cgd struct fp {
     47  1.1  cgd 	FILE *fp;
     48  1.1  cgd 	int pipe;
     49  1.1  cgd 	struct fp *link;
     50  1.1  cgd };
     51  1.1  cgd static struct fp *fp_head;
     52  1.1  cgd 
     53  1.1  cgd FILE *
     54  1.1  cgd Fopen(file, mode)
     55  1.1  cgd 	char *file, *mode;
     56  1.1  cgd {
     57  1.1  cgd 	FILE *fp;
     58  1.1  cgd 
     59  1.1  cgd 	if ((fp = fopen(file, mode)) != NULL)
     60  1.1  cgd 		register_file(fp, 0);
     61  1.1  cgd 	return fp;
     62  1.1  cgd }
     63  1.1  cgd 
     64  1.1  cgd FILE *
     65  1.1  cgd Fdopen(fd, mode)
     66  1.1  cgd 	char *mode;
     67  1.1  cgd {
     68  1.1  cgd 	FILE *fp;
     69  1.1  cgd 
     70  1.1  cgd 	if ((fp = fdopen(fd, mode)) != NULL)
     71  1.1  cgd 		register_file(fp, 0);
     72  1.1  cgd 	return fp;
     73  1.1  cgd }
     74  1.1  cgd 
     75  1.1  cgd Fclose(fp)
     76  1.1  cgd 	FILE *fp;
     77  1.1  cgd {
     78  1.1  cgd 	unregister_file(fp);
     79  1.1  cgd 	return fclose(fp);
     80  1.1  cgd }
     81  1.1  cgd 
     82  1.1  cgd FILE *
     83  1.1  cgd Popen(cmd, mode)
     84  1.1  cgd 	char *cmd;
     85  1.1  cgd 	char *mode;
     86  1.1  cgd {
     87  1.1  cgd 	int p[2];
     88  1.1  cgd 	int myside, hisside, fd0, fd1;
     89  1.1  cgd 	FILE *fp;
     90  1.1  cgd 
     91  1.1  cgd 	if (pid == 0)
     92  1.1  cgd 		pid = (int *) malloc((unsigned) sizeof (int) * getdtablesize());
     93  1.1  cgd 	if (pipe(p) < 0)
     94  1.1  cgd 		return NULL;
     95  1.1  cgd 	if (*mode == 'r') {
     96  1.1  cgd 		myside = p[READ];
     97  1.1  cgd 		fd0 = -1;
     98  1.1  cgd 		hisside = fd1 = p[WRITE];
     99  1.1  cgd 	} else {
    100  1.1  cgd 		myside = p[WRITE];
    101  1.1  cgd 		hisside = fd0 = p[READ];
    102  1.1  cgd 		fd1 = -1;
    103  1.1  cgd 	}
    104  1.1  cgd 	if ((pid[myside] = start_command(cmd, 0, fd0, fd1, NOSTR)) < 0) {
    105  1.1  cgd 		close(p[READ]);
    106  1.1  cgd 		close(p[WRITE]);
    107  1.1  cgd 		return NULL;
    108  1.1  cgd 	}
    109  1.1  cgd 	(void) close(hisside);
    110  1.1  cgd 	if ((fp = fdopen(myside, mode)) != NULL)
    111  1.1  cgd 		register_file(fp, 1);
    112  1.1  cgd 	return fp;
    113  1.1  cgd }
    114  1.1  cgd 
    115  1.1  cgd Pclose(ptr)
    116  1.1  cgd 	FILE *ptr;
    117  1.1  cgd {
    118  1.1  cgd 	int i;
    119  1.1  cgd 	int omask;
    120  1.1  cgd 
    121  1.1  cgd 	i = fileno(ptr);
    122  1.1  cgd 	unregister_file(ptr);
    123  1.1  cgd 	(void) fclose(ptr);
    124  1.1  cgd 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGHUP));
    125  1.1  cgd 	i = wait_child(pid[i]);
    126  1.1  cgd 	sigsetmask(omask);
    127  1.1  cgd 	return i;
    128  1.1  cgd }
    129  1.1  cgd 
    130  1.1  cgd close_all_files()
    131  1.1  cgd {
    132  1.1  cgd 
    133  1.1  cgd 	while (fp_head)
    134  1.1  cgd 		if (fp_head->pipe)
    135  1.1  cgd 			(void) Pclose(fp_head->fp);
    136  1.1  cgd 		else
    137  1.1  cgd 			(void) Fclose(fp_head->fp);
    138  1.1  cgd }
    139  1.1  cgd 
    140  1.1  cgd register_file(fp, pipe)
    141  1.1  cgd 	FILE *fp;
    142  1.1  cgd {
    143  1.1  cgd 	struct fp *fpp;
    144  1.1  cgd 
    145  1.1  cgd 	if ((fpp = (struct fp *) malloc(sizeof *fpp)) == NULL)
    146  1.1  cgd 		panic("Out of memory");
    147  1.1  cgd 	fpp->fp = fp;
    148  1.1  cgd 	fpp->pipe = pipe;
    149  1.1  cgd 	fpp->link = fp_head;
    150  1.1  cgd 	fp_head = fpp;
    151  1.1  cgd }
    152  1.1  cgd 
    153  1.1  cgd unregister_file(fp)
    154  1.1  cgd 	FILE *fp;
    155  1.1  cgd {
    156  1.1  cgd 	struct fp **pp, *p;
    157  1.1  cgd 
    158  1.1  cgd 	for (pp = &fp_head; p = *pp; pp = &p->link)
    159  1.1  cgd 		if (p->fp == fp) {
    160  1.1  cgd 			*pp = p->link;
    161  1.1  cgd 			free((char *) p);
    162  1.1  cgd 			return;
    163  1.1  cgd 		}
    164  1.1  cgd 	/* XXX
    165  1.1  cgd 	 * Ignore this for now; there may still be uncaught
    166  1.1  cgd 	 * duplicate closes.
    167  1.1  cgd 	panic("Invalid file pointer");
    168  1.1  cgd 	*/
    169  1.1  cgd }
    170  1.1  cgd 
    171  1.1  cgd /*
    172  1.1  cgd  * Run a command without a shell, with optional arguments and splicing
    173  1.1  cgd  * of stdin and stdout.  The command name can be a sequence of words.
    174  1.1  cgd  * Signals must be handled by the caller.
    175  1.1  cgd  * "Mask" contains the signals to ignore in the new process.
    176  1.1  cgd  * SIGINT is enabled unless it's in the mask.
    177  1.1  cgd  */
    178  1.1  cgd /*VARARGS4*/
    179  1.1  cgd run_command(cmd, mask, infd, outfd, a0, a1, a2)
    180  1.1  cgd 	char *cmd;
    181  1.1  cgd 	int mask, infd, outfd;
    182  1.1  cgd 	char *a0, *a1, *a2;
    183  1.1  cgd {
    184  1.1  cgd 	int pid;
    185  1.1  cgd 
    186  1.1  cgd 	if ((pid = start_command(cmd, mask, infd, outfd, a0, a1, a2)) < 0)
    187  1.1  cgd 		return -1;
    188  1.1  cgd 	return wait_command(pid);
    189  1.1  cgd }
    190  1.1  cgd 
    191  1.1  cgd /*VARARGS4*/
    192  1.1  cgd start_command(cmd, mask, infd, outfd, a0, a1, a2)
    193  1.1  cgd 	char *cmd;
    194  1.1  cgd 	int mask, infd, outfd;
    195  1.1  cgd 	char *a0, *a1, *a2;
    196  1.1  cgd {
    197  1.1  cgd 	int pid;
    198  1.1  cgd 
    199  1.1  cgd 	if ((pid = vfork()) < 0) {
    200  1.1  cgd 		perror("fork");
    201  1.1  cgd 		return -1;
    202  1.1  cgd 	}
    203  1.1  cgd 	if (pid == 0) {
    204  1.1  cgd 		char *argv[100];
    205  1.1  cgd 		int i = getrawlist(cmd, argv, sizeof argv / sizeof *argv);
    206  1.1  cgd 
    207  1.1  cgd 		if ((argv[i++] = a0) != NOSTR &&
    208  1.1  cgd 		    (argv[i++] = a1) != NOSTR &&
    209  1.1  cgd 		    (argv[i++] = a2) != NOSTR)
    210  1.1  cgd 			argv[i] = NOSTR;
    211  1.1  cgd 		prepare_child(mask, infd, outfd);
    212  1.1  cgd 		execvp(argv[0], argv);
    213  1.1  cgd 		perror(argv[0]);
    214  1.1  cgd 		_exit(1);
    215  1.1  cgd 	}
    216  1.1  cgd 	return pid;
    217  1.1  cgd }
    218  1.1  cgd 
    219  1.1  cgd prepare_child(mask, infd, outfd)
    220  1.1  cgd 	int mask, infd, outfd;
    221  1.1  cgd {
    222  1.1  cgd 	int i;
    223  1.1  cgd 
    224  1.1  cgd 	if (infd >= 0)
    225  1.1  cgd 		dup2(infd, 0);
    226  1.1  cgd 	if (outfd >= 0)
    227  1.1  cgd 		dup2(outfd, 1);
    228  1.1  cgd 	for (i = getdtablesize(); --i > 2;)
    229  1.1  cgd 		close(i);
    230  1.1  cgd 	for (i = 1; i <= NSIG; i++)
    231  1.1  cgd 		if (mask & sigmask(i))
    232  1.1  cgd 			(void) signal(i, SIG_IGN);
    233  1.1  cgd 	if ((mask & sigmask(SIGINT)) == 0)
    234  1.1  cgd 		(void) signal(SIGINT, SIG_DFL);
    235  1.1  cgd 	(void) sigsetmask(0);
    236  1.1  cgd }
    237  1.1  cgd 
    238  1.1  cgd wait_command(pid)
    239  1.1  cgd 	int pid;
    240  1.1  cgd {
    241  1.1  cgd 
    242  1.1  cgd 	if (wait_child(pid) < 0) {
    243  1.1  cgd 		printf("Fatal error in process.\n");
    244  1.1  cgd 		return -1;
    245  1.1  cgd 	}
    246  1.1  cgd 	return 0;
    247  1.1  cgd }
    248  1.1  cgd 
    249  1.1  cgd struct child {
    250  1.1  cgd 	int pid;
    251  1.1  cgd 	char done;
    252  1.1  cgd 	char free;
    253  1.1  cgd 	union wait status;
    254  1.1  cgd 	struct child *link;
    255  1.1  cgd };
    256  1.1  cgd static struct child *child;
    257  1.1  cgd 
    258  1.1  cgd struct child *
    259  1.1  cgd findchild(pid)
    260  1.1  cgd 	int pid;
    261  1.1  cgd {
    262  1.1  cgd 	register struct child **cpp;
    263  1.1  cgd 
    264  1.1  cgd 	for (cpp = &child; *cpp != NULL && (*cpp)->pid != pid;
    265  1.1  cgd 	     cpp = &(*cpp)->link)
    266  1.1  cgd 			;
    267  1.1  cgd 	if (*cpp == NULL) {
    268  1.1  cgd 		*cpp = (struct child *) malloc(sizeof (struct child));
    269  1.1  cgd 		(*cpp)->pid = pid;
    270  1.1  cgd 		(*cpp)->done = (*cpp)->free = 0;
    271  1.1  cgd 		(*cpp)->link = NULL;
    272  1.1  cgd 	}
    273  1.1  cgd 	return *cpp;
    274  1.1  cgd }
    275  1.1  cgd 
    276  1.1  cgd delchild(cp)
    277  1.1  cgd 	register struct child *cp;
    278  1.1  cgd {
    279  1.1  cgd 	register struct child **cpp;
    280  1.1  cgd 
    281  1.1  cgd 	for (cpp = &child; *cpp != cp; cpp = &(*cpp)->link)
    282  1.1  cgd 		;
    283  1.1  cgd 	*cpp = cp->link;
    284  1.1  cgd 	free((char *) cp);
    285  1.1  cgd }
    286  1.1  cgd 
    287  1.1  cgd void
    288  1.1  cgd sigchild()
    289  1.1  cgd {
    290  1.1  cgd 	int pid;
    291  1.1  cgd 	union wait status;
    292  1.1  cgd 	register struct child *cp;
    293  1.1  cgd 
    294  1.1  cgd 	while ((pid =
    295  1.1  cgd 	    wait3((int *)&status, WNOHANG, (struct rusage *)0)) > 0) {
    296  1.1  cgd 		cp = findchild(pid);
    297  1.1  cgd 		if (cp->free)
    298  1.1  cgd 			delchild(cp);
    299  1.1  cgd 		else {
    300  1.1  cgd 			cp->done = 1;
    301  1.1  cgd 			cp->status = status;
    302  1.1  cgd 		}
    303  1.1  cgd 	}
    304  1.1  cgd }
    305  1.1  cgd 
    306  1.1  cgd union wait wait_status;
    307  1.1  cgd 
    308  1.1  cgd /*
    309  1.1  cgd  * Wait for a specific child to die.
    310  1.1  cgd  */
    311  1.1  cgd wait_child(pid)
    312  1.1  cgd 	int pid;
    313  1.1  cgd {
    314  1.1  cgd 	int mask = sigblock(sigmask(SIGCHLD));
    315  1.1  cgd 	register struct child *cp = findchild(pid);
    316  1.1  cgd 
    317  1.1  cgd 	while (!cp->done)
    318  1.1  cgd 		sigpause(mask);
    319  1.1  cgd 	wait_status = cp->status;
    320  1.1  cgd 	delchild(cp);
    321  1.1  cgd 	sigsetmask(mask);
    322  1.1  cgd 	return wait_status.w_status ? -1 : 0;
    323  1.1  cgd }
    324  1.1  cgd 
    325  1.1  cgd /*
    326  1.1  cgd  * Mark a child as don't care.
    327  1.1  cgd  */
    328  1.1  cgd free_child(pid)
    329  1.1  cgd 	int pid;
    330  1.1  cgd {
    331  1.1  cgd 	int mask = sigblock(sigmask(SIGCHLD));
    332  1.1  cgd 	register struct child *cp = findchild(pid);
    333  1.1  cgd 
    334  1.1  cgd 	if (cp->done)
    335  1.1  cgd 		delchild(cp);
    336  1.1  cgd 	else
    337  1.1  cgd 		cp->free = 1;
    338  1.1  cgd 	sigsetmask(mask);
    339  1.1  cgd }
    340