00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef _PNOTIFY_INTERNAL_H
00020 #define _PNOTIFY_INTERNAL_H
00021
00022 #include <assert.h>
00023 #include <err.h>
00024 #include <errno.h>
00025 #include <dirent.h>
00026 #include <semaphore.h>
00027 #include <signal.h>
00028 #include <stdbool.h>
00029 #include <stdlib.h>
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <unistd.h>
00035
00036 #include "pnotify.h"
00037 #include "queue.h"
00038 #include "thread.h"
00039
00040
00041 #if defined(__linux__)
00042 # define HAVE_INOTIFY 1
00043 # include <sys/inotify.h>
00044 # include <sys/epoll.h>
00045 #else
00046 # define HAVE_KQUEUE 1
00047 # include <sys/event.h>
00048 #endif
00049
00051 struct pnotify_event {
00052
00054 struct pn_watch *watch;
00055
00059 int parent;
00060
00062 int mask;
00063
00067 char name[NAME_MAX + 1];
00068
00069 TAILQ_ENTRY(pnotify_event) entries;
00070 };
00071
00073 struct pnotify_ctx {
00074
00076 TAILQ_HEAD(, pnotify_event) event;
00077
00079 pthread_mutex_t mutex;
00080
00089 sem_t event_count;
00090 };
00091
00093 struct directory {
00094
00096 char *path;
00097 size_t path_len;
00098
00100 DIR *dirp;
00101
00102
00103 LIST_HEAD(, dentry) all;
00104 };
00105
00107 struct dentry {
00108
00110 struct dirent ent;
00111
00113 int fd;
00114
00116 struct stat st;
00117
00119 int mask;
00120
00122 LIST_ENTRY(dentry) entries;
00123 };
00124
00126 struct pn_watch {
00127
00128
00129
00131 enum pn_watch_type type;
00132
00134 enum pn_event_bitmask mask;
00135
00137 union pn_resource_id ident;
00138
00146 void (*cb)();
00147 void *arg;
00148
00149
00150
00152 struct pnotify_ctx * ctx;
00153
00154 int fd;
00155 int wd;
00156 bool is_dir;
00158
00159 struct directory dir;
00160
00161
00162
00163
00164
00165 int parent_wd;
00166
00167 #if HAVE_KQUEUE
00168
00169
00170 struct kevent kev;
00171
00172
00173 char path[PATH_MAX + 1];
00174
00175 #elif __linux__
00176
00177 struct epoll_event epoll_evt;
00178
00179 #endif
00180
00181
00182 LIST_ENTRY(pn_watch) entries;
00183 };
00184
00185
00186
00187 #if PNOTIFY_DEBUG
00188 # define dprintf printf
00189 # define dprint_event(e) (void) pnotify_print_event(e)
00190 #else
00191 # define dprintf(...) do { ; } while (0)
00192 # define dprint_event(e) do { ; } while (0)
00193 #endif
00194
00196 #define WATCH_MAX 1000
00197
00198
00199
00200 int pnotify_reap_signal(struct pnotify_event *, struct pnotify_ctx *);
00201 int sys_trap_signal(struct pnotify_ctx *, int signum);
00202 int pn_trap_signal(struct pnotify_ctx *, int signum);
00203 void * pn_signal_loop(void *);
00204 void * pn_timer_loop(void *);
00205 void pn_event_add(struct pnotify_ctx *, struct pnotify_event *);
00206 struct pn_watch * pn_get_watch_by_id(int wd);
00207 void pn_mask_signals();
00208 void pn_timer_init(void);
00209 int pn_add_timer(struct pn_watch *watch);
00210 int pn_rm_timer(struct pn_watch *watch);
00211
00212
00213 struct pnotify_vtable {
00214 void (*init_once)(void);
00215 int (*add_watch)(struct pn_watch *);
00216 int (*rm_watch)(struct pn_watch *);
00217 void (*cleanup)();
00218 };
00219 extern const struct pnotify_vtable * const sys;
00220 extern const struct pnotify_vtable LINUX_VTABLE;
00221 extern const struct pnotify_vtable BSD_VTABLE;
00222
00223 #endif