Blame nss/nss_db/db-open.c

Packit 6c4009
/* Common database routines for nss_db.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <not-cancel.h>
Packit 6c4009
Packit 6c4009
#include "nss_db.h"
Packit 6c4009
Packit 6c4009
/* Open the database stored in FILE.  If succesful, store either a
Packit 6c4009
   pointer to the mapped file or a file handle for the file in H and
Packit 6c4009
   return NSS_STATUS_SUCCESS.  On failure, return the appropriate
Packit 6c4009
   lookup status.  */
Packit 6c4009
enum nss_status
Packit 6c4009
internal_setent (const char *file, struct nss_db_map *mapping)
Packit 6c4009
{
Packit 6c4009
  enum nss_status status = NSS_STATUS_UNAVAIL;
Packit 6c4009
Packit 6c4009
  int fd = __open_nocancel (file, O_RDONLY | O_LARGEFILE | O_CLOEXEC);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      struct nss_db_header header;
Packit 6c4009
Packit 6c4009
      if (read (fd, &header, sizeof (header)) == sizeof (header))
Packit 6c4009
	{
Packit 6c4009
	  mapping->header = mmap (NULL, header.allocate, PROT_READ,
Packit 6c4009
				  MAP_PRIVATE, fd, 0);
Packit 6c4009
	  mapping->len = header.allocate;
Packit 6c4009
	  if (mapping->header != MAP_FAILED)
Packit 6c4009
	    status = NSS_STATUS_SUCCESS;
Packit 6c4009
	  else if (errno == ENOMEM)
Packit 6c4009
	    status = NSS_STATUS_TRYAGAIN;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      __close_nocancel_nostatus (fd);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return status;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Close the database.  */
Packit 6c4009
void
Packit 6c4009
internal_endent (struct nss_db_map *mapping)
Packit 6c4009
{
Packit Service 3b0880
  munmap (mapping->header, mapping->len);
Packit 6c4009
}