Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/w add utmpx support.



details:   https://anonhg.NetBSD.org/src/rev/ba22074c57c2
branches:  trunk
changeset: 534481:ba22074c57c2
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jul 27 23:58:40 2002 +0000

description:
add utmpx support.

diffstat:

 usr.bin/w/Makefile |    4 +-
 usr.bin/w/w.c      |  253 ++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 178 insertions(+), 79 deletions(-)

diffs (truncated from 426 to 300 lines):

diff -r 4a830d406110 -r ba22074c57c2 usr.bin/w/Makefile
--- a/usr.bin/w/Makefile        Sat Jul 27 23:57:39 2002 +0000
+++ b/usr.bin/w/Makefile        Sat Jul 27 23:58:40 2002 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.16 2000/09/28 18:20:29 jdolecek Exp $
+#      $NetBSD: Makefile,v 1.17 2002/07/27 23:58:40 christos Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/6/93
 
 PROG=  w
@@ -7,7 +7,7 @@
 DPADD= ${LIBKVM}
 LDADD= -lkvm
 LINKS= ${BINDIR}/w ${BINDIR}/uptime
-CPPFLAGS+= -DSUPPORT_FTPD_UTMP
+CPPFLAGS+= -DSUPPORT_FTPD_UTMP -DSUPPORT_UTMP -DSUPPORT_UTMPX
 
 .PATH: ${.CURDIR}/../../bin/ps
 
diff -r 4a830d406110 -r ba22074c57c2 usr.bin/w/w.c
--- a/usr.bin/w/w.c     Sat Jul 27 23:57:39 2002 +0000
+++ b/usr.bin/w/w.c     Sat Jul 27 23:58:40 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: w.c,v 1.47 2001/11/05 03:35:49 enami Exp $     */
+/*     $NetBSD: w.c,v 1.48 2002/07/27 23:58:40 christos Exp $  */
 
 /*-
  * Copyright (c) 1980, 1991, 1993, 1994
@@ -43,7 +43,7 @@
 #if 0
 static char sccsid[] = "@(#)w.c        8.6 (Berkeley) 6/30/94";
 #else
-__RCSID("$NetBSD: w.c,v 1.47 2001/11/05 03:35:49 enami Exp $");
+__RCSID("$NetBSD: w.c,v 1.48 2002/07/27 23:58:40 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -81,7 +81,12 @@
 #include <time.h>
 #include <tzfile.h>
 #include <unistd.h>
+#ifdef SUPPORT_UTMP
 #include <utmp.h>
+#endif
+#ifdef SUPPORT_UTMPX
+#include <utmpx.h>
+#endif
 #include <vis.h>
 
 #include "extern.h"
@@ -89,7 +94,6 @@
 #define        max(a,b)        (((a)>(b))?(a):(b))
 
 struct timeval boottime;
-struct utmp    utmp;
 struct winsize ws;
 kvm_t         *kd;
 time_t         now;            /* the current time of day */
@@ -101,13 +105,17 @@
 int            sortidle;       /* sort bu idle time */
 char          *sel_user;       /* login of particular user selected */
 char           domain[MAXHOSTNAMELEN + 1];
+int maxname = 8, maxline = 3, maxhost = 16;
 
 /*
  * One of these per active utmp entry.
  */
 struct entry {
        struct  entry *next;
-       struct  utmp utmp;
+       char name[65];
+       char line[65];
+       char host[257];
+       struct timeval tv;
        dev_t   tdev;                   /* dev_t of terminal */
        time_t  idle;                   /* idle time of terminal in seconds */
        struct  kinfo_proc2 *kp;        /* `most interesting' proc */
@@ -118,7 +126,10 @@
 
 static void     pr_args(struct kinfo_proc2 *);
 static void     pr_header(time_t *, int);
-static struct stat *ttystat(char *, size_t);
+#if defined(SUPPORT_UTMP) || defined(SUPPORT_UTMPX)
+static struct stat *ttystat(char *);
+static void    process(struct entry *);
+#endif
 static void     usage(int);
 int    main(int, char **);
 
@@ -127,12 +138,16 @@
 {
        struct kinfo_proc2 *kp;
        struct hostent *hp;
-       struct stat *stp;
-       FILE *ut;
        struct in_addr l;
-       time_t touched;
-       int ch, i, nentries, nusers, wcmd, lognamelen;
+       int ch, i, nentries, nusers, wcmd;
        char *memf, *nlistf, *p, *x;
+       time_t then;
+#ifdef SUPPORT_UTMP
+       struct utmp *ut;
+#endif
+#ifdef SUPPORT_UTMPX
+       struct utmpx *utx;
+#endif
        const char *progname;
        char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
 
@@ -182,66 +197,82 @@
                errx(1, "%s", errbuf);
 
        (void)time(&now);
-       if ((ut = fopen(_PATH_UTMP, "r")) == NULL && wcmd)
-               err(1, "%s", _PATH_UTMP);
+       if (wcmd) {
+#ifdef SUPPORT_UTMP
+               setutent();
+#endif
+#ifdef SUPPORT_UTMP
+               setutxent();
+#endif
+       }
 
        if (*argv)
                sel_user = *argv;
 
-       for (nusers = 0; ut && fread(&utmp, sizeof(utmp), 1, ut);) {
-               if (utmp.ut_name[0] == '\0')
+       nusers = 0;
+#ifdef SUPPORT_UTMPX
+       while ((utx = getutxent()) != NULL) {
+               if (utx->ut_type != USER_PROCESS)
                        continue;
                ++nusers;
                if (wcmd == 0 || (sel_user &&
-                   strncmp(utmp.ut_name, sel_user, UT_NAMESIZE) != 0))
+                   strncmp(utx->ut_name, sel_user, sizeof(utx->ut_name) != 0)))
                        continue;
                if ((ep = calloc(1, sizeof(struct entry))) == NULL)
                        err(1, NULL);
+               (void)memcpy(ep->name, utx->ut_name, sizeof(utx->ut_name));
+               (void)memcpy(ep->line, utx->ut_line, sizeof(utx->ut_line));
+               (void)memcpy(ep->host, utx->ut_host, sizeof(utx->ut_host));
+               ep->name[sizeof(utx->ut_name)] = '\0';
+               ep->line[sizeof(utx->ut_line)] = '\0';
+               ep->host[sizeof(utx->ut_host)] = '\0';
+               ep->tv = utx->ut_tv;
                *nextp = ep;
                nextp = &(ep->next);
-               memmove(&(ep->utmp), &utmp, sizeof(struct utmp));
-               if (!(stp = ttystat(ep->utmp.ut_line, UT_LINESIZE))) {
-#ifdef SUPPORT_FTPD_UTMP
-                       /*
-                        * Hack to recognize and correctly parse
-                        * utmp entry made by ftpd. The "tty" used
-                        * by ftpd is not a real tty, just identifier in
-                        * form ftpPROCESS_ID. Pid parsed from the "tty name"
-                        * is used later to match corresponding process.
-                        */
-                       if (strncmp(ep->utmp.ut_line, "ftp", 3) == 0)
-                               ep->ftpd_pid =
-                                   strtol(ep->utmp.ut_line + 3, NULL, 10);
-#endif /* SUPPORT_FTPD_UTMP */
-                       
+               process(ep);
+       }
+#endif
+
+#ifdef SUPPORT_UTMP
+       while ((ut = getutent()) != NULL) {
+               ++nusers;
+               if (wcmd == 0 || (sel_user &&
+                   strncmp(ut->ut_name, sel_user, sizeof(ut->ut_name) != 0)))
                        continue;
+
+               /* Don't process entries that we have utmpx for */
+               for (ep = ehead; ep != NULL; ep = ep->next) {
+                       if (strncmp(ep->line, ut->ut_line,
+                           sizeof(ut->ut_line)) == 0)
+                               break;
                }
-               ep->tdev = stp->st_rdev;
-               /*
-                * If this is the console device, attempt to ascertain
-                * the true console device dev_t.
-                */
-               if (ep->tdev == 0) {
-                       int mib[2];
-                       size_t size;
+               if (ep != NULL)
+                       continue;
 
-                       mib[0] = CTL_KERN;
-                       mib[1] = KERN_CONSDEV;
-                       size = sizeof(dev_t);
-                       (void) sysctl(mib, 2, &ep->tdev, &size, NULL, 0);
-               }
+               if ((ep = calloc(1, sizeof(struct entry))) == NULL)
+                       err(1, NULL);
+               (void)memcpy(ep->name, ut->ut_name, sizeof(ut->ut_name));
+               (void)memcpy(ep->line, ut->ut_line, sizeof(ut->ut_line));
+               (void)memcpy(ep->host, ut->ut_host, sizeof(ut->ut_host));
+               ep->name[sizeof(ut->ut_name)] = '\0';
+               ep->line[sizeof(ut->ut_line)] = '\0';
+               ep->host[sizeof(ut->ut_host)] = '\0';
+               ep->tv.tv_sec = ut->ut_time;
+               ep->tv.tv_usec = 0;
+               *nextp = ep;
+               nextp = &(ep->next);
+               process(ep);
+       }
+#endif
 
-               touched = stp->st_atime;
-               if (touched < ep->utmp.ut_time) {
-                       /* tty untouched since before login */
-                       touched = ep->utmp.ut_time;
-               }
-               if ((ep->idle = now - touched) < 0)
-                       ep->idle = 0;
-       }
-       if (ut)
-               (void)fclose(ut);
-       else
+       if (wcmd) {
+#ifdef SUPPORT_UTMPX
+               endutxent();
+#endif
+#ifdef SUPPORT_UTMP
+               endutent();
+#endif
+       } else
                nusers = 1;
 
        if (header || wcmd == 0) {
@@ -255,7 +286,6 @@
                errx(1, "%s", kvm_geterr(kd));
 
        /* Include trailing space because TTY header starts one column early. */
-       lognamelen = sizeof("USER ") - 1 /* NUL */;
        for (i = 0; i < nentries; i++, kp++) {
 
                if (kp->p_stat == SIDL || kp->p_stat == SZOMB)
@@ -269,8 +299,6 @@
                                 */
                                if (proc_compare(ep->kp, kp)) {
                                        ep->kp = kp;
-                                       lognamelen = max(lognamelen,
-                                           strlen(kp->p_login));
                                }
                                break;
                        }
@@ -281,15 +309,13 @@
                        else if (ep->tdev == 0 && kp->p_tdev == NODEV &&
                            ep->ftpd_pid == kp->p_pid) {
                                ep->kp = kp;
-                               lognamelen = max(lognamelen,
-                                   strlen(kp->p_login));
                        }
 #endif /* SUPPORT_FTPD_UTMP */
                }
        }
 
        argwidth = printf("%-*sTTY %-*s %*s  IDLE WHAT\n",
-           lognamelen, "USER", UT_HOSTSIZE, "FROM",
+           maxname, "USER", maxhost, "FROM",
            7 /* "dddhhXm" */, "LOGIN@");
        argwidth -= sizeof("WHAT\n") - 1 /* NUL */;
 
@@ -318,6 +344,23 @@
                        *nextp = save;
                }
        }
+#if defined(SUPPORT_UTMP) && defined(SUPPORT_UTMPX)
+       else if (ehead != NULL) {
+               struct entry *from = ehead, *save;
+               
+               ehead = NULL;
+               while (from != NULL) {
+                       for (nextp = &ehead;
+                           (*nextp) && strcmp(from->line, (*nextp)->line) > 0;
+                           nextp = &(*nextp)->next)
+                               continue;
+                       save = from;
+                       from = from->next;
+                       save->next = *nextp;
+                       *nextp = save;
+               }
+       }
+#endif
                        
        if (!nflag) {
                int     rv;
@@ -331,16 +374,16 @@
        }
 
        for (ep = ehead; ep != NULL; ep = ep->next) {
-               char host_buf[UT_HOSTSIZE + 1];
+               char host_buf[MAXHOSTNAMELEN + 1];



Home | Main Index | Thread Index | Old Index