blob: ac6c2fb8969da75f15c05def7e313f01ca162137 [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
412 size = sizeof(*je) + thread_number * sizeof(char);
Jens Axboe7f868312011-10-10 09:55:21 +0200413 je = malloc(size);
414 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200415
416 if (!calc_thread_status(je, 1)) {
417 free(je);
418 return 0;
419 }
420
421 dprint(FD_NET, "server sending status\n");
422
423 je->nr_running = cpu_to_le32(je->nr_running);
424 je->nr_ramp = cpu_to_le32(je->nr_ramp);
425 je->nr_pending = cpu_to_le32(je->nr_pending);
426 je->files_open = cpu_to_le32(je->files_open);
427 je->m_rate = cpu_to_le32(je->m_rate);
428 je->t_rate = cpu_to_le32(je->t_rate);
429 je->m_iops = cpu_to_le32(je->m_iops);
430 je->t_iops = cpu_to_le32(je->t_iops);
431
432 for (i = 0; i < 2; i++) {
433 je->rate[i] = cpu_to_le32(je->rate[i]);
434 je->iops[i] = cpu_to_le32(je->iops[i]);
435 }
436
437 je->elapsed_sec = cpu_to_le32(je->nr_running);
438 je->eta_sec = cpu_to_le64(je->eta_sec);
439
Jens Axboe7f868312011-10-10 09:55:21 +0200440 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200441 free(je);
442 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200443}
444
Jens Axboe132159a2011-09-30 15:01:32 -0600445static int handle_command(struct fio_net_cmd *cmd)
446{
447 int ret;
448
Jens Axboe89c17072011-10-11 10:15:51 +0200449 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
450 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600451
Jens Axboe132159a2011-09-30 15:01:32 -0600452 switch (cmd->opcode) {
453 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200454 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200455 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200456 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600457 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200458 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600459 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200460 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600461 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200462 case FIO_NET_CMD_JOBLINE:
463 ret = handle_jobline_cmd(cmd);
464 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200465 case FIO_NET_CMD_PROBE:
466 ret = handle_probe_cmd(cmd);
467 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200468 case FIO_NET_CMD_SEND_ETA:
469 ret = handle_send_eta_cmd(cmd);
470 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600471 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200472 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600473 ret = 1;
474 }
475
476 return ret;
477}
478
Jens Axboe70e0c312011-10-04 09:18:30 +0200479static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600480{
481 struct fio_net_cmd *cmd = NULL;
482 int ret = 0;
483
484 /* read forever */
485 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200486 struct pollfd pfd = {
487 .fd = sk,
488 .events = POLLIN,
489 };
490
491 ret = 0;
492 do {
493 ret = poll(&pfd, 1, 100);
494 if (ret < 0) {
495 if (errno == EINTR)
496 break;
497 log_err("fio: poll: %s\n", strerror(errno));
498 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200499 } else if (!ret) {
500 if (!block)
501 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200502 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200503 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200504
505 if (pfd.revents & POLLIN)
506 break;
507 if (pfd.revents & (POLLERR|POLLHUP)) {
508 ret = 1;
509 break;
510 }
Jens Axboe19c65172011-10-05 22:05:37 +0200511 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200512
513 if (ret < 0)
514 break;
515
516 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600517 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200518 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600519 break;
520 }
521
Jens Axboe132159a2011-09-30 15:01:32 -0600522 ret = handle_command(cmd);
523 if (ret)
524 break;
525
526 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400527 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600528 }
529
530 if (cmd)
531 free(cmd);
532
533 return ret;
534}
535
Jens Axboecc0df002011-10-03 20:53:32 +0200536void fio_server_idle_loop(void)
537{
538 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200539 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200540}
541
Jens Axboe50d16972011-09-29 17:45:28 -0600542static int accept_loop(int listen_sk)
543{
Jens Axboebb447a22011-10-04 15:06:42 +0200544 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200545 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600546 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600547 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600548
Jens Axboe60efd142011-10-04 13:30:11 +0200549 dprint(FD_NET, "server enter accept loop\n");
550
Jens Axboe009b1be2011-09-29 18:27:02 -0600551 flags = fcntl(listen_sk, F_GETFL);
552 flags |= O_NONBLOCK;
553 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600554again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600555 pfd.fd = listen_sk;
556 pfd.events = POLLIN;
557 do {
558 ret = poll(&pfd, 1, 100);
559 if (ret < 0) {
560 if (errno == EINTR)
561 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600562 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600563 goto out;
564 } else if (!ret)
565 continue;
566
567 if (pfd.revents & POLLIN)
568 break;
569 } while (!exit_backend);
570
571 if (exit_backend)
572 goto out;
573
Jens Axboe6b976bf2011-10-04 15:07:43 +0200574 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600575 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600576 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600577 return -1;
578 }
579
Jens Axboebb447a22011-10-04 15:06:42 +0200580 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600581
Jens Axboe37db14f2011-09-30 17:00:42 -0600582 server_fd = sk;
583
Jens Axboe70e0c312011-10-04 09:18:30 +0200584 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600585
Jens Axboe37db14f2011-09-30 17:00:42 -0600586 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600587 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600588
Jens Axboe009b1be2011-09-29 18:27:02 -0600589 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600590 goto again;
591
Jens Axboe009b1be2011-09-29 18:27:02 -0600592out:
Jens Axboe132159a2011-09-30 15:01:32 -0600593 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600594}
595
Jens Axboe13755d92011-10-10 19:51:26 +0200596int fio_server_text_output(const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600597{
Jens Axboe337d75a2011-10-01 12:36:32 -0600598 if (server_fd != -1)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200599 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
Jens Axboe337d75a2011-10-01 12:36:32 -0600600
Jens Axboe13755d92011-10-10 19:51:26 +0200601 return log_local_buf(buf, len);
Jens Axboe142575e2011-09-30 17:35:45 -0600602}
603
Jens Axboea64e88d2011-10-03 14:20:01 +0200604static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
605{
606 dst->max_val = cpu_to_le64(src->max_val);
607 dst->min_val = cpu_to_le64(src->min_val);
608 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200609
610 /*
611 * Encode to IEEE 754 for network transfer
612 */
613 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
614 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200615}
616
617static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
618{
619 int i;
620
621 for (i = 0; i < 2; i++) {
622 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
623 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
624 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
625 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
626 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
627 dst->agg[i] = cpu_to_le64(src->agg[i]);
628 }
629
630 dst->kb_base = cpu_to_le32(src->kb_base);
631 dst->groupid = cpu_to_le32(src->groupid);
632}
633
634/*
635 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
636 * into a single payload.
637 */
638void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
639{
640 struct cmd_ts_pdu p;
641 int i, j;
642
Jens Axboe60efd142011-10-04 13:30:11 +0200643 dprint(FD_NET, "server sending end stats\n");
644
Jens Axboe317b3c82011-10-03 21:47:27 +0200645 memset(&p, 0, sizeof(p));
646
Jens Axboea64e88d2011-10-03 14:20:01 +0200647 strcpy(p.ts.name, ts->name);
648 strcpy(p.ts.verror, ts->verror);
649 strcpy(p.ts.description, ts->description);
650
Jens Axboeddcc0b62011-10-03 14:45:27 +0200651 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200652 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200653 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200654 p.ts.members = cpu_to_le32(ts->members);
655
656 for (i = 0; i < 2; i++) {
657 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
658 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
659 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
660 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
661 }
662
663 p.ts.usr_time = cpu_to_le64(ts->usr_time);
664 p.ts.sys_time = cpu_to_le64(ts->sys_time);
665 p.ts.ctx = cpu_to_le64(ts->ctx);
666 p.ts.minf = cpu_to_le64(ts->minf);
667 p.ts.majf = cpu_to_le64(ts->majf);
668 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200669
670 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
671 fio_fp64_t *fp = &p.ts.percentile_list[i];
672
673 fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f));
674 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200675
676 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
677 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
678 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
679 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
680 }
681
682 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
683 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
684 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
685 }
686
687 for (i = 0; i < 2; i++)
688 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
689 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
690
691 for (i = 0; i < 3; i++) {
692 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200693 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200694 }
695
Jens Axboe93eee042011-10-03 21:08:48 +0200696 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200697 p.ts.total_complete = cpu_to_le64(ts->total_complete);
698
699 for (i = 0; i < 2; i++) {
700 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
701 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
702 }
703
704 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
705 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
706 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200707 p.ts.first_error = cpu_to_le32(ts->first_error);
708 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200709
710 convert_gs(&p.rs, rs);
711
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200712 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200713}
714
715void fio_server_send_gs(struct group_run_stats *rs)
716{
717 struct group_run_stats gs;
718
Jens Axboe60efd142011-10-04 13:30:11 +0200719 dprint(FD_NET, "server sending group run stats\n");
720
Jens Axboea64e88d2011-10-03 14:20:01 +0200721 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200722 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200723}
724
Jens Axboe142575e2011-09-30 17:35:45 -0600725int fio_server_log(const char *format, ...)
726{
727 char buffer[1024];
728 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600729 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600730
Jens Axboe60efd142011-10-04 13:30:11 +0200731 dprint(FD_NET, "server log\n");
732
Jens Axboe142575e2011-09-30 17:35:45 -0600733 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600734 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600735 va_end(args);
736
Jens Axboe82fa6b22011-09-30 22:29:08 -0600737 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600738}
Jens Axboee46d8092011-10-03 09:11:02 +0200739
Jens Axboe87aa8f12011-10-06 21:24:13 +0200740static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200741{
Jens Axboe87aa8f12011-10-06 21:24:13 +0200742 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200743
744 sk = socket(AF_INET, SOCK_STREAM, 0);
745 if (sk < 0) {
746 log_err("fio: socket: %s\n", strerror(errno));
747 return -1;
748 }
749
750 opt = 1;
751 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
752 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200753 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200754 return -1;
755 }
756#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200757 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200758 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200759 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200760 return -1;
761 }
762#endif
763
764 saddr_in.sin_family = AF_INET;
Jens Axboe81179ee2011-10-04 12:42:06 +0200765
766 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
767 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200768 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200769 return -1;
770 }
771
Jens Axboe87aa8f12011-10-06 21:24:13 +0200772 return sk;
773}
774
775static int fio_init_server_sock(void)
776{
777 struct sockaddr_un addr;
778 fio_socklen_t len;
779 mode_t mode;
780 int sk;
781
782 sk = socket(AF_UNIX, SOCK_STREAM, 0);
783 if (sk < 0) {
784 log_err("fio: socket: %s\n", strerror(errno));
785 return -1;
786 }
787
788 mode = umask(000);
789
790 memset(&addr, 0, sizeof(addr));
791 addr.sun_family = AF_UNIX;
792 strcpy(addr.sun_path, bind_sock);
793 unlink(bind_sock);
794
795 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
796
797 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
798 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200799 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200800 return -1;
801 }
802
803 umask(mode);
804 return sk;
805}
806
807static int fio_init_server_connection(void)
808{
Jens Axboebebe6392011-10-07 10:00:51 +0200809 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200810 int sk;
811
812 dprint(FD_NET, "starting server\n");
813
814 if (!bind_sock)
815 sk = fio_init_server_ip();
816 else
817 sk = fio_init_server_sock();
818
819 if (sk < 0)
820 return sk;
821
Jens Axboebebe6392011-10-07 10:00:51 +0200822 if (!bind_sock)
823 sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port);
824 else
825 strcpy(bind_str, bind_sock);
826
827 log_info("fio: server listening on %s\n", bind_str);
828
Jens Axboe89c17072011-10-11 10:15:51 +0200829 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200830 log_err("fio: listen: %s\n", strerror(errno));
831 return -1;
832 }
833
Jens Axboe87aa8f12011-10-06 21:24:13 +0200834 return sk;
835}
836
Jens Axboebebe6392011-10-07 10:00:51 +0200837int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
838 int *port, struct in_addr *inp)
839{
840 *ptr = NULL;
841 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +0200842 *port = fio_net_port;
Jens Axboebebe6392011-10-07 10:00:51 +0200843
844 if (!strncmp(str, "sock:", 5)) {
845 *ptr = strdup(str + 5);
846 *is_sock = 1;
847 } else {
848 const char *host = str;
849 char *portp;
850 int lport = 0;
851
852 /*
853 * Is it ip:<ip or host>:port
854 */
855 if (!strncmp(host, "ip:", 3))
856 host += 3;
857 else if (host[0] == ':') {
858 /* String is :port */
859 host++;
860 lport = atoi(host);
861 if (!lport || lport > 65535) {
862 log_err("fio: bad server port %u\n", port);
863 return 1;
864 }
865 /* no hostname given, we are done */
866 *port = lport;
867 return 0;
868 }
869
870 /*
871 * If no port seen yet, check if there's a last ':' at the end
872 */
873 if (!lport) {
874 portp = strchr(host, ':');
875 if (portp) {
876 *portp = '\0';
877 portp++;
878 lport = atoi(portp);
879 if (!lport || lport > 65535) {
880 log_err("fio: bad server port %u\n", port);
881 return 1;
882 }
883 }
884 }
885
886 if (lport)
887 *port = lport;
888
889 *ptr = strdup(host);
890
891 if (inet_aton(host, inp) != 1) {
892 struct hostent *hent;
893
894 hent = gethostbyname(host);
895 if (!hent) {
Jens Axboebebe6392011-10-07 10:00:51 +0200896 free(*ptr);
897 *ptr = NULL;
898 return 1;
899 }
900
901 memcpy(inp, hent->h_addr, 4);
902 }
903 }
904
905 if (*port == 0)
906 *port = fio_net_port;
907
908 return 0;
909}
910
Jens Axboe87aa8f12011-10-06 21:24:13 +0200911/*
912 * Server arg should be one of:
913 *
914 * sock:/path/to/socket
915 * ip:1.2.3.4
916 * 1.2.3.4
917 *
918 * Where sock uses unix domain sockets, and ip binds the server to
919 * a specific interface. If no arguments are given to the server, it
920 * uses IP and binds to 0.0.0.0.
921 *
922 */
923static int fio_handle_server_arg(void)
924{
Jens Axboe6d2cf392011-10-07 13:19:28 +0200925 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +0200926 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +0200927
Jens Axboe87aa8f12011-10-06 21:24:13 +0200928 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
929
930 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +0200931 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200932
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200933 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe6d2cf392011-10-07 13:19:28 +0200934 &port, &saddr_in.sin_addr);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200935
936 if (!is_sock && bind_sock) {
937 free(bind_sock);
938 bind_sock = NULL;
939 }
940
Jens Axboea7de0a12011-10-07 12:55:14 +0200941out:
Jens Axboe6d2cf392011-10-07 13:19:28 +0200942 fio_net_port = port;
943 saddr_in.sin_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200944 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200945}
946
947static int fio_server(void)
948{
949 int sk, ret;
950
951 dprint(FD_NET, "starting server\n");
952
953 if (fio_handle_server_arg())
954 return -1;
955
956 sk = fio_init_server_connection();
957 if (sk < 0)
958 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200959
960 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200961
Jens Axboe81179ee2011-10-04 12:42:06 +0200962 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200963
964 if (fio_server_arg) {
965 free(fio_server_arg);
966 fio_server_arg = NULL;
967 }
Jens Axboebebe6392011-10-07 10:00:51 +0200968 if (bind_sock)
969 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200970
Jens Axboe81179ee2011-10-04 12:42:06 +0200971 return ret;
972}
973
Jens Axboe7b821682011-10-11 12:16:32 +0200974void fio_server_got_signal(int signal)
Jens Axboe9abea482011-10-04 13:21:54 +0200975{
Jens Axboe7b821682011-10-11 12:16:32 +0200976 if (signal == SIGPIPE)
977 server_fd = -1;
978 else {
979 log_info("\nfio: terminating on signal %d\n", signal);
980 exit_backend = 1;
981 }
Jens Axboe9abea482011-10-04 13:21:54 +0200982}
983
Jens Axboe13755d92011-10-10 19:51:26 +0200984static int check_existing_pidfile(const char *pidfile)
985{
986 struct stat sb;
987 char buf[16];
988 pid_t pid;
989 FILE *f;
990
991 if (stat(pidfile, &sb))
992 return 0;
993
994 f = fopen(pidfile, "r");
995 if (!f)
996 return 0;
997
Jens Axboebfc3b172011-10-10 21:16:55 +0200998 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +0200999 fclose(f);
1000 return 1;
1001 }
1002 fclose(f);
1003
1004 pid = atoi(buf);
1005 if (kill(pid, SIGCONT) < 0)
Jens Axboeea5aa1b2011-10-11 11:45:35 +02001006 return errno != ESRCH;
Jens Axboe13755d92011-10-10 19:51:26 +02001007
1008 return 1;
1009}
1010
1011static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001012{
1013 FILE *fpid;
1014
1015 fpid = fopen(pidfile, "w");
1016 if (!fpid) {
1017 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001018 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001019 }
1020
1021 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001022 fclose(fpid);
1023 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001024}
1025
1026/*
1027 * If pidfile is specified, background us.
1028 */
1029int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001030{
1031 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001032 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001033
Jens Axboe402668f2011-10-10 15:28:58 +02001034 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001035 return fio_server();
1036
Jens Axboe13755d92011-10-10 19:51:26 +02001037 if (check_existing_pidfile(pidfile)) {
1038 log_err("fio: pidfile %s exists and server appears alive\n",
1039 pidfile);
1040 return -1;
1041 }
1042
Jens Axboee46d8092011-10-03 09:11:02 +02001043 pid = fork();
1044 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001045 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001046 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001047 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001048 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001049 int ret = write_pid(pid, pidfile);
1050
1051 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001052 }
Jens Axboee46d8092011-10-03 09:11:02 +02001053
1054 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001055 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1056 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001057 close(STDIN_FILENO);
1058 close(STDOUT_FILENO);
1059 close(STDERR_FILENO);
1060 f_out = NULL;
1061 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001062
1063 ret = fio_server();
1064
1065 closelog();
1066 unlink(pidfile);
1067 free(pidfile);
1068 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001069}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001070
Jens Axboebebe6392011-10-07 10:00:51 +02001071void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001072{
1073 fio_server_arg = strdup(arg);
1074}