Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cis.ohio-state.edu!ucbvax!agate!usenet From: izumi@pinoko.berkeley.edu (Izumi Ohzawa) Newsgroups: comp.sys.next Subject: Trimmer for /usr/adm/wtmp Message-ID: <1991Jun19.022344.26696@agate.berkeley.edu> Date: 19 Jun 91 02:23:44 GMT Sender: usenet@agate.berkeley.edu (USENET Administrator) Reply-To: izumi@pinoko.berkeley.edu Organization: University of California, Berkeley Lines: 118 Here's a little more reasonable file trimmer for /usr/adm/wtmp file. With the default monthly script (/usr/adm/monthly), the wtmp file is wiped clean on the first of each month. It also wipes out /usr/adm/lastlog, which shouldn't be necessary (lastlog doesn't grow). -------- cut here ----- /* Program: wtmptrim -- trims the size of /usr/adm/wtmp file by keeping the last N entries. * Usage: wtmptrim [numentries] where numentries defaults to 2000 entries. * Valid for 1.0, 2.0, 2.1 * Compile: cc -O -o wtmptrim wtmptrim.c strip wtmptrim * Install wtmptrim in /usr/local/bin. * Modify /usr/adm/monthly as below: ----------------------------------------------------------------- *** monthly.old Tue Jun 18 18:46:43 1991 --- monthly Tue Jun 18 18:50:05 1991 *************** *** 5,15 **** # commands in here, like pruning of log files # ! # Trim the lastlog ! cp -p /usr/adm/lastlog /usr/adm/lastlog.old ! cat /dev/null > /usr/adm/lastlog ! # Trim wtmp ! cp -p /usr/adm/wtmp /usr/adm/wtmp.old ! cat /dev/null > /usr/adm/wtmp ! --- 5,13 ---- # commands in here, like pruning of log files # ! # NO NEED to trim the lastlog ! # cp -p /usr/adm/lastlog /usr/adm/lastlog.old ! # cat /dev/null > /usr/adm/lastlog ! # Trim wtmp to the last 2000 entries ! /usr/local/bin/wtmptrim 2000 ----------------------------------------------------------------- */ #include #include #include #include #include #define DEF_ENTRIES 2000 #define MAXENTRIES 100000 #define MINENTRIES 100 #define WTMPFILE "/usr/adm/wtmp" #define TEMPFILE "/usr/adm/wtmp.new" #define WTMPOLD "/usr/adm/wtmp.old" void main(int argc, char *argv[]) { int i; int numtrim = DEF_ENTRIES; int numcurrent, nemptyto; FILE *fin_wtmp, *fout_wtmp; struct stat filestat; struct utmp s_utmp; if(argc > 1) { /* if argument is given, interpret argv[1] as numentries */ i = atoi(argv[1]); if(i > MINENTRIES && i <= MAXENTRIES ) numtrim = i; /* defaults to DEF_ENTRIES */ } if(stat(WTMPFILE, &filestat)) { printf("Can't get stat for file: %s\n", WTMPFILE); exit(1); } else { numcurrent = filestat.st_size / sizeof(s_utmp); if(numcurrent <= numtrim) exit(0); /* Small, no need to trim file */ nemptyto = numcurrent - numtrim; /* Empty this many records */ if ((fin_wtmp = fopen(WTMPFILE, "r")) != NULL && (fout_wtmp = fopen(TEMPFILE, "w")) != NULL) { for(i=0; i