summaryrefslogtreecommitdiff
path: root/tests/fs-tests/utils/fstest_monitor.c
blob: db687c18c4ee033b52599ef22c956691d0549810 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2007 Nokia Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Author: Adrian Hunter
 */

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>

struct child_info {
	struct child_info *next;
	pid_t pid;
	int terminated;
	int killed;
	int gone;
};

struct child_info *children = 0;

static void kill_children(void)
{
	struct child_info *child;

	child = children;
	while (child) {
		if (!child->gone) {
			if (!child->terminated) {
				child->terminated = 1;
				kill(child->pid, SIGTERM);
			} /*else if (!child->killed) {
				child->killed = 1;
				kill(child->pid, SIGKILL);
			}*/
		}
		child = child->next;
	}
}

static void add_child(pid_t child_pid)
{
	struct child_info *child;
	size_t sz;

	sz = sizeof(struct child_info);
	child = (struct child_info *) malloc(sz);
	memset(child, 0, sz);
	child->pid = child_pid;
	child->next = children;
	children = child;
}

static void mark_child_gone(pid_t child_pid)
{
	struct child_info *child;

	child = children;
	while (child) {
		if (child->pid == child_pid) {
			child->gone = 1;
			break;
		}
		child = child->next;
	}
}

static int have_children(void)
{
	struct child_info *child;

	child = children;
	while (child) {
		if (!child->gone)
			return 1;
		child = child->next;
	}
	return 0;
}

static int parse_command_line(char *cmdline, int *pargc, char ***pargv)
{
	char **tmp;
	char *p, *v, *q;
	size_t sz;
	int argc = 0;
	int state = 0;
	char *argv[1024];

	if (!cmdline)
		return 1;
	q = v = (char *) malloc(strlen(cmdline) + 1024);
	if (!v)
		return 1;
	p = cmdline;
	for (;;) {
		char c = *p++;
		if (!c) {
			*v++ = 0;
			break;
		}
		switch (state) {
			case 0: /* Between args */
				if (isspace(c))
					break;
				argv[argc++] = v;
				if (c == '"') {
					state = 2;
					break;
				} else if (c == '\'') {
					state = 3;
					break;
				}
				state = 1;
				/* fall-through */
			case 1: /* Not quoted */
				if (c == '\\') {
					if (*p)
						*v++ = *p;
				} else if (isspace(c)) {
					*v++ = 0;
					state = 0;
				} else
					*v++ = c;
				break;
			case 2: /* Double quoted */
				if (c == '\\' && *p == '"') {
					*v++ = '"';
					++p;
				} else if (c == '"') {
					*v++ = 0;
					state = 0;
				} else
					*v++ = c;
				break;
			case 3: /* Single quoted */
				if (c == '\'') {
					*v++ = 0;
					state = 0;
				} else
					*v++ = c;
				break;
		}
	}
	argv[argc] = 0;
	sz = sizeof(char *) * (argc + 1);
	tmp = (char **) malloc(sz);
	if (!tmp) {
		free(q);
		return 1;
	}
	if (argc == 0)
		free(q);
	memcpy(tmp, argv, sz);
	*pargc = argc;
	*pargv = tmp;
	return 0;
}

static void signal_handler(int signum)
{
	(void)signum;
	kill_children();
}

int result = 0;
int alarm_gone_off = 0;

static void alarm_handler(int signum)
{
	(void)signum;
	if (!result)
		alarm_gone_off = 1;
	kill_children();
}

int main(int argc, char *argv[], char **env)
{
	int p;
	pid_t child_pid;
	int status;
	int duration = 0;

	p = 1;
	if (argc > 1) {
		if (strncmp(argv[p], "--help", 6) == 0 ||
				strncmp(argv[p], "-h", 2) == 0) {
			printf(	"Usage is: "
				"fstest_monitor options programs...\n"
				"    Options are:\n"
				"        -h, --help           "
				"This help message\n"
				"        -d, --duration arg   "
				"Stop after arg seconds\n"
				"\n"
				"Run programs and wait for them."
				" If duration is specified,\n"
				"kill all programs"
				" after that number of seconds have elapsed.\n"
				"Example: "
				"fstest_monitor \"/bin/ls -l\" /bin/date\n"
				);
			return 1;
		}
		if (strncmp(argv[p], "--duration", 10) == 0 ||
				strncmp(argv[p], "-d", 2) == 0) {
			char *s;
			if (p+1 < argc && !isdigit(argv[p][strlen(argv[p])-1]))
				++p;
			s = argv[p];
			while (*s && !isdigit(*s))
				++s;
			duration = atoi(s);
			++p;
		}
	}

	signal(SIGTERM, signal_handler);
	signal(SIGINT, signal_handler);
	for (; p < argc; ++p) {
		child_pid = fork();
		if (child_pid) {
			/* Parent */
			if (child_pid == (pid_t) -1) {
				kill_children();
				result = 1;
				break;
			}
			add_child(child_pid);
		} else {
			/* Child */
			int cargc;
			char **cargv;

			if (parse_command_line(argv[p], &cargc, &cargv))
				return 1;
			execve(cargv[0], cargv, env);
			return 1;
		}
	}
	if (!result && duration > 0) {
		signal(SIGALRM, alarm_handler);
		alarm(duration);
	}
	while (have_children()) {
		status = 0;
		child_pid = wait(&status);
		if (child_pid == (pid_t) -1) {
			if (errno == EINTR)
				continue;
			kill_children();
			return 1;
		}
		mark_child_gone(child_pid);
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
			result = 1;
			kill_children();
		}
	}

	if (alarm_gone_off)
		return 0;

	return result;
}