libexplain  1.4.D001
libexplain/buffer/errno/fopen.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008, 2009, 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
00007  * it 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
00009  * your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public 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/fcntl.h>
00022 #include <libexplain/ac/string.h>
00023 #include <libexplain/ac/unistd.h>
00024 
00025 #include <libexplain/buffer/enomem.h>
00026 #include <libexplain/buffer/errno/fopen.h>
00027 #include <libexplain/buffer/errno/open.h>
00028 #include <libexplain/buffer/pointer.h>
00029 #include <libexplain/explanation.h>
00030 #include <libexplain/string_buffer.h>
00031 #include <libexplain/string_flags.h>
00032 
00033 
00034 static void
00035 explain_buffer_errno_fopen_system_call(explain_string_buffer_t *sb,
00036     int errnum, const char *pathname, const char *flags_string)
00037 {
00038     /*
00039      * Note: EFAULT has to be the pathname, because if flags was broken,
00040      * it would have raised a SEGFAULT signal from user space.
00041      */
00042 
00043     explain_string_buffer_printf(sb, "fopen(pathname = ");
00044     if (errnum == EFAULT)
00045         explain_buffer_pointer(sb, pathname);
00046     else
00047         explain_string_buffer_puts_quoted(sb, pathname);
00048     explain_string_buffer_puts(sb, ", flags = ");
00049     explain_string_buffer_puts_quoted(sb, flags_string);
00050     explain_string_buffer_putc(sb, ')');
00051 }
00052 
00053 
00054 void
00055 explain_buffer_errno_fopen_explanation(explain_string_buffer_t *sb,
00056     int errnum, const char *syscall_name, const char *pathname,
00057     const char *flags)
00058 {
00059     explain_string_flags_t sf;
00060     int             permission_mode;
00061 
00062     explain_string_flags_init(&sf, flags);
00063     permission_mode = 0666;
00064     switch (errnum)
00065     {
00066     case EINVAL:
00067         explain_string_flags_einval(&sf, sb, "flags");
00068         break;
00069 
00070     case ENOMEM:
00071         {
00072             int             fd;
00073 
00074             /*
00075              * Try to figure out if it was a kernel ENOMEM or a user-space
00076              * (sbrk) ENOMEM.  This is doomed to be inaccurate.
00077              */
00078             errno = 0;
00079             fd = open(pathname, O_RDONLY);
00080             if (fd < 0)
00081             {
00082                 if (errno == ENOMEM)
00083                 {
00084                     explain_buffer_enomem_kernel(sb);
00085                     break;
00086                 }
00087                 explain_buffer_enomem_kernel_or_user(sb);
00088                 break;
00089             }
00090             close(fd);
00091             explain_buffer_enomem_user(sb, 0);
00092         }
00093         break;
00094 
00095     default:
00096         /*
00097          * Punt everything else to open()
00098          */
00099         explain_buffer_errno_open_explanation
00100         (
00101             sb,
00102             errnum,
00103             syscall_name,
00104             pathname,
00105             sf.flags,
00106             permission_mode
00107         );
00108         break;
00109     }
00110 }
00111 
00112 
00113 void
00114 explain_buffer_errno_fopen(explain_string_buffer_t *sb, int errnum,
00115     const char *pathname, const char *flags_string)
00116 {
00117     explain_explanation_t exp;
00118 
00119     explain_explanation_init(&exp, errnum);
00120     explain_buffer_errno_fopen_system_call
00121     (
00122         &exp.system_call_sb,
00123         errnum,
00124         pathname,
00125         flags_string
00126     );
00127     explain_buffer_errno_fopen_explanation
00128     (
00129         &exp.explanation_sb,
00130         errnum,
00131         "fopen",
00132         pathname,
00133         flags_string
00134     );
00135     explain_explanation_assemble(&exp, sb);
00136 }
00137 
00138 
00139 void
00140 explain_string_flags_einval(const explain_string_flags_t *sf,
00141     explain_string_buffer_t *sb, const char *caption)
00142 {
00143     size_t          n;
00144 
00145     explain_string_buffer_printf
00146     (
00147         sb,
00148         "the %s argument is not valid",
00149         caption
00150     );
00151     if (!sf->rwa_seen)
00152     {
00153         explain_string_buffer_puts
00154         (
00155             sb,
00156             ", you must specify 'r', 'w' or 'a' at the start of the string"
00157         );
00158     }
00159     n = strlen(sf->invalid);
00160     if (n > 0)
00161     {
00162         explain_string_buffer_printf
00163         (
00164             sb,
00165             ", flag character%s ",
00166             (n == 1 ? "" : "s")
00167         );
00168         explain_string_buffer_puts_quoted(sb, sf->invalid);
00169         explain_string_buffer_puts(sb, " unknown");
00170     }
00171 }
00172 
00173 
00174 /* vim: set ts=8 sw=4 et : */