From 6404ed459468bcd3aeb35e21e73da960463461f0 Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Tue, 3 Aug 1999 21:29:17 +0000 Subject: [PATCH] Update dump-utmp from KerbNet 1.2. Add makefile rules for building dump-utmp on request. Fix line id for console on Solaris. (PR561) git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@11618 dc483132-0cff-0310-8789-dd5450dbe970 --- src/util/pty/ChangeLog | 28 +++++++++++ src/util/pty/Makefile.in | 4 ++ src/util/pty/dump-utmp.c | 99 +++++++++++++++++++++++++++----------- src/util/pty/update_utmp.c | 8 ++- 4 files changed, 109 insertions(+), 30 deletions(-) diff --git a/src/util/pty/ChangeLog b/src/util/pty/ChangeLog index efc93bc2d..859b4a7cd 100644 --- a/src/util/pty/ChangeLog +++ b/src/util/pty/ChangeLog @@ -1,3 +1,31 @@ +1999-08-03 Ken Raeburn + + * update_utmp.c (pty_update_utmp): Use "co" instead of "cons" for + console line on Solaris. Patch from Larry Schwimmer + (schwim@whatmore.Stanford.EDU). + + * Makefile.in (dump-utmp): Add a rule for building, never + automatically done. + (dump-utmp.o): Depends on dump-utmp.c. + + Updates from Cygnus KerbNet: + + * dump-utmp.c (ut_typename): Only define if + HAVE_STRUCT_UTMP_UT_TYPE is defined. + (main): Dump more info, and conditionalize it better. + + * dump-utmp.c (ctime): Declare, to prevent crashes on Alpha. + + * dump-utmp.c (UTMPX): Define if not defined but HAVE_UTMPX_H is + defined. + (ut_typename): Return shorter forms for some symbols. + (main): Require `-x' flag for [uw]tmpx file instead of guessing + from the name. Reject unknown `-' arguments. Print a message if + an error occurs while reading from utmpx file. Break up output + statements into smaller pieces. Conditionalize output of some + utmp fields on whether those fields are present. Print out exit + status fields and timestamp. + Fri Apr 23 23:13:57 1999 Tom Yu * update_utmp.c (pty_update_utmp): utx.ut_pid is a pid_t, and diff --git a/src/util/pty/Makefile.in b/src/util/pty/Makefile.in index 2fd37ca30..8f573d84e 100644 --- a/src/util/pty/Makefile.in +++ b/src/util/pty/Makefile.in @@ -46,6 +46,10 @@ all-unix:: includes pty_err.h all-unix:: all-liblinks +dump-utmp: dump-utmp.o + $(CC) $(LDFLAGS) -o dump-utmp dump-utmp.o +dump-utmp.o: dump-utmp.c + install-unix:: install-libs clean-unix:: diff --git a/src/util/pty/dump-utmp.c b/src/util/pty/dump-utmp.c index b349b9afe..7cc846911 100644 --- a/src/util/pty/dump-utmp.c +++ b/src/util/pty/dump-utmp.c @@ -2,33 +2,45 @@ #include #include +#ifndef UTMPX +#ifdef HAVE_UTMPX_H +#define UTMPX +#endif +#endif + #ifdef UTMPX #include #endif #include +extern char *ctime (); + +#if defined (HAVE_STRUCT_UTMP_UT_TYPE) || defined (UTMPX) char *ut_typename (t) { switch (t) { #define S(N) case N : return #N +#define S2(N,N2) case N : return #N2 S(EMPTY); S(RUN_LVL); S(BOOT_TIME); S(OLD_TIME); S(NEW_TIME); - S(INIT_PROCESS); - S(LOGIN_PROCESS); - S(USER_PROCESS); - S(DEAD_PROCESS); + S2(INIT_PROCESS,INIT); + S2(LOGIN_PROCESS,LOGIN); + S2(USER_PROCESS,USER); + S2(DEAD_PROCESS,DEAD); S(ACCOUNTING); default: return "??"; } } +#endif int main (argc, argv) int argc; char *argv[]; { int f; - char id[5], user[50]; + char id[5], user[50], host[100]; char *file = 0; int all = 0; + int is_utmpx = 0; while (*++argv) { @@ -37,6 +49,13 @@ int main (argc, argv) int argc; char *argv[]; { break; if (!strcmp ("-a", arg)) all = 1; + else if (!strcmp ("-x", arg)) + is_utmpx = 1; + else if (arg[0] == '-') + { + fprintf (stderr, "unknown arg `%s'\n", arg); + return 1; + } else if (file) { fprintf (stderr, "already got a file\n"); @@ -51,50 +70,74 @@ int main (argc, argv) int argc; char *argv[]; { exit (1); } id[4] = 0; + if (is_utmpx) { #ifdef UTMPX - if ('x' != file[strlen(file) - 1]) { struct utmpx u; - while (read (f, &u, sizeof (u)) == sizeof (u)) { - char c; + while (1) { + int nread = read (f, &u, sizeof (u)); + if (nread == 0) { + /* eof */ + return 0; + } else if (nread == -1) { + /* error */ + perror ("read"); + return 1; + } if ((u.ut_type == DEAD_PROCESS || u.ut_type == EMPTY) && !all) continue; strncpy (id, u.ut_id, 4); - printf ("%-8s:%-12s:%-4s:%6d %s", - u.ut_user, u.ut_line, id, - u.ut_pid, ut_typename (u.ut_type)); - /* ctime (&u.ut_xtime) + 4 */ + printf ("%-8s:%-12s:%-4s", u.ut_user, u.ut_line, id); + printf (":%5d", u.ut_pid); + printf ("(%5d,%5d)", u.ut_exit.e_termination, u.ut_exit.e_exit); + printf (" %-9s %s", ut_typename (u.ut_type), ctime (&u.ut_xtime) + 4); if (u.ut_syslen && u.ut_host[0]) - printf (" %s", u.ut_host); - printf ("\n"); - return 0; + printf (" %s\n", u.ut_host); } + abort (); +#else + fprintf (stderr, "utmpx support not compiled in\n"); + return 1; +#endif } /* else */ -#endif { struct utmp u; - user[sizeof(u.ut_user)] = 0; while (read (f, &u, sizeof (u)) == sizeof (u)) { - char c; +#ifdef EMPTY if ((u.ut_type == DEAD_PROCESS || u.ut_type == EMPTY) && !all) continue; +#endif +#ifdef HAVE_STRUCT_UTMP_UT_PID strncpy (id, u.ut_id, 4); strncpy (user, u.ut_user, sizeof (u.ut_user)); - printf ("%-8s:%-12s:%-4s:%6d %s", user, u.ut_line, id, - u.ut_pid, ut_typename (u.ut_type)); - printf ("\n"); -#if 0 - printf ("user: %-32s id: %s\n", user, id); - printf (" line: %-32s pid:%-6d type: %s\n", - u.ut_line, u.ut_pid, ut_typename (u.ut_type)); - printf (" exit_status: %d,%d\n", - u.ut_exit.e_termination, u.ut_exit.e_exit); - printf (" time: %s\n", ctime (&u.ut_time) + 4); + user[sizeof(u.ut_user)] = 0; + printf ("%-8s:%-12s:%-4s", user, u.ut_line, id); + printf (":%5d", u.ut_pid); +#else + strncpy (user, u.ut_name, sizeof (u.ut_name)); + user[sizeof(u.ut_name)] = 0; + printf ("%-8s:%-12s", user, u.ut_line); +#endif +#ifdef HAVE_STRUCT_UTMP_UT_HOST + { + char host[sizeof (u.ut_host) + 1]; + strncpy (host, u.ut_host, sizeof(u.ut_host)); + host[sizeof (u.ut_host)] = 0; + printf (":%-*s", sizeof (u.ut_host), host); + } +#endif +#ifdef HAVE_STRUCT_UTMP_UT_EXIT + printf ("(%5d,%5d)", u.ut_exit.e_termination, u.ut_exit.e_exit); +#endif +#ifdef HAVE_STRUCT_UTMP_UT_TYPE + printf (" %-9s", ut_typename (u.ut_type)); #endif + /* this ends with a newline */ + printf (" %s", ctime (&u.ut_time) + 4); } } diff --git a/src/util/pty/update_utmp.c b/src/util/pty/update_utmp.c index be4fc32d0..ce8b546f2 100644 --- a/src/util/pty/update_utmp.c +++ b/src/util/pty/update_utmp.c @@ -83,9 +83,13 @@ long pty_update_utmp (process_type, pid, username, line, host, flags) #endif #ifndef NO_UT_PID - if (!strcmp (line, "/dev/console")) + if (!strcmp (line, "/dev/console")) { +#if (defined(sun) && defined(__SVR4)) + strncpy (ent.ut_id, "co", 4); +#else strncpy (ent.ut_id, "cons", 4); - else { +#endif + } else { tmpx = line + strlen(line)-1; if (*(tmpx-1) != '/') tmpx--; /* last two characters, unless it's a / */ #ifdef __hpux -- 2.26.2