]>
Commit | Line | Data |
---|---|---|
9d8525f2 | 1 | /* Common database routines for nss_db. |
dff8da6b | 2 | Copyright (C) 2000-2024 Free Software Foundation, Inc. |
9a6450d5 UD |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9a6450d5 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
9a6450d5 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
9a6450d5 | 18 | |
9d8525f2 | 19 | #include <errno.h> |
9a6450d5 UD |
20 | #include <fcntl.h> |
21 | #include <dlfcn.h> | |
9a6450d5 | 22 | #include <stdlib.h> |
d1c2a14c | 23 | #include <string.h> |
07f494a0 | 24 | #include <unistd.h> |
2666d441 UD |
25 | #include <sys/mman.h> |
26 | #include <not-cancel.h> | |
9a6450d5 | 27 | |
9a6450d5 UD |
28 | #include "nss_db.h" |
29 | ||
7f0d9e61 | 30 | /* Open the database stored in FILE. If successful, store either a |
2666d441 UD |
31 | pointer to the mapped file or a file handle for the file in H and |
32 | return NSS_STATUS_SUCCESS. On failure, return the appropriate | |
33 | lookup status. */ | |
d1c2a14c | 34 | enum nss_status |
2666d441 | 35 | internal_setent (const char *file, struct nss_db_map *mapping) |
d1c2a14c | 36 | { |
2666d441 UD |
37 | enum nss_status status = NSS_STATUS_UNAVAIL; |
38 | ||
c2284574 | 39 | int fd = __open_nocancel (file, O_RDONLY | O_LARGEFILE | O_CLOEXEC); |
2666d441 | 40 | if (fd != -1) |
9a6450d5 | 41 | { |
2666d441 | 42 | struct nss_db_header header; |
9a6450d5 | 43 | |
2666d441 | 44 | if (read (fd, &header, sizeof (header)) == sizeof (header)) |
9a6450d5 | 45 | { |
2666d441 UD |
46 | mapping->header = mmap (NULL, header.allocate, PROT_READ, |
47 | MAP_PRIVATE, fd, 0); | |
48 | mapping->len = header.allocate; | |
49 | if (mapping->header != MAP_FAILED) | |
50 | status = NSS_STATUS_SUCCESS; | |
51 | else if (errno == ENOMEM) | |
52 | status = NSS_STATUS_TRYAGAIN; | |
9a6450d5 UD |
53 | } |
54 | ||
c181840c | 55 | __close_nocancel_nostatus (fd); |
9d8525f2 UD |
56 | } |
57 | ||
58 | return status; | |
59 | } | |
60 | ||
61 | ||
2666d441 | 62 | /* Close the database. */ |
9d8525f2 | 63 | void |
2666d441 | 64 | internal_endent (struct nss_db_map *mapping) |
9d8525f2 | 65 | { |
99135114 DD |
66 | if (mapping->header != NULL) |
67 | { | |
68 | munmap (mapping->header, mapping->len); | |
69 | mapping->header = NULL; | |
70 | } | |
9a6450d5 | 71 | } |