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
48 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
50 #include <sys/param.h>
51 #include <sys/types.h>
54 typedef struct acctv2 struct_acct;
58 #error Do not know what struct_acct to use on this platform
65 static int forwards, nobanner, usestdin, raw, usages;
67 static int de_used, de_allocd;
68 static struct deventry {
73 static const struct cmdinfo cmdinfos[]= {
74 { "--forwards", 'f', 0, &forwards, 0, 0, 1, 0, 0 },
75 { "--no-banner", 'q', 0, &nobanner, 0, 0, 1, 0, 0 },
76 { "--stdin", 'p', 0, &usestdin, 0, 0, 1, 0, 0 },
77 { "--raw", 'r', 0, &raw, 0, 0, 1, 0, 0 },
78 { "--resource", 'u', 0, &usages, 0, 0, 1, 0, 0 },
82 #ifdef HAVE_AC_EXITCODE
83 static const char *sigabbrev[]= {
84 "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "BUS", "FPE",
85 "KILL", "USR1", "SEGV", "USR2", "PIPE", "ALRM", "TERM", "STKFLT",
86 "CHLD", "CONT", "STOP", "TSTP", "TTIN", "TTOU", "URG", "XCPU",
87 "XFSZ", "VTALRM", "PROF", "WINCH", "IO"
91 void usagemessage(void) {
92 fputs("usage: acctdump [<options>] [<file> ...]\n"
93 "options: -f|--forwards -q|--no-banner -p|--stdin -r|--raw -u|--resource\n",
95 if (ferror(stderr)) { perror("print usage"); exit(8); }
98 static void checkstdout(void) {
99 if (ferror(stdout)) { perror("stdout"); exit(8); }
102 static void scandev(const char *basename, int levelsleft) {
103 /* We deliberately ignore most errors */
107 int fnbufalloc, fnbufreq, r, basel, nallocd;
108 char *fnbuf, *nfnbuf;
109 struct deventry *ndeventries;
111 if (levelsleft==0) return;
113 dir= opendir(basename);
115 fprintf(stderr, "%s: opendir: %s\n", basename, strerror(errno));
120 basel= strlen(basename);
122 while ((errno=0, de= readdir(dir))) {
123 fnbufreq= basel+1+strlen(de->d_name)+1;
124 if (fnbufalloc<fnbufreq) {
125 fnbufalloc= fnbufreq+10;
126 nfnbuf= realloc(fnbuf,fnbufalloc);
127 if (!nfnbuf) { free(fnbuf); fnbufalloc=0; continue; }
130 sprintf(fnbuf,"%s/%s",basename,de->d_name);
131 r= lstat(fnbuf,&stab);
133 fprintf(stderr, "%s: %s\n", fnbuf, strerror(errno));
136 if (S_ISCHR(stab.st_mode)) {
137 if (de_used >= de_allocd) {
138 nallocd= (de_allocd+10)<<1;
139 ndeventries= realloc(deventries,nallocd*sizeof(*deventries));
140 if (!ndeventries) continue;
142 deventries= ndeventries;
144 deventries[de_used].fn= strdup(fnbuf+5); /* remove /dev */
145 if (!deventries[de_used].fn) continue;
146 deventries[de_used].dev= stab.st_rdev;
148 } else if (S_ISDIR(stab.st_mode) && de->d_name[0] != '.') {
149 scandev(fnbuf,levelsleft-1);
153 fprintf(stderr, "%s: readdir: %s\n", basename, strerror(errno));
158 static int walkdev_cptr(const void *av, const void *bv) {
159 const struct deventry *a= av;
160 const struct deventry *b= bv;
161 return a->dev - b->dev;
164 static void printbanner(void) {
166 fputs("begin date command "
167 "uid gid tty dev FSDX "
168 #ifdef HAVE_AC_EXITCODE
173 fputs("begin date and time command "
174 "user group tty dev FSDX "
175 #ifdef HAVE_AC_EXITCODE
181 fputs(" user time sys time elap time "
191 static void printrecord(const struct_acct *as, const char *filename) {
192 static int walkeddev;
198 struct deventry *deve, devlookfor;
204 printf("%10lu ",(unsigned long)as->ac_btime);
207 tm= localtime(&btime);
208 strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tm); buf[sizeof(buf)-1]= 0;
212 printf("%-16.16s ", as->ac_comm);
214 pw= raw ? 0 : getpwuid(as->ac_uid);
215 if (pw) printf("%-8s ",pw->pw_name);
216 else printf("%-8ld ",(long)as->ac_uid);
218 gr= raw ? 0 : getgrgid(as->ac_gid);
219 if (gr) printf("%-8s ",gr->gr_name);
220 else printf("%-8ld ",(long)as->ac_gid);
223 if (!(as->ac_tty + 1) /* check for -1 without knowing type */) {
226 printf("%08lx ",(unsigned long)as->ac_tty);
229 if (!(as->ac_tty + 1)) {
234 qsort(deventries,de_used,sizeof(*deventries),walkdev_cptr);
238 devlookfor.dev= as->ac_tty;
239 deve= bsearch(&devlookfor,deventries,de_used,sizeof(*deventries),walkdev_cptr);
241 printf("%-10s ",deve->fn);
243 printf("%08lx ",(unsigned long)as->ac_tty);
249 for (i=1, fp= "FS4DX"; *fp; fp++, i<<=1) {
253 } else if (!isdigit(*fp)) {
262 #ifdef HAVE_AC_EXITCODE
264 dc= WCOREDUMP(as->ac_exitcode) ? 'd' : 'k';
266 if (WIFEXITED(as->ac_exitcode)) {
267 printf(" %3d",WEXITSTATUS(as->ac_exitcode));
268 } else if (WIFSIGNALED(as->ac_exitcode)) {
271 WTERMSIG(as->ac_exitcode));
273 printf("%04lx",(unsigned long)as->ac_exitcode);
276 if (WIFEXITED(as->ac_exitcode)) {
277 printf(" %6d",WEXITSTATUS(as->ac_exitcode));
278 } else if (WIFSIGNALED(as->ac_exitcode)) {
279 r= WTERMSIG(as->ac_exitcode);
280 if (r>0 && r<=sizeof(sigabbrev)/sizeof(*sigabbrev)) {
290 printf("#%04lx",(unsigned long)as->ac_exitcode);
293 #endif /*HAVE_AC_EXITCODE*/
296 printf(" %10lu %10lu %10lu",
297 (unsigned long)as->ac_utime,
298 (unsigned long)as->ac_stime,
299 (unsigned long)as->ac_etime);
302 (unsigned long)as->ac_minflt,
303 (unsigned long)as->ac_majflt);
311 static void processfile(FILE *file, const char *filename) {
317 while ((r= fread(&as,1,sizeof(as),file)) == sizeof(as)) {
318 printrecord(&as,filename);
321 r= fseek(file,0,SEEK_END); if (r) { perror(filename); exit(8); }
322 pos= ftell(file); if (pos==-1) { perror(filename); exit(8); }
323 if (pos % sizeof(as)) {
324 fprintf(stderr, "%s: File size is not an integral number "
325 "of accounting records\n", filename);
329 if (pos<sizeof(as)) break;
331 r= fseek(file,pos,SEEK_SET); if (r==-1) { perror(filename); exit(8); }
332 r= fread(&as,1,sizeof(as),file); if (r!=sizeof(as)) { perror(filename); exit(8); }
333 printrecord(&as,filename);
336 if (ferror(file) || fclose(file)) { perror(filename); exit(8); }
339 static void processnamedfile(const char *filename) {
342 file= fopen(filename,"rb"); if (!file) { perror(filename); exit(8); }
343 processfile(file,filename);
346 int main(int argc, const char *const *argv) {
347 myopt(&argv,cmdinfos);
348 if (!nobanner) printbanner();
350 processfile(stdin,"<standard input>");
352 processnamedfile("/var/log/account/pacct");
355 processnamedfile(*argv);
360 if (fflush(stdout)) { perror("flush stdout"); exit(8); }