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 | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 11 | #include <sys/mman.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 12 | #include <netinet/in.h> |
| 13 | #include <arpa/inet.h> |
| 14 | #include <netdb.h> |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 15 | #include <syslog.h> |
Jens Axboe | 9e22ecb | 2011-10-04 14:50:21 +0200 | [diff] [blame] | 16 | #include <signal.h> |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 17 | |
| 18 | #include "fio.h" |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 19 | #include "server.h" |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 20 | #include "crc/crc16.h" |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 21 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 22 | int fio_net_port = 8765; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 23 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 24 | int exit_backend = 0; |
| 25 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 26 | static int server_fd = -1; |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 27 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 28 | int fio_send_data(int sk, const void *p, unsigned int len) |
| 29 | { |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 30 | assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU); |
| 31 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 32 | do { |
| 33 | int ret = send(sk, p, len, 0); |
| 34 | |
| 35 | if (ret > 0) { |
| 36 | len -= ret; |
| 37 | if (!len) |
| 38 | break; |
| 39 | p += ret; |
| 40 | continue; |
| 41 | } else if (!ret) |
| 42 | break; |
| 43 | else if (errno == EAGAIN || errno == EINTR) |
| 44 | continue; |
| 45 | } while (!exit_backend); |
| 46 | |
| 47 | if (!len) |
| 48 | return 0; |
| 49 | |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | int fio_recv_data(int sk, void *p, unsigned int len) |
| 54 | { |
| 55 | do { |
| 56 | int ret = recv(sk, p, len, MSG_WAITALL); |
| 57 | |
| 58 | if (ret > 0) { |
| 59 | len -= ret; |
| 60 | if (!len) |
| 61 | break; |
| 62 | p += ret; |
| 63 | continue; |
| 64 | } else if (!ret) |
| 65 | break; |
| 66 | else if (errno == EAGAIN || errno == EINTR) |
| 67 | continue; |
| 68 | } while (!exit_backend); |
| 69 | |
| 70 | if (!len) |
| 71 | return 0; |
| 72 | |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | static int verify_convert_cmd(struct fio_net_cmd *cmd) |
| 77 | { |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 78 | uint16_t crc; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 79 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 80 | cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16); |
| 81 | cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 82 | |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 83 | crc = crc16(cmd, FIO_NET_CMD_CRC_SZ); |
| 84 | if (crc != cmd->cmd_crc16) { |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 85 | 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] | 86 | cmd->cmd_crc16, crc); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | cmd->version = le16_to_cpu(cmd->version); |
| 91 | cmd->opcode = le16_to_cpu(cmd->opcode); |
| 92 | cmd->flags = le32_to_cpu(cmd->flags); |
| 93 | cmd->serial = le64_to_cpu(cmd->serial); |
| 94 | cmd->pdu_len = le32_to_cpu(cmd->pdu_len); |
| 95 | |
| 96 | switch (cmd->version) { |
| 97 | case FIO_SERVER_VER1: |
| 98 | break; |
| 99 | default: |
| 100 | log_err("fio: bad server cmd version %d\n", cmd->version); |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | if (cmd->pdu_len > FIO_SERVER_MAX_PDU) { |
| 105 | log_err("fio: command payload too large: %u\n", cmd->pdu_len); |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 112 | /* |
| 113 | * Read (and defragment, if necessary) incoming commands |
| 114 | */ |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 115 | struct fio_net_cmd *fio_net_recv_cmd(int sk, int block) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 116 | { |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 117 | struct fio_net_cmd cmd, *cmdret = NULL; |
| 118 | size_t cmd_size = 0, pdu_offset = 0; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 119 | uint16_t crc; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 120 | int ret, first = 1; |
| 121 | void *pdu = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 122 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 123 | do { |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 124 | struct pollfd pfd; |
| 125 | |
| 126 | pfd.fd = sk; |
| 127 | pfd.events = POLLIN; |
| 128 | ret = 0; |
| 129 | do { |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 130 | int timeo = block ? 100 : 10; |
| 131 | |
| 132 | ret = poll(&pfd, 1, timeo); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 133 | if (ret < 0) { |
Jens Axboe | 5fc58b0 | 2011-10-03 21:09:14 +0200 | [diff] [blame] | 134 | if (errno == EINTR) |
| 135 | break; |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 136 | log_err("fio: poll: %s\n", strerror(errno)); |
| 137 | break; |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 138 | } else if (!ret) { |
| 139 | if (!block) |
| 140 | return NULL; |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 141 | continue; |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 142 | } |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 143 | |
| 144 | if (pfd.revents & POLLIN) |
| 145 | break; |
| 146 | if (pfd.revents & (POLLERR|POLLHUP)) { |
| 147 | ret = 1; |
| 148 | break; |
| 149 | } |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 150 | } while (ret >= 0 && block); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 151 | |
| 152 | if (ret < 0) |
| 153 | break; |
| 154 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 155 | ret = fio_recv_data(sk, &cmd, sizeof(cmd)); |
| 156 | if (ret) |
| 157 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 158 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 159 | /* We have a command, verify it and swap if need be */ |
| 160 | ret = verify_convert_cmd(&cmd); |
| 161 | if (ret) |
| 162 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 163 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 164 | if (first) { |
| 165 | /* if this is text, add room for \0 at the end */ |
| 166 | cmd_size = sizeof(cmd) + cmd.pdu_len + 1; |
| 167 | assert(!cmdret); |
| 168 | } else |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 169 | cmd_size += cmd.pdu_len; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 170 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 171 | cmdret = realloc(cmdret, cmd_size); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 172 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 173 | if (first) |
| 174 | memcpy(cmdret, &cmd, sizeof(cmd)); |
| 175 | else |
| 176 | assert(cmdret->opcode == cmd.opcode); |
| 177 | |
| 178 | if (!cmd.pdu_len) |
| 179 | break; |
| 180 | |
| 181 | /* There's payload, get it */ |
| 182 | pdu = (void *) cmdret->payload + pdu_offset; |
| 183 | ret = fio_recv_data(sk, pdu, cmd.pdu_len); |
| 184 | if (ret) |
| 185 | break; |
| 186 | |
| 187 | /* Verify payload crc */ |
| 188 | crc = crc16(pdu, cmd.pdu_len); |
| 189 | if (crc != cmd.pdu_crc16) { |
| 190 | log_err("fio: server bad crc on payload "); |
| 191 | log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc); |
| 192 | ret = 1; |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | pdu_offset += cmd.pdu_len; |
Jens Axboe | 817f06b | 2011-10-03 15:03:08 +0200 | [diff] [blame] | 197 | if (!first) |
| 198 | cmdret->pdu_len += cmd.pdu_len; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 199 | first = 0; |
| 200 | } while (cmd.flags & FIO_NET_CMD_F_MORE); |
| 201 | |
| 202 | if (ret) { |
| 203 | free(cmdret); |
| 204 | cmdret = NULL; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 205 | } else if (cmdret) { |
| 206 | /* zero-terminate text input */ |
| 207 | if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT || |
| 208 | cmdret->opcode == FIO_NET_CMD_JOB)) { |
| 209 | char *buf = (char *) cmdret->payload; |
| 210 | |
| 211 | buf[cmdret->pdu_len ] = '\0'; |
| 212 | } |
| 213 | /* frag flag is internal */ |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 214 | cmdret->flags &= ~FIO_NET_CMD_F_MORE; |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 215 | } |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 216 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 217 | return cmdret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void fio_net_cmd_crc(struct fio_net_cmd *cmd) |
| 221 | { |
| 222 | uint32_t pdu_len; |
| 223 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 224 | 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] | 225 | |
| 226 | pdu_len = le32_to_cpu(cmd->pdu_len); |
| 227 | if (pdu_len) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 228 | cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len)); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 229 | } |
| 230 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 231 | int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size) |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 232 | { |
| 233 | struct fio_net_cmd *cmd; |
| 234 | size_t this_len; |
| 235 | int ret; |
| 236 | |
| 237 | do { |
| 238 | this_len = size; |
| 239 | if (this_len > FIO_SERVER_MAX_PDU) |
| 240 | this_len = FIO_SERVER_MAX_PDU; |
| 241 | |
| 242 | cmd = malloc(sizeof(*cmd) + this_len); |
| 243 | |
| 244 | fio_init_net_cmd(cmd, opcode, buf, this_len); |
| 245 | |
| 246 | if (this_len < size) |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 247 | cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 248 | |
| 249 | fio_net_cmd_crc(cmd); |
| 250 | |
| 251 | ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); |
| 252 | free(cmd); |
| 253 | size -= this_len; |
| 254 | buf += this_len; |
| 255 | } while (!ret && size); |
| 256 | |
| 257 | return ret; |
| 258 | } |
| 259 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 260 | int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t serial) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 261 | { |
| 262 | struct fio_net_cmd cmd = { |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 263 | .version = __cpu_to_le16(FIO_SERVER_VER1), |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 264 | .opcode = cpu_to_le16(opcode), |
Jens Axboe | bdab444 | 2011-09-30 22:58:13 -0600 | [diff] [blame] | 265 | .serial = cpu_to_le64(serial), |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | fio_net_cmd_crc(&cmd); |
| 269 | |
| 270 | return fio_send_data(sk, &cmd, sizeof(cmd)); |
| 271 | } |
| 272 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 273 | static int fio_server_send_quit_cmd(void) |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 274 | { |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 275 | dprint(FD_NET, "server: sending quit\n"); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 276 | return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0); |
Jens Axboe | 437377e | 2011-10-01 08:31:30 -0600 | [diff] [blame] | 277 | } |
| 278 | |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 279 | static int handle_job_cmd(struct fio_net_cmd *cmd) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 280 | { |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 281 | char *buf = (char *) cmd->payload; |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 282 | int ret; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 283 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 284 | if (parse_jobs_ini(buf, 1, 0)) |
| 285 | return -1; |
| 286 | |
| 287 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0); |
| 288 | |
| 289 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 290 | fio_server_send_quit_cmd(); |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 291 | reset_fio_state(); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static int handle_jobline_cmd(struct fio_net_cmd *cmd) |
| 296 | { |
| 297 | struct cmd_line_pdu *pdu = (struct cmd_line_pdu *) cmd->payload; |
| 298 | char *argv[FIO_NET_CMD_JOBLINE_ARGV]; |
| 299 | int ret, i; |
| 300 | |
| 301 | pdu->argc = le16_to_cpu(pdu->argc); |
| 302 | |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 303 | dprint(FD_NET, "server: %d command line args\n", pdu->argc); |
| 304 | |
| 305 | for (i = 0; i < pdu->argc; i++) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 306 | argv[i] = (char *) pdu->argv[i]; |
Jens Axboe | 39e8e01 | 2011-10-04 13:46:08 +0200 | [diff] [blame] | 307 | dprint(FD_NET, "server: %d: %s\n", i, argv[i]); |
| 308 | } |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 309 | |
| 310 | if (parse_cmd_line(pdu->argc, argv)) |
| 311 | return -1; |
| 312 | |
| 313 | fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0); |
| 314 | |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 315 | ret = exec_run(); |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 316 | fio_server_send_quit_cmd(); |
Jens Axboe | 794d69c | 2011-10-01 08:48:50 -0600 | [diff] [blame] | 317 | reset_fio_state(); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 318 | return ret; |
| 319 | } |
| 320 | |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 321 | static int handle_probe_cmd(struct fio_net_cmd *cmd) |
| 322 | { |
| 323 | struct cmd_probe_pdu probe; |
| 324 | |
| 325 | memset(&probe, 0, sizeof(probe)); |
| 326 | gethostname((char *) probe.hostname, sizeof(probe.hostname)); |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 327 | #ifdef FIO_BIG_ENDIAN |
| 328 | probe.bigendian = 1; |
| 329 | #endif |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 330 | probe.fio_major = FIO_MAJOR; |
| 331 | probe.fio_minor = FIO_MINOR; |
| 332 | probe.fio_patch = FIO_PATCH; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 333 | |
| 334 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe)); |
| 335 | } |
| 336 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 337 | static int handle_command(struct fio_net_cmd *cmd) |
| 338 | { |
| 339 | int ret; |
| 340 | |
Jens Axboe | 46c48f1 | 2011-10-01 12:50:51 -0600 | [diff] [blame] | 341 | dprint(FD_NET, "server: got opcode %d\n", cmd->opcode); |
| 342 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 343 | switch (cmd->opcode) { |
| 344 | case FIO_NET_CMD_QUIT: |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 345 | fio_terminate_threads(TERMINATE_ALL); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 346 | return -1; |
Jens Axboe | d795918 | 2011-10-03 11:48:39 +0200 | [diff] [blame] | 347 | case FIO_NET_CMD_EXIT: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 348 | exit_backend = 1; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 349 | return -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 350 | case FIO_NET_CMD_JOB: |
Jens Axboe | 0b8f30a | 2011-10-04 10:31:53 +0200 | [diff] [blame] | 351 | ret = handle_job_cmd(cmd); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 352 | break; |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 353 | case FIO_NET_CMD_JOBLINE: |
| 354 | ret = handle_jobline_cmd(cmd); |
| 355 | break; |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 356 | case FIO_NET_CMD_PROBE: |
| 357 | ret = handle_probe_cmd(cmd); |
| 358 | break; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 359 | default: |
| 360 | log_err("fio: unknown opcode: %d\n", cmd->opcode); |
| 361 | ret = 1; |
| 362 | } |
| 363 | |
| 364 | return ret; |
| 365 | } |
| 366 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 367 | static int handle_connection(int sk, int block) |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 368 | { |
| 369 | struct fio_net_cmd *cmd = NULL; |
| 370 | int ret = 0; |
| 371 | |
| 372 | /* read forever */ |
| 373 | while (!exit_backend) { |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 374 | cmd = fio_net_recv_cmd(sk, block); |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 375 | if (!cmd) { |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 376 | ret = -1; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 380 | ret = handle_command(cmd); |
| 381 | if (ret) |
| 382 | break; |
| 383 | |
| 384 | free(cmd); |
Jens Axboe | c77a99e | 2011-10-01 16:26:42 -0400 | [diff] [blame] | 385 | cmd = NULL; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if (cmd) |
| 389 | free(cmd); |
| 390 | |
| 391 | return ret; |
| 392 | } |
| 393 | |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 394 | void fio_server_idle_loop(void) |
| 395 | { |
| 396 | if (server_fd != -1) |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 397 | handle_connection(server_fd, 0); |
Jens Axboe | cc0df00 | 2011-10-03 20:53:32 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 400 | static int accept_loop(int listen_sk) |
| 401 | { |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 402 | struct sockaddr_in addr; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 403 | unsigned int len = sizeof(addr); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 404 | struct pollfd pfd; |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 405 | int ret, sk, flags, exitval = 0; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 406 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 407 | dprint(FD_NET, "server enter accept loop\n"); |
| 408 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 409 | flags = fcntl(listen_sk, F_GETFL); |
| 410 | flags |= O_NONBLOCK; |
| 411 | fcntl(listen_sk, F_SETFL, flags); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 412 | again: |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 413 | pfd.fd = listen_sk; |
| 414 | pfd.events = POLLIN; |
| 415 | do { |
| 416 | ret = poll(&pfd, 1, 100); |
| 417 | if (ret < 0) { |
| 418 | if (errno == EINTR) |
| 419 | break; |
Jens Axboe | fcee5ff | 2011-09-30 22:50:39 -0600 | [diff] [blame] | 420 | log_err("fio: poll: %s\n", strerror(errno)); |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 421 | goto out; |
| 422 | } else if (!ret) |
| 423 | continue; |
| 424 | |
| 425 | if (pfd.revents & POLLIN) |
| 426 | break; |
| 427 | } while (!exit_backend); |
| 428 | |
| 429 | if (exit_backend) |
| 430 | goto out; |
| 431 | |
Jens Axboe | 6b976bf | 2011-10-04 15:07:43 +0200 | [diff] [blame] | 432 | sk = accept(listen_sk, (struct sockaddr *) &addr, &len); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 433 | if (sk < 0) { |
Jens Axboe | 690e09a | 2011-09-29 18:38:08 -0600 | [diff] [blame] | 434 | log_err("fio: accept: %s\n", strerror(errno)); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 435 | return -1; |
| 436 | } |
| 437 | |
Jens Axboe | bb447a2 | 2011-10-04 15:06:42 +0200 | [diff] [blame] | 438 | 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] | 439 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 440 | server_fd = sk; |
| 441 | |
Jens Axboe | 70e0c31 | 2011-10-04 09:18:30 +0200 | [diff] [blame] | 442 | exitval = handle_connection(sk, 1); |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 443 | |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 444 | server_fd = -1; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 445 | close(sk); |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 446 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 447 | if (!exit_backend) |
Jens Axboe | 5c341e9 | 2011-09-29 18:00:35 -0600 | [diff] [blame] | 448 | goto again; |
| 449 | |
Jens Axboe | 009b1be | 2011-09-29 18:27:02 -0600 | [diff] [blame] | 450 | out: |
Jens Axboe | 132159a | 2011-09-30 15:01:32 -0600 | [diff] [blame] | 451 | return exitval; |
Jens Axboe | 50d1697 | 2011-09-29 17:45:28 -0600 | [diff] [blame] | 452 | } |
| 453 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 454 | int fio_server_text_output(const char *buf, unsigned int len) |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 455 | { |
Jens Axboe | 337d75a | 2011-10-01 12:36:32 -0600 | [diff] [blame] | 456 | if (server_fd != -1) |
| 457 | return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len); |
| 458 | |
Jens Axboe | 5037b84 | 2011-10-04 17:32:16 +0200 | [diff] [blame] | 459 | return fwrite(buf, len, 1, f_err); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 460 | } |
| 461 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 462 | static void convert_io_stat(struct io_stat *dst, struct io_stat *src) |
| 463 | { |
| 464 | dst->max_val = cpu_to_le64(src->max_val); |
| 465 | dst->min_val = cpu_to_le64(src->min_val); |
| 466 | dst->samples = cpu_to_le64(src->samples); |
| 467 | /* FIXME */ |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 468 | dst->mean = __cpu_to_le64(src->mean); |
| 469 | dst->S = __cpu_to_le64(src->S); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src) |
| 473 | { |
| 474 | int i; |
| 475 | |
| 476 | for (i = 0; i < 2; i++) { |
| 477 | dst->max_run[i] = cpu_to_le64(src->max_run[i]); |
| 478 | dst->min_run[i] = cpu_to_le64(src->min_run[i]); |
| 479 | dst->max_bw[i] = cpu_to_le64(src->max_bw[i]); |
| 480 | dst->min_bw[i] = cpu_to_le64(src->min_bw[i]); |
| 481 | dst->io_kb[i] = cpu_to_le64(src->io_kb[i]); |
| 482 | dst->agg[i] = cpu_to_le64(src->agg[i]); |
| 483 | } |
| 484 | |
| 485 | dst->kb_base = cpu_to_le32(src->kb_base); |
| 486 | dst->groupid = cpu_to_le32(src->groupid); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Send a CMD_TS, which packs struct thread_stat and group_run_stats |
| 491 | * into a single payload. |
| 492 | */ |
| 493 | void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs) |
| 494 | { |
| 495 | struct cmd_ts_pdu p; |
| 496 | int i, j; |
| 497 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 498 | dprint(FD_NET, "server sending end stats\n"); |
| 499 | |
Jens Axboe | 317b3c8 | 2011-10-03 21:47:27 +0200 | [diff] [blame] | 500 | memset(&p, 0, sizeof(p)); |
| 501 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 502 | strcpy(p.ts.name, ts->name); |
| 503 | strcpy(p.ts.verror, ts->verror); |
| 504 | strcpy(p.ts.description, ts->description); |
| 505 | |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 506 | p.ts.error = cpu_to_le32(ts->error); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 507 | p.ts.groupid = cpu_to_le32(ts->groupid); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 508 | p.ts.pid = cpu_to_le32(ts->pid); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 509 | p.ts.members = cpu_to_le32(ts->members); |
| 510 | |
| 511 | for (i = 0; i < 2; i++) { |
| 512 | convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]); |
| 513 | convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]); |
| 514 | convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]); |
| 515 | convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]); |
| 516 | } |
| 517 | |
| 518 | p.ts.usr_time = cpu_to_le64(ts->usr_time); |
| 519 | p.ts.sys_time = cpu_to_le64(ts->sys_time); |
| 520 | p.ts.ctx = cpu_to_le64(ts->ctx); |
| 521 | p.ts.minf = cpu_to_le64(ts->minf); |
| 522 | p.ts.majf = cpu_to_le64(ts->majf); |
| 523 | p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles); |
| 524 | p.ts.percentile_list = NULL; |
| 525 | |
| 526 | for (i = 0; i < FIO_IO_U_MAP_NR; i++) { |
| 527 | p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]); |
| 528 | p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]); |
| 529 | p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]); |
| 530 | } |
| 531 | |
| 532 | for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) { |
| 533 | p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]); |
| 534 | p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]); |
| 535 | } |
| 536 | |
| 537 | for (i = 0; i < 2; i++) |
| 538 | for (j = 0; j < FIO_IO_U_PLAT_NR; j++) |
| 539 | p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]); |
| 540 | |
| 541 | for (i = 0; i < 3; i++) { |
| 542 | 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] | 543 | 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] | 544 | } |
| 545 | |
Jens Axboe | 93eee04 | 2011-10-03 21:08:48 +0200 | [diff] [blame] | 546 | p.ts.total_submit = cpu_to_le64(ts->total_submit); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 547 | p.ts.total_complete = cpu_to_le64(ts->total_complete); |
| 548 | |
| 549 | for (i = 0; i < 2; i++) { |
| 550 | p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]); |
| 551 | p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]); |
| 552 | } |
| 553 | |
| 554 | p.ts.total_run_time = cpu_to_le64(ts->total_run_time); |
| 555 | p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error); |
| 556 | p.ts.total_err_count = cpu_to_le64(ts->total_err_count); |
Jens Axboe | ddcc0b6 | 2011-10-03 14:45:27 +0200 | [diff] [blame] | 557 | p.ts.first_error = cpu_to_le32(ts->first_error); |
| 558 | p.ts.kb_base = cpu_to_le32(ts->kb_base); |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 559 | |
| 560 | convert_gs(&p.rs, rs); |
| 561 | |
| 562 | fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p)); |
| 563 | } |
| 564 | |
| 565 | void fio_server_send_gs(struct group_run_stats *rs) |
| 566 | { |
| 567 | struct group_run_stats gs; |
| 568 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 569 | dprint(FD_NET, "server sending group run stats\n"); |
| 570 | |
Jens Axboe | a64e88d | 2011-10-03 14:20:01 +0200 | [diff] [blame] | 571 | convert_gs(&gs, rs); |
| 572 | fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs)); |
| 573 | } |
| 574 | |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 575 | void fio_server_send_status(void) |
| 576 | { |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 577 | struct jobs_eta *je; |
| 578 | size_t size; |
| 579 | void *buf; |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 580 | int i; |
| 581 | |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 582 | size = sizeof(*je) + thread_number * sizeof(char); |
| 583 | buf = malloc(size); |
| 584 | memset(buf, 0, size); |
| 585 | je = buf; |
| 586 | |
| 587 | if (!calc_thread_status(je)) { |
| 588 | free(je); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 589 | return; |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 592 | dprint(FD_NET, "server sending status\n"); |
| 593 | |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 594 | je->nr_running = cpu_to_le32(je->nr_running); |
| 595 | je->nr_ramp = cpu_to_le32(je->nr_ramp); |
| 596 | je->nr_pending = cpu_to_le32(je->nr_pending); |
| 597 | je->files_open = cpu_to_le32(je->files_open); |
| 598 | je->m_rate = cpu_to_le32(je->m_rate); |
| 599 | je->t_rate = cpu_to_le32(je->t_rate); |
| 600 | je->m_iops = cpu_to_le32(je->m_iops); |
| 601 | je->t_iops = cpu_to_le32(je->t_iops); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 602 | |
Jens Axboe | 1d1f45a | 2011-10-03 19:44:41 +0200 | [diff] [blame] | 603 | for (i = 0; i < 2; i++) { |
| 604 | je->rate[i] = cpu_to_le32(je->rate[i]); |
| 605 | je->iops[i] = cpu_to_le32(je->iops[i]); |
| 606 | } |
| 607 | |
| 608 | je->elapsed_sec = cpu_to_le32(je->nr_running); |
| 609 | je->eta_sec = cpu_to_le64(je->eta_sec); |
| 610 | |
| 611 | fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, buf, size); |
| 612 | free(je); |
Jens Axboe | cf451d1 | 2011-10-03 16:48:30 +0200 | [diff] [blame] | 613 | } |
| 614 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 615 | int fio_server_log(const char *format, ...) |
| 616 | { |
| 617 | char buffer[1024]; |
| 618 | va_list args; |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 619 | size_t len; |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 620 | |
Jens Axboe | 60efd14 | 2011-10-04 13:30:11 +0200 | [diff] [blame] | 621 | dprint(FD_NET, "server log\n"); |
| 622 | |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 623 | va_start(args, format); |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 624 | len = vsnprintf(buffer, sizeof(buffer), format, args); |
Jens Axboe | 142575e | 2011-09-30 17:35:45 -0600 | [diff] [blame] | 625 | va_end(args); |
| 626 | |
Jens Axboe | 82fa6b2 | 2011-09-30 22:29:08 -0600 | [diff] [blame] | 627 | return fio_server_text_output(buffer, len); |
Jens Axboe | 37db14f | 2011-09-30 17:00:42 -0600 | [diff] [blame] | 628 | } |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 629 | |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 630 | static int fio_server(void) |
| 631 | { |
| 632 | struct sockaddr_in saddr_in; |
| 633 | struct sockaddr addr; |
| 634 | unsigned int len; |
| 635 | int sk, opt, ret; |
| 636 | |
| 637 | dprint(FD_NET, "starting server\n"); |
| 638 | |
| 639 | sk = socket(AF_INET, SOCK_STREAM, 0); |
| 640 | if (sk < 0) { |
| 641 | log_err("fio: socket: %s\n", strerror(errno)); |
| 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | opt = 1; |
| 646 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) { |
| 647 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
| 648 | return -1; |
| 649 | } |
| 650 | #ifdef SO_REUSEPORT |
Jens Axboe | 6eb2479 | 2011-10-04 15:00:30 +0200 | [diff] [blame] | 651 | if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) { |
Jens Axboe | 81179ee | 2011-10-04 12:42:06 +0200 | [diff] [blame] | 652 | log_err("fio: setsockopt: %s\n", strerror(errno)); |
| 653 | return -1; |
| 654 | } |
| 655 | #endif |
| 656 | |
| 657 | saddr_in.sin_family = AF_INET; |
| 658 | saddr_in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 659 | saddr_in.sin_port = htons(fio_net_port); |
| 660 | |
| 661 | if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) { |
| 662 | log_err("fio: bind: %s\n", strerror(errno)); |
| 663 | return -1; |
| 664 | } |
| 665 | |
| 666 | if (listen(sk, 1) < 0) { |
| 667 | log_err("fio: listen: %s\n", strerror(errno)); |
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | len = sizeof(addr); |
| 672 | if (getsockname(sk, &addr, &len) < 0) { |
| 673 | log_err("fio: getsockname: %s\n", strerror(errno)); |
| 674 | return -1; |
| 675 | } |
| 676 | |
| 677 | ret = accept_loop(sk); |
| 678 | close(sk); |
| 679 | return ret; |
| 680 | } |
| 681 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 682 | static void sig_int(int sig) |
| 683 | { |
| 684 | fio_terminate_threads(TERMINATE_ALL); |
| 685 | exit_backend = 1; |
| 686 | } |
| 687 | |
| 688 | static void server_signal_handler(void) |
| 689 | { |
| 690 | struct sigaction act; |
| 691 | |
| 692 | memset(&act, 0, sizeof(act)); |
| 693 | act.sa_handler = sig_int; |
| 694 | act.sa_flags = SA_RESTART; |
| 695 | sigaction(SIGINT, &act, NULL); |
| 696 | |
| 697 | memset(&act, 0, sizeof(act)); |
| 698 | act.sa_handler = sig_int; |
| 699 | act.sa_flags = SA_RESTART; |
| 700 | sigaction(SIGTERM, &act, NULL); |
| 701 | } |
| 702 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 703 | int fio_start_server(int daemonize) |
| 704 | { |
| 705 | pid_t pid; |
| 706 | |
Jens Axboe | 9abea48 | 2011-10-04 13:21:54 +0200 | [diff] [blame] | 707 | server_signal_handler(); |
| 708 | |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 709 | if (!daemonize) |
| 710 | return fio_server(); |
| 711 | |
| 712 | openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER); |
| 713 | pid = fork(); |
| 714 | if (pid < 0) { |
| 715 | syslog(LOG_ERR, "failed server fork"); |
Jens Axboe | c28e8e8 | 2011-10-04 08:57:39 +0200 | [diff] [blame] | 716 | return -1; |
Jens Axboe | e46d809 | 2011-10-03 09:11:02 +0200 | [diff] [blame] | 717 | } else if (pid) |
| 718 | exit(0); |
| 719 | |
| 720 | setsid(); |
| 721 | close(STDIN_FILENO); |
| 722 | close(STDOUT_FILENO); |
| 723 | close(STDERR_FILENO); |
| 724 | f_out = NULL; |
| 725 | f_err = NULL; |
| 726 | log_syslog = 1; |
| 727 | return fio_server(); |
| 728 | } |