1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 32*7c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 33*7c478bd9Sstevel@tonic-gate */ 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #if !defined(lint) && defined(SCCSIDS) 38*7c478bd9Sstevel@tonic-gate static char sccsid[] = "%Z%%M% %I% %E% SMI"; 39*7c478bd9Sstevel@tonic-gate #endif 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate /* 42*7c478bd9Sstevel@tonic-gate * openchild.c 43*7c478bd9Sstevel@tonic-gate * 44*7c478bd9Sstevel@tonic-gate * Open two pipes to a child process, one for reading, one for writing. 45*7c478bd9Sstevel@tonic-gate * The pipes are accessed by FILE pointers. This is NOT a public 46*7c478bd9Sstevel@tonic-gate * interface, but for internal use only! 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate #include <stdio.h> 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate extern void *malloc(); 51*7c478bd9Sstevel@tonic-gate extern char *strrchr(); 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate static char *basename(); 54*7c478bd9Sstevel@tonic-gate static char SHELL[] = "/bin/sh"; 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate extern int close(); 57*7c478bd9Sstevel@tonic-gate extern int dup(); 58*7c478bd9Sstevel@tonic-gate extern int execl(); 59*7c478bd9Sstevel@tonic-gate extern void exit(); 60*7c478bd9Sstevel@tonic-gate extern long fork(); 61*7c478bd9Sstevel@tonic-gate extern int pipe(); 62*7c478bd9Sstevel@tonic-gate extern unsigned int strlen(); 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* 65*7c478bd9Sstevel@tonic-gate * returns pid, or -1 for failure 66*7c478bd9Sstevel@tonic-gate */ 67*7c478bd9Sstevel@tonic-gate _openchild(command, fto, ffrom) 68*7c478bd9Sstevel@tonic-gate char *command; 69*7c478bd9Sstevel@tonic-gate FILE **fto; 70*7c478bd9Sstevel@tonic-gate FILE **ffrom; 71*7c478bd9Sstevel@tonic-gate { 72*7c478bd9Sstevel@tonic-gate int i; 73*7c478bd9Sstevel@tonic-gate int pid; 74*7c478bd9Sstevel@tonic-gate int pdto[2]; 75*7c478bd9Sstevel@tonic-gate int pdfrom[2]; 76*7c478bd9Sstevel@tonic-gate char *com; 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate if (pipe(pdto) < 0) { 80*7c478bd9Sstevel@tonic-gate goto error1; 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate if (pipe(pdfrom) < 0) { 83*7c478bd9Sstevel@tonic-gate goto error2; 84*7c478bd9Sstevel@tonic-gate } 85*7c478bd9Sstevel@tonic-gate switch (pid = fork()) { 86*7c478bd9Sstevel@tonic-gate case -1: 87*7c478bd9Sstevel@tonic-gate goto error3; 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate case 0: 90*7c478bd9Sstevel@tonic-gate /* 91*7c478bd9Sstevel@tonic-gate * child: read from pdto[0], write into pdfrom[1] 92*7c478bd9Sstevel@tonic-gate */ 93*7c478bd9Sstevel@tonic-gate (void) close(0); 94*7c478bd9Sstevel@tonic-gate (void) dup(pdto[0]); 95*7c478bd9Sstevel@tonic-gate (void) close(1); 96*7c478bd9Sstevel@tonic-gate (void) dup(pdfrom[1]); 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate /* 99*7c478bd9Sstevel@tonic-gate * adding this closelog for bug id 1238429 100*7c478bd9Sstevel@tonic-gate */ 101*7c478bd9Sstevel@tonic-gate closelog(); 102*7c478bd9Sstevel@tonic-gate closefrom(3); 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate com = malloc((unsigned)strlen(command) + 6); 105*7c478bd9Sstevel@tonic-gate if (com == NULL) { 106*7c478bd9Sstevel@tonic-gate exit(1); 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate (void) sprintf(com, "exec %s", command); 109*7c478bd9Sstevel@tonic-gate execl(SHELL, basename(SHELL), "-c", com, NULL); 110*7c478bd9Sstevel@tonic-gate exit(1); 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate default: 113*7c478bd9Sstevel@tonic-gate /* 114*7c478bd9Sstevel@tonic-gate * parent: write into pdto[1], read from pdfrom[0] 115*7c478bd9Sstevel@tonic-gate */ 116*7c478bd9Sstevel@tonic-gate *fto = fdopen(pdto[1], "w"); 117*7c478bd9Sstevel@tonic-gate (void) close(pdto[0]); 118*7c478bd9Sstevel@tonic-gate *ffrom = fdopen(pdfrom[0], "r"); 119*7c478bd9Sstevel@tonic-gate (void) close(pdfrom[1]); 120*7c478bd9Sstevel@tonic-gate break; 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate return (pid); 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate /* 125*7c478bd9Sstevel@tonic-gate * error cleanup and return 126*7c478bd9Sstevel@tonic-gate */ 127*7c478bd9Sstevel@tonic-gate error3: 128*7c478bd9Sstevel@tonic-gate printf("openchild: error3"); 129*7c478bd9Sstevel@tonic-gate (void) close(pdfrom[0]); 130*7c478bd9Sstevel@tonic-gate (void) close(pdfrom[1]); 131*7c478bd9Sstevel@tonic-gate error2: 132*7c478bd9Sstevel@tonic-gate printf("openchild: error2"); 133*7c478bd9Sstevel@tonic-gate (void) close(pdto[0]); 134*7c478bd9Sstevel@tonic-gate (void) close(pdto[1]); 135*7c478bd9Sstevel@tonic-gate error1: 136*7c478bd9Sstevel@tonic-gate printf("openchild: error1"); 137*7c478bd9Sstevel@tonic-gate return (-1); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate static char * 141*7c478bd9Sstevel@tonic-gate basename(path) 142*7c478bd9Sstevel@tonic-gate char *path; 143*7c478bd9Sstevel@tonic-gate { 144*7c478bd9Sstevel@tonic-gate char *p; 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate p = strrchr(path, '/'); 147*7c478bd9Sstevel@tonic-gate if (p == NULL) { 148*7c478bd9Sstevel@tonic-gate return (path); 149*7c478bd9Sstevel@tonic-gate } else { 150*7c478bd9Sstevel@tonic-gate return (p + 1); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate } 153