summaryrefslogtreecommitdiff
path: root/src/lib/libast/misc/procclose.c
blob: a4924545e6d3aaeaa635052b25f8a8a57efc2d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2011 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                 Eclipse Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*          http://www.eclipse.org/org/documents/epl-v10.html           *
*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#pragma prototyped
/*
 * Glenn Fowler
 * AT&T Research
 *
 * close a proc opened by procopen()
 * otherwise exit() status of process is returned
 */

#include "proclib.h"

int
procclose(register Proc_t* p)
{
	int	pid;
	int	flags = 0;
	int	status = -1;

	if (p)
	{
		if (p->rfd >= 0)
			close(p->rfd);
		if (p->wfd >= 0 && p->wfd != p->rfd)
			close(p->wfd);
		if (p->flags & PROC_ORPHAN)
			status = 0;
		else
		{
			if (p->flags & PROC_ZOMBIE)
			{
				/*
				 * process may leave a zombie behind
				 * give it a chance to do that but
				 * don't hang waiting for it
				 */

				flags |= WNOHANG;
				sleep(1);
			}
			if (!(p->flags & PROC_FOREGROUND))
				sigcritical(SIG_REG_EXEC|SIG_REG_PROC);
			while ((pid = waitpid(p->pid, &status, flags)) == -1 && errno == EINTR);
			if (pid != p->pid && (flags & WNOHANG))
				status = 0;
			if (!(p->flags & PROC_FOREGROUND))
				sigcritical(0);
			else
			{
				if (p->sigint != SIG_IGN)
					signal(SIGINT, p->sigint);
				if (p->sigquit != SIG_IGN)
					signal(SIGQUIT, p->sigquit);
#if defined(SIGCHLD)
#if _lib_sigprocmask
				sigprocmask(SIG_SETMASK, &p->mask, NiL);
#else
#if _lib_sigsetmask
				sigsetmask(p->mask);
#else
				if (p->sigchld != SIG_DFL)
					signal(SIGCHLD, p->sigchld);
#endif
#endif
#endif
			}
			status = status == -1 ?
				 EXIT_QUIT :
				 WIFSIGNALED(status) ?
				 EXIT_TERM(WTERMSIG(status)) :
				 EXIT_CODE(WEXITSTATUS(status));
		}
		procfree(p);
	}
	else
		status = errno == ENOENT ? EXIT_NOTFOUND : EXIT_NOEXEC;
	return status;
}