int main(int argc, char * argv[]) { stonith_ops_t * st_op; cl_log_set_entity("STDAPI_TEST"); cl_log_set_facility(LOG_USER); cl_log_enable_stderr(TRUE); if (argc != 5) { cl_log(LOG_ERR, "parameter error."); printf("%s optype target_node timeout expect_value.\n", argv[0]); return -1; } if (ST_OK != stonithd_signon("apitest")) { return -1; } st_op = g_new(stonith_ops_t, 1); st_op->optype = atoi(argv[1]); st_op->node_name = g_strdup(argv[2]); st_op->node_uuid = g_strdup(argv[2]); st_op->timeout = atoi(argv[3]); st_op->private_data = g_strdup(argv[4]); if (ST_OK!=stonithd_set_stonith_ops_callback(stonith_ops_cb)) { stonithd_signoff(); return -1; } if (ST_OK == stonithd_node_fence( st_op )) { while (stonithd_op_result_ready() != TRUE) { ; } if (ST_OK!=stonithd_receive_ops_result(TRUE)) { return -1; } } else { g_rc = -1; } /* stonithRA_ops_t * stra_op; int call_id; stra_op = g_new(stonithRA_ops_t, 1); stra_op->ra_name = g_strdup("/root/test"); stra_op->op_type = g_strdup("start"); stra_op->params = NULL; stonithd_virtual_stonithRA_ops( stra_op, &call_id ); */ if (ST_OK!=stonithd_signoff()) { g_rc = -1; } return g_rc; }
gboolean te_stop(void) { destroy_graph(transition_graph); #if SUPPORT_HEARTBEAT if(is_heartbeat_cluster()) { stonithd_signoff(); } #endif crm_free(te_uuid); }
/* A_TE_START, A_TE_STOP, A_TE_RESTART */ void do_te_control(long long action, enum crmd_fsa_cause cause, enum crmd_fsa_state cur_state, enum crmd_fsa_input current_input, fsa_data_t *msg_data) { int dummy; gboolean init_ok = TRUE; cl_uuid_t new_uuid; char uuid_str[UU_UNPARSE_SIZEOF]; if(action & A_TE_STOP) { if(transition_graph) { destroy_graph(transition_graph); transition_graph = NULL; } if(fsa_cib_conn && cib_ok != fsa_cib_conn->cmds->del_notify_callback( fsa_cib_conn, T_CIB_DIFF_NOTIFY, te_update_diff)) { crm_err("Could not unset CIB notification callback"); } clear_bit_inplace(fsa_input_register, te_subsystem->flag_connected); crm_info("Transitioner is now inactive"); if(stonith_src) { GCHSource *source = stonith_src; crm_info("Disconnecting STONITH..."); stonith_src = NULL; /* so that we don't try to reconnect */ G_main_del_IPC_Channel(source); stonithd_signoff(); } } if((action & A_TE_START) == 0) { return; } else if(is_set(fsa_input_register, te_subsystem->flag_connected)) { crm_debug("The transitioner is already active"); return; } else if((action & A_TE_START) && cur_state == S_STOPPING) { crm_info("Ignoring request to start %s while shutting down", te_subsystem->name); return; } cl_uuid_generate(&new_uuid); cl_uuid_unparse(&new_uuid, uuid_str); te_uuid = crm_strdup(uuid_str); crm_info("Registering TE UUID: %s", te_uuid); if(transition_trigger == NULL) { transition_trigger = mainloop_add_trigger( G_PRIORITY_LOW, te_graph_trigger, NULL); } if(stonith_reconnect == NULL) { stonith_reconnect = mainloop_add_trigger( G_PRIORITY_LOW, te_connect_stonith, &dummy); } if(cib_ok != fsa_cib_conn->cmds->add_notify_callback( fsa_cib_conn, T_CIB_DIFF_NOTIFY, te_update_diff)) { crm_err("Could not set CIB notification callback"); init_ok = FALSE; } if(cib_ok != fsa_cib_conn->cmds->set_op_callback(fsa_cib_conn, global_cib_callback)) { crm_err("Could not set CIB global callback"); init_ok = FALSE; } if(init_ok) { mainloop_set_trigger(stonith_reconnect); set_graph_functions(&te_graph_fns); if(transition_graph) { destroy_graph(transition_graph); } /* create a blank one */ crm_debug("Transitioner is now active"); transition_graph = create_blank_graph(); set_bit_inplace(fsa_input_register, te_subsystem->flag_connected); } }
int stonithd_signon(const char * client_name) { int rc = ST_FAIL; char path[] = IPC_PATH_ATTR; char sock[] = STONITHD_SOCK; char cbsock[] = STONITHD_CALLBACK_SOCK; struct ha_msg * request; struct ha_msg * reply; GHashTable * wchanattrs; uid_t my_euid; gid_t my_egid; const char * tmpstr; int rc_tmp; gboolean connected = TRUE; cl_uuid_t cookie, *cptr = NULL; if (chan == NULL || chan->ch_status != IPC_CONNECT) { connected = FALSE; } else if (cbchan == NULL || cbchan->ch_status != IPC_CONNECT) { connected = FALSE; } if(!connected) { /* cleanup */ if (NULL != chan) { chan->ops->destroy(chan); chan = NULL; } if (NULL != cbchan) { cbchan->ops->destroy(cbchan); cbchan = NULL; } stdlib_log(LOG_DEBUG, "stonithd_signon: creating connection"); wchanattrs = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(wchanattrs, path, sock); /* Connect to the stonith deamon */ chan = ipc_channel_constructor(IPC_ANYTYPE, wchanattrs); g_hash_table_destroy(wchanattrs); if (chan == NULL) { stdlib_log(LOG_ERR, "stonithd_signon: Can't connect " " to stonithd"); rc = ST_FAIL; goto end; } if (chan->ops->initiate_connection(chan) != IPC_OK) { stdlib_log(LOG_ERR, "stonithd_signon: Can't initiate " "connection to stonithd"); rc = ST_FAIL; goto end; } } CLIENT_PID = getpid(); snprintf(CLIENT_PID_STR, sizeof(CLIENT_PID_STR), "%d", CLIENT_PID); if ( client_name != NULL ) { CLIENT_NAME = client_name; } else { CLIENT_NAME = CLIENT_PID_STR; } if ( (request = create_basic_reqmsg_fields(ST_SIGNON)) == NULL) { rc = ST_FAIL; goto end; } /* important error check client name length */ my_euid = geteuid(); my_egid = getegid(); if ( ( ha_msg_add_int(request, F_STONITHD_CEUID, my_euid) != HA_OK ) ||( ha_msg_add_int(request, F_STONITHD_CEGID, my_egid) != HA_OK ) ||( ha_msg_add(request, F_STONITHD_COOKIE, "") != HA_OK ) ) { stdlib_log(LOG_ERR, "stonithd_signon: " "cannot add field to ha_msg."); ZAPMSG(request); rc = ST_FAIL; goto end; } stdlib_log(LOG_DEBUG, "sending out the signon msg."); /* Send the registration request message */ if (msg2ipcchan(request, chan) != HA_OK) { ZAPMSG(request); stdlib_log(LOG_ERR, "can't send signon message to IPC"); rc = ST_FAIL; goto end; } /* waiting for the output to finish */ do { rc_tmp= chan_waitout_timeout(chan, DEFAULT_TIMEOUT); } while (rc_tmp == IPC_INTR); ZAPMSG(request); if (IPC_OK != rc_tmp) { stdlib_log(LOG_ERR, "%s:%d: waitout failed." , __FUNCTION__, __LINE__); rc = ST_FAIL; goto end; } /* Read the reply... */ if ( IPC_OK != chan_waitin_timeout(chan, DEFAULT_TIMEOUT) ) { stdlib_log(LOG_ERR, "%s:%d: waitin failed." , __FUNCTION__, __LINE__); rc = ST_FAIL; goto end; } if ( (reply = msgfromIPC_noauth(chan)) == NULL ) { stdlib_log(LOG_ERR, "stonithd_signon: failed to fetch reply."); rc = ST_FAIL; goto end; } if ( TRUE == is_expected_msg(reply, F_STONITHD_TYPE, ST_APIRPL, F_STONITHD_APIRPL, ST_RSIGNON, TRUE) ) { if ( ((tmpstr=cl_get_string(reply, F_STONITHD_APIRET)) != NULL) && (STRNCMP_CONST(tmpstr, ST_APIOK) == 0) ) { rc = ST_OK; stdlib_log(LOG_DEBUG, "signed on to stonithd."); /* get cookie if any */ if( cl_get_uuid(reply, F_STONITHD_COOKIE, &cookie) == HA_OK ) { cptr = &cookie; } } else { stdlib_log(LOG_WARNING, "failed to signon to the " "stonithd."); } } else { stdlib_log(LOG_ERR, "stonithd_signon: " "Got an unexpected message."); } ZAPMSG(reply); if (ST_OK != rc) { /* Something wrong when try to sign on to stonithd */ goto end; } /* Connect to the stonith deamon via callback channel */ wchanattrs = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(wchanattrs, path, cbsock); cbchan = ipc_channel_constructor(IPC_ANYTYPE, wchanattrs); g_hash_table_destroy(wchanattrs); if (cbchan == NULL) { stdlib_log(LOG_ERR, "stonithd_signon: Can't construct " "callback channel to stonithd."); rc = ST_FAIL; goto end; } if (cbchan->ops->initiate_connection(cbchan) != IPC_OK) { stdlib_log(LOG_ERR, "stonithd_signon: Can't initiate " "connection with the callback channel"); rc = ST_FAIL; goto end; } if ( (reply = msgfromIPC_noauth(cbchan)) == NULL ) { stdlib_log(LOG_ERR, "%s:%d: failed to fetch reply via the " " callback channel" , __FUNCTION__, __LINE__); rc = ST_FAIL; goto end; } if ( TRUE == is_expected_msg(reply, F_STONITHD_TYPE, ST_APIRPL, F_STONITHD_APIRPL, ST_RSIGNON, TRUE) ) { tmpstr=cl_get_string(reply, F_STONITHD_APIRET); if ( !STRNCMP_CONST(tmpstr, ST_APIOK) ) { /* * If the server directly authenticates us (probably * via pid-auth), go ahead. */ stdlib_log(LOG_DEBUG, "%s:%d: Got a good signon reply " "via the callback channel." , __FUNCTION__, __LINE__); } else if ( !STRNCMP_CONST(tmpstr, ST_COOKIE) ) { /* * If the server asks for a cookie to identify myself, * initiate cookie authentication. */ if (cptr == NULL) { stdlib_log(LOG_ERR, "server requested cookie auth on " "the callback channel, but it didn't " "provide the cookie on the main channel."); rc = ST_FAIL; } else { rc = authenticate_with_cookie(cbchan, cptr); } } else { /* Unknown response. */ rc = ST_FAIL; stdlib_log(LOG_ERR, "%s:%d: Got a bad signon reply " "via the callback channel." , __FUNCTION__, __LINE__); } } else { rc = ST_FAIL; stdlib_log(LOG_ERR, "stonithd_signon: " "Got an unexpected message via the callback chan."); } ZAPMSG(reply); end: if (ST_OK != rc) { /* Something wrong when confirm via callback channel */ stonithd_signoff(); } return rc; }