libexplain  1.4.D001
libexplain/buffer/errno/getpriority.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 #include <libexplain/ac/sys/time.h>
00021 #include <libexplain/ac/sys/resource.h>
00022 #include <libexplain/ac/unistd.h>
00023 
00024 #include <libexplain/buffer/dac.h>
00025 #include <libexplain/buffer/eacces.h>
00026 #include <libexplain/buffer/einval.h>
00027 #include <libexplain/buffer/eperm.h>
00028 #include <libexplain/buffer/eperm.h>
00029 #include <libexplain/buffer/errno/generic.h>
00030 #include <libexplain/buffer/errno/getpriority.h>
00031 #include <libexplain/buffer/esrch.h>
00032 #include <libexplain/buffer/prio_which.h>
00033 #include <libexplain/buffer/pid_t_star.h>
00034 #include <libexplain/buffer/uid.h>
00035 #include <libexplain/buffer/int.h>
00036 #include <libexplain/option.h>
00037 #include <libexplain/explanation.h>
00038 #include <libexplain/uid_from_pid.h>
00039 
00040 
00041 static void
00042 explain_buffer_errno_getpriority_system_call(explain_string_buffer_t *sb, int
00043     errnum, int which, int who)
00044 {
00045     (void)errnum;
00046     explain_string_buffer_puts(sb, "getpriority(which = ");
00047     explain_buffer_prio_which(sb, which);
00048     explain_string_buffer_puts(sb, ", who = ");
00049     switch (which)
00050     {
00051     case PRIO_PROCESS:
00052     case PRIO_PGRP:
00053         explain_buffer_pid_t(sb, who);
00054         break;
00055 
00056     case PRIO_USER:
00057         explain_buffer_uid(sb, who);
00058         break;
00059 
00060     default:
00061         explain_buffer_int(sb, who);
00062         break;
00063     }
00064     explain_string_buffer_putc(sb, ')');
00065 }
00066 
00067 
00068 void
00069 explain_buffer_errno_getpriority_explanation(explain_string_buffer_t *sb, int
00070     errnum, const char *syscall_name, int which, int who)
00071 {
00072     switch (errnum)
00073     {
00074     case EINVAL:
00075         /*
00076          * which was not one of PRIO_PROCESS, PRIO_PGRP, or PRIO_USER.
00077          */
00078         switch (which)
00079         {
00080         case PRIO_PROCESS:
00081         case PRIO_PGRP:
00082         case PRIO_USER:
00083             goto generic;
00084 
00085         default:
00086             explain_buffer_einval_vague(sb, "which");
00087             break;
00088         }
00089         break;
00090 
00091     case ESRCH:
00092         /*
00093          * No process was located using the which and who values specified.
00094          */
00095         switch (which)
00096         {
00097         case PRIO_PROCESS:
00098             explain_buffer_esrch(sb, "who", who);
00099             return;
00100 
00101         case PRIO_PGRP:
00102             explain_buffer_esrch(sb, "who", -who);
00103             return;
00104 
00105         default:
00106             explain_string_buffer_puts
00107             (
00108                 sb,
00109                 "no process was located using the which and who values "
00110                 "specified"
00111             );
00112             goto generic;
00113         }
00114 
00115     case EPERM:
00116         /*
00117          * A process was located, but its effective user ID did not
00118          * match either the effective or the real user ID of the
00119          * caller, and was not privileged (on Linux: did not have the
00120          * CAP_SYS_NICE capability).
00121          */
00122         explain_string_buffer_puts
00123         (
00124             sb,
00125             /* FIXME: i18n */
00126             " A process was located, but its effective user ID did not "
00127             " match the effective user ID of this process"
00128         );
00129 
00130         /* suplementary */
00131         if (explain_option_dialect_specific())
00132         {
00133             int the_other_guy = explain_uid_from_pid(who);
00134             if (the_other_guy > 0)
00135             {
00136                 explain_string_buffer_puts(sb, " (");
00137                 explain_buffer_uid(sb, the_other_guy);
00138                 explain_string_buffer_puts(sb, " != ");
00139                 explain_buffer_uid(sb, geteuid());
00140                 explain_string_buffer_putc(sb, ')');
00141             }
00142         }
00143 
00144         explain_buffer_dac_sys_nice(sb);
00145         break;
00146 
00147     default:
00148         generic:
00149         explain_buffer_errno_generic(sb, errnum, syscall_name);
00150         break;
00151     }
00152 }
00153 
00154 
00155 void
00156 explain_buffer_errno_getpriority(explain_string_buffer_t *sb, int errnum, int
00157     which, int who)
00158 {
00159     explain_explanation_t exp;
00160 
00161     explain_explanation_init(&exp, errnum);
00162     explain_buffer_errno_getpriority_system_call(&exp.system_call_sb, errnum,
00163         which, who);
00164     explain_buffer_errno_getpriority_explanation(&exp.explanation_sb, errnum,
00165         "getpriority", which, who);
00166     explain_explanation_assemble(&exp, sb);
00167 }
00168 
00169 
00170 /* vim: set ts=8 sw=4 et : */