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 | |
Jens Axboe | b814fb2 | 2011-10-12 09:20:34 +0200 | [diff] [blame] | 412 | if (!thread_number) |
| 413 | return 0; |
| 414 | |
| 415 | size = sizeof(*je) + thread_number * sizeof(char) + 1; |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 416 | je = malloc(size); |
| 417 | memset(je, 0, size); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 418 | |
| 419 | if (!calc_thread_status(je, 1)) { |
| 420 | free(je); |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | dprint(FD_NET, "server sending status\n"); |
| 425 | |
| 426 | je->nr_running = cpu_to_le32(je->nr_running); |
| 427 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 428 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 429 | je->files_open = cpu_to_le32(je->files_open); |
| 430 | je->m_rate = cpu_to_le32(je->m_rate); |
| 431 | je->t_rate = cpu_to_le32(je->t_rate); |
| 432 | je->m_iops = cpu_to_le32(je->m_iops); |
| 433 | je->t_iops = cpu_to_le32(je->t_iops); |
| 434 | |
| 435 | for (i = 0; i < 2; i++) { |
| 436 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 437 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 438 | } |
| 439 | |
| 440 | je->elapsed_sec = cpu_to_le32(je->nr_running); |
| 441 | je->eta_sec = cpu_to_le64(je->eta_sec); |
| 442 | |
Jens Axboe | 7f86831 | 2011-10-10 09:55:21 +0200 | [diff] [blame] | 443 | 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] | 444 | free(je); |
| 445 | return 0; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 448 | static int handle_command(struct fio_net_cmd *cmd) |
| 449 | { |
| 450 | int ret; |
| 451 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 452 | dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n", |
| 453 | fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag); |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 454 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 455 | switch (cmd->opcode) { |
| 456 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 457 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 458 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 459 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 460 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 461 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 462 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 463 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 464 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 465 | case FIO_NET_CMD_JOBLINE: |
| 466 | ret = handle_jobline_cmd(cmd); |
| 467 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 468 | case FIO_NET_CMD_PROBE: |
| 469 | ret = handle_probe_cmd(cmd); |
| 470 | break; |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 471 | case FIO_NET_CMD_SEND_ETA: |
| 472 | ret = handle_send_eta_cmd(cmd); |
| 473 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 474 | default: |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 475 | log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 476 | ret = 1; |
| 477 | } |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 482 | static int handle_connection(int sk, int block) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 483 | { |
| 484 | struct fio_net_cmd *cmd = NULL; |
| 485 | int ret = 0; |
| 486 | |
| 487 | /* read forever */ |
| 488 | while (!exit_backend) { |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 489 | struct pollfd pfd = { |
| 490 | .fd = sk, |
| 491 | .events = POLLIN, |
| 492 | }; |
| 493 | |
| 494 | ret = 0; |
| 495 | do { |
| 496 | ret = poll(&pfd, 1, 100); |
| 497 | if (ret < 0) { |
| 498 | if (errno == EINTR) |
| 499 | break; |
| 500 | log_err("fio: poll: %s\n", strerror(errno)); |
| 501 | break; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 502 | } else if (!ret) { |
| 503 | if (!block) |
| 504 | return 0; |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 505 | continue; |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 506 | } |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 507 | |
| 508 | if (pfd.revents & POLLIN) |
| 509 | break; |
| 510 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 511 | ret = 1; |
| 512 | break; |
| 513 | } |
Jens Axboe | 19c6517 | 2011-10-05 22:05:37 +0200 | [diff] [blame] | 514 | } while (!exit_backend); |
Jens Axboe | e951bdc | 2011-10-05 21:58:45 +0200 | [diff] [blame] | 515 | |
| 516 | if (ret < 0) |
| 517 | break; |
| 518 | |
| 519 | cmd = fio_net_recv_cmd(sk); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 520 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 521 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 525 | ret = handle_command(cmd); |
| 526 | if (ret) |
| 527 | break; |
| 528 | |
| 529 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 530 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if (cmd) |
| 534 | free(cmd); |
| 535 | |
| 536 | return ret; |
| 537 | } |
| 538 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 539 | void fio_server_idle_loop(void) |
| 540 | { |
| 541 | if (server_fd != -1) |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 542 | handle_connection(server_fd, 0); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 543 | } |
| 544 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 545 | static int accept_loop(int listen_sk) |
| 546 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 547 | struct sockaddr_in addr; |
Jens Axboe | 5ba13ea | 2011-10-04 23:50:28 +0200 | [diff] [blame] | 548 | fio_socklen_t len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 549 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 550 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 551 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 552 | dprint(FD_NET, "server enter accept loop\n"); |
| 553 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 554 | flags = fcntl(listen_sk, F_GETFL); |
| 555 | flags |= O_NONBLOCK; |
| 556 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 557 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 558 | pfd.fd = listen_sk; |
| 559 | pfd.events = POLLIN; |
| 560 | do { |
| 561 | ret = poll(&pfd, 1, 100); |
| 562 | if (ret < 0) { |
| 563 | if (errno == EINTR) |
| 564 | break; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 565 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 566 | goto out; |
| 567 | } else if (!ret) |
| 568 | continue; |
| 569 | |
| 570 | if (pfd.revents & POLLIN) |
| 571 | break; |
| 572 | } while (!exit_backend); |
| 573 | |
| 574 | if (exit_backend) |
| 575 | goto out; |
| 576 | |
Jens Axboe | 6b976bf | 2011-10-04 15:07:43 +0200 | [diff] [blame] | 577 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 578 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 579 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 580 | return -1; |
| 581 | } |
| 582 | |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 583 | 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] | 584 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 585 | server_fd = sk; |
| 586 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 587 | exitval = handle_connection(sk, 1); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 588 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 589 | server_fd = -1; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 590 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 591 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 592 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 593 | goto again; |
| 594 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 595 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 596 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 597 | } |
| 598 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 599 | int fio_server_text_output(const char *buf, size_t len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 600 | { |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 601 | if (server_fd != -1) |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 602 | 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] | 603 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 604 | return log_local_buf(buf, len); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 605 | } |
| 606 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 607 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 608 | { |
| 609 | dst->max_val = cpu_to_le64(src->max_val); |
| 610 | dst->min_val = cpu_to_le64(src->min_val); |
| 611 | dst->samples = cpu_to_le64(src->samples); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Encode to IEEE 754 for network transfer |
| 615 | */ |
| 616 | dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f)); |
| 617 | 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] | 618 | } |
| 619 | |
| 620 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 621 | { |
| 622 | int i; |
| 623 | |
| 624 | for (i = 0; i < 2; i++) { |
| 625 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 626 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 627 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 628 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 629 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 630 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 631 | } |
| 632 | |
| 633 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 634 | dst->groupid = cpu_to_le32(src->groupid); |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 639 | * into a single payload. |
| 640 | */ |
| 641 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 642 | { |
| 643 | struct cmd_ts_pdu p; |
| 644 | int i, j; |
| 645 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 646 | dprint(FD_NET, "server sending end stats\n"); |
| 647 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 648 | memset(&p, 0, sizeof(p)); |
| 649 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 650 | strcpy(p.ts.name, ts->name); |
| 651 | strcpy(p.ts.verror, ts->verror); |
| 652 | strcpy(p.ts.description, ts->description); |
| 653 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 654 | p.ts.error = cpu_to_le32(ts->error); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 655 | p.ts.groupid = cpu_to_le32(ts->groupid); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 656 | p.ts.pid = cpu_to_le32(ts->pid); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 657 | p.ts.members = cpu_to_le32(ts->members); |
| 658 | |
| 659 | for (i = 0; i < 2; i++) { |
| 660 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 661 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 662 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 663 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 664 | } |
| 665 | |
| 666 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 667 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 668 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 669 | p.ts.minf = cpu_to_le64(ts->minf); |
| 670 | p.ts.majf = cpu_to_le64(ts->majf); |
| 671 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 672 | |
| 673 | for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) { |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 674 | fio_fp64_t *src = &ts->percentile_list[i]; |
| 675 | fio_fp64_t *dst = &p.ts.percentile_list[i]; |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 676 | |
Jens Axboe | cfc03e4 | 2011-10-12 21:20:42 +0200 | [diff] [blame] | 677 | dst->u.i = __cpu_to_le64(fio_double_to_uint64(src->u.f)); |
Jens Axboe | 802ad4a | 2011-10-05 09:51:58 +0200 | [diff] [blame] | 678 | } |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 679 | |
| 680 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 681 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 682 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 683 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 684 | } |
| 685 | |
| 686 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 687 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 688 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 689 | } |
| 690 | |
| 691 | for (i = 0; i < 2; i++) |
| 692 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 693 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 694 | |
| 695 | for (i = 0; i < 3; i++) { |
| 696 | 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] | 697 | 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] | 698 | } |
| 699 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 700 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 701 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 702 | |
| 703 | for (i = 0; i < 2; i++) { |
| 704 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 705 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 706 | } |
| 707 | |
| 708 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 709 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 710 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 711 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 712 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 713 | |
| 714 | convert_gs(&p.rs, rs); |
| 715 | |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 716 | 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] | 717 | } |
| 718 | |
| 719 | void fio_server_send_gs(struct group_run_stats *rs) |
| 720 | { |
| 721 | struct group_run_stats gs; |
| 722 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 723 | dprint(FD_NET, "server sending group run stats\n"); |
| 724 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 725 | convert_gs(&gs, rs); |
Jens Axboe | af9c9fb | 2011-10-09 21:54:10 +0200 | [diff] [blame] | 726 | 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] | 727 | } |
| 728 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 729 | int fio_server_log(const char *format, ...) |
| 730 | { |
| 731 | char buffer[1024]; |
| 732 | va_list args; |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 733 | size_t len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 734 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 735 | dprint(FD_NET, "server log\n"); |
| 736 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 737 | va_start(args, format); |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 738 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 739 | va_end(args); |
| 740 | |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 741 | return fio_server_text_output(buffer, len); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 742 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 743 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 744 | static int fio_init_server_ip(void) |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 745 | { |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 746 | int sk, opt; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 747 | |
| 748 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 749 | if (sk < 0) { |
| 750 | log_err("fio: socket: %s\n", strerror(errno)); |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | opt = 1; |
| 755 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 756 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 757 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 758 | return -1; |
| 759 | } |
| 760 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 761 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 762 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 763 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 764 | return -1; |
| 765 | } |
| 766 | #endif |
| 767 | |
| 768 | saddr_in.sin_family = AF_INET; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 769 | |
| 770 | if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { |
| 771 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 772 | close(sk); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 773 | return -1; |
| 774 | } |
| 775 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 776 | return sk; |
| 777 | } |
| 778 | |
| 779 | static int fio_init_server_sock(void) |
| 780 | { |
| 781 | struct sockaddr_un addr; |
| 782 | fio_socklen_t len; |
| 783 | mode_t mode; |
| 784 | int sk; |
| 785 | |
| 786 | sk = socket(AF_UNIX, SOCK_STREAM, 0); |
| 787 | if (sk < 0) { |
| 788 | log_err("fio: socket: %s\n", strerror(errno)); |
| 789 | return -1; |
| 790 | } |
| 791 | |
| 792 | mode = umask(000); |
| 793 | |
| 794 | memset(&addr, 0, sizeof(addr)); |
| 795 | addr.sun_family = AF_UNIX; |
| 796 | strcpy(addr.sun_path, bind_sock); |
| 797 | unlink(bind_sock); |
| 798 | |
| 799 | len = sizeof(addr.sun_family) + strlen(bind_sock) + 1; |
| 800 | |
| 801 | if (bind(sk, (struct sockaddr *) &addr, len) < 0) { |
| 802 | log_err("fio: bind: %s\n", strerror(errno)); |
Jens Axboe | b94cba4 | 2011-10-06 21:27:10 +0200 | [diff] [blame] | 803 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | umask(mode); |
| 808 | return sk; |
| 809 | } |
| 810 | |
| 811 | static int fio_init_server_connection(void) |
| 812 | { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 813 | char bind_str[128]; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 814 | int sk; |
| 815 | |
| 816 | dprint(FD_NET, "starting server\n"); |
| 817 | |
| 818 | if (!bind_sock) |
| 819 | sk = fio_init_server_ip(); |
| 820 | else |
| 821 | sk = fio_init_server_sock(); |
| 822 | |
| 823 | if (sk < 0) |
| 824 | return sk; |
| 825 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 826 | if (!bind_sock) |
| 827 | sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port); |
| 828 | else |
| 829 | strcpy(bind_str, bind_sock); |
| 830 | |
| 831 | log_info("fio: server listening on %s\n", bind_str); |
| 832 | |
Jens Axboe | 89c1707 | 2011-10-11 10:15:51 +0200 | [diff] [blame] | 833 | if (listen(sk, 0) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 834 | log_err("fio: listen: %s\n", strerror(errno)); |
| 835 | return -1; |
| 836 | } |
| 837 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 838 | return sk; |
| 839 | } |
| 840 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 841 | int fio_server_parse_string(const char *str, char **ptr, int *is_sock, |
| 842 | int *port, struct in_addr *inp) |
| 843 | { |
| 844 | *ptr = NULL; |
| 845 | *is_sock = 0; |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 846 | *port = fio_net_port; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 847 | |
| 848 | if (!strncmp(str, "sock:", 5)) { |
| 849 | *ptr = strdup(str + 5); |
| 850 | *is_sock = 1; |
| 851 | } else { |
| 852 | const char *host = str; |
| 853 | char *portp; |
| 854 | int lport = 0; |
| 855 | |
| 856 | /* |
| 857 | * Is it ip:<ip or host>:port |
| 858 | */ |
| 859 | if (!strncmp(host, "ip:", 3)) |
| 860 | host += 3; |
| 861 | else if (host[0] == ':') { |
| 862 | /* String is :port */ |
| 863 | host++; |
| 864 | lport = atoi(host); |
| 865 | if (!lport || lport > 65535) { |
| 866 | log_err("fio: bad server port %u\n", port); |
| 867 | return 1; |
| 868 | } |
| 869 | /* no hostname given, we are done */ |
| 870 | *port = lport; |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * If no port seen yet, check if there's a last ':' at the end |
| 876 | */ |
| 877 | if (!lport) { |
| 878 | portp = strchr(host, ':'); |
| 879 | if (portp) { |
| 880 | *portp = '\0'; |
| 881 | portp++; |
| 882 | lport = atoi(portp); |
| 883 | if (!lport || lport > 65535) { |
| 884 | log_err("fio: bad server port %u\n", port); |
| 885 | return 1; |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | if (lport) |
| 891 | *port = lport; |
| 892 | |
| 893 | *ptr = strdup(host); |
| 894 | |
| 895 | if (inet_aton(host, inp) != 1) { |
| 896 | struct hostent *hent; |
| 897 | |
| 898 | hent = gethostbyname(host); |
| 899 | if (!hent) { |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 900 | free(*ptr); |
| 901 | *ptr = NULL; |
| 902 | return 1; |
| 903 | } |
| 904 | |
| 905 | memcpy(inp, hent->h_addr, 4); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | if (*port == 0) |
| 910 | *port = fio_net_port; |
| 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 915 | /* |
| 916 | * Server arg should be one of: |
| 917 | * |
| 918 | * sock:/path/to/socket |
| 919 | * ip:1.2.3.4 |
| 920 | * 1.2.3.4 |
| 921 | * |
| 922 | * Where sock uses unix domain sockets, and ip binds the server to |
| 923 | * a specific interface. If no arguments are given to the server, it |
| 924 | * uses IP and binds to 0.0.0.0. |
| 925 | * |
| 926 | */ |
| 927 | static int fio_handle_server_arg(void) |
| 928 | { |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 929 | int port = fio_net_port; |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 930 | int is_sock, ret = 0; |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 931 | |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 932 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 933 | |
| 934 | if (!fio_server_arg) |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 935 | goto out; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 936 | |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 937 | ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock, |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 938 | &port, &saddr_in.sin_addr); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 939 | |
| 940 | if (!is_sock && bind_sock) { |
| 941 | free(bind_sock); |
| 942 | bind_sock = NULL; |
| 943 | } |
| 944 | |
Jens Axboe | a7de0a1 | 2011-10-07 12:55:14 +0200 | [diff] [blame] | 945 | out: |
Jens Axboe | 6d2cf39 | 2011-10-07 13:19:28 +0200 | [diff] [blame] | 946 | fio_net_port = port; |
| 947 | saddr_in.sin_port = htons(port); |
Jens Axboe | 4e5b8fb | 2011-10-07 10:03:44 +0200 | [diff] [blame] | 948 | return ret; |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | static int fio_server(void) |
| 952 | { |
| 953 | int sk, ret; |
| 954 | |
| 955 | dprint(FD_NET, "starting server\n"); |
| 956 | |
| 957 | if (fio_handle_server_arg()) |
| 958 | return -1; |
| 959 | |
| 960 | sk = fio_init_server_connection(); |
| 961 | if (sk < 0) |
| 962 | return -1; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 963 | |
| 964 | ret = accept_loop(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 965 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 966 | close(sk); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 967 | |
| 968 | if (fio_server_arg) { |
| 969 | free(fio_server_arg); |
| 970 | fio_server_arg = NULL; |
| 971 | } |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 972 | if (bind_sock) |
| 973 | free(bind_sock); |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 974 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 975 | return ret; |
| 976 | } |
| 977 | |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 978 | void fio_server_got_signal(int signal) |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 979 | { |
Jens Axboe | 7b82168 | 2011-10-11 12:16:32 +0200 | [diff] [blame] | 980 | if (signal == SIGPIPE) |
| 981 | server_fd = -1; |
| 982 | else { |
| 983 | log_info("\nfio: terminating on signal %d\n", signal); |
| 984 | exit_backend = 1; |
| 985 | } |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 986 | } |
| 987 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 988 | static int check_existing_pidfile(const char *pidfile) |
| 989 | { |
| 990 | struct stat sb; |
| 991 | char buf[16]; |
| 992 | pid_t pid; |
| 993 | FILE *f; |
| 994 | |
| 995 | if (stat(pidfile, &sb)) |
| 996 | return 0; |
| 997 | |
| 998 | f = fopen(pidfile, "r"); |
| 999 | if (!f) |
| 1000 | return 0; |
| 1001 | |
Jens Axboe | bfc3b17 | 2011-10-10 21:16:55 +0200 | [diff] [blame] | 1002 | if (fread(buf, sb.st_size, 1, f) <= 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1003 | fclose(f); |
| 1004 | return 1; |
| 1005 | } |
| 1006 | fclose(f); |
| 1007 | |
| 1008 | pid = atoi(buf); |
| 1009 | if (kill(pid, SIGCONT) < 0) |
Jens Axboe | ea5aa1b | 2011-10-11 11:45:35 +0200 | [diff] [blame] | 1010 | return errno != ESRCH; |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1011 | |
| 1012 | return 1; |
| 1013 | } |
| 1014 | |
| 1015 | static int write_pid(pid_t pid, const char *pidfile) |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1016 | { |
| 1017 | FILE *fpid; |
| 1018 | |
| 1019 | fpid = fopen(pidfile, "w"); |
| 1020 | if (!fpid) { |
| 1021 | log_err("fio: failed opening pid file %s\n", pidfile); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1022 | return 1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | fprintf(fpid, "%u\n", (unsigned int) pid); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1026 | fclose(fpid); |
| 1027 | return 0; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * If pidfile is specified, background us. |
| 1032 | */ |
| 1033 | int fio_start_server(char *pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1034 | { |
| 1035 | pid_t pid; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1036 | int ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1037 | |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1038 | if (!pidfile) |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1039 | return fio_server(); |
| 1040 | |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1041 | if (check_existing_pidfile(pidfile)) { |
| 1042 | log_err("fio: pidfile %s exists and server appears alive\n", |
| 1043 | pidfile); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1047 | pid = fork(); |
| 1048 | if (pid < 0) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1049 | log_err("fio: failed server fork: %s", strerror(errno)); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1050 | free(pidfile); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 1051 | return -1; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1052 | } else if (pid) { |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1053 | int ret = write_pid(pid, pidfile); |
| 1054 | |
| 1055 | exit(ret); |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1056 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1057 | |
| 1058 | setsid(); |
Jens Axboe | 13755d9 | 2011-10-10 19:51:26 +0200 | [diff] [blame] | 1059 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 1060 | log_syslog = 1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1061 | close(STDIN_FILENO); |
| 1062 | close(STDOUT_FILENO); |
| 1063 | close(STDERR_FILENO); |
| 1064 | f_out = NULL; |
| 1065 | f_err = NULL; |
Jens Axboe | 402668f | 2011-10-10 15:28:58 +0200 | [diff] [blame] | 1066 | |
| 1067 | ret = fio_server(); |
| 1068 | |
| 1069 | closelog(); |
| 1070 | unlink(pidfile); |
| 1071 | free(pidfile); |
| 1072 | return ret; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 1073 | } |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1074 | |
Jens Axboe | bebe639 | 2011-10-07 10:00:51 +0200 | [diff] [blame] | 1075 | void fio_server_set_arg(const char *arg) |
Jens Axboe | 87aa8f1 | 2011-10-06 21:24:13 +0200 | [diff] [blame] | 1076 | { |
| 1077 | fio_server_arg = strdup(arg); |
| 1078 | } |