summaryrefslogtreecommitdiffabout
path: root/src/watcher.c
blob: da5586145d40a900458efd7692e71c995915fed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009-2013 Sergey Poznyakoff

   Wydawca is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Wydawca is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include <sys/inotify.h>
#include <sys/ioctl.h>

/* A directory watcher is described by the following structure */
struct dirwatcher {
	struct dirwatcher *next, *prev;
	struct dirwatcher *parent;	/* Points to the parent watcher.
					   NULL for top-level watchers */
	struct spool *spool;
	int wd;	     		        /* Watch descriptor */
};

static struct dirwatcher *dirwatcher_list;

struct dirwatcher *
dirwatcher_unlink(struct dirwatcher **root, struct dirwatcher *p)
{
	if (p->prev)
		p->prev->next = p->next;
	else
		*root = p->next;
	if (p->next)
		p->next->prev = p->prev;
	p->next = p->prev = NULL;
	return p;
}

struct dirwatcher *
dirwatcher_pop(struct dirwatcher **pp)
{
	if (*pp)
		return dirwatcher_unlink(pp, *pp);
	return NULL;
}

static void
dirwatcher_push(struct dirwatcher **pp, struct dirwatcher *p)
{
	p->prev = NULL;
	p->next = *pp;
	if (*pp)
		(*pp)->prev = p;
	*pp = p;
}

/* Find a watcher with the given descriptor */
static struct dirwatcher *
dirwatcher_find_wd(int wd)
{
	struct dirwatcher *dwp;

	for (dwp = dirwatcher_list; dwp; dwp = dwp->next)
		if (dwp->wd == wd)
			break;
	return dwp;
}

static int
create_watcher(struct spool *sp, void *data)
{
	int ifd = *(int *)data;
	struct dirwatcher *dwp;
	int wd;
	const char *path = sp->source_dir;

	if (!sp)
		return 0;

	if (!sp->inotify_enable) {
		wy_debug(2, ("disabling inotify support for spool %s",
			     sp->tag));
		return 0;
	}

	wy_debug(2, ("spool %s: creating watcher %s", sp->tag, path));
	dwp = malloc(sizeof(*dwp));
	if (!dwp) {
		wy_log(LOG_ERR, "not enough memory");
		return 1;
	}
	dwp->spool = sp;
	dwp->parent = NULL;

	wd = inotify_add_watch(ifd, path,
			       IN_DELETE | IN_CREATE | IN_CLOSE_WRITE |
			       IN_MOVED_FROM | IN_MOVED_TO);
	if (wd == -1) {
		wy_log(LOG_ERR, "cannot set watch on %s: %s", path,
		       strerror(errno));
		free(dwp);
		return 1;
	}

	dwp->wd = wd;
	dirwatcher_push(&dirwatcher_list, dwp);
	return 0;
}

int
watcher_init()
{
	int ifd, rc;

	if (!inotify_enable) {
		wy_debug(2, ("disabling inotify support"));
		return -1;
	}

	wy_debug(2, ("setting up inotify"));
	ifd = inotify_init();
	if (ifd == -1) {
		wy_log(LOG_ERR, "inotify_init: %s", strerror(errno));
		return -1;
	}

	rc = for_each_spool(create_watcher, &ifd);
	if (rc)
		exit(EX_OSERR);
	if (!dirwatcher_list) {
		wy_debug(2, ("inotify: nothing to watch"));
		close(ifd);
		ifd = -1;
	} else 
		wy_debug(2, ("inotify initialized successfully"));

	return ifd;
}

static void
process_event(struct inotify_event *ep)
{
	static struct dirwatcher *dwp;
	dwp = dirwatcher_find_wd(ep->wd);
	struct wy_triplet *tp;
	
	if (ep->mask & IN_IGNORED)
		/* nothing */ ;
	else if (ep->mask & IN_Q_OVERFLOW)
		wy_log(LOG_NOTICE, "event queue overflow");
	else if (ep->mask & IN_UNMOUNT)
		/* FIXME: not sure if there's
		   anything to do. Perhaps we should
		   deregister the watched dirs that
		   were located under the mountpoint
		 */ ;
	else if (!dwp) {
		if (ep->name)
			wy_log(LOG_NOTICE, "unrecognized event %x for %s",
			       ep->mask, ep->name);
		else
			wy_log(LOG_NOTICE, "unrecognized event %x", ep->mask);
	} else if (ep->mask & IN_CREATE) {
		wy_debug(1, ("%s/%s created",
			     dwp->spool->source_dir, ep->name));
	} else if (ep->mask & (IN_DELETE | IN_MOVED_FROM)) {
		wy_debug(1, ("%s/%s %s", dwp->spool->source_dir,
			      ep->name,
			      ep->mask & IN_DELETE ? "deleted" : "moved out"));
		triplet_remove_file(dwp->spool, ep->name);
	} else if (ep->mask & (IN_CLOSE_WRITE | IN_MOVED_TO)) {
		wy_debug(1, ("%s/%s written",
			     dwp->spool->source_dir, ep->name));
		if (spool_add_new_file(dwp->spool, ep->name, 0, NULL) == 0
		    && (tp = link_processable_triplets()))
			schedule_job(&inotify_spool, getuid(), tp);
	} else
		wy_log(LOG_NOTICE, "%s/%s: unexpected event %x",
		       dwp->spool->source_dir, ep->name, ep->mask);

}

static char buffer[4096];
static int offset;

int
watcher_run(int ifd)
{
	int n;
	int rdbytes;

	if (ioctl(ifd, FIONREAD, &n)) {
		wy_log(LOG_ERR, "ioctl: %s", strerror(errno));
		return -1;
	}
	if (offset + n > sizeof buffer)
		n = sizeof buffer - offset;
	if (n) {
		rdbytes = read(ifd, buffer + offset, n);
		if (rdbytes == -1) {
			if (errno == EINTR) {
				/*FIXME
				  wy_log (LOG_NOTICE, "got signal %d", signo);
				*/
				return 0;
			}

			wy_log(LOG_NOTICE, "read failed: %s", strerror(errno));
			return -1;
		}
	}
	offset += n;

	for (n = 0; offset - n >= sizeof(struct inotify_event);) {
		struct inotify_event *ep;
		size_t size;

		ep = (struct inotify_event *)(buffer + n);
		size = sizeof(*ep) + ep->len;
		if (offset - n < size)
			break;

		process_event(ep);

		n += size;
	}
	if (n > 0 && offset - n > 0)
		memmove(buffer, buffer + n, offset - n);
	offset -= n;

	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.