[TUHS] Binary log files

Steffen Nurpmeso steffen at sdaoden.eu
Mon Jul 5 23:41:59 AEST 2021


John Cowan wrote in
 <CAD2gp_SXHQSauT_VibXPvP6PWrFULiMFYkvfs5=YxjHYbHGPwg at mail.gmail.com>:
 |As long ago as the 7th Edition, several binary log files were maintained:
 |the file generated by acct(2) (one record per process) and the utmp and
 |wtmp files (one record per login).  Both of these are defined by structs in
 |.h files, so they are definitely not portable (int sizes, endianism, etc.)

And how did you handle it?
On a very current GNU/Linux system these files grow indefinetely,
and sometimes you find several megabytes that track years of data,
and yourself writing (nonetheless quickshot) truncation code like

  #?0|kent:~# less bin/truncate-wutmp.sh
  #!/bin/sh -
  #@ /root/bin/truncate-wutmp.sh

  trap 'rm -f /tmp/.doit-${$}.*' EXIT

  cat >/tmp/.doit-${$}.c <<'_EOT'
  #include <utmp.h>
  #include <stdio.h>
  int main(){
      printf("%lu\n", sizeof(struct utmp));
      return 0;
  }
  _EOT

  cc -o /tmp/.doit-${$}.exe /tmp/.doit-${$}.c || exit 1

  i=$(/tmp/.doit-${$}.exe)
  echo "struct utmp is ${i} bytes"

  cd /var/log

  s=$(stat -c '%s' wtmp)
  [ ${?} -eq 0 ] || exit 2
  echo "wtmp size is ${s} bytes"

  ix=$((s / i))
  echo "... that makes ${ix} utmp entries"

  [ ${ix} -gt 42 ] || exit 3
  ix=$((ix - 42))

  dd if=wtmp of=wtmp.new bs=${i} skip=${ix} || exit 4
  mv wmtp.new wtmp
  chmod 0644 wtmp

  # s-sh-mode

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)


More information about the TUHS mailing list