1106121Sdes/*
2124208Sdes * Copyright (c) 2002 Tim Rice.  All rights reserved.
3124208Sdes * MAP_FAILED code by Solar Designer.
4124208Sdes *
5106121Sdes * Redistribution and use in source and binary forms, with or without
6106121Sdes * modification, are permitted provided that the following conditions
7106121Sdes * are met:
8106121Sdes * 1. Redistributions of source code must retain the above copyright
9106121Sdes *    notice, this list of conditions and the following disclaimer.
10106121Sdes * 2. Redistributions in binary form must reproduce the above copyright
11106121Sdes *    notice, this list of conditions and the following disclaimer in the
12106121Sdes *    documentation and/or other materials provided with the distribution.
13106121Sdes *
14106121Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15106121Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16106121Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17106121Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18106121Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19106121Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20106121Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21106121Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22106121Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23106121Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24106121Sdes */
25106121Sdes
26192595Sdes/* $Id: xmmap.c,v 1.15 2009/02/16 04:21:40 djm Exp $ */
27124208Sdes
28106121Sdes#include "includes.h"
29106121Sdes
30162852Sdes#include <sys/types.h>
31106121Sdes#ifdef HAVE_SYS_MMAN_H
32106121Sdes#include <sys/mman.h>
33106121Sdes#endif
34162852Sdes#include <sys/stat.h>
35106121Sdes
36162852Sdes#ifdef HAVE_FCNTL_H
37162852Sdes# include <fcntl.h>
38162852Sdes#endif
39162852Sdes#include <errno.h>
40162852Sdes#include <stdarg.h>
41181111Sdes#include <stdlib.h>
42162852Sdes#include <string.h>
43162852Sdes#include <unistd.h>
44162852Sdes
45106121Sdes#include "log.h"
46106121Sdes
47181111Sdesvoid *
48181111Sdesxmmap(size_t size)
49106121Sdes{
50162852Sdes#ifdef HAVE_MMAP
51106121Sdes	void *address;
52106121Sdes
53106121Sdes# ifdef MAP_ANON
54106121Sdes	address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED,
55137015Sdes	    -1, (off_t)0);
56106121Sdes# else
57106121Sdes	address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED,
58137015Sdes	    open("/dev/zero", O_RDWR), (off_t)0);
59106121Sdes# endif
60106121Sdes
61106121Sdes#define MM_SWAP_TEMPLATE "/var/run/sshd.mm.XXXXXXXX"
62146998Sdes	if (address == (void *)MAP_FAILED) {
63106121Sdes		char tmpname[sizeof(MM_SWAP_TEMPLATE)] = MM_SWAP_TEMPLATE;
64106121Sdes		int tmpfd;
65137015Sdes		mode_t old_umask;
66106121Sdes
67137015Sdes		old_umask = umask(0177);
68106121Sdes		tmpfd = mkstemp(tmpname);
69137015Sdes		umask(old_umask);
70106121Sdes		if (tmpfd == -1)
71106121Sdes			fatal("mkstemp(\"%s\"): %s",
72106121Sdes			    MM_SWAP_TEMPLATE, strerror(errno));
73106121Sdes		unlink(tmpname);
74192595Sdes		if (ftruncate(tmpfd, size) != 0)
75192595Sdes			fatal("%s: ftruncate: %s", __func__, strerror(errno));
76106121Sdes		address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED,
77137015Sdes		    tmpfd, (off_t)0);
78106121Sdes		close(tmpfd);
79106121Sdes	}
80106121Sdes
81106121Sdes	return (address);
82106121Sdes#else
83106121Sdes	fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported",
84106121Sdes	    __func__);
85106121Sdes#endif /* HAVE_MMAP */
86106121Sdes
87106121Sdes}
88106121Sdes
89