1 /* Copyright (C) 1996-2002, 2003, 2004 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>
31 #include "utmp-private.h"
32 #include "utmp-equal.h"
35 /* Descriptor for the file and position. */
36 static int file_fd = -1;
37 static off64_t file_offset;
39 /* Cache for the last read entry. */
40 static struct utmp last_entry;
43 /* Locking timeout. */
48 /* Do-nothing handler for locking timeout. */
49 static void timeout_handler (int signum) {};
51 /* LOCK_FILE(fd, type) failure_statement
52 attempts to get a lock on the utmp file referenced by FD. If it fails,
53 the failure_statement is executed, otherwise it is skipped.
55 jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
57 unlocks the utmp file referenced by FD and performs the cleanup of
60 #define LOCK_FILE(fd, type) \
63 struct sigaction action, old_action; \
64 unsigned int old_timeout; \
66 /* Cancel any existing alarm. */ \
67 old_timeout = alarm (0); \
69 /* Establish signal handler. */ \
70 action.sa_handler = timeout_handler; \
71 __sigemptyset (&action.sa_mask); \
72 action.sa_flags = 0; \
73 __sigaction (SIGALRM, &action, &old_action); \
77 /* Try to get the lock. */ \
78 memset (&fl, '\0', sizeof (struct flock)); \
80 fl.l_whence = SEEK_SET; \
81 if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0)
83 #define LOCKING_FAILED() \
86 #define UNLOCK_FILE(fd) \
87 /* Unlock the file. */ \
88 fl.l_type = F_UNLCK; \
89 fcntl_not_cancel ((fd), F_SETLKW, &fl); \
92 /* Reset the signal handler and alarm. We must reset the alarm \
93 before resetting the handler so our alarm does not generate a \
94 spurious SIGALRM seen by the user. However, we cannot just set \
95 the user's old alarm before restoring the handler, because then \
96 it's possible our handler could catch the user alarm's SIGARLM \
97 and then the user would never see the signal he expected. */ \
99 __sigaction (SIGALRM, &old_action, NULL); \
100 if (old_timeout != 0) \
101 alarm (old_timeout); \
105 /* Functions defined here. */
106 static int setutent_file (void);
107 static int getutent_r_file (struct utmp *buffer, struct utmp **result);
108 static int getutid_r_file (const struct utmp *key, struct utmp *buffer,
109 struct utmp **result);
110 static int getutline_r_file (const struct utmp *key, struct utmp *buffer,
111 struct utmp **result);
112 static struct utmp *pututline_file (const struct utmp *data);
113 static void endutent_file (void);
114 static int updwtmp_file (const char *file, const struct utmp *utmp);
116 /* Jump table for file functions. */
117 const struct utfuncs __libc_utmp_file_functions =
129 #ifndef TRANSFORM_UTMP_FILE_NAME
130 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
138 const char *file_name;
141 file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
143 file_fd = open_not_cancel_2 (file_name, O_RDWR);
146 /* Hhm, read-write access did not work. Try read-only. */
147 file_fd = open_not_cancel_2 (file_name, O_RDONLY);
152 /* We have to make sure the file is `closed on exec'. */
153 result = fcntl_not_cancel (file_fd, F_GETFD, 0);
155 result = fcntl_not_cancel (file_fd, F_SETFD, result | FD_CLOEXEC);
158 close_not_cancel_no_status (file_fd);
163 __lseek64 (file_fd, 0, SEEK_SET);
166 /* Make sure the entry won't match. */
167 #if _HAVE_UT_TYPE - 0
168 last_entry.ut_type = -1;
170 last_entry.ut_line[0] = '\177';
172 last_entry.ut_id[0] = '\0';
181 getutent_r_file (struct utmp *buffer, struct utmp **result)
185 assert (file_fd >= 0);
187 if (file_offset == -1l)
194 LOCK_FILE (file_fd, F_RDLCK)
200 /* Read the next entry. */
201 nbytes = read_not_cancel (file_fd, &last_entry, sizeof (struct utmp));
203 UNLOCK_FILE (file_fd);
205 if (nbytes != sizeof (struct utmp))
213 /* Update position pointer. */
214 file_offset += sizeof (struct utmp);
216 memcpy (buffer, &last_entry, sizeof (struct utmp));
224 internal_getut_r (const struct utmp *id, struct utmp *buffer)
228 LOCK_FILE (file_fd, F_RDLCK)
231 #if _HAVE_UT_TYPE - 0
232 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
233 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
235 /* Search for next entry with type RUN_LVL, BOOT_TIME,
236 OLD_TIME, or NEW_TIME. */
240 /* Read the next entry. */
241 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
242 != sizeof (struct utmp))
248 file_offset += sizeof (struct utmp);
250 if (id->ut_type == buffer->ut_type)
255 #endif /* _HAVE_UT_TYPE */
257 /* Search for the next entry with the specified ID and with type
258 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
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 (__utmp_equal (buffer, id))
280 UNLOCK_FILE (file_fd);
286 /* For implementing this function we don't use the getutent_r function
287 because we can avoid the reposition on every new entry this way. */
289 getutid_r_file (const struct utmp *id, struct utmp *buffer,
290 struct utmp **result)
292 assert (file_fd >= 0);
294 if (file_offset == -1l)
300 if (internal_getut_r (id, &last_entry) < 0)
306 memcpy (buffer, &last_entry, sizeof (struct utmp));
313 /* For implementing this function we don't use the getutent_r function
314 because we can avoid the reposition on every new entry this way. */
316 getutline_r_file (const struct utmp *line, struct utmp *buffer,
317 struct utmp **result)
319 assert (file_fd >= 0);
321 if (file_offset == -1l)
327 LOCK_FILE (file_fd, F_RDLCK)
335 /* Read the next entry. */
336 if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp))
337 != sizeof (struct utmp))
344 file_offset += sizeof (struct utmp);
346 /* Stop if we found a user or login entry. */
348 #if _HAVE_UT_TYPE - 0
349 (last_entry.ut_type == USER_PROCESS
350 || last_entry.ut_type == LOGIN_PROCESS)
353 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
357 memcpy (buffer, &last_entry, sizeof (struct utmp));
361 UNLOCK_FILE (file_fd);
363 return ((*result == NULL) ? -1 : 0);
368 pututline_file (const struct utmp *data)
374 assert (file_fd >= 0);
376 /* Find the correct place to insert the data. */
379 #if _HAVE_UT_TYPE - 0
380 (last_entry.ut_type == data->ut_type
381 && (last_entry.ut_type == RUN_LVL
382 || last_entry.ut_type == BOOT_TIME
383 || last_entry.ut_type == OLD_TIME
384 || last_entry.ut_type == NEW_TIME))
387 __utmp_equal (&last_entry, data)))
390 found = internal_getut_r (data, &buffer);
392 LOCK_FILE (file_fd, F_WRLCK)
400 /* We append the next entry. */
401 file_offset = __lseek64 (file_fd, 0, SEEK_END);
402 if (file_offset % sizeof (struct utmp) != 0)
404 file_offset -= file_offset % sizeof (struct utmp);
405 __ftruncate64 (file_fd, file_offset);
407 if (__lseek64 (file_fd, 0, SEEK_END) < 0)
416 /* We replace the just read entry. */
417 file_offset -= sizeof (struct utmp);
418 __lseek64 (file_fd, file_offset, SEEK_SET);
421 /* Write the new data. */
422 if (write_not_cancel (file_fd, data, sizeof (struct utmp))
423 != sizeof (struct utmp))
425 /* If we appended a new record this is only partially written.
428 (void) __ftruncate64 (file_fd, file_offset);
433 file_offset += sizeof (struct utmp);
434 pbuf = (struct utmp *) data;
438 UNLOCK_FILE (file_fd);
447 assert (file_fd >= 0);
449 close_not_cancel_no_status (file_fd);
455 updwtmp_file (const char *file, const struct utmp *utmp)
461 /* Open WTMP file. */
462 fd = open_not_cancel_2 (file, O_WRONLY);
466 LOCK_FILE (fd, F_WRLCK)
469 /* Remember original size of log file. */
470 offset = __lseek64 (fd, 0, SEEK_END);
471 if (offset % sizeof (struct utmp) != 0)
473 offset -= offset % sizeof (struct utmp);
474 __ftruncate64 (fd, offset);
476 if (__lseek64 (fd, 0, SEEK_END) < 0)
480 /* Write the entry. If we can't write all the bytes, reset the file
481 size back to the original size. That way, no partial entries
483 if (write_not_cancel (fd, utmp, sizeof (struct utmp))
484 != sizeof (struct utmp))
486 __ftruncate64 (fd, offset);
495 /* Close WTMP file. */
496 close_not_cancel_no_status (fd);