libexplain  1.4.D001
libexplain/buffer/errno/nanosleep.c
Go to the documentation of this file.
00001 /*
00002  * libexplain - Explain errno values returned by libc functions
00003  * Copyright (C) 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 
00021 #include <libexplain/buffer/efault.h>
00022 #include <libexplain/buffer/eintr.h>
00023 #include <libexplain/buffer/einval.h>
00024 #include <libexplain/buffer/errno/generic.h>
00025 #include <libexplain/buffer/errno/nanosleep.h>
00026 #include <libexplain/buffer/timespec.h>
00027 #include <libexplain/explanation.h>
00028 #include <libexplain/is_efault.h>
00029 
00030 
00031 static void
00032 explain_buffer_errno_nanosleep_system_call(explain_string_buffer_t *sb, int
00033     errnum, const struct timespec *req, struct timespec *rem)
00034 {
00035     (void)errnum;
00036     explain_string_buffer_puts(sb, "nanosleep(req = ");
00037     explain_buffer_timespec(sb, req);
00038     explain_string_buffer_puts(sb, ", rem = ");
00039     explain_buffer_timespec(sb, rem);
00040     explain_string_buffer_putc(sb, ')');
00041 }
00042 
00043 
00044 static int
00045 is_valid_timespec(const struct timespec *ts)
00046 {
00047     return (ts->tv_nsec >= 0 && ts->tv_nsec < 1000000000);
00048 }
00049 
00050 
00051 void
00052 explain_buffer_errno_nanosleep_explanation(explain_string_buffer_t *sb, int
00053     errnum, const char *syscall_name, const struct timespec *req, struct
00054     timespec *rem)
00055 {
00056     switch (errnum)
00057     {
00058     case EFAULT:
00059         if (explain_is_efault_pointer(req, sizeof(*req)))
00060         {
00061             explain_buffer_efault(sb, "req");
00062             break;
00063         }
00064 
00065         if (explain_is_efault_pointer(rem, sizeof(*rem)))
00066         {
00067             explain_buffer_efault(sb, "rem");
00068             break;
00069         }
00070 
00071         goto generic;
00072 
00073     case EINTR:
00074         explain_buffer_eintr(sb, syscall_name);
00075         break;
00076 
00077     case EINVAL:
00078         if (!is_valid_timespec(req))
00079         {
00080             explain_buffer_einval_vague(sb, "req");
00081             break;
00082         }
00083         goto generic;
00084 
00085     default:
00086         generic:
00087         explain_buffer_errno_generic(sb, errnum, syscall_name);
00088         break;
00089     }
00090 }
00091 
00092 
00093 void
00094 explain_buffer_errno_nanosleep(explain_string_buffer_t *sb, int errnum, const
00095     struct timespec *req, struct timespec *rem)
00096 {
00097     explain_explanation_t exp;
00098 
00099     explain_explanation_init(&exp, errnum);
00100     explain_buffer_errno_nanosleep_system_call(&exp.system_call_sb, errnum, req,
00101         rem);
00102     explain_buffer_errno_nanosleep_explanation(&exp.explanation_sb, errnum,
00103         "nanosleep", req, rem);
00104     explain_explanation_assemble(&exp, sb);
00105 }
00106 
00107 
00108 /* vim: set ts=8 sw=4 et : */