xref: /freebsd-src/lib/libc/iconv/citrus_mmap.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
19ca40936STijl Coosemans /*	$NetBSD: citrus_mmap.c,v 1.4 2011/10/15 23:00:01 christos Exp $	*/
2ad30f8e7SGabor Kovesdan 
3ad30f8e7SGabor Kovesdan /*-
4*d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
5*d915a14eSPedro F. Giffuni  *
6ad30f8e7SGabor Kovesdan  * Copyright (c)2003 Citrus Project,
7ad30f8e7SGabor Kovesdan  * All rights reserved.
8ad30f8e7SGabor Kovesdan  *
9ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan  * are met:
12ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan  *
18ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan  */
30ad30f8e7SGabor Kovesdan 
3159797edfSJilles Tjoelker #include "namespace.h"
32ad30f8e7SGabor Kovesdan #include <sys/mman.h>
33ad30f8e7SGabor Kovesdan #include <sys/types.h>
34ad30f8e7SGabor Kovesdan #include <sys/stat.h>
35ad30f8e7SGabor Kovesdan 
36ad30f8e7SGabor Kovesdan #include <assert.h>
37ad30f8e7SGabor Kovesdan #include <errno.h>
38ad30f8e7SGabor Kovesdan #include <fcntl.h>
39ad30f8e7SGabor Kovesdan #include <limits.h>
40ad30f8e7SGabor Kovesdan #include <stdio.h>
41ad30f8e7SGabor Kovesdan #include <stdlib.h>
42ad30f8e7SGabor Kovesdan #include <string.h>
43ad30f8e7SGabor Kovesdan #include <unistd.h>
4459797edfSJilles Tjoelker #include "un-namespace.h"
45ad30f8e7SGabor Kovesdan 
46ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
47ad30f8e7SGabor Kovesdan #include "citrus_region.h"
48ad30f8e7SGabor Kovesdan #include "citrus_mmap.h"
49ad30f8e7SGabor Kovesdan 
50ad30f8e7SGabor Kovesdan int
_citrus_map_file(struct _citrus_region * __restrict r,const char * __restrict path)51ad30f8e7SGabor Kovesdan _citrus_map_file(struct _citrus_region * __restrict r,
52ad30f8e7SGabor Kovesdan     const char * __restrict path)
53ad30f8e7SGabor Kovesdan {
54ad30f8e7SGabor Kovesdan 	struct stat st;
55ad30f8e7SGabor Kovesdan 	void *head;
56ad30f8e7SGabor Kovesdan 	int fd, ret;
57ad30f8e7SGabor Kovesdan 
58ad30f8e7SGabor Kovesdan 	ret = 0;
59ad30f8e7SGabor Kovesdan 
60ad30f8e7SGabor Kovesdan 	_region_init(r, NULL, 0);
61ad30f8e7SGabor Kovesdan 
6259797edfSJilles Tjoelker 	if ((fd = _open(path, O_RDONLY | O_CLOEXEC)) == -1)
63ad30f8e7SGabor Kovesdan 		return (errno);
64ad30f8e7SGabor Kovesdan 
6559797edfSJilles Tjoelker 	if (_fstat(fd, &st)  == -1) {
66ad30f8e7SGabor Kovesdan 		ret = errno;
67ad30f8e7SGabor Kovesdan 		goto error;
68ad30f8e7SGabor Kovesdan 	}
69ad30f8e7SGabor Kovesdan 	if (!S_ISREG(st.st_mode)) {
70ad30f8e7SGabor Kovesdan 		ret = EOPNOTSUPP;
71ad30f8e7SGabor Kovesdan 		goto error;
72ad30f8e7SGabor Kovesdan 	}
73ad30f8e7SGabor Kovesdan 
74ad30f8e7SGabor Kovesdan 	head = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE,
75ad30f8e7SGabor Kovesdan 	    fd, (off_t)0);
76ad30f8e7SGabor Kovesdan 	if (head == MAP_FAILED) {
77ad30f8e7SGabor Kovesdan 		ret = errno;
78ad30f8e7SGabor Kovesdan 		goto error;
79ad30f8e7SGabor Kovesdan 	}
80ad30f8e7SGabor Kovesdan 	_region_init(r, head, (size_t)st.st_size);
81ad30f8e7SGabor Kovesdan 
82ad30f8e7SGabor Kovesdan error:
8359797edfSJilles Tjoelker 	(void)_close(fd);
84ad30f8e7SGabor Kovesdan 	return (ret);
85ad30f8e7SGabor Kovesdan }
86ad30f8e7SGabor Kovesdan 
87ad30f8e7SGabor Kovesdan void
_citrus_unmap_file(struct _citrus_region * r)88ad30f8e7SGabor Kovesdan _citrus_unmap_file(struct _citrus_region *r)
89ad30f8e7SGabor Kovesdan {
90ad30f8e7SGabor Kovesdan 
91ad30f8e7SGabor Kovesdan 	if (_region_head(r) != NULL) {
92ad30f8e7SGabor Kovesdan 		(void)munmap(_region_head(r), _region_size(r));
93ad30f8e7SGabor Kovesdan 		_region_init(r, NULL, 0);
94ad30f8e7SGabor Kovesdan 	}
95ad30f8e7SGabor Kovesdan }
96