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>
33 #include "utmp-private.h"
34 #include "utmp-equal.h"
37 /* Descriptor for the file and position. */
38 static int file_fd
= -1;
39 static bool file_writable
;
40 static off64_t file_offset
;
42 /* Cache for the last read entry. */
43 static struct utmp last_entry
;
46 /* Locking timeout. */
51 /* Do-nothing handler for locking timeout. */
52 static void timeout_handler (int signum
) {};
54 /* LOCK_FILE(fd, type) failure_statement
55 attempts to get a lock on the utmp file referenced by FD. If it fails,
56 the failure_statement is executed, otherwise it is skipped.
58 jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
60 unlocks the utmp file referenced by FD and performs the cleanup of
63 #define LOCK_FILE(fd, type) \
66 struct sigaction action, old_action; \
67 unsigned int old_timeout; \
69 /* Cancel any existing alarm. */ \
70 old_timeout = alarm (0); \
72 /* Establish signal handler. */ \
73 action.sa_handler = timeout_handler; \
74 __sigemptyset (&action.sa_mask); \
75 action.sa_flags = 0; \
76 __sigaction (SIGALRM, &action, &old_action); \
80 /* Try to get the lock. */ \
81 memset (&fl, '\0', sizeof (struct flock)); \
83 fl.l_whence = SEEK_SET; \
84 if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0)
86 #define LOCKING_FAILED() \
89 #define UNLOCK_FILE(fd) \
90 /* Unlock the file. */ \
91 fl.l_type = F_UNLCK; \
92 fcntl_not_cancel ((fd), F_SETLKW, &fl); \
95 /* Reset the signal handler and alarm. We must reset the alarm \
96 before resetting the handler so our alarm does not generate a \
97 spurious SIGALRM seen by the user. However, we cannot just set \
98 the user's old alarm before restoring the handler, because then \
99 it's possible our handler could catch the user alarm's SIGARLM \
100 and then the user would never see the signal he expected. */ \
102 __sigaction (SIGALRM, &old_action, NULL); \
103 if (old_timeout != 0) \
104 alarm (old_timeout); \
108 /* Functions defined here. */
109 static int setutent_file (void);
110 static int getutent_r_file (struct utmp
*buffer
, struct utmp
**result
);
111 static int getutid_r_file (const struct utmp
*key
, struct utmp
*buffer
,
112 struct utmp
**result
);
113 static int getutline_r_file (const struct utmp
*key
, struct utmp
*buffer
,
114 struct utmp
**result
);
115 static struct utmp
*pututline_file (const struct utmp
*data
);
116 static void endutent_file (void);
117 static int updwtmp_file (const char *file
, const struct utmp
*utmp
);
119 /* Jump table for file functions. */
120 const struct utfuncs __libc_utmp_file_functions
=
132 #ifndef TRANSFORM_UTMP_FILE_NAME
133 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
141 const char *file_name
;
143 file_name
= TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name
);
145 file_writable
= false;
146 file_fd
= open_not_cancel_2
147 (file_name
, O_RDONLY
| O_LARGEFILE
| O_CLOEXEC
);
152 __lseek64 (file_fd
, 0, SEEK_SET
);
155 /* Make sure the entry won't match. */
156 #if _HAVE_UT_TYPE - 0
157 last_entry
.ut_type
= -1;
159 last_entry
.ut_line
[0] = '\177';
161 last_entry
.ut_id
[0] = '\0';
170 getutent_r_file (struct utmp
*buffer
, struct utmp
**result
)
174 assert (file_fd
>= 0);
176 if (file_offset
== -1l)
183 LOCK_FILE (file_fd
, F_RDLCK
)
189 /* Read the next entry. */
190 nbytes
= read_not_cancel (file_fd
, &last_entry
, sizeof (struct utmp
));
192 UNLOCK_FILE (file_fd
);
194 if (nbytes
!= sizeof (struct utmp
))
202 /* Update position pointer. */
203 file_offset
+= sizeof (struct utmp
);
205 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
213 internal_getut_r (const struct utmp
*id
, struct utmp
*buffer
,
218 LOCK_FILE (file_fd
, F_RDLCK
)
224 #if _HAVE_UT_TYPE - 0
225 if (id
->ut_type
== RUN_LVL
|| id
->ut_type
== BOOT_TIME
226 || id
->ut_type
== OLD_TIME
|| id
->ut_type
== NEW_TIME
)
228 /* Search for next entry with type RUN_LVL, BOOT_TIME,
229 OLD_TIME, or NEW_TIME. */
233 /* Read the next entry. */
234 if (read_not_cancel (file_fd
, buffer
, sizeof (struct utmp
))
235 != sizeof (struct utmp
))
241 file_offset
+= sizeof (struct utmp
);
243 if (id
->ut_type
== buffer
->ut_type
)
248 #endif /* _HAVE_UT_TYPE */
250 /* Search for the next entry with the specified ID and with type
251 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
255 /* Read the next entry. */
256 if (read_not_cancel (file_fd
, buffer
, sizeof (struct utmp
))
257 != sizeof (struct utmp
))
263 file_offset
+= sizeof (struct utmp
);
265 if (__utmp_equal (buffer
, id
))
273 UNLOCK_FILE (file_fd
);
279 /* For implementing this function we don't use the getutent_r function
280 because we can avoid the reposition on every new entry this way. */
282 getutid_r_file (const struct utmp
*id
, struct utmp
*buffer
,
283 struct utmp
**result
)
285 assert (file_fd
>= 0);
287 if (file_offset
== -1l)
293 /* We don't have to distinguish whether we can lock the file or
294 whether there is no entry. */
295 bool lock_failed
= false;
296 if (internal_getut_r (id
, &last_entry
, &lock_failed
) < 0)
302 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
309 /* For implementing this function we don't use the getutent_r function
310 because we can avoid the reposition on every new entry this way. */
312 getutline_r_file (const struct utmp
*line
, struct utmp
*buffer
,
313 struct utmp
**result
)
315 assert (file_fd
>= 0);
317 if (file_offset
== -1l)
323 LOCK_FILE (file_fd
, F_RDLCK
)
331 /* Read the next entry. */
332 if (read_not_cancel (file_fd
, &last_entry
, sizeof (struct utmp
))
333 != sizeof (struct utmp
))
340 file_offset
+= sizeof (struct utmp
);
342 /* Stop if we found a user or login entry. */
344 #if _HAVE_UT_TYPE - 0
345 (last_entry
.ut_type
== USER_PROCESS
346 || last_entry
.ut_type
== LOGIN_PROCESS
)
349 !strncmp (line
->ut_line
, last_entry
.ut_line
, sizeof line
->ut_line
))
353 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
357 UNLOCK_FILE (file_fd
);
359 return ((*result
== NULL
) ? -1 : 0);
364 pututline_file (const struct utmp
*data
)
370 assert (file_fd
>= 0);
374 /* We must make the file descriptor writable before going on. */
375 const char *file_name
= TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name
);
377 int new_fd
= open_not_cancel_2
378 (file_name
, O_RDWR
| O_LARGEFILE
| O_CLOEXEC
);
382 if (__lseek64 (new_fd
, __lseek64 (file_fd
, 0, SEEK_CUR
), SEEK_SET
) == -1
383 || __dup2 (new_fd
, file_fd
) < 0)
385 close_not_cancel_no_status (new_fd
);
388 close_not_cancel_no_status (new_fd
);
389 file_writable
= true;
392 /* Find the correct place to insert the data. */
395 #if _HAVE_UT_TYPE - 0
396 (last_entry
.ut_type
== data
->ut_type
397 && (last_entry
.ut_type
== RUN_LVL
398 || last_entry
.ut_type
== BOOT_TIME
399 || last_entry
.ut_type
== OLD_TIME
400 || last_entry
.ut_type
== NEW_TIME
))
403 __utmp_equal (&last_entry
, data
)))
407 bool lock_failed
= false;
408 found
= internal_getut_r (data
, &buffer
, &lock_failed
);
410 if (__builtin_expect (lock_failed
, false))
412 __set_errno (EAGAIN
);
417 LOCK_FILE (file_fd
, F_WRLCK
)
425 /* We append the next entry. */
426 file_offset
= __lseek64 (file_fd
, 0, SEEK_END
);
427 if (file_offset
% sizeof (struct utmp
) != 0)
429 file_offset
-= file_offset
% sizeof (struct utmp
);
430 __ftruncate64 (file_fd
, file_offset
);
432 if (__lseek64 (file_fd
, 0, SEEK_END
) < 0)
441 /* We replace the just read entry. */
442 file_offset
-= sizeof (struct utmp
);
443 __lseek64 (file_fd
, file_offset
, SEEK_SET
);
446 /* Write the new data. */
447 if (write_not_cancel (file_fd
, data
, sizeof (struct utmp
))
448 != sizeof (struct utmp
))
450 /* If we appended a new record this is only partially written.
453 (void) __ftruncate64 (file_fd
, file_offset
);
458 file_offset
+= sizeof (struct utmp
);
459 pbuf
= (struct utmp
*) data
;
463 UNLOCK_FILE (file_fd
);
472 assert (file_fd
>= 0);
474 close_not_cancel_no_status (file_fd
);
480 updwtmp_file (const char *file
, const struct utmp
*utmp
)
486 /* Open WTMP file. */
487 fd
= open_not_cancel_2 (file
, O_WRONLY
| O_LARGEFILE
);
491 LOCK_FILE (fd
, F_WRLCK
)
494 /* Remember original size of log file. */
495 offset
= __lseek64 (fd
, 0, SEEK_END
);
496 if (offset
% sizeof (struct utmp
) != 0)
498 offset
-= offset
% sizeof (struct utmp
);
499 __ftruncate64 (fd
, offset
);
501 if (__lseek64 (fd
, 0, SEEK_END
) < 0)
505 /* Write the entry. If we can't write all the bytes, reset the file
506 size back to the original size. That way, no partial entries
508 if (write_not_cancel (fd
, utmp
, sizeof (struct utmp
))
509 != sizeof (struct utmp
))
511 __ftruncate64 (fd
, offset
);
520 /* Close WTMP file. */
521 close_not_cancel_no_status (fd
);