forward.c revision 330897
10Sduke/*-
214104Salanb * SPDX-License-Identifier: BSD-3-Clause
30Sduke *
40Sduke * Copyright (c) 1991, 1993
50Sduke *	The Regents of the University of California.  All rights reserved.
60Sduke *
70Sduke * This code is derived from software contributed to Berkeley by
80Sduke * Edward Sze-Tyan Wang.
90Sduke *
100Sduke * Redistribution and use in source and binary forms, with or without
110Sduke * modification, are permitted provided that the following conditions
120Sduke * are met:
130Sduke * 1. Redistributions of source code must retain the above copyright
140Sduke *    notice, this list of conditions and the following disclaimer.
150Sduke * 2. Redistributions in binary form must reproduce the above copyright
160Sduke *    notice, this list of conditions and the following disclaimer in the
170Sduke *    documentation and/or other materials provided with the distribution.
180Sduke * 4. Neither the name of the University nor the names of its contributors
192362Sohair *    may be used to endorse or promote products derived from this software
202362Sohair *    without specific prior written permission.
212362Sohair *
220Sduke * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
230Sduke * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
240Sduke * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2514104Salanb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
260Sduke * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
270Sduke * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
280Sduke * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
290Sduke * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
306092Sdfuchs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
310Sduke * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
320Sduke * SUCH DAMAGE.
330Sduke */
340Sduke
350Sduke#include <sys/cdefs.h>
360Sduke
370Sduke__FBSDID("$FreeBSD: stable/11/usr.bin/tail/forward.c 330897 2018-03-14 03:19:51Z eadler $");
380Sduke
390Sduke#ifndef lint
406092Sdfuchsstatic const char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
416092Sdfuchs#endif
426092Sdfuchs
436092Sdfuchs#include <sys/param.h>
440Sduke#include <sys/mount.h>
4514104Salanb#include <sys/types.h>
460Sduke#include <sys/stat.h>
470Sduke#include <sys/time.h>
480Sduke#include <sys/mman.h>
490Sduke#include <sys/event.h>
500Sduke
510Sduke#include <err.h>
526092Sdfuchs#include <errno.h>
530Sduke#include <fcntl.h>
540Sduke#include <limits.h>
550Sduke#include <stdio.h>
560Sduke#include <stdlib.h>
576092Sdfuchs#include <string.h>
580Sduke#include <unistd.h>
590Sduke
600Sduke#include "extern.h"
610Sduke
620Sdukestatic void rlines(FILE *, const char *fn, off_t, struct stat *);
630Sdukestatic int show(file_info_t *);
640Sdukestatic void set_events(file_info_t *files);
650Sduke
660Sduke/* defines for inner loop actions */
670Sduke#define USE_SLEEP	0
680Sduke#define USE_KQUEUE	1
690Sduke#define ADD_EVENTS	2
700Sduke
710Sdukestatic struct kevent *ev;
720Sdukestatic int action = USE_SLEEP;
730Sdukestatic int kq;
740Sduke
750Sdukestatic const file_info_t *last;
760Sduke
770Sduke/*
780Sduke * forward -- display the file, from an offset, forward.
790Sduke *
800Sduke * There are eight separate cases for this -- regular and non-regular
810Sduke * files, by bytes or lines and from the beginning or end of the file.
820Sduke *
830Sduke * FBYTES	byte offset from the beginning of the file
840Sduke *	REG	seek
850Sduke *	NOREG	read, counting bytes
860Sduke *
870Sduke * FLINES	line offset from the beginning of the file
880Sduke *	REG	read, counting lines
890Sduke *	NOREG	read, counting lines
900Sduke *
910Sduke * RBYTES	byte offset from the end of the file
920Sduke *	REG	seek
930Sduke *	NOREG	cyclically read characters into a wrap-around buffer
940Sduke *
950Sduke * RLINES
960Sduke *	REG	mmap the file and step back until reach the correct offset.
970Sduke *	NOREG	cyclically read lines into a wrap-around array of buffers
980Sduke */
990Sdukevoid
1000Sdukeforward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
1010Sduke{
1020Sduke	int ch;
1030Sduke
1040Sduke	switch(style) {
1050Sduke	case FBYTES:
1060Sduke		if (off == 0)
1070Sduke			break;
1080Sduke		if (S_ISREG(sbp->st_mode)) {
1090Sduke			if (sbp->st_size < off)
1100Sduke				off = sbp->st_size;
1110Sduke			if (fseeko(fp, off, SEEK_SET) == -1) {
1120Sduke				ierr(fn);
1130Sduke				return;
1140Sduke			}
1150Sduke		} else while (off--)
1160Sduke			if ((ch = getc(fp)) == EOF) {
1170Sduke				if (ferror(fp)) {
1180Sduke					ierr(fn);
1190Sduke					return;
120				}
121				break;
122			}
123		break;
124	case FLINES:
125		if (off == 0)
126			break;
127		for (;;) {
128			if ((ch = getc(fp)) == EOF) {
129				if (ferror(fp)) {
130					ierr(fn);
131					return;
132				}
133				break;
134			}
135			if (ch == '\n' && !--off)
136				break;
137		}
138		break;
139	case RBYTES:
140		if (S_ISREG(sbp->st_mode)) {
141			if (sbp->st_size >= off &&
142			    fseeko(fp, -off, SEEK_END) == -1) {
143				ierr(fn);
144				return;
145			}
146		} else if (off == 0) {
147			while (getc(fp) != EOF);
148			if (ferror(fp)) {
149				ierr(fn);
150				return;
151			}
152		} else
153			if (bytes(fp, fn, off))
154				return;
155		break;
156	case RLINES:
157		if (S_ISREG(sbp->st_mode))
158			if (!off) {
159				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
160					ierr(fn);
161					return;
162				}
163			} else
164				rlines(fp, fn, off, sbp);
165		else if (off == 0) {
166			while (getc(fp) != EOF);
167			if (ferror(fp)) {
168				ierr(fn);
169				return;
170			}
171		} else
172			if (lines(fp, fn, off))
173				return;
174		break;
175	default:
176		break;
177	}
178
179	while ((ch = getc(fp)) != EOF)
180		if (putchar(ch) == EOF)
181			oerr();
182	if (ferror(fp)) {
183		ierr(fn);
184		return;
185	}
186	(void)fflush(stdout);
187}
188
189/*
190 * rlines -- display the last offset lines of the file.
191 */
192static void
193rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp)
194{
195	struct mapinfo map;
196	off_t curoff, size;
197	int i;
198
199	if (!(size = sbp->st_size))
200		return;
201	map.start = NULL;
202	map.fd = fileno(fp);
203	map.mapoff = map.maxoff = size;
204
205	/*
206	 * Last char is special, ignore whether newline or not. Note that
207	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
208	 */
209	curoff = size - 2;
210	while (curoff >= 0) {
211		if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
212			ierr(fn);
213			return;
214		}
215		for (i = curoff - map.mapoff; i >= 0; i--)
216			if (map.start[i] == '\n' && --off == 0)
217				break;
218		/* `i' is either the map offset of a '\n', or -1. */
219		curoff = map.mapoff + i;
220		if (i >= 0)
221			break;
222	}
223	curoff++;
224	if (mapprint(&map, curoff, size - curoff) != 0) {
225		ierr(fn);
226		exit(1);
227	}
228
229	/* Set the file pointer to reflect the length displayed. */
230	if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
231		ierr(fn);
232		return;
233	}
234	if (map.start != NULL && munmap(map.start, map.maplen)) {
235		ierr(fn);
236		return;
237	}
238}
239
240static int
241show(file_info_t *file)
242{
243	int ch;
244
245	while ((ch = getc(file->fp)) != EOF) {
246		if (last != file && no_files > 1) {
247			if (!qflag)
248				printfn(file->file_name, 1);
249			last = file;
250		}
251		if (putchar(ch) == EOF)
252			oerr();
253	}
254	(void)fflush(stdout);
255	if (ferror(file->fp)) {
256		fclose(file->fp);
257		file->fp = NULL;
258		ierr(file->file_name);
259		return 0;
260	}
261	clearerr(file->fp);
262	return 1;
263}
264
265static void
266set_events(file_info_t *files)
267{
268	int i, n = 0;
269	file_info_t *file;
270	struct timespec ts;
271	struct statfs sf;
272
273	ts.tv_sec = 0;
274	ts.tv_nsec = 0;
275
276	action = USE_KQUEUE;
277	for (i = 0, file = files; i < no_files; i++, file++) {
278		if (! file->fp)
279			continue;
280
281		if (fstatfs(fileno(file->fp), &sf) == 0 &&
282		    (sf.f_flags & MNT_LOCAL) == 0) {
283			action = USE_SLEEP;
284			return;
285		}
286
287		if (Fflag && fileno(file->fp) != STDIN_FILENO) {
288			EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE,
289			    EV_ADD | EV_ENABLE | EV_CLEAR,
290			    NOTE_DELETE | NOTE_RENAME, 0, 0);
291			n++;
292		}
293		EV_SET(&ev[n], fileno(file->fp), EVFILT_READ,
294		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
295		n++;
296	}
297
298	if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
299		action = USE_SLEEP;
300	}
301}
302
303/*
304 * follow -- display the file, from an offset, forward.
305 *
306 */
307void
308follow(file_info_t *files, enum STYLE style, off_t off)
309{
310	int active, ev_change, i, n = -1;
311	struct stat sb2;
312	file_info_t *file;
313	struct timespec ts;
314
315	/* Position each of the files */
316
317	file = files;
318	active = 0;
319	n = 0;
320	for (i = 0; i < no_files; i++, file++) {
321		if (file->fp) {
322			active = 1;
323			n++;
324			if (no_files > 1 && !qflag)
325				printfn(file->file_name, 1);
326			forward(file->fp, file->file_name, style, off, &file->st);
327			if (Fflag && fileno(file->fp) != STDIN_FILENO)
328				n++;
329		}
330	}
331	if (!Fflag && !active)
332		return;
333
334	last = --file;
335
336	kq = kqueue();
337	if (kq < 0)
338		err(1, "kqueue");
339	ev = malloc(n * sizeof(struct kevent));
340	if (! ev)
341	    err(1, "Couldn't allocate memory for kevents.");
342	set_events(files);
343
344	for (;;) {
345		ev_change = 0;
346		if (Fflag) {
347			for (i = 0, file = files; i < no_files; i++, file++) {
348				if (!file->fp) {
349					file->fp = fopen(file->file_name, "r");
350					if (file->fp != NULL &&
351					    fstat(fileno(file->fp), &file->st)
352					    == -1) {
353						fclose(file->fp);
354						file->fp = NULL;
355					}
356					if (file->fp != NULL)
357						ev_change++;
358					continue;
359				}
360				if (fileno(file->fp) == STDIN_FILENO)
361					continue;
362				if (stat(file->file_name, &sb2) == -1) {
363					if (errno != ENOENT)
364						ierr(file->file_name);
365					show(file);
366					if (file->fp != NULL) {
367						fclose(file->fp);
368						file->fp = NULL;
369					}
370					ev_change++;
371					continue;
372				}
373
374				if (sb2.st_ino != file->st.st_ino ||
375				    sb2.st_dev != file->st.st_dev ||
376				    sb2.st_nlink == 0) {
377					show(file);
378					file->fp = freopen(file->file_name, "r",
379					    file->fp);
380					if (file->fp != NULL)
381						memcpy(&file->st, &sb2,
382						    sizeof(struct stat));
383					else if (errno != ENOENT)
384						ierr(file->file_name);
385					ev_change++;
386				}
387			}
388		}
389
390		for (i = 0, file = files; i < no_files; i++, file++)
391			if (file->fp && !show(file))
392				ev_change++;
393
394		if (ev_change)
395			set_events(files);
396
397		switch (action) {
398		case USE_KQUEUE:
399			ts.tv_sec = 1;
400			ts.tv_nsec = 0;
401			/*
402			 * In the -F case we set a timeout to ensure that
403			 * we re-stat the file at least once every second.
404			 */
405			n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
406			if (n < 0)
407				err(1, "kevent");
408			if (n == 0) {
409				/* timeout */
410				break;
411			} else if (ev->filter == EVFILT_READ && ev->data < 0) {
412				/* file shrank, reposition to end */
413				if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) {
414					ierr(file->file_name);
415					continue;
416				}
417			}
418			break;
419
420		case USE_SLEEP:
421			(void) usleep(250000);
422			break;
423		}
424	}
425}
426