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;
37 /* Sadly this thing is not very portable */
39 #if defined(__linux__)
41 #include <sys/types.h>
44 typedef struct acct_v3 struct_acct;
45 #define HAVE_AC_EXITCODE
47 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
49 #include <sys/param.h>
50 #include <sys/types.h>
53 typedef struct acctv2 struct_acct;
57 #error Do not know what struct_acct to use on this platform
64 static int forwards, nobanner, usestdin, raw, usages;
66 static int de_used, de_allocd;
67 static struct deventry {
72 static const struct cmdinfo cmdinfos[]= {
73 { "--forwards", 'f', 0, &forwards, 0, 0, 1, 0, 0 },
74 { "--no-banner", 'q', 0, &nobanner, 0, 0, 1, 0, 0 },
75 { "--stdin", 'p', 0, &usestdin, 0, 0, 1, 0, 0 },
76 { "--raw", 'r', 0, &raw, 0, 0, 1, 0, 0 },
77 { "--resource", 'u', 0, &usages, 0, 0, 1, 0, 0 },
81 #ifdef HAVE_AC_EXITCODE
82 static const char *sigabbrev[]= {
83 "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE",
84 "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT",
85 "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU",
86 "XFSZ", "VTALRM", "PROF", "WINCH", "IO"
90 void usagemessage(void) {
91 fputs("usage: acctdump [<options>] [<file> ...]\n"
92 "options: -f|--forwards -q|--no-banner -p|--stdin -r|--raw -u|--resource\n",
94 if (ferror(stderr)) { perror("print usage"); exit(8); }
97 static void checkstdout(void) {
98 if (ferror(stdout)) { perror("stdout"); exit(8); }
101 static void scandev(const char *basename, int levelsleft) {
102 /* We deliberately ignore most errors */
106 int fnbufalloc, fnbufreq, r, basel, nallocd;
107 char *fnbuf, *nfnbuf;
108 struct deventry *ndeventries;
110 if (levelsleft==0) return;
112 dir= opendir(basename);
114 fprintf(stderr, "%s: opendir: %s\n", basename, strerror(errno));
119 basel= strlen(basename);
121 while ((errno=0, de= readdir(dir))) {
122 fnbufreq= basel+1+strlen(de->d_name)+1;
123 if (fnbufalloc<fnbufreq) {
124 fnbufalloc= fnbufreq+10;
125 nfnbuf= realloc(fnbuf,fnbufalloc);
126 if (!nfnbuf) { free(fnbuf); fnbufalloc=0; continue; }
129 sprintf(fnbuf,"%s/%s",basename,de->d_name);
130 r= lstat(fnbuf,&stab);
132 fprintf(stderr, "%s: %s\n", fnbuf, strerror(errno));
135 if (S_ISCHR(stab.st_mode)) {
136 if (de_used >= de_allocd) {
137 nallocd= (de_allocd+10)<<1;
138 ndeventries= realloc(deventries,nallocd*sizeof(*deventries));
139 if (!ndeventries) continue;
141 deventries= ndeventries;
143 deventries[de_used].fn= strdup(fnbuf+5); /* remove /dev */
144 if (!deventries[de_used].fn) continue;
145 deventries[de_used].dev= stab.st_rdev;
147 } else if (S_ISDIR(stab.st_mode) && de->d_name[0] != '.') {
148 scandev(fnbuf,levelsleft-1);
152 fprintf(stderr, "%s: readdir: %s\n", basename, strerror(errno));
157 static int walkdev_cptr(const void *av, const void *bv) {
158 const struct deventry *a= av;
159 const struct deventry *b= bv;
160 return a->dev - b->dev;
163 static void printbanner(void) {
165 fputs("begin date command "
166 "uid gid tty dev FSDX "
167 #ifdef HAVE_AC_EXITCODE
172 fputs("begin date and time command "
173 "user group tty dev FSDX "
174 #ifdef HAVE_AC_EXITCODE
180 fputs(" user time sys time elap time minflt maxflt",
187 static void printrecord(const struct_acct *as, const char *filename) {
188 static int walkeddev;
194 struct deventry *deve, devlookfor;
200 printf("%10lu ",(unsigned long)as->ac_btime);
203 tm= localtime(&btime);
204 strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
208 printf("%-16.16s ", as->ac_comm);
210 pw= raw ? 0 : getpwuid(as->ac_uid);
211 if (pw) printf("%-8s ",pw->pw_name);
212 else printf("%-8ld ",(long)as->ac_uid);
214 gr= raw ? 0 : getgrgid(as->ac_gid);
215 if (gr) printf("%-8s ",gr->gr_name);
216 else printf("%-8ld ",(long)as->ac_gid);
219 if (!(as->ac_tty + 1) /* check for -1 without knowing type */) {
222 printf("%08lx ",(unsigned long)as->ac_tty);
225 if (!(as->ac_tty + 1)) {
230 qsort(deventries,de_used,sizeof(*deventries),walkdev_cptr);
234 devlookfor.dev= as->ac_tty;
235 deve= bsearch(&devlookfor,deventries,de_used,sizeof(*deventries),walkdev_cptr);
237 printf("%-10s ",deve->fn);
239 printf("%08lx ",(unsigned long)as->ac_tty);
245 for (i=1, fp= "FS4DX"; *fp; fp++, i<<=1) {
249 } else if (!isdigit(*fp)) {
258 #ifdef HAVE_AC_EXITCODE
260 dc= WCOREDUMP(as->ac_exitcode) ? 'd' : 'k';
262 if (WIFEXITED(as->ac_exitcode)) {
263 printf(" %3d",WEXITSTATUS(as->ac_exitcode));
264 } else if (WIFSIGNALED(as->ac_exitcode)) {
267 WTERMSIG(as->ac_exitcode));
269 printf("%04lx",(unsigned long)as->ac_exitcode);
272 if (WIFEXITED(as->ac_exitcode)) {
273 printf(" %6d",WEXITSTATUS(as->ac_exitcode));
274 } else if (WIFSIGNALED(as->ac_exitcode)) {
275 r= WTERMSIG(as->ac_exitcode);
276 if (r>0 && r<=sizeof(sigabbrev)/sizeof(*sigabbrev)) {
286 printf("#%04lx",(unsigned long)as->ac_exitcode);
289 #endif /*HAVE_AC_EXITCODE*/
292 printf(" %10lu %10lu %10lu %8ld %8ld",
293 (unsigned long)as->ac_utime,
294 (unsigned long)as->ac_stime,
295 (unsigned long)as->ac_etime,
296 (unsigned long)as->ac_minflt,
297 (unsigned long)as->ac_majflt);
304 static void processfile(FILE *file, const char *filename) {
310 while ((r= fread(&as,1,sizeof(as),file)) == sizeof(as)) {
311 printrecord(&as,filename);
314 r= fseek(file,0,SEEK_END); if (r) { perror(filename); exit(8); }
315 pos= ftell(file); if (pos==-1) { perror(filename); exit(8); }
316 if (pos % sizeof(as)) {
317 fprintf(stderr, "%s: File size is not an integral number "
318 "of accounting records\n", filename);
322 if (pos<sizeof(as)) break;
324 r= fseek(file,pos,SEEK_SET); if (r==-1) { perror(filename); exit(8); }
325 r= fread(&as,1,sizeof(as),file); if (r!=sizeof(as)) { perror(filename); exit(8); }
326 printrecord(&as,filename);
329 if (ferror(file) || fclose(file)) { perror(filename); exit(8); }
332 static void processnamedfile(const char *filename) {
335 file= fopen(filename,"rb"); if (!file) { perror(filename); exit(8); }
336 processfile(file,filename);
339 int main(int argc, const char *const *argv) {
340 myopt(&argv,cmdinfos);
341 if (!nobanner) printbanner();
343 processfile(stdin,"<standard input>");
345 processnamedfile("/var/log/account/pacct");
348 processnamedfile(*argv);
353 if (fflush(stdout)) { perror("flush stdout"); exit(8); }