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