root/lib/pacemaker/pcmk_result_code.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pcmk__show_result_code
  2. pcmk_show_result_code
  3. pcmk__list_result_codes
  4. pcmk_list_result_codes

   1 /*
   2  * Copyright 2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 
  12 #include <crm/cib/internal.h>
  13 #include <crm/crm.h>
  14 
  15 #include <pacemaker.h>
  16 #include <pacemaker-internal.h>
  17 
  18 #include <inttypes.h>   // PRIx32
  19 #include <stdint.h>     // uint32_t
  20 
  21 /*!
  22  * \internal
  23  * \brief Display the name and/or description of a result code
  24  *
  25  * \param[in,out] out    Output object
  26  * \param[in]     code   The result code
  27  * \param[in]     type   Interpret \c code as this type of result code.
  28  *                       Supported values: \c pcmk_result_legacy,
  29  *                       \c pcmk_result_rc, \c pcmk_result_exitcode.
  30  * \param[in]     flags  Group of \c pcmk_rc_disp_flags
  31  *
  32  * \return Standard Pacemaker return code
  33  */
  34 int
  35 pcmk__show_result_code(pcmk__output_t *out, int code,
     /* [previous][next][first][last][top][bottom][index][help] */
  36                        enum pcmk_result_type type, uint32_t flags)
  37 {
  38     int rc = pcmk_rc_ok;
  39     bool quiet_orig = out->quiet;
  40     const char *name = NULL;
  41     const char *desc = NULL;
  42 
  43     rc = pcmk_result_get_strings(code, type, &name, &desc);
  44     if (rc != pcmk_rc_ok) {
  45         out->err(out, "Error looking up result code %d", code);
  46         return rc;
  47     }
  48 
  49     // out->quiet controls whether the code is shown (if quiet is supported)
  50     out->quiet = !pcmk_is_set(flags, pcmk_rc_disp_code);
  51 
  52     out->message(out, "result-code", code,
  53                  pcmk_is_set(flags, pcmk_rc_disp_name)? name : NULL,
  54                  pcmk_is_set(flags, pcmk_rc_disp_desc)? desc : NULL);
  55     out->quiet = quiet_orig;
  56 
  57     return rc;
  58 }
  59 
  60 // Documented in header
  61 int
  62 pcmk_show_result_code(xmlNodePtr *xml, int code, enum pcmk_result_type type,
     /* [previous][next][first][last][top][bottom][index][help] */
  63                       uint32_t flags)
  64 {
  65     pcmk__output_t *out = NULL;
  66     int rc = pcmk_rc_ok;
  67 
  68     rc = pcmk__xml_output_new(&out, xml);
  69     if (rc != pcmk_rc_ok) {
  70         return rc;
  71     }
  72 
  73     pcmk__register_lib_messages(out);
  74 
  75     rc = pcmk__show_result_code(out, code, type, flags);
  76     pcmk__xml_output_finish(out, pcmk_rc2exitc(rc), xml);
  77     return rc;
  78 }
  79 
  80 /*!
  81  * \internal
  82  * \brief List all valid result codes in a particular family
  83  *
  84  * \param[in,out] out    Output object
  85  * \param[in]     type   The family of result codes to list. Supported
  86  *                       values: \c pcmk_result_legacy, \c pcmk_result_rc,
  87  *                       \c pcmk_result_exitcode.
  88  * \param[in]     flags  Group of \c pcmk_rc_disp_flags
  89  *
  90  * \return Standard Pacemaker return code
  91  */
  92 int
  93 pcmk__list_result_codes(pcmk__output_t *out, enum pcmk_result_type type,
     /* [previous][next][first][last][top][bottom][index][help] */
  94                         uint32_t flags)
  95 {
  96     int rc = pcmk_rc_ok;
  97     int start = 0;
  98     int end = 0;
  99     int code = 0;
 100 
 101     bool quiet_orig = out->quiet;
 102     const char *name = NULL;
 103     const char *desc = NULL;
 104 
 105     rc = pcmk__result_bounds(type, &start, &end);
 106     if (rc != pcmk_rc_ok) {
 107         out->err(out,
 108                 "Failed to get result code bounds for result code type "
 109                  "%#010x" PRIx32, (uint32_t) type);
 110         return rc;
 111     }
 112 
 113     code = start;
 114     while (code <= end) {
 115         int local_rc = pcmk_rc_ok;
 116 
 117         if (code == (pcmk_rc_error + 1)) {
 118             /* Values between pcmk_rc_error and pcmk_rc_ok are reserved for
 119              * callers, so skip them
 120              */
 121             code = pcmk_rc_ok;
 122             continue;
 123         }
 124 
 125         // Shouldn't affect the return code of the whole list operation
 126         local_rc = pcmk_result_get_strings(code, type, &name, &desc);
 127 
 128         if ((local_rc != pcmk_rc_ok) || (name == NULL)
 129             || pcmk__str_any_of(name, "Unknown", "CRM_EX_UNKNOWN", NULL)) {
 130 
 131             code++;
 132             continue;
 133         }
 134 
 135         // out->quiet controls whether the code is shown (if quiet is supported)
 136         out->quiet = !pcmk_is_set(flags, pcmk_rc_disp_code);
 137 
 138         out->message(out, "result-code", code,
 139                      pcmk_is_set(flags, pcmk_rc_disp_name)? name : NULL,
 140                      pcmk_is_set(flags, pcmk_rc_disp_desc)? desc : NULL);
 141         out->quiet = quiet_orig;
 142 
 143         code++;
 144     }
 145 
 146     return rc;
 147 }
 148 
 149 // Documented in header
 150 int
 151 pcmk_list_result_codes(xmlNodePtr *xml, enum pcmk_result_type type,
     /* [previous][next][first][last][top][bottom][index][help] */
 152                        uint32_t flags)
 153 {
 154     pcmk__output_t *out = NULL;
 155     int rc = pcmk_rc_ok;
 156 
 157     rc = pcmk__xml_output_new(&out, xml);
 158     if (rc != pcmk_rc_ok) {
 159         return rc;
 160     }
 161 
 162     pcmk__register_lib_messages(out);
 163 
 164     rc = pcmk__list_result_codes(out, type, flags);
 165     pcmk__xml_output_finish(out, pcmk_rc2exitc(rc), xml);
 166     return rc;
 167 }

/* [previous][next][first][last][top][bottom][index][help] */