/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* Glenn Fowler24* AT&T Research25*26* close a proc opened by procopen()27* otherwise exit() status of process is returned28*/2930#include "proclib.h"3132int33procclose(register Proc_t* p)34{35int pid;36int flags = 0;37int status = -1;3839if (p)40{41if (p->rfd >= 0)42close(p->rfd);43if (p->wfd >= 0 && p->wfd != p->rfd)44close(p->wfd);45if (p->flags & PROC_ORPHAN)46status = 0;47else48{49if (p->flags & PROC_ZOMBIE)50{51/*52* process may leave a zombie behind53* give it a chance to do that but54* don't hang waiting for it55*/5657flags |= WNOHANG;58sleep(1);59}60if (!(p->flags & PROC_FOREGROUND))61sigcritical(SIG_REG_EXEC|SIG_REG_PROC);62while ((pid = waitpid(p->pid, &status, flags)) == -1 && errno == EINTR);63if (pid != p->pid && (flags & WNOHANG))64status = 0;65if (!(p->flags & PROC_FOREGROUND))66sigcritical(0);67else68{69if (p->sigint != SIG_IGN)70signal(SIGINT, p->sigint);71if (p->sigquit != SIG_IGN)72signal(SIGQUIT, p->sigquit);73#if defined(SIGCHLD)74#if _lib_sigprocmask75sigprocmask(SIG_SETMASK, &p->mask, NiL);76#else77#if _lib_sigsetmask78sigsetmask(p->mask);79#else80if (p->sigchld != SIG_DFL)81signal(SIGCHLD, p->sigchld);82#endif83#endif84#endif85}86status = status == -1 ?87EXIT_QUIT :88WIFSIGNALED(status) ?89EXIT_TERM(WTERMSIG(status)) :90EXIT_CODE(WEXITSTATUS(status));91}92procfree(p);93}94else95status = errno == ENOENT ? EXIT_NOTFOUND : EXIT_NOEXEC;96return status;97}9899100