date and time created 88/12/22 13:04:38 by sam
[unix-history] / usr / src / usr.bin / w / w.c
index f707f7b..c7292d1 100644 (file)
@@ -11,7 +11,7 @@ char copyright[] =
 #endif not lint
 
 #ifndef lint
 #endif not lint
 
 #ifndef lint
-static char sccsid[] = "@(#)w.c        5.4 (Berkeley) %G%";
+static char sccsid[] = "@(#)w.c        5.9 (Berkeley) %G%";
 #endif not lint
 
 /*
 #endif not lint
 
 /*
@@ -32,6 +32,7 @@ static char sccsid[] = "@(#)w.c       5.4 (Berkeley) %G%";
 #include <sys/ioctl.h>
 #include <machine/pte.h>
 #include <sys/vm.h>
 #include <sys/ioctl.h>
 #include <machine/pte.h>
 #include <sys/vm.h>
+#include <sys/tty.h>
 
 #define NMAX sizeof(utmp.ut_name)
 #define LMAX sizeof(utmp.ut_line)
 
 #define NMAX sizeof(utmp.ut_name)
 #define LMAX sizeof(utmp.ut_line)
@@ -56,20 +57,20 @@ struct pr {
 int    nproc;
 
 struct nlist nl[] = {
 int    nproc;
 
 struct nlist nl[] = {
+       { "_avenrun" },
+#define        X_AVENRUN       0
+       { "_boottime" },
+#define        X_BOOTTIME      1
        { "_proc" },
        { "_proc" },
-#define        X_PROC          0
+#define        X_PROC          2
        { "_swapdev" },
        { "_swapdev" },
-#define        X_SWAPDEV       1
+#define        X_SWAPDEV       3
        { "_Usrptmap" },
        { "_Usrptmap" },
-#define        X_USRPTMA       2
+#define        X_USRPTMA       4
        { "_usrpt" },
        { "_usrpt" },
-#define        X_USRPT         3
+#define        X_USRPT         5
        { "_nswap" },
        { "_nswap" },
-#define        X_NSWAP         4
-       { "_avenrun" },
-#define        X_AVENRUN       5
-       { "_boottime" },
-#define        X_BOOTTIME      6
+#define        X_NSWAP         6
        { "_nproc" },
 #define        X_NPROC         7
        { "_dmmin" },
        { "_nproc" },
 #define        X_NPROC         7
        { "_dmmin" },
@@ -93,13 +94,13 @@ char        doing[520];             /* process attached to terminal */
 time_t proctime;               /* cpu time of process in doing */
 double avenrun[3];
 struct proc *aproc;
 time_t proctime;               /* cpu time of process in doing */
 double avenrun[3];
 struct proc *aproc;
+struct  tty ttyent;
 
 #define        DIV60(t)        ((t+30)/60)    /* x/60 rounded */ 
 
 #define        DIV60(t)        ((t+30)/60)    /* x/60 rounded */ 
-#define        TTYEQ           (tty == pr[i].w_tty && uid == pr[i].w_uid)
+#define        TTYEQ           (tty == pr[i].w_tty)
 #define IGINT          (1+3*1)         /* ignoring both SIGINT & SIGQUIT */
 
 char   *getargs();
 #define IGINT          (1+3*1)         /* ignoring both SIGINT & SIGQUIT */
 
 char   *getargs();
-char   *fread();
 char   *ctime();
 char   *rindex();
 FILE   *popen();
 char   *ctime();
 char   *rindex();
 FILE   *popen();
@@ -188,8 +189,6 @@ main(argc, argv)
                argc--; argv++;
        }
 
                argc--; argv++;
        }
 
-       if (ioctl(1, TIOCGWINSZ, &win) == -1 || win.ws_col > 70)
-               ttywidth = win.ws_col;
        if ((kmem = open("/dev/kmem", 0)) < 0) {
                fprintf(stderr, "No kmem\n");
                exit(1);
        if ((kmem = open("/dev/kmem", 0)) < 0) {
                fprintf(stderr, "No kmem\n");
                exit(1);
@@ -200,8 +199,13 @@ main(argc, argv)
                exit(1);
        }
 
                exit(1);
        }
 
-       if (firstchar != 'u')
+       if (firstchar == 'u')   /* uptime(1) */
+               nl[X_BOOTTIME+1].n_name = "";
+       else {                  /* then read in procs, get window size */
                readpr();
                readpr();
+               if (ioctl(1, TIOCGWINSZ, &win) != -1 && win.ws_col > 70)
+                       ttywidth = win.ws_col;
+       }
 
        ut = fopen("/etc/utmp","r");
        time(&now);
 
        ut = fopen("/etc/utmp","r");
        time(&now);
@@ -257,7 +261,7 @@ main(argc, argv)
                        printf(" %.2f", avenrun[i]);
                }
                printf("\n");
                        printf(" %.2f", avenrun[i]);
                }
                printf("\n");
-               if (firstchar == 'u')
+               if (firstchar == 'u')   /* if this was uptime(1), finished */
                        exit(0);
 
                /* Headers for rest of output */
                        exit(0);
 
                /* Headers for rest of output */
@@ -405,6 +409,7 @@ putline()
 }
 
 /* find & return number of minutes current tty has been idle */
 }
 
 /* find & return number of minutes current tty has been idle */
+time_t
 findidle()
 {
        struct stat stbuf;
 findidle()
 {
        struct stat stbuf;
@@ -476,6 +481,11 @@ prtat(time)
 /*
  * readpr finds and reads in the array pr, containing the interesting
  * parts of the proc and user tables for each live process.
 /*
  * readpr finds and reads in the array pr, containing the interesting
  * parts of the proc and user tables for each live process.
+ * We only accept procs whos controlling tty has a pgrp equal to the
+ * pgrp of the proc.  This accurately defines the notion of the current
+ * process(s), but because of time skew, we always read in the tty struct
+ * after reading the proc, even though the same tty struct may have been
+ * read earlier on.
  */
 readpr()
 {
  */
 readpr()
 {
@@ -521,7 +531,8 @@ readpr()
                lseek(kmem, (int)(aproc + pn), 0);
                read(kmem, &mproc, sizeof mproc);
                /* decide if it's an interesting process */
                lseek(kmem, (int)(aproc + pn), 0);
                read(kmem, &mproc, sizeof mproc);
                /* decide if it's an interesting process */
-               if (mproc.p_stat==0 || mproc.p_stat==SZOMB || mproc.p_pgrp==0)
+               if (mproc.p_stat==0 || mproc.p_stat==SZOMB 
+                   || mproc.p_stat==SSTOP || mproc.p_pgrp==0)
                        continue;
                /* find & read in the user structure */
                if ((mproc.p_flag & SLOAD) == 0) {
                        continue;
                /* find & read in the user structure */
                if ((mproc.p_flag & SLOAD) == 0) {
@@ -560,6 +571,13 @@ cont:
                if (up.u_ttyp == NULL)
                        continue;
 
                if (up.u_ttyp == NULL)
                        continue;
 
+               /* only include a process whose tty has a pgrp which matchs its own */
+               lseek(kmem, (long)up.u_ttyp, 0);
+               if (read(kmem, &ttyent, sizeof(ttyent)) != sizeof(ttyent))
+                       continue;
+               if (ttyent.t_pgrp != mproc.p_pgrp)
+                       continue;
+
                /* save the interesting parts */
                pr[np].w_pid = mproc.p_pid;
                pr[np].w_flag = mproc.p_flag;
                /* save the interesting parts */
                pr[np].w_pid = mproc.p_pid;
                pr[np].w_flag = mproc.p_flag;