File: utmp_do.c

package info (click to toggle)
ngetty 1.1-3.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 392 kB
  • sloc: ansic: 1,588; makefile: 206; sh: 193; asm: 100
file content (98 lines) | stat: -rw-r--r-- 1,928 bytes parent folder | download | duplicates (3)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "lib.h"

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

#if 0
/*EXTRACT_START*/
SC(u.ut_line, line);
if (u.ut_id[0]==0) SC(u.ut_id, p);

if (login) {
  u.ut_type=LOGIN_PROCESS;
  u.ut_pid=getpid();
  p="LOGIN";
} else {
  if (u.ut_type == DEAD_PROCESS) do_wtmp =0;
  u.ut_type=DEAD_PROCESS;
  p="";
}

SC(u.ut_user,p);
SC(u.ut_host,"");
u.ut_tv.tv_sec = time(0);
/*EXTRACT_END*/
#endif

#ifdef USE_LIBC_UTMP
void utmp_do(int login, char *line) { 
  struct utmp_type u, *ut;
  char foundone=0, do_wtmp=1;
  char *p;

  p=line;
  while (str_len(p) > sizeof u.ut_id) p++;

  str_copynz((char *)&u,"",sizeof(u));
  f_setutent();
  while (f_getutent())
    if ((INIT_PROCESS <= ut->ut_type && 
	 ut->ut_type <= DEAD_PROCESS &&
	 SD(ut->ut_id, p)) || SD(ut->ut_line, line))
      { u = *ut; foundone=1; break; }

#include "utmp_defs.h"

  if (foundone || login) {
    f_setutent(); /* comment this ? */
    f_pututline(&u);
  }
  f_endutent();

  if (do_wtmp)
    f_updwtmp(Wtmp_File, &u);
}

#else
#include <unistd.h>
#include <fcntl.h>

/* line tty5 */
void utmp_do(int login, char *line) /*EXTRACT_INCL*/{ 
  struct utmp_type u;
  char foundone=0, do_wtmp=1;
  int fd=open(Utmp_File, O_RDWR);
  off_t pos=0;
  char *p;

  p=line;
  while (str_len(p) > sizeof u.ut_id) p++;

  while (utmp_io(fd, &u, F_RDLCK)) {
    if ((INIT_PROCESS <= u.ut_type && 
	 u.ut_type <= DEAD_PROCESS &&
	 SD(u.ut_id, p)) || SD(u.ut_line, line)) { foundone=1; break; }
    pos += UTMP_SIZE;
  }

  if (!foundone) {
    str_copynz((char *)&u,"",sizeof(u));
    pos = lseek(fd,0,SEEK_END);
    if (pos<0) pos =0;
    pos = pos - (pos % (UTMP_SIZE));
  }

#include "utmp_defs.h"

  if (foundone || login)
    if (lseek(fd,pos,SEEK_SET) == pos)
      utmp_io(fd,&u,F_WRLCK);
  close(fd);

  if (do_wtmp) {
    fd=open(Wtmp_File, O_WRONLY|O_APPEND);
    write(fd, &u, UTMP_SIZE);
    close(fd);
  }
}
#endif