root/tools/crm_mon_curses.c

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

DEFINITIONS

This source file includes following definitions.
  1. curses_free_priv
  2. curses_init
  3. curses_finish
  4. curses_reset
  5. curses_subprocess_output
  6. curses_ver
  7. G_GNUC_PRINTF
  8. G_GNUC_PRINTF
  9. curses_output_xml
  10. G_GNUC_PRINTF
  11. G_GNUC_PRINTF
  12. curses_increment_list
  13. curses_end_list
  14. curses_is_quiet
  15. curses_spacer
  16. curses_progress
  17. curses_prompt
  18. crm_mon_mk_curses_output
  19. G_GNUC_PRINTF
  20. G_GNUC_PRINTF
  21. G_GNUC_PRINTF
  22. G_GNUC_PRINTF
  23. PCMK__OUTPUT_ARGS
  24. PCMK__OUTPUT_ARGS
  25. PCMK__OUTPUT_ARGS
  26. crm_mon_register_messages
  27. blank_screen

   1 /*
   2  * Copyright 2019-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 #include <stdarg.h>
  12 #include <stdint.h>
  13 #include <stdlib.h>
  14 #include <crm/crm.h>
  15 #include <crm/common/output.h>
  16 #include <crm/common/cmdline_internal.h>
  17 #include <crm/stonith-ng.h>
  18 #include <crm/fencing/internal.h>
  19 #include <crm/pengine/internal.h>
  20 #include <glib.h>
  21 #include <pacemaker-internal.h>
  22 
  23 #include "crm_mon.h"
  24 
  25 #if CURSES_ENABLED
  26 
  27 GOptionEntry crm_mon_curses_output_entries[] = {
  28     { NULL }
  29 };
  30 
  31 typedef struct curses_list_data_s {
  32     unsigned int len;
  33     char *singular_noun;
  34     char *plural_noun;
  35 } curses_list_data_t;
  36 
  37 typedef struct private_data_s {
  38     GQueue *parent_q;
  39 } private_data_t;
  40 
  41 static void
  42 curses_free_priv(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  43     private_data_t *priv = NULL;
  44 
  45     if (out == NULL || out->priv == NULL) {
  46         return;
  47     }
  48 
  49     priv = out->priv;
  50 
  51     g_queue_free(priv->parent_q);
  52     free(priv);
  53     out->priv = NULL;
  54 }
  55 
  56 static bool
  57 curses_init(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  58     private_data_t *priv = NULL;
  59 
  60     CRM_ASSERT(out != NULL);
  61 
  62     /* If curses_init was previously called on this output struct, just return. */
  63     if (out->priv != NULL) {
  64         return true;
  65     } else {
  66         out->priv = calloc(1, sizeof(private_data_t));
  67         if (out->priv == NULL) {
  68             return false;
  69         }
  70 
  71         priv = out->priv;
  72     }
  73 
  74     priv->parent_q = g_queue_new();
  75 
  76     initscr();
  77     cbreak();
  78     noecho();
  79 
  80     return true;
  81 }
  82 
  83 static void
  84 curses_finish(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest) {
     /* [previous][next][first][last][top][bottom][index][help] */
  85     CRM_ASSERT(out != NULL);
  86 
  87     echo();
  88     nocbreak();
  89     endwin();
  90 }
  91 
  92 static void
  93 curses_reset(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  94     CRM_ASSERT(out != NULL);
  95 
  96     curses_free_priv(out);
  97     curses_init(out);
  98 }
  99 
 100 static void
 101 curses_subprocess_output(pcmk__output_t *out, int exit_status,
     /* [previous][next][first][last][top][bottom][index][help] */
 102                          const char *proc_stdout, const char *proc_stderr) {
 103     CRM_ASSERT(out != NULL);
 104 
 105     if (proc_stdout != NULL) {
 106         printw("%s\n", proc_stdout);
 107     }
 108 
 109     if (proc_stderr != NULL) {
 110         printw("%s\n", proc_stderr);
 111     }
 112 
 113     clrtoeol();
 114     refresh();
 115 }
 116 
 117 /* curses_version is defined in curses.h, so we can't use that name here.
 118  * Note that this function prints out via text, not with curses.
 119  */
 120 static void
 121 curses_ver(pcmk__output_t *out, bool extended) {
     /* [previous][next][first][last][top][bottom][index][help] */
 122     CRM_ASSERT(out != NULL);
 123 
 124     if (extended) {
 125         printf("Pacemaker %s (Build: %s): %s\n", PACEMAKER_VERSION, BUILD_VERSION, CRM_FEATURES);
 126     } else {
 127         printf("Pacemaker %s\n", PACEMAKER_VERSION);
 128         printf("Written by Andrew Beekhof and the "
 129                "Pacemaker project contributors\n");
 130     }
 131 }
 132 
 133 G_GNUC_PRINTF(2, 3)
     /* [previous][next][first][last][top][bottom][index][help] */
 134 static void
 135 curses_error(pcmk__output_t *out, const char *format, ...) {
 136     va_list ap;
 137 
 138     CRM_ASSERT(out != NULL);
 139 
 140     /* Informational output does not get indented, to separate it from other
 141      * potentially indented list output.
 142      */
 143     va_start(ap, format);
 144     vw_printw(stdscr, format, ap);
 145     va_end(ap);
 146 
 147     /* Add a newline. */
 148     addch('\n');
 149 
 150     clrtoeol();
 151     refresh();
 152     sleep(2);
 153 }
 154 
 155 G_GNUC_PRINTF(2, 3)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 static int
 157 curses_info(pcmk__output_t *out, const char *format, ...) {
 158     va_list ap;
 159 
 160     CRM_ASSERT(out != NULL);
 161 
 162     if (out->is_quiet(out)) {
 163         return pcmk_rc_no_output;
 164     }
 165 
 166     /* Informational output does not get indented, to separate it from other
 167      * potentially indented list output.
 168      */
 169     va_start(ap, format);
 170     vw_printw(stdscr, format, ap);
 171     va_end(ap);
 172 
 173     /* Add a newline. */
 174     addch('\n');
 175 
 176     clrtoeol();
 177     refresh();
 178     return pcmk_rc_ok;
 179 }
 180 
 181 static void
 182 curses_output_xml(pcmk__output_t *out, const char *name, const char *buf) {
     /* [previous][next][first][last][top][bottom][index][help] */
 183     CRM_ASSERT(out != NULL);
 184     curses_indented_printf(out, "%s", buf);
 185 }
 186 
 187 G_GNUC_PRINTF(4, 5)
     /* [previous][next][first][last][top][bottom][index][help] */
 188 static void
 189 curses_begin_list(pcmk__output_t *out, const char *singular_noun, const char *plural_noun,
 190                   const char *format, ...) {
 191     private_data_t *priv = NULL;
 192     curses_list_data_t *new_list = NULL;
 193     va_list ap;
 194 
 195     CRM_ASSERT(out != NULL && out->priv != NULL);
 196     priv = out->priv;
 197 
 198     /* Empty formats can be used to create a new level of indentation, but without
 199      * displaying some sort of list header.  In that case we need to not do any of
 200      * this stuff. vw_printw will act weird if told to print a NULL.
 201      */
 202     if (format != NULL) {
 203         va_start(ap, format);
 204 
 205         curses_indented_vprintf(out, format, ap);
 206         printw(":\n");
 207 
 208         va_end(ap);
 209     }
 210 
 211     new_list = calloc(1, sizeof(curses_list_data_t));
 212     new_list->len = 0;
 213     pcmk__str_update(&new_list->singular_noun, singular_noun);
 214     pcmk__str_update(&new_list->plural_noun, plural_noun);
 215 
 216     g_queue_push_tail(priv->parent_q, new_list);
 217 }
 218 
 219 G_GNUC_PRINTF(3, 4)
     /* [previous][next][first][last][top][bottom][index][help] */
 220 static void
 221 curses_list_item(pcmk__output_t *out, const char *id, const char *format, ...) {
 222     va_list ap;
 223 
 224     CRM_ASSERT(out != NULL);
 225 
 226     va_start(ap, format);
 227 
 228     if (id != NULL) {
 229         curses_indented_printf(out, "%s: ", id);
 230         vw_printw(stdscr, format, ap);
 231     } else {
 232         curses_indented_vprintf(out, format, ap);
 233     }
 234 
 235     addch('\n');
 236     va_end(ap);
 237 
 238     out->increment_list(out);
 239 }
 240 
 241 static void
 242 curses_increment_list(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 243     private_data_t *priv = NULL;
 244     gpointer tail;
 245 
 246     CRM_ASSERT(out != NULL && out->priv != NULL);
 247     priv = out->priv;
 248 
 249     tail = g_queue_peek_tail(priv->parent_q);
 250     CRM_ASSERT(tail != NULL);
 251     ((curses_list_data_t *) tail)->len++;
 252 }
 253 
 254 static void
 255 curses_end_list(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 256     private_data_t *priv = NULL;
 257     curses_list_data_t *node = NULL;
 258 
 259     CRM_ASSERT(out != NULL && out->priv != NULL);
 260     priv = out->priv;
 261 
 262     node = g_queue_pop_tail(priv->parent_q);
 263 
 264     if (node->singular_noun != NULL && node->plural_noun != NULL) {
 265         if (node->len == 1) {
 266             curses_indented_printf(out, "%d %s found\n", node->len, node->singular_noun);
 267         } else {
 268             curses_indented_printf(out, "%d %s found\n", node->len, node->plural_noun);
 269         }
 270     }
 271 
 272     free(node);
 273 }
 274 
 275 static bool
 276 curses_is_quiet(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 277     CRM_ASSERT(out != NULL);
 278     return out->quiet;
 279 }
 280 
 281 static void
 282 curses_spacer(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 283     CRM_ASSERT(out != NULL);
 284     addch('\n');
 285 }
 286 
 287 static void
 288 curses_progress(pcmk__output_t *out, bool end) {
     /* [previous][next][first][last][top][bottom][index][help] */
 289     CRM_ASSERT(out != NULL);
 290 
 291     if (end) {
 292         printw(".\n");
 293     } else {
 294         addch('.');
 295     }
 296 }
 297 
 298 static void
 299 curses_prompt(const char *prompt, bool do_echo, char **dest)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301     int rc = OK;
 302 
 303     CRM_ASSERT(prompt != NULL && dest != NULL);
 304 
 305     /* This is backwards from the text version of this function on purpose.  We
 306      * disable echo by default in curses_init, so we need to enable it here if
 307      * asked for.
 308      */
 309     if (do_echo) {
 310         rc = echo();
 311     }
 312 
 313     if (rc == OK) {
 314         printw("%s: ", prompt);
 315 
 316         if (*dest != NULL) {
 317             free(*dest);
 318         }
 319 
 320         *dest = calloc(1, 1024);
 321         /* On older systems, scanw is defined as taking a char * for its first argument,
 322          * while newer systems rightly want a const char *.  Accomodate both here due
 323          * to building with -Werror.
 324          */
 325         rc = scanw((NCURSES_CONST char *) "%1023s", *dest);
 326         addch('\n');
 327     }
 328 
 329     if (rc < 1) {
 330         free(*dest);
 331         *dest = NULL;
 332     }
 333 
 334     if (do_echo) {
 335         noecho();
 336     }
 337 }
 338 
 339 pcmk__output_t *
 340 crm_mon_mk_curses_output(char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
 341     pcmk__output_t *retval = calloc(1, sizeof(pcmk__output_t));
 342 
 343     if (retval == NULL) {
 344         return NULL;
 345     }
 346 
 347     retval->fmt_name = "console";
 348     retval->request = pcmk__quote_cmdline(argv);
 349 
 350     retval->init = curses_init;
 351     retval->free_priv = curses_free_priv;
 352     retval->finish = curses_finish;
 353     retval->reset = curses_reset;
 354 
 355     retval->register_message = pcmk__register_message;
 356     retval->message = pcmk__call_message;
 357 
 358     retval->subprocess_output = curses_subprocess_output;
 359     retval->version = curses_ver;
 360     retval->err = curses_error;
 361     retval->info = curses_info;
 362     retval->output_xml = curses_output_xml;
 363 
 364     retval->begin_list = curses_begin_list;
 365     retval->list_item = curses_list_item;
 366     retval->increment_list = curses_increment_list;
 367     retval->end_list = curses_end_list;
 368 
 369     retval->is_quiet = curses_is_quiet;
 370     retval->spacer = curses_spacer;
 371     retval->progress = curses_progress;
 372     retval->prompt = curses_prompt;
 373 
 374     return retval;
 375 }
 376 
 377 G_GNUC_PRINTF(2, 0)
     /* [previous][next][first][last][top][bottom][index][help] */
 378 void
 379 curses_formatted_vprintf(pcmk__output_t *out, const char *format, va_list args) {
 380     vw_printw(stdscr, format, args);
 381 
 382     clrtoeol();
 383     refresh();
 384 }
 385 
 386 G_GNUC_PRINTF(2, 3)
     /* [previous][next][first][last][top][bottom][index][help] */
 387 void
 388 curses_formatted_printf(pcmk__output_t *out, const char *format, ...) {
 389     va_list ap;
 390 
 391     va_start(ap, format);
 392     curses_formatted_vprintf(out, format, ap);
 393     va_end(ap);
 394 }
 395 
 396 G_GNUC_PRINTF(2, 0)
     /* [previous][next][first][last][top][bottom][index][help] */
 397 void
 398 curses_indented_vprintf(pcmk__output_t *out, const char *format, va_list args) {
 399     int level = 0;
 400     private_data_t *priv = NULL;
 401 
 402     CRM_ASSERT(out != NULL && out->priv != NULL);
 403 
 404     priv = out->priv;
 405 
 406     level = g_queue_get_length(priv->parent_q);
 407 
 408     for (int i = 0; i < level; i++) {
 409         printw("  ");
 410     }
 411 
 412     if (level > 0) {
 413         printw("* ");
 414     }
 415 
 416     curses_formatted_vprintf(out, format, args);
 417 }
 418 
 419 G_GNUC_PRINTF(2, 3)
     /* [previous][next][first][last][top][bottom][index][help] */
 420 void
 421 curses_indented_printf(pcmk__output_t *out, const char *format, ...) {
 422     va_list ap;
 423 
 424     va_start(ap, format);
 425     curses_indented_vprintf(out, format, ap);
 426     va_end(ap);
 427 }
 428 
 429 PCMK__OUTPUT_ARGS("maint-mode", "unsigned long long int")
     /* [previous][next][first][last][top][bottom][index][help] */
 430 static int
 431 cluster_maint_mode_console(pcmk__output_t *out, va_list args) {
 432     unsigned long long flags = va_arg(args, unsigned long long);
 433 
 434     if (pcmk_is_set(flags, pe_flag_maintenance_mode)) {
 435         curses_formatted_printf(out, "\n              *** Resource management is DISABLED ***\n");
 436         curses_formatted_printf(out, "  The cluster will not attempt to start, stop or recover services\n");
 437         return pcmk_rc_ok;
 438     } else if (pcmk_is_set(flags, pe_flag_stop_everything)) {
 439         curses_formatted_printf(out, "\n    *** Resource management is DISABLED ***\n");
 440         curses_formatted_printf(out, "  The cluster will keep all resources stopped\n");
 441         return pcmk_rc_ok;
 442     } else {
 443         return pcmk_rc_no_output;
 444     }
 445 }
 446 
 447 PCMK__OUTPUT_ARGS("cluster-status", "pe_working_set_t *", "crm_exit_t",
     /* [previous][next][first][last][top][bottom][index][help] */
 448                   "stonith_history_t *", "enum pcmk__fence_history", "uint32_t",
 449                   "uint32_t", "const char *", "GList *", "GList *")
 450 static int
 451 cluster_status_console(pcmk__output_t *out, va_list args) {
 452     int rc = pcmk_rc_no_output;
 453 
 454     blank_screen();
 455     rc = pcmk__cluster_status_text(out, args);
 456     refresh();
 457     return rc;
 458 }
 459 
 460 PCMK__OUTPUT_ARGS("stonith-event", "stonith_history_t *", "bool", "bool",
     /* [previous][next][first][last][top][bottom][index][help] */
 461                   "const char *", "uint32_t")
 462 static int
 463 stonith_event_console(pcmk__output_t *out, va_list args)
 464 {
 465     stonith_history_t *event = va_arg(args, stonith_history_t *);
 466     bool full_history = va_arg(args, int);
 467     bool completed_only G_GNUC_UNUSED = va_arg(args, int);
 468     const char *succeeded = va_arg(args, const char *);
 469     uint32_t show_opts = va_arg(args, uint32_t);
 470 
 471     gchar *desc = stonith__history_description(event, full_history, succeeded,
 472                                                show_opts);
 473 
 474 
 475     curses_indented_printf(out, "%s\n", desc);
 476     g_free(desc);
 477     return pcmk_rc_ok;
 478 }
 479 
 480 static pcmk__message_entry_t fmt_functions[] = {
 481     { "cluster-status", "console", cluster_status_console },
 482     { "maint-mode", "console", cluster_maint_mode_console },
 483     { "stonith-event", "console", stonith_event_console },
 484 
 485     { NULL, NULL, NULL }
 486 };
 487 
 488 #endif
 489 
 490 void
 491 crm_mon_register_messages(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
 492 #if CURSES_ENABLED
 493     pcmk__register_messages(out, fmt_functions);
 494 #endif
 495 }
 496 
 497 void
 498 blank_screen(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 499 {
 500 #if CURSES_ENABLED
 501     int lpc = 0;
 502 
 503     for (lpc = 0; lpc < LINES; lpc++) {
 504         move(lpc, 0);
 505         clrtoeol();
 506     }
 507     move(0, 0);
 508     refresh();
 509 #endif
 510 }

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