[BACK]Return to lastcomm.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / lastcomm

Annotation of src/usr.bin/lastcomm/lastcomm.c, Revision 1.36

1.36    ! deraadt     1: /*     $OpenBSD: lastcomm.c,v 1.35 2024/01/19 14:25:03 deraadt Exp $   */
1.2       deraadt     2: /*     $NetBSD: lastcomm.c,v 1.9 1995/10/22 01:43:42 ghudson Exp $     */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.11      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
1.30      deraadt    33: #include <sys/types.h>
1.1       deraadt    34: #include <sys/stat.h>
                     35: #include <sys/acct.h>
                     36:
                     37: #include <ctype.h>
                     38: #include <err.h>
                     39: #include <fcntl.h>
1.2       deraadt    40: #include <math.h>
1.1       deraadt    41: #include <stdio.h>
                     42: #include <stdlib.h>
                     43: #include <string.h>
                     44: #include <unistd.h>
                     45: #include <utmp.h>
1.13      deraadt    46: #include <pwd.h>
1.1       deraadt    47: #include "pathnames.h"
                     48:
1.10      millert    49: time_t  expand(u_int);
                     50: char   *flagbits(int);
                     51: char   *getdev(dev_t);
                     52: int     requested(char *[], struct acct *);
                     53: void    usage(void);
1.21      millert    54:
                     55: #define SECSPERMIN     (60)
                     56: #define SECSPERHOUR    (60 * 60)
1.1       deraadt    57:
                     58: int
1.12      deraadt    59: main(int argc, char *argv[])
1.1       deraadt    60: {
1.9       mpech      61:        char *p;
1.1       deraadt    62:        struct acct ab;
                     63:        struct stat sb;
                     64:        FILE *fp;
                     65:        off_t size;
                     66:        time_t t;
1.2       deraadt    67:        double delta;
1.1       deraadt    68:        int ch;
                     69:        char *acctfile;
1.22      deraadt    70:
1.23      deraadt    71:        if (pledge("stdio rpath getpw", NULL) == -1)
                     72:                err(1, "pledge");
1.1       deraadt    73:
                     74:        acctfile = _PATH_ACCT;
1.4       millert    75:        while ((ch = getopt(argc, argv, "f:")) != -1)
1.19      okan       76:                switch(ch) {
1.1       deraadt    77:                case 'f':
                     78:                        acctfile = optarg;
                     79:                        break;
                     80:                default:
                     81:                        usage();
                     82:                }
                     83:        argc -= optind;
                     84:        argv += optind;
                     85:
                     86:        /* Open the file. */
                     87:        if ((fp = fopen(acctfile, "r")) == NULL || fstat(fileno(fp), &sb))
                     88:                err(1, "%s", acctfile);
                     89:
                     90:        /*
                     91:         * Round off to integral number of accounting records, probably
                     92:         * not necessary, but it doesn't hurt.
                     93:         */
                     94:        size = sb.st_size - sb.st_size % sizeof(struct acct);
                     95:
                     96:        /* Check if any records to display. */
                     97:        if (size < sizeof(struct acct))
                     98:                exit(0);
                     99:
                    100:        /*
                    101:         * Seek to before the last entry in the file; use lseek(2) in case
                    102:         * the file is bigger than a "long".
                    103:         */
                    104:        size -= sizeof(struct acct);
                    105:        if (lseek(fileno(fp), size, SEEK_SET) == -1)
                    106:                err(1, "%s", acctfile);
                    107:
                    108:        for (;;) {
1.32      bluhm     109:                char commpid[sizeof(ab.ac_comm) + 13];
                    110:
1.1       deraadt   111:                if (fread(&ab, sizeof(struct acct), 1, fp) != 1)
                    112:                        err(1, "%s", acctfile);
                    113:
                    114:                if (ab.ac_comm[0] == '\0') {
                    115:                        ab.ac_comm[0] = '?';
                    116:                        ab.ac_comm[1] = '\0';
                    117:                } else
                    118:                        for (p = &ab.ac_comm[0];
1.24      guenther  119:                            p < &ab.ac_comm[sizeof ab.ac_comm] && *p; ++p)
1.18      deraadt   120:                                if (!isprint((unsigned char)*p))
1.1       deraadt   121:                                        *p = '?';
1.32      bluhm     122:                snprintf(commpid, sizeof(commpid), "%s[%d]",
                    123:                    ab.ac_comm, ab.ac_pid);
1.30      deraadt   124:                if (!*argv || requested(argv, &ab)) {
1.6       flipk     125:                        t = expand(ab.ac_utime) + expand(ab.ac_stime);
                    126:                        (void)printf("%-*.*s %-7s %-*.*s %-*.*s %6.2f secs %.16s",
1.32      bluhm     127:                            (int)sizeof commpid,
                    128:                            (int)sizeof commpid,
                    129:                            commpid, flagbits(ab.ac_flag), UT_NAMESIZE,
1.7       deraadt   130:                            UT_NAMESIZE, user_from_uid(ab.ac_uid, 0),
                    131:                            UT_LINESIZE, UT_LINESIZE, getdev(ab.ac_tty),
                    132:                            t / (double)AHZ, ctime(&ab.ac_btime));
1.6       flipk     133:                        delta = expand(ab.ac_etime) / (double)AHZ;
1.8       pvalchev  134:                        printf(" (%1.0f:%02.0f:%05.2f)\n",
1.7       deraadt   135:                            delta / SECSPERHOUR,
1.14      deraadt   136:                            fmod(delta, (double)SECSPERHOUR) / SECSPERMIN,
                    137:                            fmod(delta, (double)SECSPERMIN));
1.6       flipk     138:                }
1.5       flipk     139:
                    140:                if (size == 0)
                    141:                        break;
1.6       flipk     142:                /* seek to previous entry */
                    143:                if (fseek(fp, 2 * -(long)sizeof(struct acct), SEEK_CUR) == -1)
                    144:                        err(1, "%s", acctfile);
                    145:                size -= sizeof(struct acct);
1.1       deraadt   146:        }
                    147:        exit(0);
                    148: }
                    149:
                    150: time_t
1.12      deraadt   151: expand(u_int t)
1.1       deraadt   152: {
1.9       mpech     153:        time_t nt;
1.1       deraadt   154:
                    155:        nt = t & 017777;
                    156:        t >>= 13;
                    157:        while (t) {
                    158:                t--;
                    159:                nt <<= 3;
                    160:        }
                    161:        return (nt);
                    162: }
                    163:
                    164: char *
1.12      deraadt   165: flagbits(int f)
1.1       deraadt   166: {
                    167:        static char flags[20] = "-";
                    168:        char *p;
                    169:
                    170: #define        BIT(flag, ch)   if (f & flag) *p++ = ch
                    171:
                    172:        p = flags + 1;
                    173:        BIT(AFORK, 'F');
1.29      bluhm     174:        BIT(AMAP, 'M');
1.1       deraadt   175:        BIT(ACORE, 'D');
                    176:        BIT(AXSIG, 'X');
1.25      bluhm     177:        BIT(APLEDGE, 'P');
1.26      bluhm     178:        BIT(ATRAP, 'T');
1.28      bluhm     179:        BIT(AUNVEIL, 'U');
1.34      deraadt   180:        BIT(APINSYS, 'S');
1.36    ! deraadt   181:        BIT(ABTCFI, 'B');
1.1       deraadt   182:        *p = '\0';
                    183:        return (flags);
                    184: }
                    185:
                    186: int
1.12      deraadt   187: requested(char *argv[], struct acct *acp)
1.1       deraadt   188: {
                    189:        do {
                    190:                if (!strcmp(user_from_uid(acp->ac_uid, 0), *argv))
                    191:                        return (1);
                    192:                if (!strcmp(getdev(acp->ac_tty), *argv))
                    193:                        return (1);
1.24      guenther  194:                if (!strncmp(acp->ac_comm, *argv, sizeof acp->ac_comm))
1.1       deraadt   195:                        return (1);
                    196:        } while (*++argv);
                    197:        return (0);
                    198: }
                    199:
                    200: char *
1.12      deraadt   201: getdev(dev_t dev)
1.1       deraadt   202: {
                    203:        static dev_t lastdev = (dev_t)-1;
                    204:        static char *lastname;
                    205:
1.30      deraadt   206:        if (dev == -1)                          /* Special case. */
1.1       deraadt   207:                return ("__");
                    208:        if (dev == lastdev)                     /* One-element cache. */
                    209:                return (lastname);
                    210:        lastdev = dev;
                    211:        if ((lastname = devname(dev, S_IFCHR)) == NULL)
                    212:                lastname = "??";
                    213:        return (lastname);
                    214: }
                    215:
                    216: void
1.12      deraadt   217: usage(void)
1.1       deraadt   218: {
                    219:        (void)fprintf(stderr,
1.16      jmc       220:            "usage: lastcomm [-f file] [command ...] [user ...] [terminal ...]\n");
1.1       deraadt   221:        exit(1);
                    222: }