1 /*********************************************************************** 2 * * 3 * This software is part of the ast package * 4 * Copyright (c) 1985-2008 AT&T Intellectual Property * 5 * and is licensed under the * 6 * Common Public License, Version 1.0 * 7 * by AT&T Intellectual Property * 8 * * 9 * A copy of the License is available at * 10 * http://www.opensource.org/licenses/cpl1.0.txt * 11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12 * * 13 * Information and Software Systems Research * 14 * AT&T Research * 15 * Florham Park NJ * 16 * * 17 * Glenn Fowler <gsf@research.att.com> * 18 * David Korn <dgk@research.att.com> * 19 * Phong Vo <kpv@research.att.com> * 20 * * 21 ***********************************************************************/ 22 #pragma prototyped 23 /* 24 * Glenn Fowler 25 * AT&T Research 26 * 27 * close a proc opened by procopen() 28 * otherwise exit() status of process is returned 29 */ 30 31 #include "proclib.h" 32 33 #include <wait.h> 34 35 int 36 procclose(register Proc_t* p) 37 { 38 int pid; 39 int flags = 0; 40 int status = -1; 41 42 if (p) 43 { 44 if (p->rfd >= 0) 45 close(p->rfd); 46 if (p->wfd >= 0 && p->wfd != p->rfd) 47 close(p->wfd); 48 if (p->flags & PROC_ZOMBIE) 49 { 50 /* 51 * process may leave a zombie behind 52 * give it a chance to do that but 53 * don't hang waiting for it 54 */ 55 56 flags |= WNOHANG; 57 sleep(1); 58 } 59 if (!(p->flags & PROC_FOREGROUND)) 60 sigcritical(SIG_REG_EXEC|SIG_REG_PROC); 61 while ((pid = waitpid(p->pid, &status, flags)) == -1 && errno == EINTR); 62 if (pid != p->pid && (flags & WNOHANG)) 63 status = 0; 64 if (!(p->flags & PROC_FOREGROUND)) 65 sigcritical(0); 66 else 67 { 68 if (p->sigint != SIG_IGN) 69 signal(SIGINT, p->sigint); 70 if (p->sigquit != SIG_IGN) 71 signal(SIGQUIT, p->sigquit); 72 #if defined(SIGCHLD) 73 #if _lib_sigprocmask 74 sigprocmask(SIG_SETMASK, &p->mask, NiL); 75 #else 76 #if _lib_sigsetmask 77 sigsetmask(p->mask); 78 #else 79 if (p->sigchld != SIG_DFL) 80 signal(SIGCHLD, p->sigchld); 81 #endif 82 #endif 83 #endif 84 } 85 status = status == -1 ? 86 EXIT_QUIT : 87 WIFSIGNALED(status) ? 88 EXIT_TERM(WTERMSIG(status)) : 89 EXIT_CODE(WEXITSTATUS(status)); 90 procfree(p); 91 } 92 else 93 status = errno == ENOENT ? EXIT_NOTFOUND : EXIT_NOEXEC; 94 return status; 95 } 96