File: utmp_do.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 (120 lines) | stat: -rw-r--r-- 2,442 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#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

#if !defined(HAVE_C_UTMP) && !defined(HAVE_C_UTMPX)

/* line tty5 */
void utmp_do(int login, char *line) /*EXTRACT_INCL*/{ 
  struct utmp u;
  int fd=open(_PATH_UTMP, O_RDWR), foundone=0, do_wtmp=1;
  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(_PATH_WTMP, O_WRONLY|O_APPEND)) >= 0) {
    write(fd, &u, UTMP_SIZE);
    close(fd);
  }
}

#else
#ifdef HAVE_C_UTMP

#define Wtmp_File	_PATH_WTMP
#define utmp_type	utmp
#define f_setutent	setutent
#define f_endutent	endutent
#define f_getutent	getutent
#define f_pututline	pututline
#define f_updwtmp	updwtmp

#else
#define __USE_GNU
#include <utmpx.h>

#define Wtmp_File	_PATH_WTMPX
#define utmp_type	utmpx
#define f_setutent	setutxent
#define f_endutent	endutxent
#define f_getutent	getutxent
#define f_pututline	pututxline
#define f_updwtmp	updwtmpx
#endif

void utmp_do(int login, char *line) { 
  struct utmp_type u, *ut;
  int 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 ((ut = 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);
}
#endif