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" | |
b66570dc | 19 | #include "flist.h" |
132159a5 | 20 | |
b66570dc JA |
21 | struct fio_client { |
22 | struct flist_head list; | |
23 | struct sockaddr_in addr; | |
24 | char *hostname; | |
25 | int fd; | |
26 | }; | |
27 | ||
28 | static FLIST_HEAD(client_list); | |
b66570dc JA |
29 | |
30 | static struct fio_client *find_client_by_fd(int fd) | |
31 | { | |
32 | struct fio_client *client; | |
33 | struct flist_head *entry; | |
34 | ||
35 | flist_for_each(entry, &client_list) { | |
36 | client = flist_entry(entry, struct fio_client, list); | |
37 | ||
38 | if (client->fd == fd) | |
39 | return client; | |
40 | } | |
41 | ||
42 | return NULL; | |
43 | } | |
44 | ||
a37f69b7 | 45 | #if 0 |
b66570dc JA |
46 | static struct fio_client *find_client_by_name(const char *name) |
47 | { | |
48 | struct fio_client *client; | |
49 | struct flist_head *entry; | |
50 | ||
51 | flist_for_each(entry, &client_list) { | |
52 | client = flist_entry(entry, struct fio_client, list); | |
53 | ||
54 | if (!strcmp(name, client->hostname)) | |
55 | return client; | |
56 | } | |
57 | ||
58 | return NULL; | |
59 | } | |
a37f69b7 | 60 | #endif |
b66570dc JA |
61 | |
62 | static void remove_client(struct fio_client *client) | |
63 | { | |
64 | printf("remove client %s\n", client->hostname); | |
65 | flist_del(&client->list); | |
66 | nr_clients--; | |
67 | free(client->hostname); | |
68 | free(client); | |
69 | } | |
132159a5 | 70 | |
a37f69b7 | 71 | void fio_client_add(const char *hostname) |
132159a5 | 72 | { |
b66570dc | 73 | struct fio_client *client; |
132159a5 | 74 | |
b66570dc | 75 | client = malloc(sizeof(*client)); |
a37f69b7 JA |
76 | memset(client, 0, sizeof(*client)); |
77 | client->hostname = strdup(hostname); | |
78 | client->fd = -1; | |
79 | flist_add(&client->list, &client_list); | |
80 | nr_clients++; | |
81 | } | |
82 | ||
83 | static int fio_client_connect(struct fio_client *client) | |
84 | { | |
85 | int fd; | |
132159a5 | 86 | |
b66570dc JA |
87 | memset(&client->addr, 0, sizeof(client->addr)); |
88 | client->addr.sin_family = AF_INET; | |
89 | client->addr.sin_port = htons(fio_net_port); | |
90 | ||
a37f69b7 | 91 | if (inet_aton(client->hostname, &client->addr.sin_addr) != 1) { |
132159a5 JA |
92 | struct hostent *hent; |
93 | ||
a37f69b7 | 94 | hent = gethostbyname(client->hostname); |
132159a5 JA |
95 | if (!hent) { |
96 | log_err("fio: gethostbyname: %s\n", strerror(errno)); | |
97 | return 1; | |
98 | } | |
99 | ||
b66570dc | 100 | memcpy(&client->addr.sin_addr, hent->h_addr, 4); |
132159a5 JA |
101 | } |
102 | ||
103 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
104 | if (fd < 0) { | |
105 | log_err("fio: socket: %s\n", strerror(errno)); | |
b66570dc | 106 | free(client); |
132159a5 JA |
107 | return 1; |
108 | } | |
109 | ||
b66570dc | 110 | if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) { |
132159a5 | 111 | log_err("fio: connect: %s\n", strerror(errno)); |
b66570dc | 112 | free(client); |
132159a5 JA |
113 | return 1; |
114 | } | |
115 | ||
b66570dc | 116 | client->fd = fd; |
132159a5 JA |
117 | return 0; |
118 | } | |
119 | ||
a37f69b7 JA |
120 | int fio_clients_connect(void) |
121 | { | |
122 | struct fio_client *client; | |
123 | struct flist_head *entry, *tmp; | |
124 | int ret; | |
125 | ||
126 | flist_for_each_safe(entry, tmp, &client_list) { | |
127 | client = flist_entry(entry, struct fio_client, list); | |
128 | ||
129 | ret = fio_client_connect(client); | |
130 | if (ret) | |
131 | remove_client(client); | |
132 | } | |
133 | ||
134 | return !nr_clients; | |
135 | } | |
136 | ||
b66570dc | 137 | static int send_file_buf(struct fio_client *client, char *buf, off_t size) |
132159a5 | 138 | { |
b66570dc | 139 | return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size); |
132159a5 JA |
140 | } |
141 | ||
142 | /* | |
143 | * Send file contents to server backend. We could use sendfile(), but to remain | |
144 | * more portable lets just read/write the darn thing. | |
145 | */ | |
a37f69b7 | 146 | static int fio_client_send_ini(struct fio_client *client, const char *filename) |
132159a5 JA |
147 | { |
148 | struct stat sb; | |
149 | char *p, *buf; | |
150 | off_t len; | |
151 | int fd, ret; | |
152 | ||
153 | fd = open(filename, O_RDONLY); | |
154 | if (fd < 0) { | |
155 | log_err("fio: job file open: %s\n", strerror(errno)); | |
156 | return 1; | |
157 | } | |
158 | ||
159 | if (fstat(fd, &sb) < 0) { | |
160 | log_err("fio: job file stat: %s\n", strerror(errno)); | |
161 | return 1; | |
162 | } | |
163 | ||
164 | buf = malloc(sb.st_size); | |
165 | ||
166 | len = sb.st_size; | |
167 | p = buf; | |
168 | do { | |
169 | ret = read(fd, p, len); | |
170 | if (ret > 0) { | |
171 | len -= ret; | |
172 | if (!len) | |
173 | break; | |
174 | p += ret; | |
175 | continue; | |
176 | } else if (!ret) | |
177 | break; | |
178 | else if (errno == EAGAIN || errno == EINTR) | |
179 | continue; | |
180 | } while (1); | |
181 | ||
b66570dc | 182 | ret = send_file_buf(client, buf, sb.st_size); |
132159a5 JA |
183 | free(buf); |
184 | return ret; | |
185 | } | |
37db14fe | 186 | |
a37f69b7 JA |
187 | int fio_clients_send_ini(const char *filename) |
188 | { | |
189 | struct fio_client *client; | |
190 | struct flist_head *entry, *tmp; | |
191 | ||
192 | flist_for_each_safe(entry, tmp, &client_list) { | |
193 | client = flist_entry(entry, struct fio_client, list); | |
194 | ||
195 | if (fio_client_send_ini(client, filename)) | |
196 | remove_client(client); | |
197 | } | |
198 | ||
199 | return !nr_clients; | |
200 | } | |
201 | ||
b66570dc | 202 | static int handle_client(struct fio_client *client) |
37db14fe JA |
203 | { |
204 | struct fio_net_cmd *cmd; | |
205 | ||
b66570dc | 206 | while ((cmd = fio_net_cmd_read(client->fd)) != NULL) { |
37db14fe JA |
207 | if (cmd->opcode == FIO_NET_CMD_ACK) { |
208 | free(cmd); | |
209 | continue; | |
210 | } | |
437377e1 | 211 | if (cmd->opcode == FIO_NET_CMD_QUIT) { |
b66570dc | 212 | remove_client(client); |
437377e1 JA |
213 | free(cmd); |
214 | break; | |
215 | } | |
37db14fe JA |
216 | if (cmd->opcode != FIO_NET_CMD_TEXT) { |
217 | printf("non text: %d\n", cmd->opcode); | |
218 | free(cmd); | |
219 | continue; | |
220 | } | |
49d14c0f | 221 | fwrite(cmd->payload, cmd->pdu_len, 1, stdout); |
37db14fe JA |
222 | fflush(stdout); |
223 | free(cmd); | |
224 | } | |
225 | ||
226 | return 0; | |
227 | } | |
b66570dc JA |
228 | |
229 | int fio_handle_clients(void) | |
230 | { | |
231 | struct fio_client *client; | |
232 | struct flist_head *entry; | |
233 | struct pollfd *pfds; | |
234 | int i = 0, ret = 0; | |
235 | ||
236 | pfds = malloc(nr_clients * sizeof(struct pollfd)); | |
237 | ||
238 | flist_for_each(entry, &client_list) { | |
239 | client = flist_entry(entry, struct fio_client, list); | |
240 | ||
241 | pfds[i].fd = client->fd; | |
242 | pfds[i].events = POLLIN; | |
243 | i++; | |
244 | } | |
245 | ||
246 | while (!exit_backend && nr_clients) { | |
247 | ret = poll(pfds, nr_clients, 100); | |
248 | if (ret < 0) { | |
249 | if (errno == EINTR) | |
250 | continue; | |
251 | log_err("fio: poll clients: %s\n", strerror(errno)); | |
252 | break; | |
253 | } else if (!ret) | |
254 | continue; | |
255 | ||
256 | for (i = 0; i < nr_clients; i++) { | |
257 | if (!(pfds[i].revents & POLLIN)) | |
258 | continue; | |
259 | ||
260 | client = find_client_by_fd(pfds[i].fd); | |
261 | if (!client) { | |
262 | log_err("fio: unknown client\n"); | |
263 | continue; | |
264 | } | |
265 | handle_client(client); | |
266 | } | |
267 | } | |
268 | ||
269 | free(pfds); | |
270 | ||
271 | return 0; | |
272 | } |