summaryrefslogtreecommitdiffstats
path: root/src/timeout.c
blob: ca4a7cd20c8c4994801930232e55ba7ec00ab57b (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * timout.c	Advanced timeout handling for file system calls
 *		to avoid deadlocks on remote file shares.
 *
 * Version:	0.2 11-Dec-2012 Fink
 *
 * Copyright 2011,2012 Werner Fink, 2011,2012 SUSE LINUX Products GmbH, Germany.
 *
 * 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 of the License, or
 * (at your option) any later version.
 *
 * Author:	Werner Fink <werner@suse.de>, 2011
 */

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif

#ifdef _FEATURES_H
# error Include local config.h before any system header file
#endif
#include "config.h"

#ifndef WITH_TIMEOUT_STAT
# define WITH_TIMEOUT_STAT	0
#endif

#ifndef USE_SOCKETPAIR
# define USE_SOCKETPAIR		1
#endif

#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <unistd.h>
#if USE_SOCKETPAIR
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>
#  ifndef SHUT_RD
#   define SHUT_RD	0
# endif
# ifndef SHUT_WR
#  define SHUT_WR	1
# endif
# undef pipe
# define pipe(v)	(((socketpair(AF_UNIX,SOCK_STREAM,0,v) < 0) || \
			(shutdown((v)[1],SHUT_RD) < 0) || (shutdown((v)[0],SHUT_WR) < 0)) ? -1 : 0)
#endif
#include <wait.h>

#include "timeout.h"

#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
# ifndef  destructor
#  define destructor		__destructor__
# endif
# ifndef  constructor
#  define constructor		__constructor__
# endif
# ifndef  packed
#  define packed		__packed__
# endif
# ifndef  inline
#  define inline		__inline__
# endif
# ifndef  unused
#  define unused		__unused__
# endif
# ifndef  volatile
#  define volatile		__volatile__
# endif
#endif
#ifndef  attribute
# define attribute(attr)	__attribute__(attr)
#endif

#if defined __GNUC__
# undef strcpy
# define strcpy(d,s)		__builtin_strcpy((d),(s))   /* Without boundary check please */
#endif

#if WITH_TIMEOUT_STAT
static sigjmp_buf jenv;
static void sigjump(int sig attribute((unused)))
{
	siglongjmp(jenv, 1);
}
#endif

#if WITH_TIMEOUT_STAT == 2
/*
 * The structure used for communication between the processes
 */
typedef struct _handle {
	int errcode;
	struct stat argument;
	stat_t function;
	size_t len;
	char path[0];
} attribute((packed)) handle_t;

/*
 * Using a forked process for doing e.g. stat(2) system call as this
 * allows us to send e.g. SIGKILL to this process if it hangs in `D'
 * state on a file share due a stalled NFS server.  This does not work
 * with (p)threads as SIGKILL would kill all threads including main.
 */

static volatile pid_t active;
static int pipes[4] = {-1, -1, -1, -1};
static handle_t *restrict handle;
static const size_t buflen = PATH_MAX+sizeof(handle_t)+1;

static void sigchild(int sig attribute((unused)))
{
	pid_t pid = waitpid(active, NULL, WNOHANG|WUNTRACED);
	if (pid <= 0)
		return;
	if (errno == ECHILD)
		return;
	active = 0;
}

static void attribute((constructor)) start(void)
{
	sigset_t sigset, oldset;
	struct sigaction act;
	char sync[1];
	ssize_t in;

	if (pipes[1] >= 0) close(pipes[1]);
	if (pipes[2] >= 0) close(pipes[2]);

	if (pipe(&pipes[0]))
		goto error;
	if (pipe(&pipes[2]))
		goto error;

	memset(&act, 0, sizeof(act));
	sigemptyset(&act.sa_mask);
	act.sa_flags = SA_RESTART;
	act.sa_handler = sigchild;
	sigaction(SIGCHLD, &act, 0);

	if (!handle)
		handle = mmap(NULL, buflen, PROT_READ|PROT_WRITE,
			      MAP_ANONYMOUS|MAP_SHARED, -1, 0);
	if (handle == MAP_FAILED)
		goto error;

	if ((active = fork()) < 0)
		goto error;

	if (active) {
		close(pipes[0]);
		close(pipes[3]);
		pipes[0] = pipes[3] = -1;
		return;
	}

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGALRM);
	sigprocmask(SIG_BLOCK, &sigset, &oldset);

	act.sa_handler = SIG_DFL;
	sigaction(SIGCHLD, &act, 0);

	close(pipes[1]);
	close(pipes[2]);
	dup2(pipes[0], STDIN_FILENO);
	dup2(pipes[3], STDOUT_FILENO);
	close(pipes[0]);
	close(pipes[3]);
	pipes[1] = pipes[2] = -1;
	pipes[0] = pipes[3] = -1;

	while ((in = read(STDIN_FILENO, &sync, sizeof(sync))) != 0) {
		ssize_t out;
		if (in < 0) {
			if (errno == EINTR)
				continue;
			break;
		}
		if (!handle)
			break;
		if (handle->function(handle->path, &handle->argument) < 0)
				handle->errcode = errno;
		do
			out = write(STDOUT_FILENO, &sync, sizeof(sync));
		while (out < 0 && errno == EINTR);
	}

	sigprocmask(SIG_SETMASK, &oldset, NULL);
	exit(0);
error:
	if (pipes[0] >= 0) close(pipes[0]);
	if (pipes[1] >= 0) close(pipes[1]);
	if (pipes[2] >= 0) close(pipes[2]);
	if (pipes[3] >= 0) close(pipes[3]);
	if (handle && handle != MAP_FAILED)
		munmap(handle, buflen);
	handle = NULL;
}

static void /* attribute((destructor)) */ stop(void)
{
	if (active && waitpid(active, NULL, WNOHANG|WUNTRACED) == 0)
		kill(active, SIGKILL);
}

/*
 * External routine
 *
 * Execute stat(2) system call with timeout to avoid deadlock
 * on network based file systems.
 *
 */
int
timeout(stat_t function, const char *path, struct stat *restrict argument, time_t seconds)
{
	struct sigaction alrm_act, pipe_act, new_act;
	sigset_t sigset, oldset;
	char sync[1] = "x";

	if (active <= 0)	/* Oops, last one failed therefore clear status and restart */
		start();
	if (!handle)		/* No shared memory area */
		return function(path, argument);
	memset(handle, 0, sizeof(handle_t));
	handle->len = strlen(path) + 1;
	if (handle->len >= PATH_MAX) {
		errno = ENAMETOOLONG;
		goto error;
	}
	handle->errcode = 0;
	handle->argument = *argument;
	handle->function = function;
	strcpy(handle->path, path);

	sigemptyset(&sigset);
	sigaddset(&sigset, SIGALRM);
	sigaddset(&sigset, SIGPIPE);
	sigprocmask(SIG_UNBLOCK, &sigset, &oldset);

	memset(&new_act, 0, sizeof(new_act));
	sigemptyset(&new_act.sa_mask);
	new_act.sa_flags = SA_RESETHAND;

	if (sigsetjmp(jenv, 1))
		goto timed;

	new_act.sa_handler = sigjump;
	sigaction(SIGALRM, &new_act, &alrm_act);
	sigaction(SIGPIPE, &new_act, &pipe_act);
	alarm(seconds);

	write(pipes[1], &sync, sizeof(sync));
	read(pipes[2], &sync, sizeof(sync));

	alarm(0);
	sigaction(SIGPIPE, &pipe_act, NULL);
	sigaction(SIGALRM, &alrm_act, NULL);

	if (handle->errcode) {
		errno = handle->errcode;
		goto error;
	}

	*argument = handle->argument;
	sigprocmask(SIG_SETMASK, &oldset, NULL);

	return 0;
timed:
	(void) alarm(0);
	sigaction(SIGPIPE, &pipe_act, NULL);
	sigaction(SIGALRM, &alrm_act, NULL);
	sigprocmask(SIG_SETMASK, &oldset, NULL);
	stop();
	errno = ETIMEDOUT;
error:
	return -1;
}
#elif WITH_TIMEOUT_STAT == 1
/*
 * External routine
 *
 * Execute stat(2) system call with timeout to avoid deadlock
 * on network based file systems.
 *
 */
int
timeout(stat_t function, const char *path, struct stat *restrict argument, time_t seconds)
{
	struct sigaction alrm_act, pipe_act, new_act;
	sigset_t sigset, oldset;
	int ret =