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.
37 static int forwards, nobanner, usestdin, raw, usages;
39 static int de_used, de_allocd;
40 static struct deventry {
45 static const struct cmdinfo cmdinfos[]= {
46 { "--forwards", 'f', 0, &forwards, 0, 0, 1, 0, 0 },
47 { "--no-banner", 'q', 0, &nobanner, 0, 0, 1, 0, 0 },
48 { "--stdin", 'p', 0, &usestdin, 0, 0, 1, 0, 0 },
49 { "--raw", 'r', 0, &raw, 0, 0, 1, 0, 0 },
50 { "--resource", 'u', 0, &usages, 0, 0, 1, 0, 0 },
54 static const char *sigabbrev[]= {
55 "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE",
56 "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT",
57 "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU",
58 "XFSZ", "VTALRM", "PROF", "WINCH", "IO"
61 static void usage(FILE *file) {
62 fputs("usage: acctdump [<options>] [<file> ...]\n"
63 "options: -f|--forwards -q|--no-banner -p|--stdin -r|--raw -u|--resource\n",
65 if (ferror(file)) { perror("print usage"); exit(8); }
68 void badusage(const char *fmt, ...) {
71 fputs("usage error: ",stderr);
73 vfprintf(stderr,fmt,al);
80 static void checkstdout(void) {
81 if (ferror(stdout)) { perror("stdout"); exit(8); }
84 static void scandev(const char *basename, int levelsleft) {
85 /* We deliberately ignore most errors */
89 int fnbufalloc, fnbufreq, r, basel, nallocd;
91 struct deventry *ndeventries;
93 if (levelsleft==0) return;
95 dir= opendir(basename);
97 fprintf(stderr, "%s: opendir: %s\n", basename, strerror(errno));
102 basel= strlen(basename);
104 while ((errno=0, de= readdir(dir))) {
105 fnbufreq= basel+1+strlen(de->d_name)+1;
106 if (fnbufalloc<fnbufreq) {
107 fnbufalloc= fnbufreq+10;
108 nfnbuf= realloc(fnbuf,fnbufalloc);
109 if (!nfnbuf) { free(fnbuf); fnbufalloc=0; continue; }
112 sprintf(fnbuf,"%s/%s",basename,de->d_name);
113 r= lstat(fnbuf,&stab);
115 fprintf(stderr, "%s: %s\n", fnbuf, strerror(errno));
118 if (S_ISCHR(stab.st_mode)) {
119 if (de_used >= de_allocd) {
120 nallocd= (de_allocd+10)<<1;
121 ndeventries= realloc(deventries,nallocd*sizeof(*deventries));
122 if (!ndeventries) continue;
124 deventries= ndeventries;
126 deventries[de_used].fn= strdup(fnbuf+5); /* remove /dev */
127 if (!deventries[de_used].fn) continue;
128 deventries[de_used].dev= stab.st_rdev;
130 } else if (S_ISDIR(stab.st_mode) && de->d_name[0] != '.') {
131 scandev(fnbuf,levelsleft-1);
135 fprintf(stderr, "%s: readdir: %s\n", basename, strerror(errno));
140 static int walkdev_cptr(const void *av, const void *bv) {
141 const struct deventry *a= av;
142 const struct deventry *b= bv;
143 return a->dev - b->dev;
146 static void printbanner(void) {
148 fputs("begin date command "
149 "uid gid tty dev FSDX exit",
152 fputs("begin date and time command "
153 "user group tty dev FSDX sigexit",
157 fputs(" user time sys time elap time minflt maxflt",
164 static void printrecord(const struct acct *as, const char *filename) {
165 static int walkeddev;
171 struct deventry *deve, devlookfor;
176 printf("%10lu ",(unsigned long)as->ac_btime);
178 time_t btime=as->ac_btime;
179 tm= localtime(&btime);
180 strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
184 printf("%-16.16s ", as->ac_comm);
186 pw= raw ? 0 : getpwuid(as->ac_uid);
187 if (pw) printf("%-8s ",pw->pw_name);
188 else printf("%-8ld ",(long)as->ac_uid);
190 gr= raw ? 0 : getgrgid(as->ac_gid);
191 if (gr) printf("%-8s ",gr->gr_name);
192 else printf("%-8ld ",(long)as->ac_gid);
195 if (as->ac_tty == (dev_t)-1) {
198 printf("%08lx ",(unsigned long)as->ac_tty);
201 if (as->ac_tty == (dev_t)-1) {
206 qsort(deventries,de_used,sizeof(*deventries),walkdev_cptr);
210 devlookfor.dev= as->ac_tty;
211 deve= bsearch(&devlookfor,deventries,de_used,sizeof(*deventries),walkdev_cptr);
213 printf("%-10s ",deve->fn);
215 printf("%08lx ",(unsigned long)as->ac_tty);
221 for (i=1, fp= "FSDX"; *fp; fp++, i<<=1) {
234 dc= WCOREDUMP(as->ac_exitcode) ? 'd' : 'k';
236 if (WIFEXITED(as->ac_exitcode)) {
237 printf(" %3d",WEXITSTATUS(as->ac_exitcode));
238 } else if (WIFSIGNALED(as->ac_exitcode)) {
241 WTERMSIG(as->ac_exitcode));
243 printf("%04lx",(unsigned long)as->ac_exitcode);
246 if (WIFEXITED(as->ac_exitcode)) {
247 printf(" %6d",WEXITSTATUS(as->ac_exitcode));
248 } else if (WIFSIGNALED(as->ac_exitcode)) {
249 r= WTERMSIG(as->ac_exitcode);
250 if (r>0 && r<=sizeof(sigabbrev)/sizeof(*sigabbrev)) {
260 printf("#%04lx",(unsigned long)as->ac_exitcode);
265 printf(" %10lu %10lu %10lu %8ld %8ld",
266 (unsigned long)as->ac_utime,
267 (unsigned long)as->ac_stime,
268 (unsigned long)as->ac_etime,
269 (unsigned long)as->ac_minflt,
270 (unsigned long)as->ac_majflt);
277 static void processfile(FILE *file, const char *filename) {
283 while ((r= fread(&as,1,sizeof(as),file)) == sizeof(as)) {
284 printrecord(&as,filename);
287 r= fseek(file,0,SEEK_END); if (r) { perror(filename); exit(8); }
288 pos= ftell(file); if (pos==-1) { perror(filename); exit(8); }
289 if (pos % sizeof(as)) {
290 fprintf(stderr, "%s: File size is not an integral number "
291 "of accounting records", filename);
295 if (pos<sizeof(as)) break;
297 r= fseek(file,pos,SEEK_SET); if (r==-1) { perror(filename); exit(8); }
298 r= fread(&as,1,sizeof(as),file); if (r!=sizeof(as)) { perror(filename); exit(8); }
299 printrecord(&as,filename);
302 if (ferror(file) || fclose(file)) { perror(filename); exit(8); }
305 static void processnamedfile(const char *filename) {
308 file= fopen(filename,"rb"); if (!file) { perror(filename); exit(8); }
309 processfile(file,filename);
312 int main(int argc, const char *const *argv) {
313 myopt(&argv,cmdinfos);
314 if (!nobanner) printbanner();
316 processfile(stdin,"<standard input>");
318 processnamedfile("/var/account/pacct");
321 processnamedfile(*argv);
326 if (fflush(stdout)) { perror("flush stdout"); exit(8); }