[BZ #6771]
[glibc.git] / login / utmp_file.c
blobc0bd22995227a2d2b0376732182f1f8f78f012a1
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 <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 if (__have_o_cloexec <= 0)
161 # endif
163 /* We have to make sure the file is `closed on exec'. */
164 result = fcntl_not_cancel (file_fd, F_GETFD, 0);
165 if (result >= 0)
167 # ifdef O_CLOEXEC
168 if (__have_o_cloexec == 0)
169 __have_o_cloexec = (result & FD_CLOEXEC) ? 1 : -1;
171 if (__have_o_cloexec < 0)
172 # endif
173 result = fcntl_not_cancel (file_fd, F_SETFD,
174 result | FD_CLOEXEC);
177 if (result == -1)
179 close_not_cancel_no_status (file_fd);
180 return 0;
183 #endif
186 __lseek64 (file_fd, 0, SEEK_SET);
187 file_offset = 0;
189 /* Make sure the entry won't match. */
190 #if _HAVE_UT_TYPE - 0
191 last_entry.ut_type = -1;
192 #else
193 last_entry.ut_line[0] = '\177';
194 # if _HAVE_UT_ID - 0
195 last_entry.ut_id[0] = '\0';
196 # endif
197 #endif
199 return 1;
203 static int
204 getutent_r_file (struct utmp *buffer, struct utmp **result)
206 ssize_t nbytes;
208 assert (file_fd >= 0);
210 if (file_offset == -1l)
212 /* Not available. */
213 *result = NULL;
214 return -1;
217 LOCK_FILE (file_fd, F_RDLCK)
219 nbytes = 0;
220 LOCKING_FAILED ();
223 /* Read the next entry. */
224 nbytes = read_not_cancel (file_fd, &last_entry, sizeof (struct utmp));
226 UNLOCK_FILE (file_fd);
228 if (nbytes != sizeof (struct utmp))
230 if (nbytes != 0)
231 file_offset = -1l;
232 *result = NULL;
233 return -1;
236 /* Update position pointer. */
237 file_offset += sizeof (struct utmp);
239 memcpy (buffer, &last_entry, sizeof (struct utmp));
240 *result = buffer;
242 return 0;
246 static int
247 internal_getut_r (const struct utmp *id, struct utmp *buffer)
249 int result = -1;
251 LOCK_FILE (file_fd, F_RDLCK)
252 LOCKING_FAILED ();
254 #if _HAVE_UT_TYPE - 0
255 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
256 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
258 /* Search for next entry with type RUN_LVL, BOOT_TIME,
259 OLD_TIME, or NEW_TIME. */
261 while (1)
263 /* Read the next entry. */
264 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
265 != sizeof (struct utmp))
267 __set_errno (ESRCH);
268 file_offset = -1l;
269 goto unlock_return;
271 file_offset += sizeof (struct utmp);
273 if (id->ut_type == buffer->ut_type)
274 break;
277 else
278 #endif /* _HAVE_UT_TYPE */
280 /* Search for the next entry with the specified ID and with type
281 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
283 while (1)
285 /* Read the next entry. */
286 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
287 != sizeof (struct utmp))
289 __set_errno (ESRCH);
290 file_offset = -1l;
291 goto unlock_return;
293 file_offset += sizeof (struct utmp);
295 if (__utmp_equal (buffer, id))
296 break;
300 result = 0;
302 unlock_return:
303 UNLOCK_FILE (file_fd);
305 return result;
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. */
311 static int
312 getutid_r_file (const struct utmp *id, struct utmp *buffer,
313 struct utmp **result)
315 assert (file_fd >= 0);
317 if (file_offset == -1l)
319 *result = NULL;
320 return -1;
323 if (internal_getut_r (id, &last_entry) < 0)
325 *result = NULL;
326 return -1;
329 memcpy (buffer, &last_entry, sizeof (struct utmp));
330 *result = buffer;
332 return 0;
336 /* For implementing this function we don't use the getutent_r function
337 because we can avoid the reposition on every new entry this way. */
338 static int
339 getutline_r_file (const struct utmp *line, struct utmp *buffer,
340 struct utmp **result)
342 assert (file_fd >= 0);
344 if (file_offset == -1l)
346 *result = NULL;
347 return -1;
350 LOCK_FILE (file_fd, F_RDLCK)
352 *result = NULL;
353 LOCKING_FAILED ();
356 while (1)
358 /* Read the next entry. */
359 if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp))
360 != sizeof (struct utmp))
362 __set_errno (ESRCH);
363 file_offset = -1l;
364 *result = NULL;
365 goto unlock_return;
367 file_offset += sizeof (struct utmp);
369 /* Stop if we found a user or login entry. */
370 if (
371 #if _HAVE_UT_TYPE - 0
372 (last_entry.ut_type == USER_PROCESS
373 || last_entry.ut_type == LOGIN_PROCESS)
375 #endif
376 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
377 break;
380 memcpy (buffer, &last_entry, sizeof (struct utmp));
381 *result = buffer;
383 unlock_return:
384 UNLOCK_FILE (file_fd);
386 return ((*result == NULL) ? -1 : 0);
390 static struct utmp *
391 pututline_file (const struct utmp *data)
393 struct utmp buffer;
394 struct utmp *pbuf;
395 int found;
397 assert (file_fd >= 0);
399 /* Find the correct place to insert the data. */
400 if (file_offset > 0
401 && (
402 #if _HAVE_UT_TYPE - 0
403 (last_entry.ut_type == data->ut_type
404 && (last_entry.ut_type == RUN_LVL
405 || last_entry.ut_type == BOOT_TIME
406 || last_entry.ut_type == OLD_TIME
407 || last_entry.ut_type == NEW_TIME))
409 #endif
410 __utmp_equal (&last_entry, data)))
411 found = 1;
412 else
413 found = internal_getut_r (data, &buffer);
415 LOCK_FILE (file_fd, F_WRLCK)
417 pbuf = NULL;
418 LOCKING_FAILED ();
421 if (found < 0)
423 /* We append the next entry. */
424 file_offset = __lseek64 (file_fd, 0, SEEK_END);
425 if (file_offset % sizeof (struct utmp) != 0)
427 file_offset -= file_offset % sizeof (struct utmp);
428 __ftruncate64 (file_fd, file_offset);
430 if (__lseek64 (file_fd, 0, SEEK_END) < 0)
432 pbuf = NULL;
433 goto unlock_return;
437 else
439 /* We replace the just read entry. */
440 file_offset -= sizeof (struct utmp);
441 __lseek64 (file_fd, file_offset, SEEK_SET);
444 /* Write the new data. */
445 if (write_not_cancel (file_fd, data, sizeof (struct utmp))
446 != sizeof (struct utmp))
448 /* If we appended a new record this is only partially written.
449 Remove it. */
450 if (found < 0)
451 (void) __ftruncate64 (file_fd, file_offset);
452 pbuf = NULL;
454 else
456 file_offset += sizeof (struct utmp);
457 pbuf = (struct utmp *) data;
460 unlock_return:
461 UNLOCK_FILE (file_fd);
463 return pbuf;
467 static void
468 endutent_file (void)
470 assert (file_fd >= 0);
472 close_not_cancel_no_status (file_fd);
473 file_fd = -1;
477 static int
478 updwtmp_file (const char *file, const struct utmp *utmp)
480 int result = -1;
481 off64_t offset;
482 int fd;
484 /* Open WTMP file. */
485 fd = open_not_cancel_2 (file, O_WRONLY | O_LARGEFILE);
486 if (fd < 0)
487 return -1;
489 LOCK_FILE (fd, F_WRLCK)
490 LOCKING_FAILED ();
492 /* Remember original size of log file. */
493 offset = __lseek64 (fd, 0, SEEK_END);
494 if (offset % sizeof (struct utmp) != 0)
496 offset -= offset % sizeof (struct utmp);
497 __ftruncate64 (fd, offset);
499 if (__lseek64 (fd, 0, SEEK_END) < 0)
500 goto unlock_return;
503 /* Write the entry. If we can't write all the bytes, reset the file
504 size back to the original size. That way, no partial entries
505 will remain. */
506 if (write_not_cancel (fd, utmp, sizeof (struct utmp))
507 != sizeof (struct utmp))
509 __ftruncate64 (fd, offset);
510 goto unlock_return;
513 result = 0;
515 unlock_return:
516 UNLOCK_FILE (fd);
518 /* Close WTMP file. */
519 close_not_cancel_no_status (fd);
521 return result;