17 #include <sys/types.h>
20 #include <sys/utsname.h>
32 #include <qb/qbdefs.h>
43 #include <libxml2/libxml/relaxng.h>
54 # define PW_BUFFER_LEN 500
67 static struct crm_option *crm_long_options = NULL;
68 static const char *crm_app_description = NULL;
69 static char *crm_short_options = NULL;
70 static const char *crm_app_usage = NULL;
158 if(stat(value, &st) != 0) {
159 crm_err(
"Script %s does not exist", value);
163 if(S_ISREG(st.st_mode) == 0) {
164 crm_err(
"Script %s is not a regular file", value);
168 if( (st.st_mode & (S_IXUSR | S_IXGRP )) == 0) {
169 crm_err(
"Script %s is not executable", value);
180 long number = strtol(value, &end, 10);
182 if(end && end[0] !=
'%') {
184 }
else if(number < 0) {
194 free(crm_short_options);
195 crm_short_options = NULL;
259 return crm_itoa(score);
264 const char *name,
const char *old_name,
const char *def_value)
266 const char *value = NULL;
267 char *new_value = NULL;
272 value = g_hash_table_lookup(options, name);
274 if ((value == NULL) && old_name) {
275 value = g_hash_table_lookup(options, old_name);
278 " is deprecated and will be removed in a future release",
282 new_value = strdup(value);
283 g_hash_table_insert(options, strdup(name), new_value);
288 if (value && validate && (validate(value) == FALSE)) {
289 crm_config_err(
"Resetting cluster option '%s' to default: value '%s' is invalid",
303 crm_trace(
"No value or default provided for cluster option '%s'",
310 crm_err(
"Bug: default value for cluster option '%s' is invalid", name);
314 crm_trace(
"Using default value '%s' for cluster option '%s'",
317 new_value = strdup(value);
318 g_hash_table_insert(options, strdup(name), new_value);
327 const char *value = NULL;
329 for (
int lpc = 0; lpc < len; lpc++) {
332 option_list[lpc].is_valid,
333 option_list[lpc].name,
334 option_list[lpc].alt_name,
335 option_list[lpc].default_value);
349 fprintf(stdout,
"<?xml version=\"1.0\"?>"
350 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"
351 "<resource-agent name=\"%s\">\n"
352 " <version>%s</version>\n"
353 " <longdesc lang=\"en\">%s</longdesc>\n"
354 " <shortdesc lang=\"en\">%s</shortdesc>\n"
355 " <parameters>\n", name,
version, desc_long, desc_short);
357 for (lpc = 0; lpc < len; lpc++) {
358 if (option_list[lpc].description_long == NULL && option_list[lpc].description_short == NULL) {
361 fprintf(stdout,
" <parameter name=\"%s\" unique=\"0\">\n"
362 " <shortdesc lang=\"en\">%s</shortdesc>\n"
363 " <content type=\"%s\" default=\"%s\"/>\n"
364 " <longdesc lang=\"en\">%s%s%s</longdesc>\n"
366 option_list[lpc].name,
367 option_list[lpc].description_short,
368 option_list[lpc].
type,
369 option_list[lpc].default_value,
370 option_list[lpc].description_long ? option_list[lpc].
371 description_long : option_list[lpc].description_short,
372 option_list[lpc].values ?
" Allowed values: " :
"",
373 option_list[lpc].values ? option_list[lpc].values :
"");
375 fprintf(stdout,
" </parameters>\n</resource-agent>\n");
383 for (lpc = 0; lpc < len; lpc++) {
385 option_list[lpc].is_valid,
386 option_list[lpc].name,
387 option_list[lpc].alt_name, option_list[lpc].default_value);
394 char *hash_key = crm_concat(sys ? sys :
"none", crm_msg_reference,
'_');
396 crm_trace(
"created hash key: (%s)", hash_key);
407 struct passwd *pwentry = NULL;
410 rc = getpwnam_r(name, &pwd, buffer,
PW_BUFFER_LEN, &pwentry);
413 *uid = pwentry->pw_uid;
416 *gid = pwentry->pw_gid;
418 crm_trace(
"User %s has uid=%d gid=%d", name, pwentry->pw_uid, pwentry->pw_gid);
421 rc = rc? -rc : -EINVAL;
430 crm_version_helper(
const char *text,
const char **end_text)
432 int atoi_result = -1;
438 if (text != NULL && text[0] != 0) {
445 atoi_result = (int) strtol(text, (
char **) end_text, 10);
447 if (errno == EINVAL) {
448 crm_err(
"Conversion of '%s' %c failed", text, text[0]);
465 const char *ver1_iter, *ver2_iter;
467 if (version1 == NULL && version2 == NULL) {
469 }
else if (version1 == NULL) {
471 }
else if (version2 == NULL) {
475 ver1_iter = version1;
476 ver2_iter = version2;
484 if (ver1_iter == ver2_iter) {
488 if (ver1_iter != NULL) {
489 digit1 = crm_version_helper(ver1_iter, &ver1_iter);
492 if (ver2_iter != NULL) {
493 digit2 = crm_version_helper(ver2_iter, &ver2_iter);
496 if (digit1 < digit2) {
500 }
else if (digit1 > digit2) {
505 if (ver1_iter != NULL && *ver1_iter ==
'.') {
508 if (ver1_iter != NULL && *ver1_iter ==
'\0') {
512 if (ver2_iter != NULL && *ver2_iter ==
'.') {
515 if (ver2_iter != NULL && *ver2_iter == 0) {
521 crm_trace(
"%s == %s (%d)", version1, version2, lpc);
523 crm_trace(
"%s < %s (%d)", version1, version2, lpc);
525 crm_trace(
"%s > %s (%d)", version1, version2, lpc);
534 # define NUMCHARS "0123456789."
538 # define WHITESPACE " \t\n\r\f"
549 }
else if (input[0] !=
'P') {
564 crm_warn(
"Using 0 instead of '%s'", input);
568 return (msec <= 0)? 0 : ((msec >= G_MAXUINT)? G_MAXUINT : (guint) msec);
574 const char *cp = input;
576 long long multiplier = 1000;
577 long long divisor = 1;
579 char *end_text = NULL;
591 if (strchr(
NUMCHARS, *cp) == NULL) {
595 if (strncasecmp(units,
"ms", 2) == 0 || strncasecmp(units,
"msec", 4) == 0) {
598 }
else if (strncasecmp(units,
"us", 2) == 0 || strncasecmp(units,
"usec", 4) == 0) {
601 }
else if (strncasecmp(units,
"s", 1) == 0 || strncasecmp(units,
"sec", 3) == 0) {
604 }
else if (strncasecmp(units,
"m", 1) == 0 || strncasecmp(units,
"min", 3) == 0) {
605 multiplier = 60 * 1000;
607 }
else if (strncasecmp(units,
"h", 1) == 0 || strncasecmp(units,
"hr", 2) == 0) {
608 multiplier = 60 * 60 * 1000;
610 }
else if (*units !=
EOS && *units !=
'\n' && *units !=
'\r') {
615 if (msec > LLONG_MAX/multiplier) {
630 crm_abort(
const char *file,
const char *
function,
int line,
631 const char *assert_condition, gboolean do_core, gboolean do_fork)
648 if (do_core == FALSE) {
649 crm_err(
"%s: Triggered assert at %s:%d : %s",
function, file, line, assert_condition);
652 }
else if (do_fork) {
656 crm_err(
"%s: Triggered fatal assert at %s:%d : %s",
function, file, line, assert_condition);
660 crm_crit(
"%s: Cannot create core for non-fatal assert at %s:%d : %s",
661 function, file, line, assert_condition);
664 }
else if(
pid == 0) {
671 crm_err(
"%s: Forked child %d to record non-fatal assert at %s:%d : %s",
672 function,
pid, file, line, assert_condition);
676 rc = waitpid(
pid, &status, 0);
681 }
while(errno == EINTR);
683 if (errno == ECHILD) {
685 crm_trace(
"Cannot wait on forked child %d - SIGCHLD is probably set to SIG_IGN",
pid);
696 const char *devnull =
"/dev/null";
698 if (daemonize == FALSE) {
704 if(rc <
pcmk_ok && rc != -ENOENT) {
706 crm_err(
"%s: already running [pid %ld in %s]", name,
pid, pidfile);
707 printf(
"%s: already running [pid %ld in %s]\n", name,
pid, pidfile);
713 fprintf(stderr,
"%s: could not start daemon\n", name);
717 }
else if (
pid > 0) {
724 printf(
"Could not lock '%s' for %s: %s (%d)\n", pidfile, name,
pcmk_strerror(rc), rc);
728 umask(S_IWGRP | S_IWOTH | S_IROTH);
731 (void)open(devnull, O_RDONLY);
732 close(STDOUT_FILENO);
733 (void)open(devnull, O_WRONLY);
734 close(STDERR_FILENO);
735 (void)open(devnull, O_WRONLY);
743 char *crm_name = NULL;
746 crm_name = crm_concat(
CRM_META, field,
'_');
749 max = strlen(crm_name);
750 for (; lpc < max; lpc++) {
751 switch (crm_name[lpc]) {
764 const char *value = NULL;
768 value = g_hash_table_lookup(hash, key);
775 static struct option *
776 crm_create_long_opts(
struct crm_option *long_options)
778 struct option *long_opts = NULL;
781 int index = 0, lpc = 0;
790 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
791 long_opts[index].name =
"__dummmy__";
792 long_opts[index].has_arg = 0;
793 long_opts[index].flag = 0;
794 long_opts[index].val =
'_';
797 for (lpc = 0; long_options[lpc].
name != NULL; lpc++) {
798 if (long_options[lpc].name[0] ==
'-') {
802 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
805 long_opts[index].name = long_options[lpc].
name;
806 long_opts[index].has_arg = long_options[lpc].
has_arg;
807 long_opts[index].flag = long_options[lpc].
flag;
808 long_opts[index].val = long_options[lpc].
val;
813 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
814 long_opts[index].name = NULL;
815 long_opts[index].has_arg = 0;
816 long_opts[index].flag = 0;
817 long_opts[index].val = 0;
825 const char *app_desc)
828 crm_short_options = strdup(short_options);
830 }
else if (long_options) {
832 int opt_string_len = 0;
833 char *local_short_options = NULL;
835 for (lpc = 0; long_options[lpc].
name != NULL; lpc++) {
836 if (long_options[lpc].val && long_options[lpc].val !=
'-' && long_options[lpc].val < UCHAR_MAX) {
837 local_short_options = realloc_safe(local_short_options, opt_string_len + 4);
838 local_short_options[opt_string_len++] = long_options[lpc].
val;
840 if (long_options[lpc].has_arg == optional_argument) {
841 local_short_options[opt_string_len++] =
':';
843 if (long_options[lpc].has_arg >= required_argument) {
844 local_short_options[opt_string_len++] =
':';
846 local_short_options[opt_string_len] = 0;
849 crm_short_options = local_short_options;
850 crm_trace(
"Generated short option string: '%s'", local_short_options);
854 crm_long_options = long_options;
857 crm_app_description = app_desc;
860 crm_app_usage = app_usage;
874 static struct option *long_opts = NULL;
876 if (long_opts == NULL && crm_long_options) {
877 long_opts = crm_create_long_opts(crm_long_options);
882 int flag = getopt_long(argc, argv, crm_short_options, long_opts, index);
886 if (long_opts[*index].val) {
887 return long_opts[*index].val;
888 }
else if (longname) {
889 *longname = long_opts[*index].name;
891 crm_notice(
"Unhandled option --%s", long_opts[*index].name);
908 if (crm_short_options) {
909 return getopt(argc, argv, crm_short_options);
919 FILE *stream = (exit_code ? stderr : stdout);
921 if (cmd ==
'v' || cmd ==
'$') {
923 fprintf(stream,
"Written by Andrew Beekhof\n");
938 if (crm_long_options) {
939 fprintf(stream,
"Options:\n");
940 for (i = 0; crm_long_options[i].
name != NULL; i++) {
944 fprintf(stream,
"%s\n\n", crm_long_options[i].desc);
947 fprintf(stream,
"\t#%s\n\n", crm_long_options[i].desc);
949 }
else if (crm_long_options[i].val ==
'-' && crm_long_options[i].desc) {
950 fprintf(stream,
"%s\n", crm_long_options[i].desc);
954 if (crm_long_options[i].val && crm_long_options[i].val <= UCHAR_MAX) {
955 fprintf(stream,
" -%c,", crm_long_options[i].val);
959 fprintf(stream,
" --%s%s\t%s\n", crm_long_options[i].name,
960 crm_long_options[i].has_arg == optional_argument ?
"[=value]" :
961 crm_long_options[i].has_arg == required_argument ?
"=value" :
"",
962 crm_long_options[i].desc ? crm_long_options[i].desc :
"");
966 }
else if (crm_short_options) {
967 fprintf(stream,
"Usage: %s - %s\n",
crm_system_name, crm_app_description);
968 for (i = 0; crm_short_options[i] != 0; i++) {
969 int has_arg = no_argument ;
971 if (crm_short_options[i + 1] ==
':') {
972 if (crm_short_options[i + 2] ==
':')
973 has_arg = optional_argument ;
975 has_arg = required_argument ;
978 fprintf(stream,
" -%c %s\n", crm_short_options[i],
979 has_arg == optional_argument ?
"[value]" :
980 has_arg == required_argument ?
"{value}" :
"");
993 qb_ipcs_service_t **ipcs_rw,
994 qb_ipcs_service_t **ipcs_shm,
995 struct qb_ipcs_service_handlers *ro_cb,
996 struct qb_ipcs_service_handlers *rw_cb)
1002 if (*ipcs_ro == NULL || *ipcs_rw == NULL || *ipcs_shm == NULL) {
1003 crm_err(
"Failed to create the CIB manager: exiting and inhibiting respawn");
1004 crm_warn(
"Verify pacemaker and pacemaker_remote are not both enabled");
1010 qb_ipcs_service_t *ipcs_rw,
1011 qb_ipcs_service_t *ipcs_shm)
1013 qb_ipcs_destroy(ipcs_ro);
1014 qb_ipcs_destroy(ipcs_rw);
1015 qb_ipcs_destroy(ipcs_shm);
1029 if (*ipcs == NULL) {
1030 crm_err(
"Failed to create pacemaker-attrd server: exiting and inhibiting respawn");
1031 crm_warn(
"Verify pacemaker and pacemaker_remote are not both enabled.");
1042 if (*ipcs == NULL) {
1043 crm_err(
"Failed to create fencer: exiting and inhibiting respawn.");
1044 crm_warn(
"Verify pacemaker and pacemaker_remote are not both enabled.");
1055 if (*handle == NULL) {
1056 *handle = dlopen(lib, RTLD_LAZY);
1060 crm_err(
"%sCould not open %s: %s", fatal ?
"Fatal: " :
"", lib, dlerror());
1067 a_function = dlsym(*handle, fn);
1068 if (a_function == NULL) {
1070 crm_err(
"%sCould not find %s in %s: %s", fatal ?
"Fatal: " :
"", fn, lib, error);
1079 #ifdef HAVE_UUID_UUID_H
1080 # include <uuid/uuid.h>
1086 unsigned char uuid[16];
1087 char *buffer = malloc(37);
1089 uuid_generate(uuid);
1090 uuid_unparse(uuid, buffer);
1111 }
else if (!strcmp(name,
"pacemaker-attrd")) {
1114 }
else if (!strcmp(name,
"pacemaker-based")) {
1117 }
else if (!strcmp(name,
"pacemaker-controld")) {
1120 }
else if (!strcmp(name,
"pacemaker-execd")) {
1123 }
else if (!strcmp(name,
"pacemaker-fenced")) {
1124 return "stonith-ng";
1126 }
else if (!strcmp(name,
"pacemaker-schedulerd")) {
1147 || !strcmp(name,
"stonith-ng")
1148 || !strcmp(name,
"attrd")
1161 int lpc = 0, len = 0;
1162 char *digest = NULL;
1165 if (buffer == NULL) {
1168 len = strlen(buffer);
1170 crm_trace(
"Beginning digest of %d bytes", len);
1175 sprintf(digest + (2 * lpc),
"%02x", raw_digest[lpc]);
1181 crm_err(
"Could not create digest");
1186 #ifdef HAVE_GNUTLS_GNUTLS_H
1188 crm_gnutls_global_init(
void)
1190 signal(SIGPIPE, SIG_IGN);
1191 gnutls_global_init();
1203 struct utsname hostinfo;
1205 return (
uname(&hostinfo) < 0)? NULL : strdup(hostinfo.nodename);