xmmap.c revision 106121
1106121Sdes/*
2106121Sdes * Redistribution and use in source and binary forms, with or without
3106121Sdes * modification, are permitted provided that the following conditions
4106121Sdes * are met:
5106121Sdes * 1. Redistributions of source code must retain the above copyright
6106121Sdes *    notice, this list of conditions and the following disclaimer.
7106121Sdes * 2. Redistributions in binary form must reproduce the above copyright
8106121Sdes *    notice, this list of conditions and the following disclaimer in the
9106121Sdes *    documentation and/or other materials provided with the distribution.
10106121Sdes *
11106121Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12106121Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
13106121Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
14106121Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
15106121Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
16106121Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
17106121Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
18106121Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19106121Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
20106121Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21106121Sdes */
22106121Sdes
23106121Sdes#include "includes.h"
24106121Sdes
25106121Sdes#ifdef HAVE_SYS_MMAN_H
26106121Sdes#include <sys/mman.h>
27106121Sdes#endif
28106121Sdes
29106121Sdes#include "log.h"
30106121Sdes
31106121Sdesvoid *xmmap(size_t size)
32106121Sdes{
33106121Sdes	void *address;
34106121Sdes
35106121Sdes#ifdef HAVE_MMAP
36106121Sdes# ifdef MAP_ANON
37106121Sdes	address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED,
38106121Sdes	    -1, 0);
39106121Sdes# else
40106121Sdes	address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED,
41106121Sdes	    open("/dev/zero", O_RDWR), 0);
42106121Sdes# endif
43106121Sdes
44106121Sdes#define MM_SWAP_TEMPLATE "/var/run/sshd.mm.XXXXXXXX"
45106121Sdes	if (address == MAP_FAILED) {
46106121Sdes		char tmpname[sizeof(MM_SWAP_TEMPLATE)] = MM_SWAP_TEMPLATE;
47106121Sdes		int tmpfd;
48106121Sdes
49106121Sdes		tmpfd = mkstemp(tmpname);
50106121Sdes		if (tmpfd == -1)
51106121Sdes			fatal("mkstemp(\"%s\"): %s",
52106121Sdes			    MM_SWAP_TEMPLATE, strerror(errno));
53106121Sdes		unlink(tmpname);
54106121Sdes		ftruncate(tmpfd, size);
55106121Sdes		address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED,
56106121Sdes		    tmpfd, 0);
57106121Sdes		close(tmpfd);
58106121Sdes	}
59106121Sdes
60106121Sdes	return (address);
61106121Sdes#else
62106121Sdes	fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
63106121Sdes	    __func__);
64106121Sdes#endif /* HAVE_MMAP */
65106121Sdes
66106121Sdes}
67106121Sdes
68