libexplain  1.4.D001
libexplain/mmap_or_die.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2010, 2012, 2013 Peter Miller
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU Lesser General Public License as published by
00007  * the Free Software Foundation; either version 3 of the License, or (at
00008  * your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #include <libexplain/ac/errno.h>
00020 #include <libexplain/ac/sys/mman.h>
00021 
00022 #include <libexplain/mmap.h>
00023 #include <libexplain/output.h>
00024 
00025 
00026 void *
00027 explain_mmap_or_die(void *data, size_t data_size, int prot, int flags, int
00028     fildes, off_t offset)
00029 {
00030     int             hold_errno;
00031     void            *result;
00032 
00033     hold_errno = errno;
00034     errno = 0;
00035     result = explain_mmap_on_error(data, data_size, prot, flags, fildes,
00036         offset);
00037     if
00038     (
00039         result == (void *)(-1)
00040     ||
00041         (
00042             !result
00043         &&
00044             (data != 0 || !(flags & MAP_FIXED) || errno)
00045         )
00046     )
00047     {
00048         explain_output_exit_failure();
00049     }
00050     errno = hold_errno;
00051     return result;
00052 }
00053 
00054 
00055 void *
00056 explain_mmap_on_error(void *data, size_t data_size, int prot, int flags, int
00057     fildes, off_t offset)
00058 {
00059     int             hold_errno;
00060     void            *result;
00061 
00062     hold_errno = errno;
00063     errno = 0;
00064 #ifdef HAVE_MMAP
00065     result = mmap(data, data_size, prot, flags, fildes, offset);
00066 #else
00067     errno = ENOSYS;
00068     result = 0;
00069 #endif
00070     if
00071     (
00072         !result
00073     &&
00074         (data != 0 || !(flags & MAP_FIXED) || errno)
00075     )
00076     {
00077         hold_errno = errno;
00078         explain_output_error
00079         (
00080             "%s",
00081             explain_errno_mmap
00082             (
00083                 hold_errno,
00084                 data,
00085                 data_size,
00086                 prot,
00087                 flags,
00088                 fildes,
00089                 offset
00090             )
00091         );
00092     }
00093     errno = hold_errno;
00094     return result;
00095 }
00096 
00097 
00098 /* vim: set ts=8 sw=4 et : */