11 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
22 #include "crc/crc16.h"
23 #include "lib/ieee754.h"
25 #include "fio_version.h"
27 int fio_net_port = 8765;
31 static int server_fd = -1;
32 static char *fio_server_arg;
33 static char *bind_sock;
34 static struct sockaddr_in saddr_in;
35 static struct sockaddr_in6 saddr_in6;
36 static int first_cmd_check;
39 static const char *fio_server_ops[FIO_NET_CMD_NR] = {
57 const char *fio_server_op(unsigned int op)
61 if (op < FIO_NET_CMD_NR)
62 return fio_server_ops[op];
64 sprintf(buf, "UNKNOWN/%d", op);
68 int fio_send_data(int sk, const void *p, unsigned int len)
70 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
73 int ret = send(sk, p, len, 0);
83 else if (errno == EAGAIN || errno == EINTR)
87 } while (!exit_backend);
95 int fio_recv_data(int sk, void *p, unsigned int len)
98 int ret = recv(sk, p, len, MSG_WAITALL);
108 else if (errno == EAGAIN || errno == EINTR)
112 } while (!exit_backend);
120 static int verify_convert_cmd(struct fio_net_cmd *cmd)
124 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
125 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
127 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
128 if (crc != cmd->cmd_crc16) {
129 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
130 cmd->cmd_crc16, crc);
134 cmd->version = le16_to_cpu(cmd->version);
135 cmd->opcode = le16_to_cpu(cmd->opcode);
136 cmd->flags = le32_to_cpu(cmd->flags);
137 cmd->tag = le64_to_cpu(cmd->tag);
138 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
140 switch (cmd->version) {
144 log_err("fio: bad server cmd version %d\n", cmd->version);
148 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
149 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
157 * Read (and defragment, if necessary) incoming commands
159 struct fio_net_cmd *fio_net_recv_cmd(int sk)
161 struct fio_net_cmd cmd, *cmdret = NULL;
162 size_t cmd_size = 0, pdu_offset = 0;
168 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
172 /* We have a command, verify it and swap if need be */
173 ret = verify_convert_cmd(&cmd);
178 /* if this is text, add room for \0 at the end */
179 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
182 cmd_size += cmd.pdu_len;
184 cmdret = realloc(cmdret, cmd_size);
187 memcpy(cmdret, &cmd, sizeof(cmd));
188 else if (cmdret->opcode != cmd.opcode) {
189 log_err("fio: fragment opcode mismatch (%d != %d)\n",
190 cmdret->opcode, cmd.opcode);
198 /* There's payload, get it */
199 pdu = (void *) cmdret->payload + pdu_offset;
200 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
204 /* Verify payload crc */
205 crc = crc16(pdu, cmd.pdu_len);
206 if (crc != cmd.pdu_crc16) {
207 log_err("fio: server bad crc on payload ");
208 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
213 pdu_offset += cmd.pdu_len;
215 cmdret->pdu_len += cmd.pdu_len;
217 } while (cmd.flags & FIO_NET_CMD_F_MORE);
223 /* zero-terminate text input */
224 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
225 cmdret->opcode == FIO_NET_CMD_JOB)) {
226 char *buf = (char *) cmdret->payload;
228 buf[cmdret->pdu_len ] = '\0';
230 /* frag flag is internal */
231 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
237 void fio_net_cmd_crc(struct fio_net_cmd *cmd)
241 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
243 pdu_len = le32_to_cpu(cmd->pdu_len);
245 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
248 int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
251 struct fio_net_cmd *cmd = NULL;
252 size_t this_len, cur_len = 0;
257 if (this_len > FIO_SERVER_MAX_PDU)
258 this_len = FIO_SERVER_MAX_PDU;
260 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
264 cur_len = sizeof(*cmd) + this_len;
265 cmd = malloc(cur_len);
268 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
271 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
273 fio_net_cmd_crc(cmd);
275 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
278 } while (!ret && size);
286 static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
288 struct fio_net_cmd cmd;
290 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
291 fio_net_cmd_crc(&cmd);
293 return fio_send_data(sk, &cmd, sizeof(cmd));
297 * If 'list' is non-NULL, then allocate and store the sent command for
298 * later verification.
300 int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
301 struct flist_head *list)
303 struct fio_net_int_cmd *cmd;
307 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
309 cmd = malloc(sizeof(*cmd));
311 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
312 fio_net_cmd_crc(&cmd->cmd);
314 INIT_FLIST_HEAD(&cmd->list);
315 gettimeofday(&cmd->tv, NULL);
316 cmd->saved_tag = tag;
318 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
324 flist_add_tail(&cmd->list, list);
328 static int fio_server_send_quit_cmd(void)
330 dprint(FD_NET, "server: sending quit\n");
331 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
334 static int handle_job_cmd(struct fio_net_cmd *cmd)
336 char *buf = (char *) cmd->payload;
337 struct cmd_start_pdu spdu;
338 struct cmd_end_pdu epdu;
341 if (parse_jobs_ini(buf, 1, 0)) {
342 fio_server_send_quit_cmd();
346 spdu.jobs = cpu_to_le32(thread_number);
347 fio_net_send_cmd(server_fd, FIO_NET_CMD_START, &spdu, sizeof(spdu), 0);
352 fio_net_send_cmd(server_fd, FIO_NET_CMD_STOP, &epdu, sizeof(epdu), 0);
354 fio_server_send_quit_cmd();
359 static int handle_jobline_cmd(struct fio_net_cmd *cmd)
361 void *pdu = cmd->payload;
362 struct cmd_single_line_pdu *cslp;
363 struct cmd_line_pdu *clp;
364 unsigned long offset;
369 clp->lines = le16_to_cpu(clp->lines);
370 argv = malloc(clp->lines * sizeof(char *));
371 offset = sizeof(*clp);
373 dprint(FD_NET, "server: %d command line args\n", clp->lines);
375 for (i = 0; i < clp->lines; i++) {
377 argv[i] = (char *) cslp->text;
379 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
380 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
383 if (parse_cmd_line(clp->lines, argv)) {
384 fio_server_send_quit_cmd();
391 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
394 fio_server_send_quit_cmd();
399 static int handle_probe_cmd(struct fio_net_cmd *cmd)
401 struct cmd_probe_pdu probe;
403 dprint(FD_NET, "server: sending probe reply\n");
405 memset(&probe, 0, sizeof(probe));
406 gethostname((char *) probe.hostname, sizeof(probe.hostname));
407 #ifdef FIO_BIG_ENDIAN
410 probe.fio_major = FIO_MAJOR;
411 probe.fio_minor = FIO_MINOR;
412 probe.fio_patch = FIO_PATCH;
415 probe.arch = FIO_ARCH;
417 probe.bpp = sizeof(void *);
419 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
422 static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
431 size = sizeof(*je) + thread_number * sizeof(char) + 1;
435 if (!calc_thread_status(je, 1)) {
440 dprint(FD_NET, "server sending status\n");
442 je->nr_running = cpu_to_le32(je->nr_running);
443 je->nr_ramp = cpu_to_le32(je->nr_ramp);
444 je->nr_pending = cpu_to_le32(je->nr_pending);
445 je->files_open = cpu_to_le32(je->files_open);
446 je->m_rate = cpu_to_le32(je->m_rate);
447 je->t_rate = cpu_to_le32(je->t_rate);
448 je->m_iops = cpu_to_le32(je->m_iops);
449 je->t_iops = cpu_to_le32(je->t_iops);
451 for (i = 0; i < 2; i++) {
452 je->rate[i] = cpu_to_le32(je->rate[i]);
453 je->iops[i] = cpu_to_le32(je->iops[i]);
456 je->elapsed_sec = cpu_to_le32(je->nr_running);
457 je->eta_sec = cpu_to_le64(je->eta_sec);
459 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
464 static int handle_command(struct fio_net_cmd *cmd)
468 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
469 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
471 switch (cmd->opcode) {
472 case FIO_NET_CMD_QUIT:
473 fio_terminate_threads(TERMINATE_ALL);
475 case FIO_NET_CMD_EXIT:
478 case FIO_NET_CMD_JOB:
479 ret = handle_job_cmd(cmd);
481 case FIO_NET_CMD_JOBLINE:
482 ret = handle_jobline_cmd(cmd);
484 case FIO_NET_CMD_PROBE:
485 ret = handle_probe_cmd(cmd);
487 case FIO_NET_CMD_SEND_ETA:
488 ret = handle_send_eta_cmd(cmd);
491 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
498 static int handle_connection(int sk, int block)
500 struct fio_net_cmd *cmd = NULL;
504 while (!exit_backend) {
505 struct pollfd pfd = {
512 ret = poll(&pfd, 1, 100);
516 log_err("fio: poll: %s\n", strerror(errno));
524 if (pfd.revents & POLLIN)
526 if (pfd.revents & (POLLERR|POLLHUP)) {
530 } while (!exit_backend);
535 cmd = fio_net_recv_cmd(sk);
541 ret = handle_command(cmd);
555 void fio_server_idle_loop(void)
557 if (!first_cmd_check)
558 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_RUN, 0, NULL);
560 handle_connection(server_fd, 0);
563 static int accept_loop(int listen_sk)
565 struct sockaddr_in addr;
566 fio_socklen_t len = sizeof(addr);
568 int ret, sk, flags, exitval = 0;
570 dprint(FD_NET, "server enter accept loop\n");
572 flags = fcntl(listen_sk, F_GETFL);
574 fcntl(listen_sk, F_SETFL, flags);
579 ret = poll(&pfd, 1, 100);
583 log_err("fio: poll: %s\n", strerror(errno));
588 if (pfd.revents & POLLIN)
590 } while (!exit_backend);
595 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
597 log_err("fio: accept: %s\n", strerror(errno));
601 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
605 exitval = handle_connection(sk, 1);
617 int fio_server_text_output(const char *buf, size_t len)
620 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
622 return log_local_buf(buf, len);
625 static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
627 dst->max_val = cpu_to_le64(src->max_val);
628 dst->min_val = cpu_to_le64(src->min_val);
629 dst->samples = cpu_to_le64(src->samples);
632 * Encode to IEEE 754 for network transfer
634 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
635 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
638 static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
642 for (i = 0; i < 2; i++) {
643 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
644 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
645 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
646 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
647 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
648 dst->agg[i] = cpu_to_le64(src->agg[i]);
651 dst->kb_base = cpu_to_le32(src->kb_base);
652 dst->groupid = cpu_to_le32(src->groupid);
656 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
657 * into a single payload.
659 void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
664 dprint(FD_NET, "server sending end stats\n");
666 memset(&p, 0, sizeof(p));
668 strcpy(p.ts.name, ts->name);
669 strcpy(p.ts.verror, ts->verror);
670 strcpy(p.ts.description, ts->description);
672 p.ts.error = cpu_to_le32(ts->error);
673 p.ts.groupid = cpu_to_le32(ts->groupid);
674 p.ts.pid = cpu_to_le32(ts->pid);
675 p.ts.members = cpu_to_le32(ts->members);
677 for (i = 0; i < 2; i++) {
678 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
679 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
680 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
681 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
684 p.ts.usr_time = cpu_to_le64(ts->usr_time);
685 p.ts.sys_time = cpu_to_le64(ts->sys_time);
686 p.ts.ctx = cpu_to_le64(ts->ctx);
687 p.ts.minf = cpu_to_le64(ts->minf);
688 p.ts.majf = cpu_to_le64(ts->majf);
689 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
691 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
692 fio_fp64_t *src = &ts->percentile_list[i];
693 fio_fp64_t *dst = &p.ts.percentile_list[i];
695 dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f));
698 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
699 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
700 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
701 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
704 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
705 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
706 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
709 for (i = 0; i < 2; i++)
710 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
711 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
713 for (i = 0; i < 3; i++) {
714 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
715 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
718 p.ts.total_submit = cpu_to_le64(ts->total_submit);
719 p.ts.total_complete = cpu_to_le64(ts->total_complete);
721 for (i = 0; i < 2; i++) {
722 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
723 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
726 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
727 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
728 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
729 p.ts.first_error = cpu_to_le32(ts->first_error);
730 p.ts.kb_base = cpu_to_le32(ts->kb_base);
732 convert_gs(&p.rs, rs);
734 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
737 void fio_server_send_gs(struct group_run_stats *rs)
739 struct group_run_stats gs;
741 dprint(FD_NET, "server sending group run stats\n");
744 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
747 static void convert_agg(struct disk_util_agg *dst, struct disk_util_agg *src)
751 for (i = 0; i < 2; i++) {
752 dst->ios[i] = cpu_to_le32(src->ios[i]);
753 dst->merges[i] = cpu_to_le32(src->merges[i]);
754 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
755 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
758 dst->io_ticks = cpu_to_le32(src->io_ticks);
759 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
760 dst->slavecount = cpu_to_le32(src->slavecount);
761 dst->max_util.u.i = __cpu_to_le64(fio_double_to_uint64(src->max_util.u.f));
764 static void convert_dus(struct disk_util_stat *dst, struct disk_util_stat *src)
768 strcpy((char *) dst->name, (char *) src->name);
770 for (i = 0; i < 2; i++) {
771 dst->ios[i] = cpu_to_le32(src->ios[i]);
772 dst->merges[i] = cpu_to_le32(src->merges[i]);
773 dst->sectors[i] = cpu_to_le64(src->sectors[i]);
774 dst->ticks[i] = cpu_to_le32(src->ticks[i]);
777 dst->io_ticks = cpu_to_le32(src->io_ticks);
778 dst->time_in_queue = cpu_to_le32(src->time_in_queue);
779 dst->msec = cpu_to_le64(src->msec);
782 void fio_server_send_du(void)
784 struct disk_util *du;
785 struct flist_head *entry;
786 struct cmd_du_pdu pdu;
788 dprint(FD_NET, "server: sending disk_util %d\n", !flist_empty(&disk_list));
790 memset(&pdu, 0, sizeof(pdu));
792 flist_for_each(entry, &disk_list) {
793 du = flist_entry(entry, struct disk_util, list);
795 convert_dus(&pdu.dus, &du->dus);
796 convert_agg(&pdu.agg, &du->agg);
798 fio_net_send_cmd(server_fd, FIO_NET_CMD_DU, &pdu, sizeof(pdu), 0);
802 int fio_server_log(const char *format, ...)
808 dprint(FD_NET, "server log\n");
810 va_start(args, format);
811 len = vsnprintf(buffer, sizeof(buffer), format, args);
814 return fio_server_text_output(buffer, len);
817 static int fio_init_server_ip(void)
819 struct sockaddr *addr;
820 fio_socklen_t socklen;
824 sk = socket(AF_INET6, SOCK_STREAM, 0);
826 sk = socket(AF_INET, SOCK_STREAM, 0);
829 log_err("fio: socket: %s\n", strerror(errno));
834 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
835 log_err("fio: setsockopt: %s\n", strerror(errno));
840 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
841 log_err("fio: setsockopt: %s\n", strerror(errno));
848 addr = (struct sockaddr *) &saddr_in6;
849 socklen = sizeof(saddr_in6);
850 saddr_in6.sin6_family = AF_INET6;
852 addr = (struct sockaddr *) &saddr_in;
853 socklen = sizeof(saddr_in);
854 saddr_in.sin_family = AF_INET;
857 if (bind(sk, addr, socklen) < 0) {
858 log_err("fio: bind: %s\n", strerror(errno));
866 static int fio_init_server_sock(void)
868 struct sockaddr_un addr;
873 sk = socket(AF_UNIX, SOCK_STREAM, 0);
875 log_err("fio: socket: %s\n", strerror(errno));
881 memset(&addr, 0, sizeof(addr));
882 addr.sun_family = AF_UNIX;
883 strcpy(addr.sun_path, bind_sock);
886 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
888 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
889 log_err("fio: bind: %s\n", strerror(errno));
898 static int fio_init_server_connection(void)
903 dprint(FD_NET, "starting server\n");
906 sk = fio_init_server_ip();
908 sk = fio_init_server_sock();
920 src = &saddr_in6.sin6_addr;
923 src = &saddr_in.sin_addr;
926 p = (char *) inet_ntop(af, src, bind_str, sizeof(bind_str));
928 sprintf(port, ",%u", fio_net_port);
932 strcpy(bind_str, port);
934 strcpy(bind_str, bind_sock);
936 log_info("fio: server listening on %s\n", bind_str);
938 if (listen(sk, 0) < 0) {
939 log_err("fio: listen: %s\n", strerror(errno));
947 * Parse a host/ip/port string. Reads from 'str'.
952 * *ptr is the host, *port is the port, inp is the destination.
954 * *ptr is the host, *port is the port, inp6 is the dest, and *ipv6 is 1.
955 * For local domain sockets:
956 * *ptr is the filename, *is_sock is 1.
958 int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
959 int *port, struct in_addr *inp,
960 struct in6_addr *inp6, int *ipv6)
962 const char *host = str;
968 *port = fio_net_port;
971 if (!strncmp(str, "sock:", 5)) {
972 *ptr = strdup(str + 5);
979 * Is it ip:<ip or host>:port
981 if (!strncmp(host, "ip:", 3))
983 else if (!strncmp(host, "ip4:", 4))
985 else if (!strncmp(host, "ip6:", 4)) {
988 } else if (host[0] == ':') {
989 /* String is :port */
992 if (!lport || lport > 65535) {
993 log_err("fio: bad server port %u\n", port);
996 /* no hostname given, we are done */
1002 * If no port seen yet, check if there's a last ':' at the end
1005 portp = strchr(host, ',');
1009 lport = atoi(portp);
1010 if (!lport || lport > 65535) {
1011 log_err("fio: bad server port %u\n", port);
1023 *ptr = strdup(host);
1026 ret = inet_pton(AF_INET6, host, inp6);
1028 ret = inet_pton(AF_INET, host, inp);
1031 struct hostent *hent;
1033 hent = gethostbyname(host);
1035 log_err("fio: failed to resolve <%s>\n", host);
1042 if (hent->h_addrtype != AF_INET6) {
1043 log_info("fio: falling back to IPv4\n");
1046 memcpy(inp6, hent->h_addr_list[0], 16);
1049 if (hent->h_addrtype != AF_INET) {
1050 log_err("fio: lookup type mismatch\n");
1055 memcpy(inp, hent->h_addr_list[0], 4);
1060 *port = fio_net_port;
1066 * Server arg should be one of:
1068 * sock:/path/to/socket
1072 * Where sock uses unix domain sockets, and ip binds the server to
1073 * a specific interface. If no arguments are given to the server, it
1074 * uses IP and binds to 0.0.0.0.
1077 static int fio_handle_server_arg(void)
1079 int port = fio_net_port;
1080 int is_sock, ret = 0;
1082 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
1084 if (!fio_server_arg)
1087 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
1088 &port, &saddr_in.sin_addr,
1089 &saddr_in6.sin6_addr, &use_ipv6);
1091 if (!is_sock && bind_sock) {
1097 fio_net_port = port;
1098 saddr_in.sin_port = htons(port);
1099 saddr_in6.sin6_port = htons(port);
1103 static int fio_server(void)
1107 dprint(FD_NET, "starting server\n");
1109 if (fio_handle_server_arg())
1112 sk = fio_init_server_connection();
1116 ret = accept_loop(sk);
1120 if (fio_server_arg) {
1121 free(fio_server_arg);
1122 fio_server_arg = NULL;
1130 void fio_server_got_signal(int signal)
1132 if (signal == SIGPIPE)
1135 log_info("\nfio: terminating on signal %d\n", signal);
1140 static int check_existing_pidfile(const char *pidfile)
1147 if (stat(pidfile, &sb))
1150 f = fopen(pidfile, "r");
1154 if (fread(buf, sb.st_size, 1, f) <= 0) {
1161 if (kill(pid, SIGCONT) < 0)
1162 return errno != ESRCH;
1167 static int write_pid(pid_t pid, const char *pidfile)
1171 fpid = fopen(pidfile, "w");
1173 log_err("fio: failed opening pid file %s\n", pidfile);
1177 fprintf(fpid, "%u\n", (unsigned int) pid);
1183 * If pidfile is specified, background us.
1185 int fio_start_server(char *pidfile)
1191 return fio_server();
1193 if (check_existing_pidfile(pidfile)) {
1194 log_err("fio: pidfile %s exists and server appears alive\n",
1201 log_err("fio: failed server fork: %s", strerror(errno));
1205 int ret = write_pid(pid, pidfile);
1211 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1213 close(STDIN_FILENO);
1214 close(STDOUT_FILENO);
1215 close(STDERR_FILENO);
1227 void fio_server_set_arg(const char *arg)
1229 fio_server_arg = strdup(arg);