10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*702Sth160488 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate * under license from the Regents of the University of California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate #if !defined(lint) && defined(SCCSIDS)
380Sstevel@tonic-gate static char sccsid[] = "%Z%%M% %I% %E% SMI";
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate * openchild.c
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * Open two pipes to a child process, one for reading, one for writing.
450Sstevel@tonic-gate * The pipes are accessed by FILE pointers. This is NOT a public
460Sstevel@tonic-gate * interface, but for internal use only!
470Sstevel@tonic-gate */
480Sstevel@tonic-gate #include <stdio.h>
490Sstevel@tonic-gate
500Sstevel@tonic-gate extern void *malloc();
510Sstevel@tonic-gate extern char *strrchr();
520Sstevel@tonic-gate
530Sstevel@tonic-gate static char *basename();
540Sstevel@tonic-gate static char SHELL[] = "/bin/sh";
550Sstevel@tonic-gate
560Sstevel@tonic-gate extern int close();
570Sstevel@tonic-gate extern int dup();
580Sstevel@tonic-gate extern int execl();
590Sstevel@tonic-gate extern void exit();
600Sstevel@tonic-gate extern long fork();
610Sstevel@tonic-gate extern int pipe();
620Sstevel@tonic-gate extern unsigned int strlen();
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * returns pid, or -1 for failure
660Sstevel@tonic-gate */
67*702Sth160488 int
_openchild(command,fto,ffrom)680Sstevel@tonic-gate _openchild(command, fto, ffrom)
690Sstevel@tonic-gate char *command;
700Sstevel@tonic-gate FILE **fto;
710Sstevel@tonic-gate FILE **ffrom;
720Sstevel@tonic-gate {
730Sstevel@tonic-gate int i;
740Sstevel@tonic-gate int pid;
750Sstevel@tonic-gate int pdto[2];
760Sstevel@tonic-gate int pdfrom[2];
770Sstevel@tonic-gate char *com;
780Sstevel@tonic-gate
790Sstevel@tonic-gate
800Sstevel@tonic-gate if (pipe(pdto) < 0) {
810Sstevel@tonic-gate goto error1;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate if (pipe(pdfrom) < 0) {
840Sstevel@tonic-gate goto error2;
850Sstevel@tonic-gate }
860Sstevel@tonic-gate switch (pid = fork()) {
870Sstevel@tonic-gate case -1:
880Sstevel@tonic-gate goto error3;
890Sstevel@tonic-gate
900Sstevel@tonic-gate case 0:
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * child: read from pdto[0], write into pdfrom[1]
930Sstevel@tonic-gate */
940Sstevel@tonic-gate (void) close(0);
950Sstevel@tonic-gate (void) dup(pdto[0]);
960Sstevel@tonic-gate (void) close(1);
970Sstevel@tonic-gate (void) dup(pdfrom[1]);
980Sstevel@tonic-gate
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate * adding this closelog for bug id 1238429
1010Sstevel@tonic-gate */
1020Sstevel@tonic-gate closelog();
1030Sstevel@tonic-gate closefrom(3);
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate com = malloc((unsigned)strlen(command) + 6);
1060Sstevel@tonic-gate if (com == NULL) {
1070Sstevel@tonic-gate exit(1);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate (void) sprintf(com, "exec %s", command);
1100Sstevel@tonic-gate execl(SHELL, basename(SHELL), "-c", com, NULL);
1110Sstevel@tonic-gate exit(1);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate default:
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate * parent: write into pdto[1], read from pdfrom[0]
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate *fto = fdopen(pdto[1], "w");
1180Sstevel@tonic-gate (void) close(pdto[0]);
1190Sstevel@tonic-gate *ffrom = fdopen(pdfrom[0], "r");
1200Sstevel@tonic-gate (void) close(pdfrom[1]);
1210Sstevel@tonic-gate break;
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate return (pid);
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * error cleanup and return
1270Sstevel@tonic-gate */
1280Sstevel@tonic-gate error3:
1290Sstevel@tonic-gate printf("openchild: error3");
1300Sstevel@tonic-gate (void) close(pdfrom[0]);
1310Sstevel@tonic-gate (void) close(pdfrom[1]);
1320Sstevel@tonic-gate error2:
1330Sstevel@tonic-gate printf("openchild: error2");
1340Sstevel@tonic-gate (void) close(pdto[0]);
1350Sstevel@tonic-gate (void) close(pdto[1]);
1360Sstevel@tonic-gate error1:
1370Sstevel@tonic-gate printf("openchild: error1");
1380Sstevel@tonic-gate return (-1);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate static char *
basename(path)1420Sstevel@tonic-gate basename(path)
1430Sstevel@tonic-gate char *path;
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate char *p;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate p = strrchr(path, '/');
1480Sstevel@tonic-gate if (p == NULL) {
1490Sstevel@tonic-gate return (path);
1500Sstevel@tonic-gate } else {
1510Sstevel@tonic-gate return (p + 1);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate }
154