pnotify-internal.h

00001 /*              $Id: $          */
00002 
00003 /*
00004  * Copyright (c) 2007 Mark Heily <devel@heily.com>
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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 /* WORKAROUND */
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         /* All entries in the directory (struct dirent) */
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         /* ---- public fields accessible via pnotify_watch structure */
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         /* ---- private fields accessible via pn_watch structure */
00150 
00152         struct pnotify_ctx * ctx;
00153 
00154         int             fd;     
00155         int             wd;     
00156         bool            is_dir; 
00158         /* A list of directory entries (only used if is_dir is true) */
00159         struct directory dir;
00160 
00161         /* The parent watch descriptor, for watches that are
00162            automatically added on files within a monitored
00163            directory. If zero, there is no parent.
00164          */
00165         int parent_wd;
00166 
00167 #if HAVE_KQUEUE
00168 
00169         /* The associated kernel event structure */
00170         struct kevent    kev;
00171 
00172         /* The pathname of the directory (only for directories) */
00173         char path[PATH_MAX + 1];
00174 
00175 #elif __linux__
00176 
00177         struct epoll_event epoll_evt;
00178 
00179 #endif
00180 
00181         /* Pointer to the next watch */
00182         LIST_ENTRY(pn_watch) entries;
00183 };
00184 
00185 
00186 /* The 'dprintf' macro is used for printf() debugging */
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 /* Forward declarations for private functions */
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 /* vtable for system-specific functions */
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 /* _PNOTIFY_INTERNAL_H */

Generated on Wed Aug 22 23:15:42 2007 for pnotify by  doxygen 1.5.1