libexplain  1.4.D001
libexplain/buffer/errno/flock.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 2009, 2010, 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,
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/sys/file.h>
00021 
00022 #include <libexplain/buffer/ebadf.h>
00023 #include <libexplain/buffer/eintr.h>
00024 #include <libexplain/buffer/einval.h>
00025 #include <libexplain/buffer/enomem.h>
00026 #include <libexplain/buffer/ewouldblock.h>
00027 #include <libexplain/buffer/errno/generic.h>
00028 #include <libexplain/buffer/errno/flock.h>
00029 #include <libexplain/buffer/fildes.h>
00030 #include <libexplain/buffer/gettext.h>
00031 #include <libexplain/explanation.h>
00032 #include <libexplain/parse_bits.h>
00033 #include <libexplain/sizeof.h>
00034 
00035 
00036 /*
00037  * On Solaris, even though flock(2) says these defines are available in
00038  * <sys/file.h>, the defines do not appear to be actually there.
00039  */
00040 static const explain_parse_bits_table_t table[] =
00041 {
00042 #ifdef LOCK_SH
00043     { "LOCK_SH", LOCK_SH },
00044 #endif
00045 #ifdef LOCK_EX
00046     { "LOCK_EX", LOCK_EX },
00047 #endif
00048 #ifdef LOCK_UN
00049     { "LOCK_UN", LOCK_UN },
00050 #endif
00051 #ifdef LOCK_NB
00052     { "LOCK_NB", LOCK_NB },
00053 #endif
00054 };
00055 
00056 
00057 int
00058 explain_flock_command_parse_or_die(const char *text, const char *caption)
00059 {
00060     return explain_parse_bits_or_die(text, table, sizeof(table), caption);
00061 }
00062 
00063 
00064 static void
00065 explain_buffer_flock_command(explain_string_buffer_t *sb, int command)
00066 {
00067     explain_parse_bits_print(sb, command, table, SIZEOF(table));
00068 }
00069 
00070 
00071 static void
00072 explain_buffer_errno_flock_system_call(explain_string_buffer_t *sb, int errnum,
00073     int fildes, int command)
00074 {
00075     (void)errnum;
00076     explain_string_buffer_puts(sb, "flock(fildes = ");
00077     explain_buffer_fildes(sb, fildes);
00078     explain_string_buffer_puts(sb, ", command = ");
00079     explain_buffer_flock_command(sb, command);
00080     explain_string_buffer_putc(sb, ')');
00081 }
00082 
00083 
00084 static void
00085 explain_buffer_errno_flock_explanation(explain_string_buffer_t *sb, int errnum,
00086     int fildes, int command)
00087 {
00088     /*
00089      * http://www.opengroup.org/onlinepubs/009695399/functions/flock.html
00090      */
00091     (void)command;
00092     switch (errnum)
00093     {
00094     case EBADF:
00095         explain_buffer_ebadf(sb, fildes, "fildes");
00096         break;
00097 
00098     case EINTR:
00099         explain_buffer_eintr(sb, "flock");
00100         break;
00101 
00102     case EINVAL:
00103         explain_buffer_einval_vague(sb, "command");
00104         break;
00105 
00106     case ENOLCK:
00107         explain_buffer_enomem_kernel(sb);
00108         break;
00109 
00110     case EAGAIN:
00111 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
00112     case EWOULDBLOCK:
00113 #endif
00114         explain_buffer_ewouldblock(sb, "flock");
00115         break;
00116 
00117     default:
00118         explain_buffer_errno_generic(sb, errnum, "flock");
00119         break;
00120     }
00121 }
00122 
00123 
00124 void
00125 explain_buffer_errno_flock(explain_string_buffer_t *sb, int errnum, int fildes,
00126     int command)
00127 {
00128     explain_explanation_t exp;
00129 
00130     explain_explanation_init(&exp, errnum);
00131     explain_buffer_errno_flock_system_call(&exp.system_call_sb, errnum, fildes,
00132         command);
00133     explain_buffer_errno_flock_explanation(&exp.explanation_sb, errnum, fildes,
00134         command);
00135     explain_explanation_assemble(&exp, sb);
00136 }
00137 
00138 
00139 /* vim: set ts=8 sw=4 et : */