Simplify sendmmsg code.
[glibc.git] / login / utmp_file.c
blob6ebe1ef12339b485c53f61f8f3585d6de33e9da7
1 /* Copyright (C) 1996-2017 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>
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 bool file_writable;
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 10
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;
142 file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
144 file_writable = false;
145 file_fd = open_not_cancel_2
146 (file_name, O_RDONLY | O_LARGEFILE | O_CLOEXEC);
147 if (file_fd == -1)
148 return 0;
151 __lseek64 (file_fd, 0, SEEK_SET);
152 file_offset = 0;
154 /* Make sure the entry won't match. */
155 #if _HAVE_UT_TYPE - 0
156 last_entry.ut_type = -1;
157 #else
158 last_entry.ut_line[0] = '\177';
159 # if _HAVE_UT_ID - 0
160 last_entry.ut_id[0] = '\0';
161 # endif
162 #endif
164 return 1;
168 static int
169 getutent_r_file (struct utmp *buffer, struct utmp **result)
171 ssize_t nbytes;
173 assert (file_fd >= 0);
175 if (file_offset == -1l)
177 /* Not available. */
178 *result = NULL;
179 return -1;
182 LOCK_FILE (file_fd, F_RDLCK)
184 nbytes = 0;
185 LOCKING_FAILED ();
188 /* Read the next entry. */
189 nbytes = read_not_cancel (file_fd, &last_entry, sizeof (struct utmp));
191 UNLOCK_FILE (file_fd);
193 if (nbytes != sizeof (struct utmp))
195 if (nbytes != 0)
196 file_offset = -1l;
197 *result = NULL;
198 return -1;
201 /* Update position pointer. */
202 file_offset += sizeof (struct utmp);
204 memcpy (buffer, &last_entry, sizeof (struct utmp));
205 *result = buffer;
207 return 0;
211 static int
212 internal_getut_r (const struct utmp *id, struct utmp *buffer,
213 bool *lock_failed)
215 int result = -1;
217 LOCK_FILE (file_fd, F_RDLCK)
219 *lock_failed = true;
220 LOCKING_FAILED ();
223 #if _HAVE_UT_TYPE - 0
224 if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME
225 || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME)
227 /* Search for next entry with type RUN_LVL, BOOT_TIME,
228 OLD_TIME, or NEW_TIME. */
230 while (1)
232 /* Read the next entry. */
233 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
234 != sizeof (struct utmp))
236 __set_errno (ESRCH);
237 file_offset = -1l;
238 goto unlock_return;
240 file_offset += sizeof (struct utmp);
242 if (id->ut_type == buffer->ut_type)
243 break;
246 else
247 #endif /* _HAVE_UT_TYPE */
249 /* Search for the next entry with the specified ID and with type
250 INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */
252 while (1)
254 /* Read the next entry. */
255 if (read_not_cancel (file_fd, buffer, sizeof (struct utmp))
256 != sizeof (struct utmp))
258 __set_errno (ESRCH);
259 file_offset = -1l;
260 goto unlock_return;
262 file_offset += sizeof (struct utmp);
264 if (__utmp_equal (buffer, id))
265 break;
269 result = 0;
271 unlock_return:
272 UNLOCK_FILE (file_fd);
274 return result;
278 /* For implementing this function we don't use the getutent_r function
279 because we can avoid the reposition on every new entry this way. */
280 static int
281 getutid_r_file (const struct utmp *id, struct utmp *buffer,
282 struct utmp **result)
284 assert (file_fd >= 0);
286 if (file_offset == -1l)
288 *result = NULL;
289 return -1;
292 /* We don't have to distinguish whether we can lock the file or
293 whether there is no entry. */
294 bool lock_failed = false;
295 if (internal_getut_r (id, &last_entry, &lock_failed) < 0)
297 *result = NULL;
298 return -1;
301 memcpy (buffer, &last_entry, sizeof (struct utmp));
302 *result = buffer;
304 return 0;
308 /* For implementing this function we don't use the getutent_r function
309 because we can avoid the reposition on every new entry this way. */
310 static int
311 getutline_r_file (const struct utmp *line, struct utmp *buffer,
312 struct utmp **result)
314 assert (file_fd >= 0);
316 if (file_offset == -1l)
318 *result = NULL;
319 return -1;
322 LOCK_FILE (file_fd, F_RDLCK)
324 *result = NULL;
325 LOCKING_FAILED ();
328 while (1)
330 /* Read the next entry. */
331 if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp))
332 != sizeof (struct utmp))
334 __set_errno (ESRCH);
335 file_offset = -1l;
336 *result = NULL;
337 goto unlock_return;
339 file_offset += sizeof (struct utmp);
341 /* Stop if we found a user or login entry. */
342 if (
343 #if _HAVE_UT_TYPE - 0
344 (last_entry.ut_type == USER_PROCESS
345 || last_entry.ut_type == LOGIN_PROCESS)
347 #endif
348 !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line))
349 break;
352 memcpy (buffer, &last_entry, sizeof (struct utmp));
353 *result = buffer;
355 unlock_return:
356 UNLOCK_FILE (file_fd);
358 return ((*result == NULL) ? -1 : 0);
362 static struct utmp *
363 pututline_file (const struct utmp *data)
365 struct utmp buffer;
366 struct utmp *pbuf;
367 int found;
369 assert (file_fd >= 0);
371 if (! file_writable)
373 /* We must make the file descriptor writable before going on. */
374 const char *file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name);
376 int new_fd = open_not_cancel_2
377 (file_name, O_RDWR | O_LARGEFILE | O_CLOEXEC);
378 if (new_fd == -1)
379 return NULL;
381 if (__lseek64 (new_fd, __lseek64 (file_fd, 0, SEEK_CUR), SEEK_SET) == -1
382 || __dup2 (new_fd, file_fd) < 0)
384 close_not_cancel_no_status (new_fd);
385 return NULL;
387 close_not_cancel_no_status (new_fd);
388 file_writable = true;
391 /* Find the correct place to insert the data. */
392 if (file_offset > 0
393 && (
394 #if _HAVE_UT_TYPE - 0
395 (last_entry.ut_type == data->ut_type
396 && (last_entry.ut_type == RUN_LVL
397 || last_entry.ut_type == BOOT_TIME
398 || last_entry.ut_type == OLD_TIME
399 || last_entry.ut_type == NEW_TIME))
401 #endif
402 __utmp_equal (&last_entry, data)))
403 found = 1;
404 else
406 bool lock_failed = false;
407 found = internal_getut_r (data, &buffer, &lock_failed);
409 if (__builtin_expect (lock_failed, false))
411 __set_errno (EAGAIN);
412 return NULL;
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;