File: cleanutmp.c

package info (click to toggle)
ngetty 1.0-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 304 kB
  • ctags: 207
  • sloc: ansic: 1,503; makefile: 236; sh: 109; asm: 100
file content (51 lines) | stat: -rw-r--r-- 1,059 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* cleanutmp.c
   chown root.utmp cleanutmp
   chmod 2711 cleanutmp
 */

#include <utmp.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "lib.h"

void ops(int n, const char *s0, const char *s1) {
  write(2,s0,str_len(s0));
  write(2,s1,str_len(s1));
  write(2,"\n",1);
  _exit(n);
}

int main() {
  int fd,wfd;
  struct utmp ut;
  off_t pos=0;

  if ((fd = open(_PATH_UTMP,O_RDWR)) <0 &&
      (fd = open(_PATH_UTMP,O_RDONLY)) <0)
    ops(1,"error opening: ", _PATH_UTMP);

  while (utmp_io(fd,&ut,F_RDLCK)) {
    pos += UTMP_SIZE;
    if (ut.ut_type != USER_PROCESS) continue;
    if (kill(ut.ut_pid, 0) && errno == ESRCH) {
      ut.ut_type = DEAD_PROCESS;
      ut.ut_tv.tv_sec = time(0);

      pos -= UTMP_SIZE;
      if (lseek(fd, pos, SEEK_SET) != pos || 
	  !utmp_io(fd,&ut,F_WRLCK))
	ops(2,"error writing to: ", _PATH_UTMP);
      pos += UTMP_SIZE;
      
      wfd=open(_PATH_WTMP, O_WRONLY|O_APPEND);
      if (wfd>=0) {
	write(fd, &ut, UTMP_SIZE);
	close(wfd);
      }
    }
  }
  close(fd);
  return 0;
}