Fix jn precision
[glibc.git] / nss / nss_db / db-open.c
blob5a805cffbf9eb6bd303b0397679946b510d81412
1 /* Common database routines for nss_db.
2 Copyright (C) 2000, 2011 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <dlfcn.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <not-cancel.h>
29 #include <kernel-features.h>
30 #include "nss_db.h"
32 /* Open the database stored in FILE. If succesful, store either a
33 pointer to the mapped file or a file handle for the file in H and
34 return NSS_STATUS_SUCCESS. On failure, return the appropriate
35 lookup status. */
36 enum nss_status
37 internal_setent (const char *file, struct nss_db_map *mapping)
39 enum nss_status status = NSS_STATUS_UNAVAIL;
41 int mode = O_RDONLY | O_LARGEFILE;
42 #ifdef O_CLOEXEC
43 mode |= O_CLOEXEC;
44 #endif
45 int fd = open_not_cancel_2 (file, mode);
46 if (fd != -1)
48 struct nss_db_header header;
50 if (read (fd, &header, sizeof (header)) == sizeof (header))
52 mapping->header = mmap (NULL, header.allocate, PROT_READ,
53 MAP_PRIVATE, fd, 0);
54 mapping->len = header.allocate;
55 if (mapping->header != MAP_FAILED)
56 status = NSS_STATUS_SUCCESS;
57 else if (errno == ENOMEM)
58 status = NSS_STATUS_TRYAGAIN;
61 close_not_cancel_no_status (fd);
64 return status;
68 /* Close the database. */
69 void
70 internal_endent (struct nss_db_map *mapping)
72 munmap (mapping->header, mapping->len);