예제 #1
0
static void test_send_message(void) {
	GIOChannel* in, * out;
	gint fds[2];
	GError* err = NULL;
	wsh_message_size_t msg_size;

	wsh_cmd_res_t res = {
		.std_output_len = 2,
		.std_error_len = 1,
		.exit_status = 0,
	};

	gchar* buf = g_malloc0(encoded_res_len);
	gsize read;

	res.std_output = g_new0(gchar*, 3);
	res.std_error = g_new0(gchar*, 2);
	res.std_output[0] = "foo";
	res.std_output[1] = "bar";
	res.std_output[2] = NULL;
	res.std_error[0] = "baz";
	res.std_error[1] = NULL;

	if (pipe(fds))
		g_assert_not_reached();

	in = g_io_channel_unix_new(fds[0]);
	out = g_io_channel_unix_new(fds[1]);

	wshd_send_message(out, &res, err);

	g_io_channel_set_encoding(in, NULL, NULL);
	g_io_channel_read_chars(in, msg_size.buf, 4, &read, NULL);
	g_assert(g_ntohl(msg_size.size) == encoded_res_len);

	g_io_channel_read_chars(in, buf, encoded_res_len, &read, NULL);
	g_assert_no_error(err);
	g_assert(read == encoded_res_len);

	for (gint i = 0; i < encoded_res_len; i++)
		g_assert(buf[i] == encoded_res[i]);

	g_free(res.std_output);
	g_free(res.std_error);
	g_free(buf);
}

int main(int argc, char** argv) {
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/Server/Output/SendMessage", test_send_message);

	return g_test_run();
}
예제 #2
0
int main(int argc, char** argv, char** env) {
	GIOChannel* in = g_io_channel_unix_new(STDIN_FILENO);
	GIOChannel* out = g_io_channel_unix_new(STDOUT_FILENO);
	GError* err = NULL;
	gint ret = 0;
	wsh_cmd_req_t* req = NULL;
	wsh_cmd_res_t* res = g_slice_new0(wsh_cmd_res_t);

	wsh_init_logger(WSH_LOGGER_SERVER);

	if (wsh_client_init_fds(&err)) {
		wsh_log_message(err->message);
		return EXIT_FAILURE;
	}

	do {
		if (errno == EINTR)
			errno = 0;

		if ((gintptr)(req = (wsh_cmd_req_t*)mmap(NULL, sizeof(*req),
		                    PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0)) == -1 &&
		        errno != EINTR) {
			wsh_log_message(strerror(errno));
			return EXIT_FAILURE;
		}
	} while (errno == EINTR);

	do {
		if (errno == EINTR)
			errno = 0;

		if (mlock((void*)req, sizeof(*req)) && errno != EINTR) {
			wsh_log_message(strerror(errno));
			return EXIT_FAILURE;
		}
	} while (errno == EINTR);

	wshd_get_message(in, &req, err);
	if (err != NULL) {
		ret = err->code;
		goto wshd_error;
	}

	wsh_run_cmd(res, req);

wshd_error:
	do {
		if (errno == EINTR)
			errno = 0;

		if (munlock((void*)req, sizeof(*req)) && errno != EINTR) {
			wsh_log_message(strerror(errno));
			return EXIT_FAILURE;
		}
	} while (errno == EINTR);

	do {
		if (errno == EINTR)
			errno = 0;

		if (munmap((void*)req, sizeof(*req)) && errno != EINTR) {
			wsh_log_message(strerror(errno));
			return EXIT_FAILURE;
		}
	} while (errno == EINTR);

	wshd_send_message(out, res, err);
	if (err != NULL)
		ret = err->code;

	g_slice_free(wsh_cmd_res_t, res);
	return ret;
}