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