xref: /titanic_41/usr/src/lib/libast/common/misc/procclose.c (revision 22337b4b3c8a2b9db615e524e0e9e1fbd3dc71bf)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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 			signal(SIGINT, p->sigint);
69 			signal(SIGQUIT, p->sigquit);
70 #if defined(SIGCHLD)
71 #if _lib_sigprocmask
72 			sigprocmask(SIG_SETMASK, &p->mask, NiL);
73 #else
74 #if _lib_sigsetmask
75 			sigsetmask(p->mask);
76 #endif
77 #endif
78 			signal(SIGCHLD, p->sigchld);
79 #endif
80 		}
81 		status = status == -1 ?
82 			 EXIT_QUIT :
83 			 WIFSIGNALED(status) ?
84 			 EXIT_TERM(WTERMSIG(status)) :
85 			 EXIT_CODE(WEXITSTATUS(status));
86 		procfree(p);
87 	}
88 	else
89 		status = errno == ENOENT ? EXIT_NOTFOUND : EXIT_NOEXEC;
90 	return status;
91 }
92