From c95b3d5b5710e4b6052ceb88c666414db216bcce Mon Sep 17 00:00:00 2001 From: philip Date: Tue, 4 Sep 2018 09:53:45 +0000 Subject: [PATCH] MFC r319508: Fix a memory leak with last free memory allocated to 'buf' Submitted by: Thomas Rix MFC r338352: Add libxo(3) support to last(1). --- usr.bin/last/Makefile | 1 + usr.bin/last/last.1 | 16 ++++++++- usr.bin/last/last.c | 77 ++++++++++++++++++++++++++++--------------- 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/usr.bin/last/Makefile b/usr.bin/last/Makefile index fd63cd989b2..3edb032fd6c 100644 --- a/usr.bin/last/Makefile +++ b/usr.bin/last/Makefile @@ -2,6 +2,7 @@ # $FreeBSD$ PROG= last +LIBADD= xo NO_WFORMAT= diff --git a/usr.bin/last/last.1 b/usr.bin/last/last.1 index 19e0f0bc7f2..54bb07187c2 100644 --- a/usr.bin/last/last.1 +++ b/usr.bin/last/last.1 @@ -28,7 +28,7 @@ .\" @(#)last.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" -.Dd June 6, 2015 +.Dd August 28, 2018 .Dt LAST 1 .Os .Sh NAME @@ -36,6 +36,7 @@ .Nd indicate last logins of users and ttys .Sh SYNOPSIS .Nm +.Op Fl -libxo .Op Fl swy .Oo .Fl d @@ -72,6 +73,13 @@ will so indicate. .Pp The following options are available: .Bl -tag -width indent-two +.It Fl -libxo +Generate output via +.Xr libxo 3 +in a selection of different human and machine readable formats. +See +.Xr xo_parse_args 3 +for details on command line arguments. .It Fl d Ar date Specify the snapshot date and time. All users logged in at the snapshot date and time will @@ -201,6 +209,8 @@ login data base .Xr getutxent 3 , .Xr ac 8 , .Xr lastlogin 8 +.Xr libxo 3 , +.Xr xo_parse_args 3 .Sh HISTORY .Nm utility first appeared in @@ -211,6 +221,10 @@ The original version was written by .An Howard P. Katseff ; .An Keith Bostic rewrote it in 1986/87 to add functionality and to improve code quality. +.An Philip Paeps +added +.Xr libxo 3 +support in August 2018. .Sh BUGS If a login shell should terminate abnormally for some reason, it is likely that a logout record will not be written to the diff --git a/usr.bin/last/last.c b/usr.bin/last/last.c index 1db10b399df..0a2e2df2552 100644 --- a/usr.bin/last/last.c +++ b/usr.bin/last/last.c @@ -1,6 +1,7 @@ /* * Copyright (c) 1987, 1993, 1994 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2018 Philip Paeps * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -58,6 +59,8 @@ __FBSDID("$FreeBSD$"); #include #include +#include + #define NO 0 /* false/no */ #define YES 1 /* true/yes */ #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; @@ -108,7 +111,7 @@ static void wtmp(void); static void usage(void) { - (void)fprintf(stderr, + xo_error( "usage: last [-swy] [-d [[CC]YY][MMDD]hhmm[.SS]] [-f file] [-h host]\n" " [-n maxrec] [-t tty] [user ...]\n"); exit(1); @@ -123,6 +126,11 @@ main(int argc, char *argv[]) (void) setlocale(LC_TIME, ""); d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); + argc = xo_parse_args(argc, argv); + if (argc < 0) + exit(1); + atexit(xo_finish_atexit); + maxrec = -1; snaptime = 0; while ((ch = getopt(argc, argv, "0123456789d:f:h:n:st:wy")) != -1) @@ -157,7 +165,7 @@ main(int argc, char *argv[]) maxrec = strtol(optarg, &p, 10); if (p == optarg || *p != '\0' || errno != 0 || maxrec <= 0) - errx(1, "%s: bad line count", optarg); + xo_errx(1, "%s: bad line count", optarg); break; case 's': sflag++; /* Show delta as seconds */ @@ -212,14 +220,16 @@ wtmp(void) SLIST_INIT(&idlist); (void)time(&t); + xo_open_container("last-information"); + /* Load the last entries from the file. */ if (setutxdb(UTXDB_LOG, file) != 0) - err(1, "%s", file); + xo_err(1, "%s", file); while ((ut = getutxent()) != NULL) { if (amount % 128 == 0) { buf = realloc(buf, (amount + 128) * sizeof *ut); if (buf == NULL) - err(1, "realloc"); + xo_err(1, "realloc"); } memcpy(&buf[amount++], ut, sizeof *ut); if (t > ut->ut_tv.tv_sec) @@ -228,12 +238,17 @@ wtmp(void) endutxent(); /* Display them in reverse order. */ + xo_open_list("last"); while (amount > 0) doentry(&buf[--amount]); - + xo_close_list("last"); + free(buf); tm = localtime(&t); (void) strftime(ct, sizeof(ct), "%+", tm); - printf("\n%s begins %s\n", ((file == NULL) ? "utx.log" : file), ct); + xo_emit("\n{:utxdb/%s}", (file == NULL) ? "utx.log" : file); + xo_attr("seconds", "%lu", (unsigned long) t); + xo_emit(" begins {:begins/%s}\n", ct); + xo_close_container("last-information"); } /* @@ -288,7 +303,7 @@ doentry(struct utmpx *bp) /* add new one */ tt = malloc(sizeof(struct idtab)); if (tt == NULL) - errx(1, "malloc failure"); + xo_errx(1, "malloc failure"); tt->logout = currentout; memcpy(tt->id, bp->ut_id, sizeof bp->ut_id); SLIST_INSERT_HEAD(&idlist, tt, list); @@ -324,6 +339,7 @@ printentry(struct utmpx *bp, struct idtab *tt) if (maxrec != -1 && !maxrec--) exit(0); + xo_open_instance("last"); t = bp->ut_tv.tv_sec; tm = localtime(&t); (void) strftime(ct, sizeof(ct), d_first ? @@ -331,48 +347,55 @@ printentry(struct utmpx *bp, struct idtab *tt) (yflag ? "%a %b %e %Y %R" : "%a %b %e %R"), tm); switch (bp->ut_type) { case BOOT_TIME: - printf("%-42s", "boot time"); + xo_emit("{:user/%-42s/%s}", "boot time"); break; case SHUTDOWN_TIME: - printf("%-42s", "shutdown time"); + xo_emit("{:user/%-42s/%s}", "shutdown time"); break; case OLD_TIME: - printf("%-42s", "old time"); + xo_emit("{:user/%-42s/%s}", "old time"); break; case NEW_TIME: - printf("%-42s", "new time"); + xo_emit("{:user/%-42s/%s}", "new time"); break; case USER_PROCESS: - printf("%-10s %-8s %-22.22s", + xo_emit("{:user/%-10s/%s} {:tty/%-8s/%s} {:from/%-22.22s/%s}", bp->ut_user, bp->ut_line, bp->ut_host); break; } - printf(" %s%c", ct, tt == NULL ? '\n' : ' '); + xo_attr("seconds", "%lu", (unsigned long)t); + xo_emit(" {:login-time/%s%c/%s}", ct, tt == NULL ? '\n' : ' '); if (tt == NULL) - return; + goto end; if (!tt->logout) { - puts(" still logged in"); - return; + xo_emit(" {:logout-time/still logged in}\n"); + goto end; } if (tt->logout < 0) { tt->logout = -tt->logout; - printf("- %s", crmsg); + xo_emit("- {:logout-reason/%s}", crmsg); } else { tm = localtime(&tt->logout); (void) strftime(ct, sizeof(ct), "%R", tm); - printf("- %s", ct); + xo_attr("seconds", "%lu", (unsigned long)tt->logout); + xo_emit("- {:logout-time/%s}", ct); } delta = tt->logout - bp->ut_tv.tv_sec; + xo_attr("seconds", "%ld", (long)delta); if (sflag) { - printf(" (%8ld)\n", (long)delta); + xo_emit(" ({:session-length/%8ld})\n", (long)delta); } else { tm = gmtime(&delta); (void) strftime(ct, sizeof(ct), width >= 8 ? "%T" : "%R", tm); if (delta < 86400) - printf(" (%s)\n", ct); + xo_emit(" ({:session-length/%s})\n", ct); else - printf(" (%ld+%s)\n", (long)delta / 86400, ct); + xo_emit(" ({:session-length/%ld+%s})\n", + (long)delta / 86400, ct); } + +end: + xo_close_instance("last"); } /* @@ -423,7 +446,7 @@ addarg(int type, char *arg) ARG *cur; if ((cur = malloc(sizeof(ARG))) == NULL) - errx(1, "malloc failure"); + xo_errx(1, "malloc failure"); cur->next = arglist; cur->type = type; cur->name = arg; @@ -448,7 +471,7 @@ hostconv(char *arg) if (first) { first = 0; if (gethostname(name, sizeof(name))) - err(1, "gethostname"); + xo_err(1, "gethostname"); hostdot = strchr(name, '.'); } if (hostdot && !strcasecmp(hostdot, argdot)) @@ -471,7 +494,7 @@ ttyconv(char *arg) if (strlen(arg) == 2) { /* either 6 for "ttyxx" or 8 for "console" */ if ((mval = malloc(8)) == NULL) - errx(1, "malloc failure"); + xo_errx(1, "malloc failure"); if (!strcmp(arg, "co")) (void)strcpy(mval, "console"); else { @@ -501,9 +524,9 @@ dateconv(char *arg) /* Start with the current time. */ if (time(&timet) < 0) - err(1, "time"); + xo_err(1, "time"); if ((t = localtime(&timet)) == NULL) - err(1, "localtime"); + xo_err(1, "localtime"); /* [[CC]YY]MMDDhhmm[.SS] */ if ((p = strchr(arg, '.')) == NULL) @@ -552,7 +575,7 @@ dateconv(char *arg) t->tm_isdst = -1; /* Figure out DST. */ timet = mktime(t); if (timet == -1) -terr: errx(1, +terr: xo_errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); return timet; } -- 2.45.0