1 // This is part of the iostream library, providing input/output for C++. 2 // Copyright (C) 1992 Per Bothner. 3 // 4 // This library is free software; you can redistribute it and/or 5 // modify it under the terms of the GNU Library General Public 6 // License as published by the Free Software Foundation; either 7 // version 2 of the License, or (at your option) any later version. 8 // 9 // This library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 // Library General Public License for more details. 13 // 14 // You should have received a copy of the GNU Library General Public 15 // License along with this library; if not, write to the Free 16 // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 18 #define _POSIX_SOURCE 19 #include "ioprivate.h" 20 #include "procbuf.h" 21 #include <signal.h> 22 #include <unistd.h> 23 #include <sys/wait.h> 24 25 26 #ifndef FORK 27 #define FORK vfork 28 #endif 29 30 procbuf::procbuf(const char *command, int mode) : filebuf() 31 { 32 open(command, mode); 33 } 34 35 procbuf *procbuf::open(const char *command, int mode) 36 { 37 int read_or_write; 38 if (is_open()) 39 return NULL; 40 int pipe_fds[2]; 41 int parent_end, child_end; 42 if (::pipe(pipe_fds) < 0) 43 return NULL; 44 if (mode == ios::in) { 45 parent_end = pipe_fds[0]; 46 child_end = pipe_fds[1]; 47 read_or_write = _S_NO_WRITES; 48 } 49 else { 50 parent_end = pipe_fds[1]; 51 child_end = pipe_fds[0]; 52 read_or_write = _S_NO_READS; 53 } 54 _pid = FORK(); 55 if (_pid == 0) { 56 ::close(parent_end); 57 int child_std_end = mode == ios::in ? 1 : 0; 58 if (child_end != child_std_end) { 59 ::dup2(child_end, child_std_end); 60 ::close(child_end); 61 } 62 ::execl("/bin/sh", "sh", "-c", command, NULL); 63 ::_exit(127); 64 } 65 ::close(child_end); 66 if (_pid < 0) { 67 ::close(parent_end); 68 return NULL; 69 } 70 _fb._fileno = parent_end; 71 xsetflags(read_or_write, _S_NO_READS|_S_NO_WRITES); 72 return this; 73 } 74 75 /* #define USE_SIGMASK */ 76 77 int procbuf::sys_close() 78 { 79 _G_pid_t wait_pid; 80 int status = filebuf::sys_close(); 81 if (status < 0) 82 return status; 83 int wstatus; 84 #if defined(SIG_BLOCK) && defined(SIG_SETMASK) 85 sigset_t set, oset; 86 sigemptyset (&set); 87 sigaddset (&set, SIGINT); 88 sigaddset (&set, SIGQUIT); 89 sigaddset (&set, SIGHUP); 90 sigprocmask (SIG_BLOCK, &set, &oset); 91 #else 92 #ifdef USE_SIGMASK 93 int mask = sigblock(sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGHUP)); 94 #else 95 typedef void (*void_func)(int); 96 void_func intsave = (void_func)signal(SIGINT, SIG_IGN); 97 void_func quitsave = (void_func)signal(SIGQUIT, SIG_IGN); 98 void_func hupsave = (void_func)signal(SIGHUP, SIG_IGN); 99 #endif 100 #endif 101 while ((wait_pid = wait(&wstatus)) != _pid && wait_pid != -1) { } 102 #if defined(SIG_BLOCK) && defined(SIG_SETMASK) 103 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL); 104 #else 105 #ifdef USE_SIGMASK 106 (void) sigsetmask(mask); 107 #else 108 signal(SIGINT, intsave); 109 signal(SIGQUIT, quitsave); 110 signal(SIGHUP, hupsave); 111 #endif 112 #endif 113 if (wait_pid == -1) 114 return -1; 115 return 0; 116 } 117 118 procbuf::~procbuf() 119 { 120 close(); 121 } 122