xref: /lighttpd1.4/src/fdevent.h (revision f0786a75)
1 #ifndef _FDEVENT_H_
2 #define _FDEVENT_H_
3 #include "first.h"
4 
5 #include "base_decls.h" /* handler_t */
6 
7 struct fdevents;        /* declaration */
8 typedef struct fdevents fdevents;
9 
10 typedef handler_t (*fdevent_handler)(void *ctx, int revents);
11 
12 struct fdnode_st {
13     fdevent_handler handler;
14     void *ctx;
15     int fd;
16     int events;
17     int fde_ndx;
18 };
19 
20 /* These must match POLL* values from operating system headers */
21 
22 #define FDEVENT_IN     0x0001
23 #define FDEVENT_PRI    0x0002
24 #define FDEVENT_OUT    0x0004
25 #define FDEVENT_ERR    0x0008
26 #define FDEVENT_HUP    0x0010
27 #define FDEVENT_NVAL   0x0020
28 #if defined(__sun) && defined(__SVR4) /* Solaris */
29 #define FDEVENT_RDHUP  0x4000
30 #else
31 #define FDEVENT_RDHUP  0x2000
32 #endif
33 
34 #define FDEVENT_STREAM_REQUEST                  BV(0)
35 #define FDEVENT_STREAM_REQUEST_BUFMIN           BV(1)
36 #define FDEVENT_STREAM_REQUEST_POLLRDHUP        BV(12)
37 #define FDEVENT_STREAM_REQUEST_TCP_FIN          BV(13)
38 #define FDEVENT_STREAM_REQUEST_BACKEND_SHUT_WR  BV(14)
39 #define FDEVENT_STREAM_REQUEST_POLLIN           BV(15)
40 
41 #define FDEVENT_STREAM_RESPONSE           BV(0)
42 #define FDEVENT_STREAM_RESPONSE_BUFMIN    BV(1)
43 #define FDEVENT_STREAM_RESPONSE_POLLRDHUP BV(15)
44 
45 __attribute_cold__
46 int fdevent_config(const char **event_handler_name, log_error_st *errh);
47 
48 __attribute_cold__
49 __attribute_const__
50 __attribute_returns_nonnull__
51 const char * fdevent_show_event_handlers(void);
52 
53 __attribute_cold__
54 fdevents * fdevent_init(const char *event_handler, int *max_fds, int *cur_fds, log_error_st *errh);
55 
56 __attribute_cold__
57 int fdevent_reset(fdevents *ev); /* "init" after fork() */
58 
59 __attribute_cold__
60 void fdevent_free(fdevents *ev);
61 
62 __attribute_cold__
63 void fdevent_socket_nb_cloexec_init(void);
64 
65 #define fdevent_fdnode_interest(fdn) (NULL != (fdn) ? (fdn)->events : 0)
66 void fdevent_fdnode_event_del(fdevents *ev, fdnode *fdn);
67 void fdevent_fdnode_event_set(fdevents *ev, fdnode *fdn, int events);
68 void fdevent_fdnode_event_add(fdevents *ev, fdnode *fdn, int event);
69 void fdevent_fdnode_event_clr(fdevents *ev, fdnode *fdn, int event);
70 
71 int fdevent_poll(fdevents *ev, int timeout_ms);
72 
73 __attribute_returns_nonnull__
74 fdnode * fdevent_register(fdevents *ev, int fd, fdevent_handler handler, void *ctx);
75 
76 void fdevent_unregister(fdevents *ev, fdnode *fdn);
77 void fdevent_sched_close(fdevents *ev, fdnode *fdn);
78 
79 void fdevent_setfd_cloexec(int fd);
80 void fdevent_clrfd_cloexec(int fd);
81 int fdevent_fcntl_set_nb(int fd);
82 int fdevent_fcntl_set_nb_cloexec(int fd);
83 int fdevent_fcntl_set_nb_cloexec_sock(int fd);
84 int fdevent_socket_cloexec(int domain, int type, int protocol);
85 int fdevent_socket_nb_cloexec(int domain, int type, int protocol);
86 int fdevent_dup_cloexec(int fd);
87 int fdevent_open_cloexec(const char *pathname, int symlinks, int flags, mode_t mode);
88 int fdevent_pipe_cloexec (int *fds, unsigned int bufsz_hint);
89 int fdevent_mkostemp(char *path, int flags);
90 int fdevent_rename(const char *oldpath, const char *newpath);
91 
92 struct sockaddr;
93 int fdevent_accept_listenfd(int listenfd, struct sockaddr *addr, size_t *addrlen);
94 
95 __attribute_pure__
96 char ** fdevent_environ(void);
97 
98 int fdevent_open_devnull(void);
99 int fdevent_open_dirname(char *path, int symlinks);
100 int fdevent_set_stdin_stdout_stderr(int fdin, int fdout, int fderr);
101 pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin, int fdout, int fderr, int dfd);
102 int fdevent_waitpid(pid_t pid, int *status, int nb);
103 int fdevent_waitpid_intr(pid_t pid, int *status);
104 
105 ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, int so_type);
106 
107 int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread);
108 
109 int fdevent_connect_status(int fd);
110 
111 /* fd must be TCP socket (AF_INET, AF_INET6), end-of-stream recv() 0 bytes */
112 int fdevent_is_tcp_half_closed(int fd);
113 int fdevent_set_tcp_nodelay (const int fd, const int opt);
114 
115 int fdevent_set_so_reuseaddr (const int fd, const int opt);
116 
117 char * fdevent_load_file (const char * const fn, off_t *lim, log_error_st *errh, void *(malloc_fn)(size_t), void(free_fn)(void *));
118 
119 int fdevent_load_file_bytes (char *buf, off_t sz, off_t off, const char *fn, log_error_st *errh);
120 
121 #endif
122