xref: /illumos-gate/usr/src/cmd/ypcmd/ypupdated/openchild.c (revision a38ddfee9c8c6b6c5a2947ff52fd2338362a4444)
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 2005 Sun Microsystems, Inc.  All rights reserved.
24  *	Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #if !defined(lint) && defined(SCCSIDS)
38 static char sccsid[] = "%Z%%M%	%I%	%E% SMI";
39 #endif
40 
41 /*
42  * openchild.c
43  *
44  * Open two pipes to a child process, one for reading, one for writing.
45  * The pipes are accessed by FILE pointers. This is NOT a public
46  * interface, but for internal use only!
47  */
48 #include <stdio.h>
49 
50 extern void *malloc();
51 extern char *strrchr();
52 
53 static char *basename();
54 static char SHELL[] = "/bin/sh";
55 
56 extern int close();
57 extern int dup();
58 extern int execl();
59 extern void exit();
60 extern long fork();
61 extern int pipe();
62 extern unsigned int strlen();
63 
64 /*
65  * returns pid, or -1 for failure
66  */
67 int
68 _openchild(command, fto, ffrom)
69 	char *command;
70 	FILE **fto;
71 	FILE **ffrom;
72 {
73 	int i;
74 	int pid;
75 	int pdto[2];
76 	int pdfrom[2];
77 	char *com;
78 
79 
80 	if (pipe(pdto) < 0) {
81 		goto error1;
82 	}
83 	if (pipe(pdfrom) < 0) {
84 		goto error2;
85 	}
86 	switch (pid = fork()) {
87 	case -1:
88 		goto error3;
89 
90 	case 0:
91 		/*
92 		 * child: read from pdto[0], write into pdfrom[1]
93 		 */
94 		(void) close(0);
95 		(void) dup(pdto[0]);
96 		(void) close(1);
97 		(void) dup(pdfrom[1]);
98 
99 		/*
100 		 * adding this closelog for bug id 1238429
101 		 */
102 		closelog();
103 		closefrom(3);
104 
105 		com = malloc((unsigned)strlen(command) + 6);
106 		if (com == NULL) {
107 			exit(1);
108 		}
109 		(void) sprintf(com, "exec %s", command);
110 		execl(SHELL, basename(SHELL), "-c", com, NULL);
111 		exit(1);
112 
113 	default:
114 		/*
115 		 * parent: write into pdto[1], read from pdfrom[0]
116 		 */
117 		*fto = fdopen(pdto[1], "w");
118 		(void) close(pdto[0]);
119 		*ffrom = fdopen(pdfrom[0], "r");
120 		(void) close(pdfrom[1]);
121 		break;
122 	}
123 	return (pid);
124 
125 	/*
126 	 * error cleanup and return
127 	 */
128 error3:
129 printf("openchild: error3");
130 	(void) close(pdfrom[0]);
131 	(void) close(pdfrom[1]);
132 error2:
133 printf("openchild: error2");
134 	(void) close(pdto[0]);
135 	(void) close(pdto[1]);
136 error1:
137 printf("openchild: error1");
138 	return (-1);
139 }
140 
141 static char *
142 basename(path)
143 	char *path;
144 {
145 	char *p;
146 
147 	p = strrchr(path, '/');
148 	if (p == NULL) {
149 		return (path);
150 	} else {
151 		return (p + 1);
152 	}
153 }
154