1 | /* Common database routines for nss_db. |
2 | Copyright (C) 2000-2017 Free Software Foundation, Inc. |
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 |
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. |
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 |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <fcntl.h> |
21 | #include <dlfcn.h> |
22 | #include <stdlib.h> |
23 | #include <string.h> |
24 | #include <unistd.h> |
25 | #include <sys/mman.h> |
26 | #include <not-cancel.h> |
27 | |
28 | #include "nss_db.h" |
29 | |
30 | /* Open the database stored in FILE. If succesful, store either a |
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. */ |
34 | enum nss_status |
35 | internal_setent (const char *file, struct nss_db_map *mapping) |
36 | { |
37 | enum nss_status status = NSS_STATUS_UNAVAIL; |
38 | |
39 | int mode = O_RDONLY | O_LARGEFILE; |
40 | #ifdef O_CLOEXEC |
41 | mode |= O_CLOEXEC; |
42 | #endif |
43 | int fd = open_not_cancel_2 (file, mode); |
44 | if (fd != -1) |
45 | { |
46 | struct nss_db_header ; |
47 | |
48 | if (read (fd, &header, sizeof (header)) == sizeof (header)) |
49 | { |
50 | mapping->header = mmap (NULL, header.allocate, PROT_READ, |
51 | MAP_PRIVATE, fd, 0); |
52 | mapping->len = header.allocate; |
53 | if (mapping->header != MAP_FAILED) |
54 | status = NSS_STATUS_SUCCESS; |
55 | else if (errno == ENOMEM) |
56 | status = NSS_STATUS_TRYAGAIN; |
57 | } |
58 | |
59 | close_not_cancel_no_status (fd); |
60 | } |
61 | |
62 | return status; |
63 | } |
64 | |
65 | |
66 | /* Close the database. */ |
67 | void |
68 | internal_endent (struct nss_db_map *mapping) |
69 | { |
70 | munmap (mapping->header, mapping->len); |
71 | } |
72 | |