aboutsummaryrefslogtreecommitdiff
path: root/gacopyz/proc.c
blob: a92c64e04997549959754d5b3df311816e34bfba (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* This file is part of gacopyz.
   Copyright (C) 2006, 2007 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#include <gacopyz_priv.h>

static int cleanup_needed;

#define PIDTAB_INCR 256

int
gacopyz_register_child(gacopyz_conn_t conn, pid_t pid)
{
	size_t i, newcount;
	pid_t *p;
	
	for (i = 0; i < conn->pidcount; i++)
		if (conn->pidtab[i] == 0) {
			conn->pidtab[i] = pid;
			return 0;
		}

	newcount = conn->pidcount + PIDTAB_INCR;
	p = realloc(conn->pidtab, newcount * sizeof(conn->pidtab[0]));
	if (!p)
		return 1;
	memset(p + i, 0, PIDTAB_INCR * sizeof(conn->pidtab[0]));
	conn->pidcount = newcount;
	conn->pidtab = p;

	conn->pidtab[i++] = pid;
	return 0;
}

int
gacopyz_unregister_child(gacopyz_conn_t conn, pid_t pid)
{
	size_t i;
	
	for (i = 0; i < conn->pidcount; i++)
		if (conn->pidtab[i] == pid) 
			conn->pidtab[i] = 0;
}

static void
print_status(gacopyz_conn_t conn, pid_t pid, int status, int expect_term)
{
	if (WIFEXITED(status)) {
		if (WEXITSTATUS(status) == 0)
			gacopyz_log(conn, SMI_LOG_DEBUG,
				    _("Child %lu exited successfully"),
				    (unsigned long) pid);
		else
			gacopyz_log(conn, SMI_LOG_ERR,
				    _("Child %lu failed with status %d"),
				    (unsigned long) pid,
				    WEXITSTATUS(status));
	} else if (WIFSIGNALED(status)) {
		int prio;
		if (expect_term && WTERMSIG(status) == SIGTERM)
			prio = SMI_LOG_DEBUG;
		else
			prio = SMI_LOG_ERR;
		gacopyz_log(conn, prio,
			    _("Child %lu terminated on signal %d"),
			    (unsigned long) pid, WTERMSIG(status));
	} else if (WIFSTOPPED(status))
		gacopyz_log(conn, SMI_LOG_ERR,
			    _("Child %lu stopped on signal %d"),
			    (unsigned long) pid, WSTOPSIG(status));
#ifdef WCOREDUMP
	else if (WCOREDUMP(status))
		gacopyz_log(conn, SMI_LOG_ERR,
			    _("Child %lu dumped core"),
			    (unsigned long) pid);
#endif
	else
		gacopyz_log(conn, SMI_LOG_ERR,
			    _("Child %lu terminated with unrecognized status"),
			    (unsigned long) pid);
}

static void
cleanup_children(gacopyz_conn_t conn, int expect_term)
{
	if (!cleanup_needed)
		return;
	for (;;) {
		int status;
		pid_t pid = waitpid((pid_t)-1, &status, WNOHANG);
		if (pid <= 0)
			break;
		gacopyz_unregister_child(conn, pid);
		print_status(conn, pid, status, expect_term);
	}
	cleanup_needed = 0;
}

void
gacopyz_cleanup_children(gacopyz_conn_t conn)
{
	cleanup_children(conn, 0);
}

void
gacopyz_cleanup_conn(gacopyz_conn_t conn)
{
	size_t i;
	gacopyz_log(conn, SMI_LOG_DEBUG, _("Terminating subprocesses"));
	
	for (i = 0; i < conn->pidcount; i++)
		if (conn->pidtab[i])
			kill(conn->pidtab[i], SIGTERM);
	cleanup_children(conn, 1);

	if (conn->cleanup)
		conn->cleanup(conn, conn->cleanup_data);
}

static RETSIGTYPE
sig_child(int sig)
{
	cleanup_needed = 1;
	signal(sig, sig_child);
}

void
gacopyz_setup_signals()
{
	signal(SIGCHLD, sig_child);
	signal(SIGPIPE, SIG_IGN); /* FIXME */
}

Return to:

Send suggestions and report system problems to the System administrator.