]>
Commit | Line | Data |
---|---|---|
bfff8b1b | 1 | /* Copyright (C) 1996-2017 Free Software Foundation, Inc. |
8a523922 UD |
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. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
8a523922 UD |
10 | |
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 | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
8a523922 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
8a523922 | 19 | |
0413b54c | 20 | #include <assert.h> |
8a523922 UD |
21 | #include <errno.h> |
22 | #include <fcntl.h> | |
efea7158 | 23 | #include <signal.h> |
1bfa05cf | 24 | #include <stdbool.h> |
8a523922 | 25 | #include <stdio.h> |
8a523922 UD |
26 | #include <string.h> |
27 | #include <unistd.h> | |
28 | #include <utmp.h> | |
ce42435c | 29 | #include <not-cancel.h> |
c942388d | 30 | #include <kernel-features.h> |
a992f506 | 31 | #include <sigsetops.h> |
8a523922 UD |
32 | |
33 | #include "utmp-private.h" | |
82c26126 | 34 | #include "utmp-equal.h" |
8a523922 | 35 | |
8a523922 UD |
36 | |
37 | /* Descriptor for the file and position. */ | |
0413b54c | 38 | static int file_fd = -1; |
37a6a271 | 39 | static bool file_writable; |
2958e6cc | 40 | static off64_t file_offset; |
8a523922 | 41 | |
0413b54c | 42 | /* Cache for the last read entry. */ |
8a523922 UD |
43 | static struct utmp last_entry; |
44 | ||
76b87c03 | 45 | |
efea7158 UD |
46 | /* Locking timeout. */ |
47 | #ifndef TIMEOUT | |
5debe363 | 48 | # define TIMEOUT 10 |
efea7158 UD |
49 | #endif |
50 | ||
51 | /* Do-nothing handler for locking timeout. */ | |
52 | static void timeout_handler (int signum) {}; | |
53 | ||
e3b0b8ba UD |
54 | /* LOCK_FILE(fd, type) failure_statement |
55 | attempts to get a lock on the utmp file referenced by FD. If it fails, | |
56 | the failure_statement is executed, otherwise it is skipped. | |
57 | LOCKING_FAILED() | |
58 | jumps into the UNLOCK_FILE macro and ensures cleanup of LOCK_FILE. | |
59 | UNLOCK_FILE(fd) | |
60 | unlocks the utmp file referenced by FD and performs the cleanup of | |
61 | LOCK_FILE. | |
62 | */ | |
efea7158 | 63 | #define LOCK_FILE(fd, type) \ |
ce42435c UD |
64 | { \ |
65 | struct flock fl; \ | |
66 | struct sigaction action, old_action; \ | |
67 | unsigned int old_timeout; \ | |
68 | \ | |
69 | /* Cancel any existing alarm. */ \ | |
70 | old_timeout = alarm (0); \ | |
71 | \ | |
72 | /* Establish signal handler. */ \ | |
73 | action.sa_handler = timeout_handler; \ | |
74 | __sigemptyset (&action.sa_mask); \ | |
75 | action.sa_flags = 0; \ | |
76 | __sigaction (SIGALRM, &action, &old_action); \ | |
77 | \ | |
78 | alarm (TIMEOUT); \ | |
79 | \ | |
80 | /* Try to get the lock. */ \ | |
81 | memset (&fl, '\0', sizeof (struct flock)); \ | |
82 | fl.l_type = (type); \ | |
83 | fl.l_whence = SEEK_SET; \ | |
6a8a852b | 84 | if (fcntl_not_cancel ((fd), F_SETLKW, &fl) < 0) |
efea7158 | 85 | |
e3b0b8ba UD |
86 | #define LOCKING_FAILED() \ |
87 | goto unalarm_return | |
88 | ||
efea7158 | 89 | #define UNLOCK_FILE(fd) \ |
ce42435c UD |
90 | /* Unlock the file. */ \ |
91 | fl.l_type = F_UNLCK; \ | |
6a8a852b | 92 | fcntl_not_cancel ((fd), F_SETLKW, &fl); \ |
ce42435c UD |
93 | \ |
94 | unalarm_return: \ | |
95 | /* Reset the signal handler and alarm. We must reset the alarm \ | |
96 | before resetting the handler so our alarm does not generate a \ | |
97 | spurious SIGALRM seen by the user. However, we cannot just set \ | |
98 | the user's old alarm before restoring the handler, because then \ | |
99 | it's possible our handler could catch the user alarm's SIGARLM \ | |
100 | and then the user would never see the signal he expected. */ \ | |
101 | alarm (0); \ | |
102 | __sigaction (SIGALRM, &old_action, NULL); \ | |
103 | if (old_timeout != 0) \ | |
104 | alarm (old_timeout); \ | |
efea7158 UD |
105 | } while (0) |
106 | ||
107 | ||
8a523922 | 108 | /* Functions defined here. */ |
8f2ece69 | 109 | static int setutent_file (void); |
8a523922 UD |
110 | static int getutent_r_file (struct utmp *buffer, struct utmp **result); |
111 | static int getutid_r_file (const struct utmp *key, struct utmp *buffer, | |
112 | struct utmp **result); | |
113 | static int getutline_r_file (const struct utmp *key, struct utmp *buffer, | |
114 | struct utmp **result); | |
115 | static struct utmp *pututline_file (const struct utmp *data); | |
116 | static void endutent_file (void); | |
76b87c03 | 117 | static int updwtmp_file (const char *file, const struct utmp *utmp); |
8a523922 UD |
118 | |
119 | /* Jump table for file functions. */ | |
b2637a22 | 120 | const struct utfuncs __libc_utmp_file_functions = |
8a523922 UD |
121 | { |
122 | setutent_file, | |
123 | getutent_r_file, | |
124 | getutid_r_file, | |
125 | getutline_r_file, | |
126 | pututline_file, | |
127 | endutent_file, | |
76b87c03 | 128 | updwtmp_file |
8a523922 UD |
129 | }; |
130 | ||
131 | ||
8619129f UD |
132 | #ifndef TRANSFORM_UTMP_FILE_NAME |
133 | # define TRANSFORM_UTMP_FILE_NAME(file_name) (file_name) | |
134 | #endif | |
135 | ||
8a523922 | 136 | static int |
8f2ece69 | 137 | setutent_file (void) |
8a523922 | 138 | { |
8f2ece69 | 139 | if (file_fd < 0) |
8a523922 | 140 | { |
8619129f | 141 | const char *file_name; |
0413b54c | 142 | |
8619129f | 143 | file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name); |
0413b54c | 144 | |
37a6a271 | 145 | file_writable = false; |
cef9b653 FW |
146 | file_fd = open_not_cancel_2 |
147 | (file_name, O_RDONLY | O_LARGEFILE | O_CLOEXEC); | |
8a523922 | 148 | if (file_fd == -1) |
37a6a271 | 149 | return 0; |
8a523922 | 150 | } |
68dbb3a6 | 151 | |
2958e6cc | 152 | __lseek64 (file_fd, 0, SEEK_SET); |
8f2ece69 | 153 | file_offset = 0; |
8a523922 | 154 | |
8f2ece69 | 155 | /* Make sure the entry won't match. */ |
82c26126 | 156 | #if _HAVE_UT_TYPE - 0 |
8f2ece69 | 157 | last_entry.ut_type = -1; |
82c26126 RM |
158 | #else |
159 | last_entry.ut_line[0] = '\177'; | |
160 | # if _HAVE_UT_ID - 0 | |
161 | last_entry.ut_id[0] = '\0'; | |
162 | # endif | |
4cca6b86 | 163 | #endif |
8a523922 | 164 | |
0413b54c | 165 | return 1; |
8a523922 UD |
166 | } |
167 | ||
168 | ||
169 | static int | |
170 | getutent_r_file (struct utmp *buffer, struct utmp **result) | |
171 | { | |
d705269e | 172 | ssize_t nbytes; |
8a523922 | 173 | |
0413b54c | 174 | assert (file_fd >= 0); |
8a523922 | 175 | |
0413b54c | 176 | if (file_offset == -1l) |
8a523922 UD |
177 | { |
178 | /* Not available. */ | |
179 | *result = NULL; | |
180 | return -1; | |
181 | } | |
182 | ||
52c82852 RM |
183 | LOCK_FILE (file_fd, F_RDLCK) |
184 | { | |
e3b0b8ba UD |
185 | nbytes = 0; |
186 | LOCKING_FAILED (); | |
52c82852 | 187 | } |
68dbb3a6 | 188 | |
8a523922 | 189 | /* Read the next entry. */ |
ce42435c | 190 | nbytes = read_not_cancel (file_fd, &last_entry, sizeof (struct utmp)); |
68dbb3a6 | 191 | |
efea7158 | 192 | UNLOCK_FILE (file_fd); |
8a523922 | 193 | |
8a4b65b4 | 194 | if (nbytes != sizeof (struct utmp)) |
8a523922 | 195 | { |
e3b0b8ba UD |
196 | if (nbytes != 0) |
197 | file_offset = -1l; | |
8a523922 UD |
198 | *result = NULL; |
199 | return -1; | |
200 | } | |
201 | ||
202 | /* Update position pointer. */ | |
203 | file_offset += sizeof (struct utmp); | |
204 | ||
205 | memcpy (buffer, &last_entry, sizeof (struct utmp)); | |
206 | *result = buffer; | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | ||
ced858d0 | 212 | static int |
1bfa05cf UD |
213 | internal_getut_r (const struct utmp *id, struct utmp *buffer, |
214 | bool *lock_failed) | |
8a523922 | 215 | { |
d705269e | 216 | int result = -1; |
f21acc89 | 217 | |
52c82852 | 218 | LOCK_FILE (file_fd, F_RDLCK) |
1bfa05cf UD |
219 | { |
220 | *lock_failed = true; | |
221 | LOCKING_FAILED (); | |
222 | } | |
d705269e | 223 | |
4cca6b86 | 224 | #if _HAVE_UT_TYPE - 0 |
8a523922 UD |
225 | if (id->ut_type == RUN_LVL || id->ut_type == BOOT_TIME |
226 | || id->ut_type == OLD_TIME || id->ut_type == NEW_TIME) | |
227 | { | |
228 | /* Search for next entry with type RUN_LVL, BOOT_TIME, | |
229 | OLD_TIME, or NEW_TIME. */ | |
230 | ||
231 | while (1) | |
232 | { | |
233 | /* Read the next entry. */ | |
ce42435c | 234 | if (read_not_cancel (file_fd, buffer, sizeof (struct utmp)) |
8a523922 UD |
235 | != sizeof (struct utmp)) |
236 | { | |
237 | __set_errno (ESRCH); | |
238 | file_offset = -1l; | |
d705269e | 239 | goto unlock_return; |
8a523922 | 240 | } |
8a4b65b4 | 241 | file_offset += sizeof (struct utmp); |
8a523922 UD |
242 | |
243 | if (id->ut_type == buffer->ut_type) | |
244 | break; | |
8a523922 UD |
245 | } |
246 | } | |
247 | else | |
4cca6b86 | 248 | #endif /* _HAVE_UT_TYPE */ |
8a523922 UD |
249 | { |
250 | /* Search for the next entry with the specified ID and with type | |
251 | INIT_PROCESS, LOGIN_PROCESS, USER_PROCESS, or DEAD_PROCESS. */ | |
252 | ||
253 | while (1) | |
254 | { | |
255 | /* Read the next entry. */ | |
ce42435c | 256 | if (read_not_cancel (file_fd, buffer, sizeof (struct utmp)) |
8a523922 UD |
257 | != sizeof (struct utmp)) |
258 | { | |
259 | __set_errno (ESRCH); | |
260 | file_offset = -1l; | |
d705269e | 261 | goto unlock_return; |
8a523922 | 262 | } |
8a4b65b4 UD |
263 | file_offset += sizeof (struct utmp); |
264 | ||
82c26126 | 265 | if (__utmp_equal (buffer, id)) |
8a523922 | 266 | break; |
8a523922 UD |
267 | } |
268 | } | |
269 | ||
d705269e UD |
270 | result = 0; |
271 | ||
272 | unlock_return: | |
efea7158 | 273 | UNLOCK_FILE (file_fd); |
d705269e UD |
274 | |
275 | return result; | |
8a523922 UD |
276 | } |
277 | ||
278 | ||
279 | /* For implementing this function we don't use the getutent_r function | |
280 | because we can avoid the reposition on every new entry this way. */ | |
281 | static int | |
282 | getutid_r_file (const struct utmp *id, struct utmp *buffer, | |
283 | struct utmp **result) | |
284 | { | |
0413b54c UD |
285 | assert (file_fd >= 0); |
286 | ||
287 | if (file_offset == -1l) | |
8a523922 UD |
288 | { |
289 | *result = NULL; | |
290 | return -1; | |
291 | } | |
292 | ||
1bfa05cf UD |
293 | /* We don't have to distinguish whether we can lock the file or |
294 | whether there is no entry. */ | |
295 | bool lock_failed = false; | |
296 | if (internal_getut_r (id, &last_entry, &lock_failed) < 0) | |
8a523922 UD |
297 | { |
298 | *result = NULL; | |
299 | return -1; | |
300 | } | |
301 | ||
302 | memcpy (buffer, &last_entry, sizeof (struct utmp)); | |
303 | *result = buffer; | |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
308 | ||
0413b54c UD |
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 | getutline_r_file (const struct utmp *line, struct utmp *buffer, | |
313 | struct utmp **result) | |
314 | { | |
0413b54c UD |
315 | assert (file_fd >= 0); |
316 | ||
317 | if (file_offset == -1l) | |
318 | { | |
319 | *result = NULL; | |
320 | return -1; | |
321 | } | |
322 | ||
52c82852 RM |
323 | LOCK_FILE (file_fd, F_RDLCK) |
324 | { | |
325 | *result = NULL; | |
e3b0b8ba | 326 | LOCKING_FAILED (); |
52c82852 | 327 | } |
0413b54c UD |
328 | |
329 | while (1) | |
330 | { | |
331 | /* Read the next entry. */ | |
ce42435c | 332 | if (read_not_cancel (file_fd, &last_entry, sizeof (struct utmp)) |
0413b54c UD |
333 | != sizeof (struct utmp)) |
334 | { | |
335 | __set_errno (ESRCH); | |
336 | file_offset = -1l; | |
337 | *result = NULL; | |
338 | goto unlock_return; | |
339 | } | |
340 | file_offset += sizeof (struct utmp); | |
341 | ||
342 | /* Stop if we found a user or login entry. */ | |
343 | if ( | |
344 | #if _HAVE_UT_TYPE - 0 | |
345 | (last_entry.ut_type == USER_PROCESS | |
346 | || last_entry.ut_type == LOGIN_PROCESS) | |
347 | && | |
348 | #endif | |
349 | !strncmp (line->ut_line, last_entry.ut_line, sizeof line->ut_line)) | |
350 | break; | |
351 | } | |
352 | ||
353 | memcpy (buffer, &last_entry, sizeof (struct utmp)); | |
354 | *result = buffer; | |
355 | ||
356 | unlock_return: | |
efea7158 | 357 | UNLOCK_FILE (file_fd); |
0413b54c UD |
358 | |
359 | return ((*result == NULL) ? -1 : 0); | |
360 | } | |
361 | ||
362 | ||
8a523922 UD |
363 | static struct utmp * |
364 | pututline_file (const struct utmp *data) | |
365 | { | |
366 | struct utmp buffer; | |
367 | struct utmp *pbuf; | |
368 | int found; | |
369 | ||
0413b54c | 370 | assert (file_fd >= 0); |
8a523922 | 371 | |
37a6a271 UD |
372 | if (! file_writable) |
373 | { | |
374 | /* We must make the file descriptor writable before going on. */ | |
375 | const char *file_name = TRANSFORM_UTMP_FILE_NAME (__libc_utmp_file_name); | |
376 | ||
cef9b653 FW |
377 | int new_fd = open_not_cancel_2 |
378 | (file_name, O_RDWR | O_LARGEFILE | O_CLOEXEC); | |
37a6a271 UD |
379 | if (new_fd == -1) |
380 | return NULL; | |
381 | ||
37a6a271 | 382 | if (__lseek64 (new_fd, __lseek64 (file_fd, 0, SEEK_CUR), SEEK_SET) == -1 |
49159e12 | 383 | || __dup2 (new_fd, file_fd) < 0) |
37a6a271 UD |
384 | { |
385 | close_not_cancel_no_status (new_fd); | |
386 | return NULL; | |
387 | } | |
388 | close_not_cancel_no_status (new_fd); | |
389 | file_writable = true; | |
390 | } | |
391 | ||
8a523922 | 392 | /* Find the correct place to insert the data. */ |
ced858d0 | 393 | if (file_offset > 0 |
4cca6b86 UD |
394 | && ( |
395 | #if _HAVE_UT_TYPE - 0 | |
396 | (last_entry.ut_type == data->ut_type | |
ced858d0 MB |
397 | && (last_entry.ut_type == RUN_LVL |
398 | || last_entry.ut_type == BOOT_TIME | |
399 | || last_entry.ut_type == OLD_TIME | |
400 | || last_entry.ut_type == NEW_TIME)) | |
4cca6b86 UD |
401 | || |
402 | #endif | |
82c26126 | 403 | __utmp_equal (&last_entry, data))) |
ced858d0 | 404 | found = 1; |
8a523922 | 405 | else |
1bfa05cf UD |
406 | { |
407 | bool lock_failed = false; | |
408 | found = internal_getut_r (data, &buffer, &lock_failed); | |
409 | ||
410 | if (__builtin_expect (lock_failed, false)) | |
411 | { | |
412 | __set_errno (EAGAIN); | |
413 | return NULL; | |
414 | } | |
415 | } | |
8a523922 | 416 | |
52c82852 | 417 | LOCK_FILE (file_fd, F_WRLCK) |
e3b0b8ba UD |
418 | { |
419 | pbuf = NULL; | |
420 | LOCKING_FAILED (); | |
421 | } | |
8a523922 UD |
422 | |
423 | if (found < 0) | |
424 | { | |
425 | /* We append the next entry. */ | |
2958e6cc | 426 | file_offset = __lseek64 (file_fd, 0, SEEK_END); |
8a523922 UD |
427 | if (file_offset % sizeof (struct utmp) != 0) |
428 | { | |
429 | file_offset -= file_offset % sizeof (struct utmp); | |
2958e6cc | 430 | __ftruncate64 (file_fd, file_offset); |
8a523922 | 431 | |
2958e6cc | 432 | if (__lseek64 (file_fd, 0, SEEK_END) < 0) |
8a523922 | 433 | { |
860d3729 UD |
434 | pbuf = NULL; |
435 | goto unlock_return; | |
8a523922 UD |
436 | } |
437 | } | |
438 | } | |
439 | else | |
440 | { | |
441 | /* We replace the just read entry. */ | |
442 | file_offset -= sizeof (struct utmp); | |
2958e6cc | 443 | __lseek64 (file_fd, file_offset, SEEK_SET); |
8a523922 UD |
444 | } |
445 | ||
446 | /* Write the new data. */ | |
ce42435c UD |
447 | if (write_not_cancel (file_fd, data, sizeof (struct utmp)) |
448 | != sizeof (struct utmp)) | |
6591c335 | 449 | { |
8a523922 UD |
450 | /* If we appended a new record this is only partially written. |
451 | Remove it. */ | |
6591c335 | 452 | if (found < 0) |
2958e6cc | 453 | (void) __ftruncate64 (file_fd, file_offset); |
8a523922 UD |
454 | pbuf = NULL; |
455 | } | |
456 | else | |
8a4b65b4 UD |
457 | { |
458 | file_offset += sizeof (struct utmp); | |
459 | pbuf = (struct utmp *) data; | |
460 | } | |
8a523922 | 461 | |
860d3729 | 462 | unlock_return: |
efea7158 | 463 | UNLOCK_FILE (file_fd); |
8a523922 UD |
464 | |
465 | return pbuf; | |
466 | } | |
467 | ||
468 | ||
0413b54c UD |
469 | static void |
470 | endutent_file (void) | |
471 | { | |
472 | assert (file_fd >= 0); | |
473 | ||
ce42435c | 474 | close_not_cancel_no_status (file_fd); |
0413b54c UD |
475 | file_fd = -1; |
476 | } | |
477 | ||
478 | ||
8a523922 | 479 | static int |
76b87c03 | 480 | updwtmp_file (const char *file, const struct utmp *utmp) |
8a523922 | 481 | { |
76b87c03 | 482 | int result = -1; |
2958e6cc | 483 | off64_t offset; |
76b87c03 | 484 | int fd; |
f21acc89 | 485 | |
76b87c03 | 486 | /* Open WTMP file. */ |
05eb8975 | 487 | fd = open_not_cancel_2 (file, O_WRONLY | O_LARGEFILE); |
76b87c03 UD |
488 | if (fd < 0) |
489 | return -1; | |
490 | ||
52c82852 | 491 | LOCK_FILE (fd, F_WRLCK) |
e3b0b8ba | 492 | LOCKING_FAILED (); |
f21acc89 | 493 | |
d705269e | 494 | /* Remember original size of log file. */ |
2958e6cc | 495 | offset = __lseek64 (fd, 0, SEEK_END); |
d705269e | 496 | if (offset % sizeof (struct utmp) != 0) |
8a523922 | 497 | { |
d705269e | 498 | offset -= offset % sizeof (struct utmp); |
2958e6cc | 499 | __ftruncate64 (fd, offset); |
8a523922 | 500 | |
2958e6cc | 501 | if (__lseek64 (fd, 0, SEEK_END) < 0) |
d705269e | 502 | goto unlock_return; |
76b87c03 | 503 | } |
8a523922 | 504 | |
76b87c03 UD |
505 | /* Write the entry. If we can't write all the bytes, reset the file |
506 | size back to the original size. That way, no partial entries | |
507 | will remain. */ | |
ce42435c UD |
508 | if (write_not_cancel (fd, utmp, sizeof (struct utmp)) |
509 | != sizeof (struct utmp)) | |
76b87c03 | 510 | { |
2958e6cc | 511 | __ftruncate64 (fd, offset); |
d705269e | 512 | goto unlock_return; |
8a523922 | 513 | } |
76b87c03 UD |
514 | |
515 | result = 0; | |
f21acc89 | 516 | |
d705269e | 517 | unlock_return: |
efea7158 | 518 | UNLOCK_FILE (fd); |
76b87c03 UD |
519 | |
520 | /* Close WTMP file. */ | |
ce42435c | 521 | close_not_cancel_no_status (fd); |
76b87c03 UD |
522 | |
523 | return result; | |
8a523922 | 524 | } |