summary refs log tree commit diff
path: root/fq.c
blob: a61240ffd8f97cd4a28ecc00282c5656a1117d6b (plain) (blame)
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
/*
 * fq [FILES...] - follow output of nq jobs, quitting when they are done
 *
 * To the extent possible under law, Leah Neukirchen <leah@vuxu.org>
 * has waived all copyright and related or neighboring rights to this work.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define DELAY 250000

#ifdef __linux__
#define USE_INOTIFY
#endif

#if defined(__FreeBSD__) || defined(__APPLE__)
#define USE_KEVENT
#endif

#ifdef USE_INOTIFY
#include <sys/inotify.h>
char ibuf[8192];
#endif

#ifdef USE_KEVENT
#include <sys/event.h>
#endif

char buf[8192];

static int
islocked(int fd)
{
	if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
		return (errno == EWOULDBLOCK);
	} else {
		flock(fd, LOCK_UN);
		return 0;
	}
}

static int
alphabetic(const void *a, const void *b)
{
	return strcmp(*(char **)a, *(char **)b);
}

int
main(int argc, char *argv[])
{
	int i, fd, dirfd;
	off_t off, loff;
	ssize_t rd;
	int didsth = 0, seen_nl = 0;
	int opt = 0, aflag = 0, nflag = 0, qflag = 0;
	char *path;

#ifdef USE_INOTIFY
	int ifd, wd;
#endif
#ifdef USE_KEVENT
	int kq, note;
	struct kevent kev;
#endif

	close(0);

	while ((opt = getopt(argc, argv, "+anq")) != -1) {
		switch (opt) {
		case 'a':
			aflag = 1;
			break;
		case 'n':
			nflag = 1;
			break;
		case 'q':
			qflag = 1;
			break;
		default:
			fputs("usage: fq [-anq] [JOBID...]\n", stderr);
			exit(1);
		}
	}

	path = getenv("NQDIR");
	if (!path)
		path = ".";

#ifdef O_DIRECTORY
	dirfd = open(path, O_RDONLY | O_DIRECTORY);
#else
	dirfd = open(path, O_RDONLY);
#endif
	if (dirfd < 0) {
		perror("open dir");
		exit(111);
	}

	if (optind == argc) {   /* behave as if $NQDIR/,* was passed. */
		DIR *dir;
		struct dirent *d;
		int len = 0;

		argc = 0;
		argv = 0;
		optind = 0;

		dir = fdopendir(dirfd);
		if (!dir) {
			perror("fdopendir");
			exit(111);
		}

		while ((d = readdir(dir))) {
			if (d->d_name[0] != ',')
				continue;
			if (argc >= len) {
				len = 2*len + 1;
				argv = realloc(argv, len * sizeof (char *));
				if (!argv)
					exit(222);
			}
			argv[argc] = strdup(d->d_name);
			if (!argv[argc])
				exit(222);
			argc++;
		}

		qsort(argv, argc, sizeof (char *), alphabetic);
	}

#ifdef USE_INOTIFY
	ifd = inotify_init();
	if (ifd < 0)
		exit(111);
#endif
#ifdef USE_KEVENT
	kq = kqueue();
	if (kq < 0)
		exit(111);
#endif

	for (i = optind; i < argc; i++) {
		loff = 0;
		seen_nl = 0;

		fd = openat(dirfd, argv[i], O_RDONLY);
		if (fd < 0)
			continue;

		/* skip not running jobs, unless -a was passed, or we did not
		 * output anything yet and are at the last argument.  */
		if (!aflag && !islocked(fd) && (didsth || i != argc - 1)) {
			close(fd);
			continue;
		}

		write(1, "==> ", 4);
		write(1, argv[i], strlen(argv[i]));
		write(1, qflag ? " " : "\n", 1);

		didsth = 1;

#ifdef USE_INOTIFY
		char fullpath[PATH_MAX];
		snprintf(fullpath, sizeof fullpath, "%s/%s", path, argv[i]);
		wd = inotify_add_watch(ifd, fullpath, IN_MODIFY | IN_CLOSE_WRITE);
		if (wd == -1) {
			perror("inotify_add_watch");
			exit(111);
		}
#endif
#ifdef USE_KEVENT
		note = NOTE_WRITE;
#ifdef __APPLE__
		note |= NOTE_FUNLOCK;
#endif
#ifdef __FreeBSD__
		note |= NOTE_CLOSE_WRITE;
#endif
		EV_SET(&kev, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, note, 0, NULL);
		if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0) {
			perror("kevent");
			exit(111);
		}
#endif

		while (1) {
			off = lseek(fd, 0, SEEK_END);

			if (off < loff)
				loff = off;               /* file truncated */

			if (off == loff) {
				if (nflag && islocked(fd))
					break;

				if (flock(fd, LOCK_SH | LOCK_NB) == -1 &&
				    errno == EWOULDBLOCK) {
#if defined(USE_INOTIFY)
					/* any inotify event is good */
					read(ifd, ibuf, sizeof ibuf);
#elif defined(USE_KEVENT)
					kevent(kq, NULL, 0, &kev, 1, NULL);
#else
					/* poll for size change */
					while (off == lseek(fd, 0, SEEK_END))
						usleep(DELAY);
#endif
					continue;
				} else {
					flock(fd, LOCK_UN);
					break;
				}
			}

			if (off - loff > sizeof buf)
				off = loff + sizeof buf;

			rd = pread(fd, &buf, off - loff, loff);
			if (qflag) {
				if (!seen_nl) {
					char *s;
					if ((s = memchr(buf, '\n', rd))) {
						write(1, buf, s+1-buf);
						seen_nl = 1;
					} else {
						write(1, buf, rd);
					}
				}
			} else {
				write(1, buf, rd);
			}

			loff += rd;
		}

		if (qflag && !seen_nl)
			write(1, "\n", 1);

#ifdef USE_INOTIFY
		inotify_rm_watch(ifd, wd);
#endif
#ifdef USE_KEVENT
		EV_SET(&kev, fd, EVFILT_VNODE, EV_DELETE, 0, 0, NULL);
		kevent(kq, &kev, 1, NULL, 0, NULL);
#endif
		close(fd);
	}

#ifdef USE_INOTIFY
	close(ifd);
#endif
#ifdef USE_KEVENT
	close(kq);
#endif
	return 0;
}