-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_loop_epoll.c
More file actions
140 lines (136 loc) · 3.47 KB
/
Copy pathevent_loop_epoll.c
File metadata and controls
140 lines (136 loc) · 3.47 KB
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
#define _GNU_SOURCE
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#if defined(__linux__)
#include <sys/epoll.h>
#endif
#include <poll.h>
static int set_nonblock(int fd)
{
int flags = fcntl(fd, F_GETFL, 0);
if (flags < 0) return -1;
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) return -1;
return 0;
}
static int make_timerfd(void)
{
#if defined(__linux__)
int fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if (fd < 0) return -1;
struct itimerspec its;
its.it_interval.tv_sec = 1;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = 1;
its.it_value.tv_nsec = 0;
if (timerfd_settime(fd, 0, &its, NULL) < 0) {
close(fd);
return -1;
}
return fd;
#else
return -1;
#endif
}
int main(void)
{
int fd_in = STDIN_FILENO;
if (set_nonblock(fd_in) < 0) {
perror("set_nonblock");
}
#if defined(__linux__)
int tfd = make_timerfd();
int ep = epoll_create1(EPOLL_CLOEXEC);
if (ep < 0) {
perror("epoll_create1");
return 1;
}
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.u32 = 1;
if (epoll_ctl(ep, EPOLL_CTL_ADD, fd_in, &ev) < 0) {
perror("epoll_ctl stdin");
return 1;
}
if (tfd >= 0) {
ev.events = EPOLLIN;
ev.data.u32 = 2;
if (epoll_ctl(ep, EPOLL_CTL_ADD, tfd, &ev) < 0) {
perror("epoll_ctl timer");
return 1;
}
}
printf("type lines; timer fires once per second; ctrl-d to exit\n");
for (;;) {
struct epoll_event evs[4];
int n = epoll_wait(ep, evs, 4, -1);
if (n < 0) {
if (errno == EINTR) continue;
perror("epoll_wait");
break;
}
for (int i = 0; i < n; i++) {
if (evs[i].data.u32 == 1) {
char buf[256];
ssize_t r = read(fd_in, buf, sizeof buf - 1);
if (r == 0) {
printf("eof\n");
close(ep);
if (tfd >= 0) close(tfd);
return 0;
} else if (r > 0) {
buf[r] = 0;
printf("in: %s", buf);
}
} else if (evs[i].data.u32 == 2) {
uint64_t exp;
ssize_t r = read(tfd, &exp, sizeof exp);
if (r > 0) {
printf("timer tick\n");
}
}
}
}
close(ep);
if (tfd >= 0) close(tfd);
return 0;
#else
printf("using poll fallback; type lines; ctrl-d to exit\n");
for (;;) {
struct pollfd pfds[1];
pfds[0].fd = fd_in;
pfds[0].events = POLLIN;
int r = poll(pfds, 1, 1000);
if (r < 0) {
if (errno == EINTR) continue;
perror("poll");
break;
} else if (r == 0) {
printf("poll timeout\n");
continue;
}
if (pfds[0].revents & POLLIN) {
char buf[256];
ssize_t n = read(fd_in, buf, sizeof buf - 1);
if (n == 0) {
printf("eof\n");
break;
} else if (n > 0) {
buf[n] = 0;
printf("in: %s", buf);
}
}
}
return 0;
#endif
}