diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-08-23 08:36:01 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2016-08-25 22:29:47 +0300 |
commit | a60dcd92df15705bb84f644c99fd166e101dc681 (patch) | |
tree | 883906dc48ff03ca96a0b63ef8ee28dc10bcb150 /src/direvent.h | |
parent | 6f39bd7e00acee689ce9a8a3916888a815242557 (diff) | |
download | direvent-a60dcd92df15705bb84f644c99fd166e101dc681.tar.gz direvent-a60dcd92df15705bb84f644c99fd166e101dc681.tar.bz2 |
Further modify struct handler.
Instead of accomodating two types of functionality, the new struct handler
has a pointer to the function that is responsible for handling the event,
and a pointer to data for that function (closure). Additionally, a
pointer to deallocator function is also provided.
Existing functionality is reimplemented using this new struct. New
types of handlers can easily be added.
* src/config.c (eventconf)<ev_mask,fpat,prog_handler>: New members
<handler>: Remove.
All uses changed.
* src/direvent.h (filpatlist_t)
(event_handler_fn, handler_free_fn): New data types.
(handler): Remove union and type. Add two pointer to functions
that are to implement the functionality (run and free) and a pointer
to data for them.
(prog_handler): New struct.
(prog_handler_alloc,prog_handler_free)
(prog_handler_envrealloc)
(watchpoint_run_handlers): New protos.
(run_handler,filename_pattern_free)
(filename_pattern_match): Remove
(filpatlist_add, filpatlist_add_exact)
(filpatlist_destroy, filpatlist_match): New protos.
* src/ev_inotify.c (process_event): Use watchpoint_run_handlers
* src/ev_kqueue.c: Likewise.
* src/fnpat.c: Major rewrite.
* src/handler.c (handler_copy)
(handler_envrealloc,handler_add_pattern)
(handler_add_exact_filename): Remove
(handler_alloc): Change arguments.
(watchpoint_run_handlers): New function.
* src/progman.c (prog_handler_alloc)
(prog_handler_envrealloc): New functions.
* src/watcher.c: Reimplement sentinel watchers.
Diffstat (limited to 'src/direvent.h')
-rw-r--r-- | src/direvent.h | 89 |
1 files changed, 45 insertions, 44 deletions
diff --git a/src/direvent.h b/src/direvent.h index faa1657..d69fa34 100644 --- a/src/direvent.h +++ b/src/direvent.h | |||
@@ -72,40 +72,25 @@ struct filename_pattern { | |||
72 | } v; | 72 | } v; |
73 | }; | 73 | }; |
74 | 74 | ||
75 | enum handler_type { | 75 | typedef struct filpatlist *filpatlist_t; |
76 | HANDLER_EXTERN, | 76 | |
77 | HANDLER_SENTINEL | 77 | struct watchpoint; |
78 | }; | 78 | |
79 | typedef int (*event_handler_fn) (struct watchpoint *wp, | ||
80 | event_mask *event, | ||
81 | const char *dir, | ||
82 | const char *file, | ||
83 | void *data); | ||
84 | typedef void (*handler_free_fn) (void *data); | ||
79 | 85 | ||
80 | /* Handler structure */ | 86 | /* Handler structure */ |
81 | struct handler { | 87 | struct handler { |
82 | size_t refcnt; /* Reference counter */ | 88 | size_t refcnt; /* Reference counter */ |
83 | event_mask ev_mask; /* Event mask */ | 89 | event_mask ev_mask; /* Event mask */ |
84 | struct grecs_list *fnames; /* File name patterns */ | 90 | filpatlist_t fnames; /* File name patterns */ |
85 | enum handler_type type; | 91 | event_handler_fn run; |
86 | union { | 92 | handler_free_fn free; |
87 | struct { | 93 | void *data; |
88 | int flags; /* Handler flags */ | ||
89 | char *command; /* Handler command (with eventual | ||
90 | arguments) */ | ||
91 | uid_t uid; /* Run as this user (unless 0) */ | ||
92 | gid_t *gidv; /* Run with these groups' privileges */ | ||
93 | size_t gidc; /* Number of elements in gidv */ | ||
94 | unsigned timeout; /* Handler timeout */ | ||
95 | char **env; /* Environment */ | ||
96 | } prog; | ||
97 | struct { | ||
98 | struct watchpoint *watchpoint; | ||
99 | } sentinel; | ||
100 | } v; | ||
101 | #define prog_flags v.prog.flags | ||
102 | #define prog_command v.prog.command | ||
103 | #define prog_uid v.prog.uid | ||
104 | #define prog_gidv v.prog.gidv | ||
105 | #define prog_gidc v.prog.gidc | ||
106 | #define prog_timeout v.prog.timeout | ||
107 | #define prog_env v.prog.env | ||
108 | #define sentinel_watchpoint v.sentinel.watchpoint | ||
109 | }; | 94 | }; |
110 | 95 | ||
111 | typedef struct handler_list *handler_list_t; | 96 | typedef struct handler_list *handler_list_t; |
@@ -133,16 +118,27 @@ struct watchpoint { | |||
133 | 118 | ||
134 | #define __cat2__(a,b) a ## b | 119 | #define __cat2__(a,b) a ## b |
135 | #define handler_matches_event(h,m,f,n) \ | 120 | #define handler_matches_event(h,m,f,n) \ |
136 | (((h)->ev_mask.__cat2__(m,_mask) & (f)) && \ | 121 | (((h)->ev_mask.__cat2__(m,_mask) & (f)) && \ |
137 | filename_pattern_match((h)->fnames, n) == 0) | 122 | filpatlist_match((h)->fnames, n) == 0) |
138 | 123 | ||
139 | struct handler *handler_alloc(enum handler_type type); | 124 | struct handler *handler_alloc(event_mask ev_mask); |
140 | void handler_free(struct handler *hp); | 125 | void handler_free(struct handler *hp); |
141 | struct handler *handler_copy(struct handler *orig); | 126 | |
142 | size_t handler_envrealloc(struct handler *hp, size_t count); | 127 | struct prog_handler { |
143 | 128 | int flags; /* Handler flags */ | |
144 | void handler_add_pattern(struct handler *hp, struct filename_pattern *pat); | 129 | char *command; /* Handler command (with eventual arguments) */ |
145 | void handler_add_exact_filename(struct handler *hp, const char *filename); | 130 | uid_t uid; /* Run as this user (unless 0) */ |
131 | gid_t *gidv; /* Run with these groups' privileges */ | ||
132 | size_t gidc; /* Number of elements in gidv */ | ||
133 | unsigned timeout; /* Handler timeout */ | ||
134 | char **env; /* Environment */ | ||
135 | }; | ||
136 | |||
137 | struct handler *prog_handler_alloc(event_mask ev_mask, filpatlist_t fpat, | ||
138 | struct prog_handler *p); | ||
139 | void prog_handler_free(struct prog_handler *); | ||
140 | size_t prog_handler_envrealloc(struct prog_handler *hp, size_t count); | ||
141 | |||
146 | 142 | ||
147 | extern int foreground; | 143 | extern int foreground; |
148 | extern int debug_level; | 144 | extern int debug_level; |
@@ -248,6 +244,10 @@ void watchpoint_gc(void); | |||
248 | 244 | ||
249 | int watchpoint_pattern_match(struct watchpoint *dwp, const char *file_name); | 245 | int watchpoint_pattern_match(struct watchpoint *dwp, const char *file_name); |
250 | 246 | ||
247 | void watchpoint_run_handlers(struct watchpoint *wp, int evflags, | ||
248 | const char *dirname, const char *filename); | ||
249 | |||
250 | |||
251 | void setup_watchers(void); | 251 | void setup_watchers(void); |
252 | void shutdown_watchers(void); | 252 | void shutdown_watchers(void); |
253 | 253 | ||
@@ -257,7 +257,7 @@ struct watchpoint *watchpoint_install(const char *path, int *pnew); | |||
257 | struct watchpoint *watchpoint_install_ptr(struct watchpoint *dw); | 257 | struct watchpoint *watchpoint_install_ptr(struct watchpoint *dw); |
258 | void watchpoint_suspend(struct watchpoint *dwp); | 258 | void watchpoint_suspend(struct watchpoint *dwp); |
259 | void watchpoint_destroy(struct watchpoint *dwp); | 259 | void watchpoint_destroy(struct watchpoint *dwp); |
260 | struct watchpoint *watchpoint_install_sentinel(struct watchpoint *dwp); | 260 | int watchpoint_install_sentinel(struct watchpoint *dwp); |
261 | 261 | ||
262 | int watch_pathname(struct watchpoint *parent, const char *dirname, int isdir, int notify); | 262 | int watch_pathname(struct watchpoint *parent, const char *dirname, int isdir, int notify); |
263 | 263 | ||
@@ -292,8 +292,6 @@ size_t handler_list_size(handler_list_t hlist); | |||
292 | struct process *process_lookup(pid_t pid); | 292 | struct process *process_lookup(pid_t pid); |
293 | void process_cleanup(int expect_term); | 293 | void process_cleanup(int expect_term); |
294 | void process_timeouts(void); | 294 | void process_timeouts(void); |
295 | int run_handler(struct watchpoint *dp, struct handler *hp, event_mask *event, | ||
296 | const char *dir, const char *file); | ||
297 | char **environ_setup(char **hint, char **kve); | 295 | char **environ_setup(char **hint, char **kve); |
298 | 296 | ||
299 | #define NITEMS(a) ((sizeof(a)/sizeof((a)[0]))) | 297 | #define NITEMS(a) ((sizeof(a)/sizeof((a)[0]))) |
@@ -308,6 +306,9 @@ int sigv_set_all(void (*handler)(int), int sigc, int *sigv, | |||
308 | int sigv_set_tab(int sigc, struct sigtab *sigtab, struct sigaction *retsa); | 306 | int sigv_set_tab(int sigc, struct sigtab *sigtab, struct sigaction *retsa); |
309 | int sigv_set_action_tab(int sigc, struct sigtab *sigtab, struct sigaction *sa); | 307 | int sigv_set_action_tab(int sigc, struct sigtab *sigtab, struct sigaction *sa); |
310 | 308 | ||
311 | void filename_pattern_free(void *p); | 309 | struct grecs_locus; |
312 | int filename_pattern_match(struct grecs_list *lp, const char *name); | 310 | int filpatlist_add(filpatlist_t *fptr, char const *arg, struct grecs_locus *loc); |
311 | void filpatlist_add_exact(filpatlist_t *fptr, char const *arg); | ||
312 | void filpatlist_destroy(filpatlist_t *fptr); | ||
313 | int filpatlist_match(filpatlist_t fp, const char *name); | ||
313 | 314 | ||