-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify-tail.c
385 lines (315 loc) · 7.94 KB
/
notify-tail.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* (c) 2011 Uli Schlachter
*
* This code is covered by the following license:
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include <errno.h>
#include <fcntl.h>
#include <libnotify/notify.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/stat.h>
#include <sys/types.h>
#define APPNAME "notify-tail"
#define LINE_BUFFER_SIZE 4096
#define TIMEOUT 10000
/* FIXME: Would have been easier to just write something which reads lines from
* stdin and sends them via notify. Waaay less code since tail could handle
* stuff.
*/
struct file_watch {
struct file_watch *next;
int watch_desc;
struct file_watch *parent;
const char *name;
int fd;
off_t file_offset;
char line_buffer_data[LINE_BUFFER_SIZE];
size_t line_buffer_pos;
};
static int inotify_fd;
static struct file_watch *file_watches = NULL;
static void handle_line(struct file_watch *watch, const char *line)
{
NotifyNotification *notify;
gchar *utf8;
if (*line == '\0')
return;
utf8 = g_locale_to_utf8(line, -1, NULL, NULL, NULL);
if (!utf8)
utf8 = g_strdup_printf("ERROR: Read invalid line from '%s'",
watch->name);
notify = notify_notification_new(utf8, NULL, NULL);
notify_notification_set_urgency(notify, NOTIFY_URGENCY_LOW);
notify_notification_set_timeout(notify, TIMEOUT);
notify_notification_show(notify, NULL);
g_object_unref(G_OBJECT(notify));
g_free(utf8);
}
static void
handle_lines(struct file_watch *watch)
{
char *start = watch->line_buffer_data;
char *end = strchr(start, '\n');
while (end != NULL) {
*end = '\0';
handle_line(watch, start);
start = end + 1;
end = strchr(start, '\n');
}
if (start != watch->line_buffer_data) {
/* This is where the data for the next line is in the buffer */
size_t offset = start - watch->line_buffer_data;
size_t pos = watch->line_buffer_pos;
if (pos > offset)
memmove(watch->line_buffer_data, start, pos - offset);
watch->line_buffer_pos -= offset;
}
if (watch->line_buffer_pos >= LINE_BUFFER_SIZE - 1) {
printf("'%s': Line longer than %d characters, splitting up\n",
watch->name, LINE_BUFFER_SIZE);
handle_line(watch, watch->line_buffer_data);
watch->line_buffer_pos = 0;
}
}
static bool
do_read(struct file_watch *watch)
{
size_t pos = watch->line_buffer_pos;
char *buf = &watch->line_buffer_data[pos];
ssize_t ret = read(watch->fd, buf, LINE_BUFFER_SIZE - pos - 1);
if (ret < 0) {
fprintf(stderr, "Error while reading from '%s': %s\n",
watch->name, strerror(ret));
return false;
}
if (ret == 0)
return false;
watch->line_buffer_pos += ret;
watch->line_buffer_data[watch->line_buffer_pos] = '\0';
return true;
}
static void
read_watch(struct file_watch *watch)
{
struct stat buf;
fstat(watch->fd, &buf);
if (buf.st_size < watch->file_offset) {
printf("Warning: '%s' was truncated, reading whole file again\n",
watch->name);
lseek(watch->fd, 0, SEEK_SET);
}
while (do_read(watch))
handle_lines(watch);
watch->file_offset = lseek(watch->fd, 0, SEEK_CUR);
}
static struct file_watch *
find_watch(int wd)
{
struct file_watch *watch = file_watches;
while (watch != NULL && watch->watch_desc != wd)
watch = watch->next;
return watch;
}
static struct file_watch *
find_watch_by_name(const char *name)
{
struct file_watch *watch = file_watches;
while (watch != NULL) {
if (watch->name && strcmp(watch->name, name) == 0)
break;
watch = watch->next;
}
return watch;
}
static struct file_watch *
init_watch(const char *name, bool is_file)
{
struct file_watch *watch;
watch = calloc(1, sizeof(*watch));
watch->name = name;
watch->fd = -1;
if (file_watches == NULL) {
file_watches = watch;
} else {
struct file_watch *pos = file_watches;
while (pos->next != NULL)
pos = pos->next;
pos->next = watch;
}
if (!is_file)
watch->watch_desc = inotify_add_watch(inotify_fd, watch->name, IN_CREATE | IN_MOVED_TO);
else
watch->watch_desc = -1;
return watch;
}
static void
wait_for_parent(struct file_watch *watch)
{
char *path;
char *end;
if (watch->parent)
return;
path = strdup(watch->name);
end = strrchr(path, '/');
if (end) {
*end = '\0';
watch->parent = find_watch_by_name(path);
if (!watch->parent)
watch->parent = init_watch(path, false);
else
free(path);
} else
free(path);
}
static void
file_deleted(struct file_watch *watch)
{
inotify_rm_watch(inotify_fd, watch->watch_desc);
close(watch->fd);
watch->fd = -1;
watch->watch_desc = -1;
watch->file_offset = 0;
watch->line_buffer_pos = 0;
}
static void
reinit_file(struct file_watch *watch)
{
int fd;
if (watch->fd >= 0)
close(watch->fd);
watch->fd = -1;
if (watch->watch_desc >= 0)
inotify_rm_watch(inotify_fd, watch->watch_desc);
watch->watch_desc = -1;
wait_for_parent(watch);
fd = open(watch->name, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Could not open '%s': %s\n", watch->name, strerror(errno));
return;
}
watch->fd = fd;
watch->watch_desc = inotify_add_watch(inotify_fd, watch->name, IN_MODIFY | IN_MOVE_SELF | IN_DELETE_SELF);
if (watch->watch_desc >= 0) {
watch->file_offset = lseek(watch->fd, 0, SEEK_END);
} else {
fprintf(stderr, "Failed to add watch for '%s': %s\n", watch->name, strerror(errno));
close(watch->fd);
watch->fd = -1;
watch->watch_desc = -1;
}
}
static bool
init_file(const char *name)
{
struct file_watch *watch;
watch = init_watch(strdup(name), true);
reinit_file(watch);
return true;
}
static bool
init_files(int num, char * const names[])
{
int i;
for (i = 0; i < num; i++) {
if (!init_file(names[i]))
return false;
}
return true;
}
static void
file_appeared(struct file_watch *watch, const char *name)
{
struct file_watch *file = file_watches;
size_t dir_len = strlen(watch->name);
while (file != NULL) {
if (strncmp(watch->name, file->name, dir_len) == 0) {
/* Ok, so this is the right dir, but is it also the
* right file? */
const char *file_name = file->name + dir_len;
while (file_name[0] == '/')
file_name++;
if (strcmp(file_name, name) == 0) {
/* Yeah, this is the right watch, re-enable it */
reinit_file(file);
}
}
file = file->next;
}
}
static void
handle_event(struct file_watch *watch, const char *name, uint32_t mask)
{
if (mask & IN_MODIFY) {
mask &= ~IN_MODIFY;
read_watch(watch);
}
if (mask & IN_CREATE || mask & IN_MOVED_TO) {
/* This is a watch for a directory, did someone create a file
* that we are looking for?
*/
mask &= ~(IN_CREATE | IN_MOVED_TO);
file_appeared(watch, name);
}
if (mask & IN_DELETE_SELF || mask & IN_MOVE_SELF) {
mask &= ~(IN_DELETE_SELF | IN_MOVE_SELF);
file_deleted(watch);
}
/* The file was deleted or we removed the watch. Whatever. */
mask &= ~IN_IGNORED;
if (mask != 0) {
fprintf(stderr, "Unhandled event 0x%08x for file '%s'\n", mask, watch->name);
}
}
static void
read_events(void)
{
const unsigned int event_buf_len = 4096;
char buffer[event_buf_len];
size_t pos = 0;
size_t length;
ssize_t ret;
ret = read(inotify_fd, buffer, sizeof(buffer));
if (ret < 0) {
fprintf(stderr, "Error reading from inotify: %s\n", strerror(errno));
return;
}
length = (size_t) ret;
while (pos + sizeof(struct inotify_event) <= length) {
struct inotify_event *event = (struct inotify_event *) &buffer[pos];
struct file_watch *watch = find_watch(event->wd);
if (watch)
handle_event(watch, &event->name[0], event->mask);
else
fprintf(stderr, "Event %08x for unknown watch descriptor %d!\n",
event->mask, event->wd);
pos += sizeof(struct inotify_event) + event->len;
}
if (pos != length)
fprintf(stderr, "inotify event of size %d, but handled %d bytes\n",
(int) length, (int) pos);
}
int
main(int argc, char *argv[])
{
notify_init(APPNAME);
inotify_fd = inotify_init();
if (inotify_fd < 0) {
fprintf(stderr, "inotify not available\n");
return -1;
}
if (!init_files(argc - 1, &argv[1])) {
fprintf(stderr, "Startup failed\n");
return -1;
}
do {
read_events();
} while (true);
notify_uninit();
close(inotify_fd);
return 0;
}