File: logwtmp.c

package info (click to toggle)
inetutils 2%3A1.6-3.1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 8,972 kB
  • ctags: 7,360
  • sloc: ansic: 58,631; sh: 5,284; yacc: 1,151; makefile: 498; perl: 47
file content (132 lines) | stat: -rw-r--r-- 3,034 bytes parent folder | download
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
121
122
123
124
125
126
127
128
129
130
131
132
/* A version of bsd `logwtmp' that should be widely portable

   Copyright (C) 1996, 2000, 2007 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* If `KEEP_OPEN' is defined, then a special version of logwtmp is compiled,
   called logwtmp_keep_open, which keeps the wtmp file descriptor open
   between calls, and doesn't attempt to open the file after the first call. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <unistd.h>
#include <sys/types.h>
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/file.h>
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#ifdef HAVE_UTMP_H
# include <utmp.h>
#else
# ifdef  HAVE_UTMPX_H
#  include <utmpx.h>
#  define utmp utmpx		/* make utmpx look more like utmp */
# endif
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif

#if !HAVE_DECL_ERRNO
extern int errno;
#endif

static void
_logwtmp (struct utmp *ut)
{
#ifdef KEEP_OPEN
  static int fd = -1;

  if (fd < 0)
    fd = open (PATH_WTMP, O_WRONLY | O_APPEND, 0);
#else
  int fd = open (PATH_WTMP, O_WRONLY | O_APPEND, 0);
#endif

  if (fd >= 0)
    {
      struct stat st;

#ifdef HAVE_FLOCK
      if (flock (fd, LOCK_EX | LOCK_NB) < 0 && errno != ENOSYS)
	{
	  sleep (1);
	  flock (fd, LOCK_EX | LOCK_NB);	/* ignore error */
	}
#endif

#ifdef HAVE_FTRUNCATE
      if (fstat (fd, &st) == 0
	  && write (fd, (char *) ut, sizeof *ut) != sizeof *ut)
	ftruncate (fd, st.st_size);
#else
      write (fd, (char *) ut, sizeof *ut);
#endif

#ifdef HAVE_FLOCK
      flock (fd, LOCK_UN);
#endif

#ifndef KEEP_OPEN
      close (fd);
#endif
    }
}

void
#ifdef KEEP_OPEN
logwtmp_keep_open (char *line, char *name, char *host)
#else
logwtmp (char *line, char *name, char *host)
#endif
{
  struct utmp ut;

  /* Set information in new entry.  */
  bzero (&ut, sizeof (ut));
#ifdef HAVE_STRUCT_UTMP_UT_TYPE
  ut.ut_type = USER_PROCESS;
#endif
  strncpy (ut.ut_line, line, sizeof ut.ut_line);
  strncpy (ut.ut_name, name, sizeof ut.ut_name);
#ifdef HAVE_STRUCT_UTMP_UT_HOST
  strncpy (ut.ut_host, host, sizeof ut.ut_host);
#endif

#ifdef HAVE_STRUCT_UTMP_UT_TV
  gettimeofday (&ut.ut_tv, NULL);
#else
  time (&ut.ut_time);
#endif

  _logwtmp (&ut);
}