chiark / gitweb /
fe7a63836c2b4779528dddd22f6b6c793fd1e881
[innduct.git] / filemon.c
1 /*
2  *  innduct
3  *  tailing reliable realtime streaming feeder for inn
4  *  filemon.c - file monitoring (inotify, kqueue, poll, etc.)
5  *
6  *  Copyright (C) 2010 Ian Jackson <ijackson@chiark.greenend.org.uk>
7  * 
8  *  This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  * 
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  * 
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  *  (I believe that when you compile and link this as part of the inn2
22  *  build, with the Makefile runes I have provided, all the libraries
23  *  and files which end up included in innduct are licence-compatible
24  *  with GPLv3.  If not then please let me know.  -Ian Jackson.)
25  */
26
27 #include "innduct.h"
28
29 /*---------- filemon implemented with inotify ----------*/
30
31 #if defined(HAVE_SYS_INOTIFY_H) && !defined(HAVE_FILEMON)
32 #define HAVE_FILEMON
33
34 #include <sys/inotify.h>
35
36 DEFLIST(Filemon_Perfile);
37
38 struct Filemon_Perfile {
39   ISNODE(Filemon_Perfile);
40   InputFile *ipf;
41   int wd;
42 };
43
44 static int filemon_inotify_fd;
45 static Filemon_PerfileList filemon_inotify_watches;
46
47 static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) {
48   pf->ipf= ipf;
49
50   pf->wd= inotify_add_watch(filemon_inotify_fd, ipf->path, IN_MODIFY);
51   if (pf->wd < 0) sysdie("inotify_add_watch %s", ipf->path);
52
53   LIST_ADDHEAD(filemon_inotify_watches, pf);
54   dbg("filemon inotify startfile %p wd=%d pf=%p", ipf, wd, pf);
55 }
56
57 static void filemon_method_stopfile(InputFile *ipf, Filemon_Perfile *pf) {
58   dbg("filemon inotify stopfile %p wd=%d pf=%p", ipf, pf->wd, pf);
59   int r= inotify_rm_watch(filemon_inotify_fd, pf->wd);
60   if (r) syscrash("inotify_rm_watch");
61   LIST_REMOVE(filemon_inotify_watches, pf);
62 }
63
64 static void *filemon_inotify_readable(oop_source *lp, int fd,
65                                       oop_event e, void *u) {
66   struct inotify_event iev;
67   for (;;) {
68     int r= read(filemon_inotify_fd, &iev, sizeof(iev));
69     if (r==-1) {
70       if (isewouldblock(errno)) break;
71       syscrash("read from inotify master");
72     } else if (r!=sizeof(iev)) {
73       crash("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
74     }
75     Filemon_Perfile *pf;
76     FOR_LIST_NODE(pf, filemon_inotify_watches)
77       if (pf->wd == iev.wd) goto found;
78     crash("inotify read event on unknown wd=%p", iev.wd);
79   found:
80     InputFile *ipf= pf->ipf;
81     /*dbg("filemon inotify readable read %p wd=%d", ipf, iev.wd);*/
82     tailing_make_readable(ipf);
83   }
84   return OOP_CONTINUE;
85 }
86
87 int filemon_method_init(void) {
88   LIST_INIT(filemon_inotify_watches);
89   filemon_inotify_fd= inotify_init();
90   if (filemon_inotify_fd<0) {
91     syswarn("filemon/inotify: inotify_init failed");
92     return 0;
93   }
94   xsetnonblock(filemon_inotify_fd, 1);
95   loop->on_fd(loop, filemon_inotify_fd, OOP_READ, filemon_inotify_readable, 0);
96
97   dbg("filemon inotify init filemon_inotify_fd=%d", filemon_inotify_fd);
98   return 1;
99 }
100
101 void filemon_method_dump_info(FILE *f) {
102   int i;
103   fprintf(f,"inotify");
104   DUMPV("%d",,filemon_inotify_fd);
105   DUMPV("%d",filemon_inotify_watches.,count);
106   fprintf(f,"\n");
107   Filemon_Perfile *pf;
108   FOR_LIST_NODE(pf, filemon_inotify_watches)
109     fprintf(f," watching %p wd=%d pf=%p\n", pf->ipf, pf->wd, pf);
110 }
111
112 #endif /* HAVE_INOTIFY && !HAVE_FILEMON */
113
114 /*---------- filemon dummy implementation ----------*/
115
116 #if !defined(HAVE_FILEMON)
117
118 struct Filemon_Perfile { int dummy; };
119
120 int filemon_method_init(void) {
121   warn("filemon/dummy: no filemon method compiled in");
122   return 0;
123 }
124 static void filemon_method_startfile(InputFile *ipf, Filemon_Perfile *pf) { }
125 static void filemon_method_stopfile(InputFile *ipf, Filemon_Perfile *pf) { }
126 void filemon_method_dump_info(FILE *f) { fprintf(f,"dummy\n"); }
127
128 #endif /* !HAVE_FILEMON */
129
130 /*---------- filemon generic interface ----------*/
131
132 void filemon_start(InputFile *ipf) {
133   assert(!ipf->filemon);
134
135   NEW(ipf->filemon);
136   filemon_method_startfile(ipf, ipf->filemon);
137 }
138
139 void filemon_stop(InputFile *ipf) {
140   if (!ipf->filemon) return;
141   filemon_method_stopfile(ipf, ipf->filemon);
142   free(ipf->filemon);
143   ipf->filemon= 0;
144 }