/* * External API */ void instance_state_get(char *instance_id, void (*completion_func)(char *status, char *ip_addr, void *data), void *data) { struct deltacloud_api api; struct deltacloud_instance instance; int rc; rc = deltacloud_initialize(&api, "http://localhost:3001/api", "dep-wp", ""); if (rc < 0) { qb_log(LOG_ERR, "Failed to initialize libdeltacloud: %s", deltacloud_get_last_error_string()); qb_leave(); return; } rc = deltacloud_get_instance_by_id(&api, instance_id, &instance); if (strcmp(instance.state, "RUNNING") == 0 && instance.private_addresses->address) { completion_func("ACTIVE", instance.private_addresses->address, data); } else { completion_func("PENDING", NULL, data); } }
static int32_t _cs_ip_to_hostname(char* ip, char* name_out) { struct sockaddr_in sa; int rc; if (strchr(ip, ':') == NULL) { sa.sin_family = AF_INET; } else { sa.sin_family = AF_INET6; } rc = inet_pton(sa.sin_family, ip, &sa.sin_addr); if (rc == 0) { return -EINVAL; } rc = getnameinfo((struct sockaddr*)&sa, sizeof(sa), name_out, CS_MAX_NAME_LENGTH, NULL, 0, 0); if (rc != 0) { qb_log(LOG_ERR, 0, "error looking up %s : %s", ip, gai_strerror(rc)); return -EINVAL; } return 0; }
/* * First test will just register SAM, with policy restart. First instance will * sleep one second, send hc and sleep another 3 seconds. This should force restart. * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds. * Then start hc again and sleep 3 seconds. This should force restart again. * Last instance just calls initialize again. This should end with error. * Then call start, followed by stop and start again. Finally, we will call finalize * twice. One should succeed, second should fail. After this, we will call every function * (none should succeed). */ static int test1 (void) { cs_error_t error; unsigned int instance_id; int i; qb_enter(); error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART); if (error != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", error); return 1; } qb_log (LOG_INFO, "register"); error = sam_register (&instance_id); if (error != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", error); return 1; } if (instance_id == 1 || instance_id == 2) { qb_log (LOG_INFO, "iid %d: start", instance_id); error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } for (i = 0; i < 10; i++) { qb_log (LOG_INFO, "iid %d: sleep 1", instance_id); sleep (1); qb_log (LOG_INFO, "iid %d: hc send", instance_id); error = sam_hc_send (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", error); return 1; } } if (instance_id == 2) { qb_log (LOG_INFO, "iid %d: stop", instance_id); error = sam_stop (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", error); return 1; } } qb_log (LOG_INFO, "iid %d: sleep 3", instance_id); sleep (3); qb_log (LOG_INFO, "iid %d: start", instance_id); error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } qb_log (LOG_INFO, "iid %d: sleep 3", instance_id); sleep (3); return 0; } if (instance_id == 3) { error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART); if (error == CS_OK) { qb_log (LOG_ERR, "Can initialize SAM API after initialization"); return 1; } error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } error = sam_stop (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", error); return 1; } error = sam_finalize (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't finalize sam. Error %d", error); return 1; } error = sam_finalize (); if (error == CS_OK) { qb_log (LOG_ERR, "Can finalize sam after finalization!"); return 1; } if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK || sam_start () == CS_OK || sam_stop () == CS_OK || sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK || sam_hc_callback_register (NULL) == CS_OK) { qb_log (LOG_ERR, "Can call one of function after finalization!"); return 1; } return 0; } return 1; }
/* * Test warn signal set. */ static int test6 (void) { cs_error_t error; unsigned int instance_id; int test6_sig_del; qb_log (LOG_INFO, " initialize"); error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART); if (error != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", error); return 1; } qb_log (LOG_INFO, " register"); error = sam_register (&instance_id); if (error != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", error); return 1; } if (instance_id == 1) { error = sam_warn_signal_set (SIGUSR1); if (error != CS_OK) { qb_log (LOG_ERR, "Can't set warn signal. Error %d", error); return 1; } signal (SIGUSR1, test6_signal); qb_log (LOG_INFO, " iid %d: start", instance_id); error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } qb_log (LOG_INFO, "iid %d: sleep 1", instance_id); sleep (1); qb_log (LOG_INFO, "iid %d: hc send", instance_id); error = sam_hc_send (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", error); return 1; } qb_log (LOG_INFO, "iid %d: wait for delivery of signal", instance_id); while (!test6_sig_delivered) { sleep (1); } qb_log (LOG_INFO, "iid %d: wait for real kill", instance_id); sleep (3); qb_log (LOG_INFO, "iid %d: wasn't killed", instance_id); return (1); } if (instance_id == 2) { error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del)); if (error != CS_OK) { qb_log (LOG_ERR, "Can't restore data. Error %d", error); return 1; } if (test6_sig_del != 1) { qb_log (LOG_ERR, "Previous test failed. Signal was not delivered"); return 1; } error = sam_warn_signal_set (SIGKILL); if (error != CS_OK) { qb_log (LOG_ERR, "Can't set warn signal. Error %d", error); return 1; } signal (SIGUSR1, test6_signal); qb_log (LOG_INFO, "iid %d: start", instance_id); error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } qb_log (LOG_INFO, "iid %d: sleep 1", instance_id); sleep (1); qb_log (LOG_INFO, "iid %d: hc send", instance_id); error = sam_hc_send (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", error); return 1; } qb_log (LOG_INFO, "iid %d: wait for delivery of signal", instance_id); while (!test6_sig_delivered) { sleep (1); } qb_log (LOG_INFO, "iid %d: wasn't killed", instance_id); return (1); } if (instance_id == 3) { error = sam_data_restore (&test6_sig_del, sizeof (test6_sig_del)); if (error != CS_OK) { qb_log (LOG_ERR, "Can't restore data. Error %d", error); return 1; } if (test6_sig_del != 1) { qb_log (LOG_ERR, "Previous test failed. Signal WAS delivered"); return 1; } return (0); } return 1; }
/* * Test sam_data_store, sam_data_restore and sam_data_getsize */ static int test4 (void) { size_t size; cs_error_t err; int i; unsigned int instance_id; char saved_data[128]; char saved_data2[128]; qb_log (LOG_INFO, "sam_data_getsize 1"); err = sam_data_getsize (&size); if (err != CS_ERR_BAD_HANDLE) { qb_log (LOG_ERR, "Test should return CS_ERR_BAD_HANDLE. Error returned %d", err); return 1; } qb_log (LOG_INFO, "sam_data_getsize 2"); err = sam_data_getsize (NULL); if (err != CS_ERR_INVALID_PARAM) { qb_log (LOG_ERR, "Test should return CS_ERR_INVALID_PARAM. Error returned %d", err); return 1; } qb_log (LOG_INFO, "sam_data_store 1"); err = sam_data_store (NULL, 0); if (err != CS_ERR_BAD_HANDLE) { qb_log (LOG_ERR, "Test should return CS_ERR_BAD_HANDLE. Error returned %d", err); return 1; } qb_log (LOG_INFO, "sam_data_restore 1"); err = sam_data_restore (saved_data, sizeof (saved_data)); if (err != CS_ERR_BAD_HANDLE) { qb_log (LOG_ERR, "Test should return CS_ERR_BAD_HANDLE. Error returned %d", err); return 1; } qb_log (LOG_INFO, "sam_initialize"); err = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART); if (err != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err); return 1; } qb_log (LOG_INFO, "sam_data_getsize 3"); err = sam_data_getsize (&size); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_ERR_BAD_HANDLE. Error returned %d", err); return 1; } if (size != 0) { qb_log (LOG_ERR, "Test should return size of 0. Returned %zx", size); return 1; } qb_log (LOG_INFO, "sam_data_restore 2"); err = sam_data_restore (NULL, sizeof (saved_data)); if (err != CS_ERR_INVALID_PARAM) { qb_log (LOG_ERR, "Test should return CS_ERR_INVALID_PARAM. Error returned %d", err); return 1; } /* * Store some real data */ for (i = 0; i < sizeof (saved_data); i++) { saved_data[i] = (char)(i + 5); } qb_log (LOG_INFO, "sam_data_store 2"); err = sam_data_store (saved_data, sizeof (saved_data)); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } qb_log (LOG_INFO, " sam_data_getsize 4"); err = sam_data_getsize (&size); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } if (size != sizeof (saved_data)) { qb_log (LOG_ERR, "Test should return size of 0. Returned %zx", size); return 1; } qb_log (LOG_INFO, " sam_data_restore 3"); err = sam_data_restore (saved_data2, sizeof (saved_data2) - 1); if (err != CS_ERR_INVALID_PARAM) { qb_log (LOG_ERR, "Test should return CS_ERR_INVALID_PARAM. Error returned %d", err); return 1; } qb_log (LOG_INFO, " sam_data_restore 4"); err = sam_data_restore (saved_data2, sizeof (saved_data2)); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) { qb_log (LOG_ERR, "Retored data are not same"); return 1; } memset (saved_data2, 0, sizeof (saved_data2)); qb_log (LOG_INFO, " sam_data_store 3"); err = sam_data_store (NULL, 1); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } qb_log (LOG_INFO, " sam_data_getsize 5"); err = sam_data_getsize (&size); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } if (size != 0) { qb_log (LOG_ERR, "Test should return size of 0. Returned %zx", size); return 1; } qb_log (LOG_INFO, " sam_data_store 4"); err = sam_data_store (saved_data, sizeof (saved_data)); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } qb_log (LOG_INFO, " register"); err = sam_register (&instance_id); if (err != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", err); return 1; } if (instance_id == 1) { qb_log (LOG_INFO, "iid %d: sam_start", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 1; } qb_log (LOG_INFO, "iid %d: sam_data_getsize 6", instance_id); err = sam_data_getsize (&size); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } if (size != sizeof (saved_data2)) { qb_log (LOG_ERR, "Test should return size of 0. Returned %zx", size); return 1; } qb_log (LOG_INFO, "iid %d: sam_data_restore 5", instance_id); err = sam_data_restore (saved_data2, sizeof (saved_data2)); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } if (memcmp (saved_data, saved_data2, sizeof (saved_data2)) != 0) { qb_log (LOG_ERR, "Retored data are not same"); return 1; } for (i = 0; i < sizeof (saved_data); i++) { saved_data[i] = (char)(i - 5); } qb_log (LOG_INFO, "iid %d: sam_data_store 5", instance_id); err = sam_data_store (saved_data, sizeof (saved_data) - 7); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } exit (1); } if (instance_id == 2) { qb_log (LOG_INFO, "iid %d: sam_start", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 1; } qb_log (LOG_INFO, "iid %d: sam_data_getsize 7", instance_id); err = sam_data_getsize (&size); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } if (size != sizeof (saved_data2) - 7) { qb_log (LOG_ERR, "Test should return size of 0. Returned %zx", size); return 1; } qb_log (LOG_INFO, "iid %d: sam_data_restore 6", instance_id); err = sam_data_restore (saved_data2, sizeof (saved_data2)); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } for (i = 0; i < sizeof (saved_data); i++) { saved_data[i] = (char)(i - 5); } if (memcmp (saved_data, saved_data2, sizeof (saved_data2) - 7) != 0) { qb_log (LOG_ERR, "Retored data are not same"); return 1; } qb_log (LOG_INFO, "iid %d: sam_data_store 6", instance_id); err = sam_data_store (NULL, 0); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } exit (1); } if (instance_id == 3) { qb_log (LOG_INFO, "iid %d: sam_data_getsize 8", instance_id); err = sam_data_getsize (&size); if (err != CS_OK) { qb_log (LOG_ERR, "Test should return CS_OK. Error returned %d", err); return 1; } if (size != 0) { qb_log (LOG_ERR, "Test should return size of 0. Returned %zx", size); return 1; } } return (0); }
int32_t main(int32_t argc, char *argv[]) { const char *options = "vhteobdf:"; int32_t opt; int32_t tracer; int32_t do_stderr = QB_FALSE; int32_t do_stdout = QB_FALSE; int32_t do_dump_blackbox = QB_FALSE; char *logfile = NULL; int32_t log_fd = -1; while ((opt = getopt(argc, argv, options)) != -1) { switch (opt) { case 'd': do_dump_blackbox = QB_TRUE; break; case 't': do_threaded = QB_TRUE; break; case 'e': do_stderr = QB_TRUE; break; case 'o': do_stdout = QB_TRUE; break; case 'b': do_blackbox = QB_TRUE; break; case 'f': logfile = optarg; break; case 'v': _log_priority++; break; case 'h': default: show_usage(argv[0]); exit(0); break; } } if (do_dump_blackbox) { qb_log_blackbox_print_from_file("simple-log.fdata"); exit(0); } signal(SIGSEGV, sigsegv_handler); qb_log_init("simple-log", LOG_USER, LOG_INFO); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_THREADED, do_threaded); qb_log_tags_stringify_fn_set(my_tags_stringify); if (do_stderr) { qb_log_filter_fn_set(m_filter); qb_log_format_set(QB_LOG_STDERR, "[%p] %4g: %f:%l %b"); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); tracer = qb_log_custom_open(trace_logger, NULL, NULL, NULL); qb_log_ctl(tracer, QB_LOG_CONF_ENABLED, QB_TRUE); qb_log_format_set(tracer, "%4g: %n() %b"); qb_log_filter_ctl2(tracer, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, __FILE__, LOG_TRACE, 200); } if (do_stdout) { qb_log_filter_ctl2(QB_LOG_STDOUT, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, __FILE__, LOG_ALERT, QB_MIN(LOG_DEBUG, _log_priority)); qb_log_format_set(QB_LOG_STDOUT, "[%p] %4g: %f:%l %b"); qb_log_ctl(QB_LOG_STDOUT, QB_LOG_CONF_ENABLED, QB_TRUE); } if (do_blackbox) { qb_log_filter_ctl(QB_LOG_BLACKBOX, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_DEBUG); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 4096); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_THREADED, QB_FALSE); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE); } if (logfile) { log_fd = qb_log_file_open(logfile); qb_log_filter_ctl(log_fd, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, __FILE__, _log_priority); qb_log_format_set(log_fd, "[%N] %t %n() [%p] %b"); qb_log_ctl(log_fd, QB_LOG_CONF_THREADED, do_threaded); qb_log_ctl(log_fd, QB_LOG_CONF_ENABLED, QB_TRUE); } if (do_threaded) { qb_log_thread_start(); } qb_log(LOG_DEBUG, "hello"); qb_log(LOG_INFO, "this is an info"); qb_log(LOG_NOTICE, "hello - notice?"); func_one(); func_two(); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); qb_log(LOG_WARNING, "no syslog"); qb_log(LOG_ERR, "no syslog"); #if 0 // test blackbox logfile = NULL; logfile[5] = 'a'; #endif if (do_blackbox) { qb_log_blackbox_write_to_file("simple-log.fdata"); qb_log_blackbox_print_from_file("simple-log.fdata"); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE); } qb_log_fini(); return 0; }
int32_t main(int32_t argc, char *argv[]) { const char *options = "mpseugh"; int32_t opt; enum qb_ipc_type ipc_type = QB_IPC_NATIVE; struct qb_ipcs_service_handlers sh = { .connection_accept = s1_connection_accept_fn, .connection_created = s1_connection_created_fn, .msg_process = s1_msg_process_fn, .connection_destroyed = s1_connection_destroyed_fn, .connection_closed = s1_connection_closed_fn, }; struct qb_ipcs_poll_handlers ph = { .job_add = my_job_add, .dispatch_add = my_dispatch_add, .dispatch_mod = my_dispatch_mod, .dispatch_del = my_dispatch_del, }; #ifdef HAVE_GLIB struct qb_ipcs_poll_handlers glib_ph = { .job_add = NULL, /* FIXME */ .dispatch_add = my_g_dispatch_add, .dispatch_mod = my_g_dispatch_mod, .dispatch_del = my_g_dispatch_del, }; #endif /* HAVE_GLIB */ while ((opt = getopt(argc, argv, options)) != -1) { switch (opt) { case 'm': ipc_type = QB_IPC_SHM; break; case 'u': ipc_type = QB_IPC_SOCKET; break; case 'g': use_glib = QB_TRUE; break; case 'e': use_events = QB_TRUE; break; case 'h': default: show_usage(argv[0]); exit(0); break; } } signal(SIGINT, sigusr1_handler); qb_log_init("ipcserver", LOG_USER, LOG_TRACE); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_format_set(QB_LOG_STDERR, "%f:%l [%p] %b"); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); s1 = qb_ipcs_create("ipcserver", 0, ipc_type, &sh); if (s1 == 0) { qb_perror(LOG_ERR, "qb_ipcs_create"); exit(1); } if (!use_glib) { bms_loop = qb_loop_create(); qb_ipcs_poll_handlers_set(s1, &ph); qb_ipcs_run(s1); qb_loop_run(bms_loop); } else { #ifdef HAVE_GLIB glib_loop = g_main_loop_new(NULL, FALSE); gio_map = qb_array_create_2(16, sizeof(struct gio_to_qb_poll), 1); qb_ipcs_poll_handlers_set(s1, &glib_ph); qb_ipcs_run(s1); g_main_loop_run(glib_loop); #else qb_log(LOG_ERR, "You don't seem to have glib-devel installed.\n"); #endif } return EXIT_SUCCESS; }
static void send_some_more_messages_normal (void) { msg_t my_msg; struct iovec iov[2]; int i; int send_now; size_t payload_size; cs_error_t res; cpg_flow_control_state_t fc_state; int retries = 0; time_t before; unsigned int sha1_len; if (cpg_fd < 0) return; send_now = my_msgs_to_send; qb_log (LOG_TRACE, "send_now:%d", send_now); my_msg.pid = my_pid; my_msg.nodeid = my_nodeid; payload_size = (rand() % 10000); my_msg.size = sizeof (msg_t) + payload_size; my_msg.seq = my_msgs_sent; for (i = 0; i < payload_size; i++) { buffer[i] = i; } PK11_DigestBegin(sha1_context); PK11_DigestOp(sha1_context, buffer, payload_size); PK11_DigestFinal(sha1_context, my_msg.sha1, &sha1_len, sizeof(my_msg.sha1)); iov[0].iov_len = sizeof (msg_t); iov[0].iov_base = &my_msg; iov[1].iov_len = payload_size; iov[1].iov_base = buffer; for (i = 0; i < send_now; i++) { if (in_cnchg && pcmk_test) { retries = 0; before = time(NULL); cs_repeat(retries, 30, res = cpg_mcast_joined(cpg_handle, CPG_TYPE_AGREED, iov, 2)); if (retries > 20) { qb_log (LOG_ERR, "cs_repeat: blocked for :%lu secs.", (unsigned long)(time(NULL) - before)); } if (res != CS_OK) { qb_log (LOG_ERR, "cpg_mcast_joined error:%d.", res); return; } } else { res = cpg_flow_control_state_get (cpg_handle, &fc_state); if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) { /* lets do this later */ send_some_more_messages_later (); qb_log (LOG_INFO, "flow control enabled."); return; } res = cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, 2); if (res == CS_ERR_TRY_AGAIN) { /* lets do this later */ send_some_more_messages_later (); if (i > 0) { qb_log (LOG_INFO, "TRY_AGAIN %d to send.", my_msgs_to_send); } return; } else if (res != CS_OK) { qb_log (LOG_ERR, "cpg_mcast_joined error:%d, exiting.", res); exit (-2); } } my_msgs_sent++; my_msg.seq = my_msgs_sent; my_msgs_to_send--; } qb_log (LOG_TRACE, "sent %d; to send %d.", my_msgs_sent, my_msgs_to_send); }
static void _cs_cmap_init(void) { cs_error_t rc = CS_OK; int cmap_fd = 0; int stats_fd = 0; cmap_track_handle_t track_handle; tracker_map = qb_trie_create(); if (!tracker_map) { qb_log(LOG_ERR, "Failed to initialize the track map. Error %d", rc); exit (EXIT_FAILURE); } rc = cmap_initialize_map (&cmap_handle, CMAP_MAP_ICMAP); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to initialize the cmap API. Error %d", rc); exit (EXIT_FAILURE); } cmap_fd_get(cmap_handle, &cmap_fd); qb_loop_poll_add(main_loop, QB_LOOP_MED, cmap_fd, POLLIN|POLLNVAL, (void*)&cmap_handle, _cs_cmap_dispatch); rc = cmap_initialize_map (&stats_handle, CMAP_MAP_STATS); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to initialize the cmap stats API. Error %d", rc); exit (EXIT_FAILURE); } cmap_fd_get(stats_handle, &stats_fd); qb_loop_poll_add(main_loop, QB_LOOP_MED, stats_fd, POLLIN|POLLNVAL, (void*)&stats_handle, _cs_cmap_dispatch); rc = cmap_track_add(cmap_handle, "runtime.members.", CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX, _cs_cmap_members_key_changed, NULL, &track_handle); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to track the members key. Error %d", rc); exit (EXIT_FAILURE); } rc = cmap_track_add(stats_handle, "stats.ipcs.", CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX, _cs_cmap_connections_key_changed, NULL, &track_handle); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to track the connections key. Error %d", rc); exit (EXIT_FAILURE); } rc = cmap_track_add(stats_handle, "stats.knet.", CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX, _cs_cmap_link_added_removed, NULL, &track_handle); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to track the knet link status key. Error %d", rc); exit (EXIT_FAILURE); } track_link_updown_events(); }
int32_t main(int32_t argc, char *argv[]) { const char *options = "nevhmpsug"; int32_t opt; int32_t rc; enum qb_ipc_type ipc_type = QB_IPC_SHM; struct qb_ipcs_service_handlers sh = { .connection_accept = s1_connection_accept_fn, .connection_created = s1_connection_created_fn, .msg_process = s1_msg_process_fn, .connection_destroyed = s1_connection_destroyed_fn, .connection_closed = s1_connection_closed_fn, }; struct qb_ipcs_poll_handlers ph = { .job_add = my_job_add, .dispatch_add = my_dispatch_add, .dispatch_mod = my_dispatch_mod, .dispatch_del = my_dispatch_del, }; #ifdef HAVE_GLIB struct qb_ipcs_poll_handlers glib_ph = { .job_add = NULL, /* FIXME */ .dispatch_add = my_g_dispatch_add, .dispatch_mod = my_g_dispatch_mod, .dispatch_del = my_g_dispatch_del, }; #endif /* HAVE_GLIB */ while ((opt = getopt(argc, argv, options)) != -1) { switch (opt) { case 'm': ipc_type = QB_IPC_SHM; break; case 'u': ipc_type = QB_IPC_SOCKET; break; case 'n': /* non-blocking */ blocking = QB_FALSE; break; case 'e': /* events */ events = QB_TRUE; break; case 'g': use_glib = QB_TRUE; break; case 'v': verbose++; break; case 'h': default: show_usage(argv[0]); exit(0); break; } } signal(SIGINT, sigusr1_handler); signal(SIGILL, sigusr1_handler); signal(SIGTERM, sigusr1_handler); qb_log_init("bms", LOG_USER, LOG_EMERG); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_INFO + verbose); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); if (!use_glib) { bms_loop = qb_loop_create(); s1 = qb_ipcs_create("bm1", 0, ipc_type, &sh); if (s1 == 0) { qb_perror(LOG_ERR, "qb_ipcs_create"); exit(1); } qb_ipcs_poll_handlers_set(s1, &ph); rc = qb_ipcs_run(s1); if (rc != 0) { errno = -rc; qb_perror(LOG_ERR, "qb_ipcs_run"); exit(1); } qb_loop_run(bms_loop); } else { #ifdef HAVE_GLIB glib_loop = g_main_loop_new(NULL, FALSE); gio_map = qb_array_create(64, sizeof(struct gio_to_qb_poll)); s1 = qb_ipcs_create("bm1", 0, ipc_type, &sh); if (s1 == 0) { qb_perror(LOG_ERR, "qb_ipcs_create"); exit(1); } qb_ipcs_poll_handlers_set(s1, &glib_ph); rc = qb_ipcs_run(s1); if (rc != 0) { errno = -rc; qb_perror(LOG_ERR, "qb_ipcs_run"); exit(1); } g_main_loop_run(glib_loop); #else qb_log(LOG_ERR, "You don't seem to have glib-devel installed.\n"); #endif } return EXIT_SUCCESS; }
static void show_usage(const char *name) { qb_log(LOG_INFO, "usage: \n"); qb_log(LOG_INFO, "%s <options>\n", name); qb_log(LOG_INFO, "\n"); qb_log(LOG_INFO, " options:\n"); qb_log(LOG_INFO, "\n"); qb_log(LOG_INFO, " -n non-blocking ipc (default blocking)\n"); qb_log(LOG_INFO, " -e send events back instead for responses\n"); qb_log(LOG_INFO, " -v verbose\n"); qb_log(LOG_INFO, " -h show this help text\n"); qb_log(LOG_INFO, " -m use shared memory\n"); qb_log(LOG_INFO, " -p use posix message queues\n"); qb_log(LOG_INFO, " -s use sysv message queues\n"); qb_log(LOG_INFO, " -u use unix sockets\n"); qb_log(LOG_INFO, " -g use glib mainloop\n"); qb_log(LOG_INFO, "\n"); }
static void sigusr1_handler(int32_t num) { qb_log(LOG_INFO, "%s(%d)\n", __func__, num); qb_ipcs_destroy(s1); exit(0); }
static int32_t s1_msg_process_fn(qb_ipcs_connection_t *c, void *data, size_t size) { struct qb_ipc_request_header *req_pt = (struct qb_ipc_request_header *)data; struct qb_ipc_response_header response = { 0, }; ssize_t res; if (req_pt->id == IPC_MSG_REQ_TX_RX) { response.size = sizeof(struct qb_ipc_response_header); response.id = IPC_MSG_RES_TX_RX; response.error = 0; res = qb_ipcs_response_send(c, &response, response.size); if (res < 0) { qb_perror(LOG_INFO, "qb_ipcs_response_send"); } else if (res != response.size) { qb_log(LOG_DEBUG, "qb_ipcs_response_send %zd != %d", res, response.size); } if (turn_on_fc) { qb_ipcs_request_rate_limit(s1, QB_IPCS_RATE_OFF); } } else if (req_pt->id == IPC_MSG_REQ_DISPATCH) { response.size = sizeof(struct qb_ipc_response_header); response.id = IPC_MSG_RES_DISPATCH; response.error = 0; res = qb_ipcs_event_send(c, &response, sizeof(response)); if (res < 0) { qb_perror(LOG_INFO, "qb_ipcs_event_send"); } } else if (req_pt->id == IPC_MSG_REQ_BULK_EVENTS) { int32_t m; int32_t num; struct qb_ipcs_connection_stats_2 *stats; uint32_t max_size = MAX_MSG_SIZE; response.size = sizeof(struct qb_ipc_response_header); response.error = 0; stats = qb_ipcs_connection_stats_get_2(c, QB_FALSE); num = stats->event_q_length; free(stats); /* crazy large message */ res = qb_ipcs_event_send(c, &response, max_size*10); ck_assert_int_eq(res, -EMSGSIZE); /* send one event before responding */ res = qb_ipcs_event_send(c, &response, sizeof(response)); ck_assert_int_eq(res, sizeof(response)); response.id++; /* There should be one more item in the event queue now. */ stats = qb_ipcs_connection_stats_get_2(c, QB_FALSE); ck_assert_int_eq(stats->event_q_length - num, 1); free(stats); /* send response */ response.id = IPC_MSG_RES_BULK_EVENTS; res = qb_ipcs_response_send(c, &response, response.size); ck_assert_int_eq(res, sizeof(response)); /* send the rest of the events after the response */ for (m = 1; m < num_bulk_events; m++) { res = qb_ipcs_event_send(c, &response, sizeof(response)); if (res == -EAGAIN || res == -ENOBUFS) { /* retry */ usleep(1000); m--; continue; } ck_assert_int_eq(res, sizeof(response)); response.id++; } } else if (req_pt->id == IPC_MSG_REQ_STRESS_EVENT) { struct { struct qb_ipc_response_header hdr __attribute__ ((aligned(8))); char data[GIANT_MSG_DATA_SIZE] __attribute__ ((aligned(8))); uint32_t sent_msgs __attribute__ ((aligned(8))); } __attribute__ ((aligned(8))) giant_event_send; int32_t m; response.size = sizeof(struct qb_ipc_response_header); response.error = 0; response.id = IPC_MSG_RES_STRESS_EVENT; res = qb_ipcs_response_send(c, &response, response.size); ck_assert_int_eq(res, sizeof(response)); giant_event_send.hdr.error = 0; giant_event_send.hdr.id = IPC_MSG_RES_STRESS_EVENT; for (m = 0; m < num_stress_events; m++) { size_t sent_len = sizeof(struct qb_ipc_response_header); if (((m+1) % 1000) == 0) { sent_len = sizeof(giant_event_send); giant_event_send.sent_msgs = m + 1; } giant_event_send.hdr.size = sent_len; res = qb_ipcs_event_send(c, &giant_event_send, sent_len); if (res < 0) { if (res == -EAGAIN || res == -ENOBUFS) { /* yield to the receive process */ usleep(1000); m--; continue; } else { qb_perror(LOG_DEBUG, "sending stress events"); ck_assert_int_eq(res, sent_len); } } else if (((m+1) % 1000) == 0) { qb_log(LOG_DEBUG, "SENT: %d stress events sent", m+1); } giant_event_send.hdr.id++; } } else if (req_pt->id == IPC_MSG_REQ_SERVER_FAIL) {
static void sigterm_handler(int32_t num) { qb_log(LOG_INFO, "writer: %s(%d)\n", __func__, num); qb_rb_close(rb); exit(0); }
static void record_messages (void) { record_messages_g = 1; qb_log (LOG_INFO, "record:%d", record_messages_g); }
static void send_some_more_messages_zcb (void) { msg_t *my_msg; int i; int send_now; size_t payload_size; size_t total_size; unsigned int sha1_len; cs_error_t res; cpg_flow_control_state_t fc_state; void *zcb_buffer; if (cpg_fd < 0) return; send_now = my_msgs_to_send; payload_size = (rand() % 100000); total_size = payload_size + sizeof (msg_t); cpg_zcb_alloc (cpg_handle, total_size, &zcb_buffer); my_msg = (msg_t*)zcb_buffer; qb_log(LOG_DEBUG, "send_now:%d", send_now); my_msg->pid = my_pid; my_msg->nodeid = my_nodeid; my_msg->size = sizeof (msg_t) + payload_size; my_msg->seq = my_msgs_sent; for (i = 0; i < payload_size; i++) { my_msg->payload[i] = i; } PK11_DigestBegin(sha1_context); PK11_DigestOp(sha1_context, my_msg->payload, payload_size); PK11_DigestFinal(sha1_context, my_msg->sha1, &sha1_len, sizeof(my_msg->sha1)); for (i = 0; i < send_now; i++) { res = cpg_flow_control_state_get (cpg_handle, &fc_state); if (res == CS_OK && fc_state == CPG_FLOW_CONTROL_ENABLED) { /* lets do this later */ send_some_more_messages_later (); qb_log (LOG_INFO, "flow control enabled."); goto free_buffer; } res = cpg_zcb_mcast_joined (cpg_handle, CPG_TYPE_AGREED, zcb_buffer, total_size); if (res == CS_ERR_TRY_AGAIN) { /* lets do this later */ send_some_more_messages_later (); goto free_buffer; } else if (res != CS_OK) { qb_log (LOG_ERR, "cpg_mcast_joined error:%d, exiting.", res); exit (-2); } my_msgs_sent++; my_msgs_to_send--; } free_buffer: cpg_zcb_free (cpg_handle, zcb_buffer); }
static void _cs_syslog_link_faulty_event(char *nodename, uint32_t our_nodeid, uint32_t nodeid, uint32_t iface_no, const char *state) { qb_log(LOG_NOTICE, "%s[%d] link %u to node %u is now %s", nodename, our_nodeid, iface_no, nodeid, state); }
static void do_command (int sock, char* func, char*args[], int num_args) { int result; char response[100]; struct cpg_name group_name; ssize_t rc; size_t send_len; qb_log (LOG_TRACE, "RPC:%s() called.", func); if (strcmp ("cpg_mcast_joined",func) == 0) { struct iovec iov[5]; int a; for (a = 0; a < num_args; a++) { iov[a].iov_base = args[a]; iov[a].iov_len = strlen(args[a])+1; } cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args); } else if (strcmp ("cpg_join",func) == 0) { if (strlen(args[0]) >= CPG_MAX_NAME_LENGTH) { qb_log (LOG_ERR, "Invalid group name"); exit (1); } strcpy (group_name.value, args[0]); group_name.length = strlen(args[0]); result = cpg_join (cpg_handle, &group_name); if (result != CS_OK) { qb_log (LOG_ERR, "Could not join process group, error %d", result); exit (1); } qb_log (LOG_INFO, "called cpg_join(%s)!", group_name.value); } else if (strcmp ("cpg_leave",func) == 0) { strcpy (group_name.value, args[0]); group_name.length = strlen(args[0]); result = cpg_leave (cpg_handle, &group_name); if (result != CS_OK) { qb_log (LOG_ERR, "Could not leave process group, error %d", result); exit (1); } qb_log (LOG_INFO, "called cpg_leave(%s)!", group_name.value); } else if (strcmp ("cpg_initialize",func) == 0) { int retry_count = 0; result = cpg_initialize (&cpg_handle, &callbacks); while (result != CS_OK) { qb_log (LOG_ERR, "cpg_initialize error %d (attempt %d)", result, retry_count); if (retry_count >= 3) { exit (1); } sleep(1); retry_count++; result = cpg_initialize (&cpg_handle, &callbacks); } cpg_fd_get (cpg_handle, &cpg_fd); qb_loop_poll_add (ta_poll_handle_get(), QB_LOOP_MED, cpg_fd, POLLIN|POLLNVAL, NULL, cpg_dispatch_wrapper_fn); } else if (strcmp ("cpg_local_get", func) == 0) { unsigned int local_nodeid; cpg_local_get (cpg_handle, &local_nodeid); snprintf (response, 100, "%u",local_nodeid); send_len = strlen (response); rc = send (sock, response, send_len, 0); assert(rc == send_len); } else if (strcmp ("cpg_finalize", func) == 0) { if (cpg_handle > 0) { cpg_finalize (cpg_handle); cpg_handle = 0; } } else if (strcmp ("record_config_events", func) == 0) { record_config_events (sock); } else if (strcmp ("record_messages", func) == 0) { record_messages (); } else if (strcmp ("read_config_event", func) == 0) { read_config_event (sock); } else if (strcmp ("read_messages", func) == 0) { read_messages (sock, args[0]); } else if (strcmp ("msg_blaster_zcb", func) == 0) { msg_blaster_zcb (sock, args[0]); } else if (strcmp ("pcmk_test", func) == 0) { pcmk_test = 1; } else if (strcmp ("msg_blaster", func) == 0) { msg_blaster (sock, args[0]); } else if (strcmp ("context_test", func) == 0) { context_test (sock); } else if (strcmp ("are_you_ok_dude", func) == 0) { snprintf (response, 100, "%s", OK_STR); send_len = strlen (response); rc = send (sock, response, strlen (response), 0); assert(rc == send_len); } else if (strcmp ("cfg_shutdown", func) == 0) { qb_log (LOG_INFO, "calling %s() called!", func); result = corosync_cfg_try_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST); qb_log (LOG_INFO,"%s() returned %d!", func, result); } else if (strcmp ("cfg_initialize",func) == 0) { int retry_count = 0; qb_log (LOG_INFO,"%s() called!", func); result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks); while (result != CS_OK) { qb_log (LOG_ERR, "cfg_initialize error %d (attempt %d)", result, retry_count); if (retry_count >= 3) { exit (1); } sleep(1); retry_count++; result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks); } qb_log (LOG_INFO,"corosync_cfg_initialize() == %d", result); result = corosync_cfg_fd_get (cfg_handle, &cfg_fd); qb_log (LOG_INFO,"corosync_cfg_fd_get() == %d", result); qb_loop_poll_add (ta_poll_handle_get(), QB_LOOP_MED, cfg_fd, POLLIN|POLLNVAL, NULL, cfg_dispatch_wrapper_fn); } else { qb_log(LOG_ERR, "RPC:%s not supported!", func); } }
static void stop_func_tmo(void*data) { qb_loop_t *l = (qb_loop_t *)data; qb_log(LOG_DEBUG, "expire_leak_counter:%d", expire_leak_counter); qb_loop_stop(l); }
static void s1_connection_destroyed_fn(qb_ipcs_connection_t * c) { qb_log(LOG_INFO, "Connection about to be freed"); }
static void _cs_syslog_node_membership_event(char *nodename, uint32_t nodeid, char *state, char* ip) { qb_log(LOG_NOTICE, "%s[%d] ip:%s %s", nodename, nodeid, ip, state); }
static int hc_allways_respond_cb(void) { qb_log (LOG_INFO, "health check OK."); return 0; }
static void _cs_syslog_rrp_faulty_event(char *nodename, uint32_t nodeid, uint32_t iface_no, const char *state) { qb_log(LOG_NOTICE, "%s[%d] interface %u is now %s", nodename, nodeid, iface_no, state); }
/* * Test event driven healtchecking. */ static int test5 (void) { cs_error_t error; unsigned int instance_id; int hc_cb_count; qb_log (LOG_INFO, " initialize"); error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART); if (error != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", error); return 1; } qb_log (LOG_INFO, " register"); error = sam_register (&instance_id); if (error != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", error); return 1; } if (instance_id == 1) { qb_log (LOG_INFO, "iid %d: hc callback register", instance_id); error = sam_hc_callback_register (test5_hc_cb); if (error != CS_OK) { qb_log (LOG_ERR, "Can't register hc cb. Error %d", error); return 1; } qb_log (LOG_INFO, "iid %d: start", instance_id); error = sam_start (); if (error != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", error); return 1; } sleep (2); qb_log (LOG_INFO, "iid %d: Failed. Wasn't killed.", instance_id); return 1; } if (instance_id == 2) { error = sam_data_restore (&hc_cb_count, sizeof (hc_cb_count)); if (error != CS_OK) { qb_log (LOG_ERR, "sam_data_restore should return CS_OK. Error returned %d", error); return 1; } if (hc_cb_count != 11) { qb_log (LOG_ERR, "iid %d: Premature killed. hc_cb_count should be 11 and it is %d", __FUNCTION__, instance_id - 1, hc_cb_count); return 1; } return 0; } return 1; }
static void log_also(void) { qb_log(LOG_INFO, "yes please"); }
/* * Test cmap integration + quit policy */ static int test8 (pid_t pid, pid_t old_pid, int test_n) { cmap_handle_t cmap_handle; cs_error_t err; uint64_t tstamp1, tstamp2; int32_t msec_diff; unsigned int instance_id; char key_name[CMAP_KEYNAME_MAXLEN]; char *str; err = cmap_initialize (&cmap_handle); if (err != CS_OK) { qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err); return (1); } qb_log (LOG_INFO, "test %d", test_n); if (test_n == 2) { /* * Object should not exist */ qb_log (LOG_INFO, "Testing if object exists (it shouldn't)"); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err == CS_OK) { qb_log (LOG_INFO, "Could find key \"%s\": %d.", key_name, err); free(str); return (2); } } if (test_n == 1 || test_n == 2) { qb_log (LOG_INFO, " initialize"); err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CMAP); if (err != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err); return 2; } qb_log (LOG_INFO, " register"); err = sam_register (&instance_id); if (err != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"recovery\" key: %d.", err); return (2); } if (strcmp(str, "quit") != 0) { qb_log (LOG_INFO, "Recovery key \"%s\" is not \"quit\".", key_name); return (2); } free(str); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: stop", instance_id); err = sam_stop (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: sleeping 5", instance_id); sleep (5); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start 2", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); if (test_n == 2) { qb_log (LOG_INFO, "iid %d: sleeping 5. Should be killed", instance_id); sleep (5); return (2); } else { qb_log (LOG_INFO, "iid %d: Test HC", instance_id); err = sam_hc_send (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.last_updated", pid); err = cmap_get_uint64(cmap_handle, key_name, &tstamp1); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err); return (2); } qb_log (LOG_INFO, "iid %d: Sleep 1", instance_id); sleep (1); err = sam_hc_send (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", err); return 2; } sleep (1); err = cmap_get_uint64(cmap_handle, key_name, &tstamp2); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err); return (2); } msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC; if (msec_diff < 500 || msec_diff > 2000) { qb_log (LOG_INFO, "Difference %d is not within <500, 2000> interval.", msec_diff); return (2); } qb_log (LOG_INFO, "iid %d: stop 2", instance_id); err = sam_stop (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: exiting", instance_id); return (0); } } if (test_n == 3) { qb_log (LOG_INFO, "Testing if status is failed"); /* * Previous should be FAILED */ snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "failed") != 0) { qb_log (LOG_INFO, "State key is not \"failed\"."); return (2); } return (0); } return (2); }
static void log_and_this_too(void) { qb_log(LOG_INFO, "this too please"); }
/* * Test cmap integration + restart policy */ static int test9 (pid_t pid, pid_t old_pid, int test_n) { cs_error_t err; cmap_handle_t cmap_handle; unsigned int instance_id; char *str; char key_name[CMAP_KEYNAME_MAXLEN]; err = cmap_initialize (&cmap_handle); if (err != CS_OK) { qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err); return (1); } qb_log (LOG_INFO, "test %d", test_n); if (test_n == 1) { qb_log (LOG_INFO, " initialize"); err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | SAM_RECOVERY_POLICY_CMAP); if (err != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err); return 2; } qb_log (LOG_INFO, " register"); err = sam_register (&instance_id); if (err != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", err); return 2; } qb_log (LOG_INFO, " iid %d", instance_id); if (instance_id < 3) { snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"recovery\" key: %d.", err); return (2); } if (strcmp(str, "restart") != 0) { qb_log (LOG_INFO, "Recovery key \"%s\" is not \"restart\".", str); return (2); } free(str); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: waiting for kill", instance_id); sleep (10); return (2); } if (instance_id == 3) { qb_log (LOG_INFO, "iid %d: mark failed", instance_id); err = sam_mark_failed (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't mark failed. Error %d", err); return 2; } sleep (10); return (2); } return (2); } if (test_n == 2) { qb_log (LOG_INFO, "Testing if status is failed"); /* * Previous should be FAILED */ snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "failed") != 0) { qb_log (LOG_INFO, "State key is not \"failed\"."); return (2); } free(str); return (0); } return (2); }
END_TEST START_TEST(test_log_basic) { int32_t t; int32_t rc; qb_log_init("test", LOG_USER, LOG_EMERG); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); t = qb_log_custom_open(_test_logger, NULL, NULL, NULL); rc = qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FORMAT, "Angus", LOG_WARNING); ck_assert_int_eq(rc, 0); qb_log_format_set(t, "%b"); rc = qb_log_ctl(t, QB_LOG_CONF_ENABLED, QB_TRUE); ck_assert_int_eq(rc, 0); /* captures last log */ memset(test_buf, 0, sizeof(test_buf)); test_priority = 0; num_msgs = 0; /* * test filtering by format */ qb_log(LOG_INFO, "Hello Angus, how are you?"); qb_log(LOG_WARNING, "Hello Steven, how are you?"); qb_log(LOG_ERR, "Hello Andrew, how are you?"); qb_log(LOG_ERR, "Hello Angus, how are you?"); qb_log(LOG_EMERG, "Hello Anna, how are you?"); ck_assert_int_eq(test_priority, LOG_ERR); ck_assert_int_eq(num_msgs, 1); ck_assert_str_eq(test_buf, "Hello Angus, how are you?"); /* * test filtering by file regex */ qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FORMAT, "*", LOG_TRACE); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE_REGEX, "^fakefile*", LOG_DEBUG); num_msgs = 0; qb_log_from_external_source(__func__, "fakefile_logging", "%s bla", LOG_INFO, 56, 0, "filename/lineno"); qb_log_from_external_source(__func__, "do_not_log_fakefile_logging", "%s bla", LOG_INFO, 56, 0, "filename/lineno"); ck_assert_int_eq(num_msgs, 1); /* * test filtering by format regex */ qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FORMAT, "*", LOG_TRACE); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FORMAT_REGEX, "^one", LOG_WARNING); num_msgs = 0; qb_log(LOG_INFO, "one two three"); qb_log(LOG_ERR, "testing one two three"); qb_log(LOG_WARNING, "one two three"); qb_log(LOG_ERR, "one two three"); qb_log(LOG_EMERG, "one two three"); ck_assert_int_eq(num_msgs, 3); /* * test filtering by function and regex */ qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FUNCTION_REGEX, "^log_.*please", LOG_WARNING); num_msgs = 0; qb_log(LOG_ERR, "try if you: log_it_please()"); log_it_please(); ck_assert_int_eq(num_msgs, 3); qb_log_filter_ctl(t, QB_LOG_FILTER_REMOVE, QB_LOG_FILTER_FUNCTION_REGEX, "log_it_please", LOG_WARNING); /* * test filtering by function */ qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FUNCTION, "log_it_please", LOG_WARNING); num_msgs = 0; qb_log(LOG_ERR, "try if you: log_it_please()"); log_it_please(); ck_assert_int_eq(num_msgs, 3); qb_log_filter_ctl(t, QB_LOG_FILTER_REMOVE, QB_LOG_FILTER_FUNCTION, "log_it_please", LOG_WARNING); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FUNCTION, __func__, LOG_DEBUG); num_msgs = 0; log_it_please(); ck_assert_int_eq(num_msgs, 0); qb_log(LOG_DEBUG, "try if you: log_it_please()"); ck_assert_int_eq(num_msgs, 1); qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FUNCTION, "log_also,log_and_this_too", LOG_DEBUG); num_msgs = 0; log_also(); log_and_this_too(); ck_assert_int_eq(num_msgs, 2); qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "fakefile.c,"__FILE__",otherfakefile", LOG_DEBUG); /* * make sure we can pass in a null filename or function name. */ qb_log_from_external_source(__func__, NULL, "%s", LOG_INFO, __LINE__, 0, "null filename"); qb_log_from_external_source(NULL, __FILE__, "%s", LOG_INFO, __LINE__, 0, "null function"); /* check same file/lineno logs with different formats work */ num_msgs = 0; qb_log_from_external_source(__func__, __FILE__, "%s bla", LOG_INFO, 56, 0, "filename/lineno"); ck_assert_int_eq(num_msgs, 1); ck_assert_str_eq(test_buf, "filename/lineno bla"); num_msgs = 0; qb_log_from_external_source(__func__, __FILE__, "%s", LOG_INFO, 56, 0, "same filename/lineno"); ck_assert_int_eq(num_msgs, 1); ck_assert_str_eq(test_buf, "same filename/lineno"); /* check filtering works on same file/lineno but different * log level. */ qb_log_filter_ctl(t, QB_LOG_FILTER_CLEAR_ALL, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_filter_ctl(t, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, __FILE__, LOG_INFO); num_msgs = 0; qb_log_from_external_source(__func__, __FILE__, "same filename/lineno, this level %d", LOG_INFO, 56, 0, LOG_INFO); ck_assert_int_eq(num_msgs, 1); ck_assert_str_eq(test_buf, "same filename/lineno, this level 6"); num_msgs = 0; qb_log_from_external_source(__func__, __FILE__, "same filename/lineno, this level %d", LOG_DEBUG, 56, 0, LOG_DEBUG); ck_assert_int_eq(num_msgs, 0); }