xref: /titanic_44/usr/src/cmd/ypcmd/ypupdated/openchild.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 2004 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 _openchild(command, fto, ffrom)
68 	char *command;
69 	FILE **fto;
70 	FILE **ffrom;
71 {
72 	int i;
73 	int pid;
74 	int pdto[2];
75 	int pdfrom[2];
76 	char *com;
77 
78 
79 	if (pipe(pdto) < 0) {
80 		goto error1;
81 	}
82 	if (pipe(pdfrom) < 0) {
83 		goto error2;
84 	}
85 	switch (pid = fork()) {
86 	case -1:
87 		goto error3;
88 
89 	case 0:
90 		/*
91 		 * child: read from pdto[0], write into pdfrom[1]
92 		 */
93 		(void) close(0);
94 		(void) dup(pdto[0]);
95 		(void) close(1);
96 		(void) dup(pdfrom[1]);
97 
98 		/*
99 		 * adding this closelog for bug id 1238429
100 		 */
101 		closelog();
102 		closefrom(3);
103 
104 		com = malloc((unsigned)strlen(command) + 6);
105 		if (com == NULL) {
106 			exit(1);
107 		}
108 		(void) sprintf(com, "exec %s", command);
109 		execl(SHELL, basename(SHELL), "-c", com, NULL);
110 		exit(1);
111 
112 	default:
113 		/*
114 		 * parent: write into pdto[1], read from pdfrom[0]
115 		 */
116 		*fto = fdopen(pdto[1], "w");
117 		(void) close(pdto[0]);
118 		*ffrom = fdopen(pdfrom[0], "r");
119 		(void) close(pdfrom[1]);
120 		break;
121 	}
122 	return (pid);
123 
124 	/*
125 	 * error cleanup and return
126 	 */
127 error3:
128 printf("openchild: error3");
129 	(void) close(pdfrom[0]);
130 	(void) close(pdfrom[1]);
131 error2:
132 printf("openchild: error2");
133 	(void) close(pdto[0]);
134 	(void) close(pdto[1]);
135 error1:
136 printf("openchild: error1");
137 	return (-1);
138 }
139 
140 static char *
141 basename(path)
142 	char *path;
143 {
144 	char *p;
145 
146 	p = strrchr(path, '/');
147 	if (p == NULL) {
148 		return (path);
149 	} else {
150 		return (p + 1);
151 	}
152 }
153