Debian Codegraph @

libinetutils/logwtmp.c

raw
1 /* logwtmp.c - A version of BSDs `logwtmp' that should be widely portable
2 Copyright (C) 1997-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /* Written by Miles Bader. */
20
21 /* If `KEEP_OPEN' is defined, then a special version of logwtmp is compiled,
22 called logwtmp_keep_open, which keeps the wtmp file descriptor open
23 between calls, and doesn't attempt to open the file after the first call. */
24
25 #include <config.h>
26
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <time.h>
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <sys/file.h>
34 #include <errno.h>
35 #ifdef HAVE_UTMPX_H
36 # ifndef __USE_GNU
37 # define __USE_GNU 1
38 # endif
39 # include <utmpx.h>
40 # define OUR_WTMP PATH_WTMPX
41 #elif defined HAVE_UTMP_H
42 # include <utmp.h>
43 # define OUR_WTMP PATH_WTMP
44 #endif
45 #include <string.h>
46
47 #ifdef HAVE_UTMPX_H
48 static void
49 _logwtmp (struct utmpx *ut)
50 #else /* !HAVE_UTMPX_H */
51 static void
52 _logwtmp (struct utmp *ut)
53 #endif /* !HAVE_UTMP_H */
54 {
55 #ifdef KEEP_OPEN
56 static int fd = -1;
57
58 if (fd < 0)
59 fd = open (OUR_WTMP, O_WRONLY | O_APPEND, 0);
60 #else
61 int fd = open (OUR_WTMP, O_WRONLY | O_APPEND, 0);
62 #endif
63
64 if (fd >= 0)
65 {
66 struct stat st;
67
68 #ifdef HAVE_FLOCK
69 if (flock (fd, LOCK_EX | LOCK_NB) < 0 && errno != ENOSYS)
70 {
71 sleep (1);
72 flock (fd, LOCK_EX | LOCK_NB); /* ignore error */
73 }
74 #endif
75
76 #ifdef HAVE_FTRUNCATE
77 if (fstat (fd, &st) == 0
78 && write (fd, (char *) ut, sizeof *ut) != sizeof *ut)
79 ftruncate (fd, st.st_size);
80 #else
81 write (fd, (char *) ut, sizeof *ut);
82 #endif
83
84 #ifdef HAVE_FLOCK
85 flock (fd, LOCK_UN);
86 #endif
87
88 #ifndef KEEP_OPEN
89 close (fd);
90 #endif
91 }
92 }
93
94 void
95 #ifdef KEEP_OPEN
96 logwtmp_keep_open (char *line, char *name, char *host)
97 #else
98 logwtmp (char *line, char *name, char *host)
99 #endif
100 {
101 #ifdef HAVE_UTMPX_H
102 struct utmpx ut;
103 struct timeval tv;
104 #else /* !HAVE_UTMPX_H */
105 struct utmp ut;
106 # ifdef HAVE_STRUCT_UTMP_UT_TV
107 struct timeval tv;
108 # endif
109 #endif
110
111 /* Set information in new entry. */
112 memset (&ut, 0, sizeof (ut));
113 #if defined HAVE_STRUCT_UTMP_UT_TYPE || defined HAVE_STRUCT_UTMPX_UT_TYPE
114 if (name && *name)
115 ut.ut_type = USER_PROCESS;
116 else
117 ut.ut_type = DEAD_PROCESS;
118 #endif /* UT_TYPE */
119 #if defined HAVE_STRUCT_UTMP_UT_PID || defined HAVE_STRUCT_UTMPX_UT_PID
120 ut.ut_pid = getpid ();
121 #endif /* UT_PID */
122
123 strncpy (ut.ut_line, line, sizeof ut.ut_line);
124 #if defined HAVE_STRUCT_UTMP_UT_USER || defined HAVE_STRUCT_UTMPX_UT_USER
125 strncpy (ut.ut_user, name, sizeof ut.ut_user);
126 #elif defined HAVE_STRUCT_UTMP_UT_NAME || defined HAVE_STRUCT_UTMPX_UT_NAME
127 strncpy (ut.ut_name, name, sizeof ut.ut_name);
128 #endif
129
130 #if defined HAVE_STRUCT_UTMP_UT_HOST || defined HAVE_STRUCT_UTMPX_UT_HOST
131 strncpy (ut.ut_host, host, sizeof ut.ut_host);
132 # ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN /* Only utmpx. */
133 if (strlen (host) < sizeof (ut.ut_host))
134 ut.ut_syslen = strlen (host) + 1; /* Including NUL. */
135 else
136 {
137 ut.ut_host[sizeof (ut.ut_host) - 1] = '\0';
138 ut.ut_syslen = sizeof (ut.ut_host);
139 }
140 # endif/* UT_SYSLEN */
141 #endif /* UT_HOST */
142
143 #if defined HAVE_STRUCT_UTMP_UT_TV || defined HAVE_STRUCT_UTMPX_UT_TV
144 gettimeofday (&tv, NULL);
145 ut.ut_tv.tv_sec = tv.tv_sec;
146 ut.ut_tv.tv_usec = tv.tv_usec;
147 #else
148 time (&ut.ut_time);
149 #endif
150
151 _logwtmp (&ut);
152 }