citrus_mmap.c revision 330897
1163953Srrs/* $FreeBSD: stable/11/lib/libc/iconv/citrus_mmap.c 330897 2018-03-14 03:19:51Z eadler $ */
2169382Srrs/*	$NetBSD: citrus_mmap.c,v 1.4 2011/10/15 23:00:01 christos Exp $	*/
3235828Stuexen
4235828Stuexen/*-
5163953Srrs * SPDX-License-Identifier: BSD-2-Clause
6163953Srrs *
7163953Srrs * Copyright (c)2003 Citrus Project,
8163953Srrs * All rights reserved.
9163953Srrs *
10228653Stuexen * Redistribution and use in source and binary forms, with or without
11163953Srrs * modification, are permitted provided that the following conditions
12163953Srrs * are met:
13163953Srrs * 1. Redistributions of source code must retain the above copyright
14228653Stuexen *    notice, this list of conditions and the following disclaimer.
15163953Srrs * 2. Redistributions in binary form must reproduce the above copyright
16163953Srrs *    notice, this list of conditions and the following disclaimer in the
17163953Srrs *    documentation and/or other materials provided with the distribution.
18163953Srrs *
19163953Srrs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20163953Srrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21163953Srrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22163953Srrs * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23163953Srrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24163953Srrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25163953Srrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26163953Srrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27163953Srrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28163953Srrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29163953Srrs * SUCH DAMAGE.
30163953Srrs */
31163953Srrs
32163953Srrs#include "namespace.h"
33163953Srrs#include <sys/cdefs.h>
34163953Srrs#include <sys/mman.h>
35163953Srrs#include <sys/types.h>
36235828Stuexen#include <sys/stat.h>
37235828Stuexen
38163953Srrs#include <assert.h>
39180387Srrs#include <errno.h>
40169378Srrs#include <fcntl.h>
41163953Srrs#include <limits.h>
42237715Stuexen#include <stdio.h>
43237049Stuexen#include <stdlib.h>
44238003Stuexen#include <string.h>
45238003Stuexen#include <unistd.h>
46238003Stuexen#include "un-namespace.h"
47238003Stuexen
48237049Stuexen#include "citrus_namespace.h"
49237049Stuexen#include "citrus_region.h"
50163953Srrs#include "citrus_mmap.h"
51163953Srrs
52169352Srrsint
53169352Srrs_citrus_map_file(struct _citrus_region * __restrict r,
54163953Srrs    const char * __restrict path)
55169352Srrs{
56240198Stuexen	struct stat st;
57169352Srrs	void *head;
58163953Srrs	int fd, ret;
59171158Srrs
60221627Stuexen	ret = 0;
61171158Srrs
62163953Srrs	_region_init(r, NULL, 0);
63163953Srrs
64	if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1)
65		return (errno);
66
67	if (_fstat(fd, &st)  == -1) {
68		ret = errno;
69		goto error;
70	}
71	if (!S_ISREG(st.st_mode)) {
72		ret = EOPNOTSUPP;
73		goto error;
74	}
75
76	head = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE,
77	    fd, (off_t)0);
78	if (head == MAP_FAILED) {
79		ret = errno;
80		goto error;
81	}
82	_region_init(r, head, (size_t)st.st_size);
83
84error:
85	(void)_close(fd);
86	return (ret);
87}
88
89void
90_citrus_unmap_file(struct _citrus_region *r)
91{
92
93	if (_region_head(r) != NULL) {
94		(void)munmap(_region_head(r), _region_size(r));
95		_region_init(r, NULL, 0);
96	}
97}
98