1 /* Copyright (C) 1996-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>
4 and Paul Janzen <pcj@primenet.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
29 #include <not-cancel.h>
30 #include <kernel-features.h>
31 #include <sigsetops.h>
32 #include <not-cancel.h>
34 #include "utmp-private.h"
35 #include "utmp-equal.h"
38 /* Descriptor for the file and position. */
39 static int file_fd
= -1;
40 static bool file_writable
;
41 static off64_t file_offset
;
43 /* Cache for the last read entry. */
44 static struct utmp last_entry
;
47 /* Locking timeout. */
52 /* Do-nothing handler for locking timeout. */
53 static void timeout_handler (int signum
) {};
55 /* LOCK_FILE(fd, type) failure_statement
56 attempts to get a lock on the utmp file referenced by FD. If it fails,
57 the failure_statement is executed, otherwise it is skipped.
59 jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
61 unlocks the utmp file referenced by FD and performs the cleanup of
64 #define LOCK_FILE(fd, type) \
67 struct sigaction action, old_action; \
68 unsigned int old_timeout; \
70 /* Cancel any existing alarm. */ \
71 old_timeout = alarm (0); \
73 /* Establish signal handler. */ \
74 action.sa_handler = timeout_handler; \
75 __sigemptyset (&action.sa_mask); \
76 action.sa_flags = 0; \
77 __sigaction (SIGALRM, &action, &old_action); \
81 /* Try to get the lock. */ \
82 memset (&fl, '\0', sizeof (struct flock)); \
84 fl.l_whence = SEEK_SET; \
85 if (__fcntl_nocancel ((fd), F_SETLKW, &fl) < 0)
87 #define LOCKING_FAILED() \
90 #define UNLOCK_FILE(fd) \
91 /* Unlock the file. */ \
92 fl.l_type = F_UNLCK; \
93 __fcntl_nocancel ((fd), F_SETLKW, &fl); \
96 /* Reset the signal handler and alarm. We must reset the alarm \
97 before resetting the handler so our alarm does not generate a \
98 spurious SIGALRM seen by the user. However, we cannot just set \
99 the user's old alarm before restoring the handler, because then \
100 it's possible our handler could catch the user alarm's SIGARLM \
101 and then the user would never see the signal he expected. */ \
103 __sigaction (SIGALRM, &old_action, NULL); \
104 if (old_timeout != 0) \
105 alarm (old_timeout); \
109 /* Functions defined here. */
110 static int setutent_file (void);
111 static int getutent_r_file (struct utmp
*buffer
, struct utmp
**result
);
112 static int getutid_r_file (const struct utmp
*key
, struct utmp
*buffer
,
113 struct utmp
**result
);
114 static int getutline_r_file (const struct utmp
*key
, struct utmp
*buffer
,
115 struct utmp
**result
);
116 static struct utmp
*pututline_file (const struct utmp
*data
);
117 static void endutent_file (void);
118 static int updwtmp_file (const char *file
, const struct utmp
*utmp
);
120 /* Jump table for file functions. */
121 const struct utfuncs __libc_utmp_file_functions
=
133 #ifndef TRANSFORM_UTMP_FILE_NAME
134 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
142 const char *file_name
;
144 file_name
= TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name
);
146 file_writable
= false;
147 file_fd
= __open_nocancel
148 (file_name
, O_RDONLY
| O_LARGEFILE
| O_CLOEXEC
);
153 __lseek64 (file_fd
, 0, SEEK_SET
);
156 /* Make sure the entry won't match. */
157 #if _HAVE_UT_TYPE - 0
158 last_entry
.ut_type
= -1;
160 last_entry
.ut_line
[0] = '\177';
162 last_entry
.ut_id
[0] = '\0';
171 getutent_r_file (struct utmp
*buffer
, struct utmp
**result
)
175 assert (file_fd
>= 0);
177 if (file_offset
== -1l)
184 LOCK_FILE (file_fd
, F_RDLCK
)
190 /* Read the next entry. */
191 nbytes
= __read_nocancel (file_fd
, &last_entry
, sizeof (struct utmp
));
193 UNLOCK_FILE (file_fd
);
195 if (nbytes
!= sizeof (struct utmp
))
203 /* Update position pointer. */
204 file_offset
+= sizeof (struct utmp
);
206 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
214 internal_getut_r (const struct utmp
*id
, struct utmp
*buffer
,
219 LOCK_FILE (file_fd
, F_RDLCK
)
225 #if _HAVE_UT_TYPE - 0
226 if (id
->ut_type
== RUN_LVL
|| id
->ut_type
== BOOT_TIME
227 || id
->ut_type
== OLD_TIME
|| id
->ut_type
== NEW_TIME
)
229 /* Search for next entry with type RUN_LVL, BOOT_TIME,
230 OLD_TIME, or NEW_TIME. */
234 /* Read the next entry. */
235 if (__read_nocancel (file_fd
, buffer
, sizeof (struct utmp
))
236 != sizeof (struct utmp
))
242 file_offset
+= sizeof (struct utmp
);
244 if (id
->ut_type
== buffer
->ut_type
)
249 #endif /* _HAVE_UT_TYPE */
251 /* Search for the next entry with the specified ID and with type
252 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
256 /* Read the next entry. */
257 if (__read_nocancel (file_fd
, buffer
, sizeof (struct utmp
))
258 != sizeof (struct utmp
))
264 file_offset
+= sizeof (struct utmp
);
266 if (__utmp_equal (buffer
, id
))
274 UNLOCK_FILE (file_fd
);
280 /* For implementing this function we don't use the getutent_r function
281 because we can avoid the reposition on every new entry this way. */
283 getutid_r_file (const struct utmp
*id
, struct utmp
*buffer
,
284 struct utmp
**result
)
286 assert (file_fd
>= 0);
288 if (file_offset
== -1l)
294 /* We don't have to distinguish whether we can lock the file or
295 whether there is no entry. */
296 bool lock_failed
= false;
297 if (internal_getut_r (id
, &last_entry
, &lock_failed
) < 0)
303 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
310 /* For implementing this function we don't use the getutent_r function
311 because we can avoid the reposition on every new entry this way. */
313 getutline_r_file (const struct utmp
*line
, struct utmp
*buffer
,
314 struct utmp
**result
)
316 assert (file_fd
>= 0);
318 if (file_offset
== -1l)
324 LOCK_FILE (file_fd
, F_RDLCK
)
332 /* Read the next entry. */
333 if (__read_nocancel (file_fd
, &last_entry
, sizeof (struct utmp
))
334 != sizeof (struct utmp
))
341 file_offset
+= sizeof (struct utmp
);
343 /* Stop if we found a user or login entry. */
345 #if _HAVE_UT_TYPE - 0
346 (last_entry
.ut_type
== USER_PROCESS
347 || last_entry
.ut_type
== LOGIN_PROCESS
)
350 !strncmp (line
->ut_line
, last_entry
.ut_line
, sizeof line
->ut_line
))
354 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
358 UNLOCK_FILE (file_fd
);
360 return ((*result
== NULL
) ? -1 : 0);
365 pututline_file (const struct utmp
*data
)
371 assert (file_fd
>= 0);
375 /* We must make the file descriptor writable before going on. */
376 const char *file_name
= TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name
);
378 int new_fd
= __open_nocancel
379 (file_name
, O_RDWR
| O_LARGEFILE
| O_CLOEXEC
);
383 if (__lseek64 (new_fd
, __lseek64 (file_fd
, 0, SEEK_CUR
), SEEK_SET
) == -1
384 || __dup2 (new_fd
, file_fd
) < 0)
386 __close_nocancel_nostatus (new_fd
);
389 __close_nocancel_nostatus (new_fd
);
390 file_writable
= true;
393 /* Find the correct place to insert the data. */
396 #if _HAVE_UT_TYPE - 0
397 (last_entry
.ut_type
== data
->ut_type
398 && (last_entry
.ut_type
== RUN_LVL
399 || last_entry
.ut_type
== BOOT_TIME
400 || last_entry
.ut_type
== OLD_TIME
401 || last_entry
.ut_type
== NEW_TIME
))
404 __utmp_equal (&last_entry
, data
)))
408 bool lock_failed
= false;
409 found
= internal_getut_r (data
, &buffer
, &lock_failed
);
411 if (__builtin_expect (lock_failed
, false))
413 __set_errno (EAGAIN
);
418 LOCK_FILE (file_fd
, F_WRLCK
)
426 /* We append the next entry. */
427 file_offset
= __lseek64 (file_fd
, 0, SEEK_END
);
428 if (file_offset
% sizeof (struct utmp
) != 0)
430 file_offset
-= file_offset
% sizeof (struct utmp
);
431 __ftruncate64 (file_fd
, file_offset
);
433 if (__lseek64 (file_fd
, 0, SEEK_END
) < 0)
442 /* We replace the just read entry. */
443 file_offset
-= sizeof (struct utmp
);
444 __lseek64 (file_fd
, file_offset
, SEEK_SET
);
447 /* Write the new data. */
448 if (__write_nocancel (file_fd
, data
, sizeof (struct utmp
))
449 != sizeof (struct utmp
))
451 /* If we appended a new record this is only partially written.
454 (void) __ftruncate64 (file_fd
, file_offset
);
459 file_offset
+= sizeof (struct utmp
);
460 pbuf
= (struct utmp
*) data
;
464 UNLOCK_FILE (file_fd
);
473 assert (file_fd
>= 0);
475 __close_nocancel_nostatus (file_fd
);
481 updwtmp_file (const char *file
, const struct utmp
*utmp
)
487 /* Open WTMP file. */
488 fd
= __open_nocancel (file
, O_WRONLY
| O_LARGEFILE
);
492 LOCK_FILE (fd
, F_WRLCK
)
495 /* Remember original size of log file. */
496 offset
= __lseek64 (fd
, 0, SEEK_END
);
497 if (offset
% sizeof (struct utmp
) != 0)
499 offset
-= offset
% sizeof (struct utmp
);
500 __ftruncate64 (fd
, offset
);
502 if (__lseek64 (fd
, 0, SEEK_END
) < 0)
506 /* Write the entry. If we can't write all the bytes, reset the file
507 size back to the original size. That way, no partial entries
509 if (__write_nocancel (fd
, utmp
, sizeof (struct utmp
))
510 != sizeof (struct utmp
))
512 __ftruncate64 (fd
, offset
);
521 /* Close WTMP file. */
522 __close_nocancel_nostatus (fd
);