Remove tabs in the source files of test cases
This commit is contained in:
		
							parent
							
								
									1dcabb09cd
								
							
						
					
					
						commit
						614ec88242
					
				| @ -37,7 +37,7 @@ static int remove_file(const char *file_path) { | |||||||
| 
 | 
 | ||||||
| static int __test_write_read(const char *file_path) { | static int __test_write_read(const char *file_path) { | ||||||
|     char *write_str = "Write to hostfs successfully!"; |     char *write_str = "Write to hostfs successfully!"; | ||||||
| 	char read_buf[128] = { 0 }; |     char read_buf[128] = { 0 }; | ||||||
|     int fd; |     int fd; | ||||||
| 
 | 
 | ||||||
|     fd = open(file_path, O_WRONLY); |     fd = open(file_path, O_WRONLY); | ||||||
| @ -52,34 +52,34 @@ static int __test_write_read(const char *file_path) { | |||||||
|     if (fd < 0) { |     if (fd < 0) { | ||||||
|         THROW_ERROR("failed to open a file to read"); |         THROW_ERROR("failed to open a file to read"); | ||||||
|     } |     } | ||||||
| 	if (read(fd, read_buf, sizeof(read_buf)) != strlen(write_str)) { |     if (read(fd, read_buf, sizeof(read_buf)) != strlen(write_str)) { | ||||||
| 		THROW_ERROR("failed to read to the file"); |         THROW_ERROR("failed to read to the file"); | ||||||
| 	} |     } | ||||||
| 	if (strcmp(write_str, read_buf) != 0) { |     if (strcmp(write_str, read_buf) != 0) { | ||||||
| 		THROW_ERROR("the message read from the file is not as it was written"); |         THROW_ERROR("the message read from the file is not as it was written"); | ||||||
| 	} |     } | ||||||
|     close(fd); |     close(fd); | ||||||
|     return 0; |     return 0; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static int __test_rename(const char *file_path) { | static int __test_rename(const char *file_path) { | ||||||
| 	char *rename_path = "/host/hostfs_rename.txt"; |     char *rename_path = "/host/hostfs_rename.txt"; | ||||||
| 	struct stat stat_buf; |     struct stat stat_buf; | ||||||
| 	int ret; |     int ret; | ||||||
| 
 | 
 | ||||||
| 	ret = rename(file_path, rename_path); |     ret = rename(file_path, rename_path); | ||||||
| 	if (ret < 0) { |     if (ret < 0) { | ||||||
|         THROW_ERROR("failed to rename"); |         THROW_ERROR("failed to rename"); | ||||||
|     } |     } | ||||||
| 	ret = stat(file_path, &stat_buf); |     ret = stat(file_path, &stat_buf); | ||||||
| 	if (!(ret < 0 && errno == ENOENT)) { |     if (!(ret < 0 && errno == ENOENT)) { | ||||||
| 		THROW_ERROR("stat should return ENOENT"); |         THROW_ERROR("stat should return ENOENT"); | ||||||
| 	} |     } | ||||||
| 	ret = stat(rename_path, &stat_buf); |     ret = stat(rename_path, &stat_buf); | ||||||
| 	if (ret < 0) { |     if (ret < 0) { | ||||||
| 		THROW_ERROR("failed to stat the file"); |         THROW_ERROR("failed to stat the file"); | ||||||
| 	} |     } | ||||||
| 	if (rename(rename_path, file_path) < 0) { |     if (rename(rename_path, file_path) < 0) { | ||||||
|         THROW_ERROR("failed to rename back"); |         THROW_ERROR("failed to rename back"); | ||||||
|     } |     } | ||||||
|     return 0; |     return 0; | ||||||
| @ -90,9 +90,9 @@ typedef int(*test_hostfs_func_t)(const char *); | |||||||
| static int test_hostfs_framework(test_hostfs_func_t fn) { | static int test_hostfs_framework(test_hostfs_func_t fn) { | ||||||
|     const char *file_path = "/host/hostfs_test.txt"; |     const char *file_path = "/host/hostfs_test.txt"; | ||||||
| 
 | 
 | ||||||
| 	if (create_file(file_path) < 0) |     if (create_file(file_path) < 0) | ||||||
| 		return -1; |         return -1; | ||||||
| 	if (fn(file_path) < 0) |     if (fn(file_path) < 0) | ||||||
|         return -1; |         return -1; | ||||||
|     if (remove_file(file_path) < 0) |     if (remove_file(file_path) < 0) | ||||||
|         return -1; |         return -1; | ||||||
|  | |||||||
							
								
								
									
										142
									
								
								test/link/main.c
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										142
									
								
								test/link/main.c
									
									
									
									
									
								
							| @ -7,84 +7,84 @@ | |||||||
| #include <errno.h> | #include <errno.h> | ||||||
| 
 | 
 | ||||||
| int main(int argc, const char* argv[]) { | int main(int argc, const char* argv[]) { | ||||||
| 	const char* file_name = "/root/test_filesystem_link.txt"; |     const char* file_name = "/root/test_filesystem_link.txt"; | ||||||
| 	const char* link_name = "/root/link.txt"; |     const char* link_name = "/root/link.txt"; | ||||||
| 	const char* write_msg = "Hello World\n"; |     const char* write_msg = "Hello World\n"; | ||||||
| 	char read_buf[128] = {0}; |     char read_buf[128] = {0}; | ||||||
| 	int ret; |     int ret; | ||||||
| 
 | 
 | ||||||
| 	// create a file and write message
 |     // create a file and write message
 | ||||||
| 	int flags = O_WRONLY | O_CREAT| O_TRUNC; |     int flags = O_WRONLY | O_CREAT| O_TRUNC; | ||||||
| 	int mode = 00666; |     int mode = 00666; | ||||||
| 	int fd = open(file_name, flags, mode); |     int fd = open(file_name, flags, mode); | ||||||
| 	if (fd < 0) { |     if (fd < 0) { | ||||||
| 		printf("ERROR: failed to open a file for write\n"); |         printf("ERROR: failed to open a file for write\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	int len = write(fd, write_msg, strlen(write_msg)); |     int len = write(fd, write_msg, strlen(write_msg)); | ||||||
| 	if (len <= 0) { |     if (len <= 0) { | ||||||
| 		printf("ERROR: failed to write to the file\n"); |         printf("ERROR: failed to write to the file\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	close(fd); |     close(fd); | ||||||
| 
 | 
 | ||||||
| 	// link
 |     // link
 | ||||||
| 	ret = link(file_name, link_name); |     ret = link(file_name, link_name); | ||||||
| 	if(ret < 0) { |     if(ret < 0) { | ||||||
| 		printf("ERROR: failed to link the file\n"); |         printf("ERROR: failed to link the file\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// read the link file
 |     // read the link file
 | ||||||
| 	fd = open(link_name, O_RDONLY, 00666); |     fd = open(link_name, O_RDONLY, 00666); | ||||||
| 	if (fd < 0) { |     if (fd < 0) { | ||||||
| 		printf("ERROR: failed to open the file for read\n"); |         printf("ERROR: failed to open the file for read\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	len = read(fd, read_buf, sizeof(read_buf)); |     len = read(fd, read_buf, sizeof(read_buf)); | ||||||
| 	if (len != strlen(write_msg)) { |     if (len != strlen(write_msg)) { | ||||||
| 		printf("ERROR: failed to read to the file\n"); |         printf("ERROR: failed to read to the file\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	ret = strcmp(write_msg, read_buf); |     ret = strcmp(write_msg, read_buf); | ||||||
| 	if (ret != 0) { |     if (ret != 0) { | ||||||
| 		printf("ERROR: the message read from the file is not as it was written\n"); |         printf("ERROR: the message read from the file is not as it was written\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// unlink
 |     // unlink
 | ||||||
| 	ret = unlink(link_name); |     ret = unlink(link_name); | ||||||
| 	if(ret < 0) { |     if(ret < 0) { | ||||||
| 		printf("ERROR: failed to link the file\n"); |         printf("ERROR: failed to link the file\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// stat
 |     // stat
 | ||||||
| 	struct stat stat_buf; |     struct stat stat_buf; | ||||||
| 	ret = stat(link_name, &stat_buf); |     ret = stat(link_name, &stat_buf); | ||||||
| 	if(!(ret < 0 && errno == ENOENT)) { |     if(!(ret < 0 && errno == ENOENT)) { | ||||||
| 		printf("ERROR: stat on \"%s\" should return ENOENT", link_name); |         printf("ERROR: stat on \"%s\" should return ENOENT", link_name); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// rename
 |     // rename
 | ||||||
| 	ret = rename(file_name, link_name); |     ret = rename(file_name, link_name); | ||||||
| 	if(ret < 0) { |     if(ret < 0) { | ||||||
| 		printf("ERROR: failed to rename the file"); |         printf("ERROR: failed to rename the file"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// stat
 |     // stat
 | ||||||
| 	ret = stat(file_name, &stat_buf); |     ret = stat(file_name, &stat_buf); | ||||||
| 	if(!(ret < 0 && errno == ENOENT)) { |     if(!(ret < 0 && errno == ENOENT)) { | ||||||
| 		printf("ERROR: stat on \"%s\" should return ENOENT", file_name); |         printf("ERROR: stat on \"%s\" should return ENOENT", file_name); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	ret = stat(link_name, &stat_buf); |     ret = stat(link_name, &stat_buf); | ||||||
| 	if(ret < 0) { |     if(ret < 0) { | ||||||
| 		printf("ERROR: failed to stat the file"); |         printf("ERROR: failed to stat the file"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
|     printf("link, unlink, rename test successful\n"); |     printf("link, unlink, rename test successful\n"); | ||||||
|     return 0; |     return 0; | ||||||
|  | |||||||
| @ -7,62 +7,62 @@ | |||||||
| 
 | 
 | ||||||
| int main(int argc, const char* argv[]) { | int main(int argc, const char* argv[]) { | ||||||
| 
 | 
 | ||||||
| 	const int BUF_SIZE = 20; |     const int BUF_SIZE = 20; | ||||||
| 	char buf[10]; |     char buf[10]; | ||||||
| 	int ret; |     int ret; | ||||||
| 
 | 
 | ||||||
| 	char* cwd = getcwd(buf, BUF_SIZE); |     char* cwd = getcwd(buf, BUF_SIZE); | ||||||
| 	if(cwd != buf) { |     if(cwd != buf) { | ||||||
| 		printf("failed to getcwd\n"); |         printf("failed to getcwd\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	const char expect_cwd[] = "/"; |     const char expect_cwd[] = "/"; | ||||||
| 	if(strcmp(buf, expect_cwd)) { |     if(strcmp(buf, expect_cwd)) { | ||||||
| 		printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd); |         printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, expect_cwd); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	//const char DIR_NAME[] = "test_dir";
 |     //const char DIR_NAME[] = "test_dir";
 | ||||||
| 	//const char DIR_PATH[] = "/test_dir";
 |     //const char DIR_PATH[] = "/test_dir";
 | ||||||
| 	const char DIR_NAME[] = "/root/test_dir"; |     const char DIR_NAME[] = "/root/test_dir"; | ||||||
| 	const char DIR_PATH[] = "/root/test_dir"; |     const char DIR_PATH[] = "/root/test_dir"; | ||||||
| 	const int DIR_MODE = 0664; |     const int DIR_MODE = 0664; | ||||||
| 	ret = mkdir(DIR_NAME, DIR_MODE); |     ret = mkdir(DIR_NAME, DIR_MODE); | ||||||
| 	if(ret < 0) { |     if(ret < 0) { | ||||||
| 		printf("failed to mkdir \"%s\"", DIR_NAME); |         printf("failed to mkdir \"%s\"", DIR_NAME); | ||||||
| 		return ret; |         return ret; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	ret = chdir(DIR_NAME); |     ret = chdir(DIR_NAME); | ||||||
| 	if(ret < 0) { |     if(ret < 0) { | ||||||
| 		printf("failed to chdir to \"%s\"", DIR_NAME); |         printf("failed to chdir to \"%s\"", DIR_NAME); | ||||||
| 		return ret; |         return ret; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	cwd = getcwd(buf, BUF_SIZE); |     cwd = getcwd(buf, BUF_SIZE); | ||||||
| 	if(cwd != buf) { |     if(cwd != buf) { | ||||||
| 		printf("failed to getcwd\n"); |         printf("failed to getcwd\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	if(strcmp(buf, DIR_PATH)) { |     if(strcmp(buf, DIR_PATH)) { | ||||||
| 		printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH); |         printf("incorrect cwd \"%s\". expect \"%s\".\n", buf, DIR_PATH); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	ret = rmdir(DIR_PATH); |     ret = rmdir(DIR_PATH); | ||||||
| 	if(ret < 0) { |     if(ret < 0) { | ||||||
| 		printf("failed to rmdir \"%s\"", DIR_PATH); |         printf("failed to rmdir \"%s\"", DIR_PATH); | ||||||
| 		return ret; |         return ret; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	struct stat stat_buf; |     struct stat stat_buf; | ||||||
| 	ret = stat(DIR_PATH, &stat_buf); |     ret = stat(DIR_PATH, &stat_buf); | ||||||
| 	if(!(ret < 0 && errno == ENOENT)) { |     if(!(ret < 0 && errno == ENOENT)) { | ||||||
| 		printf("stat on \"%s\" should return ENOENT", DIR_PATH); |         printf("stat on \"%s\" should return ENOENT", DIR_PATH); | ||||||
| 		return ret; |         return ret; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
|     printf("getcwd, mkdir, rmdir, chdir test successful\n"); |     printf("getcwd, mkdir, rmdir, chdir test successful\n"); | ||||||
|     return 0; |     return 0; | ||||||
|  | |||||||
| @ -45,35 +45,35 @@ int main(int argc, const char* argv[]) { | |||||||
|         return -1; |         return -1; | ||||||
|     } |     } | ||||||
|     int file_mode = stat_buf.st_mode & MODE_MASK; |     int file_mode = stat_buf.st_mode & MODE_MASK; | ||||||
| 	if (file_mode != mode) { |     if (file_mode != mode) { | ||||||
| 		printf("Incorrect file mode %o. Expected %o\n", file_mode, mode); |         printf("Incorrect file mode %o. Expected %o\n", file_mode, mode); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	int file_type = stat_buf.st_mode & S_IFMT; |     int file_type = stat_buf.st_mode & S_IFMT; | ||||||
| 	if (file_type != S_IFREG) { |     if (file_type != S_IFREG) { | ||||||
| 		printf("Incorrect file type %o. Expected %o\n", file_type, S_IFREG); |         printf("Incorrect file type %o. Expected %o\n", file_type, S_IFREG); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
|     close(fd); |     close(fd); | ||||||
| 
 | 
 | ||||||
| 	ret = truncate(FILE_NAME, TRUNC_LEN1); |     ret = truncate(FILE_NAME, TRUNC_LEN1); | ||||||
| 	if (ret < 0) { |     if (ret < 0) { | ||||||
| 		printf("failed to truncate the file\n"); |         printf("failed to truncate the file\n"); | ||||||
| 		return ret; |         return ret; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	ret = stat(FILE_NAME, &stat_buf); |     ret = stat(FILE_NAME, &stat_buf); | ||||||
| 	if (ret < 0) { |     if (ret < 0) { | ||||||
| 		printf("failed to stat the file\n"); |         printf("failed to stat the file\n"); | ||||||
| 		return ret; |         return ret; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	file_size = stat_buf.st_size; |     file_size = stat_buf.st_size; | ||||||
| 	if (file_size != TRUNC_LEN1) { |     if (file_size != TRUNC_LEN1) { | ||||||
| 		printf("Incorrect file size %d. Expected %d\n", file_size, TRUNC_LEN1); |         printf("Incorrect file size %d. Expected %d\n", file_size, TRUNC_LEN1); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
|     printf("(f)truncate, (f)stat test successful\n"); |     printf("(f)truncate, (f)stat test successful\n"); | ||||||
|     return 0; |     return 0; | ||||||
|  | |||||||
| @ -21,150 +21,150 @@ | |||||||
| const char SOCK_PATH[] = "echo_socket"; | const char SOCK_PATH[] = "echo_socket"; | ||||||
| 
 | 
 | ||||||
| int create_server_socket() { | int create_server_socket() { | ||||||
| 	int fd = socket(AF_UNIX, SOCK_STREAM, 0); |     int fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||||||
| 	if (fd == -1) { |     if (fd == -1) { | ||||||
| 		printf("ERROR: failed to create a unix socket\n"); |         printf("ERROR: failed to create a unix socket\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	struct sockaddr_un local; |     struct sockaddr_un local; | ||||||
| 	local.sun_family = AF_UNIX; |     local.sun_family = AF_UNIX; | ||||||
| 	strcpy(local.sun_path, SOCK_PATH); |     strcpy(local.sun_path, SOCK_PATH); | ||||||
| 	unlink(local.sun_path); |     unlink(local.sun_path); | ||||||
| 	socklen_t len = strlen(local.sun_path) + sizeof(local.sun_family); |     socklen_t len = strlen(local.sun_path) + sizeof(local.sun_family); | ||||||
| 
 | 
 | ||||||
| 	if (bind(fd, (struct sockaddr *)&local, len) == -1) { |     if (bind(fd, (struct sockaddr *)&local, len) == -1) { | ||||||
| 		printf("ERROR: failed to bind\n"); |         printf("ERROR: failed to bind\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	if (listen(fd, 5) == -1) { |     if (listen(fd, 5) == -1) { | ||||||
| 		printf("ERROR: failed to listen\n"); |         printf("ERROR: failed to listen\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	return fd; |     return fd; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int create_client_socket() { | int create_client_socket() { | ||||||
| 	int fd = socket(AF_UNIX, SOCK_STREAM, 0); |     int fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||||||
| 	if (fd == -1) { |     if (fd == -1) { | ||||||
| 		printf("ERROR: failed to create a unix socket\n"); |         printf("ERROR: failed to create a unix socket\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	struct sockaddr_un remote; |     struct sockaddr_un remote; | ||||||
| 	remote.sun_family = AF_UNIX; |     remote.sun_family = AF_UNIX; | ||||||
| 	strcpy(remote.sun_path, SOCK_PATH); |     strcpy(remote.sun_path, SOCK_PATH); | ||||||
| 	socklen_t len = strlen(remote.sun_path) + sizeof(remote.sun_family); |     socklen_t len = strlen(remote.sun_path) + sizeof(remote.sun_family); | ||||||
| 
 | 
 | ||||||
| 	if (connect(fd, (struct sockaddr *)&remote, len) == -1) { |     if (connect(fd, (struct sockaddr *)&remote, len) == -1) { | ||||||
| 		printf("ERROR: failed to connect\n"); |         printf("ERROR: failed to connect\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	return fd; |     return fd; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| int main(int argc, const char* argv[]) { | int main(int argc, const char* argv[]) { | ||||||
| 	size_t buf_size, total_bytes; |     size_t buf_size, total_bytes; | ||||||
| 	if (argc >= 2) { |     if (argc >= 2) { | ||||||
| 		buf_size = atol(argv[1]); |         buf_size = atol(argv[1]); | ||||||
| 	} else { |     } else { | ||||||
| 		buf_size = BUF_SIZE; |         buf_size = BUF_SIZE; | ||||||
| 	} |     } | ||||||
| 	if (argc >= 3) { |     if (argc >= 3) { | ||||||
| 		total_bytes = atol(argv[2]); |         total_bytes = atol(argv[2]); | ||||||
| 	} else { |     } else { | ||||||
| 		// BUG: throughput fall down when buf_size > 65536
 |         // BUG: throughput fall down when buf_size > 65536
 | ||||||
| 		total_bytes = buf_size > 65536? buf_size << 15: buf_size << 21; |         total_bytes = buf_size > 65536? buf_size << 15: buf_size << 21; | ||||||
| 	} |     } | ||||||
| 	printf("buf_size = 0x%zx\n", buf_size); |     printf("buf_size = 0x%zx\n", buf_size); | ||||||
| 	printf("total_bytes = 0x%zx\n", total_bytes); |     printf("total_bytes = 0x%zx\n", total_bytes); | ||||||
| 
 | 
 | ||||||
| 	int listen_fd = create_server_socket(); |     int listen_fd = create_server_socket(); | ||||||
| 	if (listen_fd == -1) { |     if (listen_fd == -1) { | ||||||
| 		printf("ERROR: failed to create server socket\n"); |         printf("ERROR: failed to create server socket\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	int socket_rd_fd = create_client_socket(); |     int socket_rd_fd = create_client_socket(); | ||||||
| 	if (socket_rd_fd == -1) { |     if (socket_rd_fd == -1) { | ||||||
| 		printf("ERROR: failed to create client socket\n"); |         printf("ERROR: failed to create client socket\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	struct sockaddr_un remote; |     struct sockaddr_un remote; | ||||||
| 	socklen_t len = sizeof(remote); |     socklen_t len = sizeof(remote); | ||||||
| 	int socket_wr_fd = accept(listen_fd, (struct sockaddr *)&remote, &len); |     int socket_wr_fd = accept(listen_fd, (struct sockaddr *)&remote, &len); | ||||||
| 	if (socket_wr_fd == -1) { |     if (socket_wr_fd == -1) { | ||||||
| 		printf("ERROR: failed to accept socket\n"); |         printf("ERROR: failed to accept socket\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// The following is same as 'pipe_throughput'
 |     // The following is same as 'pipe_throughput'
 | ||||||
| 
 | 
 | ||||||
| 	// Spawn a child process that reads from the pipe
 |     // Spawn a child process that reads from the pipe
 | ||||||
| 	posix_spawn_file_actions_t file_actions; |     posix_spawn_file_actions_t file_actions; | ||||||
| 	posix_spawn_file_actions_init(&file_actions); |     posix_spawn_file_actions_init(&file_actions); | ||||||
| 	posix_spawn_file_actions_adddup2(&file_actions, socket_rd_fd, STDIN_FILENO); |     posix_spawn_file_actions_adddup2(&file_actions, socket_rd_fd, STDIN_FILENO); | ||||||
| 	posix_spawn_file_actions_addclose(&file_actions, socket_wr_fd); |     posix_spawn_file_actions_addclose(&file_actions, socket_wr_fd); | ||||||
| 
 | 
 | ||||||
| 	int child_pid; |     int child_pid; | ||||||
| 	extern char ** environ; |     extern char ** environ; | ||||||
| 	char* new_argv[] = {"/bin/data_sink", NULL}; |     char* new_argv[] = {"/bin/data_sink", NULL}; | ||||||
| 	if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions, |     if (posix_spawn(&child_pid, "/bin/data_sink", &file_actions, | ||||||
| 					NULL, new_argv, environ) < 0) { |                     NULL, new_argv, environ) < 0) { | ||||||
| 		printf("ERROR: failed to spawn a child process\n"); |         printf("ERROR: failed to spawn a child process\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 	close(socket_rd_fd); |     close(socket_rd_fd); | ||||||
| 
 | 
 | ||||||
| 	// Start the timer
 |     // Start the timer
 | ||||||
| 	struct timeval tv_start, tv_end; |     struct timeval tv_start, tv_end; | ||||||
| 	gettimeofday(&tv_start, NULL); |     gettimeofday(&tv_start, NULL); | ||||||
| 
 | 
 | ||||||
| 	// Tell the reader how many data are to be transfered
 |     // Tell the reader how many data are to be transfered
 | ||||||
| 	size_t remain_bytes = total_bytes; |     size_t remain_bytes = total_bytes; | ||||||
| 	if (write(socket_wr_fd, &remain_bytes, sizeof(remain_bytes)) != sizeof(remain_bytes)) { |     if (write(socket_wr_fd, &remain_bytes, sizeof(remain_bytes)) != sizeof(remain_bytes)) { | ||||||
| 		printf("ERROR: failed to write to pipe\n"); |         printf("ERROR: failed to write to pipe\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// Tell the reader the buffer size that it should use
 |     // Tell the reader the buffer size that it should use
 | ||||||
| 	if (write(socket_wr_fd, &buf_size, sizeof(buf_size)) != sizeof(buf_size)) { |     if (write(socket_wr_fd, &buf_size, sizeof(buf_size)) != sizeof(buf_size)) { | ||||||
| 		printf("ERROR: failed to write to pipe\n"); |         printf("ERROR: failed to write to pipe\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// Write a specified amount of data in a buffer of specified size
 |     // Write a specified amount of data in a buffer of specified size
 | ||||||
| 	char buf[BUF_SIZE] = {0}; |     char buf[BUF_SIZE] = {0}; | ||||||
| 	while (remain_bytes > 0) { |     while (remain_bytes > 0) { | ||||||
| 		size_t len = MIN(buf_size, remain_bytes); |         size_t len = MIN(buf_size, remain_bytes); | ||||||
| 		if ((len = write(socket_wr_fd, &buf, len)) < 0) { |         if ((len = write(socket_wr_fd, &buf, len)) < 0) { | ||||||
| 			printf("ERROR: failed to write to pipe\n"); |             printf("ERROR: failed to write to pipe\n"); | ||||||
| 			return -1; |             return -1; | ||||||
| 		} |         } | ||||||
| 		remain_bytes -= len; |         remain_bytes -= len; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// Wait for the child process to read all data and exit
 |     // Wait for the child process to read all data and exit
 | ||||||
| 	int status = 0; |     int status = 0; | ||||||
| 	if (wait4(child_pid, &status, 0, NULL) < 0) { |     if (wait4(child_pid, &status, 0, NULL) < 0) { | ||||||
| 		printf("ERROR: failed to wait4 the child process\n"); |         printf("ERROR: failed to wait4 the child process\n"); | ||||||
| 		return -1; |         return -1; | ||||||
| 	} |     } | ||||||
| 
 | 
 | ||||||
| 	// Stop the timer
 |     // Stop the timer
 | ||||||
| 	gettimeofday(&tv_end, NULL); |     gettimeofday(&tv_end, NULL); | ||||||
| 
 | 
 | ||||||
| 	// Calculate the throughput
 |     // Calculate the throughput
 | ||||||
| 	double total_s = (tv_end.tv_sec - tv_start.tv_sec) |     double total_s = (tv_end.tv_sec - tv_start.tv_sec) | ||||||
| 					 + (double)(tv_end.tv_usec - tv_start.tv_usec) / 1000000; |                      + (double)(tv_end.tv_usec - tv_start.tv_usec) / 1000000; | ||||||
| 	if (total_s < 1.0) { |     if (total_s < 1.0) { | ||||||
| 		printf("WARNING: run long enough to get meaningful results\n"); |         printf("WARNING: run long enough to get meaningful results\n"); | ||||||
| 		if (total_s == 0) { return 0; } |         if (total_s == 0) { return 0; } | ||||||
| 	} |     } | ||||||
| 	double total_mb = (double)total_bytes / MB; |     double total_mb = (double)total_bytes / MB; | ||||||
| 	double throughput = total_mb / total_s; |     double throughput = total_mb / total_s; | ||||||
| 	printf("Throughput of unix socket is %.2f MB/s\n", throughput); |     printf("Throughput of unix socket is %.2f MB/s\n", throughput); | ||||||
| 	return 0; |     return 0; | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user