summaryrefslogtreecommitdiff
path: root/system-bsd.c
blob: a44e356fcfafdc6e70ad424fe51e2ca0ec48cf90 (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
/* signal-safe read and write (for BSD slow devices). writeall() also
allows partial writes */

#include <errno.h>
#include <sys/wait.h>

#include "rc.h"
#include "jbwrap.h"

static char *safe_buf;
static size_t safe_remain;

extern void writeall(int fd, char *buf, size_t remain) {
	int i;

	safe_buf = buf;
	safe_remain = remain;
	for (i = 0; safe_remain > 0; buf += i, safe_remain -= i) {
		interrupt_happened = FALSE;
		if (!sigsetjmp(slowbuf.j, 1)) {
			slow = TRUE;
			if (interrupt_happened)
				break;
			else if ((i = write(fd, safe_buf, safe_remain)) <= 0)
				break; /* abort silently on errors in write() */
		} else
			break;
		slow = FALSE;
	}
	slow = FALSE;
	sigchk();
}

extern int rc_read(int fd, char *buf, size_t n) {
	ssize_t r;

	interrupt_happened = FALSE;
	if (!sigsetjmp(slowbuf.j, 1)) {
		slow = TRUE;
		if (!interrupt_happened)
			r = read(fd, buf, n);
		else
			r = -2;
	} else
		r = -2;
	slow = FALSE;
	if (r == -2) {
		errno = EINTR;
		r = -1;
	}
	return r;
}

static int r = -1;
extern pid_t rc_wait(int *stat) {
	interrupt_happened = FALSE;
	if (!sigsetjmp(slowbuf.j, 1)) {
		slow = TRUE;
		if (!interrupt_happened)
			r = wait(stat);
		else
			r = -2;
	} else
		r = -2;
	slow = FALSE;
	if (r == -2) {
		errno = EINTR;
		r = -1;
	}
	return r;
}

/* signal-safe readline wrapper */
#if READLINE
extern char *rc_readline(char *prompt) {
	char *r;

	interrupt_happened = FALSE;
	if (!sigsetjmp(slowbuf.j, 1)) {
		slow = TRUE;
		if (!interrupt_happened)
			r = readline(prompt);
		else
			r = NULL;
	} else
		r = NULL;
	slow = FALSE;
	if (r == NULL)
		errno = EINTR;
	sigchk();
	return r;
}
#endif /* READLINE */