libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 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, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Lesser 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/stdlib.h> 00021 #include <libexplain/ac/string.h> 00022 #include <libexplain/ac/sys/stat.h> 00023 #include <libexplain/ac/unistd.h> 00024 00025 #include <libexplain/buffer/caption_name_type.h> 00026 #include <libexplain/buffer/efault.h> 00027 #include <libexplain/buffer/errno/execv.h> 00028 #include <libexplain/buffer/errno/execve.h> 00029 #include <libexplain/buffer/pathname.h> 00030 #include <libexplain/buffer/pointer.h> 00031 #include <libexplain/explanation.h> 00032 #include <libexplain/have_permission.h> 00033 #include <libexplain/name_max.h> 00034 #include <libexplain/is_efault.h> 00035 #include <libexplain/sizeof.h> 00036 00037 00038 static size_t 00039 count(char *const *p) 00040 { 00041 size_t result; 00042 00043 result = 0; 00044 while (*p) 00045 { 00046 ++result; 00047 ++p; 00048 } 00049 return result; 00050 } 00051 00052 00053 static void 00054 explain_buffer_errno_execv_system_call(explain_string_buffer_t *sb, 00055 int errnum, const char *pathname, char *const *argv) 00056 { 00057 (void)errnum; 00058 explain_string_buffer_puts(sb, "execv(pathname = "); 00059 explain_buffer_pathname(sb, pathname); 00060 explain_string_buffer_puts(sb, ", argv = "); 00061 if (explain_is_efault_pointer(argv, sizeof(argv[0]))) 00062 explain_buffer_pointer(sb, argv); 00063 else 00064 { 00065 size_t argc; 00066 size_t n; 00067 size_t argsize; 00068 00069 /* 00070 * produce output similar to execve 00071 */ 00072 argc = count(argv); 00073 explain_string_buffer_putc(sb, '['); 00074 argsize = 0; 00075 for (n = 0; n < argc && argsize < 1000; ++n) 00076 { 00077 const char *s; 00078 00079 if (n) 00080 explain_string_buffer_puts(sb, ", "); 00081 s = argv[n]; 00082 if (explain_is_efault_path(s)) 00083 { 00084 explain_buffer_pointer(sb, s); 00085 argsize += 8; 00086 } 00087 else 00088 { 00089 explain_string_buffer_puts_quoted(sb, s); 00090 argsize += strlen(s); 00091 } 00092 } 00093 if (n < argc) 00094 { 00095 explain_string_buffer_printf 00096 ( 00097 sb, 00098 /* FIXME: i18n */ 00099 "... plus another %d command line arguments", 00100 (int)(count(argv) - n) 00101 ); 00102 } 00103 explain_string_buffer_putc(sb, ']'); 00104 } 00105 explain_string_buffer_putc(sb, ')'); 00106 } 00107 00108 00109 void 00110 explain_buffer_errno_execv_explanation(explain_string_buffer_t *sb, 00111 int errnum, const char *syscall_name, const char *pathname, 00112 char *const *argv) 00113 { 00114 explain_buffer_errno_execve_explanation 00115 ( 00116 sb, 00117 errnum, 00118 syscall_name, 00119 pathname, 00120 argv, 00121 environ 00122 ); 00123 } 00124 00125 00126 void 00127 explain_buffer_errno_execv(explain_string_buffer_t *sb, int errnum, 00128 const char *pathname, char *const *argv) 00129 { 00130 explain_explanation_t exp; 00131 00132 explain_explanation_init(&exp, errnum); 00133 explain_buffer_errno_execv_system_call 00134 ( 00135 &exp.system_call_sb, 00136 errnum, 00137 pathname, 00138 argv 00139 ); 00140 explain_buffer_errno_execv_explanation 00141 ( 00142 &exp.explanation_sb, 00143 errnum, 00144 "execv", 00145 pathname, 00146 argv 00147 ); 00148 explain_explanation_assemble(&exp, sb); 00149 } 00150 00151 /* vim: set ts=8 sw=4 et : */