1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1980, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)lastcomm.c 8.1 (Berkeley) 6/6/93"; 39 #endif 40 #endif /* not lint */ 41 #include <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include <sys/param.h> 45 #include <sys/stat.h> 46 #include <sys/acct.h> 47 48 #include <ctype.h> 49 #include <err.h> 50 #include <errno.h> 51 #include <fcntl.h> 52 #include <grp.h> 53 #include <pwd.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 #include "pathnames.h" 59 60 /*XXX*/#include <inttypes.h> 61 62 time_t expand(u_int); 63 char *flagbits(int); 64 const char *getdev(dev_t); 65 int readrec_forward(FILE *f, struct acctv2 *av2); 66 int readrec_backward(FILE *f, struct acctv2 *av2); 67 int requested(char *[], struct acctv2 *); 68 static void usage(void); 69 70 #define AC_UTIME 1 /* user */ 71 #define AC_STIME 2 /* system */ 72 #define AC_ETIME 4 /* elapsed */ 73 #define AC_CTIME 8 /* user + system time, default */ 74 75 #define AC_BTIME 16 /* starting time */ 76 #define AC_FTIME 32 /* exit time (starting time + elapsed time )*/ 77 78 int 79 main(int argc, char *argv[]) 80 { 81 struct acctv2 ab; 82 char *p; 83 FILE *fp; 84 int (*readrec)(FILE *f, struct acctv2 *av2); 85 time_t t; 86 int ch, rv; 87 const char *acctfile; 88 int flags = 0; 89 90 acctfile = _PATH_ACCT; 91 while ((ch = getopt(argc, argv, "f:usecSE")) != -1) 92 switch((char)ch) { 93 case 'f': 94 acctfile = optarg; 95 break; 96 97 case 'u': 98 flags |= AC_UTIME; /* user time */ 99 break; 100 case 's': 101 flags |= AC_STIME; /* system time */ 102 break; 103 case 'e': 104 flags |= AC_ETIME; /* elapsed time */ 105 break; 106 case 'c': 107 flags |= AC_CTIME; /* user + system time */ 108 break; 109 110 case 'S': 111 flags |= AC_BTIME; /* starting time */ 112 break; 113 case 'E': 114 /* exit time (starting time + elapsed time )*/ 115 flags |= AC_FTIME; 116 break; 117 118 case '?': 119 default: 120 usage(); 121 } 122 123 /* default user + system time and starting time */ 124 if (!flags) { 125 flags = AC_CTIME | AC_BTIME; 126 } 127 128 argc -= optind; 129 argv += optind; 130 131 if (strcmp(acctfile, "-") == 0) { 132 fp = stdin; 133 readrec = readrec_forward; 134 } else { 135 /* Open the file. */ 136 if ((fp = fopen(acctfile, "r")) == NULL) 137 err(1, "could not open %s", acctfile); 138 if (fseek(fp, 0l, SEEK_END) == -1) 139 err(1, "seek to end of %s failed", acctfile); 140 readrec = readrec_backward; 141 } 142 143 while ((rv = readrec(fp, &ab)) == 1) { 144 for (p = &ab.ac_comm[0]; 145 p < &ab.ac_comm[AC_COMM_LEN] && *p; ++p) 146 if (!isprint(*p)) 147 *p = '?'; 148 149 if (*argv && !requested(argv, &ab)) 150 continue; 151 152 (void)printf("%-*.*s %-7s %-*s %-8s", 153 AC_COMM_LEN, AC_COMM_LEN, ab.ac_comm, 154 flagbits(ab.ac_flagx), 155 MAXLOGNAME - 1, user_from_uid(ab.ac_uid, 0), 156 getdev(ab.ac_tty)); 157 158 159 /* user + system time */ 160 if (flags & AC_CTIME) { 161 (void)printf(" %6.3f secs", 162 (ab.ac_utime + ab.ac_stime) / 1000000); 163 } 164 165 /* usr time */ 166 if (flags & AC_UTIME) { 167 (void)printf(" %6.3f us", ab.ac_utime / 1000000); 168 } 169 170 /* system time */ 171 if (flags & AC_STIME) { 172 (void)printf(" %6.3f sy", ab.ac_stime / 1000000); 173 } 174 175 /* elapsed time */ 176 if (flags & AC_ETIME) { 177 (void)printf(" %8.3f es", ab.ac_etime / 1000000); 178 } 179 180 /* starting time */ 181 if (flags & AC_BTIME) { 182 (void)printf(" %.16s", ctime(&ab.ac_btime)); 183 } 184 185 /* exit time (starting time + elapsed time )*/ 186 if (flags & AC_FTIME) { 187 t = ab.ac_btime; 188 t += (time_t)(ab.ac_etime / 1000000); 189 (void)printf(" %.16s", ctime(&t)); 190 } 191 printf("\n"); 192 } 193 if (rv == EOF) 194 err(1, "read record from %s failed", acctfile); 195 196 if (fflush(stdout)) 197 err(1, "stdout"); 198 exit(0); 199 } 200 201 char * 202 flagbits(int f) 203 { 204 static char flags[20] = "-"; 205 char *p; 206 207 #define BIT(flag, ch) if (f & flag) *p++ = ch 208 209 p = flags + 1; 210 BIT(ASU, 'S'); 211 BIT(AFORK, 'F'); 212 BIT(ACOMPAT, 'C'); 213 BIT(ACORE, 'D'); 214 BIT(AXSIG, 'X'); 215 *p = '\0'; 216 return (flags); 217 } 218 219 int 220 requested(char *argv[], struct acctv2 *acp) 221 { 222 const char *p; 223 224 do { 225 p = user_from_uid(acp->ac_uid, 0); 226 if (!strcmp(p, *argv)) 227 return (1); 228 if ((p = getdev(acp->ac_tty)) && !strcmp(p, *argv)) 229 return (1); 230 if (!strncmp(acp->ac_comm, *argv, AC_COMM_LEN)) 231 return (1); 232 } while (*++argv); 233 return (0); 234 } 235 236 const char * 237 getdev(dev_t dev) 238 { 239 static dev_t lastdev = (dev_t)-1; 240 static const char *lastname; 241 242 if (dev == NODEV) /* Special case. */ 243 return ("__"); 244 if (dev == lastdev) /* One-element cache. */ 245 return (lastname); 246 lastdev = dev; 247 lastname = devname(dev, S_IFCHR); 248 return (lastname); 249 } 250 251 static void 252 usage(void) 253 { 254 (void)fprintf(stderr, 255 "usage: lastcomm [-EScesu] [-f file] [command ...] [user ...] [terminal ...]\n"); 256 exit(1); 257 } 258