xref: /titanic_52/usr/src/contrib/ast/src/lib/libast/misc/procclose.c (revision 906afcb89d0412cc073b95c2d701a804a8cdb62c)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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 int
34 procclose(register Proc_t* p)
35 {
36 	int	pid;
37 	int	flags = 0;
38 	int	status = -1;
39 
40 	if (p)
41 	{
42 		if (p->rfd >= 0)
43 			close(p->rfd);
44 		if (p->wfd >= 0 && p->wfd != p->rfd)
45 			close(p->wfd);
46 		if (p->flags & PROC_ORPHAN)
47 			status = 0;
48 		else
49 		{
50 			if (p->flags & PROC_ZOMBIE)
51 			{
52 				/*
53 				 * process may leave a zombie behind
54 				 * give it a chance to do that but
55 				 * don't hang waiting for it
56 				 */
57 
58 				flags |= WNOHANG;
59 				sleep(1);
60 			}
61 			if (!(p->flags & PROC_FOREGROUND))
62 				sigcritical(SIG_REG_EXEC|SIG_REG_PROC);
63 			while ((pid = waitpid(p->pid, &status, flags)) == -1 && errno == EINTR);
64 			if (pid != p->pid && (flags & WNOHANG))
65 				status = 0;
66 			if (!(p->flags & PROC_FOREGROUND))
67 				sigcritical(0);
68 			else
69 			{
70 				if (p->sigint != SIG_IGN)
71 					signal(SIGINT, p->sigint);
72 				if (p->sigquit != SIG_IGN)
73 					signal(SIGQUIT, p->sigquit);
74 #if defined(SIGCHLD)
75 #if _lib_sigprocmask
76 				sigprocmask(SIG_SETMASK, &p->mask, NiL);
77 #else
78 #if _lib_sigsetmask
79 				sigsetmask(p->mask);
80 #else
81 				if (p->sigchld != SIG_DFL)
82 					signal(SIGCHLD, p->sigchld);
83 #endif
84 #endif
85 #endif
86 			}
87 			status = status == -1 ?
88 				 EXIT_QUIT :
89 				 WIFSIGNALED(status) ?
90 				 EXIT_TERM(WTERMSIG(status)) :
91 				 EXIT_CODE(WEXITSTATUS(status));
92 		}
93 		procfree(p);
94 	}
95 	else
96 		status = errno == ENOENT ? EXIT_NOTFOUND : EXIT_NOEXEC;
97 	return status;
98 }
99