2006-12-06 17:26:12 -06:00
|
|
|
/* tailf.c -- tail a log file and then follow it
|
|
|
|
* Created: Tue Jan 9 15:49:21 1996 by faith@acm.org
|
|
|
|
* Copyright 1996, 2003 Rickard E. Faith (faith@acm.org)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
2007-12-06 18:56:06 -06:00
|
|
|
*
|
2006-12-06 17:26:12 -06:00
|
|
|
* less -F and tail -f cause a disk access every five seconds. This
|
|
|
|
* program avoids this problem by waiting for the file size to change.
|
|
|
|
* Hence, the file is not accessed, and the access time does not need to be
|
|
|
|
* flushed back to disk. This is sort of a "stealth" tail.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2007-10-03 16:15:03 -05:00
|
|
|
#include <string.h>
|
2007-12-06 18:56:06 -06:00
|
|
|
#include <sys/types.h>
|
2006-12-06 17:26:12 -06:00
|
|
|
#include <sys/stat.h>
|
2007-12-06 18:56:06 -06:00
|
|
|
#include <fcntl.h>
|
2007-10-11 06:57:35 -05:00
|
|
|
#include <ctype.h>
|
2007-12-06 18:56:06 -06:00
|
|
|
#include <errno.h>
|
2011-02-24 15:39:11 -06:00
|
|
|
#include <getopt.h>
|
2007-12-17 17:52:39 -06:00
|
|
|
#ifdef HAVE_INOTIFY_INIT
|
2007-12-06 18:56:06 -06:00
|
|
|
#include <sys/inotify.h>
|
2007-12-17 17:52:39 -06:00
|
|
|
#endif
|
2010-10-22 10:23:31 -05:00
|
|
|
|
2006-12-06 17:26:12 -06:00
|
|
|
#include "nls.h"
|
2010-10-22 10:23:31 -05:00
|
|
|
#include "xalloc.h"
|
2011-02-24 15:39:11 -06:00
|
|
|
#include "strutils.h"
|
2011-01-25 15:44:52 -06:00
|
|
|
#include "c.h"
|
2012-04-04 12:39:15 -05:00
|
|
|
#include "closestream.h"
|
2006-12-06 17:26:12 -06:00
|
|
|
|
2007-10-11 06:57:35 -05:00
|
|
|
#define DEFAULT_LINES 10
|
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
static void
|
|
|
|
tailf(const char *filename, int lines)
|
2006-12-06 17:26:12 -06:00
|
|
|
{
|
2007-12-06 18:56:06 -06:00
|
|
|
char *buf, *p;
|
|
|
|
int head = 0;
|
|
|
|
int tail = 0;
|
|
|
|
FILE *str;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(str = fopen(filename, "r")))
|
2012-07-15 03:39:57 -05:00
|
|
|
err(EXIT_FAILURE, _("cannot open %s"), filename);
|
2007-12-06 18:56:06 -06:00
|
|
|
|
2011-03-18 03:52:07 -05:00
|
|
|
buf = xmalloc((lines ? lines : 1) * BUFSIZ);
|
2007-12-06 18:56:06 -06:00
|
|
|
p = buf;
|
|
|
|
while (fgets(p, BUFSIZ, str)) {
|
|
|
|
if (++tail >= lines) {
|
|
|
|
tail = 0;
|
|
|
|
head = 1;
|
|
|
|
}
|
|
|
|
p = buf + (tail * BUFSIZ);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (head) {
|
|
|
|
for (i = tail; i < lines; i++)
|
|
|
|
fputs(buf + (i * BUFSIZ), stdout);
|
|
|
|
for (i = 0; i < tail; i++)
|
|
|
|
fputs(buf + (i * BUFSIZ), stdout);
|
|
|
|
} else {
|
|
|
|
for (i = head; i < tail; i++)
|
|
|
|
fputs(buf + (i * BUFSIZ), stdout);
|
|
|
|
}
|
2006-12-06 17:26:12 -06:00
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
fflush(stdout);
|
|
|
|
free(buf);
|
|
|
|
fclose(str);
|
2006-12-06 17:26:12 -06:00
|
|
|
}
|
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
static void
|
|
|
|
roll_file(const char *filename, off_t *size)
|
2006-12-06 17:26:12 -06:00
|
|
|
{
|
2007-12-06 18:56:06 -06:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
2010-08-14 04:15:47 -05:00
|
|
|
off_t pos;
|
2007-12-06 18:56:06 -06:00
|
|
|
|
2011-03-11 07:25:45 -06:00
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0)
|
2012-07-15 03:39:57 -05:00
|
|
|
err(EXIT_FAILURE, _("cannot open %s"), filename);
|
2007-12-06 18:56:06 -06:00
|
|
|
|
|
|
|
if (fstat(fd, &st) == -1)
|
2015-01-29 14:21:48 -06:00
|
|
|
err(EXIT_FAILURE, _("stat of %s failed"), filename);
|
2007-12-06 18:56:06 -06:00
|
|
|
|
|
|
|
if (st.st_size == *size) {
|
|
|
|
close(fd);
|
|
|
|
return;
|
2006-12-06 17:26:12 -06:00
|
|
|
}
|
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
if (lseek(fd, *size, SEEK_SET) != (off_t)-1) {
|
|
|
|
ssize_t rc, wc;
|
2006-12-06 17:26:12 -06:00
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
while ((rc = read(fd, buf, sizeof(buf))) > 0) {
|
|
|
|
wc = write(STDOUT_FILENO, buf, rc);
|
|
|
|
if (rc != wc)
|
2009-11-20 20:41:14 -06:00
|
|
|
warnx(_("incomplete write to \"%s\" (written %zd, expected %zd)\n"),
|
2007-12-06 18:56:06 -06:00
|
|
|
filename, wc, rc);
|
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2010-08-14 04:15:47 -05:00
|
|
|
|
|
|
|
pos = lseek(fd, 0, SEEK_CUR);
|
|
|
|
|
|
|
|
/* If we've successfully read something, use the file position, this
|
|
|
|
* avoids data duplication. If we read nothing or hit an error, reset
|
|
|
|
* to the reported size, this handles truncated files.
|
|
|
|
*/
|
|
|
|
*size = (pos != -1 && pos != *size) ? pos : st.st_size;
|
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
close(fd);
|
|
|
|
}
|
2007-10-21 22:27:15 -05:00
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
static void
|
|
|
|
watch_file(const char *filename, off_t *size)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
roll_file(filename, size);
|
2014-01-24 06:04:14 -06:00
|
|
|
xusleep(250000);
|
2007-12-06 18:56:06 -06:00
|
|
|
} while(1);
|
2006-12-06 17:26:12 -06:00
|
|
|
}
|
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
|
|
|
|
#ifdef HAVE_INOTIFY_INIT
|
|
|
|
|
|
|
|
#define EVENTS (IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)
|
|
|
|
#define NEVENTS 4
|
|
|
|
|
|
|
|
static int
|
|
|
|
watch_file_inotify(const char *filename, off_t *size)
|
2006-12-06 17:26:12 -06:00
|
|
|
{
|
2007-12-06 18:56:06 -06:00
|
|
|
char buf[ NEVENTS * sizeof(struct inotify_event) ];
|
|
|
|
int fd, ffd, e;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
fd = inotify_init();
|
|
|
|
if (fd == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ffd = inotify_add_watch(fd, filename, EVENTS);
|
2009-08-11 08:22:35 -05:00
|
|
|
if (ffd == -1) {
|
|
|
|
if (errno == ENOSPC)
|
|
|
|
errx(EXIT_FAILURE, _("%s: cannot add inotify watch "
|
|
|
|
"(limit of inotify watches was reached)."),
|
|
|
|
filename);
|
|
|
|
|
|
|
|
err(EXIT_FAILURE, _("%s: cannot add inotify watch."), filename);
|
|
|
|
}
|
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
while (ffd >= 0) {
|
|
|
|
len = read(fd, buf, sizeof(buf));
|
|
|
|
if (len < 0 && (errno == EINTR || errno == EAGAIN))
|
|
|
|
continue;
|
|
|
|
if (len < 0)
|
2009-08-11 08:22:35 -05:00
|
|
|
err(EXIT_FAILURE,
|
|
|
|
_("%s: cannot read inotify events"), filename);
|
2007-12-06 18:56:06 -06:00
|
|
|
|
|
|
|
for (e = 0; e < len; ) {
|
|
|
|
struct inotify_event *ev = (struct inotify_event *) &buf[e];
|
|
|
|
|
|
|
|
if (ev->mask & IN_MODIFY)
|
|
|
|
roll_file(filename, size);
|
|
|
|
else {
|
|
|
|
close(ffd);
|
|
|
|
ffd = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
e += sizeof(struct inotify_event) + ev->len;
|
|
|
|
}
|
2007-10-11 06:57:35 -05:00
|
|
|
}
|
2007-12-06 18:56:06 -06:00
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_INOTIFY_INIT */
|
|
|
|
|
2011-02-24 15:39:11 -06:00
|
|
|
static void __attribute__ ((__noreturn__)) usage(FILE *out)
|
|
|
|
{
|
2014-12-24 10:56:09 -06:00
|
|
|
fputs(USAGE_HEADER, out);
|
|
|
|
fprintf(out, _(" %s [option] <file>\n"), program_invocation_short_name);
|
2011-02-24 15:39:11 -06:00
|
|
|
|
2014-12-22 15:57:17 -06:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("Follow the growth of a log file.\n"), out);
|
|
|
|
|
2014-12-24 10:56:09 -06:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
|
|
|
fputs(_(" -n, --lines <number> output the last <number> lines\n"), out);
|
|
|
|
fputs(_(" -<number> same as '-n <number>'\n"), out);
|
2011-02-24 15:39:11 -06:00
|
|
|
|
2014-12-24 10:56:09 -06:00
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(USAGE_HELP, out);
|
|
|
|
fputs(USAGE_VERSION, out);
|
2014-09-28 14:51:40 -05:00
|
|
|
fprintf(out, USAGE_MAN_TAIL("tailf(1)"));
|
2014-12-24 10:56:09 -06:00
|
|
|
|
2011-02-24 15:39:11 -06:00
|
|
|
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parses -N option */
|
2012-07-16 11:47:29 -05:00
|
|
|
static long old_style_option(int *argc, char **argv)
|
2011-02-24 15:39:11 -06:00
|
|
|
{
|
|
|
|
int i = 1, nargs = *argc;
|
|
|
|
long lines = -1;
|
|
|
|
|
|
|
|
while(i < nargs) {
|
|
|
|
if (argv[i][0] == '-' && isdigit(argv[i][1])) {
|
|
|
|
lines = strtol_or_err(argv[i] + 1,
|
|
|
|
_("failed to parse number of lines"));
|
|
|
|
nargs--;
|
2011-03-08 07:13:41 -06:00
|
|
|
if (nargs - i)
|
|
|
|
memmove(argv + i, argv + i + 1,
|
|
|
|
sizeof(char *) * (nargs - i));
|
2011-02-24 15:39:11 -06:00
|
|
|
} else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
*argc = nargs;
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *filename;
|
2011-02-24 15:39:11 -06:00
|
|
|
long lines;
|
|
|
|
int ch;
|
2007-12-06 18:56:06 -06:00
|
|
|
struct stat st;
|
|
|
|
off_t size = 0;
|
|
|
|
|
2011-02-24 15:39:11 -06:00
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "lines", required_argument, 0, 'n' },
|
|
|
|
{ "version", no_argument, 0, 'V' },
|
|
|
|
{ "help", no_argument, 0, 'h' },
|
|
|
|
{ NULL, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2013-04-08 14:32:49 -05:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
atexit(close_stdout);
|
|
|
|
|
2011-02-24 15:39:11 -06:00
|
|
|
lines = old_style_option(&argc, argv);
|
|
|
|
if (lines < 0)
|
|
|
|
lines = DEFAULT_LINES;
|
|
|
|
|
|
|
|
while ((ch = getopt_long(argc, argv, "n:N:Vh", longopts, NULL)) != -1)
|
|
|
|
switch((char)ch) {
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
lines = strtol_or_err(optarg,
|
|
|
|
_("failed to parse number of lines"));
|
|
|
|
break;
|
|
|
|
case 'V':
|
2014-09-28 14:51:39 -05:00
|
|
|
printf(UTIL_LINUX_VERSION);
|
2011-02-24 15:39:11 -06:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'h':
|
|
|
|
usage(stdout);
|
|
|
|
default:
|
|
|
|
usage(stderr);
|
2007-12-06 18:56:06 -06:00
|
|
|
}
|
2006-12-06 17:26:12 -06:00
|
|
|
|
2011-02-24 15:39:11 -06:00
|
|
|
if (argc == optind)
|
|
|
|
errx(EXIT_FAILURE, _("no input file specified"));
|
2006-12-06 17:26:12 -06:00
|
|
|
|
2011-02-24 15:39:11 -06:00
|
|
|
filename = argv[optind];
|
2006-12-06 17:26:12 -06:00
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
if (stat(filename, &st) != 0)
|
2015-01-29 14:21:48 -06:00
|
|
|
err(EXIT_FAILURE, _("stat of %s failed"), filename);
|
2007-10-18 07:52:46 -05:00
|
|
|
|
2007-12-06 18:56:06 -06:00
|
|
|
size = st.st_size;;
|
|
|
|
tailf(filename, lines);
|
|
|
|
|
|
|
|
#ifdef HAVE_INOTIFY_INIT
|
|
|
|
if (!watch_file_inotify(filename, &size))
|
|
|
|
#endif
|
|
|
|
watch_file(filename, &size);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2006-12-06 17:26:12 -06:00
|
|
|
}
|
2007-12-06 18:56:06 -06:00
|
|
|
|