blob: 5506ca9fedbbb860f7df89b02147d507a0d394ec [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 Axboe132159a2011-09-30 15:01:32 -060036int fio_send_data(int sk, const void *p, unsigned int len)
37{
Jens Axboe794d69c2011-10-01 08:48:50 -060038 assert(len <= sizeof(struct fio_net_cmd) + FIO_SERVER_MAX_PDU);
39
Jens Axboe132159a2011-09-30 15:01:32 -060040 do {
41 int ret = send(sk, p, len, 0);
42
43 if (ret > 0) {
44 len -= ret;
45 if (!len)
46 break;
47 p += ret;
48 continue;
49 } else if (!ret)
50 break;
51 else if (errno == EAGAIN || errno == EINTR)
52 continue;
53 } while (!exit_backend);
54
55 if (!len)
56 return 0;
57
58 return 1;
59}
60
61int fio_recv_data(int sk, void *p, unsigned int len)
62{
63 do {
64 int ret = recv(sk, p, len, MSG_WAITALL);
65
66 if (ret > 0) {
67 len -= ret;
68 if (!len)
69 break;
70 p += ret;
71 continue;
72 } else if (!ret)
73 break;
74 else if (errno == EAGAIN || errno == EINTR)
75 continue;
76 } while (!exit_backend);
77
78 if (!len)
79 return 0;
80
81 return -1;
82}
83
84static int verify_convert_cmd(struct fio_net_cmd *cmd)
85{
Jens Axboefcee5ff2011-09-30 22:50:39 -060086 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -060087
Jens Axboefcee5ff2011-09-30 22:50:39 -060088 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
89 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -060090
Jens Axboefcee5ff2011-09-30 22:50:39 -060091 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
92 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -060093 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -060094 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -060095 return 1;
96 }
97
98 cmd->version = le16_to_cpu(cmd->version);
99 cmd->opcode = le16_to_cpu(cmd->opcode);
100 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200101 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600102 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
103
104 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200105 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600106 break;
107 default:
108 log_err("fio: bad server cmd version %d\n", cmd->version);
109 return 1;
110 }
111
112 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
113 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
114 return 1;
115 }
116
117 return 0;
118}
119
Jens Axboea64e88d2011-10-03 14:20:01 +0200120/*
121 * Read (and defragment, if necessary) incoming commands
122 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200123struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600124{
Jens Axboea64e88d2011-10-03 14:20:01 +0200125 struct fio_net_cmd cmd, *cmdret = NULL;
126 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600127 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200128 int ret, first = 1;
129 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600130
Jens Axboea64e88d2011-10-03 14:20:01 +0200131 do {
132 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
133 if (ret)
134 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600135
Jens Axboea64e88d2011-10-03 14:20:01 +0200136 /* We have a command, verify it and swap if need be */
137 ret = verify_convert_cmd(&cmd);
138 if (ret)
139 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600140
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200141 if (first) {
142 /* if this is text, add room for \0 at the end */
143 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
144 assert(!cmdret);
145 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200146 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600147
Jens Axboea64e88d2011-10-03 14:20:01 +0200148 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600149
Jens Axboea64e88d2011-10-03 14:20:01 +0200150 if (first)
151 memcpy(cmdret, &cmd, sizeof(cmd));
152 else
153 assert(cmdret->opcode == cmd.opcode);
154
155 if (!cmd.pdu_len)
156 break;
157
158 /* There's payload, get it */
159 pdu = (void *) cmdret->payload + pdu_offset;
160 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
161 if (ret)
162 break;
163
164 /* Verify payload crc */
165 crc = crc16(pdu, cmd.pdu_len);
166 if (crc != cmd.pdu_crc16) {
167 log_err("fio: server bad crc on payload ");
168 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
169 ret = 1;
170 break;
171 }
172
173 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200174 if (!first)
175 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200176 first = 0;
177 } while (cmd.flags & FIO_NET_CMD_F_MORE);
178
179 if (ret) {
180 free(cmdret);
181 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200182 } else if (cmdret) {
183 /* zero-terminate text input */
184 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
185 cmdret->opcode == FIO_NET_CMD_JOB)) {
186 char *buf = (char *) cmdret->payload;
187
188 buf[cmdret->pdu_len ] = '\0';
189 }
190 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200191 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200192 }
Jens Axboe132159a2011-09-30 15:01:32 -0600193
Jens Axboea64e88d2011-10-03 14:20:01 +0200194 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600195}
196
197void fio_net_cmd_crc(struct fio_net_cmd *cmd)
198{
199 uint32_t pdu_len;
200
Jens Axboeddcc0b62011-10-03 14:45:27 +0200201 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600202
203 pdu_len = le32_to_cpu(cmd->pdu_len);
204 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200205 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600206}
207
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200208int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
209 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600210{
Jens Axboe7f868312011-10-10 09:55:21 +0200211 struct fio_net_cmd *cmd = NULL;
212 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600213 int ret;
214
215 do {
216 this_len = size;
217 if (this_len > FIO_SERVER_MAX_PDU)
218 this_len = FIO_SERVER_MAX_PDU;
219
Jens Axboe7f868312011-10-10 09:55:21 +0200220 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
221 if (cmd)
222 free(cmd);
223
224 cur_len = sizeof(*cmd) + this_len;
225 cmd = malloc(cur_len);
226 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600227
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200228 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600229
230 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200231 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600232
233 fio_net_cmd_crc(cmd);
234
235 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600236 size -= this_len;
237 buf += this_len;
238 } while (!ret && size);
239
Jens Axboe7f868312011-10-10 09:55:21 +0200240 if (cmd)
241 free(cmd);
242
Jens Axboe794d69c2011-10-01 08:48:50 -0600243 return ret;
244}
245
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200246int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600247{
Jens Axboe178cde92011-10-05 22:14:31 +0200248 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600249
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200250 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600251 fio_net_cmd_crc(&cmd);
252
253 return fio_send_data(sk, &cmd, sizeof(cmd));
254}
255
Jens Axboe9abea482011-10-04 13:21:54 +0200256static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600257{
Jens Axboe46c48f12011-10-01 12:50:51 -0600258 dprint(FD_NET, "server: sending quit\n");
Jens Axboecc0df002011-10-03 20:53:32 +0200259 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0);
Jens Axboe437377e2011-10-01 08:31:30 -0600260}
261
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200262static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600263{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200264 char *buf = (char *) cmd->payload;
Jens Axboea64e88d2011-10-03 14:20:01 +0200265 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600266
Jens Axboee6d1c662011-10-05 20:41:06 +0200267 if (parse_jobs_ini(buf, 1, 0)) {
268 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200269 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200270 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200271
272 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
273
274 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200275 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200276 reset_fio_state();
277 return ret;
278}
279
280static int handle_jobline_cmd(struct fio_net_cmd *cmd)
281{
Jens Axboefa2ea802011-10-08 21:07:29 +0200282 void *pdu = cmd->payload;
283 struct cmd_single_line_pdu *cslp;
284 struct cmd_line_pdu *clp;
285 unsigned long offset;
286 char **argv;
Jens Axboe81179ee2011-10-04 12:42:06 +0200287 int ret, i;
288
Jens Axboefa2ea802011-10-08 21:07:29 +0200289 clp = pdu;
290 clp->lines = le16_to_cpu(clp->lines);
291 argv = malloc(clp->lines * sizeof(char *));
292 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200293
Jens Axboefa2ea802011-10-08 21:07:29 +0200294 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200295
Jens Axboefa2ea802011-10-08 21:07:29 +0200296 for (i = 0; i < clp->lines; i++) {
297 cslp = pdu + offset;
298 argv[i] = (char *) cslp->text;
299
300 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200301 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
302 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200303
Jens Axboefa2ea802011-10-08 21:07:29 +0200304 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200305 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200306 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200307 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200308 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200309
Jens Axboefa2ea802011-10-08 21:07:29 +0200310 free(argv);
311
Jens Axboe81179ee2011-10-04 12:42:06 +0200312 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0);
313
Jens Axboe794d69c2011-10-01 08:48:50 -0600314 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200315 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600316 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600317 return ret;
318}
319
Jens Axboec28e8e82011-10-04 08:57:39 +0200320static int handle_probe_cmd(struct fio_net_cmd *cmd)
321{
322 struct cmd_probe_pdu probe;
323
324 memset(&probe, 0, sizeof(probe));
325 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200326#ifdef FIO_BIG_ENDIAN
327 probe.bigendian = 1;
328#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200329 probe.fio_major = FIO_MAJOR;
330 probe.fio_minor = FIO_MINOR;
331 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200332
Jens Axboecca84642011-10-07 12:47:57 +0200333 probe.os = FIO_OS;
334 probe.arch = FIO_ARCH;
335
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200336 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), 0);
337}
338
339static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
340{
341 struct jobs_eta *je;
342 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200343 int i;
344
345 size = sizeof(*je) + thread_number * sizeof(char);
Jens Axboe7f868312011-10-10 09:55:21 +0200346 je = malloc(size);
347 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200348
349 if (!calc_thread_status(je, 1)) {
350 free(je);
351 return 0;
352 }
353
354 dprint(FD_NET, "server sending status\n");
355
356 je->nr_running = cpu_to_le32(je->nr_running);
357 je->nr_ramp = cpu_to_le32(je->nr_ramp);
358 je->nr_pending = cpu_to_le32(je->nr_pending);
359 je->files_open = cpu_to_le32(je->files_open);
360 je->m_rate = cpu_to_le32(je->m_rate);
361 je->t_rate = cpu_to_le32(je->t_rate);
362 je->m_iops = cpu_to_le32(je->m_iops);
363 je->t_iops = cpu_to_le32(je->t_iops);
364
365 for (i = 0; i < 2; i++) {
366 je->rate[i] = cpu_to_le32(je->rate[i]);
367 je->iops[i] = cpu_to_le32(je->iops[i]);
368 }
369
370 je->elapsed_sec = cpu_to_le32(je->nr_running);
371 je->eta_sec = cpu_to_le64(je->eta_sec);
372
Jens Axboe7f868312011-10-10 09:55:21 +0200373 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200374 free(je);
375 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200376}
377
Jens Axboe132159a2011-09-30 15:01:32 -0600378static int handle_command(struct fio_net_cmd *cmd)
379{
380 int ret;
381
Jens Axboe7f868312011-10-10 09:55:21 +0200382 dprint(FD_NET, "server: got opcode %d, pdu=%u\n", cmd->opcode, cmd->pdu_len);
Jens Axboe46c48f12011-10-01 12:50:51 -0600383
Jens Axboe132159a2011-09-30 15:01:32 -0600384 switch (cmd->opcode) {
385 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200386 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200387 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200388 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600389 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200390 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600391 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200392 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600393 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200394 case FIO_NET_CMD_JOBLINE:
395 ret = handle_jobline_cmd(cmd);
396 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200397 case FIO_NET_CMD_PROBE:
398 ret = handle_probe_cmd(cmd);
399 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200400 case FIO_NET_CMD_SEND_ETA:
401 ret = handle_send_eta_cmd(cmd);
402 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600403 default:
404 log_err("fio: unknown opcode: %d\n", cmd->opcode);
405 ret = 1;
406 }
407
408 return ret;
409}
410
Jens Axboe70e0c312011-10-04 09:18:30 +0200411static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600412{
413 struct fio_net_cmd *cmd = NULL;
414 int ret = 0;
415
416 /* read forever */
417 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200418 struct pollfd pfd = {
419 .fd = sk,
420 .events = POLLIN,
421 };
422
423 ret = 0;
424 do {
425 ret = poll(&pfd, 1, 100);
426 if (ret < 0) {
427 if (errno == EINTR)
428 break;
429 log_err("fio: poll: %s\n", strerror(errno));
430 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200431 } else if (!ret) {
432 if (!block)
433 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200434 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200435 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200436
437 if (pfd.revents & POLLIN)
438 break;
439 if (pfd.revents & (POLLERR|POLLHUP)) {
440 ret = 1;
441 break;
442 }
Jens Axboe19c65172011-10-05 22:05:37 +0200443 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200444
445 if (ret < 0)
446 break;
447
448 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600449 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200450 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600451 break;
452 }
453
Jens Axboe132159a2011-09-30 15:01:32 -0600454 ret = handle_command(cmd);
455 if (ret)
456 break;
457
458 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400459 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600460 }
461
462 if (cmd)
463 free(cmd);
464
465 return ret;
466}
467
Jens Axboecc0df002011-10-03 20:53:32 +0200468void fio_server_idle_loop(void)
469{
470 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200471 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200472}
473
Jens Axboe50d16972011-09-29 17:45:28 -0600474static int accept_loop(int listen_sk)
475{
Jens Axboebb447a22011-10-04 15:06:42 +0200476 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200477 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600478 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600479 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600480
Jens Axboe60efd142011-10-04 13:30:11 +0200481 dprint(FD_NET, "server enter accept loop\n");
482
Jens Axboe009b1be2011-09-29 18:27:02 -0600483 flags = fcntl(listen_sk, F_GETFL);
484 flags |= O_NONBLOCK;
485 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600486again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600487 pfd.fd = listen_sk;
488 pfd.events = POLLIN;
489 do {
490 ret = poll(&pfd, 1, 100);
491 if (ret < 0) {
492 if (errno == EINTR)
493 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600494 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600495 goto out;
496 } else if (!ret)
497 continue;
498
499 if (pfd.revents & POLLIN)
500 break;
501 } while (!exit_backend);
502
503 if (exit_backend)
504 goto out;
505
Jens Axboe6b976bf2011-10-04 15:07:43 +0200506 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600507 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600508 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600509 return -1;
510 }
511
Jens Axboebb447a22011-10-04 15:06:42 +0200512 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600513
Jens Axboe37db14f2011-09-30 17:00:42 -0600514 server_fd = sk;
515
Jens Axboe70e0c312011-10-04 09:18:30 +0200516 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600517
Jens Axboe37db14f2011-09-30 17:00:42 -0600518 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600519 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600520
Jens Axboe009b1be2011-09-29 18:27:02 -0600521 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600522 goto again;
523
Jens Axboe009b1be2011-09-29 18:27:02 -0600524out:
Jens Axboe132159a2011-09-30 15:01:32 -0600525 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600526}
527
Jens Axboe13755d92011-10-10 19:51:26 +0200528int fio_server_text_output(const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600529{
Jens Axboe337d75a2011-10-01 12:36:32 -0600530 if (server_fd != -1)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200531 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
Jens Axboe337d75a2011-10-01 12:36:32 -0600532
Jens Axboe13755d92011-10-10 19:51:26 +0200533 return log_local_buf(buf, len);
Jens Axboe142575e2011-09-30 17:35:45 -0600534}
535
Jens Axboea64e88d2011-10-03 14:20:01 +0200536static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
537{
538 dst->max_val = cpu_to_le64(src->max_val);
539 dst->min_val = cpu_to_le64(src->min_val);
540 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200541
542 /*
543 * Encode to IEEE 754 for network transfer
544 */
545 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
546 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200547}
548
549static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
550{
551 int i;
552
553 for (i = 0; i < 2; i++) {
554 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
555 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
556 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
557 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
558 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
559 dst->agg[i] = cpu_to_le64(src->agg[i]);
560 }
561
562 dst->kb_base = cpu_to_le32(src->kb_base);
563 dst->groupid = cpu_to_le32(src->groupid);
564}
565
566/*
567 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
568 * into a single payload.
569 */
570void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
571{
572 struct cmd_ts_pdu p;
573 int i, j;
574
Jens Axboe60efd142011-10-04 13:30:11 +0200575 dprint(FD_NET, "server sending end stats\n");
576
Jens Axboe317b3c82011-10-03 21:47:27 +0200577 memset(&p, 0, sizeof(p));
578
Jens Axboea64e88d2011-10-03 14:20:01 +0200579 strcpy(p.ts.name, ts->name);
580 strcpy(p.ts.verror, ts->verror);
581 strcpy(p.ts.description, ts->description);
582
Jens Axboeddcc0b62011-10-03 14:45:27 +0200583 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200584 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200585 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200586 p.ts.members = cpu_to_le32(ts->members);
587
588 for (i = 0; i < 2; i++) {
589 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
590 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
591 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
592 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
593 }
594
595 p.ts.usr_time = cpu_to_le64(ts->usr_time);
596 p.ts.sys_time = cpu_to_le64(ts->sys_time);
597 p.ts.ctx = cpu_to_le64(ts->ctx);
598 p.ts.minf = cpu_to_le64(ts->minf);
599 p.ts.majf = cpu_to_le64(ts->majf);
600 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200601
602 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
603 fio_fp64_t *fp = &p.ts.percentile_list[i];
604
605 fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f));
606 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200607
608 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
609 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
610 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
611 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
612 }
613
614 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
615 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
616 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
617 }
618
619 for (i = 0; i < 2; i++)
620 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
621 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
622
623 for (i = 0; i < 3; i++) {
624 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200625 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200626 }
627
Jens Axboe93eee042011-10-03 21:08:48 +0200628 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200629 p.ts.total_complete = cpu_to_le64(ts->total_complete);
630
631 for (i = 0; i < 2; i++) {
632 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
633 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
634 }
635
636 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
637 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
638 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200639 p.ts.first_error = cpu_to_le32(ts->first_error);
640 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200641
642 convert_gs(&p.rs, rs);
643
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200644 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200645}
646
647void fio_server_send_gs(struct group_run_stats *rs)
648{
649 struct group_run_stats gs;
650
Jens Axboe60efd142011-10-04 13:30:11 +0200651 dprint(FD_NET, "server sending group run stats\n");
652
Jens Axboea64e88d2011-10-03 14:20:01 +0200653 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200654 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200655}
656
Jens Axboe142575e2011-09-30 17:35:45 -0600657int fio_server_log(const char *format, ...)
658{
659 char buffer[1024];
660 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600661 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600662
Jens Axboe60efd142011-10-04 13:30:11 +0200663 dprint(FD_NET, "server log\n");
664
Jens Axboe142575e2011-09-30 17:35:45 -0600665 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600666 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600667 va_end(args);
668
Jens Axboe82fa6b22011-09-30 22:29:08 -0600669 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600670}
Jens Axboee46d8092011-10-03 09:11:02 +0200671
Jens Axboe87aa8f12011-10-06 21:24:13 +0200672static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200673{
Jens Axboe87aa8f12011-10-06 21:24:13 +0200674 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200675
676 sk = socket(AF_INET, SOCK_STREAM, 0);
677 if (sk < 0) {
678 log_err("fio: socket: %s\n", strerror(errno));
679 return -1;
680 }
681
682 opt = 1;
683 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
684 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200685 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200686 return -1;
687 }
688#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200689 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200690 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200691 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200692 return -1;
693 }
694#endif
695
696 saddr_in.sin_family = AF_INET;
Jens Axboe81179ee2011-10-04 12:42:06 +0200697
698 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
699 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200700 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200701 return -1;
702 }
703
Jens Axboe87aa8f12011-10-06 21:24:13 +0200704 return sk;
705}
706
707static int fio_init_server_sock(void)
708{
709 struct sockaddr_un addr;
710 fio_socklen_t len;
711 mode_t mode;
712 int sk;
713
714 sk = socket(AF_UNIX, SOCK_STREAM, 0);
715 if (sk < 0) {
716 log_err("fio: socket: %s\n", strerror(errno));
717 return -1;
718 }
719
720 mode = umask(000);
721
722 memset(&addr, 0, sizeof(addr));
723 addr.sun_family = AF_UNIX;
724 strcpy(addr.sun_path, bind_sock);
725 unlink(bind_sock);
726
727 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
728
729 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
730 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200731 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200732 return -1;
733 }
734
735 umask(mode);
736 return sk;
737}
738
739static int fio_init_server_connection(void)
740{
Jens Axboebebe6392011-10-07 10:00:51 +0200741 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200742 int sk;
743
744 dprint(FD_NET, "starting server\n");
745
746 if (!bind_sock)
747 sk = fio_init_server_ip();
748 else
749 sk = fio_init_server_sock();
750
751 if (sk < 0)
752 return sk;
753
Jens Axboebebe6392011-10-07 10:00:51 +0200754 if (!bind_sock)
755 sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port);
756 else
757 strcpy(bind_str, bind_sock);
758
759 log_info("fio: server listening on %s\n", bind_str);
760
Jens Axboe81179ee2011-10-04 12:42:06 +0200761 if (listen(sk, 1) < 0) {
762 log_err("fio: listen: %s\n", strerror(errno));
763 return -1;
764 }
765
Jens Axboe87aa8f12011-10-06 21:24:13 +0200766 return sk;
767}
768
Jens Axboebebe6392011-10-07 10:00:51 +0200769int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
770 int *port, struct in_addr *inp)
771{
772 *ptr = NULL;
773 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +0200774 *port = fio_net_port;
Jens Axboebebe6392011-10-07 10:00:51 +0200775
776 if (!strncmp(str, "sock:", 5)) {
777 *ptr = strdup(str + 5);
778 *is_sock = 1;
779 } else {
780 const char *host = str;
781 char *portp;
782 int lport = 0;
783
784 /*
785 * Is it ip:<ip or host>:port
786 */
787 if (!strncmp(host, "ip:", 3))
788 host += 3;
789 else if (host[0] == ':') {
790 /* String is :port */
791 host++;
792 lport = atoi(host);
793 if (!lport || lport > 65535) {
794 log_err("fio: bad server port %u\n", port);
795 return 1;
796 }
797 /* no hostname given, we are done */
798 *port = lport;
799 return 0;
800 }
801
802 /*
803 * If no port seen yet, check if there's a last ':' at the end
804 */
805 if (!lport) {
806 portp = strchr(host, ':');
807 if (portp) {
808 *portp = '\0';
809 portp++;
810 lport = atoi(portp);
811 if (!lport || lport > 65535) {
812 log_err("fio: bad server port %u\n", port);
813 return 1;
814 }
815 }
816 }
817
818 if (lport)
819 *port = lport;
820
821 *ptr = strdup(host);
822
823 if (inet_aton(host, inp) != 1) {
824 struct hostent *hent;
825
826 hent = gethostbyname(host);
827 if (!hent) {
Jens Axboebebe6392011-10-07 10:00:51 +0200828 free(*ptr);
829 *ptr = NULL;
830 return 1;
831 }
832
833 memcpy(inp, hent->h_addr, 4);
834 }
835 }
836
837 if (*port == 0)
838 *port = fio_net_port;
839
840 return 0;
841}
842
Jens Axboe87aa8f12011-10-06 21:24:13 +0200843/*
844 * Server arg should be one of:
845 *
846 * sock:/path/to/socket
847 * ip:1.2.3.4
848 * 1.2.3.4
849 *
850 * Where sock uses unix domain sockets, and ip binds the server to
851 * a specific interface. If no arguments are given to the server, it
852 * uses IP and binds to 0.0.0.0.
853 *
854 */
855static int fio_handle_server_arg(void)
856{
Jens Axboe6d2cf392011-10-07 13:19:28 +0200857 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +0200858 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +0200859
Jens Axboe87aa8f12011-10-06 21:24:13 +0200860 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
861
862 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +0200863 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200864
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200865 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe6d2cf392011-10-07 13:19:28 +0200866 &port, &saddr_in.sin_addr);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200867
868 if (!is_sock && bind_sock) {
869 free(bind_sock);
870 bind_sock = NULL;
871 }
872
Jens Axboea7de0a12011-10-07 12:55:14 +0200873out:
Jens Axboe6d2cf392011-10-07 13:19:28 +0200874 fio_net_port = port;
875 saddr_in.sin_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200876 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200877}
878
879static int fio_server(void)
880{
881 int sk, ret;
882
883 dprint(FD_NET, "starting server\n");
884
885 if (fio_handle_server_arg())
886 return -1;
887
888 sk = fio_init_server_connection();
889 if (sk < 0)
890 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200891
892 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200893
Jens Axboe81179ee2011-10-04 12:42:06 +0200894 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200895
896 if (fio_server_arg) {
897 free(fio_server_arg);
898 fio_server_arg = NULL;
899 }
Jens Axboebebe6392011-10-07 10:00:51 +0200900 if (bind_sock)
901 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200902
Jens Axboe81179ee2011-10-04 12:42:06 +0200903 return ret;
904}
905
Jens Axboe9abea482011-10-04 13:21:54 +0200906static void sig_int(int sig)
907{
908 fio_terminate_threads(TERMINATE_ALL);
909 exit_backend = 1;
910}
911
912static void server_signal_handler(void)
913{
914 struct sigaction act;
915
916 memset(&act, 0, sizeof(act));
917 act.sa_handler = sig_int;
918 act.sa_flags = SA_RESTART;
919 sigaction(SIGINT, &act, NULL);
920
921 memset(&act, 0, sizeof(act));
922 act.sa_handler = sig_int;
923 act.sa_flags = SA_RESTART;
924 sigaction(SIGTERM, &act, NULL);
925}
926
Jens Axboe13755d92011-10-10 19:51:26 +0200927static int check_existing_pidfile(const char *pidfile)
928{
929 struct stat sb;
930 char buf[16];
931 pid_t pid;
932 FILE *f;
933
934 if (stat(pidfile, &sb))
935 return 0;
936
937 f = fopen(pidfile, "r");
938 if (!f)
939 return 0;
940
Jens Axboebfc3b172011-10-10 21:16:55 +0200941 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +0200942 fclose(f);
943 return 1;
944 }
945 fclose(f);
946
947 pid = atoi(buf);
948 if (kill(pid, SIGCONT) < 0)
949 return 0;
950
951 return 1;
952}
953
954static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +0200955{
956 FILE *fpid;
957
958 fpid = fopen(pidfile, "w");
959 if (!fpid) {
960 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +0200961 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +0200962 }
963
964 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +0200965 fclose(fpid);
966 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +0200967}
968
969/*
970 * If pidfile is specified, background us.
971 */
972int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +0200973{
974 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +0200975 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +0200976
Jens Axboe9abea482011-10-04 13:21:54 +0200977 server_signal_handler();
978
Jens Axboe402668f2011-10-10 15:28:58 +0200979 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +0200980 return fio_server();
981
Jens Axboe13755d92011-10-10 19:51:26 +0200982 if (check_existing_pidfile(pidfile)) {
983 log_err("fio: pidfile %s exists and server appears alive\n",
984 pidfile);
985 return -1;
986 }
987
Jens Axboee46d8092011-10-03 09:11:02 +0200988 pid = fork();
989 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +0200990 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +0200991 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +0200992 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +0200993 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +0200994 int ret = write_pid(pid, pidfile);
995
996 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +0200997 }
Jens Axboee46d8092011-10-03 09:11:02 +0200998
999 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001000 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1001 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001002 close(STDIN_FILENO);
1003 close(STDOUT_FILENO);
1004 close(STDERR_FILENO);
1005 f_out = NULL;
1006 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001007
1008 ret = fio_server();
1009
1010 closelog();
1011 unlink(pidfile);
1012 free(pidfile);
1013 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001014}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001015
Jens Axboebebe6392011-10-07 10:00:51 +02001016void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001017{
1018 fio_server_arg = strdup(arg);
1019}