Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 3 | #include <stdarg.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <limits.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 6 | #include <errno.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/poll.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/wait.h> |
Jens Axboe | d05c4a0 | 2011-10-05 00:16:11 +0200 | [diff] [blame] | 11 | #include <sys/socket.h> |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 12 | #include <sys/stat.h> |
| 13 | #include <sys/un.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 14 | #include <netinet/in.h> |
| 15 | #include <arpa/inet.h> |
| 16 | #include <netdb.h> |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 17 | #include <syslog.h> |
Jens Axboe | 9e22ecb | 2011-10-04 14:50:21 +0200 | [diff] [blame] | 18 | #include <signal.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 19 | |
| 20 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 21 | #include "server.h" |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 22 | #include "crc/crc16.h" |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 23 | #include "ieee754.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 24 | |
Jens Axboe | 89cf148 | 2011-10-07 10:10:18 +0200 | [diff] [blame] | 25 | #include "fio_version.h" |
| 26 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 27 | int fio_net_port = 8765; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 28 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 29 | int exit_backend = 0; |
| 30 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 31 | static int server_fd = -1; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 32 | static char *fio_server_arg; |
| 33 | static char *bind_sock; |
| 34 | static struct sockaddr_in saddr_in; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 35 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 36 | static const char *fio_server_ops[FIO_NET_CMD_NR] = { |
| 37 | "", |
| 38 | "QUIT", |
| 39 | "EXIT", |
| 40 | "JOB", |
| 41 | "JOBLINE", |
| 42 | "TEXT", |
| 43 | "TS", |
| 44 | "GS", |
| 45 | "SEND_ETA", |
| 46 | "ETA", |
| 47 | "PROBE", |
| 48 | "START", |
| 49 | "STOP" |
| 50 | }; |
| 51 | |
| 52 | const char *fio_server_op(unsigned int op) |
| 53 | { |
| 54 | static char buf[32]; |
| 55 | |
| 56 | if (op < FIO_NET_CMD_NR) |
| 57 | return fio_server_ops[op]; |
| 58 | |
| 59 | sprintf(buf, "UNKNOWN/%d", op); |
| 60 | return buf; |
| 61 | } |
| 62 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 63 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 64 | { |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 65 | assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU); |
| 66 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 67 | do { |
| 68 | int ret = send(sk, p, len, 0); |
| 69 | |
| 70 | if (ret > 0) { |
| 71 | len -= ret; |
| 72 | if (!len) |
| 73 | break; |
| 74 | p += ret; |
| 75 | continue; |
| 76 | } else if (!ret) |
| 77 | break; |
| 78 | else if (errno == EAGAIN || errno == EINTR) |
| 79 | continue; |
| 80 | } while (!exit_backend); |
| 81 | |
| 82 | if (!len) |
| 83 | return 0; |
| 84 | |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 89 | { |
| 90 | do { |
| 91 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 92 | |
| 93 | if (ret > 0) { |
| 94 | len -= ret; |
| 95 | if (!len) |
| 96 | break; |
| 97 | p += ret; |
| 98 | continue; |
| 99 | } else if (!ret) |
| 100 | break; |
| 101 | else if (errno == EAGAIN || errno == EINTR) |
| 102 | continue; |
| 103 | } while (!exit_backend); |
| 104 | |
| 105 | if (!len) |
| 106 | return 0; |
| 107 | |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 112 | { |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 113 | uint16_t crc; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 114 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 115 | cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); |
| 116 | cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 117 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 118 | crc = crc16(cmd, FIO_NET_CMD_CRC_SZ); |
| 119 | if (crc != cmd->cmd_crc16) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 120 | log_err("fio: server bad crc on command (got %x, wanted %x)\n", |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 121 | cmd->cmd_crc16, crc); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | cmd->version = le16_to_cpu(cmd->version); |
| 126 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 127 | cmd->flags = le32_to_cpu(cmd->flags); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 128 | cmd->tag = le64_to_cpu(cmd->tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 129 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 130 | |
| 131 | switch (cmd->version) { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 132 | case FIO_SERVER_VER: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 133 | break; |
| 134 | default: |
| 135 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | if (cmd->pdu_len > FIO_SERVER_MAX_PDU) { |
| 140 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 147 | /* |
| 148 | * Read (and defragment, if necessary) incoming commands |
| 149 | */ |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 150 | struct fio_net_cmd *fio_net_recv_cmd(int sk) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 151 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 152 | struct fio_net_cmd cmd, *cmdret = NULL; |
| 153 | size_t cmd_size = 0, pdu_offset = 0; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 154 | uint16_t crc; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 155 | int ret, first = 1; |
| 156 | void *pdu = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 157 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 158 | do { |
| 159 | ret = fio_recv_data(sk, &cmd, sizeof(cmd)); |
| 160 | if (ret) |
| 161 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 162 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 163 | /* We have a command, verify it and swap if need be */ |
| 164 | ret = verify_convert_cmd(&cmd); |
| 165 | if (ret) |
| 166 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 167 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 168 | if (first) { |
| 169 | /* if this is text, add room for \0 at the end */ |
| 170 | cmd_size = sizeof(cmd) + cmd.pdu_len + 1; |
| 171 | assert(!cmdret); |
| 172 | } else |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 173 | cmd_size += cmd.pdu_len; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 174 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 175 | cmdret = realloc(cmdret, cmd_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 176 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 177 | if (first) |
| 178 | memcpy(cmdret, &cmd, sizeof(cmd)); |
| 179 | else |
| 180 | assert(cmdret->opcode == cmd.opcode); |
| 181 | |
| 182 | if (!cmd.pdu_len) |
| 183 | break; |
| 184 | |
| 185 | /* There's payload, get it */ |
| 186 | pdu = (void *) cmdret->payload + pdu_offset; |
| 187 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 188 | if (ret) |
| 189 | break; |
| 190 | |
| 191 | /* Verify payload crc */ |
| 192 | crc = crc16(pdu, cmd.pdu_len); |
| 193 | if (crc != cmd.pdu_crc16) { |
| 194 | log_err("fio: server bad crc on payload "); |
| 195 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 196 | ret = 1; |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 201 | if (!first) |
| 202 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 203 | first = 0; |
| 204 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 205 | |
| 206 | if (ret) { |
| 207 | free(cmdret); |
| 208 | cmdret = NULL; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 209 | } else if (cmdret) { |
| 210 | /* zero-terminate text input */ |
| 211 | if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT || |
| 212 | cmdret->opcode == FIO_NET_CMD_JOB)) { |
| 213 | char *buf = (char *) cmdret->payload; |
| 214 | |
| 215 | buf[cmdret->pdu_len ] = '\0'; |
| 216 | } |
| 217 | /* frag flag is internal */ |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 218 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 219 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 220 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 221 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 225 | { |
| 226 | uint32_t pdu_len; |
| 227 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 228 | cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 229 | |
| 230 | pdu_len = le32_to_cpu(cmd->pdu_len); |
| 231 | if (pdu_len) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 232 | cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 233 | } |
| 234 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 235 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, |
| 236 | uint64_t tag) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 237 | { |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 238 | struct fio_net_cmd *cmd = NULL; |
| 239 | size_t this_len, cur_len = 0; |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 240 | int ret; |
| 241 | |
| 242 | do { |
| 243 | this_len = size; |
| 244 | if (this_len > FIO_SERVER_MAX_PDU) |
| 245 | this_len = FIO_SERVER_MAX_PDU; |
| 246 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 247 | if (!cmd || cur_len < sizeof(*cmd) + this_len) { |
| 248 | if (cmd) |
| 249 | free(cmd); |
| 250 | |
| 251 | cur_len = sizeof(*cmd) + this_len; |
| 252 | cmd = malloc(cur_len); |
| 253 | } |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 254 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 255 | fio_init_net_cmd(cmd, opcode, buf, this_len, tag); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 256 | |
| 257 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 258 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 259 | |
| 260 | fio_net_cmd_crc(cmd); |
| 261 | |
| 262 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 263 | size -= this_len; |
| 264 | buf += this_len; |
| 265 | } while (!ret && size); |
| 266 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 267 | if (cmd) |
| 268 | free(cmd); |
| 269 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 270 | return ret; |
| 271 | } |
| 272 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 273 | static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 274 | { |
Jens Axboe | 178cde9 | 2011-10-05 22:14:31 +0200 | [diff] [blame] | 275 | struct fio_net_cmd cmd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 276 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 277 | fio_init_net_cmd(&cmd, opcode, NULL, 0, tag); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 278 | fio_net_cmd_crc(&cmd); |
| 279 | |
| 280 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 281 | } |
| 282 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 283 | /* |
| 284 | * If 'list' is non-NULL, then allocate and store the sent command for |
| 285 | * later verification. |
| 286 | */ |
| 287 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag, |
| 288 | struct flist_head *list) |
| 289 | { |
| 290 | struct fio_net_int_cmd *cmd; |
| 291 | int ret; |
| 292 | |
| 293 | if (!list) |
| 294 | return fio_net_send_simple_stack_cmd(sk, opcode, tag); |
| 295 | |
| 296 | cmd = malloc(sizeof(*cmd)); |
| 297 | |
| 298 | fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uint64_t) cmd); |
| 299 | fio_net_cmd_crc(&cmd->cmd); |
| 300 | |
| 301 | INIT_FLIST_HEAD(&cmd->list); |
| 302 | gettimeofday(&cmd->tv, NULL); |
| 303 | cmd->saved_tag = tag; |
| 304 | |
| 305 | ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd)); |
| 306 | if (ret) { |
| 307 | free(cmd); |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | flist_add_tail(&cmd->list, list); |
| 312 | return 0; |
| 313 | } |
| 314 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 315 | static int fio_server_send_quit_cmd(void) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 316 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 317 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 318 | return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL); |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 319 | } |
| 320 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 321 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 322 | { |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 323 | char *buf = (char *) cmd->payload; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 324 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 325 | |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 326 | if (parse_jobs_ini(buf, 1, 0)) { |
| 327 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 328 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 329 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 330 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 331 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 332 | |
| 333 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 334 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 335 | reset_fio_state(); |
| 336 | return ret; |
| 337 | } |
| 338 | |
| 339 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 340 | { |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 341 | void *pdu = cmd->payload; |
| 342 | struct cmd_single_line_pdu *cslp; |
| 343 | struct cmd_line_pdu *clp; |
| 344 | unsigned long offset; |
| 345 | char **argv; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 346 | int ret, i; |
| 347 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 348 | clp = pdu; |
| 349 | clp->lines = le16_to_cpu(clp->lines); |
| 350 | argv = malloc(clp->lines * sizeof(char *)); |
| 351 | offset = sizeof(*clp); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 352 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 353 | dprint(FD_NET, "server: %d command line args\n", clp->lines); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 354 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 355 | for (i = 0; i < clp->lines; i++) { |
| 356 | cslp = pdu + offset; |
| 357 | argv[i] = (char *) cslp->text; |
| 358 | |
| 359 | offset += sizeof(*cslp) + le16_to_cpu(cslp->len); |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 360 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 361 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 362 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 363 | if (parse_cmd_line(clp->lines, argv)) { |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 364 | fio_server_send_quit_cmd(); |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 365 | free(argv); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 366 | return -1; |
Jens Axboe | e6d1c66 | 2011-10-05 20:41:06 +0200 | [diff] [blame] | 367 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 368 | |
Jens Axboe | fa2ea80 | 2011-10-08 21:07:29 +0200 | [diff] [blame] | 369 | free(argv); |
| 370 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 371 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 372 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 373 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 374 | fio_server_send_quit_cmd(); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 375 | reset_fio_state(); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 376 | return ret; |
| 377 | } |
| 378 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 379 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 380 | { |
| 381 | struct cmd_probe_pdu probe; |
| 382 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 383 | dprint(FD_NET, "server: sending probe reply\n"); |
| 384 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 385 | memset(&probe, 0, sizeof(probe)); |
| 386 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 387 | #ifdef FIO_BIG_ENDIAN |
| 388 | probe.bigendian = 1; |
| 389 | #endif |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 390 | probe.fio_major = FIO_MAJOR; |
| 391 | probe.fio_minor = FIO_MINOR; |
| 392 | probe.fio_patch = FIO_PATCH; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 393 | |
Jens Axboe | cca8464 | 2011-10-07 12:47:57 +0200 | [diff] [blame] | 394 | probe.os = FIO_OS; |
| 395 | probe.arch = FIO_ARCH; |
| 396 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 397 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static int handle_send_eta_cmd(struct fio_net_cmd *cmd) |
| 401 | { |
| 402 | struct jobs_eta *je; |
| 403 | size_t size; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 404 | int i; |
| 405 | |
| 406 | size = sizeof(*je) + thread_number * sizeof(char); |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 407 | je = malloc(size); |
| 408 | memset(je, 0, size); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 409 | |
| 410 | if (!calc_thread_status(je, 1)) { |
| 411 | free(je); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | dprint(FD_NET, "server sending status\n"); |
| 416 | |
| 417 | je->nr_running = cpu_to_le32(je->nr_running); |
| 418 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 419 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 420 | je->files_open = cpu_to_le32(je->files_open); |
| 421 | je->m_rate = cpu_to_le32(je->m_rate); |
| 422 | je->t_rate = cpu_to_le32(je->t_rate); |
| 423 | je->m_iops = cpu_to_le32(je->m_iops); |
| 424 | je->t_iops = cpu_to_le32(je->t_iops); |
| 425 | |
| 426 | for (i = 0; i < 2; i++) { |
| 427 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 428 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 429 | } |
| 430 | |
| 431 | je->elapsed_sec = cpu_to_le32(je->nr_running); |
| 432 | je->eta_sec = cpu_to_le64(je->eta_sec); |
| 433 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 434 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 435 | free(je); |
| 436 | return 0; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 437 | } |
| 438 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 439 | static int handle_command(struct fio_net_cmd *cmd) |
| 440 | { |
| 441 | int ret; |
| 442 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 443 | dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n", |
| 444 | fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 445 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 446 | switch (cmd->opcode) { |
| 447 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 448 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 449 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 450 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 451 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 452 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 453 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 454 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 455 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 456 | case FIO_NET_CMD_JOBLINE: |
| 457 | ret = handle_jobline_cmd(cmd); |
| 458 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 459 | case FIO_NET_CMD_PROBE: |
| 460 | ret = handle_probe_cmd(cmd); |
| 461 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 462 | case FIO_NET_CMD_SEND_ETA: |
| 463 | ret = handle_send_eta_cmd(cmd); |
| 464 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 465 | default: |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 466 | log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 467 | ret = 1; |
| 468 | } |
| 469 | |
| 470 | return ret; |
| 471 | } |
| 472 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 473 | static int handle_connection(int sk, int block) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 474 | { |
| 475 | struct fio_net_cmd *cmd = NULL; |
| 476 | int ret = 0; |
| 477 | |
| 478 | /* read forever */ |
| 479 | while (!exit_backend) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 480 | struct pollfd pfd = { |
| 481 | .fd = sk, |
| 482 | .events = POLLIN, |
| 483 | }; |
| 484 | |
| 485 | ret = 0; |
| 486 | do { |
| 487 | ret = poll(&pfd, 1, 100); |
| 488 | if (ret < 0) { |
| 489 | if (errno == EINTR) |
| 490 | break; |
| 491 | log_err("fio: poll: %s\n", strerror(errno)); |
| 492 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 493 | } else if (!ret) { |
| 494 | if (!block) |
| 495 | return 0; |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 496 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 497 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 498 | |
| 499 | if (pfd.revents & POLLIN) |
| 500 | break; |
| 501 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 502 | ret = 1; |
| 503 | break; |
| 504 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 505 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 506 | |
| 507 | if (ret < 0) |
| 508 | break; |
| 509 | |
| 510 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 511 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 512 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 516 | ret = handle_command(cmd); |
| 517 | if (ret) |
| 518 | break; |
| 519 | |
| 520 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 521 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | if (cmd) |
| 525 | free(cmd); |
| 526 | |
| 527 | return ret; |
| 528 | } |
| 529 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 530 | void fio_server_idle_loop(void) |
| 531 | { |
| 532 | if (server_fd != -1) |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 533 | handle_connection(server_fd, 0); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 536 | static int accept_loop(int listen_sk) |
| 537 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 538 | struct sockaddr_in addr; |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 539 | fio_socklen_t len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 540 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 541 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 542 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 543 | dprint(FD_NET, "server enter accept loop\n"); |
| 544 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 545 | flags = fcntl(listen_sk, F_GETFL); |
| 546 | flags |= O_NONBLOCK; |
| 547 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 548 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 549 | pfd.fd = listen_sk; |
| 550 | pfd.events = POLLIN; |
| 551 | do { |
| 552 | ret = poll(&pfd, 1, 100); |
| 553 | if (ret < 0) { |
| 554 | if (errno == EINTR) |
| 555 | break; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 556 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 557 | goto out; |
| 558 | } else if (!ret) |
| 559 | continue; |
| 560 | |
| 561 | if (pfd.revents & POLLIN) |
| 562 | break; |
| 563 | } while (!exit_backend); |
| 564 | |
| 565 | if (exit_backend) |
| 566 | goto out; |
| 567 | |
Jens Axboe | 6b976bf | 2011-10-04 15:07:43 +0200 | [diff] [blame] | 568 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 569 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 570 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 571 | return -1; |
| 572 | } |
| 573 | |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 574 | dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr)); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 575 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 576 | server_fd = sk; |
| 577 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 578 | exitval = handle_connection(sk, 1); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 579 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 580 | server_fd = -1; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 581 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 582 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 583 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 584 | goto again; |
| 585 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 586 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 587 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 588 | } |
| 589 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 590 | int fio_server_text_output(const char *buf, size_t len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 591 | { |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 592 | if (server_fd != -1) |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 593 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0); |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 594 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 595 | return log_local_buf(buf, len); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 596 | } |
| 597 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 598 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 599 | { |
| 600 | dst->max_val = cpu_to_le64(src->max_val); |
| 601 | dst->min_val = cpu_to_le64(src->min_val); |
| 602 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 603 | |
| 604 | /* |
| 605 | * Encode to IEEE 754 for network transfer |
| 606 | */ |
| 607 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 608 | dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f)); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 612 | { |
| 613 | int i; |
| 614 | |
| 615 | for (i = 0; i < 2; i++) { |
| 616 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 617 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 618 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 619 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 620 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 621 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 622 | } |
| 623 | |
| 624 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 625 | dst->groupid = cpu_to_le32(src->groupid); |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 630 | * into a single payload. |
| 631 | */ |
| 632 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 633 | { |
| 634 | struct cmd_ts_pdu p; |
| 635 | int i, j; |
| 636 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 637 | dprint(FD_NET, "server sending end stats\n"); |
| 638 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 639 | memset(&p, 0, sizeof(p)); |
| 640 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 641 | strcpy(p.ts.name, ts->name); |
| 642 | strcpy(p.ts.verror, ts->verror); |
| 643 | strcpy(p.ts.description, ts->description); |
| 644 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 645 | p.ts.error = cpu_to_le32(ts->error); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 646 | p.ts.groupid = cpu_to_le32(ts->groupid); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 647 | p.ts.pid = cpu_to_le32(ts->pid); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 648 | p.ts.members = cpu_to_le32(ts->members); |
| 649 | |
| 650 | for (i = 0; i < 2; i++) { |
| 651 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 652 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 653 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 654 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 655 | } |
| 656 | |
| 657 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 658 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 659 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 660 | p.ts.minf = cpu_to_le64(ts->minf); |
| 661 | p.ts.majf = cpu_to_le64(ts->majf); |
| 662 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 663 | |
| 664 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
| 665 | fio_fp64_t *fp = &p.ts.percentile_list[i]; |
| 666 | |
| 667 | fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f)); |
| 668 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 669 | |
| 670 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 671 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 672 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 673 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 674 | } |
| 675 | |
| 676 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 677 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 678 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 679 | } |
| 680 | |
| 681 | for (i = 0; i < 2; i++) |
| 682 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 683 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 684 | |
| 685 | for (i = 0; i < 3; i++) { |
| 686 | p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]); |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 687 | p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 690 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 691 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 692 | |
| 693 | for (i = 0; i < 2; i++) { |
| 694 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 695 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 696 | } |
| 697 | |
| 698 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 699 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 700 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 701 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 702 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 703 | |
| 704 | convert_gs(&p.rs, rs); |
| 705 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 706 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | void fio_server_send_gs(struct group_run_stats *rs) |
| 710 | { |
| 711 | struct group_run_stats gs; |
| 712 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 713 | dprint(FD_NET, "server sending group run stats\n"); |
| 714 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 715 | convert_gs(&gs, rs); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 716 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 717 | } |
| 718 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 719 | int fio_server_log(const char *format, ...) |
| 720 | { |
| 721 | char buffer[1024]; |
| 722 | va_list args; |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 723 | size_t len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 724 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 725 | dprint(FD_NET, "server log\n"); |
| 726 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 727 | va_start(args, format); |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 728 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 729 | va_end(args); |
| 730 | |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 731 | return fio_server_text_output(buffer, len); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 732 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 733 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 734 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 735 | { |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 736 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 737 | |
| 738 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 739 | if (sk < 0) { |
| 740 | log_err("fio: socket: %s\n", strerror(errno)); |
| 741 | return -1; |
| 742 | } |
| 743 | |
| 744 | opt = 1; |
| 745 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 746 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 747 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 748 | return -1; |
| 749 | } |
| 750 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 751 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 752 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 753 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 754 | return -1; |
| 755 | } |
| 756 | #endif |
| 757 | |
| 758 | saddr_in.sin_family = AF_INET; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 759 | |
| 760 | if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { |
| 761 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 762 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 763 | return -1; |
| 764 | } |
| 765 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 766 | return sk; |
| 767 | } |
| 768 | |
| 769 | static int fio_init_server_sock(void) |
| 770 | { |
| 771 | struct sockaddr_un addr; |
| 772 | fio_socklen_t len; |
| 773 | mode_t mode; |
| 774 | int sk; |
| 775 | |
| 776 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 777 | if (sk < 0) { |
| 778 | log_err("fio: socket: %s\n", strerror(errno)); |
| 779 | return -1; |
| 780 | } |
| 781 | |
| 782 | mode = umask(000); |
| 783 | |
| 784 | memset(&addr, 0, sizeof(addr)); |
| 785 | addr.sun_family = AF_UNIX; |
| 786 | strcpy(addr.sun_path, bind_sock); |
| 787 | unlink(bind_sock); |
| 788 | |
| 789 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 790 | |
| 791 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 792 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 793 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 794 | return -1; |
| 795 | } |
| 796 | |
| 797 | umask(mode); |
| 798 | return sk; |
| 799 | } |
| 800 | |
| 801 | static int fio_init_server_connection(void) |
| 802 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 803 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 804 | int sk; |
| 805 | |
| 806 | dprint(FD_NET, "starting server\n"); |
| 807 | |
| 808 | if (!bind_sock) |
| 809 | sk = fio_init_server_ip(); |
| 810 | else |
| 811 | sk = fio_init_server_sock(); |
| 812 | |
| 813 | if (sk < 0) |
| 814 | return sk; |
| 815 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 816 | if (!bind_sock) |
| 817 | sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port); |
| 818 | else |
| 819 | strcpy(bind_str, bind_sock); |
| 820 | |
| 821 | log_info("fio: server listening on %s\n", bind_str); |
| 822 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 823 | if (listen(sk, 0) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 824 | log_err("fio: listen: %s\n", strerror(errno)); |
| 825 | return -1; |
| 826 | } |
| 827 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 828 | return sk; |
| 829 | } |
| 830 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 831 | int fio_server_parse_string(const char *str, char **ptr, int *is_sock, |
| 832 | int *port, struct in_addr *inp) |
| 833 | { |
| 834 | *ptr = NULL; |
| 835 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 836 | *port = fio_net_port; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 837 | |
| 838 | if (!strncmp(str, "sock:", 5)) { |
| 839 | *ptr = strdup(str + 5); |
| 840 | *is_sock = 1; |
| 841 | } else { |
| 842 | const char *host = str; |
| 843 | char *portp; |
| 844 | int lport = 0; |
| 845 | |
| 846 | /* |
| 847 | * Is it ip:<ip or host>:port |
| 848 | */ |
| 849 | if (!strncmp(host, "ip:", 3)) |
| 850 | host += 3; |
| 851 | else if (host[0] == ':') { |
| 852 | /* String is :port */ |
| 853 | host++; |
| 854 | lport = atoi(host); |
| 855 | if (!lport || lport > 65535) { |
| 856 | log_err("fio: bad server port %u\n", port); |
| 857 | return 1; |
| 858 | } |
| 859 | /* no hostname given, we are done */ |
| 860 | *port = lport; |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * If no port seen yet, check if there's a last ':' at the end |
| 866 | */ |
| 867 | if (!lport) { |
| 868 | portp = strchr(host, ':'); |
| 869 | if (portp) { |
| 870 | *portp = '\0'; |
| 871 | portp++; |
| 872 | lport = atoi(portp); |
| 873 | if (!lport || lport > 65535) { |
| 874 | log_err("fio: bad server port %u\n", port); |
| 875 | return 1; |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | if (lport) |
| 881 | *port = lport; |
| 882 | |
| 883 | *ptr = strdup(host); |
| 884 | |
| 885 | if (inet_aton(host, inp) != 1) { |
| 886 | struct hostent *hent; |
| 887 | |
| 888 | hent = gethostbyname(host); |
| 889 | if (!hent) { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 890 | free(*ptr); |
| 891 | *ptr = NULL; |
| 892 | return 1; |
| 893 | } |
| 894 | |
| 895 | memcpy(inp, hent->h_addr, 4); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (*port == 0) |
| 900 | *port = fio_net_port; |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 905 | /* |
| 906 | * Server arg should be one of: |
| 907 | * |
| 908 | * sock:/path/to/socket |
| 909 | * ip:1.2.3.4 |
| 910 | * 1.2.3.4 |
| 911 | * |
| 912 | * Where sock uses unix domain sockets, and ip binds the server to |
| 913 | * a specific interface. If no arguments are given to the server, it |
| 914 | * uses IP and binds to 0.0.0.0. |
| 915 | * |
| 916 | */ |
| 917 | static int fio_handle_server_arg(void) |
| 918 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 919 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 920 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 921 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 922 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 923 | |
| 924 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 925 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 926 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 927 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 928 | &port, &saddr_in.sin_addr); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 929 | |
| 930 | if (!is_sock && bind_sock) { |
| 931 | free(bind_sock); |
| 932 | bind_sock = NULL; |
| 933 | } |
| 934 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 935 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 936 | fio_net_port = port; |
| 937 | saddr_in.sin_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 938 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | static int fio_server(void) |
| 942 | { |
| 943 | int sk, ret; |
| 944 | |
| 945 | dprint(FD_NET, "starting server\n"); |
| 946 | |
| 947 | if (fio_handle_server_arg()) |
| 948 | return -1; |
| 949 | |
| 950 | sk = fio_init_server_connection(); |
| 951 | if (sk < 0) |
| 952 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 953 | |
| 954 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 955 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 956 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 957 | |
| 958 | if (fio_server_arg) { |
| 959 | free(fio_server_arg); |
| 960 | fio_server_arg = NULL; |
| 961 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 962 | if (bind_sock) |
| 963 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 964 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 965 | return ret; |
| 966 | } |
| 967 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 968 | static void sig_int(int sig) |
| 969 | { |
| 970 | fio_terminate_threads(TERMINATE_ALL); |
| 971 | exit_backend = 1; |
| 972 | } |
| 973 | |
| 974 | static void server_signal_handler(void) |
| 975 | { |
| 976 | struct sigaction act; |
| 977 | |
| 978 | memset(&act, 0, sizeof(act)); |
| 979 | act.sa_handler = sig_int; |
| 980 | act.sa_flags = SA_RESTART; |
| 981 | sigaction(SIGINT, &act, NULL); |
| 982 | |
| 983 | memset(&act, 0, sizeof(act)); |
| 984 | act.sa_handler = sig_int; |
| 985 | act.sa_flags = SA_RESTART; |
| 986 | sigaction(SIGTERM, &act, NULL); |
| 987 | } |
| 988 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 989 | static int check_existing_pidfile(const char *pidfile) |
| 990 | { |
| 991 | struct stat sb; |
| 992 | char buf[16]; |
| 993 | pid_t pid; |
| 994 | FILE *f; |
| 995 | |
| 996 | if (stat(pidfile, &sb)) |
| 997 | return 0; |
| 998 | |
| 999 | f = fopen(pidfile, "r"); |
| 1000 | if (!f) |
| 1001 | return 0; |
| 1002 | |
Jens Axboe | bfc3b17 | 2011-10-10 21:16:55 +0200 | [diff] [blame] | 1003 | if (fread(buf, sb.st_size, 1, f) <= 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1004 | fclose(f); |
| 1005 | return 1; |
| 1006 | } |
| 1007 | fclose(f); |
| 1008 | |
| 1009 | pid = atoi(buf); |
| 1010 | if (kill(pid, SIGCONT) < 0) |
Jens Axboe | ea5aa1b | 2011-10-11 11:45:35 +0200 | [diff] [blame^] | 1011 | return errno != ESRCH; |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1012 | |
| 1013 | return 1; |
| 1014 | } |
| 1015 | |
| 1016 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1017 | { |
| 1018 | FILE *fpid; |
| 1019 | |
| 1020 | fpid = fopen(pidfile, "w"); |
| 1021 | if (!fpid) { |
| 1022 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1023 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1027 | fclose(fpid); |
| 1028 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * If pidfile is specified, background us. |
| 1033 | */ |
| 1034 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1035 | { |
| 1036 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1037 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1038 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 1039 | server_signal_handler(); |
| 1040 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1041 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1042 | return fio_server(); |
| 1043 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1044 | if (check_existing_pidfile(pidfile)) { |
| 1045 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 1046 | pidfile); |
| 1047 | return -1; |
| 1048 | } |
| 1049 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1050 | pid = fork(); |
| 1051 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1052 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1053 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 1054 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1055 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1056 | int ret = write_pid(pid, pidfile); |
| 1057 | |
| 1058 | exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1059 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1060 | |
| 1061 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1062 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1063 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1064 | close(STDIN_FILENO); |
| 1065 | close(STDOUT_FILENO); |
| 1066 | close(STDERR_FILENO); |
| 1067 | f_out = NULL; |
| 1068 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1069 | |
| 1070 | ret = fio_server(); |
| 1071 | |
| 1072 | closelog(); |
| 1073 | unlink(pidfile); |
| 1074 | free(pidfile); |
| 1075 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1076 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1077 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1078 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1079 | { |
| 1080 | fio_server_arg = strdup(arg); |
| 1081 | } |