Example #1
0
static cs_error_t sam_parent_reply_send (
	cs_error_t err,
	int parent_fd_in,
	int parent_fd_out)
{
	char reply;

	if (err == CS_OK) {
		reply = SAM_REPLY_OK;

		if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
			err = CS_ERR_LIBRARY;
			goto error_reply;
		}

		return (CS_OK);
	}

error_reply:
	reply = SAM_REPLY_ERROR;
	if (sam_safe_write (parent_fd_out, &reply, sizeof (reply)) != sizeof (reply)) {
		return (CS_ERR_LIBRARY);
	}
	if (sam_safe_write (parent_fd_out, &err, sizeof (err)) != sizeof (err)) {
		return (CS_ERR_LIBRARY);
	}

	return (err);
}
Example #2
0
cs_error_t sam_warn_signal_set (int warn_signal)
{
	char command;
	cs_error_t err;

	if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
		sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
		sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
		return (CS_ERR_BAD_HANDLE);
	}

	pthread_mutex_lock (&sam_internal_data.lock);

	if (sam_internal_data.am_i_child) {
		/*
		 * We are child so we must send data to parent
		 */
		command = SAM_COMMAND_WARN_SIGNAL_SET;
		if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
			err = CS_ERR_LIBRARY;

			goto error_unlock;
		}

		if (sam_safe_write (sam_internal_data.child_fd_out, &warn_signal, sizeof (warn_signal)) !=
		   sizeof (warn_signal)) {
			err = CS_ERR_LIBRARY;

			goto error_unlock;
		}

		/*
		 * And wait for reply
		 */
		if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
			goto error_unlock;
		}
	}

	/*
	 * We are parent or we received OK reply from parent -> do required action
	 */
	sam_internal_data.warn_signal = warn_signal;

	pthread_mutex_unlock (&sam_internal_data.lock);

	return (CS_OK);

error_unlock:
	pthread_mutex_unlock (&sam_internal_data.lock);

	return (err);
}
Example #3
0
cs_error_t sam_start (void)
{
	char command;
	cs_error_t err;
	sam_recovery_policy_t recpol;

	if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
		return (CS_ERR_BAD_HANDLE);
	}

	recpol = sam_internal_data.recovery_policy;

	if (recpol & SAM_RECOVERY_POLICY_QUORUM || recpol & SAM_RECOVERY_POLICY_CONFDB) {
		pthread_mutex_lock (&sam_internal_data.lock);
	}

	command = SAM_COMMAND_START;

	if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
		if (recpol & SAM_RECOVERY_POLICY_QUORUM || recpol & SAM_RECOVERY_POLICY_CONFDB) {
			pthread_mutex_unlock (&sam_internal_data.lock);
		}

		return (CS_ERR_LIBRARY);
	}

	if (recpol & SAM_RECOVERY_POLICY_QUORUM || recpol & SAM_RECOVERY_POLICY_CONFDB) {
		/*
		 * Wait for parent reply
		 */
		if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
			pthread_mutex_unlock (&sam_internal_data.lock);

			return (err);
		}

		pthread_mutex_unlock (&sam_internal_data.lock);
	}

	if (sam_internal_data.hc_callback)
		if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, sizeof (command)) != sizeof (command))
			return (CS_ERR_LIBRARY);

	sam_internal_data.internal_status = SAM_INTERNAL_STATUS_STARTED;

	return (CS_OK);
}
Example #4
0
cs_error_t sam_stop (void)
{
	char command;

	if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
		return (CS_ERR_BAD_HANDLE);
	}

	command = SAM_COMMAND_STOP;

	if (sam_safe_write (sam_internal_data.parent_fd, &command, 1) == -1)
		return (CS_ERR_LIBRARY);

	if (sam_internal_data.hc_callback)
		if (sam_safe_write (sam_internal_data.cb_wpipe_fd, &command, 1) == -1)
			return (CS_ERR_LIBRARY);

	sam_internal_data.internal_status = SAM_INTERNAL_STATUS_REGISTERED;

	return (CS_OK);
}
Example #5
0
cs_error_t sam_hc_send (void)
{
	char command;

	if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
		return (CS_ERR_BAD_HANDLE);
	}

	command = SAM_COMMAND_HB;

	if (sam_safe_write (sam_internal_data.parent_fd, &command, 1) == -1)
		return (CS_ERR_LIBRARY);

	return (CS_OK);
}
Example #6
0
cs_error_t sam_hc_send (void)
{
	char command;

	if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {
		return (CS_ERR_BAD_HANDLE);
	}

	command = SAM_COMMAND_HB;

	if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
		return (CS_ERR_LIBRARY);

	return (CS_OK);
}
Example #7
0
cs_error_t sam_mark_failed (void)
{
	char command;

	if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED &&
	    sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED) {
		return (CS_ERR_BAD_HANDLE);
	}

	if (!(sam_internal_data.recovery_policy & SAM_RECOVERY_POLICY_CONFDB)) {
		return (CS_ERR_INVALID_PARAM);
	}

	command = SAM_COMMAND_MARK_FAILED;

	if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command))
		return (CS_ERR_LIBRARY);

	return (CS_OK);
}
Example #8
0
cs_error_t sam_data_store (
	const void *data,
	size_t size)
{
	cs_error_t err;
	char command;
	char *new_data;

	if (sam_internal_data.internal_status != SAM_INTERNAL_STATUS_INITIALIZED &&
		sam_internal_data.internal_status != SAM_INTERNAL_STATUS_REGISTERED &&
		sam_internal_data.internal_status != SAM_INTERNAL_STATUS_STARTED) {

		return (CS_ERR_BAD_HANDLE);
	}


	if (data == NULL) {
		size = 0;
	}

	pthread_mutex_lock (&sam_internal_data.lock);

	if (sam_internal_data.am_i_child) {
		/*
		 * We are child so we must send data to parent
		 */
		command = SAM_COMMAND_DATA_STORE;
		if (sam_safe_write (sam_internal_data.child_fd_out, &command, sizeof (command)) != sizeof (command)) {
			err = CS_ERR_LIBRARY;

			goto error_unlock;
		}

		if (sam_safe_write (sam_internal_data.child_fd_out, &size, sizeof (size)) != sizeof (size)) {
			err = CS_ERR_LIBRARY;

			goto error_unlock;
		}

		if (data != NULL && sam_safe_write (sam_internal_data.child_fd_out, data, size) != size) {
			err = CS_ERR_LIBRARY;

			goto error_unlock;
		}

		/*
		 * And wait for reply
		 */
		if ((err = sam_read_reply (sam_internal_data.child_fd_in)) != CS_OK) {
			goto error_unlock;
		}
	}

	/*
	 * We are parent or we received OK reply from parent -> do required action
	 */
	if (data == NULL) {
		free (sam_internal_data.user_data);
		sam_internal_data.user_data = NULL;
		sam_internal_data.user_data_allocated = 0;
		sam_internal_data.user_data_size = 0;
	} else {
		if (sam_internal_data.user_data_allocated < size) {
			if ((new_data = realloc (sam_internal_data.user_data, size)) == NULL) {
				err = CS_ERR_NO_MEMORY;

				goto error_unlock;
			}

			sam_internal_data.user_data_allocated = size;
		} else {
			new_data = sam_internal_data.user_data;
		}
		sam_internal_data.user_data = new_data;
		sam_internal_data.user_data_size = size;

		memcpy (sam_internal_data.user_data, data, size);
	}

	pthread_mutex_unlock (&sam_internal_data.lock);

	return (CS_OK);

error_unlock:
	pthread_mutex_unlock (&sam_internal_data.lock);

	return (err);
}