Update copyright notices with scripts/update-copyrights
[glibc.git] / nss / nss_db / db-open.c
blobea867a7a7fb30d2b48cfa0347e6edd780df7e4b9
1 /* Common database routines for nss_db.
2 Copyright (C) 2000-2014 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/>. */
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>
28 #include <kernel-features.h>
29 #include "nss_db.h"
31 /* Open the database stored in FILE. If succesful, store either a
32 pointer to the mapped file or a file handle for the file in H and
33 return NSS_STATUS_SUCCESS. On failure, return the appropriate
34 lookup status. */
35 enum nss_status
36 internal_setent (const char *file, struct nss_db_map *mapping)
38 enum nss_status status = NSS_STATUS_UNAVAIL;
40 int mode = O_RDONLY | O_LARGEFILE;
41 #ifdef O_CLOEXEC
42 mode |= O_CLOEXEC;
43 #endif
44 int fd = open_not_cancel_2 (file, mode);
45 if (fd != -1)
47 struct nss_db_header header;
49 if (read (fd, &header, sizeof (header)) == sizeof (header))
51 mapping->header = mmap (NULL, header.allocate, PROT_READ,
52 MAP_PRIVATE, fd, 0);
53 mapping->len = header.allocate;
54 if (mapping->header != MAP_FAILED)
55 status = NSS_STATUS_SUCCESS;
56 else if (errno == ENOMEM)
57 status = NSS_STATUS_TRYAGAIN;
60 close_not_cancel_no_status (fd);
63 return status;
67 /* Close the database. */
68 void
69 internal_endent (struct nss_db_map *mapping)
71 munmap (mapping->header, mapping->len);