Commit | Line | Data |
---|---|---|
132159a5 JA |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <unistd.h> | |
4 | #include <limits.h> | |
5 | #include <errno.h> | |
6 | #include <fcntl.h> | |
7 | #include <sys/poll.h> | |
8 | #include <sys/types.h> | |
9 | #include <sys/stat.h> | |
10 | #include <sys/wait.h> | |
11 | #include <sys/mman.h> | |
12 | #include <netinet/in.h> | |
13 | #include <arpa/inet.h> | |
14 | #include <netdb.h> | |
15 | ||
16 | #include "fio.h" | |
17 | #include "server.h" | |
18 | #include "crc/crc32.h" | |
19 | ||
20 | int fio_client_fd = -1; | |
21 | ||
22 | int fio_client_connect(const char *host) | |
23 | { | |
24 | struct sockaddr_in addr; | |
25 | int fd; | |
26 | ||
27 | memset(&addr, 0, sizeof(addr)); | |
28 | addr.sin_family = AF_INET; | |
29 | addr.sin_port = htons(fio_net_port); | |
30 | ||
31 | if (inet_aton(host, &addr.sin_addr) != 1) { | |
32 | struct hostent *hent; | |
33 | ||
34 | hent = gethostbyname(host); | |
35 | if (!hent) { | |
36 | log_err("fio: gethostbyname: %s\n", strerror(errno)); | |
37 | return 1; | |
38 | } | |
39 | ||
40 | memcpy(&addr.sin_addr, hent->h_addr, 4); | |
41 | } | |
42 | ||
43 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
44 | if (fd < 0) { | |
45 | log_err("fio: socket: %s\n", strerror(errno)); | |
46 | return 1; | |
47 | } | |
48 | ||
49 | if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { | |
50 | log_err("fio: connect: %s\n", strerror(errno)); | |
51 | return 1; | |
52 | } | |
53 | ||
54 | fio_client_fd = fd; | |
55 | return 0; | |
56 | } | |
57 | ||
58 | static int send_file_buf(char *buf, off_t size) | |
59 | { | |
60 | struct fio_net_cmd *cmd; | |
61 | int ret; | |
62 | ||
63 | cmd = malloc(sizeof(*cmd) + size); | |
64 | ||
65 | fio_init_net_cmd(cmd); | |
66 | cmd->opcode = cpu_to_le16(FIO_NET_CMD_JOB_END); | |
67 | cmd->pdu_len = cpu_to_le32(size); | |
68 | ||
69 | memcpy(&cmd->payload, buf, size); | |
70 | ||
71 | fio_net_cmd_crc(cmd); | |
72 | ||
73 | printf("cmd crc %x, pdu crc %x\n", cmd->cmd_crc32, cmd->pdu_crc32); | |
74 | printf("job %x\n", cmd->opcode); | |
75 | ||
76 | ret = fio_send_data(fio_client_fd, cmd, sizeof(*cmd) + size); | |
77 | free(cmd); | |
78 | return ret; | |
79 | } | |
80 | ||
81 | /* | |
82 | * Send file contents to server backend. We could use sendfile(), but to remain | |
83 | * more portable lets just read/write the darn thing. | |
84 | */ | |
85 | int fio_client_send_ini(const char *filename) | |
86 | { | |
87 | struct stat sb; | |
88 | char *p, *buf; | |
89 | off_t len; | |
90 | int fd, ret; | |
91 | ||
92 | fd = open(filename, O_RDONLY); | |
93 | if (fd < 0) { | |
94 | log_err("fio: job file open: %s\n", strerror(errno)); | |
95 | return 1; | |
96 | } | |
97 | ||
98 | if (fstat(fd, &sb) < 0) { | |
99 | log_err("fio: job file stat: %s\n", strerror(errno)); | |
100 | return 1; | |
101 | } | |
102 | ||
103 | buf = malloc(sb.st_size); | |
104 | ||
105 | len = sb.st_size; | |
106 | p = buf; | |
107 | do { | |
108 | ret = read(fd, p, len); | |
109 | if (ret > 0) { | |
110 | len -= ret; | |
111 | if (!len) | |
112 | break; | |
113 | p += ret; | |
114 | continue; | |
115 | } else if (!ret) | |
116 | break; | |
117 | else if (errno == EAGAIN || errno == EINTR) | |
118 | continue; | |
119 | } while (1); | |
120 | ||
121 | ret = send_file_buf(buf, sb.st_size); | |
122 | free(buf); | |
123 | return ret; | |
124 | } |