libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2010, 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 00021 #include <libexplain/buffer/einval.h> 00022 #include <libexplain/buffer/errno/generic.h> 00023 #include <libexplain/buffer/errno/munmap.h> 00024 #include <libexplain/buffer/must_be_multiple_of_page_size.h> 00025 #include <libexplain/buffer/pointer.h> 00026 #include <libexplain/buffer/size_t.h> 00027 #include <libexplain/explanation.h> 00028 #include <libexplain/getpagesize.h> 00029 00030 00031 static void 00032 explain_buffer_errno_munmap_system_call(explain_string_buffer_t *sb, int errnum, 00033 void *data, size_t data_size) 00034 { 00035 (void)errnum; 00036 explain_string_buffer_puts(sb, "munmap(data = "); 00037 explain_buffer_pointer(sb, data); 00038 explain_string_buffer_puts(sb, ", data_size = "); 00039 explain_buffer_size_t(sb, data_size); 00040 explain_string_buffer_putc(sb, ')'); 00041 } 00042 00043 00044 void 00045 explain_buffer_errno_munmap_explanation(explain_string_buffer_t *sb, int errnum, 00046 const char *syscall_name, void *data, size_t data_size) 00047 { 00048 int page_size; 00049 00050 /* 00051 * http://www.opengroup.org/onlinepubs/009695399/functions/munmap.html 00052 * 00053 * Note: it is not an error to munmap an address range that is not 00054 * mapped, an nothing is reported. 00055 */ 00056 switch (errnum) 00057 { 00058 case EINVAL: 00059 page_size = explain_getpagesize(); 00060 if (page_size > 0) 00061 { 00062 int mask = page_size - 1; 00063 if ((unsigned long)data & mask) 00064 { 00065 explain_buffer_must_be_multiple_of_page_size(sb, "data"); 00066 break; 00067 } 00068 if ((unsigned long)data_size & mask) 00069 { 00070 explain_buffer_must_be_multiple_of_page_size(sb, "data_size"); 00071 break; 00072 } 00073 } 00074 if (data_size == 0) 00075 { 00076 explain_buffer_einval_too_small(sb, "data_size", data_size); 00077 break; 00078 } 00079 /* Fall through... */ 00080 00081 default: 00082 explain_buffer_errno_generic(sb, errnum, syscall_name); 00083 break; 00084 } 00085 } 00086 00087 00088 void 00089 explain_buffer_errno_munmap(explain_string_buffer_t *sb, int errnum, void *data, 00090 size_t data_size) 00091 { 00092 explain_explanation_t exp; 00093 00094 explain_explanation_init(&exp, errnum); 00095 explain_buffer_errno_munmap_system_call(&exp.system_call_sb, errnum, data, 00096 data_size); 00097 explain_buffer_errno_munmap_explanation(&exp.explanation_sb, errnum, 00098 "munmap", data, data_size); 00099 explain_explanation_assemble(&exp, sb); 00100 } 00101 00102 00103 /* vim: set ts=8 sw=4 et : */