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 68 static struct crm_option *crm_long_options = NULL;
69 static const char *crm_app_description = NULL;
70 static char *crm_short_options = NULL;
71 static const char *crm_app_usage = NULL;
86 free(crm_short_options);
187 if(stat(value, &st) != 0) {
188 crm_err(
"Script %s does not exist", value);
192 if(S_ISREG(st.st_mode) == 0) {
193 crm_err(
"Script %s is not a regular file", value);
197 if( (st.st_mode & (S_IXUSR | S_IXGRP )) == 0) {
198 crm_err(
"Script %s is not executable", value);
209 long number = strtol(value, &end, 10);
211 if(end && end[0] !=
'%') {
213 }
else if(number < 0) {
286 const char *name,
const char *old_name,
const char *def_value)
288 const char *value = NULL;
289 char *new_value = NULL;
294 value = g_hash_table_lookup(options, name);
296 if ((value == NULL) && old_name) {
297 value = g_hash_table_lookup(options, old_name);
300 " is deprecated and will be removed in a future release",
304 new_value = strdup(value);
305 g_hash_table_insert(options, strdup(name), new_value);
310 if (value && validate && (validate(value) == FALSE)) {
311 crm_config_err(
"Resetting cluster option '%s' to default: value '%s' is invalid",
325 crm_trace(
"No value or default provided for cluster option '%s'",
332 crm_err(
"Bug: default value for cluster option '%s' is invalid", name);
336 crm_trace(
"Using default value '%s' for cluster option '%s'",
339 new_value = strdup(value);
340 g_hash_table_insert(options, strdup(name), new_value);
349 const char *value = NULL;
351 for (
int lpc = 0; lpc < len; lpc++) {
354 option_list[lpc].is_valid,
355 option_list[lpc].name,
356 option_list[lpc].alt_name,
357 option_list[lpc].default_value);
371 fprintf(stdout,
"<?xml version=\"1.0\"?>" 372 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n" 373 "<resource-agent name=\"%s\">\n" 374 " <version>%s</version>\n" 375 " <longdesc lang=\"en\">%s</longdesc>\n" 376 " <shortdesc lang=\"en\">%s</shortdesc>\n" 377 " <parameters>\n", name, version, desc_long, desc_short);
379 for (lpc = 0; lpc < len; lpc++) {
380 if (option_list[lpc].description_long == NULL && option_list[lpc].description_short == NULL) {
383 fprintf(stdout,
" <parameter name=\"%s\" unique=\"0\">\n" 384 " <shortdesc lang=\"en\">%s</shortdesc>\n" 385 " <content type=\"%s\" default=\"%s\"/>\n" 386 " <longdesc lang=\"en\">%s%s%s</longdesc>\n" 388 option_list[lpc].name,
389 option_list[lpc].description_short,
390 option_list[lpc].
type,
391 option_list[lpc].default_value,
392 option_list[lpc].description_long ? option_list[lpc].
393 description_long : option_list[lpc].description_short,
394 option_list[lpc].values ?
" Allowed values: " :
"",
395 option_list[lpc].values ? option_list[lpc].values :
"");
397 fprintf(stdout,
" </parameters>\n</resource-agent>\n");
405 for (lpc = 0; lpc < len; lpc++) {
407 option_list[lpc].is_valid,
408 option_list[lpc].name,
409 option_list[lpc].alt_name, option_list[lpc].default_value);
416 char *hash_key =
crm_concat(sys ? sys :
"none", crm_msg_reference,
'_');
418 crm_trace(
"created hash key: (%s)", hash_key);
429 struct passwd *pwentry = NULL;
432 rc = getpwnam_r(name, &pwd, buffer,
PW_BUFFER_LEN, &pwentry);
435 *uid = pwentry->pw_uid;
438 *gid = pwentry->pw_gid;
440 crm_trace(
"User %s has uid=%d gid=%d", name, pwentry->pw_uid, pwentry->pw_gid);
443 rc = rc? -rc : -EINVAL;
452 crm_version_helper(
const char *text,
char **end_text)
454 int atoi_result = -1;
460 if (text != NULL && text[0] != 0) {
461 atoi_result = (int)strtol(text, end_text, 10);
463 if (errno == EINVAL) {
464 crm_err(
"Conversion of '%s' %c failed", text, text[0]);
481 char *ver1_copy = NULL, *ver2_copy = NULL;
482 char *rest1 = NULL, *rest2 = NULL;
484 if (version1 == NULL && version2 == NULL) {
486 }
else if (version1 == NULL) {
488 }
else if (version2 == NULL) {
492 ver1_copy = strdup(version1);
493 ver2_copy = strdup(version2);
503 if (rest1 == rest2) {
508 digit1 = crm_version_helper(rest1, &rest1);
512 digit2 = crm_version_helper(rest2, &rest2);
515 if (digit1 < digit2) {
519 }
else if (digit1 > digit2) {
524 if (rest1 != NULL && rest1[0] ==
'.') {
527 if (rest1 != NULL && rest1[0] == 0) {
531 if (rest2 != NULL && rest2[0] ==
'.') {
534 if (rest2 != NULL && rest2[0] == 0) {
543 crm_trace(
"%s == %s (%d)", version1, version2, lpc);
545 crm_trace(
"%s < %s (%d)", version1, version2, lpc);
547 crm_trace(
"%s > %s (%d)", version1, version2, lpc);
556 # define NUMCHARS "0123456789." 560 # define WHITESPACE " \t\n\r\f" 566 unsigned long long msec = 0;
571 }
else if (input[0] !=
'P') {
591 const char *cp = input;
593 long long multiplier = 1000;
594 long long divisor = 1;
596 char *end_text = NULL;
608 if (strchr(
NUMCHARS, *cp) == NULL) {
612 if (strncasecmp(units,
"ms", 2) == 0 || strncasecmp(units,
"msec", 4) == 0) {
615 }
else if (strncasecmp(units,
"us", 2) == 0 || strncasecmp(units,
"usec", 4) == 0) {
618 }
else if (strncasecmp(units,
"s", 1) == 0 || strncasecmp(units,
"sec", 3) == 0) {
621 }
else if (strncasecmp(units,
"m", 1) == 0 || strncasecmp(units,
"min", 3) == 0) {
622 multiplier = 60 * 1000;
624 }
else if (strncasecmp(units,
"h", 1) == 0 || strncasecmp(units,
"hr", 2) == 0) {
625 multiplier = 60 * 60 * 1000;
627 }
else if (*units !=
EOS && *units !=
'\n' && *units !=
'\r') {
632 if (msec > LLONG_MAX/multiplier) {
647 crm_abort(
const char *file,
const char *
function,
int line,
648 const char *assert_condition, gboolean do_core, gboolean do_fork)
657 if(crm_is_daemon == FALSE) {
665 if (do_core == FALSE) {
666 crm_err(
"%s: Triggered assert at %s:%d : %s",
function, file, line, assert_condition);
669 }
else if (do_fork) {
673 crm_err(
"%s: Triggered fatal assert at %s:%d : %s",
function, file, line, assert_condition);
677 crm_crit(
"%s: Cannot create core for non-fatal assert at %s:%d : %s",
678 function, file, line, assert_condition);
681 }
else if(pid == 0) {
688 crm_err(
"%s: Forked child %d to record non-fatal assert at %s:%d : %s",
689 function, pid, file, line, assert_condition);
693 rc = waitpid(pid, &status, 0);
698 }
while(errno == EINTR);
700 if (errno == ECHILD) {
702 crm_trace(
"Cannot wait on forked child %d - SIGCHLD is probably set to SIG_IGN", pid);
705 crm_perror(LOG_ERR,
"Cannot wait on forked child %d", pid);
711 static int last_asked_pid = 0;
713 static int have_proc_pid = 0;
715 static int have_proc_pid = -1;
719 if (have_proc_pid == 0) {
721 char proc_path[PATH_MAX], exe_path[PATH_MAX];
722 snprintf(proc_path,
sizeof(proc_path),
"/proc/%lu/exe",
723 (
long unsigned int) getpid());
725 if (readlink(proc_path, exe_path,
sizeof(exe_path) - 1) < 0) {
733 }
else if ((rc = kill(pid, 0)) < 0 && errno == ESRCH) {
736 }
else if (rc < 0 && (daemon == NULL || have_proc_pid == -1)) {
737 if (last_asked_pid != pid) {
739 last_asked_pid =
pid;
743 }
else if (rc == 0 && (daemon == NULL || have_proc_pid == -1)) {
746 }
else if (daemon != NULL) {
749 bool checked_through_kill = (rc == 0);
750 char proc_path[PATH_MAX], exe_path[PATH_MAX], myexe_path[PATH_MAX];
751 snprintf(proc_path,
sizeof(proc_path),
"/proc/%ld/exe", pid);
753 rc = readlink(proc_path, exe_path,
sizeof(exe_path) - 1);
754 if ((rc < 0) && (errno == EACCES)) {
755 if (last_asked_pid != pid) {
756 crm_info(
"Could not read from %s: %s", proc_path,
758 last_asked_pid =
pid;
760 return checked_through_kill ? 1 : -2;
762 if (last_asked_pid != pid) {
763 crm_err(
"Could not read from %s: %s (%d)", proc_path,
765 last_asked_pid =
pid;
771 if (daemon[0] !=
'/') {
772 rc = snprintf(myexe_path,
sizeof(myexe_path),
CRM_DAEMON_DIR"/%s",
775 rc = snprintf(myexe_path,
sizeof(myexe_path),
"%s", daemon);
778 if (rc > 0 && rc <
sizeof(myexe_path) && !strcmp(exe_path, myexe_path)) {
786 #define LOCKSTRLEN 11 796 if ((fd = open(filename, O_RDONLY)) < 0) {
800 if (fstat(fd, &sbuf) >= 0 && sbuf.st_size <
LOCKSTRLEN) {
806 if (read(fd, buf,
sizeof(buf)) < 1) {
810 if (sscanf(buf,
"%lu", &pid) > 0) {
814 crm_trace(
"Got pid %lu from %s\n", pid, filename);
835 }
else if (mypid && pid == mypid) {
844 }
else if (mypid && pid != mypid) {
853 crm_lock_pidfile(
const char *filename,
const char *name)
859 mypid = (
unsigned long)getpid();
870 if ((fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0644)) < 0) {
875 snprintf(buf,
sizeof(buf),
"%*lu\n",
LOCKSTRLEN - 1, mypid);
880 crm_perror(LOG_ERR,
"Incomplete write to %s", filename);
892 const char *devnull =
"/dev/null";
894 if (daemonize == FALSE) {
900 if(rc <
pcmk_ok && rc != -ENOENT) {
902 crm_err(
"%s: already running [pid %ld in %s]", name, pid, pidfile);
903 printf(
"%s: already running [pid %ld in %s]\n", name, pid, pidfile);
909 fprintf(stderr,
"%s: could not start daemon\n", name);
913 }
else if (pid > 0) {
917 rc = crm_lock_pidfile(pidfile, name);
920 printf(
"Could not lock '%s' for %s: %s (%d)\n", pidfile, name,
pcmk_strerror(rc), rc);
924 umask(S_IWGRP | S_IWOTH | S_IROTH);
927 (void)open(devnull, O_RDONLY);
928 close(STDOUT_FILENO);
929 (void)open(devnull, O_WRONLY);
930 close(STDERR_FILENO);
931 (void)open(devnull, O_WRONLY);
939 char *crm_name = NULL;
945 max = strlen(crm_name);
946 for (; lpc < max; lpc++) {
947 switch (crm_name[lpc]) {
960 const char *value = NULL;
964 value = g_hash_table_lookup(hash, key);
971 static struct option *
972 crm_create_long_opts(
struct crm_option *long_options)
974 struct option *long_opts = NULL;
977 int index = 0, lpc = 0;
986 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
987 long_opts[index].name =
"__dummmy__";
988 long_opts[index].has_arg = 0;
989 long_opts[index].flag = 0;
990 long_opts[index].val =
'_';
993 for (lpc = 0; long_options[lpc].
name != NULL; lpc++) {
994 if (long_options[lpc].name[0] ==
'-') {
998 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
1001 long_opts[index].name = long_options[lpc].
name;
1002 long_opts[index].has_arg = long_options[lpc].
has_arg;
1003 long_opts[index].flag = long_options[lpc].
flag;
1004 long_opts[index].val = long_options[lpc].
val;
1009 long_opts = realloc_safe(long_opts, (index + 1) *
sizeof(
struct option));
1010 long_opts[index].name = NULL;
1011 long_opts[index].has_arg = 0;
1012 long_opts[index].flag = 0;
1013 long_opts[index].val = 0;
1021 const char *app_desc)
1023 if (short_options) {
1024 crm_short_options = strdup(short_options);
1026 }
else if (long_options) {
1028 int opt_string_len = 0;
1029 char *local_short_options = NULL;
1031 for (lpc = 0; long_options[lpc].
name != NULL; lpc++) {
1032 if (long_options[lpc].val && long_options[lpc].val !=
'-' && long_options[lpc].val < UCHAR_MAX) {
1033 local_short_options = realloc_safe(local_short_options, opt_string_len + 4);
1034 local_short_options[opt_string_len++] = long_options[lpc].
val;
1036 if (long_options[lpc].has_arg == optional_argument) {
1037 local_short_options[opt_string_len++] =
':';
1039 if (long_options[lpc].has_arg >= required_argument) {
1040 local_short_options[opt_string_len++] =
':';
1042 local_short_options[opt_string_len] = 0;
1045 crm_short_options = local_short_options;
1046 crm_trace(
"Generated short option string: '%s'", local_short_options);
1050 crm_long_options = long_options;
1053 crm_app_description = app_desc;
1056 crm_app_usage = app_usage;
1069 #ifdef HAVE_GETOPT_H 1070 static struct option *long_opts = NULL;
1072 if (long_opts == NULL && crm_long_options) {
1073 long_opts = crm_create_long_opts(crm_long_options);
1078 int flag = getopt_long(argc, argv, crm_short_options, long_opts, index);
1082 if (long_opts[*index].val) {
1083 return long_opts[*index].val;
1084 }
else if (longname) {
1085 *longname = long_opts[*index].name;
1087 crm_notice(
"Unhandled option --%s", long_opts[*index].name);
1104 if (crm_short_options) {
1105 return getopt(argc, argv, crm_short_options);
1115 FILE *stream = (exit_code ? stderr : stdout);
1117 if (cmd ==
'v' || cmd ==
'$') {
1119 fprintf(stream,
"Written by Andrew Beekhof\n");
1130 if (crm_app_usage) {
1134 if (crm_long_options) {
1135 fprintf(stream,
"Options:\n");
1136 for (i = 0; crm_long_options[i].
name != NULL; i++) {
1140 fprintf(stream,
"%s\n\n", crm_long_options[i].desc);
1143 fprintf(stream,
"\t#%s\n\n", crm_long_options[i].desc);
1145 }
else if (crm_long_options[i].val ==
'-' && crm_long_options[i].desc) {
1146 fprintf(stream,
"%s\n", crm_long_options[i].desc);
1150 if (crm_long_options[i].val && crm_long_options[i].val <= UCHAR_MAX) {
1151 fprintf(stream,
" -%c,", crm_long_options[i].val);
1155 fprintf(stream,
" --%s%s\t%s\n", crm_long_options[i].name,
1156 crm_long_options[i].has_arg == optional_argument ?
"[=value]" :
1157 crm_long_options[i].has_arg == required_argument ?
"=value" :
"",
1158 crm_long_options[i].desc ? crm_long_options[i].desc :
"");
1162 }
else if (crm_short_options) {
1163 fprintf(stream,
"Usage: %s - %s\n",
crm_system_name, crm_app_description);
1164 for (i = 0; crm_short_options[i] != 0; i++) {
1165 int has_arg = no_argument ;
1167 if (crm_short_options[i + 1] ==
':') {
1168 if (crm_short_options[i + 2] ==
':')
1169 has_arg = optional_argument ;
1171 has_arg = required_argument ;
1174 fprintf(stream,
" -%c %s\n", crm_short_options[i],
1175 has_arg == optional_argument ?
"[value]" :
1176 has_arg == required_argument ?
"{value}" :
"");
1188 qb_ipcs_service_t **ipcs_rw,
1189 qb_ipcs_service_t **ipcs_shm,
1190 struct qb_ipcs_service_handlers *ro_cb,
1191 struct qb_ipcs_service_handlers *rw_cb)
1197 if (*ipcs_ro == NULL || *ipcs_rw == NULL || *ipcs_shm == NULL) {
1198 crm_err(
"Failed to create cib servers: exiting and inhibiting respawn.");
1199 crm_warn(
"Verify pacemaker and pacemaker_remote are not both enabled.");
1205 qb_ipcs_service_t *ipcs_rw,
1206 qb_ipcs_service_t *ipcs_shm)
1208 qb_ipcs_destroy(ipcs_ro);
1209 qb_ipcs_destroy(ipcs_rw);
1210 qb_ipcs_destroy(ipcs_shm);
1224 if (*ipcs == NULL) {
1225 crm_err(
"Failed to create attrd servers: exiting and inhibiting respawn.");
1226 crm_warn(
"Verify pacemaker and pacemaker_remote are not both enabled.");
1237 if (*ipcs == NULL) {
1238 crm_err(
"Failed to create stonith-ng servers: exiting and inhibiting respawn.");
1239 crm_warn(
"Verify pacemaker and pacemaker_remote are not both enabled.");
1250 if (*handle == NULL) {
1251 *handle = dlopen(lib, RTLD_LAZY);
1255 crm_err(
"%sCould not open %s: %s", fatal ?
"Fatal: " :
"", lib, dlerror());
1262 a_function = dlsym(*handle, fn);
1263 if (a_function == NULL) {
1265 crm_err(
"%sCould not find %s in %s: %s", fatal ?
"Fatal: " :
"", fn, lib, error);
1274 #ifdef HAVE_UUID_UUID_H 1275 # include <uuid/uuid.h> 1281 unsigned char uuid[16];
1282 char *buffer = malloc(37);
1284 uuid_generate(uuid);
1285 uuid_unparse(uuid, buffer);
1315 int lpc = 0, len = 0;
1316 char *digest = NULL;
1319 if (buffer == NULL) {
1322 len = strlen(buffer);
1324 crm_trace(
"Beginning digest of %d bytes", len);
1329 sprintf(digest + (2 * lpc),
"%02x", raw_digest[lpc]);
1335 crm_err(
"Could not create digest");
1340 #ifdef HAVE_GNUTLS_GNUTLS_H 1342 crm_gnutls_global_init(
void)
1344 signal(SIGPIPE, SIG_IGN);
1345 gnutls_global_init();
#define CRM_CHECK(expr, failure_action)
void * find_library_function(void **handle, const char *lib, const char *fn, gboolean fatal)
void verify_all_options(GHashTable *options, pe_cluster_option *option_list, int len)
void crm_write_blackbox(int nsig, struct qb_log_callsite *callsite)
long long crm_get_msec(const char *input)
int crm_get_option_long(int argc, char **argv, int *index, const char **longname)
#define crm_notice(fmt, args...)
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
void crm_enable_stderr(int enable)
#define crm_crit(fmt, args...)
gboolean check_utilization(const char *value)
const char * crm_meta_value(GHashTable *hash, const char *field)
void cib_ipc_servers_destroy(qb_ipcs_service_t *ipcs_ro, qb_ipcs_service_t *ipcs_rw, qb_ipcs_service_t *ipcs_shm)
void attrd_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb)
struct crm_time_s crm_time_t
const char * pcmk_strerror(int rc)
qb_ipcs_service_t * mainloop_add_ipc_server_with_prio(const char *name, enum qb_ipc_type type, struct qb_ipcs_service_handlers *callbacks, enum qb_loop_priority prio)
Start server-side API end-point, hooked into the internal event loop.
gboolean check_time(const char *value)
#define crm_config_err(fmt...)
void crm_make_daemon(const char *name, gboolean daemonize, const char *pidfile)
void crm_xml_cleanup(void)
long long crm_int_helper(const char *text, char **end_text)
void cib_ipc_servers_init(qb_ipcs_service_t **ipcs_ro, qb_ipcs_service_t **ipcs_rw, qb_ipcs_service_t **ipcs_shm, struct qb_ipcs_service_handlers *ro_cb, struct qb_ipcs_service_handlers *rw_cb)
char * crm_md5sum(const char *buffer)
bool crm_is_daemon_name(const char *name)
Check whether a string represents a cluster daemon name.
gboolean crm_config_warning
int crm_help(char cmd, int exit_code)
void mainloop_cleanup(void)
int crm_parse_int(const char *text, const char *default_text)
Parse an integer value from a string.
crm_time_t * crm_time_parse_duration(const char *duration_str)
void crm_set_options(const char *short_options, const char *app_usage, struct crm_option *long_options, const char *app_desc)
const char * get_cluster_pref(GHashTable *options, pe_cluster_option *option_list, int len, const char *name)
char * strerror(int errnum)
#define PACEMAKER_VERSION
Wrappers for and extensions to glib mainloop.
gboolean check_positive_number(const char *value)
void config_metadata(const char *name, const char *version, const char *desc_short, const char *desc_long, pe_cluster_option *option_list, int len)
long crm_read_pidfile(const char *filename)
void stonith_ipc_server_init(qb_ipcs_service_t **ipcs, struct qb_ipcs_service_handlers *cb)
char * crm_meta_name(const char *field)
void * md5_buffer(const char *buffer, size_t len, void *resblock)
gboolean check_quorum(const char *value)
#define crm_warn(fmt, args...)
int crm_pid_active(long pid, const char *daemon)
int daemon(int nochdir, int noclose)
char * generate_hash_key(const char *crm_msg_reference, const char *sys)
char * score2char(int score)
#define pcmk_option_example
unsigned long long crm_get_interval(const char *input)
#define crm_trace(fmt, args...)
const char * cluster_option(GHashTable *options, gboolean(*validate)(const char *), const char *name, const char *old_name, const char *def_value)
#define pcmk_option_paragraph
Wrappers for and extensions to libxml2.
char * crm_generate_uuid(void)
char * crm_itoa_stack(int an_int, char *buf, size_t len)
CRM_TRACE_INIT_DATA(common)
long long int crm_time_get_seconds(crm_time_t *dt)
#define DAEMON_RESPAWN_STOP
int compare_version(const char *version1, const char *version2)
gboolean check_script(const char *value)
#define crm_config_warn(fmt...)
#define CRM_SYSTEM_STONITHD
int crm_str_to_boolean(const char *s, int *ret)
void crm_abort(const char *file, const char *function, int line, const char *assert_condition, gboolean do_core, gboolean do_fork)
#define CRM_SYSTEM_TENGINE
gboolean check_timer(const char *value)
long crm_pidfile_inuse(const char *filename, long mypid, const char *daemon)
#define crm_perror(level, fmt, args...)
Log a system error message.
int char2score(const char *score)
#define crm_err(fmt, args...)
gboolean check_number(const char *value)
#define pcmk_option_hidden
int crm_get_option(int argc, char **argv, int *index)
Wrappers for and extensions to libqb IPC.
#define PACKAGE_BUGREPORT
gboolean crm_config_error
char * crm_concat(const char *prefix, const char *suffix, char join)
char * crm_itoa(int an_int)
#define safe_str_eq(a, b)
qb_ipcs_service_t * mainloop_add_ipc_server(const char *name, enum qb_ipc_type type, struct qb_ipcs_service_handlers *callbacks)
gboolean check_boolean(const char *value)
#define crm_info(fmt, args...)
char * score2char_stack(int score, char *buf, size_t len)
qb_ipcs_service_t * crmd_ipc_server_init(struct qb_ipcs_service_handlers *cb)
enum crm_ais_msg_types type
void crm_time_free(crm_time_t *dt)