Hi,

There's no need to call pledge(2) so many times, or on many places, with the
same promises, just call it once before the switch case while at the same time
hoisting one unveil(2) so they are all grouped.

The call to pledge(2) on file() can also be simplified since "stdio rpath getpw"
will already be activated when we reach it.

OK?

Index: who.c
===================================================================
RCS file: /cvs/src/usr.bin/who/who.c,v
retrieving revision 1.31
diff -u -p -u -r1.31 who.c
--- who.c       10 Nov 2021 15:06:24 -0000      1.31
+++ who.c       10 Nov 2021 15:31:25 -0000
@@ -125,10 +125,15 @@ main(int argc, char *argv[])
                if (unveil(_PATH_DEV, "r") == -1)
                        err(1, "unveil %s", _PATH_DEV);
        }
+       if (argc == 1) {
+               if (unveil(*argv, "r") == -1)
+                       err(1, "unveil %s", *argv);
+       }
+       if (pledge("stdio rpath getpw", NULL) == -1)
+               err(1, "pledge");
+
        switch (argc) {
        case 0:                                 /* who */
-               if (pledge("stdio rpath getpw", NULL) == -1)
-                       err(1, "pledge");
                ufp = file(_PATH_UTMP);
 
                if (only_current_term) {
@@ -155,10 +160,6 @@ main(int argc, char *argv[])
                }
                break;
        case 1:                                 /* who utmp_file */
-               if (unveil(*argv, "r") == -1)
-                       err(1, "unveil %s", *argv);
-               if (pledge("stdio rpath getpw", NULL) == -1)
-                       err(1, "pledge");
                ufp = file(*argv);
 
                if (only_current_term) {
@@ -184,8 +185,6 @@ main(int argc, char *argv[])
                }
                break;
        case 2:                                 /* who am i */
-               if (pledge("stdio rpath getpw", NULL) == -1)
-                       err(1, "pledge");
                ufp = file(_PATH_UTMP);
                who_am_i(ufp);
                break;
@@ -301,10 +300,7 @@ file(char *name)
                err(1, "%s", name);
                /* NOTREACHED */
        }
-       if (show_term || show_idle) {
-               if (pledge("stdio rpath getpw", NULL) == -1)
-                       err(1, "pledge");
-       } else {
+       if (!show_term && !show_idle) {
                if (pledge("stdio getpw", NULL) == -1)
                        err(1, "pledge");
        }

Reply via email to