aboutsummaryrefslogtreecommitdiff
path: root/src/direvent.h
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-08-23 08:36:01 +0300
committerSergey Poznyakoff <gray@gnu.org>2016-08-25 22:29:47 +0300
commita60dcd92df15705bb84f644c99fd166e101dc681 (patch)
tree883906dc48ff03ca96a0b63ef8ee28dc10bcb150 /src/direvent.h
parent6f39bd7e00acee689ce9a8a3916888a815242557 (diff)
downloaddirevent-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.h89
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
75enum handler_type { 75typedef struct filpatlist *filpatlist_t;
76 HANDLER_EXTERN, 76
77 HANDLER_SENTINEL 77struct watchpoint;
78}; 78
79typedef int (*event_handler_fn) (struct watchpoint *wp,
80 event_mask *event,
81 const char *dir,
82 const char *file,
83 void *data);
84typedef void (*handler_free_fn) (void *data);
79 85
80/* Handler structure */ 86/* Handler structure */
81struct handler { 87struct 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
111typedef struct handler_list *handler_list_t; 96typedef 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
139struct handler *handler_alloc(enum handler_type type); 124struct handler *handler_alloc(event_mask ev_mask);
140void handler_free(struct handler *hp); 125void handler_free(struct handler *hp);
141struct handler *handler_copy(struct handler *orig); 126
142size_t handler_envrealloc(struct handler *hp, size_t count); 127struct prog_handler {
143 128 int flags; /* Handler flags */
144void handler_add_pattern(struct handler *hp, struct filename_pattern *pat); 129 char *command; /* Handler command (with eventual arguments) */
145void 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
137struct handler *prog_handler_alloc(event_mask ev_mask, filpatlist_t fpat,
138 struct prog_handler *p);
139void prog_handler_free(struct prog_handler *);
140size_t prog_handler_envrealloc(struct prog_handler *hp, size_t count);
141
146 142
147extern int foreground; 143extern int foreground;
148extern int debug_level; 144extern int debug_level;
@@ -248,6 +244,10 @@ void watchpoint_gc(void);
248 244
249int watchpoint_pattern_match(struct watchpoint *dwp, const char *file_name); 245int watchpoint_pattern_match(struct watchpoint *dwp, const char *file_name);
250 246
247void watchpoint_run_handlers(struct watchpoint *wp, int evflags,
248 const char *dirname, const char *filename);
249
250
251void setup_watchers(void); 251void setup_watchers(void);
252void shutdown_watchers(void); 252void shutdown_watchers(void);
253 253
@@ -257,7 +257,7 @@ struct watchpoint *watchpoint_install(const char *path, int *pnew);
257struct watchpoint *watchpoint_install_ptr(struct watchpoint *dw); 257struct watchpoint *watchpoint_install_ptr(struct watchpoint *dw);
258void watchpoint_suspend(struct watchpoint *dwp); 258void watchpoint_suspend(struct watchpoint *dwp);
259void watchpoint_destroy(struct watchpoint *dwp); 259void watchpoint_destroy(struct watchpoint *dwp);
260struct watchpoint *watchpoint_install_sentinel(struct watchpoint *dwp); 260int watchpoint_install_sentinel(struct watchpoint *dwp);
261 261
262int watch_pathname(struct watchpoint *parent, const char *dirname, int isdir, int notify); 262int 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);
292struct process *process_lookup(pid_t pid); 292struct process *process_lookup(pid_t pid);
293void process_cleanup(int expect_term); 293void process_cleanup(int expect_term);
294void process_timeouts(void); 294void process_timeouts(void);
295int run_handler(struct watchpoint *dp, struct handler *hp, event_mask *event,
296 const char *dir, const char *file);
297char **environ_setup(char **hint, char **kve); 295char **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,
308int sigv_set_tab(int sigc, struct sigtab *sigtab, struct sigaction *retsa); 306int sigv_set_tab(int sigc, struct sigtab *sigtab, struct sigaction *retsa);
309int sigv_set_action_tab(int sigc, struct sigtab *sigtab, struct sigaction *sa); 307int sigv_set_action_tab(int sigc, struct sigtab *sigtab, struct sigaction *sa);
310 308
311void filename_pattern_free(void *p); 309struct grecs_locus;
312int filename_pattern_match(struct grecs_list *lp, const char *name); 310int filpatlist_add(filpatlist_t *fptr, char const *arg, struct grecs_locus *loc);
311void filpatlist_add_exact(filpatlist_t *fptr, char const *arg);
312void filpatlist_destroy(filpatlist_t *fptr);
313int filpatlist_match(filpatlist_t fp, const char *name);
313 314

Return to:

Send suggestions and report system problems to the System administrator.