add UNLEASHED_OBJ to unleashed.mk
[unleashed/tickless.git] / usr / src / cmd / ypcmd / ypupdated / openchild.c
bloba4deb1280a31c9e4054bf2b9716a55f199fee4b0
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
39 * openchild.c
41 * Open two pipes to a child process, one for reading, one for writing.
42 * The pipes are accessed by FILE pointers. This is NOT a public
43 * interface, but for internal use only!
45 #include <stdio.h>
47 extern void *malloc();
48 extern char *strrchr();
50 static char *basename();
51 static char SHELL[] = "/bin/sh";
53 extern int close();
54 extern int dup();
55 extern int execl();
56 extern void exit();
57 extern long fork();
58 extern int pipe();
59 extern unsigned int strlen();
62 * returns pid, or -1 for failure
64 int
65 _openchild(command, fto, ffrom)
66 char *command;
67 FILE **fto;
68 FILE **ffrom;
70 int i;
71 int pid;
72 int pdto[2];
73 int pdfrom[2];
74 char *com;
77 if (pipe(pdto) < 0) {
78 goto error1;
80 if (pipe(pdfrom) < 0) {
81 goto error2;
83 switch (pid = fork()) {
84 case -1:
85 goto error3;
87 case 0:
89 * child: read from pdto[0], write into pdfrom[1]
91 (void) close(0);
92 (void) dup(pdto[0]);
93 (void) close(1);
94 (void) dup(pdfrom[1]);
97 * adding this closelog for bug id 1238429
99 closelog();
100 closefrom(3);
102 com = malloc((unsigned)strlen(command) + 6);
103 if (com == NULL) {
104 exit(1);
106 (void) sprintf(com, "exec %s", command);
107 execl(SHELL, basename(SHELL), "-c", com, NULL);
108 exit(1);
110 default:
112 * parent: write into pdto[1], read from pdfrom[0]
114 *fto = fdopen(pdto[1], "w");
115 (void) close(pdto[0]);
116 *ffrom = fdopen(pdfrom[0], "r");
117 (void) close(pdfrom[1]);
118 break;
120 return (pid);
123 * error cleanup and return
125 error3:
126 printf("openchild: error3");
127 (void) close(pdfrom[0]);
128 (void) close(pdfrom[1]);
129 error2:
130 printf("openchild: error2");
131 (void) close(pdto[0]);
132 (void) close(pdto[1]);
133 error1:
134 printf("openchild: error1");
135 return (-1);
138 static char *
139 basename(path)
140 char *path;
142 char *p;
144 p = strrchr(path, '/');
145 if (p == NULL) {
146 return (path);
147 } else {
148 return (p + 1);