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.
36 static int forwards, nobanner, usestdin, raw, usages;
38 static int de_used, de_allocd;
39 static struct deventry {
44 static const struct cmdinfo cmdinfos[]= {
45 { "--forwards", 'f', 0, &forwards, 0, 0, 1, 0, 0 },
46 { "--no-banner", 'q', 0, &nobanner, 0, 0, 1, 0, 0 },
47 { "--stdin", 'p', 0, &usestdin, 0, 0, 1, 0, 0 },
48 { "--raw", 'r', 0, &raw, 0, 0, 1, 0, 0 },
49 { "--resource", 'u', 0, &usages, 0, 0, 1, 0, 0 },
53 static const char *sigabbrev[]= {
54 "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE",
55 "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT",
56 "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU",
57 "XFSZ", "VTALRM", "PROF", "WINCH", "IO"
60 static void usage(FILE *file) {
61 fputs("usage: acctdump [<options>] [<file> ...]\n"
62 "options: -f|--forwards -q|--no-banner -p|--stdin -r|--raw -u|--resource\n",
64 if (ferror(file)) { perror("print usage"); exit(8); }
67 void badusage(const char *fmt, ...) {
70 fputs("usage error: ",stderr);
72 vfprintf(stderr,fmt,al);
79 static void checkstdout(void) {
80 if (ferror(stdout)) { perror("stdout"); exit(8); }
83 static void scandev(const char *basename, int levelsleft) {
84 /* We deliberately ignore most errors */
88 int fnbufalloc, fnbufreq, r, basel, nallocd;
90 struct deventry *ndeventries;
92 if (levelsleft==0) return;
94 dir= opendir(basename); if (!dir) return;
97 basel= strlen(basename);
99 while ((de= readdir(dir))) {
100 fnbufreq= basel+1+strlen(de->d_name)+1;
101 if (fnbufalloc<fnbufreq) {
102 fnbufalloc= fnbufreq+10;
103 nfnbuf= realloc(fnbuf,fnbufalloc);
104 if (!nfnbuf) { free(fnbuf); fnbufalloc=0; continue; }
107 sprintf(fnbuf,"%s/%s",basename,de->d_name);
108 r= lstat(fnbuf,&stab);
109 if (S_ISCHR(stab.st_mode)) {
110 if (de_used >= de_allocd) {
111 nallocd= (de_allocd+10)<<1;
112 ndeventries= realloc(deventries,nallocd*sizeof(*deventries));
113 if (!ndeventries) continue;
115 deventries= ndeventries;
117 deventries[de_used].fn= strdup(fnbuf+5); /* remove /dev */
118 if (!deventries[de_used].fn) continue;
119 deventries[de_used].dev= stab.st_rdev;
121 } else if (S_ISDIR(stab.st_mode) && de->d_name[0] != '.') {
122 scandev(fnbuf,levelsleft-1);
129 static int walkdev_cptr(const void *av, const void *bv) {
130 const struct deventry *a= av;
131 const struct deventry *b= bv;
132 return a->dev - b->dev;
135 static void printbanner(void) {
137 fputs("begin date command "
138 "uid gid tty dev FSDX exit",
141 fputs("begin date and time command "
142 "user group tty dev FSDX sigexit",
146 fputs(" user time sys time elap time minflt maxflt",
153 static void printrecord(const struct acct *as, const char *filename) {
154 static int walkeddev;
160 struct deventry *deve, devlookfor;
165 printf("%10lu ",(unsigned long)as->ac_btime);
167 tm= localtime(&as->ac_btime);
168 strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
172 printf("%-16.16s ", as->ac_comm);
174 pw= raw ? 0 : getpwuid(as->ac_uid);
175 if (pw) printf("%-8s ",pw->pw_name);
176 else printf("%-8ld ",(long)as->ac_uid);
178 gr= raw ? 0 : getgrgid(as->ac_gid);
179 if (gr) printf("%-8s ",gr->gr_name);
180 else printf("%-8ld ",(long)as->ac_gid);
183 if (as->ac_tty == (dev_t)-1) {
186 printf("%08lx ",(unsigned long)as->ac_tty);
189 if (as->ac_tty == (dev_t)-1) {
194 qsort(deventries,de_used,sizeof(*deventries),walkdev_cptr);
198 devlookfor.dev= as->ac_tty;
199 deve= bsearch(&devlookfor,deventries,de_used,sizeof(*deventries),walkdev_cptr);
201 printf("%-10s ",deve->fn);
203 printf("%08lx ",(unsigned long)as->ac_tty);
209 for (i=1, fp= "FSDX"; *fp; fp++, i<<=1) {
222 dc= WCOREDUMP(as->ac_exitcode) ? 'd' : 'k';
224 if (WIFEXITED(as->ac_exitcode)) {
225 printf(" %3d",WEXITSTATUS(as->ac_exitcode));
226 } else if (WIFSIGNALED(as->ac_exitcode)) {
229 WTERMSIG(as->ac_exitcode));
231 printf("%04lx",(unsigned long)as->ac_exitcode);
234 if (WIFEXITED(as->ac_exitcode)) {
235 printf(" %6d",WEXITSTATUS(as->ac_exitcode));
236 } else if (WIFSIGNALED(as->ac_exitcode)) {
237 r= WTERMSIG(as->ac_exitcode);
238 if (r>0 && r<=sizeof(sigabbrev)/sizeof(*sigabbrev)) {
248 printf("#%04lx",(unsigned long)as->ac_exitcode);
253 printf(" %10lu %10lu %10lu %8ld %8ld",
254 (unsigned long)as->ac_utime,
255 (unsigned long)as->ac_stime,
256 (unsigned long)as->ac_etime,
257 (unsigned long)as->ac_minflt,
258 (unsigned long)as->ac_majflt);
265 static void processfile(FILE *file, const char *filename) {
271 while ((r= fread(&as,1,sizeof(as),file)) == sizeof(as)) {
272 printrecord(&as,filename);
275 r= fseek(file,0,SEEK_END); if (r) { perror(filename); exit(8); }
276 r= fgetpos(file,&pos); if (r) { perror(filename); exit(8); }
278 if (pos<sizeof(as)) break;
280 r= fsetpos(file,&pos); if (r) { perror(filename); exit(8); }
281 r= fread(&as,1,sizeof(as),file); if (r!=sizeof(as)) { perror(filename); exit(8); }
282 printrecord(&as,filename);
285 if (ferror(file) || fclose(file)) { perror(filename); exit(8); }
288 static void processnamedfile(const char *filename) {
291 file= fopen(filename,"rb"); if (!file) { perror(filename); exit(8); }
292 processfile(file,filename);
295 int main(int argc, const char *const *argv) {
296 myopt(&argv,cmdinfos);
297 if (!nobanner) printbanner();
299 processfile(stdin,"<standard input>");
301 processnamedfile("/var/account/pacct");
304 processnamedfile(*argv);
309 if (fflush(stdout)) { perror("flush stdout"); exit(8); }