libexplain  1.4.D001
libexplain/buffer/errno/dup2.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2008-2010, 2013, 2014 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/fcntl.h>
00021 #include <libexplain/ac/unistd.h>
00022 
00023 #include <libexplain/buffer/check_fildes_range.h>
00024 #include <libexplain/buffer/ebadf.h>
00025 #include <libexplain/buffer/eintr.h>
00026 #include <libexplain/buffer/eintr.h>
00027 #include <libexplain/buffer/einval.h>
00028 #include <libexplain/buffer/emfile.h>
00029 #include <libexplain/buffer/errno/dup2.h>
00030 #include <libexplain/buffer/errno/generic.h>
00031 #include <libexplain/buffer/fildes_to_pathname.h>
00032 #include <libexplain/buffer/gettext.h>
00033 #include <libexplain/explanation.h>
00034 #include <libexplain/option.h>
00035 
00036 
00037 static void
00038 explain_buffer_errno_dup2_system_call(explain_string_buffer_t *sb,
00039     int errnum, int oldfd, int newfd)
00040 {
00041     (void)errnum;
00042     explain_string_buffer_printf(sb, "dup2(oldfd = %d", oldfd);
00043     explain_buffer_fildes_to_pathname(sb, oldfd);
00044     explain_string_buffer_printf(sb, ", newfd = %d", newfd);
00045     explain_buffer_fildes_to_pathname(sb, newfd);
00046     explain_string_buffer_putc(sb, ')');
00047 }
00048 
00049 
00050 static void
00051 explain_buffer_errno_dup2_explanation(explain_string_buffer_t *sb,
00052     int errnum, int oldfd, int newfd)
00053 {
00054     /*
00055      * http://www.opengroup.org/onlinepubs/009695399/functions/dup2.html
00056      */
00057     switch (errnum)
00058     {
00059     case EBADF:
00060         if (fcntl(oldfd, F_GETFL) < 0)
00061         {
00062             /* oldfd is not an open file */
00063             explain_buffer_ebadf(sb, oldfd, "oldfd");
00064             return;
00065         }
00066         if (explain_buffer_check_fildes_range(sb, newfd, "newfd") >= 0)
00067         {
00068             /* newfd is out of range */
00069             return;
00070         }
00071         explain_string_buffer_puts
00072         (
00073             sb,
00074             /* FIXME: i18n */
00075             "the oldfd argument isn't an open file descriptor; or, "
00076             "the newf argument is outside the allowed range for file "
00077             "descriptors"
00078         );
00079         break;
00080 
00081     case EINVAL:
00082         /* not on Linux */
00083         if (explain_buffer_check_fildes_range(sb, newfd, "newfd") >= 0)
00084         {
00085             /* newfd is out of range */
00086             return;
00087         }
00088         explain_buffer_einval_vague(sb, "data");
00089         break;
00090 
00091     case EBUSY:
00092         explain_string_buffer_puts
00093         (
00094             sb,
00095             "a race condition was detected between open(2) and dup(2)"
00096         );
00097         break;
00098 
00099     case EINTR:
00100         explain_buffer_eintr(sb, "dup2");
00101         break;
00102 
00103     case EMFILE:
00104         if (explain_buffer_check_fildes_range(sb, newfd, "newfd"))
00105             explain_buffer_emfile(sb);
00106         break;
00107 
00108     default:
00109         explain_buffer_errno_generic(sb, errnum, "dup2");
00110         break;
00111     }
00112     if (fcntl(newfd, F_GETFL) >= 0)
00113     {
00114         explain_string_buffer_puts(sb->footnotes, "; ");
00115         explain_buffer_gettext
00116         (
00117             sb->footnotes,
00118             /*
00119              * xgettext:  This error message is used when dup2(2) system
00120              * call fails, the destination file descriptor may or may
00121              * not be closed.
00122              */
00123             i18n("note that any errors that would have been reported by "
00124             "close(newfd) are lost, a careful programmer will not use "
00125             "dup2() without closing newfd first")
00126         );
00127     }
00128 }
00129 
00130 
00131 void
00132 explain_buffer_errno_dup2(explain_string_buffer_t *sb, int errnum,
00133     int oldfd, int newfd)
00134 {
00135     explain_explanation_t exp;
00136 
00137     explain_explanation_init(&exp, errnum);
00138     explain_buffer_errno_dup2_system_call
00139     (
00140         &exp.system_call_sb,
00141         errnum,
00142         oldfd,
00143         newfd
00144     );
00145     explain_buffer_errno_dup2_explanation
00146     (
00147         &exp.explanation_sb,
00148         errnum,
00149         oldfd,
00150         newfd
00151     );
00152     explain_explanation_assemble(&exp, sb);
00153 }
00154 
00155 
00156 /* vim: set ts=8 sw=4 et : */