Annotation of embedaddon/lighttpd/src/fdevent.c, revision 1.1.1.1
1.1 misho 1: #include "base.h"
2: #include "log.h"
3:
4: #include <sys/types.h>
5:
6: #include <unistd.h>
7: #include <stdlib.h>
8: #include <string.h>
9: #include <errno.h>
10: #include <stdio.h>
11: #include <fcntl.h>
12: #include <assert.h>
13:
14:
15: fdevents *fdevent_init(server *srv, size_t maxfds, fdevent_handler_t type) {
16: fdevents *ev;
17:
18: ev = calloc(1, sizeof(*ev));
19: ev->srv = srv;
20: ev->fdarray = calloc(maxfds, sizeof(*ev->fdarray));
21: ev->maxfds = maxfds;
22:
23: switch(type) {
24: case FDEVENT_HANDLER_POLL:
25: if (0 != fdevent_poll_init(ev)) {
26: log_error_write(ev->srv, __FILE__, __LINE__, "S",
27: "event-handler poll failed");
28:
29: return NULL;
30: }
31: return ev;
32: case FDEVENT_HANDLER_SELECT:
33: if (0 != fdevent_select_init(ev)) {
34: log_error_write(ev->srv, __FILE__, __LINE__, "S",
35: "event-handler select failed");
36: return NULL;
37: }
38: return ev;
39: case FDEVENT_HANDLER_LINUX_SYSEPOLL:
40: if (0 != fdevent_linux_sysepoll_init(ev)) {
41: log_error_write(ev->srv, __FILE__, __LINE__, "S",
42: "event-handler linux-sysepoll failed, try to set server.event-handler = \"poll\" or \"select\"");
43: return NULL;
44: }
45: return ev;
46: case FDEVENT_HANDLER_SOLARIS_DEVPOLL:
47: if (0 != fdevent_solaris_devpoll_init(ev)) {
48: log_error_write(ev->srv, __FILE__, __LINE__, "S",
49: "event-handler solaris-devpoll failed, try to set server.event-handler = \"poll\" or \"select\"");
50: return NULL;
51: }
52: return ev;
53: case FDEVENT_HANDLER_SOLARIS_PORT:
54: if (0 != fdevent_solaris_port_init(ev)) {
55: log_error_write(ev->srv, __FILE__, __LINE__, "S",
56: "event-handler solaris-eventports failed, try to set server.event-handler = \"poll\" or \"select\"");
57: return NULL;
58: }
59: return ev;
60: case FDEVENT_HANDLER_FREEBSD_KQUEUE:
61: if (0 != fdevent_freebsd_kqueue_init(ev)) {
62: log_error_write(ev->srv, __FILE__, __LINE__, "S",
63: "event-handler freebsd-kqueue failed, try to set server.event-handler = \"poll\" or \"select\"");
64: return NULL;
65: }
66: return ev;
67: case FDEVENT_HANDLER_LIBEV:
68: if (0 != fdevent_libev_init(ev)) {
69: log_error_write(ev->srv, __FILE__, __LINE__, "S",
70: "event-handler libev failed, try to set server.event-handler = \"poll\" or \"select\"");
71: return NULL;
72: }
73: return ev;
74: case FDEVENT_HANDLER_UNSET:
75: break;
76: }
77:
78: log_error_write(ev->srv, __FILE__, __LINE__, "S",
79: "event-handler is unknown, try to set server.event-handler = \"poll\" or \"select\"");
80: return NULL;
81: }
82:
83: void fdevent_free(fdevents *ev) {
84: size_t i;
85: if (!ev) return;
86:
87: if (ev->free) ev->free(ev);
88:
89: for (i = 0; i < ev->maxfds; i++) {
90: if (ev->fdarray[i]) free(ev->fdarray[i]);
91: }
92:
93: free(ev->fdarray);
94: free(ev);
95: }
96:
97: int fdevent_reset(fdevents *ev) {
98: if (ev->reset) return ev->reset(ev);
99:
100: return 0;
101: }
102:
103: static fdnode *fdnode_init(void) {
104: fdnode *fdn;
105:
106: fdn = calloc(1, sizeof(*fdn));
107: fdn->fd = -1;
108: return fdn;
109: }
110:
111: static void fdnode_free(fdnode *fdn) {
112: free(fdn);
113: }
114:
115: int fdevent_register(fdevents *ev, int fd, fdevent_handler handler, void *ctx) {
116: fdnode *fdn;
117:
118: fdn = fdnode_init();
119: fdn->handler = handler;
120: fdn->fd = fd;
121: fdn->ctx = ctx;
122: fdn->handler_ctx = NULL;
123: fdn->events = 0;
124:
125: ev->fdarray[fd] = fdn;
126:
127: return 0;
128: }
129:
130: int fdevent_unregister(fdevents *ev, int fd) {
131: fdnode *fdn;
132:
133: if (!ev) return 0;
134: fdn = ev->fdarray[fd];
135:
136: assert(fdn->events == 0);
137:
138: fdnode_free(fdn);
139:
140: ev->fdarray[fd] = NULL;
141:
142: return 0;
143: }
144:
145: int fdevent_event_del(fdevents *ev, int *fde_ndx, int fd) {
146: int fde = fde_ndx ? *fde_ndx : -1;
147:
148: if (NULL == ev->fdarray[fd]) return 0;
149:
150: if (ev->event_del) fde = ev->event_del(ev, fde, fd);
151: ev->fdarray[fd]->events = 0;
152:
153: if (fde_ndx) *fde_ndx = fde;
154:
155: return 0;
156: }
157:
158: int fdevent_event_set(fdevents *ev, int *fde_ndx, int fd, int events) {
159: int fde = fde_ndx ? *fde_ndx : -1;
160:
161: if (ev->event_set) fde = ev->event_set(ev, fde, fd, events);
162: ev->fdarray[fd]->events = events;
163:
164: if (fde_ndx) *fde_ndx = fde;
165:
166: return 0;
167: }
168:
169: int fdevent_poll(fdevents *ev, int timeout_ms) {
170: if (ev->poll == NULL) SEGFAULT();
171: return ev->poll(ev, timeout_ms);
172: }
173:
174: int fdevent_event_get_revent(fdevents *ev, size_t ndx) {
175: if (ev->event_get_revent == NULL) SEGFAULT();
176:
177: return ev->event_get_revent(ev, ndx);
178: }
179:
180: int fdevent_event_get_fd(fdevents *ev, size_t ndx) {
181: if (ev->event_get_fd == NULL) SEGFAULT();
182:
183: return ev->event_get_fd(ev, ndx);
184: }
185:
186: fdevent_handler fdevent_get_handler(fdevents *ev, int fd) {
187: if (ev->fdarray[fd] == NULL) SEGFAULT();
188: if (ev->fdarray[fd]->fd != fd) SEGFAULT();
189:
190: return ev->fdarray[fd]->handler;
191: }
192:
193: void * fdevent_get_context(fdevents *ev, int fd) {
194: if (ev->fdarray[fd] == NULL) SEGFAULT();
195: if (ev->fdarray[fd]->fd != fd) SEGFAULT();
196:
197: return ev->fdarray[fd]->ctx;
198: }
199:
200: int fdevent_fcntl_set(fdevents *ev, int fd) {
201: #ifdef FD_CLOEXEC
202: /* close fd on exec (cgi) */
203: fcntl(fd, F_SETFD, FD_CLOEXEC);
204: #endif
205: if ((ev) && (ev->fcntl_set)) return ev->fcntl_set(ev, fd);
206: #ifdef O_NONBLOCK
207: return fcntl(fd, F_SETFL, O_NONBLOCK | O_RDWR);
208: #else
209: return 0;
210: #endif
211: }
212:
213:
214: int fdevent_event_next_fdndx(fdevents *ev, int ndx) {
215: if (ev->event_next_fdndx) return ev->event_next_fdndx(ev, ndx);
216:
217: return -1;
218: }
219:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>