On 12/29/18 5:05 PM, leo_...@volny.cz wrote:
> zeur here.
> 
> The following {,ab}use of tail(1):
> 
> % </var/log/messages tail -f
> 
> causes an abort trap 'cause of a missing "rpath" pledge. An excellent
> demonstration of pledge(2)'s usefulness, since it exposed a little bug
> that caused tail to try and re-open stdin, which is useless on
> OpenBSD[0] (unless we parse stdin as "/dev/stdin" and its ino would
> change -- unlikely).
> 
> The below patch fixes this by simply not trying to re-open stdin.
> 
>          --zeurkous.
> 
> [0] NetBSD, iirc, keeps track of the original path of opened files
>     (other systems might too), but I suppose that's irrelevant here :)
> 
I'd like to propose a slightly different diff, since POSIX states that a
filename "-" should mean stdin I would like to prepare the struct
tailfile to contain the is_stdin element, so this can be added a little
easier in the future.

OK?

martijn@

Index: extern.h
===================================================================
RCS file: /cvs/src/usr.bin/tail/extern.h,v
retrieving revision 1.12
diff -u -p -r1.12 extern.h
--- extern.h    19 Nov 2015 17:50:04 -0000      1.12
+++ extern.h    2 Jan 2019 08:52:28 -0000
@@ -40,6 +40,7 @@ struct tailfile {
        char            *fname;
        FILE            *fp;
        struct stat      sb;
+       int              is_stdin;
 };
 
 enum STYLE { NOTSET = 0, FBYTES, FLINES, RBYTES, RLINES, REVERSE };
Index: forward.c
===================================================================
RCS file: /cvs/src/usr.bin/tail/forward.c,v
retrieving revision 1.31
diff -u -p -r1.31 forward.c
--- forward.c   5 Jul 2016 05:06:27 -0000       1.31
+++ forward.c   2 Jan 2019 08:52:28 -0000
@@ -335,7 +335,8 @@ tfreopen(struct tailfile *tf) {
        struct tailfile                 **treopen, *ttf;
        int                               i;
 
-       if (tf && ((stat(tf->fname, &sb) != 0) || sb.st_ino != tf->sb.st_ino)) {
+       if (tf && !tf->is_stdin &&
+           ((stat(tf->fname, &sb) != 0) || sb.st_ino != tf->sb.st_ino)) {
                if (afiles < ++nfiles) {
                        afiles += AFILESINCR;
                        treopen = reallocarray(reopen, afiles, sizeof(*reopen));
Index: tail.c
===================================================================
RCS file: /cvs/src/usr.bin/tail/tail.c,v
retrieving revision 1.21
diff -u -p -r1.21 tail.c
--- tail.c      3 Feb 2016 12:23:57 -0000       1.21
+++ tail.c      2 Jan 2019 08:52:28 -0000
@@ -45,7 +45,6 @@
 #include "extern.h"
 
 int fflag, rflag, rval;
-int is_stdin;
 
 static void obsolete(char **);
 static void usage(void);
@@ -155,6 +154,7 @@ main(int argc, char *argv[])
        if (argc) {
                for (i = 0; *argv; i++) {
                        tf[i].fname = *argv++;
+                       tf[i].is_stdin = 0;
                        if ((tf[i].fp = fopen(tf[i].fname, "r")) == NULL ||
                            fstat(fileno(tf[i].fp), &(tf[i].sb))) {
                                ierr(tf[i].fname);
@@ -173,7 +173,7 @@ main(int argc, char *argv[])
 
                tf[0].fname = "stdin";
                tf[0].fp = stdin;
-               is_stdin = 1;
+               tf[0].is_stdin = 1;
 
                if (fstat(fileno(stdin), &(tf[0].sb))) {
                        ierr(tf[0].fname);

Reply via email to