|
| 1 | +#define _GNU_SOURCE |
| 2 | +#include <stdio.h> |
| 3 | +#include <sys/socket.h> |
| 4 | +#include <linux/un.h> |
| 5 | +#include <sys/types.h> |
| 6 | +#include <sys/stat.h> |
| 7 | +#include <fcntl.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#include "../src/hyper.h" |
| 12 | +#include "../src/net.h" |
| 13 | + |
| 14 | +int test_sendmsg(int fd, unsigned int type, unsigned int len, char *message) |
| 15 | +{ |
| 16 | + uint8_t buf[4096]; |
| 17 | + |
| 18 | + |
| 19 | + /* send hyper info to guest */ |
| 20 | + hyper_set_be32(buf, type); |
| 21 | + |
| 22 | + len += 8; |
| 23 | + |
| 24 | + fprintf(stdout, "type is %u, len is %u\n", type, len); |
| 25 | + hyper_set_be32(buf + 4, len); |
| 26 | + |
| 27 | + if (message) |
| 28 | + memcpy(buf + 8, message, len - 8); |
| 29 | + |
| 30 | + if (write(fd, buf, len) != len) { |
| 31 | + fprintf(stderr, "send SETDVM MESSAGE failed\n"); |
| 32 | + return -1; |
| 33 | + } |
| 34 | + |
| 35 | + fprintf(stdout, "finish sending\n"); |
| 36 | + return 0; |
| 37 | +} |
| 38 | + |
| 39 | +int test_sendmsg_from_file(int fd, unsigned int type, char *file) |
| 40 | +{ |
| 41 | + int file_fd = open(file, O_RDONLY); |
| 42 | + uint8_t buf[4096]; |
| 43 | + unsigned int len = 0; |
| 44 | + |
| 45 | + if (file_fd < 0) { |
| 46 | + perror("fail to open file"); |
| 47 | + return -1; |
| 48 | + } |
| 49 | + |
| 50 | + while (len < 4096) { |
| 51 | + int size = read(file_fd, buf + len, sizeof(buf) - len); |
| 52 | + |
| 53 | + fprintf(stdout, "read %d data\n", size); |
| 54 | + if (size < 0) { |
| 55 | + perror("fail to read data"); |
| 56 | + return -1; |
| 57 | + } else if (size == 0) { |
| 58 | + /* buf[len] = '\0';*/ |
| 59 | + fprintf(stdout, "get buf %s, call sendmsg\n", buf); |
| 60 | + test_sendmsg(fd, type, len, (char *)buf); |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + len += size; |
| 65 | + } |
| 66 | + |
| 67 | + close(file_fd); |
| 68 | + |
| 69 | + if (read(fd, buf, 8) != 8) { |
| 70 | + fprintf(stderr, "read response failed\n"); |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | + type = hyper_get_be32(buf); |
| 75 | + if (type != ACK) { |
| 76 | + fprintf(stderr, "incorrect type %d\n", type); |
| 77 | + return -1; |
| 78 | + } |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +int main(int argc, char *argv[]) |
| 84 | +{ |
| 85 | + int sock, fd = -1; |
| 86 | + struct sockaddr_un addr; |
| 87 | + uint8_t buf[8]; |
| 88 | + unsigned int type; |
| 89 | + |
| 90 | + unlink("/tmp/hyper.sock"); |
| 91 | + |
| 92 | + sock = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); |
| 93 | + if (sock == -1) { |
| 94 | + perror("create unix socket failed"); |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + addr.sun_family = AF_UNIX; |
| 99 | + strncpy(addr.sun_path, "/tmp/hyper.sock", UNIX_PATH_MAX); |
| 100 | + addr.sun_path[UNIX_PATH_MAX - 1] = '\0'; |
| 101 | + |
| 102 | + if (bind(sock, ((struct sockaddr *) &addr), sizeof(addr)) == -1) { |
| 103 | + perror("bind failed"); |
| 104 | + goto out; |
| 105 | + } |
| 106 | + |
| 107 | + if (listen(sock, 1) == -1) { |
| 108 | + perror("bind failed"); |
| 109 | + goto out; |
| 110 | + } |
| 111 | + |
| 112 | + while (fd == -1) |
| 113 | + fd = accept4(sock, NULL, NULL, SOCK_CLOEXEC); |
| 114 | + |
| 115 | + fprintf(stdout, "connected\n"); |
| 116 | + if (read(fd, buf, 8) != 8) { |
| 117 | + fprintf(stderr, "read failed, buf %s\n", buf); |
| 118 | + goto out1; |
| 119 | + } |
| 120 | + |
| 121 | + type = hyper_get_be32(buf); |
| 122 | + fprintf(stdout, "get type %d\n", type); |
| 123 | + |
| 124 | + if (type != READY) { |
| 125 | + fprintf(stderr, "incorrect type %d\n", type); |
| 126 | + goto out1; |
| 127 | + } |
| 128 | + |
| 129 | + fprintf(stdout, "get length %d\n", hyper_get_be32(buf + 4)); |
| 130 | + |
| 131 | + /* test_sendmsg_from_file(fd, SETDVM, "sethyper.json"); */ |
| 132 | + if (test_sendmsg_from_file(fd, STARTPOD, "startpod.json") < 0) { |
| 133 | + fprintf(stderr, "send startpod message failed\n"); |
| 134 | + goto out1; |
| 135 | + } |
| 136 | + |
| 137 | + if (test_sendmsg_from_file(fd, EXECCMD, "execcmd.json") < 0) { |
| 138 | + fprintf(stderr, "send execcmd message failed\n"); |
| 139 | + goto out1; |
| 140 | + } |
| 141 | + |
| 142 | + if (test_sendmsg(fd, STOPPOD, 0, NULL) < 0) { |
| 143 | + fprintf(stderr, "send stoppod message failed\n"); |
| 144 | + goto out1; |
| 145 | + } |
| 146 | + |
| 147 | + if (read(fd, buf, 8) != 8) { |
| 148 | + fprintf(stderr, "read response failed\n"); |
| 149 | + return -1; |
| 150 | + } |
| 151 | + |
| 152 | + type = hyper_get_be32(buf); |
| 153 | + if (type != ACK) { |
| 154 | + fprintf(stderr, "incorrect type %d\n", type); |
| 155 | + return -1; |
| 156 | + } |
| 157 | + |
| 158 | + if (read(fd, buf, 8) != 8) { |
| 159 | + fprintf(stderr, "read response failed\n"); |
| 160 | + return -1; |
| 161 | + } |
| 162 | +out1: |
| 163 | + close(fd); |
| 164 | +out: |
| 165 | + close(sock); |
| 166 | + |
| 167 | + return 0; |
| 168 | +} |
0 commit comments