19 #include <sys/types.h>
33 static char *crm_short_options = NULL;
35 static const char *crm_app_description = NULL;
36 static const char *crm_app_usage = NULL;
41 free(crm_short_options);
42 crm_short_options = NULL;
45 static struct option *
48 struct option *long_opts = NULL;
51 int index = 0, lpc = 0;
60 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
61 long_opts[index].name =
"__dummmy__";
62 long_opts[index].has_arg = 0;
63 long_opts[index].flag = 0;
64 long_opts[index].val =
'_';
69 for (lpc = 0; long_options[lpc].
name != NULL; lpc++) {
70 if (long_options[lpc].
name[0] ==
'-') {
74 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
77 long_opts[index].name = long_options[lpc].
name;
78 long_opts[index].has_arg = long_options[lpc].
has_arg;
79 long_opts[index].flag = long_options[lpc].
flag;
80 long_opts[index].val = long_options[lpc].
val;
85 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
86 long_opts[index].name = NULL;
87 long_opts[index].has_arg = 0;
88 long_opts[index].flag = 0;
89 long_opts[index].val = 0;
109 crm_short_options = strdup(short_options);
111 }
else if (long_options) {
113 int opt_string_len = 0;
114 char *local_short_options = NULL;
116 for (lpc = 0; long_options[lpc].
name != NULL; lpc++) {
117 if (long_options[lpc].val && long_options[lpc].val !=
'-' && long_options[lpc].val < UCHAR_MAX) {
118 local_short_options = realloc_safe(local_short_options, opt_string_len + 4);
119 local_short_options[opt_string_len++] = long_options[lpc].
val;
121 if (long_options[lpc].has_arg == optional_argument) {
122 local_short_options[opt_string_len++] =
':';
125 local_short_options[opt_string_len++] =
':';
127 local_short_options[opt_string_len] = 0;
130 crm_short_options = local_short_options;
131 crm_trace(
"Generated short option string: '%s'", local_short_options);
135 crm_long_options = long_options;
138 crm_app_description = app_desc;
141 crm_app_usage = app_usage;
149 static struct option *long_opts = NULL;
151 if (long_opts == NULL && crm_long_options) {
152 long_opts = create_long_opts(crm_long_options);
157 int flag = getopt_long(argc, argv, crm_short_options, long_opts, index);
161 if (long_opts[*index].val) {
162 return long_opts[*index].val;
163 }
else if (longname) {
164 *longname = long_opts[*index].name;
183 if (crm_short_options) {
184 return getopt(argc, argv, crm_short_options);
194 FILE *stream = (exit_code ? stderr : stdout);
196 if (cmd ==
'v' || cmd ==
'$') {
198 fprintf(stream,
"Written by Andrew Beekhof\n");
213 if (crm_long_options) {
214 fprintf(stream,
"Options:\n");
215 for (i = 0; crm_long_options[i].
name != NULL; i++) {
219 fprintf(stream,
"%s\n\n", crm_long_options[i].desc);
222 fprintf(stream,
"\t#%s\n\n", crm_long_options[i].desc);
224 }
else if (crm_long_options[i].val ==
'-' && crm_long_options[i].desc) {
225 fprintf(stream,
"%s\n", crm_long_options[i].desc);
229 if (crm_long_options[i].val && crm_long_options[i].val <= UCHAR_MAX) {
230 fprintf(stream,
" -%c,", crm_long_options[i].val);
234 fprintf(stream,
" --%s%s\t%s\n", crm_long_options[i].
name,
235 crm_long_options[i].has_arg == optional_argument ?
"[=value]" :
237 crm_long_options[i].desc ? crm_long_options[i].desc :
"");
241 }
else if (crm_short_options) {
242 fprintf(stream,
"Usage: %s - %s\n",
crm_system_name, crm_app_description);
243 for (i = 0; crm_short_options[i] != 0; i++) {
246 if (crm_short_options[i + 1] ==
':') {
247 if (crm_short_options[i + 2] ==
':')
248 has_arg = optional_argument ;
253 fprintf(stream,
" -%c %s\n", crm_short_options[i],
254 has_arg == optional_argument ?
"[value]" :
287 const char *value = NULL;
289 snprintf(env_name,
NAME_MAX,
"PCMK_%s", option);
290 value = getenv(env_name);
292 crm_trace(
"Found %s = %s", env_name, value);
296 snprintf(env_name,
NAME_MAX,
"HA_%s", option);
297 value = getenv(env_name);
299 crm_trace(
"Found %s = %s", env_name, value);
303 crm_trace(
"Nothing found for %s", option);
321 snprintf(env_name,
NAME_MAX,
"PCMK_%s", option);
323 crm_trace(
"Setting %s to %s", env_name, value);
324 setenv(env_name, value, 1);
330 snprintf(env_name,
NAME_MAX,
"HA_%s", option);
332 crm_trace(
"Setting %s to %s", env_name, value);
333 setenv(env_name, value, 1);
358 return (value != NULL) && (
crm_is_true(value) || strstr(value, daemon));
422 if (stat(value, &st) != 0) {
423 crm_err(
"Script %s does not exist", value);
427 if (S_ISREG(st.st_mode) == 0) {
428 crm_err(
"Script %s is not a regular file", value);
432 if ((st.st_mode & (S_IXUSR | S_IXGRP)) == 0) {
433 crm_err(
"Script %s is not executable", value);
444 long number = strtol(value, &end, 10);
446 if (end && (end[0] !=
'%')) {
465 cluster_option_value(GHashTable *options,
bool (*validate)(
const char *),
466 const char *
name,
const char *old_name,
467 const char *def_value)
469 const char *value = NULL;
470 char *new_value = NULL;
475 value = g_hash_table_lookup(options, name);
477 if ((value == NULL) && old_name) {
478 value = g_hash_table_lookup(options, old_name);
481 "option '%s' is deprecated and will be "
482 "removed in a future release",
486 new_value = strdup(value);
487 g_hash_table_insert(options, strdup(name), new_value);
492 if (value && validate && (validate(value) == FALSE)) {
494 "because '%s' is invalid", name, value);
507 crm_trace(
"No value or default provided for cluster option '%s'",
514 crm_err(
"Bug: default value for cluster option '%s' is invalid", name);
518 crm_trace(
"Using default value '%s' for cluster option '%s'",
521 new_value = strdup(value);
522 g_hash_table_insert(options, strdup(name), new_value);
540 int len,
const char *name)
542 const char *value = NULL;
544 for (
int lpc = 0; lpc < len; lpc++) {
546 value = cluster_option_value(options, option_list[lpc].is_valid,
547 option_list[lpc].name,
548 option_list[lpc].alt_name,
549 option_list[lpc].default_value);
559 const char *desc_short,
const char *desc_long,
564 fprintf(stdout,
"<?xml version=\"1.0\"?>"
565 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"
566 "<resource-agent name=\"%s\">\n"
567 " <version>%s</version>\n"
568 " <longdesc lang=\"en\">%s</longdesc>\n"
569 " <shortdesc lang=\"en\">%s</shortdesc>\n"
570 " <parameters>\n", name, version, desc_long, desc_short);
572 for (lpc = 0; lpc < len; lpc++) {
573 if ((option_list[lpc].description_long == NULL)
574 && (option_list[lpc].description_short == NULL)) {
577 fprintf(stdout,
" <parameter name=\"%s\" unique=\"0\">\n"
578 " <shortdesc lang=\"en\">%s</shortdesc>\n"
579 " <content type=\"%s\" default=\"%s\"/>\n"
580 " <longdesc lang=\"en\">%s%s%s</longdesc>\n"
582 option_list[lpc].name,
583 option_list[lpc].description_short,
584 option_list[lpc].
type,
585 option_list[lpc].default_value,
586 option_list[lpc].description_long?
587 option_list[lpc].description_long :
588 option_list[lpc].description_short,
589 (option_list[lpc].values?
" Allowed values: " :
""),
590 (option_list[lpc].values? option_list[lpc].values :
""));
592 fprintf(stdout,
" </parameters>\n</resource-agent>\n");
599 for (
int lpc = 0; lpc < len; lpc++) {
600 cluster_option_value(options, option_list[lpc].is_valid,
601 option_list[lpc].name,
602 option_list[lpc].alt_name,
603 option_list[lpc].default_value);
bool pcmk__valid_script(const char *value)
#define CRM_CHECK(expr, failure_action)
#define crm_notice(fmt, args...)
_Noreturn crm_exit_t crm_exit(crm_exit_t rc)
#define pcmk__config_err(fmt...)
#define required_argument
enum crm_exit_e crm_exit_t
void pcmk__set_env_option(const char *option, const char *value)
Set or unset a Pacemaker environment variable option.
guint crm_parse_interval_spec(const char *input)
Parse milliseconds from a Pacemaker interval specification.
_Noreturn void pcmk__cli_help(char cmd, crm_exit_t exit_code)
const char * pcmk__env_option(const char *option)
#define PACEMAKER_VERSION
bool pcmk__valid_positive_number(const char *value)
bool pcmk__valid_quorum(const char *value)
bool pcmk__valid_interval_spec(const char *value)
int daemon(int nochdir, int noclose)
#define crm_trace(fmt, args...)
int setenv(const char *name, const char *value, int why)
void pcmk__set_cli_options(const char *short_options, const char *usage, pcmk__cli_option_t *long_options, const char *app_desc)
const char * pcmk__cluster_option(GHashTable *options, pcmk__cluster_option_t *option_list, int len, const char *name)
bool pcmk_str_is_infinity(const char *s)
void pcmk__print_option_metadata(const char *name, const char *version, const char *desc_short, const char *desc_long, pcmk__cluster_option_t *option_list, int len)
bool pcmk__valid_utilization(const char *value)
bool pcmk__valid_boolean(const char *value)
int crm_str_to_boolean(const char *s, int *ret)
long long crm_parse_ll(const char *text, const char *default_text)
Parse a long long integer value from a string.
void pcmk__cli_option_cleanup(void)
int pcmk__next_cli_option(int argc, char **argv, int *index, const char **longname)
#define crm_err(fmt, args...)
bool pcmk_str_is_minus_infinity(const char *s)
bool pcmk__valid_number(const char *value)
#define PACKAGE_BUGREPORT
void pcmk__validate_cluster_options(GHashTable *options, pcmk__cluster_option_t *option_list, int len)
gboolean crm_is_true(const char *s)
#define safe_str_eq(a, b)
#define pcmk__config_warn(fmt...)
bool pcmk__env_option_enabled(const char *daemon, const char *option)
enum crm_ais_msg_types type