xmmap.c revision 124208
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 26124208Sdes/* $Id: xmmap.c,v 1.3 2003/06/02 02:25:27 tim Exp $ */ 27124208Sdes 28106121Sdes#include "includes.h" 29106121Sdes 30106121Sdes#ifdef HAVE_SYS_MMAN_H 31106121Sdes#include <sys/mman.h> 32106121Sdes#endif 33106121Sdes 34106121Sdes#include "log.h" 35106121Sdes 36106121Sdesvoid *xmmap(size_t size) 37106121Sdes{ 38106121Sdes void *address; 39106121Sdes 40106121Sdes#ifdef HAVE_MMAP 41106121Sdes# ifdef MAP_ANON 42106121Sdes address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_ANON|MAP_SHARED, 43106121Sdes -1, 0); 44106121Sdes# else 45106121Sdes address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, 46106121Sdes open("/dev/zero", O_RDWR), 0); 47106121Sdes# endif 48106121Sdes 49106121Sdes#define MM_SWAP_TEMPLATE "/var/run/sshd.mm.XXXXXXXX" 50106121Sdes if (address == MAP_FAILED) { 51106121Sdes char tmpname[sizeof(MM_SWAP_TEMPLATE)] = MM_SWAP_TEMPLATE; 52106121Sdes int tmpfd; 53106121Sdes 54106121Sdes tmpfd = mkstemp(tmpname); 55106121Sdes if (tmpfd == -1) 56106121Sdes fatal("mkstemp(\"%s\"): %s", 57106121Sdes MM_SWAP_TEMPLATE, strerror(errno)); 58106121Sdes unlink(tmpname); 59106121Sdes ftruncate(tmpfd, size); 60106121Sdes address = mmap(NULL, size, PROT_WRITE|PROT_READ, MAP_SHARED, 61106121Sdes tmpfd, 0); 62106121Sdes close(tmpfd); 63106121Sdes } 64106121Sdes 65106121Sdes return (address); 66106121Sdes#else 67106121Sdes fatal("%s: UsePrivilegeSeparation=yes and Compression=yes not supported", 68106121Sdes __func__); 69106121Sdes#endif /* HAVE_MMAP */ 70106121Sdes 71106121Sdes} 72106121Sdes 73