libexplain  1.4.D001
libexplain/buffer/errno/poll.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2010, 2011, 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>
00021 #include <libexplain/ac/stdio.h>
00022 #include <libexplain/ac/sys/resource.h>
00023 
00024 #include <libexplain/buffer/ebadf.h>
00025 #include <libexplain/buffer/efault.h>
00026 #include <libexplain/buffer/eintr.h>
00027 #include <libexplain/buffer/enomem.h>
00028 #include <libexplain/buffer/errno/generic.h>
00029 #include <libexplain/buffer/errno/poll.h>
00030 #include <libexplain/buffer/gettext.h>
00031 #include <libexplain/buffer/int.h>
00032 #include <libexplain/buffer/pollfd.h>
00033 #include <libexplain/explanation.h>
00034 #include <libexplain/is_efault.h>
00035 
00036 
00037 static void
00038 explain_buffer_errno_poll_system_call(explain_string_buffer_t *sb, int errnum,
00039     struct pollfd *data, int data_size, int timeout)
00040 {
00041     (void)errnum;
00042     explain_string_buffer_puts(sb, "poll(data = ");
00043     explain_buffer_pollfd_array(sb, data, data_size, 0);
00044     explain_string_buffer_puts(sb, ", data_size = ");
00045     explain_buffer_int(sb, data_size);
00046     explain_string_buffer_puts(sb, ", timeout = ");
00047     explain_buffer_int(sb, timeout);
00048     explain_string_buffer_putc(sb, ')');
00049 }
00050 
00051 
00052 static int
00053 ebadf(int fildes)
00054 {
00055     int             flags;
00056 
00057     return (fcntl(fildes, F_GETFD, &flags) < 0 && errno == EBADF);
00058 }
00059 
00060 
00061 void
00062 explain_buffer_errno_poll_explanation(explain_string_buffer_t *sb, int errnum,
00063     const char *syscall_name, struct pollfd *data, int data_size, int timeout)
00064 {
00065     /*
00066      * http://www.opengroup.org/onlinepubs/009695399/functions/poll.html
00067      */
00068     (void)data;
00069     (void)timeout;
00070     switch (errnum)
00071     {
00072     case EFAULT:
00073         explain_buffer_efault(sb, "data");
00074         break;
00075 
00076     case EINTR:
00077         explain_buffer_eintr(sb, syscall_name);
00078         break;
00079 
00080     case EINVAL:
00081         {
00082             struct rlimit   lim;
00083 
00084             explain_buffer_gettext
00085             (
00086                 sb,
00087                 /*
00088                  * xgettext: This error message is used to explain an EINVAL
00089                  * error reported by the poll(2) system call, in the case where
00090                  * the data_size value exceeds the RLIMIT_NOFILE value.
00091                  */
00092                 i18n("the data_size value exceeds the RLIMIT_NOFILE value")
00093             );
00094             if (getrlimit(RLIMIT_NOFILE, &lim) >= 0)
00095             {
00096                 explain_string_buffer_printf
00097                 (
00098                     sb,
00099                     " (%d >= %d)",
00100                     data_size,
00101                     (int)lim.rlim_cur
00102                 );
00103             }
00104         }
00105         break;
00106 
00107     case ENOMEM:
00108         explain_buffer_enomem_kernel(sb);
00109         break;
00110 
00111 #ifdef HAVE_POLL_H
00112     case EBADF:
00113         if
00114         (
00115             data_size > 0
00116         &&
00117             !explain_is_efault_pointer(data, data_size * sizeof(struct pollfd))
00118         )
00119         {
00120             int             j;
00121 
00122             for (j = 0; j < data_size; ++j)
00123             {
00124                 if (ebadf(data[j].fd))
00125                 {
00126                     char            caption[30];
00127 
00128                     snprintf(caption, sizeof(caption), "data[%d].fd", j);
00129                     explain_buffer_ebadf(sb, data[j].fd, caption);
00130                     return;
00131                 }
00132             }
00133         }
00134         /* fall through... */
00135 #endif
00136 
00137     default:
00138         explain_buffer_errno_generic(sb, errnum, syscall_name);
00139         break;
00140     }
00141 }
00142 
00143 
00144 void
00145 explain_buffer_errno_poll(explain_string_buffer_t *sb, int errnum,
00146     struct pollfd *data, int data_size, int timeout)
00147 {
00148     explain_explanation_t exp;
00149 
00150     explain_explanation_init(&exp, errnum);
00151     explain_buffer_errno_poll_system_call(&exp.system_call_sb, errnum, data,
00152         data_size, timeout);
00153     explain_buffer_errno_poll_explanation(&exp.explanation_sb, errnum, "poll",
00154         data, data_size, timeout);
00155     explain_explanation_assemble(&exp, sb);
00156 }
00157 
00158 
00159 /* vim: set ts=8 sw=4 et : */