Beispiel #1
0
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;
}
int
stonithd_receive_ops_result(gboolean blocking)
{
	struct ha_msg* reply = NULL;
	const char *reply_type;
	int rc = ST_OK;

	stdlib_log(LOG_DEBUG, "stonithd_receive_ops_result: begin");

	/* If there is no msg ready and none blocking mode, then return */
	if ((stonithd_op_result_ready() == FALSE) && (blocking == FALSE)) {
		stdlib_log(LOG_DEBUG, "stonithd_receive_ops_result: "
			   "no result available.");
		return ST_OK;
	}

	if (stonithd_op_result_ready() == FALSE) {
	/* at that time, blocking must be TRUE */
		rc = cbchan->ops->waitin(cbchan);
		if (IPC_OK != rc) {
			if (cbchan->ch_status == IPC_DISCONNECT) {
				stdlib_log(LOG_INFO, "%s:%d: disconnected",
				__FUNCTION__, __LINE__); 
			} else if (IPC_INTR == rc) {
				stdlib_log(LOG_INFO, "%s:%d: waitin interrupted",
				__FUNCTION__, __LINE__); 
			} else {
				stdlib_log(LOG_WARNING, "%s:%d: waitin failed: %d",
				__FUNCTION__, __LINE__,rc); 
			}
			return ST_FAIL;
		}
	}

	reply = msgfromIPC_noauth(cbchan);
	reply_type = cl_get_string(reply, F_STONITHD_APIRPL);
	if ( !is_expected_msg(reply, F_STONITHD_TYPE, ST_APIRPL, 
			     F_STONITHD_APIRPL, reply_type, TRUE)) {
		ZAPMSG(reply);
		stdlib_log(LOG_DEBUG, "%s:%d: "
			   "got an unexpected message", __FUNCTION__, __LINE__);
		return ST_FAIL;
	}
	if( !strcmp(reply_type, ST_STRET) ) {
		stonith_ops_t *st_op = NULL;
		/* handle the stonith op result message */
		if( !(st_op = g_new(stonith_ops_t, 1)) ) {
			stdlib_log(LOG_ERR, "out of memory");
			return ST_FAIL;
		}
		st_op->node_uuid = NULL;
		st_op->private_data = NULL;

		st_get_int_value(reply, F_STONITHD_OPTYPE, (int*)&st_op->optype);	
		st_save_string(reply, F_STONITHD_NODE, st_op->node_name);
		st_save_string(reply, F_STONITHD_NODE_UUID, st_op->node_uuid);
		st_get_int_value(reply, F_STONITHD_TIMEOUT, &st_op->timeout);
		st_get_int_value(reply, F_STONITHD_CALLID, &st_op->call_id);
		st_get_int_value(reply, F_STONITHD_FRC, (int*)&st_op->op_result);
		st_save_string(reply, F_STONITHD_NLIST, st_op->node_list);
		st_save_string(reply, F_STONITHD_PDATA, st_op->private_data);

		if (stonith_ops_cb != NULL) {
			stonith_ops_cb(st_op);
		}

		free_stonith_ops_t(st_op);
	}
	else if( !strcmp(reply_type, ST_RAOPRET) ) {
		stonithRA_ops_t *ra_op = NULL;
		/* handle the stonithRA op result message */
		if( !(ra_op = g_new(stonithRA_ops_t, 1)) ) {
			stdlib_log(LOG_ERR, "out of memory");
			return ST_FAIL;
		}

		st_save_string(reply, F_STONITHD_RSCID, ra_op->rsc_id);
		st_save_string(reply, F_STONITHD_RAOPTYPE, ra_op->op_type);
		st_save_string(reply, F_STONITHD_RANAME, ra_op->ra_name);
		st_get_hashtable(reply, F_STONITHD_PARAMS, ra_op->params);
		st_get_int_value(reply, F_STONITHD_CALLID, &ra_op->call_id);
		st_get_int_value(reply, F_STONITHD_FRC, &ra_op->op_result);

		/* if ( rc == ST_OK && stonithRA_ops_cb != NULL)  */
		if ( stonithRA_ops_cb ) {
			stonithRA_ops_cb(ra_op, stonithRA_ops_cb_private_data);
		}

		free_stonithRA_ops_t(ra_op);
	}
	else {
		stdlib_log(LOG_DEBUG, "%s:%d: "
			   "got an unexpected message", __FUNCTION__, __LINE__);
		rc = ST_FAIL;
	}
	ZAPMSG(reply);
	return rc;
}