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, 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/fcntl.h> /* for AT_FDCWD */ 00021 00022 #include <libexplain/buffer/eacces.h> 00023 #include <libexplain/buffer/ebadf.h> 00024 #include <libexplain/buffer/efault.h> 00025 #include <libexplain/buffer/einval.h> 00026 #include <libexplain/buffer/eloop.h> 00027 #include <libexplain/buffer/enametoolong.h> 00028 #include <libexplain/buffer/enoent.h> 00029 #include <libexplain/buffer/enotdir.h> 00030 #include <libexplain/buffer/eperm.h> 00031 #include <libexplain/buffer/erofs.h> 00032 #include <libexplain/buffer/errno/futimens.h> 00033 #include <libexplain/buffer/errno/path_resolution.h> 00034 #include <libexplain/buffer/errno/utimens.h> 00035 #include <libexplain/buffer/errno/utimensat.h> 00036 #include <libexplain/buffer/esrch.h> 00037 #include <libexplain/buffer/fildes.h> 00038 #include <libexplain/buffer/gettext.h> 00039 #include <libexplain/buffer/is_the_null_pointer.h> 00040 #include <libexplain/buffer/pathname.h> 00041 #include <libexplain/buffer/timespec.h> 00042 #include <libexplain/buffer/utimensat_fildes.h> 00043 #include <libexplain/buffer/utimensat_flags.h> 00044 #include <libexplain/explanation.h> 00045 #include <libexplain/fileinfo.h> 00046 #include <libexplain/is_efault.h> 00047 00048 00049 static void 00050 explain_buffer_errno_utimensat_system_call(explain_string_buffer_t *sb, 00051 int errnum, int fildes, const char *pathname, const struct timespec *data, 00052 int flags) 00053 { 00054 (void)errnum; 00055 explain_string_buffer_puts(sb, "utimensat(fildes = "); 00056 explain_buffer_utimensat_fildes(sb, fildes); 00057 explain_string_buffer_puts(sb, ", pathname = "); 00058 explain_buffer_pathname(sb, pathname); 00059 explain_string_buffer_puts(sb, ", data = "); 00060 explain_buffer_timespec_array(sb, data, 2); 00061 explain_string_buffer_puts(sb, ", flags = "); 00062 explain_buffer_utimensat_flags(sb, flags); 00063 explain_string_buffer_putc(sb, ')'); 00064 } 00065 00066 00067 static int 00068 is_a_directory(int fildes) 00069 { 00070 struct stat st; 00071 00072 if (fildes == AT_FDCWD) 00073 return 1; 00074 if (fstat(fildes, &st) < 0) 00075 return -1; 00076 return S_ISDIR(st.st_mode); 00077 } 00078 00079 00080 static int 00081 valid_nsec(long value) 00082 { 00083 if (value == UTIME_NOW) 00084 return 1; 00085 if (value == UTIME_OMIT) 00086 return 1; 00087 return (value >= 0 && value < 1000000000L); 00088 } 00089 00090 00091 static void 00092 user_path_at(explain_string_buffer_t *sb, int fildes, const char *pathname) 00093 { 00094 char dirpath[PATH_MAX + 1]; 00095 00096 if (!pathname || !*pathname) 00097 pathname = "."; 00098 00099 if (pathname[0] == '/') 00100 { 00101 explain_string_buffer_puts(sb, pathname); 00102 return; 00103 } 00104 if (fildes == AT_FDCWD) 00105 { 00106 explain_string_buffer_puts(sb, pathname); 00107 return; 00108 } 00109 explain_fileinfo_self_fd_n(fildes, dirpath, sizeof(dirpath)); 00110 explain_string_buffer_puts(sb, dirpath); 00111 explain_string_buffer_path_join(sb, pathname); 00112 } 00113 00114 00115 void 00116 explain_buffer_errno_utimensat_explanation(explain_string_buffer_t *sb, 00117 int errnum, const char *syscall_name, int fildes, const char *pathname, 00118 const struct timespec *data, int flags) 00119 { 00120 explain_final_t final_component; 00121 struct timespec phony_data[2]; 00122 char pathname2[PATH_MAX + 1]; 00123 00124 /* 00125 * Strange handling of corner case. See Linux kernel sources file 00126 * fs/utimes.c, around line 122, for more detail. 00127 */ 00128 if (pathname == NULL) 00129 { 00130 explain_buffer_errno_futimens_explanation(sb, errnum, syscall_name, 00131 fildes, data); 00132 return; 00133 } 00134 00135 /* 00136 * Extract an absolute path from the arguments. This has the 00137 * potential to exceed PATH_MAX, which is partly why the *st 00138 * funtions exist. 00139 */ 00140 { 00141 explain_string_buffer_t sb2; 00142 explain_string_buffer_init(&sb2, pathname2, sizeof(pathname2)); 00143 user_path_at(&sb2, fildes, pathname); 00144 pathname = pathname2; 00145 } 00146 00147 /* 00148 * Default the data, if necessary. 00149 */ 00150 if (!data) 00151 { 00152 phony_data[0].tv_sec = 0; 00153 phony_data[0].tv_nsec= UTIME_NOW; 00154 phony_data[1].tv_sec = 0; 00155 phony_data[1].tv_nsec= UTIME_NOW; 00156 data = phony_data; 00157 } 00158 00159 explain_final_init(&final_component); 00160 if 00161 ( 00162 data[0].tv_nsec != UTIME_OMIT 00163 || 00164 data[1].tv_nsec != UTIME_OMIT 00165 ) 00166 { 00167 if (data[0].tv_nsec == UTIME_NOW && data[1].tv_nsec == UTIME_NOW) 00168 final_component.want_to_write = 1; 00169 else 00170 final_component.want_to_modify_inode = 1; 00171 } 00172 if (0 == (flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW))) 00173 final_component.follow_symlink = 1; 00174 if (flags & AT_SYMLINK_FOLLOW) 00175 final_component.follow_symlink = 1; 00176 if (flags & AT_SYMLINK_NOFOLLOW) 00177 final_component.follow_symlink = 0; 00178 final_component.must_exist = 1; 00179 00180 switch (errnum) 00181 { 00182 case EINVAL: 00183 /* see fs/utimes.c: 134 */ 00184 if (!valid_nsec(data[0].tv_nsec)) 00185 { 00186 explain_buffer_einval_vague(sb, "data[0]"); 00187 return; 00188 } 00189 if (!valid_nsec(data[1].tv_nsec)) 00190 { 00191 explain_buffer_einval_vague(sb, "data[1]"); 00192 return; 00193 } 00194 if (data[0].tv_nsec == UTIME_NOW && data[1].tv_nsec == UTIME_NOW) 00195 { 00196 explain_buffer_gettext 00197 ( 00198 sb, 00199 /* FIXME:i8n */ 00200 "both tv_nsec values are UTIME_NOW" 00201 ); 00202 return; 00203 } 00204 if (flags & ~explain_allbits_utimensat_flags()) 00205 { 00206 /* 00207 * Invalid value in flags. 00208 */ 00209 explain_buffer_einval_vague(sb, "flags"); 00210 return; 00211 } 00212 if (data) 00213 { 00214 /* 00215 * "Invalid value in one of the tv_nsec fields (value outside 00216 * range 0 to 999,999,999, and not UTIME_NOW or UTIME_OMIT); or an 00217 * invalid value in one of the tv_sec fields." 00218 */ 00219 if (!valid_nsec(data[0].tv_nsec)) 00220 { 00221 explain_buffer_einval_vague(sb, "data[0].tv_nsec"); 00222 return; 00223 } 00224 if (!valid_nsec(data[1].tv_nsec)) 00225 { 00226 explain_buffer_einval_vague(sb, "data[1].tv_nsec"); 00227 return; 00228 } 00229 } 00230 break; 00231 00232 case EACCES: 00233 explain_buffer_eacces(sb, pathname, "pathname", &final_component); 00234 return; 00235 00236 case EBADF: 00237 { 00238 struct stat st; 00239 if (fstat(fildes, &st) < 0) 00240 { 00241 explain_buffer_ebadf(sb, fildes, "fildes"); 00242 return; 00243 } 00244 if (!S_ISDIR(st.st_mode)) 00245 { 00246 explain_buffer_ebadf_dir(sb, "fildes"); 00247 return; 00248 } 00249 } 00250 break; 00251 00252 case EFAULT: 00253 if (explain_is_efault_pointer(data, sizeof(*data) * 2)) 00254 { 00255 explain_buffer_efault(sb, "data"); 00256 return; 00257 } 00258 00259 /* 00260 * 'data' pointed to an invalid address; or, 'fildes' was 00261 * AT_FDCWD, and 'pathname' is NULL or an invalid address 00262 */ 00263 if (explain_is_efault_path(pathname)) 00264 { 00265 explain_buffer_efault(sb, "pathname"); 00266 return; 00267 } 00268 break; 00269 00270 case ELOOP: 00271 case EMLINK: 00272 /* 00273 * Too many symbolic links were encountered in resolving 00274 * pathname. 00275 */ 00276 explain_buffer_eloop(sb, pathname, "pathname", &final_component); 00277 return; 00278 00279 case ENAMETOOLONG: 00280 /* 00281 * pathname is too long. 00282 */ 00283 explain_buffer_enametoolong(sb, pathname, "pathname", &final_component); 00284 return; 00285 00286 case ENOENT: 00287 /* 00288 * A component of pathname does not refer to an existing 00289 * directory or file, or pathname is an empty string. 00290 */ 00291 explain_buffer_enoent(sb, pathname, "pathname", &final_component); 00292 return; 00293 00294 case ENOTDIR: 00295 /* 00296 * pathname is a relative pathname, but dirfd is neiher 00297 * AT_FDCWD nor a file descriptor referring to a directory; 00298 */ 00299 if 00300 ( 00301 pathname[0] != '/' 00302 && 00303 fildes != AT_FDCWD 00304 && 00305 0 == is_a_directory(fildes) 00306 ) 00307 { 00308 explain_buffer_enotdir_fd(sb, fildes, "fildes"); 00309 return; 00310 } 00311 00312 /* 00313 * or, one of the prefix components of pathname is not a directory. 00314 */ 00315 explain_buffer_enotdir(sb, pathname, "pathname", &final_component); 00316 return; 00317 00318 case EPERM: 00319 if (!data) 00320 break; 00321 if (data[0].tv_nsec == UTIME_OMIT && data[1].tv_nsec == UTIME_OMIT) 00322 break; 00323 break; 00324 00325 case EROFS: 00326 /* 00327 * "The file is on a read-only file system." 00328 */ 00329 explain_buffer_erofs(sb, pathname, "pathname"); 00330 break; 00331 00332 case ESRCH: 00333 /* 00334 * "Search permission is denied for one of the prefix components 00335 * of pathname." 00336 */ 00337 errnum = ENOENT; 00338 explain_buffer_eacces(sb, pathname, "pathname", &final_component); 00339 break; 00340 00341 default: 00342 break; 00343 } 00344 00345 explain_buffer_errno_utimens_explanation(sb, errnum, syscall_name, 00346 pathname, data); 00347 } 00348 00349 00350 void 00351 explain_buffer_errno_utimensat(explain_string_buffer_t *sb, int errnum, 00352 int fildes, const char *pathname, const struct timespec *data, int flags) 00353 { 00354 explain_explanation_t exp; 00355 00356 explain_explanation_init(&exp, errnum); 00357 explain_buffer_errno_utimensat_system_call(&exp.system_call_sb, errnum, 00358 fildes, pathname, data, flags); 00359 explain_buffer_errno_utimensat_explanation(&exp.explanation_sb, errnum, 00360 "utimensat", fildes, pathname, data, flags); 00361 explain_explanation_assemble(&exp, sb); 00362 } 00363 00364 00365 /* vim: set ts=8 sw=4 et : */