xref: /illumos-gate/usr/src/cmd/bnu/shio.c (revision a5f69788de7ac07553de47f7fec8c05a9a94c105)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1988 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "uucp.h"
34 
35 /*
36  * use shell to execute command with
37  * fi, fo, and fe as standard input/output/error
38  *	cmd 	-> command to execute
39  *	fi 	-> standard input
40  *	fo 	-> standard output
41  *	fe 	-> standard error
42  * return:
43  *	0		-> success
44  *	non zero	-> failure  -  status from child
45 			(Note - -1 means the fork failed)
46  */
47 int
48 shio(cmd, fi, fo, fe)
49 char *cmd, *fi, *fo, *fe;
50 {
51 	register pid_t pid, ret;
52 	int status;
53 
54 	if (fi == NULL)
55 		fi = "/dev/null";
56 	if (fo == NULL)
57 		fo = "/dev/null";
58 	if (fe == NULL)
59 		fe = "/dev/null";
60 
61 	DEBUG(3, "shio - %s\n", cmd);
62 	if ((pid = fork()) == 0) {
63 		(void) signal(SIGINT, SIG_IGN);
64 		(void) signal(SIGHUP, SIG_IGN);
65 		(void) signal(SIGQUIT, SIG_IGN);
66 		ucloselog();
67 		(void) close(Ifn);	/* close connection fd's */
68 		(void) close(Ofn);
69 		(void) close(0);	/* get stdin from file fi */
70 		if (open(fi, 0) != 0)
71 			exit(errno);
72 		(void) close(1);	/* divert stdout to fo */
73 		if (creat(fo, PUB_FILEMODE) != 1)
74 			exit(errno);
75 		(void) close(2);	/* divert stderr to fe */
76 		if (creat(fe, PUB_FILEMODE) != 2)
77 			exit(errno);
78 		(void) execle(SHELL, "sh", "-c", cmd, (char *) 0, Env);
79 		exit(100);
80 	}
81 
82 	/*
83 	 * the status returned from wait can never be -1
84 	 * see man page wait(2)
85 	 * So we use the -1 value to indicate fork failed
86 	 * or the wait failed.
87 	 */
88 	if (pid == -1)
89 		return(-1);
90 
91 	while ((ret = wait(&status)) != pid)
92 	    if (ret == -1 && errno != EINTR)
93 		return(-1);
94 	DEBUG(3, "status %d\n", status);
95 	return(status);
96 }
97