12 #include <netinet/in.h>
13 #include <arpa/inet.h>
18 #include "crc/crc32.h"
22 struct flist_head list;
23 struct sockaddr_in addr;
28 static FLIST_HEAD(client_list);
29 static unsigned int nr_clients;
31 static struct fio_client *find_client_by_fd(int fd)
33 struct fio_client *client;
34 struct flist_head *entry;
36 flist_for_each(entry, &client_list) {
37 client = flist_entry(entry, struct fio_client, list);
46 static struct fio_client *find_client_by_name(const char *name)
48 struct fio_client *client;
49 struct flist_head *entry;
51 flist_for_each(entry, &client_list) {
52 client = flist_entry(entry, struct fio_client, list);
54 if (!strcmp(name, client->hostname))
61 static void remove_client(struct fio_client *client)
63 printf("remove client %s\n", client->hostname);
64 flist_del(&client->list);
66 free(client->hostname);
70 int fio_client_connect(const char *host)
72 struct fio_client *client;
75 client = malloc(sizeof(*client));
77 memset(&client->addr, 0, sizeof(client->addr));
78 client->addr.sin_family = AF_INET;
79 client->addr.sin_port = htons(fio_net_port);
81 if (inet_aton(host, &client->addr.sin_addr) != 1) {
84 hent = gethostbyname(host);
86 log_err("fio: gethostbyname: %s\n", strerror(errno));
90 memcpy(&client->addr.sin_addr, hent->h_addr, 4);
93 fd = socket(AF_INET, SOCK_STREAM, 0);
95 log_err("fio: socket: %s\n", strerror(errno));
100 if (connect(fd, (struct sockaddr *) &client->addr, sizeof(client->addr)) < 0) {
101 log_err("fio: connect: %s\n", strerror(errno));
106 client->hostname = strdup(host);
107 flist_add(&client->list, &client_list);
113 static int send_file_buf(struct fio_client *client, char *buf, off_t size)
115 return fio_net_send_cmd(client->fd, FIO_NET_CMD_JOB, buf, size);
119 * Send file contents to server backend. We could use sendfile(), but to remain
120 * more portable lets just read/write the darn thing.
122 int fio_client_send_ini(const char *hostname, const char *filename)
124 struct fio_client *client;
130 client = find_client_by_name(hostname);
132 log_err("fio: can't find client for host %s\n", hostname);
136 fd = open(filename, O_RDONLY);
138 log_err("fio: job file open: %s\n", strerror(errno));
142 if (fstat(fd, &sb) < 0) {
143 log_err("fio: job file stat: %s\n", strerror(errno));
147 buf = malloc(sb.st_size);
152 ret = read(fd, p, len);
161 else if (errno == EAGAIN || errno == EINTR)
165 ret = send_file_buf(client, buf, sb.st_size);
170 static int handle_client(struct fio_client *client)
172 struct fio_net_cmd *cmd;
174 while ((cmd = fio_net_cmd_read(client->fd)) != NULL) {
175 if (cmd->opcode == FIO_NET_CMD_ACK) {
179 if (cmd->opcode == FIO_NET_CMD_QUIT) {
180 remove_client(client);
184 if (cmd->opcode != FIO_NET_CMD_TEXT) {
185 printf("non text: %d\n", cmd->opcode);
189 fwrite(cmd->payload, cmd->pdu_len, 1, stdout);
197 int fio_handle_clients(void)
199 struct fio_client *client;
200 struct flist_head *entry;
204 pfds = malloc(nr_clients * sizeof(struct pollfd));
206 flist_for_each(entry, &client_list) {
207 client = flist_entry(entry, struct fio_client, list);
209 pfds[i].fd = client->fd;
210 pfds[i].events = POLLIN;
214 while (!exit_backend && nr_clients) {
215 ret = poll(pfds, nr_clients, 100);
219 log_err("fio: poll clients: %s\n", strerror(errno));
224 for (i = 0; i < nr_clients; i++) {
225 if (!(pfds[i].revents & POLLIN))
228 client = find_client_by_fd(pfds[i].fd);
230 log_err("fio: unknown client\n");
233 handle_client(client);