home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / sys / next / sysadmin / 5085 < prev    next >
Encoding:
Text File  |  1992-09-08  |  4.4 KB  |  126 lines

  1. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!agate!agate!usenet
  2. From: izumi@pinoko.berkeley.edu (Izumi Ohzawa)
  3. Newsgroups: comp.sys.next.sysadmin
  4. Subject: Re: how to purge wtmp file
  5. Date: 9 Sep 1992 00:55:28 GMT
  6. Organization: University of California, Berkeley
  7. Lines: 112
  8. Distribution: world
  9. Message-ID: <18ji20INNli8@agate.berkeley.edu>
  10. References: <1992Sep8.174653.3657@essex.com>
  11. Reply-To: izumi@pinoko.berkeley.edu
  12. NNTP-Posting-Host: moica.berkeley.edu
  13.  
  14. In article <1992Sep8.174653.3657@essex.com> steved@essex.com writes:
  15. >I just noticed my /private/wtmp file today (and some other fairly large files  
  16. >in /private/adm).  These seem to grow indefinitely....
  17. >
  18. >I reviewed the man pages but didn't find an easy way to manage these files, but  
  19. >in poking around in /private/tmp I found the "monthly" and "weekly" shell  
  20. >scripts.  I ran these manually and they seemed to pare down the nasty files (my  
  21. >machine is not networked, so I don't need all the otherwise neat history in  
  22. >these files) Does anyone use these shell scripts, and if so, how do you set  
  23. >them up in crontab?
  24.  
  25. Hopefully, this post is short enough not to elicit any complaint from the net police.
  26.  
  27. It's already setup in /etc/crontab.  But it just wipes out the whole wtmp to
  28. zero length on the first of every months.  The following is a much nicer program
  29. which does the paring by keeping only the last N records in wtmp file.
  30. Modify /usr/adm/monthly to:
  31.  
  32. # Trim wtmp to the last 2000 entries
  33. /usr/local/bin/wtmptrim 2000
  34.  
  35. ----- cut here --------------------------------------------------------------
  36. /* wtrim.c -- /usr/adm/wtmp file trimmer 
  37. ** by Michael Galassi -- nerd@percival.rain.com
  38. ** this is public domain, you can even say you wrote it,
  39. ** I don't care compile with:
  40. **       cc -ansi -O wtrim.c -o wtrim -object -s
  41. */
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <utmp.h>
  46. #include <sys/file.h>
  47. #include <sys/stat.h>
  48.  
  49. #define WTMP "/usr/adm/wtmp"
  50. #define OWTMP "/usr/adm/wtmp.old"
  51. #define DEFTRIM 2000
  52.  
  53. void main(int argc, char *argv[])
  54. {
  55.         int size;
  56.         int fd;
  57.         struct stat st;
  58.         struct utmp *ptr;
  59.  
  60.         if (argc == 2)
  61.                 size = atoi(argv[1]) * sizeof(struct utmp);
  62.         else if (argc == 1)
  63.                 size = DEFTRIM * sizeof(struct utmp);
  64.         else {
  65.                 fprintf(stderr, "usage: %s [count]\n", argv[0]);
  66.                 exit(1);
  67.         }
  68.         if (stat(WTMP, &st) < 0) {
  69.                 fprintf(stderr, "%s: can't stat " WTMP "\n", argv[0]);
  70.                 exit(1);
  71.         }
  72.         if (st.st_size <= size)         /* already OK */
  73.                 exit(0);
  74.         if ((fd = open(WTMP, O_CREAT|O_RDWR, 0644)) < 0) {
  75.                 fprintf(stderr, "%s: can't open " WTMP "\n", argv[0]);
  76.                 exit(1);
  77.         }
  78.         if (lseek(fd, st.st_size - size, L_SET) != st.st_size - size) {
  79.                 fprintf(stderr, "%s: can't lseek in " WTMP "\n", argv[0]);
  80.                 close(fd);
  81.                 exit(1);
  82.         }
  83.         if ((ptr = (struct utmp *) malloc(size)) == (void *)NULL) {
  84.                 fprintf(stderr, "%s: can't malloc %d bytes\n", argv[0], size);
  85.                 close(fd);
  86.                 exit(1);
  87.         }
  88.         if (read(fd, ptr, size) != size) {
  89.                 fprintf(stderr, "%s: can't malloc %d bytes\n", argv[0], size);
  90.                 free(ptr);
  91.                 close(fd);
  92.                 exit(1);
  93.         }
  94.         close(fd);
  95.         unlink(OWTMP);
  96.         if (rename(WTMP, OWTMP) < 0) {
  97.                 fprintf(stderr, "%s: can't rename " WTMP " to " OWTMP "\n", argv[0]);
  98.                 free(ptr);
  99.                 exit(1);
  100.         }
  101.         if ((fd = open(WTMP, O_CREAT|O_WRONLY, 0644)) < 0) {
  102.                 fprintf(stderr, "%s: can't open new " WTMP "\n", argv[0]);
  103.                 free(ptr);
  104.                 rename(OWTMP, WTMP);
  105.                 exit(1);
  106.         }
  107.         if (write(fd, ptr, size) != size) {
  108.                 fprintf(stderr, "%s: can't write new " WTMP "\n", argv[0]);
  109.                 free(ptr);
  110.                 close(fd);
  111.                 unlink(WTMP);
  112.                 rename(OWTMP, WTMP);
  113.                 exit(1);
  114.         }
  115.         close(fd);
  116.         free(ptr);
  117.         exit(0);
  118. }
  119. ---- cut here -----------------------
  120.  
  121. --
  122. Izumi Ohzawa             [ $BBg_78^=;(J ]
  123. USMail: University of California, 360 Minor Hall, Berkeley, CA 94720
  124. Telephone: (510) 642-6440     Fax:  (510) 642-3323
  125. Internet: izumi@pinoko.berkeley.edu (NeXT Mail OK)
  126.