-
Notifications
You must be signed in to change notification settings - Fork 2
/
event-linux.c
150 lines (129 loc) · 3.49 KB
/
event-linux.c
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
/* $Id$ */
/*
* Copyright (c) 2004 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "logfmon.h"
/*
* Okay, this is a bit of a hack since, as far as I can tell:
*
* a) Linux doesn't support notification when a file is renamed or
* deleted. fcntl with F_NOTIFY only works on directories, which
* is useless.
*
* b) Linux doesn't support any useful form of notification when a file is
* appended to:
*
* - select() & poll() return immediately when a file is at EOF.
* - epoll is poorly documented, not well supported by distros as yet (both
* a 2.6 kernel and a patched glibc is needed), and refuses to work for me
* altogether on files.
* - aio_* is not what I am looking for at all, and it would be a real pain
* to make it do what I want.
* - F_SETSIG also looks annoying to implement, and I'm not sure it would do
* the right thing either.
*
* So, we are left with a very sucky manual poll with stat() which is, well,
* crap and I'm not very happy about at all.
*
*/
struct file *evfile = NULL;
void
init_events(void)
{
struct file *file;
TAILQ_FOREACH(file, &conf.files, entry) {
log_debug("init file: tag=%s", file->tag.name);
file->data = xmalloc(sizeof (off_t));
/* This gives us an EVENT_READ first time around which
strictly speaking is not correct (get_event should only
return if the file is actually changed), but who cares? */
*((off_t *) file->data) = 0;
}
}
void
close_events(void)
{
struct file *file;
TAILQ_FOREACH(file, &conf.files, entry) {
if (file->data != NULL) {
xfree(file->data);
file->data = NULL;
}
}
}
struct file *
get_event(enum event *event, int timeout)
{
struct stat sb;
int n;
off_t *size;
struct file *file;
n = 0;
for (;;) {
*event = EVENT_NONE;
if (usleep(100000L) == -1) {
if (errno == EINTR)
return (NULL);
log_fatal("usleep");
}
n++;
if (n > timeout * 10) {
*event = EVENT_TIMEOUT;
return (NULL);
}
if (evfile == NULL)
evfile = TAILQ_FIRST(&conf.files);
while (evfile != NULL) {
file = evfile;
evfile = TAILQ_NEXT(evfile, entry);
if (file->fd != NULL) {
size = file->data;
if (stat(file->path, &sb) != 0) {
*size = 0;
*event = EVENT_REOPEN;
return (file);
}
if (sb.st_size < *size) {
*size = 0;
*event = EVENT_REOPEN;
return (file);
}
if (sb.st_size > *size) {
if (fsync(fileno(file->fd)) != 0)
log_warn("fsync");
*size = sb.st_size;
*event = EVENT_READ;
return (file);
}
}
}
}
}
void
reinit_events(void)
{
close_events();
init_events();
}