This source file includes following definitions.
- fpm_event_port_module
- fpm_event_port_init
- fpm_event_port_clean
- fpm_event_port_wait
- fpm_event_port_add
- fpm_event_port_remove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include "../fpm_config.h"
22 #include "../fpm_events.h"
23 #include "../fpm.h"
24 #include "../zlog.h"
25
26 #if HAVE_PORT
27
28 #include <port.h>
29 #include <poll.h>
30 #include <errno.h>
31
32 static int fpm_event_port_init(int max);
33 static int fpm_event_port_clean();
34 static int fpm_event_port_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
35 static int fpm_event_port_add(struct fpm_event_s *ev);
36 static int fpm_event_port_remove(struct fpm_event_s *ev);
37
38 static struct fpm_event_module_s port_module = {
39 .name = "port",
40 .support_edge_trigger = 0,
41 .init = fpm_event_port_init,
42 .clean = fpm_event_port_clean,
43 .wait = fpm_event_port_wait,
44 .add = fpm_event_port_add,
45 .remove = fpm_event_port_remove,
46 };
47
48 port_event_t *events = NULL;
49 int nevents = 0;
50 static int pfd = -1;
51
52 #endif
53
54 struct fpm_event_module_s *fpm_event_port_module()
55 {
56 #if HAVE_PORT
57 return &port_module;
58 #else
59 return NULL;
60 #endif
61 }
62
63
64 #if HAVE_PORT
65
66
67
68
69 static int fpm_event_port_init(int max)
70 {
71
72 pfd = port_create();
73 if (pfd < 0) {
74 zlog(ZLOG_ERROR, "port: unable to initialize port_create()");
75 return -1;
76 }
77
78 if (max < 1) {
79 return 0;
80 }
81
82
83 events = malloc(sizeof(port_event_t) * max);
84 if (!events) {
85 zlog(ZLOG_ERROR, "port: Unable to allocate %d events", max);
86 return -1;
87 }
88
89 nevents = max;
90 return 0;
91 }
92
93
94
95
96
97 static int fpm_event_port_clean()
98 {
99 if (pfd > -1) {
100 close(pfd);
101 pfd = -1;
102 }
103
104 if (events) {
105 free(events);
106 events = NULL;
107 }
108
109 nevents = 0;
110 return 0;
111 }
112
113
114
115
116
117 static int fpm_event_port_wait(struct fpm_event_queue_s *queue, unsigned long int timeout)
118 {
119 int ret, i, nget;
120 timespec_t t;
121
122
123 t.tv_sec = (int)(timeout / 1000);
124 t.tv_nsec = (timeout % 1000) * 1000 * 1000;
125
126
127 nget = 1;
128 ret = port_getn(pfd, events, nevents, &nget, &t);
129 if (ret < 0) {
130
131
132 if (errno != EINTR && errno != ETIME) {
133 zlog(ZLOG_WARNING, "poll() returns %d", errno);
134 return -1;
135 }
136 }
137
138 for (i = 0; i < nget; i++) {
139
140
141 if (!events[i].portev_user) {
142 continue;
143 }
144
145
146 fpm_event_fire((struct fpm_event_s *)events[i].portev_user);
147
148
149 if (fpm_globals.parent_pid != getpid()) {
150 return -2;
151 }
152 }
153 return nget;
154 }
155
156
157
158
159
160 static int fpm_event_port_add(struct fpm_event_s *ev)
161 {
162
163 if (port_associate(pfd, PORT_SOURCE_FD, ev->fd, POLLIN, (void *)ev) < 0) {
164 zlog(ZLOG_ERROR, "port: unable to add the event");
165 return -1;
166 }
167 return 0;
168 }
169
170
171
172
173
174 static int fpm_event_port_remove(struct fpm_event_s *ev)
175 {
176
177 if (port_dissociate(pfd, PORT_SOURCE_FD, ev->fd) < 0) {
178 zlog(ZLOG_ERROR, "port: unable to add the event");
179 return -1;
180 }
181 return 0;
182 }
183
184
185 #endif