1 #ifndef _FDEVENT_H_ 2 #define _FDEVENT_H_ 3 #include "first.h" 4 5 #include "base_decls.h" /* handler_t */ 6 7 struct log_error_st; /* declaration */ 8 struct fdevents; /* declaration */ 9 typedef struct fdevents fdevents; 10 11 typedef handler_t (*fdevent_handler)(void *ctx, int revents); 12 13 struct fdnode_st { 14 fdevent_handler handler; 15 void *ctx; 16 int fd; 17 int events; 18 int fde_ndx; 19 #ifdef HAVE_LIBEV 20 void *handler_ctx; 21 #endif 22 }; 23 24 /* These must match POLL* values from operating system headers */ 25 26 #define FDEVENT_IN 0x0001 27 #define FDEVENT_PRI 0x0002 28 #define FDEVENT_OUT 0x0004 29 #define FDEVENT_ERR 0x0008 30 #define FDEVENT_HUP 0x0010 31 #define FDEVENT_NVAL 0x0020 32 #if defined(__sun) && defined(__SVR4) /* Solaris */ 33 #define FDEVENT_RDHUP 0x4000 34 #else 35 #define FDEVENT_RDHUP 0x2000 36 #endif 37 38 #define FDEVENT_STREAM_REQUEST BV(0) 39 #define FDEVENT_STREAM_REQUEST_BUFMIN BV(1) 40 #define FDEVENT_STREAM_REQUEST_POLLRDHUP BV(12) 41 #define FDEVENT_STREAM_REQUEST_TCP_FIN BV(13) 42 #define FDEVENT_STREAM_REQUEST_BACKEND_SHUT_WR BV(14) 43 #define FDEVENT_STREAM_REQUEST_POLLIN BV(15) 44 45 #define FDEVENT_STREAM_RESPONSE BV(0) 46 #define FDEVENT_STREAM_RESPONSE_BUFMIN BV(1) 47 #define FDEVENT_STREAM_RESPONSE_POLLRDHUP BV(15) 48 49 __attribute_cold__ 50 int fdevent_config(const char **event_handler_name, struct log_error_st *errh); 51 52 __attribute_cold__ 53 __attribute_const__ 54 __attribute_returns_nonnull__ 55 const char * fdevent_show_event_handlers(void); 56 57 __attribute_cold__ 58 fdevents * fdevent_init(const char *event_handler, int *max_fds, int *cur_fds, struct log_error_st *errh); 59 60 __attribute_cold__ 61 int fdevent_reset(fdevents *ev); /* "init" after fork() */ 62 63 __attribute_cold__ 64 void fdevent_free(fdevents *ev); 65 66 #define fdevent_fdnode_interest(fdn) (NULL != (fdn) ? (fdn)->events : 0) 67 void fdevent_fdnode_event_del(fdevents *ev, fdnode *fdn); 68 void fdevent_fdnode_event_set(fdevents *ev, fdnode *fdn, int events); 69 void fdevent_fdnode_event_add(fdevents *ev, fdnode *fdn, int event); 70 void fdevent_fdnode_event_clr(fdevents *ev, fdnode *fdn, int event); 71 72 int fdevent_poll(fdevents *ev, int timeout_ms); 73 74 __attribute_returns_nonnull__ 75 fdnode * fdevent_register(fdevents *ev, int fd, fdevent_handler handler, void *ctx); 76 77 void fdevent_unregister(fdevents *ev, int fd); 78 void fdevent_sched_close(fdevents *ev, int fd, int issock); 79 80 void fdevent_setfd_cloexec(int fd); 81 void fdevent_clrfd_cloexec(int fd); 82 int fdevent_fcntl_set_nb(int fd); 83 int fdevent_fcntl_set_nb_cloexec(int fd); 84 int fdevent_fcntl_set_nb_cloexec_sock(int fd); 85 int fdevent_socket_cloexec(int domain, int type, int protocol); 86 int fdevent_socket_nb_cloexec(int domain, int type, int protocol); 87 int fdevent_dup_cloexec(int fd); 88 int fdevent_open_cloexec(const char *pathname, int symlinks, int flags, mode_t mode); 89 int fdevent_mkstemp_append(char *path); 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_open_logger(const char *logger); 104 int fdevent_cycle_logger(const char *logger, int *curfd); 105 int fdevent_reaped_logger_pipe(pid_t pid); 106 int fdevent_waitpid_logger_pipe_pid(pid_t pid, time_t ts); 107 void fdevent_restart_logger_pipes(time_t ts); 108 void fdevent_close_logger_pipes(void); 109 void fdevent_breakagelog_logger_pipe(int fd); 110 void fdevent_clr_logger_pipe_pids(void); 111 112 ssize_t fdevent_socket_read_discard (int fd, char *buf, size_t sz, int family, int so_type); 113 114 int fdevent_ioctl_fionread (int fd, int fdfmt, int *toread); 115 116 int fdevent_connect_status(int fd); 117 118 /* fd must be TCP socket (AF_INET, AF_INET6), end-of-stream recv() 0 bytes */ 119 int fdevent_is_tcp_half_closed(int fd); 120 int fdevent_set_tcp_nodelay (const int fd, const int opt); 121 122 int fdevent_set_so_reuseaddr (const int fd, const int opt); 123 124 char * fdevent_load_file (const char * const fn, off_t *lim, log_error_st *errh, void *(malloc_fn)(size_t), void(free_fn)(void *)); 125 126 int fdevent_load_file_bytes (char *buf, off_t sz, off_t off, const char *fn, log_error_st *errh); 127 128 #endif 129