libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 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/string.h> 00021 #include <libexplain/ac/sys/stat.h> 00022 00023 #include <libexplain/buffer/ebadf.h> 00024 #include <libexplain/buffer/enomem.h> 00025 #include <libexplain/buffer/enotdir.h> 00026 #include <libexplain/buffer/errno/generic.h> 00027 #include <libexplain/buffer/errno/open.h> 00028 #include <libexplain/buffer/errno/openat.h> 00029 #include <libexplain/buffer/fildes.h> 00030 #include <libexplain/buffer/open_flags.h> 00031 #include <libexplain/buffer/pathname.h> 00032 #include <libexplain/buffer/permission_mode.h> 00033 #include <libexplain/explanation.h> 00034 #include <libexplain/fileinfo.h> 00035 #include <libexplain/fildes_is_dot.h> 00036 00037 00038 static void 00039 explain_buffer_errno_openat_system_call(explain_string_buffer_t *sb, int errnum, 00040 int fildes, const char *pathname, int flags, mode_t mode) 00041 { 00042 (void)errnum; 00043 explain_string_buffer_puts(sb, "openat(fildes = "); 00044 explain_buffer_fildes(sb, fildes); 00045 explain_string_buffer_puts(sb, ", pathname = "); 00046 explain_buffer_pathname(sb, pathname); 00047 explain_string_buffer_puts(sb, ", flags = "); 00048 explain_buffer_open_flags(sb, flags); 00049 explain_string_buffer_puts(sb, ", mode = "); 00050 explain_buffer_permission_mode(sb, mode); 00051 explain_string_buffer_putc(sb, ')'); 00052 } 00053 00054 00055 void 00056 explain_buffer_errno_openat_explanation(explain_string_buffer_t *sb, int errnum, 00057 const char *syscall_name, int fildes, const char *pathname, int flags, 00058 mode_t mode) 00059 { 00060 (void)pathname; 00061 switch (errnum) 00062 { 00063 case EBADF: 00064 explain_buffer_ebadf(sb, fildes, "fildes"); 00065 return; 00066 00067 case ENOTDIR: 00068 /* 00069 * This is ambiguous, it could refer to fildes or it could refer 00070 * to pathname. 00071 */ 00072 if (!explain_fildes_is_a_directory(fildes)) 00073 { 00074 explain_buffer_enotdir_fd(sb, fildes, "fildes"); 00075 return; 00076 } 00077 break; 00078 00079 case ENOMEM: 00080 explain_buffer_enomem_kernel(sb); 00081 return; 00082 00083 default: 00084 break; 00085 } 00086 00087 if (explain_fildes_is_dot(fildes)) 00088 { 00089 while (*pathname == '/') 00090 ++pathname; 00091 if (!*pathname) 00092 pathname = "."; 00093 explain_buffer_errno_open_explanation 00094 ( 00095 sb, 00096 errnum, 00097 syscall_name, 00098 pathname, 00099 flags, 00100 mode 00101 ); 00102 return; 00103 } 00104 00105 { 00106 char long_path[PATH_MAX * 2]; 00107 if (explain_fileinfo_self_fd_n(fildes, long_path, sizeof(long_path))) 00108 { 00109 char *lp = long_path + strlen(long_path); 00110 char *end = long_path + sizeof(long_path); 00111 lp = explain_strendcpy(lp, "/", end); 00112 lp = explain_strendcpy(lp, pathname, end); 00113 explain_buffer_errno_open_explanation 00114 ( 00115 sb, 00116 errnum, 00117 syscall_name, 00118 long_path, 00119 flags, 00120 mode 00121 ); 00122 return; 00123 } 00124 /* Fall through... */ 00125 } 00126 00127 explain_buffer_errno_generic(sb, errnum, syscall_name); 00128 } 00129 00130 00131 void 00132 explain_buffer_errno_openat(explain_string_buffer_t *sb, int errnum, int fildes, 00133 const char *pathname, int flags, mode_t mode) 00134 { 00135 explain_explanation_t exp; 00136 00137 explain_explanation_init(&exp, errnum); 00138 explain_buffer_errno_openat_system_call(&exp.system_call_sb, errnum, fildes, 00139 pathname, flags, mode); 00140 explain_buffer_errno_openat_explanation(&exp.explanation_sb, errnum, 00141 "openat", fildes, pathname, flags, mode); 00142 explain_explanation_assemble(&exp, sb); 00143 } 00144 00145 00146 /* vim: set ts=8 sw=4 et : */