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