Updated to fedora-glibc-20070731T1624
[glibc.git] / login / utmp_file.c
blob4a9e4094545fe21660271f51867e1ed2f8e639bf
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
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <utmp.h>
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. */
45 #ifndef TIMEOUT
46 # define TIMEOUT 1
47 #endif
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.
55 LOCKING_FAILED()
56 jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
57 UNLOCK_FILE(fd)
58 unlocks the utmp file referenced by FD and performs the cleanup of
59 LOCK_FILE.
61 #define LOCK_FILE(fd, type) \
62 { \
63 struct flock fl; \
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); \
76 alarm (TIMEOUT); \
78 /* Try to get the lock. */ \
79 memset (&fl, '\0', sizeof (struct flock)); \
80 fl.l_type = (type); \
81 fl.l_whence = SEEK_SET; \
82 if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0)
84 #define LOCKING_FAILED() \
85 goto unalarm_return
87 #define UNLOCK_FILE(fd) \
88 /* Unlock the file. */ \
89 fl.l_type = F_UNLCK; \
90 fcntl_not_cancel ((fd), F_SETLKW, &fl); \
92 unalarm_return: \
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. */ \
99 alarm (0); \
100 __sigaction (SIGALRM, &old_action, NULL); \
101 if (old_timeout != 0) \
102 alarm (old_timeout); \
103 } while (0)
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 =
120 setutent_file,
121 getutent_r_file,
122 getutid_r_file,
123 getutline_r_file,
124 pututline_file,
125 endutent_file,
126 updwtmp_file
130 #ifndef TRANSFORM_UTMP_FILE_NAME
131 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
132 #endif
134 static int
135 setutent_file (void)
137 if (file_fd < 0)
139 const char *file_name;
140 int result;
142 file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
144 #ifdef O_CLOEXEC
145 # define O_flags O_LARGEFILE | O_CLOEXEC
146 #else
147 # define O_flags O_LARGEFILE
148 #endif
149 file_fd = open_not_cancel_2 (file_name, O_RDWR | O_flags);
150 if (file_fd == -1)
152 /* Hhm, read-write access did not work. Try read-only. */
153 file_fd = open_not_cancel_2 (file_name, O_RDONLY | O_flags);
154 if (file_fd == -1)
155 return 0;
158 #ifndef __ASSUME_O_CLOEXEC
159 # ifdef O_CLOEXEC
160 static int have_o_cloexec;
162 if (have_o_cloexec <= 0)
163 # endif
165 /* We have to make sure the file is `closed on exec'. */
166 result = fcntl_not_cancel (file_fd, F_GETFD, 0);
167 if (result >= 0)
169 # ifdef O_CLOEXEC
170 if (have_o_cloexec == 0)
171 have_o_cloexec = (result & FD_CLOEXEC) ? 1 : -1;
172 # endif
174 result = fcntl_not_cancel (file_fd, F_SETFD,
175 result | FD_CLOEXEC);
178 if (result == -1)
180 close_not_cancel_no_status (file_fd);
181 return 0;
184 #endif
187 __lseek64 (file_fd, 0, SEEK_SET);
188 file_offset = 0;
190 /* Make sure the entry won't match. */
191 #if _HAVE_UT_TYPE - 0
192 last_entry.ut_type = -1;
193 #else
194 last_entry.ut_line[0] = '\177';
195 # if _HAVE_UT_ID - 0
196 last_entry.ut_id[0] = '\0';
197 # endif
198 #endif
200 return 1;
204 static int
205 getutent_r_file (struct utmp *buffer, struct utmp **result)
207 ssize_t nbytes;
209 assert (file_fd >= 0);
211 if (file_offset == -1l)
213 /* Not available. */
214 *result = NULL;
215 return -1;
218 LOCK_FILE (file_fd, F_RDLCK)
220 nbytes = 0;
221 LOCKING_FAILED ();
224 /* Read the next entry. */
225 nbytes = read_not_cancel (file_fd, &last_entry, sizeof (struct utmp));
227 UNLOCK_FILE (file_fd);
229 if (nbytes != sizeof (struct utmp))
231 if (nbytes != 0)
232 file_offset = -1l;
233 *result = NULL;
234 return -1;
237 /* Update position pointer. */
238 file_offset += sizeof (struct utmp);
240 memcpy (buffer, &last_entry, sizeof (struct utmp));
241 *result = buffer;
243 return 0;
247 static int
248 internal_getut_r (const struct utmp *id, struct utmp *buffer)
250 int result = -1;
252 LOCK_FILE (file_fd, F_RDLCK)
253 LOCKING_FAILED ();
255 #if _HAVE_UT_TYPE - 0
256 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
257 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
259 /* Search for next entry with type RUN_LVL, BOOT_TIME,
260 OLD_TIME, or NEW_TIME. */
262 while (1)
264 /* Read the next entry. */
265 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
266 != sizeof (struct utmp))
268 __set_errno (ESRCH);
269 file_offset = -1l;
270 goto unlock_return;
272 file_offset += sizeof (struct utmp);
274 if (id->ut_type == buffer->ut_type)
275 break;
278 else
279 #endif /* _HAVE_UT_TYPE */
281 /* Search for the next entry with the specified ID and with type
282 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
284 while (1)
286 /* Read the next entry. */
287 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
288 != sizeof (struct utmp))
290 __set_errno (ESRCH);
291 file_offset = -1l;
292 goto unlock_return;
294 file_offset += sizeof (struct utmp);
296 if (__utmp_equal (buffer, id))
297 break;
301 result = 0;
303 unlock_return:
304 UNLOCK_FILE (file_fd);
306 return result;
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. */
312 static int
313 getutid_r_file (const struct utmp *id, struct utmp *buffer,
314 struct utmp **result)
316 assert (file_fd >= 0);
318 if (file_offset == -1l)
320 *result = NULL;
321 return -1;
324 if (internal_getut_r (id, &last_entry) < 0)
326 *result = NULL;
327 return -1;
330 memcpy (buffer, &last_entry, sizeof (struct utmp));
331 *result = buffer;
333 return 0;
337 /* For implementing this function we don't use the getutent_r function
338 because we can avoid the reposition on every new entry this way. */
339 static int
340 getutline_r_file (const struct utmp *line, struct utmp *buffer,
341 struct utmp **result)
343 assert (file_fd >= 0);
345 if (file_offset == -1l)
347 *result = NULL;
348 return -1;
351 LOCK_FILE (file_fd, F_RDLCK)
353 *result = NULL;
354 LOCKING_FAILED ();
357 while (1)
359 /* Read the next entry. */
360 if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp))
361 != sizeof (struct utmp))
363 __set_errno (ESRCH);
364 file_offset = -1l;
365 *result = NULL;
366 goto unlock_return;
368 file_offset += sizeof (struct utmp);
370 /* Stop if we found a user or login entry. */
371 if (
372 #if _HAVE_UT_TYPE - 0
373 (last_entry.ut_type == USER_PROCESS
374 || last_entry.ut_type == LOGIN_PROCESS)
376 #endif
377 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
378 break;
381 memcpy (buffer, &last_entry, sizeof (struct utmp));
382 *result = buffer;
384 unlock_return:
385 UNLOCK_FILE (file_fd);
387 return ((*result == NULL) ? -1 : 0);
391 static struct utmp *
392 pututline_file (const struct utmp *data)
394 struct utmp buffer;
395 struct utmp *pbuf;
396 int found;
398 assert (file_fd >= 0);
400 /* Find the correct place to insert the data. */
401 if (file_offset > 0
402 && (
403 #if _HAVE_UT_TYPE - 0
404 (last_entry.ut_type == data->ut_type
405 && (last_entry.ut_type == RUN_LVL
406 || last_entry.ut_type == BOOT_TIME
407 || last_entry.ut_type == OLD_TIME
408 || last_entry.ut_type == NEW_TIME))
410 #endif
411 __utmp_equal (&last_entry, data)))
412 found = 1;
413 else
414 found = internal_getut_r (data, &buffer);
416 LOCK_FILE (file_fd, F_WRLCK)
418 pbuf = NULL;
419 LOCKING_FAILED ();
422 if (found < 0)
424 /* We append the next entry. */
425 file_offset = __lseek64 (file_fd, 0, SEEK_END);
426 if (file_offset % sizeof (struct utmp) != 0)
428 file_offset -= file_offset % sizeof (struct utmp);
429 __ftruncate64 (file_fd, file_offset);
431 if (__lseek64 (file_fd, 0, SEEK_END) < 0)
433 pbuf = NULL;
434 goto unlock_return;
438 else
440 /* We replace the just read entry. */
441 file_offset -= sizeof (struct utmp);
442 __lseek64 (file_fd, file_offset, SEEK_SET);
445 /* Write the new data. */
446 if (write_not_cancel (file_fd, data, sizeof (struct utmp))
447 != sizeof (struct utmp))
449 /* If we appended a new record this is only partially written.
450 Remove it. */
451 if (found < 0)
452 (void) __ftruncate64 (file_fd, file_offset);
453 pbuf = NULL;
455 else
457 file_offset += sizeof (struct utmp);
458 pbuf = (struct utmp *) data;
461 unlock_return:
462 UNLOCK_FILE (file_fd);
464 return pbuf;
468 static void
469 endutent_file (void)
471 assert (file_fd >= 0);
473 close_not_cancel_no_status (file_fd);
474 file_fd = -1;
478 static int
479 updwtmp_file (const char *file, const struct utmp *utmp)
481 int result = -1;
482 off64_t offset;
483 int fd;
485 /* Open WTMP file. */
486 fd = open_not_cancel_2 (file, O_WRONLY | O_LARGEFILE);
487 if (fd < 0)
488 return -1;
490 LOCK_FILE (fd, F_WRLCK)
491 LOCKING_FAILED ();
493 /* Remember original size of log file. */
494 offset = __lseek64 (fd, 0, SEEK_END);
495 if (offset % sizeof (struct utmp) != 0)
497 offset -= offset % sizeof (struct utmp);
498 __ftruncate64 (fd, offset);
500 if (__lseek64 (fd, 0, SEEK_END) < 0)
501 goto unlock_return;
504 /* Write the entry. If we can't write all the bytes, reset the file
505 size back to the original size. That way, no partial entries
506 will remain. */
507 if (write_not_cancel (fd, utmp, sizeof (struct utmp))
508 != sizeof (struct utmp))
510 __ftruncate64 (fd, offset);
511 goto unlock_return;
514 result = 0;
516 unlock_return:
517 UNLOCK_FILE (fd);
519 /* Close WTMP file. */
520 close_not_cancel_no_status (fd);
522 return result;