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 /* 36 * openchild.c 37 * 38 * Open two pipes to a child process, one for reading, one for writing. 39 * The pipes are accessed by FILE pointers. This is NOT a public 40 * interface, but for internal use only! 41 */ 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <strings.h> 45 46 static char *basename(char *); 47 static char SHELL[] = "/bin/sh"; 48 49 /* 50 * returns pid, or -1 for failure 51 */ 52 int 53 _openchild(char *command, FILE **fto, FILE **ffrom) 54 { 55 int i; 56 int pid; 57 int pdto[2]; 58 int pdfrom[2]; 59 char *com; 60 61 62 if (pipe(pdto) < 0) { 63 goto error1; 64 } 65 if (pipe(pdfrom) < 0) { 66 goto error2; 67 } 68 switch (pid = fork()) { 69 case -1: 70 goto error3; 71 72 case 0: 73 /* 74 * child: read from pdto[0], write into pdfrom[1] 75 */ 76 (void) close(0); 77 (void) dup(pdto[0]); 78 (void) close(1); 79 (void) dup(pdfrom[1]); 80 81 /* 82 * adding this closelog for bug id 1238429 83 */ 84 closelog(); 85 closefrom(3); 86 87 com = malloc((unsigned)strlen(command) + 6); 88 if (com == NULL) { 89 exit(1); 90 } 91 (void) sprintf(com, "exec %s", command); 92 execl(SHELL, basename(SHELL), "-c", com, NULL); 93 exit(1); 94 95 default: 96 /* 97 * parent: write into pdto[1], read from pdfrom[0] 98 */ 99 *fto = fdopen(pdto[1], "w"); 100 (void) close(pdto[0]); 101 *ffrom = fdopen(pdfrom[0], "r"); 102 (void) close(pdfrom[1]); 103 break; 104 } 105 return (pid); 106 107 /* 108 * error cleanup and return 109 */ 110 error3: 111 printf("openchild: error3"); 112 (void) close(pdfrom[0]); 113 (void) close(pdfrom[1]); 114 error2: 115 printf("openchild: error2"); 116 (void) close(pdto[0]); 117 (void) close(pdto[1]); 118 error1: 119 printf("openchild: error1"); 120 return (-1); 121 } 122 123 static char * 124 basename(char *path) 125 { 126 char *p; 127 128 p = strrchr(path, '/'); 129 if (p == NULL) { 130 return (path); 131 } else { 132 return (p + 1); 133 } 134 } 135