Add glibc.malloc.mxfast tunable
[glibc.git] / login / utmp_file.c
blobcb8a02825cb5b2d2df969c3a3d0f575f77634fe9
1 /* Copyright (C) 1996-2019 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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <signal.h>
24 #include <stdbool.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>
31 #include <sigsetops.h>
32 #include <not-cancel.h>
34 #include "utmp-private.h"
35 #include "utmp-equal.h"
38 /* Descriptor for the file and position. */
39 static int file_fd = -1;
40 static bool file_writable;
41 static off64_t file_offset;
43 /* Cache for the last read entry. */
44 static struct utmp last_entry;
47 /* Locking timeout. */
48 #ifndef TIMEOUT
49 # define TIMEOUT 10
50 #endif
52 /* Do-nothing handler for locking timeout. */
53 static void timeout_handler (int signum) {};
55 /* LOCK_FILE(fd, type) failure_statement
56 attempts to get a lock on the utmp file referenced by FD. If it fails,
57 the failure_statement is executed, otherwise it is skipped.
58 LOCKING_FAILED()
59 jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE.
60 UNLOCK_FILE(fd)
61 unlocks the utmp file referenced by FD and performs the cleanup of
62 LOCK_FILE.
64 #define LOCK_FILE(fd, type) \
65 { \
66 struct flock fl; \
67 struct sigaction action, old_action; \
68 unsigned int old_timeout; \
70 /* Cancel any existing alarm. */ \
71 old_timeout = alarm (0); \
73 /* Establish signal handler. */ \
74 action.sa_handler = timeout_handler; \
75 __sigemptyset (&action.sa_mask); \
76 action.sa_flags = 0; \
77 __sigaction (SIGALRM, &action, &old_action); \
79 alarm (TIMEOUT); \
81 /* Try to get the lock. */ \
82 memset (&fl, '\0', sizeof (struct flock)); \
83 fl.l_type = (type); \
84 fl.l_whence = SEEK_SET; \
85 if (__fcntl64_nocancel ((fd), F_SETLKW, &fl) < 0)
87 #define LOCKING_FAILED() \
88 goto unalarm_return
90 #define UNLOCK_FILE(fd) \
91 /* Unlock the file. */ \
92 fl.l_type = F_UNLCK; \
93 __fcntl64_nocancel ((fd), F_SETLKW, &fl); \
95 unalarm_return: \
96 /* Reset the signal handler and alarm. We must reset the alarm \
97 before resetting the handler so our alarm does not generate a \
98 spurious SIGALRM seen by the user. However, we cannot just set \
99 the user's old alarm before restoring the handler, because then \
100 it's possible our handler could catch the user alarm's SIGARLM \
101 and then the user would never see the signal he expected. */ \
102 alarm (0); \
103 __sigaction (SIGALRM, &old_action, NULL); \
104 if (old_timeout != 0) \
105 alarm (old_timeout); \
106 } while (0)
108 #ifndef TRANSFORM_UTMP_FILE_NAME
109 # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name)
110 #endif
113 __libc_setutent (void)
115 if (file_fd < 0)
117 const char *file_name;
119 file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
121 file_writable = false;
122 file_fd = __open_nocancel
123 (file_name, O_RDONLY | O_LARGEFILE | O_CLOEXEC);
124 if (file_fd == -1)
125 return 0;
128 __lseek64 (file_fd, 0, SEEK_SET);
129 file_offset = 0;
131 /* Make sure the entry won't match. */
132 #if _HAVE_UT_TYPE - 0
133 last_entry.ut_type = -1;
134 #else
135 last_entry.ut_line[0] = '\177';
136 # if _HAVE_UT_ID - 0
137 last_entry.ut_id[0] = '\0';
138 # endif
139 #endif
141 return 1;
144 /* Preform initialization if necessary. */
145 static bool
146 maybe_setutent (void)
148 return file_fd >= 0 || __libc_setutent ();
152 __libc_getutent_r (struct utmp *buffer, struct utmp **result)
154 ssize_t nbytes;
156 if (!maybe_setutent () || file_offset == -1l)
158 /* Not available. */
159 *result = NULL;
160 return -1;
163 LOCK_FILE (file_fd, F_RDLCK)
165 nbytes = 0;
166 LOCKING_FAILED ();
169 /* Read the next entry. */
170 nbytes = __read_nocancel (file_fd, &last_entry, sizeof (struct utmp));
172 UNLOCK_FILE (file_fd);
174 if (nbytes != sizeof (struct utmp))
176 if (nbytes != 0)
177 file_offset = -1l;
178 *result = NULL;
179 return -1;
182 /* Update position pointer. */
183 file_offset += sizeof (struct utmp);
185 memcpy (buffer, &last_entry, sizeof (struct utmp));
186 *result = buffer;
188 return 0;
192 static int
193 internal_getut_r (const struct utmp *id, struct utmp *buffer,
194 bool *lock_failed)
196 int result = -1;
198 LOCK_FILE (file_fd, F_RDLCK)
200 *lock_failed = true;
201 LOCKING_FAILED ();
204 #if _HAVE_UT_TYPE - 0
205 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
206 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
208 /* Search for next entry with type RUN_LVL, BOOT_TIME,
209 OLD_TIME, or NEW_TIME. */
211 while (1)
213 /* Read the next entry. */
214 if (__read_nocancel (file_fd, buffer, sizeof (struct utmp))
215 != sizeof (struct utmp))
217 __set_errno (ESRCH);
218 file_offset = -1l;
219 goto unlock_return;
221 file_offset += sizeof (struct utmp);
223 if (id->ut_type == buffer->ut_type)
224 break;
227 else
228 #endif /* _HAVE_UT_TYPE */
230 /* Search for the next entry with the specified ID and with type
231 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
233 while (1)
235 /* Read the next entry. */
236 if (__read_nocancel (file_fd, buffer, sizeof (struct utmp))
237 != sizeof (struct utmp))
239 __set_errno (ESRCH);
240 file_offset = -1l;
241 goto unlock_return;
243 file_offset += sizeof (struct utmp);
245 if (__utmp_equal (buffer, id))
246 break;
250 result = 0;
252 unlock_return:
253 UNLOCK_FILE (file_fd);
255 return result;
259 /* For implementing this function we don't use the getutent_r function
260 because we can avoid the reposition on every new entry this way. */
262 __libc_getutid_r (const struct utmp *id, struct utmp *buffer,
263 struct utmp **result)
265 if (!maybe_setutent () || file_offset == -1l)
267 *result = NULL;
268 return -1;
271 /* We don't have to distinguish whether we can lock the file or
272 whether there is no entry. */
273 bool lock_failed = false;
274 if (internal_getut_r (id, &last_entry, &lock_failed) < 0)
276 *result = NULL;
277 return -1;
280 memcpy (buffer, &last_entry, sizeof (struct utmp));
281 *result = buffer;
283 return 0;
287 /* For implementing this function we don't use the getutent_r function
288 because we can avoid the reposition on every new entry this way. */
290 __libc_getutline_r (const struct utmp *line, struct utmp *buffer,
291 struct utmp **result)
293 if (!maybe_setutent () || file_offset == -1l)
295 *result = NULL;
296 return -1;
299 LOCK_FILE (file_fd, F_RDLCK)
301 *result = NULL;
302 LOCKING_FAILED ();
305 while (1)
307 /* Read the next entry. */
308 if (__read_nocancel (file_fd, &last_entry, sizeof (struct utmp))
309 != sizeof (struct utmp))
311 __set_errno (ESRCH);
312 file_offset = -1l;
313 *result = NULL;
314 goto unlock_return;
316 file_offset += sizeof (struct utmp);
318 /* Stop if we found a user or login entry. */
319 if (
320 #if _HAVE_UT_TYPE - 0
321 (last_entry.ut_type == USER_PROCESS
322 || last_entry.ut_type == LOGIN_PROCESS)
324 #endif
325 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
326 break;
329 memcpy (buffer, &last_entry, sizeof (struct utmp));
330 *result = buffer;
332 unlock_return:
333 UNLOCK_FILE (file_fd);
335 return ((*result == NULL) ? -1 : 0);
339 struct utmp *
340 __libc_pututline (const struct utmp *data)
342 if (!maybe_setutent ())
343 return NULL;
345 struct utmp buffer;
346 struct utmp *pbuf;
347 int found;
349 if (! file_writable)
351 /* We must make the file descriptor writable before going on. */
352 const char *file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
354 int new_fd = __open_nocancel
355 (file_name, O_RDWR | O_LARGEFILE | O_CLOEXEC);
356 if (new_fd == -1)
357 return NULL;
359 if (__lseek64 (new_fd, __lseek64 (file_fd, 0, SEEK_CUR), SEEK_SET) == -1
360 || __dup2 (new_fd, file_fd) < 0)
362 __close_nocancel_nostatus (new_fd);
363 return NULL;
365 __close_nocancel_nostatus (new_fd);
366 file_writable = true;
369 /* Find the correct place to insert the data. */
370 if (file_offset > 0
371 && (
372 #if _HAVE_UT_TYPE - 0
373 (last_entry.ut_type == data->ut_type
374 && (last_entry.ut_type == RUN_LVL
375 || last_entry.ut_type == BOOT_TIME
376 || last_entry.ut_type == OLD_TIME
377 || last_entry.ut_type == NEW_TIME))
379 #endif
380 __utmp_equal (&last_entry, data)))
381 found = 1;
382 else
384 bool lock_failed = false;
385 found = internal_getut_r (data, &buffer, &lock_failed);
387 if (__builtin_expect (lock_failed, false))
389 __set_errno (EAGAIN);
390 return NULL;
394 LOCK_FILE (file_fd, F_WRLCK)
396 pbuf = NULL;
397 LOCKING_FAILED ();
400 if (found < 0)
402 /* We append the next entry. */
403 file_offset = __lseek64 (file_fd, 0, SEEK_END);
404 if (file_offset % sizeof (struct utmp) != 0)
406 file_offset -= file_offset % sizeof (struct utmp);
407 __ftruncate64 (file_fd, file_offset);
409 if (__lseek64 (file_fd, 0, SEEK_END) < 0)
411 pbuf = NULL;
412 goto unlock_return;
416 else
418 /* We replace the just read entry. */
419 file_offset -= sizeof (struct utmp);
420 __lseek64 (file_fd, file_offset, SEEK_SET);
423 /* Write the new data. */
424 if (__write_nocancel (file_fd, data, sizeof (struct utmp))
425 != sizeof (struct utmp))
427 /* If we appended a new record this is only partially written.
428 Remove it. */
429 if (found < 0)
430 (void) __ftruncate64 (file_fd, file_offset);
431 pbuf = NULL;
433 else
435 file_offset += sizeof (struct utmp);
436 pbuf = (struct utmp *) data;
439 unlock_return:
440 UNLOCK_FILE (file_fd);
442 return pbuf;
446 void
447 __libc_endutent (void)
449 if (file_fd >= 0)
451 __close_nocancel_nostatus (file_fd);
452 file_fd = -1;
458 __libc_updwtmp (const char *file, const struct utmp *utmp)
460 int result = -1;
461 off64_t offset;
462 int fd;
464 /* Open WTMP file. */
465 fd = __open_nocancel (file, O_WRONLY | O_LARGEFILE);
466 if (fd < 0)
467 return -1;
469 LOCK_FILE (fd, F_WRLCK)
470 LOCKING_FAILED ();
472 /* Remember original size of log file. */
473 offset = __lseek64 (fd, 0, SEEK_END);
474 if (offset % sizeof (struct utmp) != 0)
476 offset -= offset % sizeof (struct utmp);
477 __ftruncate64 (fd, offset);
479 if (__lseek64 (fd, 0, SEEK_END) < 0)
480 goto unlock_return;
483 /* Write the entry. If we can't write all the bytes, reset the file
484 size back to the original size. That way, no partial entries
485 will remain. */
486 if (__write_nocancel (fd, utmp, sizeof (struct utmp))
487 != sizeof (struct utmp))
489 __ftruncate64 (fd, offset);
490 goto unlock_return;
493 result = 0;
495 unlock_return:
496 UNLOCK_FILE (fd);
498 /* Close WTMP file. */
499 __close_nocancel_nostatus (fd);
501 return result;