-
Notifications
You must be signed in to change notification settings - Fork 201
/
self_pipe.c
151 lines (119 loc) · 4.82 KB
/
self_pipe.c
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
/**********************************************************************\
* Copyright (C) Michael Kerrisk, 2010. *
* *
* This program is free software. You may use, modify, and redistribute *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 or (at *
* your option) any later version. This program is distributed without *
* any warranty. See the file COPYING for details. *
\**********************************************************************/
/* self_pipe.c
Employ the self-pipe trick so that we can avoid race conditions while both
selecting on a set of file descriptors and also waiting for a signal.
Usage as shown in synopsis below; for example:
self_pipe - 0
*/
#include <sys/time.h>
#if ! defined(__hpux) /* HP-UX 11 doesn't have this header file */
#include <sys/select.h>
#endif
#include <fcntl.h>
#include <signal.h>
#include "tlpi_hdr.h"
static int pfd[2]; /* File descriptors for pipe */
static void
handler(int sig)
{
int savedErrno; /* In case we change 'errno' */
savedErrno = errno;
if (write(pfd[1], "x", 1) == -1 && errno != EAGAIN)
errExit("write");
errno = savedErrno;
}
int
main(int argc, char *argv[])
{
fd_set readfds;
int ready, nfds, flags;
struct timeval timeout;
struct timeval *pto;
struct sigaction sa;
char ch;
int fd, j;
if (argc < 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s {timeout|-} fd...\n"
"\t\t('-' means infinite timeout)\n", argv[0]);
/* Initialize 'timeout', 'readfds', and 'nfds' for select() */
if (strcmp(argv[1], "-") == 0) {
pto = NULL; /* Infinite timeout */
} else {
pto = &timeout;
timeout.tv_sec = getLong(argv[1], 0, "timeout");
timeout.tv_usec = 0; /* No microseconds */
}
nfds = 0;
/* Build the 'readfds' from the fd numbers given in command line */
FD_ZERO(&readfds);
for (j = 2; j < argc; j++) {
fd = getInt(argv[j], 0, "fd");
if (fd >= FD_SETSIZE)
cmdLineErr("file descriptor exceeds limit (%d)\n", FD_SETSIZE);
if (fd >= nfds)
nfds = fd + 1; /* Record maximum fd + 1 */
FD_SET(fd, &readfds);
}
/* Create pipe before establishing signal handler to prevent race */
if (pipe(pfd) == -1)
errExit("pipe");
FD_SET(pfd[0], &readfds); /* Add read end of pipe to 'readfds' */
nfds = max(nfds, pfd[0] + 1); /* And adjust 'nfds' if required */
/* Make read and write ends of pipe nonblocking */
flags = fcntl(pfd[0], F_GETFL);
if (flags == -1)
errExit("fcntl-F_GETFL");
flags |= O_NONBLOCK; /* Make read end nonblocking */
if (fcntl(pfd[0], F_SETFL, flags) == -1)
errExit("fcntl-F_SETFL");
flags = fcntl(pfd[1], F_GETFL);
if (flags == -1)
errExit("fcntl-F_GETFL");
flags |= O_NONBLOCK; /* Make write end nonblocking */
if (fcntl(pfd[1], F_SETFL, flags) == -1)
errExit("fcntl-F_SETFL");
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART; /* Restart interrupted reads()s */
sa.sa_handler = handler;
if (sigaction(SIGINT, &sa, NULL) == -1)
errExit("sigaction");
while ((ready = select(nfds, &readfds, NULL, NULL, pto)) == -1 &&
errno == EINTR)
continue; /* Restart if interrupted by signal */
if (ready == -1) /* Unexpected error */
errExit("select");
if (FD_ISSET(pfd[0], &readfds)) { /* Handler was called */
printf("A signal was caught\n");
for (;;) { /* Consume bytes from pipe */
if (read(pfd[0], &ch, 1) == -1) {
if (errno == EAGAIN)
break; /* No more bytes */
else
errExit("read"); /* Some other error */
}
/* Perform any actions that should be taken in response to signal */
}
}
/* Examine file descriptor sets returned by select() to see
which other file descriptors are ready */
printf("ready = %d\n", ready);
for (j = 2; j < argc; j++) {
fd = getInt(argv[j], 0, "fd");
printf("%d: %s\n", fd, FD_ISSET(fd, &readfds) ? "r" : "");
}
/* And check if read end of pipe is ready */
printf("%d: %s (read end of pipe)\n", pfd[0],
FD_ISSET(pfd[0], &readfds) ? "r" : "");
if (pto != NULL)
printf("timeout after select(): %ld.%03ld\n",
(long) timeout.tv_sec, (long) timeout.tv_usec / 10000);
exit(EXIT_SUCCESS);
}