1 /* Copyright (C) 1996-2002, 2003, 2004, 2007 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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include <not-cancel.h>
30 #include <kernel-features.h>
32 #include "utmp-private.h"
33 #include "utmp-equal.h"
36 /* Descriptor for the file and position. */
37 static int file_fd
= -1;
38 static off64_t file_offset
;
40 /* Cache for the last read entry. */
41 static struct utmp last_entry
;
44 /* Locking timeout. */
49 /* Do-nothing handler for locking timeout. */
50 static void timeout_handler (int signum
) {};
52 /* LOCK_FILE(fd, type) failure_statement
53 attempts to get a lock on the utmp file referenced by FD. If it fails,
54 the failure_statement is executed, otherwise it is skipped.
56 jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
58 unlocks the utmp file referenced by FD and performs the cleanup of
61 #define LOCK_FILE(fd, type) \
64 struct sigaction action, old_action; \
65 unsigned int old_timeout; \
67 /* Cancel any existing alarm. */ \
68 old_timeout = alarm (0); \
70 /* Establish signal handler. */ \
71 action.sa_handler = timeout_handler; \
72 __sigemptyset (&action.sa_mask); \
73 action.sa_flags = 0; \
74 __sigaction (SIGALRM, &action, &old_action); \
78 /* Try to get the lock. */ \
79 memset (&fl, '\0', sizeof (struct flock)); \
81 fl.l_whence = SEEK_SET; \
82 if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0)
84 #define LOCKING_FAILED() \
87 #define UNLOCK_FILE(fd) \
88 /* Unlock the file. */ \
89 fl.l_type = F_UNLCK; \
90 fcntl_not_cancel ((fd), F_SETLKW, &fl); \
93 /* Reset the signal handler and alarm. We must reset the alarm \
94 before resetting the handler so our alarm does not generate a \
95 spurious SIGALRM seen by the user. However, we cannot just set \
96 the user's old alarm before restoring the handler, because then \
97 it's possible our handler could catch the user alarm's SIGARLM \
98 and then the user would never see the signal he expected. */ \
100 __sigaction (SIGALRM, &old_action, NULL); \
101 if (old_timeout != 0) \
102 alarm (old_timeout); \
106 /* Functions defined here. */
107 static int setutent_file (void);
108 static int getutent_r_file (struct utmp
*buffer
, struct utmp
**result
);
109 static int getutid_r_file (const struct utmp
*key
, struct utmp
*buffer
,
110 struct utmp
**result
);
111 static int getutline_r_file (const struct utmp
*key
, struct utmp
*buffer
,
112 struct utmp
**result
);
113 static struct utmp
*pututline_file (const struct utmp
*data
);
114 static void endutent_file (void);
115 static int updwtmp_file (const char *file
, const struct utmp
*utmp
);
117 /* Jump table for file functions. */
118 const struct utfuncs __libc_utmp_file_functions
=
130 #ifndef TRANSFORM_UTMP_FILE_NAME
131 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
139 const char *file_name
;
142 file_name
= TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name
);
145 # define O_flags O_LARGEFILE | O_CLOEXEC
147 # define O_flags O_LARGEFILE
149 file_fd
= open_not_cancel_2 (file_name
, O_RDWR
| O_flags
);
152 /* Hhm, read-write access did not work. Try read-only. */
153 file_fd
= open_not_cancel_2 (file_name
, O_RDONLY
| O_flags
);
158 #ifndef __ASSUME_O_CLOEXEC
160 if (__have_o_cloexec
<= 0)
163 /* We have to make sure the file is `closed on exec'. */
164 result
= fcntl_not_cancel (file_fd
, F_GETFD
, 0);
168 if (__have_o_cloexec
== 0)
169 __have_o_cloexec
= (result
& FD_CLOEXEC
) ? 1 : -1;
172 result
= fcntl_not_cancel (file_fd
, F_SETFD
,
173 result
| FD_CLOEXEC
);
178 close_not_cancel_no_status (file_fd
);
185 __lseek64 (file_fd
, 0, SEEK_SET
);
188 /* Make sure the entry won't match. */
189 #if _HAVE_UT_TYPE - 0
190 last_entry
.ut_type
= -1;
192 last_entry
.ut_line
[0] = '\177';
194 last_entry
.ut_id
[0] = '\0';
203 getutent_r_file (struct utmp
*buffer
, struct utmp
**result
)
207 assert (file_fd
>= 0);
209 if (file_offset
== -1l)
216 LOCK_FILE (file_fd
, F_RDLCK
)
222 /* Read the next entry. */
223 nbytes
= read_not_cancel (file_fd
, &last_entry
, sizeof (struct utmp
));
225 UNLOCK_FILE (file_fd
);
227 if (nbytes
!= sizeof (struct utmp
))
235 /* Update position pointer. */
236 file_offset
+= sizeof (struct utmp
);
238 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
246 internal_getut_r (const struct utmp
*id
, struct utmp
*buffer
)
250 LOCK_FILE (file_fd
, F_RDLCK
)
253 #if _HAVE_UT_TYPE - 0
254 if (id
->ut_type
== RUN_LVL
|| id
->ut_type
== BOOT_TIME
255 || id
->ut_type
== OLD_TIME
|| id
->ut_type
== NEW_TIME
)
257 /* Search for next entry with type RUN_LVL, BOOT_TIME,
258 OLD_TIME, or NEW_TIME. */
262 /* Read the next entry. */
263 if (read_not_cancel (file_fd
, buffer
, sizeof (struct utmp
))
264 != sizeof (struct utmp
))
270 file_offset
+= sizeof (struct utmp
);
272 if (id
->ut_type
== buffer
->ut_type
)
277 #endif /* _HAVE_UT_TYPE */
279 /* Search for the next entry with the specified ID and with type
280 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
284 /* Read the next entry. */
285 if (read_not_cancel (file_fd
, buffer
, sizeof (struct utmp
))
286 != sizeof (struct utmp
))
292 file_offset
+= sizeof (struct utmp
);
294 if (__utmp_equal (buffer
, id
))
302 UNLOCK_FILE (file_fd
);
308 /* For implementing this function we don't use the getutent_r function
309 because we can avoid the reposition on every new entry this way. */
311 getutid_r_file (const struct utmp
*id
, struct utmp
*buffer
,
312 struct utmp
**result
)
314 assert (file_fd
>= 0);
316 if (file_offset
== -1l)
322 if (internal_getut_r (id
, &last_entry
) < 0)
328 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
335 /* For implementing this function we don't use the getutent_r function
336 because we can avoid the reposition on every new entry this way. */
338 getutline_r_file (const struct utmp
*line
, struct utmp
*buffer
,
339 struct utmp
**result
)
341 assert (file_fd
>= 0);
343 if (file_offset
== -1l)
349 LOCK_FILE (file_fd
, F_RDLCK
)
357 /* Read the next entry. */
358 if (read_not_cancel (file_fd
, &last_entry
, sizeof (struct utmp
))
359 != sizeof (struct utmp
))
366 file_offset
+= sizeof (struct utmp
);
368 /* Stop if we found a user or login entry. */
370 #if _HAVE_UT_TYPE - 0
371 (last_entry
.ut_type
== USER_PROCESS
372 || last_entry
.ut_type
== LOGIN_PROCESS
)
375 !strncmp (line
->ut_line
, last_entry
.ut_line
, sizeof line
->ut_line
))
379 memcpy (buffer
, &last_entry
, sizeof (struct utmp
));
383 UNLOCK_FILE (file_fd
);
385 return ((*result
== NULL
) ? -1 : 0);
390 pututline_file (const struct utmp
*data
)
396 assert (file_fd
>= 0);
398 /* Find the correct place to insert the data. */
401 #if _HAVE_UT_TYPE - 0
402 (last_entry
.ut_type
== data
->ut_type
403 && (last_entry
.ut_type
== RUN_LVL
404 || last_entry
.ut_type
== BOOT_TIME
405 || last_entry
.ut_type
== OLD_TIME
406 || last_entry
.ut_type
== NEW_TIME
))
409 __utmp_equal (&last_entry
, data
)))
412 found
= internal_getut_r (data
, &buffer
);
414 LOCK_FILE (file_fd
, F_WRLCK
)
422 /* We append the next entry. */
423 file_offset
= __lseek64 (file_fd
, 0, SEEK_END
);
424 if (file_offset
% sizeof (struct utmp
) != 0)
426 file_offset
-= file_offset
% sizeof (struct utmp
);
427 __ftruncate64 (file_fd
, file_offset
);
429 if (__lseek64 (file_fd
, 0, SEEK_END
) < 0)
438 /* We replace the just read entry. */
439 file_offset
-= sizeof (struct utmp
);
440 __lseek64 (file_fd
, file_offset
, SEEK_SET
);
443 /* Write the new data. */
444 if (write_not_cancel (file_fd
, data
, sizeof (struct utmp
))
445 != sizeof (struct utmp
))
447 /* If we appended a new record this is only partially written.
450 (void) __ftruncate64 (file_fd
, file_offset
);
455 file_offset
+= sizeof (struct utmp
);
456 pbuf
= (struct utmp
*) data
;
460 UNLOCK_FILE (file_fd
);
469 assert (file_fd
>= 0);
471 close_not_cancel_no_status (file_fd
);
477 updwtmp_file (const char *file
, const struct utmp
*utmp
)
483 /* Open WTMP file. */
484 fd
= open_not_cancel_2 (file
, O_WRONLY
| O_LARGEFILE
);
488 LOCK_FILE (fd
, F_WRLCK
)
491 /* Remember original size of log file. */
492 offset
= __lseek64 (fd
, 0, SEEK_END
);
493 if (offset
% sizeof (struct utmp
) != 0)
495 offset
-= offset
% sizeof (struct utmp
);
496 __ftruncate64 (fd
, offset
);
498 if (__lseek64 (fd
, 0, SEEK_END
) < 0)
502 /* Write the entry. If we can't write all the bytes, reset the file
503 size back to the original size. That way, no partial entries
505 if (write_not_cancel (fd
, utmp
, sizeof (struct utmp
))
506 != sizeof (struct utmp
))
508 __ftruncate64 (fd
, offset
);
517 /* Close WTMP file. */
518 close_not_cancel_no_status (fd
);