2 * acctdump.c - accounting data dump utility
4 * Copyright (C) 1998 Ian Jackson <ian@chiark.greenend.org.uk>
6 * This is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2,
9 * or (at your option) any later version.
11 * This is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this file; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 typedef unsigned long long u64;
36 /*#include <sys/acct.h>*/
37 typedef struct acct_v3 struct_acct;
41 static int forwards, nobanner, usestdin, raw, usages;
43 static int de_used, de_allocd;
44 static struct deventry {
49 static const struct cmdinfo cmdinfos[]= {
50 { "--forwards", 'f', 0, &forwards, 0, 0, 1, 0, 0 },
51 { "--no-banner", 'q', 0, &nobanner, 0, 0, 1, 0, 0 },
52 { "--stdin", 'p', 0, &usestdin, 0, 0, 1, 0, 0 },
53 { "--raw", 'r', 0, &raw, 0, 0, 1, 0, 0 },
54 { "--resource", 'u', 0, &usages, 0, 0, 1, 0, 0 },
58 static const char *sigabbrev[]= {
59 "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE",
60 "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT",
61 "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU",
62 "XFSZ", "VTALRM", "PROF", "WINCH", "IO"
65 static void usage(FILE *file) {
66 fputs("usage: acctdump [<options>] [<file> ...]\n"
67 "options: -f|--forwards -q|--no-banner -p|--stdin -r|--raw -u|--resource\n",
69 if (ferror(file)) { perror("print usage"); exit(8); }
72 void badusage(const char *fmt, ...) {
75 fputs("usage error: ",stderr);
77 vfprintf(stderr,fmt,al);
84 static void checkstdout(void) {
85 if (ferror(stdout)) { perror("stdout"); exit(8); }
88 static void scandev(const char *basename, int levelsleft) {
89 /* We deliberately ignore most errors */
93 int fnbufalloc, fnbufreq, r, basel, nallocd;
95 struct deventry *ndeventries;
97 if (levelsleft==0) return;
99 dir= opendir(basename);
101 fprintf(stderr, "%s: opendir: %s\n", basename, strerror(errno));
106 basel= strlen(basename);
108 while ((errno=0, de= readdir(dir))) {
109 fnbufreq= basel+1+strlen(de->d_name)+1;
110 if (fnbufalloc<fnbufreq) {
111 fnbufalloc= fnbufreq+10;
112 nfnbuf= realloc(fnbuf,fnbufalloc);
113 if (!nfnbuf) { free(fnbuf); fnbufalloc=0; continue; }
116 sprintf(fnbuf,"%s/%s",basename,de->d_name);
117 r= lstat(fnbuf,&stab);
119 fprintf(stderr, "%s: %s\n", fnbuf, strerror(errno));
122 if (S_ISCHR(stab.st_mode)) {
123 if (de_used >= de_allocd) {
124 nallocd= (de_allocd+10)<<1;
125 ndeventries= realloc(deventries,nallocd*sizeof(*deventries));
126 if (!ndeventries) continue;
128 deventries= ndeventries;
130 deventries[de_used].fn= strdup(fnbuf+5); /* remove /dev */
131 if (!deventries[de_used].fn) continue;
132 deventries[de_used].dev= stab.st_rdev;
134 } else if (S_ISDIR(stab.st_mode) && de->d_name[0] != '.') {
135 scandev(fnbuf,levelsleft-1);
139 fprintf(stderr, "%s: readdir: %s\n", basename, strerror(errno));
144 static int walkdev_cptr(const void *av, const void *bv) {
145 const struct deventry *a= av;
146 const struct deventry *b= bv;
147 return a->dev - b->dev;
150 static void printbanner(void) {
152 fputs("begin date command "
153 "uid gid tty dev FSDX exit",
156 fputs("begin date and time command "
157 "user group tty dev FSDX sigexit",
161 fputs(" user time sys time elap time minflt maxflt",
168 static void printrecord(const struct_acct *as, const char *filename) {
169 static int walkeddev;
175 struct deventry *deve, devlookfor;
181 printf("%10lu ",(unsigned long)as->ac_btime);
184 tm= localtime(&btime);
185 strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
189 printf("%-16.16s ", as->ac_comm);
191 pw= raw ? 0 : getpwuid(as->ac_uid);
192 if (pw) printf("%-8s ",pw->pw_name);
193 else printf("%-8ld ",(long)as->ac_uid);
195 gr= raw ? 0 : getgrgid(as->ac_gid);
196 if (gr) printf("%-8s ",gr->gr_name);
197 else printf("%-8ld ",(long)as->ac_gid);
200 if (!(as->ac_tty + 1) /* check for -1 without knowing type */) {
203 printf("%08lx ",(unsigned long)as->ac_tty);
206 if (!(as->ac_tty + 1)) {
211 qsort(deventries,de_used,sizeof(*deventries),walkdev_cptr);
215 devlookfor.dev= as->ac_tty;
216 deve= bsearch(&devlookfor,deventries,de_used,sizeof(*deventries),walkdev_cptr);
218 printf("%-10s ",deve->fn);
220 printf("%08lx ",(unsigned long)as->ac_tty);
226 for (i=1, fp= "FS4DX"; *fp; fp++, i<<=1) {
230 } else if (!isdigit(*fp)) {
239 dc= WCOREDUMP(as->ac_exitcode) ? 'd' : 'k';
241 if (WIFEXITED(as->ac_exitcode)) {
242 printf(" %3d",WEXITSTATUS(as->ac_exitcode));
243 } else if (WIFSIGNALED(as->ac_exitcode)) {
246 WTERMSIG(as->ac_exitcode));
248 printf("%04lx",(unsigned long)as->ac_exitcode);
251 if (WIFEXITED(as->ac_exitcode)) {
252 printf(" %6d",WEXITSTATUS(as->ac_exitcode));
253 } else if (WIFSIGNALED(as->ac_exitcode)) {
254 r= WTERMSIG(as->ac_exitcode);
255 if (r>0 && r<=sizeof(sigabbrev)/sizeof(*sigabbrev)) {
265 printf("#%04lx",(unsigned long)as->ac_exitcode);
270 printf(" %10lu %10lu %10lu %8ld %8ld",
271 (unsigned long)as->ac_utime,
272 (unsigned long)as->ac_stime,
273 (unsigned long)as->ac_etime,
274 (unsigned long)as->ac_minflt,
275 (unsigned long)as->ac_majflt);
282 static void processfile(FILE *file, const char *filename) {
288 while ((r= fread(&as,1,sizeof(as),file)) == sizeof(as)) {
289 printrecord(&as,filename);
292 long seekdist= -(long)sizeof(as);
293 r= fseek(file,0,SEEK_END); if (r) { perror(filename); exit(8); }
294 pos= ftell(file); if (pos==-1) { perror(filename); exit(8); }
295 if (pos % sizeof(as)) {
296 fprintf(stderr, "%s: File size is not an integral number "
297 "of accounting records", filename);
301 if (pos<sizeof(as)) break;
303 r= fseek(file,pos,SEEK_SET); if (r==-1) { perror(filename); exit(8); }
304 r= fread(&as,1,sizeof(as),file); if (r!=sizeof(as)) { perror(filename); exit(8); }
305 printrecord(&as,filename);
306 seekdist= -2*(long)sizeof(as);
309 if (ferror(file) || fclose(file)) { perror(filename); exit(8); }
312 static void processnamedfile(const char *filename) {
315 file= fopen(filename,"rb"); if (!file) { perror(filename); exit(8); }
316 processfile(file,filename);
319 int main(int argc, const char *const *argv) {
320 union { struct_acct ac; char c[1]; } xu;
321 myopt(&argv,cmdinfos);
322 if (!nobanner) printbanner();
323 fprintf(stdout,"s=%d %d\n",(int)sizeof(struct_acct),
324 (int)((char*)&xu.ac.ac_comm - xu.c));
326 processfile(stdin,"<standard input>");
328 processnamedfile("/var/account/pacct");
331 processnamedfile(*argv);
336 if (fflush(stdout)) { perror("flush stdout"); exit(8); }