libexplain  1.4.D001
libexplain/is_efault/string.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - a library of system-call-specific strerror replacements
00003  * Copyright (C) 2011, 2013 Peter Miller
00004  * Written by Peter Miller <pmiller@opensource.org.au>
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <libexplain/ac/errno.h>
00021 #include <libexplain/ac/string.h>
00022 #include <libexplain/ac/sys/mman.h>
00023 #include <libexplain/ac/unistd.h>
00024 
00025 #include <libexplain/is_efault.h>
00026 
00027 
00028 int
00029 explain_is_efault_string(const char *data)
00030 {
00031 #ifdef HAVE_MINCORE
00032     /* mincore doesn't seem to work as expected on 64-bit Linux */
00033     static ssize_t  page_size;
00034     size_t          page_num;
00035     const char      *page_begin;
00036     const char      *page_end;
00037 
00038     if (!data)
00039         return 1;
00040     if (!page_size)
00041     {
00042 #ifdef HAVE_GETPAGESIZE
00043         page_size = getpagesize();
00044 #else
00045         page_size = sysconf(_SC_PAGESIZE);
00046 #endif
00047     }
00048     if (page_size <= 0)
00049     {
00050         plan_b:
00051         return explain_is_efault_path(data);
00052     }
00053     page_num = (size_t)data / page_size;
00054     page_begin = (const char *)(page_num * page_size);
00055     page_end = page_begin + page_size;
00056     for (;;)
00057     {
00058         unsigned char   vec[1];
00059         const char      *p;
00060 
00061         if (mincore((void *)page_begin, page_size, vec) < 0)
00062         {
00063             if (errno == ENOMEM)
00064                 return 1;
00065             goto plan_b;
00066         }
00067         p = memchr(data, '\0', page_end - data);
00068         if (p)
00069             return 0;
00070         data = page_begin;
00071         page_begin += page_size;
00072         page_end += page_size;
00073     }
00074 
00075 #else
00076     if (!data)
00077         return 1;
00078     return explain_is_efault_path(data);
00079 #endif
00080 }
00081 
00082 
00083 /* vim: set ts=8 sw=4 et : */