From owner-svn-src-head@freebsd.org Sun Jun 9 22:55:24 2019 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 26B0315AE18D; Sun, 9 Jun 2019 22:55:24 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B0E9E89D82; Sun, 9 Jun 2019 22:55:23 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 9682199CB; Sun, 9 Jun 2019 22:55:23 +0000 (UTC) (envelope-from oshogbo@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x59MtNBQ068213; Sun, 9 Jun 2019 22:55:23 GMT (envelope-from oshogbo@FreeBSD.org) Received: (from oshogbo@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x59MtMmW068206; Sun, 9 Jun 2019 22:55:22 GMT (envelope-from oshogbo@FreeBSD.org) Message-Id: <201906092255.x59MtMmW068206@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: oshogbo set sender to oshogbo@FreeBSD.org using -f From: Mariusz Zaborski Date: Sun, 9 Jun 2019 22:55:22 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r348842 - head/usr.bin/tail X-SVN-Group: head X-SVN-Commit-Author: oshogbo X-SVN-Commit-Paths: head/usr.bin/tail X-SVN-Commit-Revision: 348842 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: B0E9E89D82 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.96 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_SHORT(-0.96)[-0.964,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US]; NEURAL_HAM_MEDIUM(-1.00)[-0.998,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 09 Jun 2019 22:55:24 -0000 Author: oshogbo Date: Sun Jun 9 22:55:21 2019 New Revision: 348842 URL: https://svnweb.freebsd.org/changeset/base/348842 Log: tail: fix the checks if the file was rotated The freopen(3) was replaced with fileargs_open(3) and fclose(3). In the following function, we skip if the stream is standard in, so it is safe to do so. This also requires us to change the logic first to open the file and then check its status. The stat(2) is disallowed in capability mode. This commit unbrakes the -F option. The bug was introduced in the r348708. Reported by: pho Tested by: pho Modified: head/usr.bin/tail/extern.h head/usr.bin/tail/forward.c head/usr.bin/tail/misc.c head/usr.bin/tail/read.c head/usr.bin/tail/reverse.c head/usr.bin/tail/tail.c Modified: head/usr.bin/tail/extern.h ============================================================================== --- head/usr.bin/tail/extern.h Sun Jun 9 22:45:07 2019 (r348841) +++ head/usr.bin/tail/extern.h Sun Jun 9 22:55:21 2019 (r348842) @@ -78,3 +78,4 @@ int maparound(struct mapinfo *, off_t); void printfn(const char *, int); extern int Fflag, fflag, qflag, rflag, rval, no_files; +extern fileargs_t *fa; Modified: head/usr.bin/tail/forward.c ============================================================================== --- head/usr.bin/tail/forward.c Sun Jun 9 22:45:07 2019 (r348841) +++ head/usr.bin/tail/forward.c Sun Jun 9 22:55:21 2019 (r348842) @@ -57,6 +57,9 @@ static const char sccsid[] = "@(#)forward.c 8.1 (Berke #include #include +#include +#include + #include "extern.h" static void rlines(FILE *, const char *fn, off_t, struct stat *); @@ -310,6 +313,7 @@ follow(file_info_t *files, enum STYLE style, off_t off int active, ev_change, i, n = -1; struct stat sb2; file_info_t *file; + FILE *ftmp; struct timespec ts; /* Position each of the files */ @@ -346,7 +350,9 @@ follow(file_info_t *files, enum STYLE style, off_t off if (Fflag) { for (i = 0, file = files; i < no_files; i++, file++) { if (!file->fp) { - file->fp = fopen(file->file_name, "r"); + file->fp = + fileargs_fopen(fa, file->file_name, + "r"); if (file->fp != NULL && fstat(fileno(file->fp), &file->st) == -1) { @@ -359,7 +365,9 @@ follow(file_info_t *files, enum STYLE style, off_t off } if (fileno(file->fp) == STDIN_FILENO) continue; - if (stat(file->file_name, &sb2) == -1) { + ftmp = fileargs_fopen(fa, file->file_name, "r"); + if (ftmp == NULL || + fstat(fileno(file->fp), &sb2) == -1) { if (errno != ENOENT) ierr(file->file_name); show(file); @@ -367,6 +375,9 @@ follow(file_info_t *files, enum STYLE style, off_t off fclose(file->fp); file->fp = NULL; } + if (ftmp != NULL) { + fclose(ftmp); + } ev_change++; continue; } @@ -375,14 +386,13 @@ follow(file_info_t *files, enum STYLE style, off_t off sb2.st_dev != file->st.st_dev || sb2.st_nlink == 0) { show(file); - file->fp = freopen(file->file_name, "r", - file->fp); - if (file->fp != NULL) - memcpy(&file->st, &sb2, - sizeof(struct stat)); - else if (errno != ENOENT) - ierr(file->file_name); + fclose(file->fp); + file->fp = ftmp; + memcpy(&file->st, &sb2, + sizeof(struct stat)); ev_change++; + } else { + fclose(ftmp); } } } Modified: head/usr.bin/tail/misc.c ============================================================================== --- head/usr.bin/tail/misc.c Sun Jun 9 22:45:07 2019 (r348841) +++ head/usr.bin/tail/misc.c Sun Jun 9 22:55:21 2019 (r348842) @@ -51,6 +51,9 @@ static const char sccsid[] = "@(#)misc.c 8.1 (Berkeley #include #include +#include +#include + #include "extern.h" void Modified: head/usr.bin/tail/read.c ============================================================================== --- head/usr.bin/tail/read.c Sun Jun 9 22:45:07 2019 (r348841) +++ head/usr.bin/tail/read.c Sun Jun 9 22:55:21 2019 (r348842) @@ -51,6 +51,9 @@ static const char sccsid[] = "@(#)read.c 8.1 (Berkeley #include #include +#include +#include + #include "extern.h" /* Modified: head/usr.bin/tail/reverse.c ============================================================================== --- head/usr.bin/tail/reverse.c Sun Jun 9 22:45:07 2019 (r348841) +++ head/usr.bin/tail/reverse.c Sun Jun 9 22:55:21 2019 (r348842) @@ -55,6 +55,9 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include + #include "extern.h" static void r_buf(FILE *, const char *); Modified: head/usr.bin/tail/tail.c ============================================================================== --- head/usr.bin/tail/tail.c Sun Jun 9 22:45:07 2019 (r348841) +++ head/usr.bin/tail/tail.c Sun Jun 9 22:55:21 2019 (r348842) @@ -65,6 +65,7 @@ static const char sccsid[] = "@(#)tail.c 8.1 (Berkeley #include "extern.h" int Fflag, fflag, qflag, rflag, rval, no_files; +fileargs_t *fa; static file_info_t *files; @@ -90,10 +91,9 @@ main(int argc, char *argv[]) int i, ch, first; file_info_t *file; char *p; - fileargs_t *fa; cap_rights_t rights; - cap_rights_init(&rights, CAP_FSTAT, CAP_FCNTL, CAP_MMAP_RW); + cap_rights_init(&rights, CAP_FSTAT, CAP_FSTATFS, CAP_FCNTL, CAP_MMAP_RW); if (caph_rights_limit(STDIN_FILENO, &rights) < 0 || caph_limit_stderr() < 0 || caph_limit_stdout() < 0) err(1, "can't limit stdio rights");