blob: 2c20e37b7e053f60d3528d4909b82d721edd2032 [file] [log] [blame]
Jens Axboe50d16972011-09-29 17:45:28 -06001#include <stdio.h>
2#include <stdlib.h>
Jens Axboe142575e2011-09-30 17:35:45 -06003#include <stdarg.h>
Jens Axboe50d16972011-09-29 17:45:28 -06004#include <unistd.h>
5#include <limits.h>
Jens Axboe50d16972011-09-29 17:45:28 -06006#include <errno.h>
7#include <fcntl.h>
8#include <sys/poll.h>
Jens Axboe50d16972011-09-29 17:45:28 -06009#include <sys/types.h>
10#include <sys/wait.h>
Jens Axboed05c4a02011-10-05 00:16:11 +020011#include <sys/socket.h>
Jens Axboe87aa8f12011-10-06 21:24:13 +020012#include <sys/stat.h>
13#include <sys/un.h>
Jens Axboe50d16972011-09-29 17:45:28 -060014#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
Jens Axboee46d8092011-10-03 09:11:02 +020017#include <syslog.h>
Jens Axboe9e22ecb2011-10-04 14:50:21 +020018#include <signal.h>
Jens Axboe50d16972011-09-29 17:45:28 -060019
20#include "fio.h"
Jens Axboe132159a2011-09-30 15:01:32 -060021#include "server.h"
Jens Axboefcee5ff2011-09-30 22:50:39 -060022#include "crc/crc16.h"
Jens Axboe802ad4a2011-10-05 09:51:58 +020023#include "ieee754.h"
Jens Axboe50d16972011-09-29 17:45:28 -060024
Jens Axboe89cf1482011-10-07 10:10:18 +020025#include "fio_version.h"
26
Jens Axboe132159a2011-09-30 15:01:32 -060027int fio_net_port = 8765;
Jens Axboe50d16972011-09-29 17:45:28 -060028
Jens Axboe009b1be2011-09-29 18:27:02 -060029int exit_backend = 0;
30
Jens Axboe46c48f12011-10-01 12:50:51 -060031static int server_fd = -1;
Jens Axboe87aa8f12011-10-06 21:24:13 +020032static char *fio_server_arg;
33static char *bind_sock;
34static struct sockaddr_in saddr_in;
Jens Axboe37db14f2011-09-30 17:00:42 -060035
Jens Axboe89c17072011-10-11 10:15:51 +020036static 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
52const 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 Axboe132159a2011-09-30 15:01:32 -060063int fio_send_data(int sk, const void *p, unsigned int len)
64{
Jens Axboe794d69c2011-10-01 08:48:50 -060065 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
66
Jens Axboe132159a2011-09-30 15:01:32 -060067 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 Axboe7b821682011-10-11 12:16:32 +020080 else
81 break;
Jens Axboe132159a2011-09-30 15:01:32 -060082 } while (!exit_backend);
83
84 if (!len)
85 return 0;
86
87 return 1;
88}
89
90int 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 Axboe7b821682011-10-11 12:16:32 +0200105 else
106 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600107 } while (!exit_backend);
108
109 if (!len)
110 return 0;
111
112 return -1;
113}
114
115static int verify_convert_cmd(struct fio_net_cmd *cmd)
116{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600117 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600118
Jens Axboefcee5ff2011-09-30 22:50:39 -0600119 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
120 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600121
Jens Axboefcee5ff2011-09-30 22:50:39 -0600122 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
123 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600124 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600125 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600126 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 Axboeaf9c9fb2011-10-09 21:54:10 +0200132 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600133 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
134
135 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200136 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600137 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 Axboea64e88d2011-10-03 14:20:01 +0200151/*
152 * Read (and defragment, if necessary) incoming commands
153 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200154struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600155{
Jens Axboea64e88d2011-10-03 14:20:01 +0200156 struct fio_net_cmd cmd, *cmdret = NULL;
157 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600158 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200159 int ret, first = 1;
160 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600161
Jens Axboea64e88d2011-10-03 14:20:01 +0200162 do {
163 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
164 if (ret)
165 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600166
Jens Axboea64e88d2011-10-03 14:20:01 +0200167 /* We have a command, verify it and swap if need be */
168 ret = verify_convert_cmd(&cmd);
169 if (ret)
170 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600171
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200172 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 Axboea64e88d2011-10-03 14:20:01 +0200177 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600178
Jens Axboea64e88d2011-10-03 14:20:01 +0200179 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600180
Jens Axboea64e88d2011-10-03 14:20:01 +0200181 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 Axboe817f06b2011-10-03 15:03:08 +0200205 if (!first)
206 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200207 first = 0;
208 } while (cmd.flags & FIO_NET_CMD_F_MORE);
209
210 if (ret) {
211 free(cmdret);
212 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200213 } 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 Axboea64e88d2011-10-03 14:20:01 +0200222 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200223 }
Jens Axboe132159a2011-09-30 15:01:32 -0600224
Jens Axboea64e88d2011-10-03 14:20:01 +0200225 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600226}
227
228void fio_net_cmd_crc(struct fio_net_cmd *cmd)
229{
230 uint32_t pdu_len;
231
Jens Axboeddcc0b62011-10-03 14:45:27 +0200232 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600233
234 pdu_len = le32_to_cpu(cmd->pdu_len);
235 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200236 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600237}
238
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200239int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
240 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600241{
Jens Axboe7f868312011-10-10 09:55:21 +0200242 struct fio_net_cmd *cmd = NULL;
243 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600244 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 Axboe7f868312011-10-10 09:55:21 +0200251 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 Axboe794d69c2011-10-01 08:48:50 -0600258
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200259 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600260
261 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200262 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600263
264 fio_net_cmd_crc(cmd);
265
266 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600267 size -= this_len;
268 buf += this_len;
269 } while (!ret && size);
270
Jens Axboe7f868312011-10-10 09:55:21 +0200271 if (cmd)
272 free(cmd);
273
Jens Axboe794d69c2011-10-01 08:48:50 -0600274 return ret;
275}
276
Jens Axboe89c17072011-10-11 10:15:51 +0200277static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600278{
Jens Axboe178cde92011-10-05 22:14:31 +0200279 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600280
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200281 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600282 fio_net_cmd_crc(&cmd);
283
284 return fio_send_data(sk, &cmd, sizeof(cmd));
285}
286
Jens Axboe89c17072011-10-11 10:15:51 +0200287/*
288 * If 'list' is non-NULL, then allocate and store the sent command for
289 * later verification.
290 */
291int 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 Axboedf380932011-10-11 14:25:08 +0200302 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uintptr_t) cmd);
Jens Axboe89c17072011-10-11 10:15:51 +0200303 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 Axboe9abea482011-10-04 13:21:54 +0200319static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600320{
Jens Axboe46c48f12011-10-01 12:50:51 -0600321 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200322 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600323}
324
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200325static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600326{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200327 char *buf = (char *) cmd->payload;
Jens Axboea64e88d2011-10-03 14:20:01 +0200328 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600329
Jens Axboee6d1c662011-10-05 20:41:06 +0200330 if (parse_jobs_ini(buf, 1, 0)) {
331 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200332 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200333 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200334
Jens Axboe89c17072011-10-11 10:15:51 +0200335 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200336
337 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200338 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200339 reset_fio_state();
340 return ret;
341}
342
343static int handle_jobline_cmd(struct fio_net_cmd *cmd)
344{
Jens Axboefa2ea802011-10-08 21:07:29 +0200345 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 Axboe81179ee2011-10-04 12:42:06 +0200350 int ret, i;
351
Jens Axboefa2ea802011-10-08 21:07:29 +0200352 clp = pdu;
353 clp->lines = le16_to_cpu(clp->lines);
354 argv = malloc(clp->lines * sizeof(char *));
355 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200356
Jens Axboefa2ea802011-10-08 21:07:29 +0200357 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200358
Jens Axboefa2ea802011-10-08 21:07:29 +0200359 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 Axboe39e8e012011-10-04 13:46:08 +0200364 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
365 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200366
Jens Axboefa2ea802011-10-08 21:07:29 +0200367 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200368 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200369 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200370 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200371 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200372
Jens Axboefa2ea802011-10-08 21:07:29 +0200373 free(argv);
374
Jens Axboe89c17072011-10-11 10:15:51 +0200375 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200376
Jens Axboe794d69c2011-10-01 08:48:50 -0600377 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200378 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600379 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600380 return ret;
381}
382
Jens Axboec28e8e82011-10-04 08:57:39 +0200383static int handle_probe_cmd(struct fio_net_cmd *cmd)
384{
385 struct cmd_probe_pdu probe;
386
Jens Axboe89c17072011-10-11 10:15:51 +0200387 dprint(FD_NET, "server: sending probe reply\n");
388
Jens Axboec28e8e82011-10-04 08:57:39 +0200389 memset(&probe, 0, sizeof(probe));
390 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200391#ifdef FIO_BIG_ENDIAN
392 probe.bigendian = 1;
393#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200394 probe.fio_major = FIO_MAJOR;
395 probe.fio_minor = FIO_MINOR;
396 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200397
Jens Axboecca84642011-10-07 12:47:57 +0200398 probe.os = FIO_OS;
399 probe.arch = FIO_ARCH;
400
Jens Axboe38fdef22011-10-11 14:30:06 +0200401 probe.bpp = sizeof(void *);
402
Jens Axboe89c17072011-10-11 10:15:51 +0200403 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200404}
405
406static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
407{
408 struct jobs_eta *je;
409 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200410 int i;
411
Jens Axboeb814fb22011-10-12 09:20:34 +0200412 if (!thread_number)
413 return 0;
414
415 size = sizeof(*je) + thread_number * sizeof(char) + 1;
Jens Axboe7f868312011-10-10 09:55:21 +0200416 je = malloc(size);
417 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200418
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 Axboe7f868312011-10-10 09:55:21 +0200443 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200444 free(je);
445 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200446}
447
Jens Axboe132159a2011-09-30 15:01:32 -0600448static int handle_command(struct fio_net_cmd *cmd)
449{
450 int ret;
451
Jens Axboe89c17072011-10-11 10:15:51 +0200452 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
453 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600454
Jens Axboe132159a2011-09-30 15:01:32 -0600455 switch (cmd->opcode) {
456 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200457 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200458 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200459 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600460 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200461 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600462 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200463 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600464 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200465 case FIO_NET_CMD_JOBLINE:
466 ret = handle_jobline_cmd(cmd);
467 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200468 case FIO_NET_CMD_PROBE:
469 ret = handle_probe_cmd(cmd);
470 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200471 case FIO_NET_CMD_SEND_ETA:
472 ret = handle_send_eta_cmd(cmd);
473 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600474 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200475 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600476 ret = 1;
477 }
478
479 return ret;
480}
481
Jens Axboe70e0c312011-10-04 09:18:30 +0200482static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600483{
484 struct fio_net_cmd *cmd = NULL;
485 int ret = 0;
486
487 /* read forever */
488 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200489 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 Axboe19c65172011-10-05 22:05:37 +0200502 } else if (!ret) {
503 if (!block)
504 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200505 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200506 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200507
508 if (pfd.revents & POLLIN)
509 break;
510 if (pfd.revents & (POLLERR|POLLHUP)) {
511 ret = 1;
512 break;
513 }
Jens Axboe19c65172011-10-05 22:05:37 +0200514 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200515
516 if (ret < 0)
517 break;
518
519 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600520 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200521 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600522 break;
523 }
524
Jens Axboe132159a2011-09-30 15:01:32 -0600525 ret = handle_command(cmd);
526 if (ret)
527 break;
528
529 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400530 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600531 }
532
533 if (cmd)
534 free(cmd);
535
536 return ret;
537}
538
Jens Axboecc0df002011-10-03 20:53:32 +0200539void fio_server_idle_loop(void)
540{
541 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200542 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200543}
544
Jens Axboe50d16972011-09-29 17:45:28 -0600545static int accept_loop(int listen_sk)
546{
Jens Axboebb447a22011-10-04 15:06:42 +0200547 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200548 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600549 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600550 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600551
Jens Axboe60efd142011-10-04 13:30:11 +0200552 dprint(FD_NET, "server enter accept loop\n");
553
Jens Axboe009b1be2011-09-29 18:27:02 -0600554 flags = fcntl(listen_sk, F_GETFL);
555 flags |= O_NONBLOCK;
556 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600557again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600558 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 Axboefcee5ff2011-09-30 22:50:39 -0600565 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600566 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 Axboe6b976bf2011-10-04 15:07:43 +0200577 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600578 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600579 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600580 return -1;
581 }
582
Jens Axboebb447a22011-10-04 15:06:42 +0200583 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600584
Jens Axboe37db14f2011-09-30 17:00:42 -0600585 server_fd = sk;
586
Jens Axboe70e0c312011-10-04 09:18:30 +0200587 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600588
Jens Axboe37db14f2011-09-30 17:00:42 -0600589 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600590 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600591
Jens Axboe009b1be2011-09-29 18:27:02 -0600592 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600593 goto again;
594
Jens Axboe009b1be2011-09-29 18:27:02 -0600595out:
Jens Axboe132159a2011-09-30 15:01:32 -0600596 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600597}
598
Jens Axboe13755d92011-10-10 19:51:26 +0200599int fio_server_text_output(const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600600{
Jens Axboe337d75a2011-10-01 12:36:32 -0600601 if (server_fd != -1)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200602 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
Jens Axboe337d75a2011-10-01 12:36:32 -0600603
Jens Axboe13755d92011-10-10 19:51:26 +0200604 return log_local_buf(buf, len);
Jens Axboe142575e2011-09-30 17:35:45 -0600605}
606
Jens Axboea64e88d2011-10-03 14:20:01 +0200607static 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 Axboe802ad4a2011-10-05 09:51:58 +0200612
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 Axboea64e88d2011-10-03 14:20:01 +0200618}
619
620static 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 */
641void 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 Axboe60efd142011-10-04 13:30:11 +0200646 dprint(FD_NET, "server sending end stats\n");
647
Jens Axboe317b3c82011-10-03 21:47:27 +0200648 memset(&p, 0, sizeof(p));
649
Jens Axboea64e88d2011-10-03 14:20:01 +0200650 strcpy(p.ts.name, ts->name);
651 strcpy(p.ts.verror, ts->verror);
652 strcpy(p.ts.description, ts->description);
653
Jens Axboeddcc0b62011-10-03 14:45:27 +0200654 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200655 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200656 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200657 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 Axboe802ad4a2011-10-05 09:51:58 +0200672
673 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
674 fio_fp64_t *fp = &p.ts.percentile_list[i];
675
676 fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f));
677 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200678
679 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
680 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
681 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
682 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
683 }
684
685 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
686 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
687 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
688 }
689
690 for (i = 0; i < 2; i++)
691 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
692 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
693
694 for (i = 0; i < 3; i++) {
695 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200696 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200697 }
698
Jens Axboe93eee042011-10-03 21:08:48 +0200699 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200700 p.ts.total_complete = cpu_to_le64(ts->total_complete);
701
702 for (i = 0; i < 2; i++) {
703 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
704 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
705 }
706
707 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
708 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
709 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200710 p.ts.first_error = cpu_to_le32(ts->first_error);
711 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200712
713 convert_gs(&p.rs, rs);
714
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200715 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200716}
717
718void fio_server_send_gs(struct group_run_stats *rs)
719{
720 struct group_run_stats gs;
721
Jens Axboe60efd142011-10-04 13:30:11 +0200722 dprint(FD_NET, "server sending group run stats\n");
723
Jens Axboea64e88d2011-10-03 14:20:01 +0200724 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200725 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200726}
727
Jens Axboe142575e2011-09-30 17:35:45 -0600728int fio_server_log(const char *format, ...)
729{
730 char buffer[1024];
731 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600732 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600733
Jens Axboe60efd142011-10-04 13:30:11 +0200734 dprint(FD_NET, "server log\n");
735
Jens Axboe142575e2011-09-30 17:35:45 -0600736 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600737 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600738 va_end(args);
739
Jens Axboe82fa6b22011-09-30 22:29:08 -0600740 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600741}
Jens Axboee46d8092011-10-03 09:11:02 +0200742
Jens Axboe87aa8f12011-10-06 21:24:13 +0200743static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200744{
Jens Axboe87aa8f12011-10-06 21:24:13 +0200745 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200746
747 sk = socket(AF_INET, SOCK_STREAM, 0);
748 if (sk < 0) {
749 log_err("fio: socket: %s\n", strerror(errno));
750 return -1;
751 }
752
753 opt = 1;
754 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
755 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200756 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200757 return -1;
758 }
759#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200760 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200761 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200762 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200763 return -1;
764 }
765#endif
766
767 saddr_in.sin_family = AF_INET;
Jens Axboe81179ee2011-10-04 12:42:06 +0200768
769 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
770 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200771 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200772 return -1;
773 }
774
Jens Axboe87aa8f12011-10-06 21:24:13 +0200775 return sk;
776}
777
778static int fio_init_server_sock(void)
779{
780 struct sockaddr_un addr;
781 fio_socklen_t len;
782 mode_t mode;
783 int sk;
784
785 sk = socket(AF_UNIX, SOCK_STREAM, 0);
786 if (sk < 0) {
787 log_err("fio: socket: %s\n", strerror(errno));
788 return -1;
789 }
790
791 mode = umask(000);
792
793 memset(&addr, 0, sizeof(addr));
794 addr.sun_family = AF_UNIX;
795 strcpy(addr.sun_path, bind_sock);
796 unlink(bind_sock);
797
798 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
799
800 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
801 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200802 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200803 return -1;
804 }
805
806 umask(mode);
807 return sk;
808}
809
810static int fio_init_server_connection(void)
811{
Jens Axboebebe6392011-10-07 10:00:51 +0200812 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200813 int sk;
814
815 dprint(FD_NET, "starting server\n");
816
817 if (!bind_sock)
818 sk = fio_init_server_ip();
819 else
820 sk = fio_init_server_sock();
821
822 if (sk < 0)
823 return sk;
824
Jens Axboebebe6392011-10-07 10:00:51 +0200825 if (!bind_sock)
826 sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port);
827 else
828 strcpy(bind_str, bind_sock);
829
830 log_info("fio: server listening on %s\n", bind_str);
831
Jens Axboe89c17072011-10-11 10:15:51 +0200832 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200833 log_err("fio: listen: %s\n", strerror(errno));
834 return -1;
835 }
836
Jens Axboe87aa8f12011-10-06 21:24:13 +0200837 return sk;
838}
839
Jens Axboebebe6392011-10-07 10:00:51 +0200840int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
841 int *port, struct in_addr *inp)
842{
843 *ptr = NULL;
844 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +0200845 *port = fio_net_port;
Jens Axboebebe6392011-10-07 10:00:51 +0200846
847 if (!strncmp(str, "sock:", 5)) {
848 *ptr = strdup(str + 5);
849 *is_sock = 1;
850 } else {
851 const char *host = str;
852 char *portp;
853 int lport = 0;
854
855 /*
856 * Is it ip:<ip or host>:port
857 */
858 if (!strncmp(host, "ip:", 3))
859 host += 3;
860 else if (host[0] == ':') {
861 /* String is :port */
862 host++;
863 lport = atoi(host);
864 if (!lport || lport > 65535) {
865 log_err("fio: bad server port %u\n", port);
866 return 1;
867 }
868 /* no hostname given, we are done */
869 *port = lport;
870 return 0;
871 }
872
873 /*
874 * If no port seen yet, check if there's a last ':' at the end
875 */
876 if (!lport) {
877 portp = strchr(host, ':');
878 if (portp) {
879 *portp = '\0';
880 portp++;
881 lport = atoi(portp);
882 if (!lport || lport > 65535) {
883 log_err("fio: bad server port %u\n", port);
884 return 1;
885 }
886 }
887 }
888
889 if (lport)
890 *port = lport;
891
892 *ptr = strdup(host);
893
894 if (inet_aton(host, inp) != 1) {
895 struct hostent *hent;
896
897 hent = gethostbyname(host);
898 if (!hent) {
Jens Axboebebe6392011-10-07 10:00:51 +0200899 free(*ptr);
900 *ptr = NULL;
901 return 1;
902 }
903
904 memcpy(inp, hent->h_addr, 4);
905 }
906 }
907
908 if (*port == 0)
909 *port = fio_net_port;
910
911 return 0;
912}
913
Jens Axboe87aa8f12011-10-06 21:24:13 +0200914/*
915 * Server arg should be one of:
916 *
917 * sock:/path/to/socket
918 * ip:1.2.3.4
919 * 1.2.3.4
920 *
921 * Where sock uses unix domain sockets, and ip binds the server to
922 * a specific interface. If no arguments are given to the server, it
923 * uses IP and binds to 0.0.0.0.
924 *
925 */
926static int fio_handle_server_arg(void)
927{
Jens Axboe6d2cf392011-10-07 13:19:28 +0200928 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +0200929 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +0200930
Jens Axboe87aa8f12011-10-06 21:24:13 +0200931 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
932
933 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +0200934 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200935
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200936 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe6d2cf392011-10-07 13:19:28 +0200937 &port, &saddr_in.sin_addr);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200938
939 if (!is_sock && bind_sock) {
940 free(bind_sock);
941 bind_sock = NULL;
942 }
943
Jens Axboea7de0a12011-10-07 12:55:14 +0200944out:
Jens Axboe6d2cf392011-10-07 13:19:28 +0200945 fio_net_port = port;
946 saddr_in.sin_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200947 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200948}
949
950static int fio_server(void)
951{
952 int sk, ret;
953
954 dprint(FD_NET, "starting server\n");
955
956 if (fio_handle_server_arg())
957 return -1;
958
959 sk = fio_init_server_connection();
960 if (sk < 0)
961 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200962
963 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200964
Jens Axboe81179ee2011-10-04 12:42:06 +0200965 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200966
967 if (fio_server_arg) {
968 free(fio_server_arg);
969 fio_server_arg = NULL;
970 }
Jens Axboebebe6392011-10-07 10:00:51 +0200971 if (bind_sock)
972 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200973
Jens Axboe81179ee2011-10-04 12:42:06 +0200974 return ret;
975}
976
Jens Axboe7b821682011-10-11 12:16:32 +0200977void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +0200978{
Jens Axboe7b821682011-10-11 12:16:32 +0200979 if (signal == SIGPIPE)
980 server_fd = -1;
981 else {
982 log_info("\nfio: terminating on signal %d\n", signal);
983 exit_backend = 1;
984 }
Jens Axboe9abea482011-10-04 13:21:54 +0200985}
986
Jens Axboe13755d92011-10-10 19:51:26 +0200987static int check_existing_pidfile(const char *pidfile)
988{
989 struct stat sb;
990 char buf[16];
991 pid_t pid;
992 FILE *f;
993
994 if (stat(pidfile, &sb))
995 return 0;
996
997 f = fopen(pidfile, "r");
998 if (!f)
999 return 0;
1000
Jens Axboebfc3b172011-10-10 21:16:55 +02001001 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001002 fclose(f);
1003 return 1;
1004 }
1005 fclose(f);
1006
1007 pid = atoi(buf);
1008 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001009 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001010
1011 return 1;
1012}
1013
1014static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001015{
1016 FILE *fpid;
1017
1018 fpid = fopen(pidfile, "w");
1019 if (!fpid) {
1020 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001021 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001022 }
1023
1024 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001025 fclose(fpid);
1026 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001027}
1028
1029/*
1030 * If pidfile is specified, background us.
1031 */
1032int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001033{
1034 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001035 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001036
Jens Axboe402668f2011-10-10 15:28:58 +02001037 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001038 return fio_server();
1039
Jens Axboe13755d92011-10-10 19:51:26 +02001040 if (check_existing_pidfile(pidfile)) {
1041 log_err("fio: pidfile %s exists and server appears alive\n",
1042 pidfile);
1043 return -1;
1044 }
1045
Jens Axboee46d8092011-10-03 09:11:02 +02001046 pid = fork();
1047 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001048 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001049 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001050 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001051 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001052 int ret = write_pid(pid, pidfile);
1053
1054 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001055 }
Jens Axboee46d8092011-10-03 09:11:02 +02001056
1057 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001058 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1059 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001060 close(STDIN_FILENO);
1061 close(STDOUT_FILENO);
1062 close(STDERR_FILENO);
1063 f_out = NULL;
1064 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001065
1066 ret = fio_server();
1067
1068 closelog();
1069 unlink(pidfile);
1070 free(pidfile);
1071 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001072}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001073
Jens Axboebebe6392011-10-07 10:00:51 +02001074void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001075{
1076 fio_server_arg = strdup(arg);
1077}