Exemplo n.º 1
0
/*
 * 主函数
 */
int main() {
	pid_t pid;
	int fd;
	pid = fork();
	if (pid < 0) {
		perror("fork failed!");
		exit(1);
	}
	if (pid > 0) {
		//子进程,创建一个alarm服务器
		alarm_server();
	} else {
		DEBUG(" Child=======>  This is the child!\n");
		fd = tcp_init_server(8887);
		loop_process(fd);
	}
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
	int fd, fd_s, clt;

	test_init(argc, argv);

	if ((fd_s = tcp_init_server(ZDTM_FAMILY, &port)) < 0) {
		err("initializing server failed");
		return 1;
	}

	clt = tcp_init_client(ZDTM_FAMILY, "localhost", port);
	if (clt < 0)
		return 1;

	/*
	 * parent is server of TCP connection
	 */
	fd = tcp_accept_server(fd_s);
	if (fd < 0) {
		err("can't accept client connection %m");
		return 1;
	}
	close(fd_s);

	shutdown(fd, SHUT_WR);
	shutdown(clt, SHUT_WR);
	close(fd);

	test_daemon();
	test_waitsig();


	pass();
	return 0;
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
	int fd_s;

	test_init(argc, argv);

	fd_s = tcp_init_server(AF_INET, &port);
	if (fd_s < 0) {
		pr_err("Server initializations failed\n");
		return 1;
	}

	test_daemon();
	test_waitsig();

	if (check_socket_state(fd_s, TCP_LISTEN)) {
		fail("Listen socket state is changed\n");
		close(fd_s);
		return 1;
	}
	close(fd_s);
	pass();
	return 0;
}
Exemplo n.º 4
0
int main(int argc, char **argv)
{
	int fd, fd_s, ctl_fd;
	pid_t extpid;
	int pfd[2];
	int sk_bsize;
	int ret, snd, snd_size, rcv_size = 0, rcv_buf_size;

#ifdef ZDTM_TCP_LOCAL
	test_init(argc, argv);
#endif

	if (pipe(pfd)) {
		pr_perror("pipe() failed");
		return 1;
	}

	extpid = fork();
	if (extpid < 0) {
		pr_perror("fork() failed");
		return 1;
	} else if (extpid == 0) {
		int size;
		char c;

#ifndef ZDTM_TCP_LOCAL
		test_ext_init(argc, argv);
#endif

		close(pfd[1]);
		read_safe(pfd[0], &port, sizeof(port));

		fd = tcp_init_client(ZDTM_FAMILY, "127.0.0.1", port);
		if (fd < 0)
			return 1;

		ctl_fd = tcp_init_client(ZDTM_FAMILY, "127.0.0.1", port);
		if (fd < 0)
			return 1;

		snd_size = fill_sock_buf(fd);
		if (snd_size <= 0)
			return 1;

		write_safe(ctl_fd, &snd_size, sizeof(snd_size));

		read_safe(ctl_fd, &rcv_buf_size, sizeof(rcv_buf_size));

		while (1) {
			/* heart beat */
			read_safe(ctl_fd, &ret, sizeof(ret));
			if (ret < 0)
				break;
			rcv_buf_size += ret;

			snd = fill_sock_buf(fd);
			if (snd < 0)
				return -1;
			snd_size += snd;

			if (rcv_buf_size / 2) {
				ret = clean_sk_buf(fd, rcv_buf_size / 2);
				if (ret <= 0)
					return 1;
			} else
				ret = 0;

			rcv_buf_size -= ret;
			rcv_size += ret;

			write_safe(ctl_fd, &snd, sizeof(snd));
		}

		read_safe(ctl_fd, &ret, sizeof(ret));
		rcv_buf_size += ret;

		write_safe(ctl_fd, &snd_size, sizeof(snd_size));

		if (read(ctl_fd, &c, 1) != 0) {
			pr_perror("read");
			return 1;
		}

		if (shutdown(fd, SHUT_WR) == -1) {
			pr_perror("shutdown");
			return 1;
		}

		size = clean_sk_buf(fd, 0);
		if (size < 0)
			return 1;

		if (size != rcv_buf_size) {
			fail("the received buffer contains only %d bytes (%d)\n", size, rcv_buf_size);
		}

		rcv_size += size;

		write_safe(ctl_fd, &rcv_size, sizeof(rcv_size));
		close(fd);

		return 0;
	}

#ifndef ZDTM_TCP_LOCAL
	test_init(argc, argv);
#endif

	if ((fd_s = tcp_init_server(ZDTM_FAMILY, &port)) < 0) {
		pr_perror("initializing server failed");
		return 1;
	}

	close(pfd[0]);
	write_safe(pfd[1], &port, sizeof(port));
	close(pfd[1]);

	/*
	 * parent is server of TCP connection
	 */
	fd = tcp_accept_server(fd_s);
	if (fd < 0) {
		pr_perror("can't accept client connection %m");
		return 1;
	}

	ctl_fd = tcp_accept_server(fd_s);
	if (ctl_fd < 0) {
		pr_perror("can't accept client connection %m");
		return 1;
	}

	sk_bsize = TCP_MAX_BUF;
	if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
			&sk_bsize, sizeof(sk_bsize)) == -1) {
		pr_perror("Can't set snd buf");
		return 1;
	}

	sk_bsize = TCP_MAX_BUF;
	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
			&sk_bsize, sizeof(sk_bsize)) == -1) {
		pr_perror("Can't set snd buf");
		return 1;
	}

	snd_size = fill_sock_buf(fd);
	if (snd_size <= 0)
		return 1;

	read_safe(ctl_fd, &rcv_buf_size, sizeof(rcv_buf_size));

	write_safe(ctl_fd, &snd_size, sizeof(snd_size));

	test_daemon();

	snd = 0;
	while (test_go()) {
		/* heart beat */
		if (rcv_buf_size / 2) {
			ret = clean_sk_buf(fd, rcv_buf_size / 2);
			if (ret <= 0)
				return 1;
		} else
			ret = 0;

		rcv_size += ret;
		rcv_buf_size -= ret;

		write_safe(ctl_fd, &snd, sizeof(snd));
		read_safe(ctl_fd, &ret, sizeof(ret));

		rcv_buf_size += ret;

		snd = fill_sock_buf(fd);
		if (snd < 0)
			return -1;
		snd_size += snd;
	}

	ret = -1;
	write_safe(ctl_fd, &ret, sizeof(ret));
	write_safe(ctl_fd, &snd, sizeof(ret));
	read_safe(ctl_fd, &snd, sizeof(snd));

	if (shutdown(ctl_fd, SHUT_WR) == -1) {
		pr_perror("shutdown");
		return 1;
	}

	if (shutdown(fd, SHUT_WR) == -1) {
		pr_perror("shutdown");
		return 1;
	}

	ret = clean_sk_buf(fd, 0);
	if (ret != rcv_buf_size) {
		fail("the received buffer contains only %d bytes (%d)\n", ret, rcv_buf_size);
	}
	rcv_size += ret;

	if (snd != rcv_size) {
		fail("The child sent %d bytes, but the parent received %d bytes\n", rcv_buf_size, rcv_size);
		return 1;
	}

	read_safe(ctl_fd, &ret, sizeof(ret));

	if (ret != snd_size) {
		fail("The parent sent %d bytes, but the child received %d bytes\n", snd_size, ret);
		return 1;
	}

	pass();
	return 0;
}
Exemplo n.º 5
0
int main(int argc, char **argv)
{
	char *newns = getenv("ZDTM_NEWNS");
	int fd, fd_s, ctl_fd;
	pid_t extpid;
	int pfd[2];
	int ret = 0, snd_size = 0, rcv_size = 0;
#ifndef ZDTM_TCP_LAST_ACK
	char buf[BUF_SIZE];
#endif

	if (newns)
		test_init(argc, argv);

	if (pipe(pfd)) {
		pr_perror("pipe() failed");
		return 1;
	}

	extpid = fork();
	if (extpid < 0) {
		pr_perror("fork() failed");
		return 1;
	} else if (extpid == 0) {
		int size = 0;
		char c;

		if (!newns)
			test_ext_init(argc, argv);

		close(pfd[1]);
		if (read(pfd[0], &port, sizeof(port)) != sizeof(port)) {
			pr_perror("Can't read port\n");
			return 1;
		}
		close(pfd[0]);

		fd = tcp_init_client(ZDTM_FAMILY, "127.0.0.1", port);
		if (fd < 0)
			return 1;

		ctl_fd = tcp_init_client(ZDTM_FAMILY, "127.0.0.1", port);
		if (ctl_fd < 0)
			return 1;

		/* == The preparation stage == */
		if (read(ctl_fd, &size, sizeof(size)) != sizeof(size)) {
			pr_perror("read");
			return 1;
		}

		if (shutdown(fd, SHUT_WR) == -1) {
			pr_perror("shutdown");
			return 1;
		}

		if (write(ctl_fd, &size, sizeof(size)) != sizeof(size)) {
			pr_perror("write");
			return 1;
		}
		/* == End of the preparation stage == */

		/* Checkpoint/restore */

		/* == The final stage == */
		if (read(ctl_fd, &c, 1) != 0) {
			pr_perror("read");
			return 1;
		}

#ifdef ZDTM_TCP_LAST_ACK
		size = clean_sk_buf(fd);
		if (size < 0)
			return 1;
#else
		if (read(fd, buf, sizeof(buf)) != sizeof(TEST_MSG) ||
		    strncmp(buf, TEST_MSG, sizeof(TEST_MSG))) {
			pr_perror("read");
			return 1;
		}
#endif

		if (write(ctl_fd, &size, sizeof(size)) != sizeof(size)) {
			pr_perror("write");
			return 1;
		}
		/* == End of the final stage == */

		close(ctl_fd);
		close(fd);

		return 0;
	}

	if (!newns)
		test_init(argc, argv);

	if ((fd_s = tcp_init_server(ZDTM_SRV_FAMILY, &port)) < 0) {
		pr_err("initializing server failed");
		return 1;
	}

	close(pfd[0]);
	if (write(pfd[1], &port, sizeof(port)) != sizeof(port)) {
		pr_perror("Can't send port");
		return 1;
	}
	close(pfd[1]);

	/*
	 * parent is server of TCP connection
	 */
	fd = tcp_accept_server(fd_s);
	if (fd < 0) {
		pr_err("can't accept client connection");
		return 1;
	}

	ctl_fd = tcp_accept_server(fd_s);
	if (ctl_fd < 0) {
		pr_err("can't accept client connection");
		return 1;
	}

	/* == The preparation stage == */
#ifdef ZDTM_TCP_LAST_ACK
	snd_size = fill_sock_buf(fd);
	if (snd_size <= 0)
		return 1;
#endif

	if (write(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
		pr_perror("read");
		return 1;
	}

	if (read(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
		pr_perror("read");
		return 1;
	}
	/* == End of the preparation stage */

#ifdef ZDTM_TCP_LAST_ACK
	if (shutdown(fd, SHUT_WR) == -1) {
		pr_perror("shutdown");
		return 1;
	}
#endif

	test_daemon();
	test_waitsig();

	/* == The final stage == */
	if (shutdown(ctl_fd, SHUT_WR) == -1) {
		pr_perror("shutdown");
		return 1;
	}

#ifndef ZDTM_TCP_LAST_ACK
	if (write(fd, TEST_MSG, sizeof(TEST_MSG)) != sizeof(TEST_MSG)) {
		pr_perror("write");
		return 1;
	}

	if (shutdown(fd, SHUT_WR) == -1) {
		pr_perror("shutdown");
		return 1;
	}
#endif

	rcv_size = clean_sk_buf(fd);

	if (ret != rcv_size) {
		fail("The child sent %d bytes, but the parent received %d bytes\n", ret, rcv_size);
		return 1;
	}

	if (read(ctl_fd, &ret, sizeof(ret)) != sizeof(ret)) {
		pr_perror("read");
		return 1;
	}
	/* == End of the final stage == */

	if (ret != snd_size) {
		fail("The parent sent %d bytes, but the child received %d bytes\n", snd_size, ret);
		return 1;
	}

	pass();
	return 0;
}
Exemplo n.º 6
0
int main(int argc, char **argv)
{
	unsigned char buf[BUF_SIZE];
	int fd, fd_s;
	int status;
	pid_t pid;
	int res;
	uint32_t crc;
	struct sigaction sa = {
		.sa_handler	= sig_hand,
		/* don't set SA_RESTART */
	};

	test_init(argc, argv);

	if ((fd_s = tcp_init_server(ZDTM_FAMILY, &port)) < 0) {
		err("initializing server failed");
		return 1;
	}

	test_daemon();
	test_waitsig();

	sigemptyset(&sa.sa_mask);
	if (sigaction(SIGCHLD, &sa, NULL))
		fprintf(stderr, "Can't set SIGTERM handler: %m\n");

	pid = test_fork();
	if (pid < 0) {
		err("fork failed. Return %d %m", pid);
		return 1;
	}

	if (pid == 0) {
		/*
		 * Chiled is client of TCP connection
		 */
		close(fd_s);
		fd = tcp_init_client(ZDTM_FAMILY, "localhost", port);
		if (fd < 0)
			return 1;

		res = read(fd, buf, BUF_SIZE);
		close(fd);
		if (res != BUF_SIZE) {
			err("read less then have to: %d instead of %d", res, BUF_SIZE);
			return -1;
		}
		if (datachk(buf, BUF_SIZE, &crc))
			return -2;
		return 0;
	}

	/*
	 * parent is server of TCP connection
	 */
	fd = tcp_accept_server(fd_s);
	close(fd_s);
	if (fd < 0) {
		err("can't accept client connection %m");
		goto error;
	}

	datagen(buf, BUF_SIZE, &crc);
	if (write(fd, buf, BUF_SIZE) < BUF_SIZE) {
		err("can't write");
		goto error;
	}
	close(fd);


	if (wait(&status) < 0) {
		err("wait failed %m");
		goto error;
	}

	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
		err("chiled failed. Return %d", WEXITSTATUS(status));
		return 1;
	}

	pass();
	return 0;
error:
	kill(pid, SIGKILL);
	wait(&status);
	return -1;
}
Exemplo n.º 7
0
int main(int argc, char **argv)
{
	unsigned char buf[BUF_SIZE];
	int fd, fd_s;
	pid_t extpid;
	uint32_t crc;
	int pfd[2];
	int val;
	socklen_t optlen;

	if (pipe(pfd)) {
		err("pipe() failed");
		return 1;
	}

	extpid = fork();
	if (extpid < 0) {
		err("fork() failed");
		return 1;
	} else if (extpid == 0) {
		test_ext_init(argc, argv);

		close(pfd[1]);
		if (read(pfd[0], &port, sizeof(port)) != sizeof(port)) {
			err("Can't read port\n");
			return 1;
		}

		fd = tcp_init_client(ZDTM_FAMILY, "localhost", port);
		if (fd < 0)
			return 1;

#ifdef STREAM
		while (1) {
			if (read_data(fd, buf, BUF_SIZE)) {
				err("read less then have to");
				return 1;
			}
			if (datachk(buf, BUF_SIZE, &crc))
				return 2;

			datagen(buf, BUF_SIZE, &crc);
			if (write_data(fd, buf, BUF_SIZE)) {
				err("can't write");
				return 1;
			}
		}
#else
		if (read_data(fd, buf, BUF_SIZE)) {
			err("read less then have to");
			return 1;
		}
		if (datachk(buf, BUF_SIZE, &crc))
			return 2;

		datagen(buf, BUF_SIZE, &crc);
		if (write_data(fd, buf, BUF_SIZE)) {
			err("can't write");
			return 1;
		}
#endif
		return 0;
	}

	test_init(argc, argv);

	if ((fd_s = tcp_init_server(ZDTM_FAMILY, &port)) < 0) {
		err("initializing server failed");
		return 1;
	}

	close(pfd[0]);
	if (write(pfd[1], &port, sizeof(port)) != sizeof(port)) {
		err("Can't send port");
		return 1;
	}
	close(pfd[1]);

	/*
	 * parent is server of TCP connection
	 */
	fd = tcp_accept_server(fd_s);
	if (fd < 0) {
		err("can't accept client connection %m");
		return 1;
	}

	val = 1;
	if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))) {
		err("setsockopt");
		return 1;
	}

	test_daemon();
#ifdef STREAM
	while (test_go()) {
		datagen(buf, BUF_SIZE, &crc);
		if (write_data(fd, buf, BUF_SIZE)) {
			err("can't write");
			return 1;
		}

		if (read_data(fd, buf, BUF_SIZE)) {
			err("read less then have to");
			return 1;
		}
		if (datachk(buf, BUF_SIZE, &crc))
			return 2;
	}
	kill(extpid, SIGKILL);
#else
	test_waitsig();

	datagen(buf, BUF_SIZE, &crc);
	if (write_data(fd, buf, BUF_SIZE)) {
		err("can't write");
		return 1;
	}

	if (read_data(fd, buf, BUF_SIZE)) {
		err("read less then have to");
		return 1;
	}
	if (datachk(buf, BUF_SIZE, &crc))
		return 2;
#endif
	optlen = sizeof(val);
	if (getsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, &optlen)) {
		err("getsockopt");
		return 1;
	}
	if (val != 1) {
		fail("SO_REUSEADDR are not set for %d\n", fd);
		return 1;
	}

	pass();
	return 0;
}
Exemplo n.º 8
0
int main(int argc, char **argv)
{
	char buf[BUF_SIZE];
	int fd, fd_s;
	struct aiocb aiocb;
	int status;
	pid_t pid;
	int ret, res;
	const struct aiocb   *aioary[1];
	task_waiter_t child_waiter;

	test_init(argc, argv);

	task_waiter_init(&child_waiter);

	if ((fd_s = tcp_init_server(AF_INET, &port)) < 0) {
		pr_err("initializing server failed\n");
		return 1;
	}

	pid = test_fork();
	if (pid < 0) {
		pr_perror("fork failed");
		return 1;
	}

	if (pid == 0) {
		/*
		 * Chiled is client of TCP connection
		 */
		close(fd_s);
		fd = tcp_init_client(AF_INET, "127.0.0.1", port);
		if (fd < 0)
			return 1;

		memset(&aiocb, 0, sizeof(struct aiocb));
		aiocb.aio_fildes = fd;
		aiocb.aio_buf = buf;
		aiocb.aio_nbytes = BUF_SIZE;
		ret = aio_read(&aiocb);
		if (ret < 0) {
			pr_perror("aio_read failed");
			return 1;
		}

		task_waiter_complete_current(&child_waiter);

		/* Wait for request completion */
		aioary[0] = &aiocb;
		ret = aio_error(&aiocb);
#ifdef DEBUG
		test_msg(".");
#endif
		res = 0;
again:
		if (aio_suspend(aioary, 1, NULL) < 0 && errno != EINTR) {
			pr_perror("aio_suspend failed");
			res = 1;
		}

		ret = aio_error(&aiocb);
		if (!res && ret == EINPROGRESS) {
#ifdef DEBUG
			test_msg("restart aio_suspend\n");
#endif
			goto again;
		}
		if (ret != 0) {
			pr_err("Error at aio_error(): %s\n", strerror(ret));
			res = 1;
		}

		if (aio_return(&aiocb) != BUF_SIZE) {
			pr_perror("Error at aio_return()");
			res = 1;
		}

		close(fd);
		return res;
	}

	/*
	 * parent is server of TCP connection
	 */
	fd = tcp_accept_server(fd_s);
	close(fd_s);
	if (fd < 0) {
		pr_err("can't accept client connection\n");
		goto error;
	}

	task_waiter_wait4(&child_waiter, pid);

	test_daemon();
	test_waitsig();

	if (write(fd, buf, BUF_SIZE) < BUF_SIZE) {
		pr_perror("can't write");
		goto error;
	}
	close(fd);


	if (wait(&status) < 0) {
		pr_perror("wait failed");
		goto error;
	}

	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
		pr_err("child failed with exit code %d\n", WEXITSTATUS(status));
		return 1;
	}

	pass();
	return 0;
error:
	kill(pid, SIGKILL);
	wait(&status);
	return -1;
}
Exemplo n.º 9
0
int main(int argc, char **argv)
{
	int fd, fd_s, clt;
	char cmd[4096], buf[10];

	test_init(argc, argv);
	signal(SIGPIPE, SIG_IGN);

	if ((fd_s = tcp_init_server(ZDTM_SRV_FAMILY, &port)) < 0) {
		pr_err("initializing server failed\n");
		return 1;
	}


	clt = tcp_init_client(ZDTM_FAMILY, "localhost", port);
	if (clt < 0) {
		pr_perror("Unable to create a client socket");
	        return 1;
	}

	/*
	* parent is server of TCP connection
	*/
	fd = tcp_accept_server(fd_s);
	if (fd < 0) {
		pr_err("can't accept client connection\n");
		return 1;
	}
	if (write(clt, "asd", 3) != 3) {
		pr_perror("Unable to write into a socket");
		return 1;
	}
	snprintf(cmd, sizeof(cmd), "iptables -w -t filter --protocol tcp -A INPUT --dport %d -j REJECT --reject-with tcp-reset", port);
	if (system(cmd))
		return 1;

	if (write(fd, "asdas", 5) == -1) {
		pr_perror("Unable to write into a socket");
		return 1;
	}

	snprintf(cmd, sizeof(cmd), "iptables -w -t filter --protocol tcp -D INPUT --dport %d -j REJECT --reject-with tcp-reset", port);
	if (system(cmd))
		return 1;

	test_daemon();
	test_waitsig();

	if (read(fd, buf, sizeof(buf)) != 3) {
		fail("Unable to read data from a socket");
		return 1;
	}

	if (write(fd, buf, 3) != -1) {
		fail("Can write into a closed socket");
		return 1;
	}

	pass();
	return 0;
}