aboutsummaryrefslogtreecommitdiff
path: root/riemann.fmi.uni-sofia.bg/programs/nlogin-0.3-pre/utmp_do.c
blob: c820b426f5b2cecaa2fcf6bdc06107cc6371f520 (plain)
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
52
53
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/stat.h>
#include "utmp_defs.h"
#include "str_defs.h"

#define SD(A,B) !str_diffn(A, B, sizeof(A))
#define SC(A,B) str_copynz(A, B, sizeof(A))

void utmp_do(char *username, char *line, int pid) /*EXTRACT_INCL*/{
  off_t pos=0;
  struct utmp u;
  char *p;
  int fd=open(_PATH_UTMP,O_RDWR);

  while (utmp_io(fd, &u,F_RDLCK)) {
    if (u.ut_pid == pid || SD(u.ut_line, line)) goto foundone;
    pos += UTMP_SIZE;
  }
  
  byte_zero(&u,sizeof(u));
  pos = lseek(fd,0,SEEK_END);
  if (pos<0) pos =0;
  pos = pos - (pos % (UTMP_SIZE));
  
 foundone:
  if (u.ut_id[0]==0) {
    p=line;
    while (str_len(p) > sizeof u.ut_id) p++;
    SC(u.ut_id, p);
  }
  SC(u.ut_line, line);

  SC(u.ut_user,username);
  u.ut_type=USER_PROCESS;
  u.ut_pid=pid;

  byte_zero(u.ut_host,sizeof u.ut_host);
  u.ut_tv.tv_sec = time(0);

  if (fd>=0) {
    if (lseek(fd,pos,SEEK_SET) == pos)
      utmp_io(fd,&u,F_WRLCK);
    close(fd);
  }

  fd=open(_PATH_WTMP, O_WRONLY|O_APPEND);
  if (fd>=0) {
    write(fd, &u, UTMP_SIZE);
    close(fd);
  }
}