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