chiark / gitweb /
pacct file has moved
[chiark-utils.git] / cprogs / acctdump.c
1 /*
2  * acctdump.c - accounting data dump utility
3  *
4  * Copyright (C) 1998 Ian Jackson <ian@chiark.greenend.org.uk>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <time.h>
24 #include <stdarg.h>
25 #include <wait.h>
26 #include <string.h>
27 #include <pwd.h>
28 #include <grp.h>
29 #include <dirent.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33
34 typedef unsigned long long u64;
35 #include "acct.h"
36 /*#include <sys/acct.h>*/
37 typedef struct acct_v3 struct_acct;
38
39 #include "myopt.h"
40
41 static int forwards, nobanner, usestdin, raw, usages;
42
43 static int de_used, de_allocd;
44 static struct deventry {
45   const char *fn;
46   dev_t dev;
47 } *deventries;
48
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 },
55   {  0                                                 }
56 };
57
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"
63 };
64
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",
68         file);
69   if (ferror(file)) { perror("print usage"); exit(8); }
70 }
71
72 void badusage(const char *fmt, ...) {
73   va_list al;
74
75   fputs("usage error: ",stderr);
76   va_start(al,fmt);
77   vfprintf(stderr,fmt,al);
78   va_end(al);
79   fputs("\n",stderr);
80   usage(stderr);
81   exit(12);
82 }
83
84 static void checkstdout(void) {
85   if (ferror(stdout)) { perror("stdout"); exit(8); }
86 }
87
88 static void scandev(const char *basename, int levelsleft) {
89   /* We deliberately ignore most errors */
90   DIR *dir;
91   struct dirent *de;
92   struct stat stab;
93   int fnbufalloc, fnbufreq, r, basel, nallocd;
94   char *fnbuf, *nfnbuf;
95   struct deventry *ndeventries;
96   
97   if (levelsleft==0) return;
98
99   dir= opendir(basename); 
100   if (!dir) {
101     fprintf(stderr, "%s: opendir: %s\n", basename, strerror(errno));
102     return;
103   }
104   fnbufalloc= 0;
105   fnbuf= 0;
106   basel= strlen(basename);
107
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; }
114       fnbuf= nfnbuf;
115     }
116     sprintf(fnbuf,"%s/%s",basename,de->d_name);
117     r= lstat(fnbuf,&stab);
118     if (r) {
119       fprintf(stderr, "%s: %s\n", fnbuf, strerror(errno));
120       continue;
121     }
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;
127         de_allocd= nallocd;
128         deventries= ndeventries;
129       }
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;
133       de_used++;
134     } else if (S_ISDIR(stab.st_mode) && de->d_name[0] != '.') {
135       scandev(fnbuf,levelsleft-1);
136     }
137   }
138   if (errno)
139       fprintf(stderr, "%s: readdir: %s\n", basename, strerror(errno));
140   closedir(dir);
141   free(fnbuf);
142 }
143
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;
148 }
149   
150 static void printbanner(void) {
151   if (raw) {
152     fputs("begin date command          "
153           "uid      gid      tty dev  FSDX exit",
154           stdout);
155   } else {
156     fputs("begin date and time command          "
157           "user     group    tty dev    FSDX sigexit",
158           stdout);
159   }
160   if (usages) {
161     fputs("  user time   sys time  elap time   minflt   maxflt",
162           stdout);
163   }
164   putchar('\n');
165   checkstdout();
166 }
167
168 static void printrecord(const struct_acct *as, const char *filename) {
169   static int walkeddev;
170
171   int i, dc, r;
172   const char *fp;
173   char buf[100];
174   struct tm *tm;
175   struct deventry *deve, devlookfor;
176   struct passwd *pw;
177   struct group *gr;
178   time_t btime;
179
180   if (raw) {
181     printf("%10lu ",(unsigned long)as->ac_btime);
182   } else {
183     btime= as->ac_btime;
184     tm= localtime(&btime);
185     strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
186     printf("%19s ",buf);
187   }
188   
189   printf("%-16.16s ", as->ac_comm);
190   
191   pw= raw ? 0 : getpwuid(as->ac_uid);
192   if (pw) printf("%-8s ",pw->pw_name);
193   else printf("%-8ld ",(long)as->ac_uid);
194   
195   gr= raw ? 0 : getgrgid(as->ac_gid);
196   if (gr) printf("%-8s ",gr->gr_name);
197   else printf("%-8ld ",(long)as->ac_gid);
198
199   if (raw) {
200     if (!(as->ac_tty + 1) /* check for -1 without knowing type */) {
201       printf("-        ");
202     } else {
203       printf("%08lx ",(unsigned long)as->ac_tty);
204     }
205   } else {
206     if (!(as->ac_tty + 1)) {
207       printf("-          ");
208     } else {
209       if (!walkeddev) {
210         scandev("/dev",4);
211         qsort(deventries,de_used,sizeof(*deventries),walkdev_cptr);
212         walkeddev= 1;
213       }
214       devlookfor.fn= 0;
215       devlookfor.dev= as->ac_tty;
216       deve= bsearch(&devlookfor,deventries,de_used,sizeof(*deventries),walkdev_cptr);
217       if (deve) {
218         printf("%-10s ",deve->fn);
219       } else {
220         printf("%08lx   ",(unsigned long)as->ac_tty);
221       }
222     }
223   }
224
225   r= as->ac_flag;
226   for (i=1, fp= "FS4DX"; *fp; fp++, i<<=1) {
227     if (r&i) {
228       putchar(*fp);
229       r &= ~i;
230     } else if (!isdigit(*fp)) {
231       putchar(' ');
232     }
233   }
234   if (r) {
235     printf("#%x",r);
236   }
237   putchar(' ');
238   
239   dc= WCOREDUMP(as->ac_exitcode) ? 'd' : 'k';
240   if (raw) {
241     if (WIFEXITED(as->ac_exitcode)) {
242       printf(" %3d",WEXITSTATUS(as->ac_exitcode));
243     } else if (WIFSIGNALED(as->ac_exitcode)) {
244       printf("%c%3d",
245              dc,
246              WTERMSIG(as->ac_exitcode));
247     } else {
248       printf("%04lx",(unsigned long)as->ac_exitcode);
249     }
250   } else {
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)) {
256         printf("%c%6s",
257                dc,
258                sigabbrev[r-1]);
259       } else {
260         printf("%cSIG%-3d",
261                dc,
262                r);
263       }
264     } else {
265       printf("#%04lx",(unsigned long)as->ac_exitcode);
266     }
267   }
268
269   if (usages) {
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);
276   }
277   putchar('\n');
278
279   checkstdout();
280 }
281
282 static void processfile(FILE *file, const char *filename) {
283   struct_acct as;
284   long pos;
285   int r;
286   
287   if (forwards) {
288     while ((r= fread(&as,1,sizeof(as),file)) == sizeof(as)) {
289       printrecord(&as,filename);
290     }
291   } else {
292     r= fseek(file,0,SEEK_END); if (r) { perror(filename); exit(8); }
293     pos= ftell(file); if (pos==-1) { perror(filename); exit(8); }
294     if (pos % sizeof(as)) { 
295       fprintf(stderr, "%s: File size is not an integral number "
296               "of accounting records\n", filename);
297       exit(8);
298     }
299     for (;;) {
300       if (pos<sizeof(as)) break;
301       pos -= sizeof(as);
302       r= fseek(file,pos,SEEK_SET); if (r==-1) { perror(filename); exit(8); }
303       r= fread(&as,1,sizeof(as),file); if (r!=sizeof(as)) { perror(filename); exit(8); }
304       printrecord(&as,filename);
305     }
306   }
307   if (ferror(file) || fclose(file)) { perror(filename); exit(8); }
308 }
309
310 static void processnamedfile(const char *filename) {
311   FILE *file;
312
313   file= fopen(filename,"rb"); if (!file) { perror(filename); exit(8); }
314   processfile(file,filename);
315 }
316
317 int main(int argc, const char *const *argv) {
318   myopt(&argv,cmdinfos);
319   if (!nobanner) printbanner();
320   if (usestdin) {
321     processfile(stdin,"<standard input>");
322   } else if (!*argv) {
323     processnamedfile("/var/log/account/pacct");
324   } else {
325     while (*argv) {
326       processnamedfile(*argv);
327       argv++;
328     }
329   }
330   checkstdout();
331   if (fflush(stdout)) { perror("flush stdout"); exit(8); }
332   return 0;
333 }