2.9
[glibc/nacl-glibc.git] / login / utmp_file.c
blob9033f72a4ee3dea3027a8af42388cbc438e94c3c
1 /* Copyright (C) 1996-2004, 2007, 2008 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 <stdbool.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <utmp.h>
30 #include <not-cancel.h>
31 #include <kernel-features.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 off64_t file_offset;
41 /* Cache for the last read entry. */
42 static struct utmp last_entry;
45 /* Locking timeout. */
46 #ifndef TIMEOUT
47 # define TIMEOUT 1
48 #endif
50 /* Do-nothing handler for locking timeout. */
51 static void timeout_handler (int signum) {};
53 /* LOCK_FILE(fd, type) failure_statement
54 attempts to get a lock on the utmp file referenced by FD. If it fails,
55 the failure_statement is executed, otherwise it is skipped.
56 LOCKING_FAILED()
57 jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
58 UNLOCK_FILE(fd)
59 unlocks the utmp file referenced by FD and performs the cleanup of
60 LOCK_FILE.
62 #define LOCK_FILE(fd, type) \
63 { \
64 struct flock fl; \
65 struct sigaction action, old_action; \
66 unsigned int old_timeout; \
68 /* Cancel any existing alarm. */ \
69 old_timeout = alarm (0); \
71 /* Establish signal handler. */ \
72 action.sa_handler = timeout_handler; \
73 __sigemptyset (&action.sa_mask); \
74 action.sa_flags = 0; \
75 __sigaction (SIGALRM, &action, &old_action); \
77 alarm (TIMEOUT); \
79 /* Try to get the lock. */ \
80 memset (&fl, '\0', sizeof (struct flock)); \
81 fl.l_type = (type); \
82 fl.l_whence = SEEK_SET; \
83 if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0)
85 #define LOCKING_FAILED() \
86 goto unalarm_return
88 #define UNLOCK_FILE(fd) \
89 /* Unlock the file. */ \
90 fl.l_type = F_UNLCK; \
91 fcntl_not_cancel ((fd), F_SETLKW, &fl); \
93 unalarm_return: \
94 /* Reset the signal handler and alarm. We must reset the alarm \
95 before resetting the handler so our alarm does not generate a \
96 spurious SIGALRM seen by the user. However, we cannot just set \
97 the user's old alarm before restoring the handler, because then \
98 it's possible our handler could catch the user alarm's SIGARLM \
99 and then the user would never see the signal he expected. */ \
100 alarm (0); \
101 __sigaction (SIGALRM, &old_action, NULL); \
102 if (old_timeout != 0) \
103 alarm (old_timeout); \
104 } while (0)
107 /* Functions defined here. */
108 static int setutent_file (void);
109 static int getutent_r_file (struct utmp *buffer, struct utmp **result);
110 static int getutid_r_file (const struct utmp *key, struct utmp *buffer,
111 struct utmp **result);
112 static int getutline_r_file (const struct utmp *key, struct utmp *buffer,
113 struct utmp **result);
114 static struct utmp *pututline_file (const struct utmp *data);
115 static void endutent_file (void);
116 static int updwtmp_file (const char *file, const struct utmp *utmp);
118 /* Jump table for file functions. */
119 const struct utfuncs __libc_utmp_file_functions =
121 setutent_file,
122 getutent_r_file,
123 getutid_r_file,
124 getutline_r_file,
125 pututline_file,
126 endutent_file,
127 updwtmp_file
131 #ifndef TRANSFORM_UTMP_FILE_NAME
132 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
133 #endif
135 static int
136 setutent_file (void)
138 if (file_fd < 0)
140 const char *file_name;
141 int result;
143 file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
145 #ifdef O_CLOEXEC
146 # define O_flags O_LARGEFILE | O_CLOEXEC
147 #else
148 # define O_flags O_LARGEFILE
149 #endif
150 file_fd = open_not_cancel_2 (file_name, O_RDWR | O_flags);
151 if (file_fd == -1)
153 /* Hhm, read-write access did not work. Try read-only. */
154 file_fd = open_not_cancel_2 (file_name, O_RDONLY | O_flags);
155 if (file_fd == -1)
156 return 0;
159 #ifndef __ASSUME_O_CLOEXEC
160 # ifdef O_CLOEXEC
161 if (__have_o_cloexec <= 0)
162 # endif
164 /* We have to make sure the file is `closed on exec'. */
165 result = fcntl_not_cancel (file_fd, F_GETFD, 0);
166 if (result >= 0)
168 # ifdef O_CLOEXEC
169 if (__have_o_cloexec == 0)
170 __have_o_cloexec = (result & FD_CLOEXEC) ? 1 : -1;
172 if (__have_o_cloexec < 0)
173 # 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,
249 bool *lock_failed)
251 int result = -1;
253 LOCK_FILE (file_fd, F_RDLCK)
255 *lock_failed = true;
256 LOCKING_FAILED ();
259 #if _HAVE_UT_TYPE - 0
260 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
261 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
263 /* Search for next entry with type RUN_LVL, BOOT_TIME,
264 OLD_TIME, or NEW_TIME. */
266 while (1)
268 /* Read the next entry. */
269 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
270 != sizeof (struct utmp))
272 __set_errno (ESRCH);
273 file_offset = -1l;
274 goto unlock_return;
276 file_offset += sizeof (struct utmp);
278 if (id->ut_type == buffer->ut_type)
279 break;
282 else
283 #endif /* _HAVE_UT_TYPE */
285 /* Search for the next entry with the specified ID and with type
286 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
288 while (1)
290 /* Read the next entry. */
291 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
292 != sizeof (struct utmp))
294 __set_errno (ESRCH);
295 file_offset = -1l;
296 goto unlock_return;
298 file_offset += sizeof (struct utmp);
300 if (__utmp_equal (buffer, id))
301 break;
305 result = 0;
307 unlock_return:
308 UNLOCK_FILE (file_fd);
310 return result;
314 /* For implementing this function we don't use the getutent_r function
315 because we can avoid the reposition on every new entry this way. */
316 static int
317 getutid_r_file (const struct utmp *id, struct utmp *buffer,
318 struct utmp **result)
320 assert (file_fd >= 0);
322 if (file_offset == -1l)
324 *result = NULL;
325 return -1;
328 /* We don't have to distinguish whether we can lock the file or
329 whether there is no entry. */
330 bool lock_failed = false;
331 if (internal_getut_r (id, &last_entry, &lock_failed) < 0)
333 *result = NULL;
334 return -1;
337 memcpy (buffer, &last_entry, sizeof (struct utmp));
338 *result = buffer;
340 return 0;
344 /* For implementing this function we don't use the getutent_r function
345 because we can avoid the reposition on every new entry this way. */
346 static int
347 getutline_r_file (const struct utmp *line, struct utmp *buffer,
348 struct utmp **result)
350 assert (file_fd >= 0);
352 if (file_offset == -1l)
354 *result = NULL;
355 return -1;
358 LOCK_FILE (file_fd, F_RDLCK)
360 *result = NULL;
361 LOCKING_FAILED ();
364 while (1)
366 /* Read the next entry. */
367 if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp))
368 != sizeof (struct utmp))
370 __set_errno (ESRCH);
371 file_offset = -1l;
372 *result = NULL;
373 goto unlock_return;
375 file_offset += sizeof (struct utmp);
377 /* Stop if we found a user or login entry. */
378 if (
379 #if _HAVE_UT_TYPE - 0
380 (last_entry.ut_type == USER_PROCESS
381 || last_entry.ut_type == LOGIN_PROCESS)
383 #endif
384 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
385 break;
388 memcpy (buffer, &last_entry, sizeof (struct utmp));
389 *result = buffer;
391 unlock_return:
392 UNLOCK_FILE (file_fd);
394 return ((*result == NULL) ? -1 : 0);
398 static struct utmp *
399 pututline_file (const struct utmp *data)
401 struct utmp buffer;
402 struct utmp *pbuf;
403 int found;
405 assert (file_fd >= 0);
407 /* Find the correct place to insert the data. */
408 if (file_offset > 0
409 && (
410 #if _HAVE_UT_TYPE - 0
411 (last_entry.ut_type == data->ut_type
412 && (last_entry.ut_type == RUN_LVL
413 || last_entry.ut_type == BOOT_TIME
414 || last_entry.ut_type == OLD_TIME
415 || last_entry.ut_type == NEW_TIME))
417 #endif
418 __utmp_equal (&last_entry, data)))
419 found = 1;
420 else
422 bool lock_failed = false;
423 found = internal_getut_r (data, &buffer, &lock_failed);
425 if (__builtin_expect (lock_failed, false))
427 __set_errno (EAGAIN);
428 return NULL;
432 LOCK_FILE (file_fd, F_WRLCK)
434 pbuf = NULL;
435 LOCKING_FAILED ();
438 if (found < 0)
440 /* We append the next entry. */
441 file_offset = __lseek64 (file_fd, 0, SEEK_END);
442 if (file_offset % sizeof (struct utmp) != 0)
444 file_offset -= file_offset % sizeof (struct utmp);
445 __ftruncate64 (file_fd, file_offset);
447 if (__lseek64 (file_fd, 0, SEEK_END) < 0)
449 pbuf = NULL;
450 goto unlock_return;
454 else
456 /* We replace the just read entry. */
457 file_offset -= sizeof (struct utmp);
458 __lseek64 (file_fd, file_offset, SEEK_SET);
461 /* Write the new data. */
462 if (write_not_cancel (file_fd, data, sizeof (struct utmp))
463 != sizeof (struct utmp))
465 /* If we appended a new record this is only partially written.
466 Remove it. */
467 if (found < 0)
468 (void) __ftruncate64 (file_fd, file_offset);
469 pbuf = NULL;
471 else
473 file_offset += sizeof (struct utmp);
474 pbuf = (struct utmp *) data;
477 unlock_return:
478 UNLOCK_FILE (file_fd);
480 return pbuf;
484 static void
485 endutent_file (void)
487 assert (file_fd >= 0);
489 close_not_cancel_no_status (file_fd);
490 file_fd = -1;
494 static int
495 updwtmp_file (const char *file, const struct utmp *utmp)
497 int result = -1;
498 off64_t offset;
499 int fd;
501 /* Open WTMP file. */
502 fd = open_not_cancel_2 (file, O_WRONLY | O_LARGEFILE);
503 if (fd < 0)
504 return -1;
506 LOCK_FILE (fd, F_WRLCK)
507 LOCKING_FAILED ();
509 /* Remember original size of log file. */
510 offset = __lseek64 (fd, 0, SEEK_END);
511 if (offset % sizeof (struct utmp) != 0)
513 offset -= offset % sizeof (struct utmp);
514 __ftruncate64 (fd, offset);
516 if (__lseek64 (fd, 0, SEEK_END) < 0)
517 goto unlock_return;
520 /* Write the entry. If we can't write all the bytes, reset the file
521 size back to the original size. That way, no partial entries
522 will remain. */
523 if (write_not_cancel (fd, utmp, sizeof (struct utmp))
524 != sizeof (struct utmp))
526 __ftruncate64 (fd, offset);
527 goto unlock_return;
530 result = 0;
532 unlock_return:
533 UNLOCK_FILE (fd);
535 /* Close WTMP file. */
536 close_not_cancel_no_status (fd);
538 return result;