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.
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.
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.
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/>. */
26 #include <not-cancel.h>
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
35 internal_setent (const char *file
, struct nss_db_map
*mapping
)
37 enum nss_status status
= NSS_STATUS_UNAVAIL
;
39 int fd
= __open_nocancel (file
, O_RDONLY
| O_LARGEFILE
| O_CLOEXEC
);
42 struct nss_db_header header
;
44 if (read (fd
, &header
, sizeof (header
)) == sizeof (header
))
46 mapping
->header
= mmap (NULL
, header
.allocate
, PROT_READ
,
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
;
55 __close_nocancel_nostatus (fd
);
62 /* Close the database. */
64 internal_endent (struct nss_db_map
*mapping
)
66 munmap (mapping
->header
, mapping
->len
);