blob: 339bb66a6f3b980e6a031434e23a5344f544b9c5 [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;
80 } while (!exit_backend);
81
82 if (!len)
83 return 0;
84
85 return 1;
86}
87
88int fio_recv_data(int sk, void *p, unsigned int len)
89{
90 do {
91 int ret = recv(sk, p, len, MSG_WAITALL);
92
93 if (ret > 0) {
94 len -= ret;
95 if (!len)
96 break;
97 p += ret;
98 continue;
99 } else if (!ret)
100 break;
101 else if (errno == EAGAIN || errno == EINTR)
102 continue;
103 } while (!exit_backend);
104
105 if (!len)
106 return 0;
107
108 return -1;
109}
110
111static int verify_convert_cmd(struct fio_net_cmd *cmd)
112{
Jens Axboefcee5ff2011-09-30 22:50:39 -0600113 uint16_t crc;
Jens Axboe132159a2011-09-30 15:01:32 -0600114
Jens Axboefcee5ff2011-09-30 22:50:39 -0600115 cmd->cmd_crc16 = le16_to_cpu(cmd->cmd_crc16);
116 cmd->pdu_crc16 = le16_to_cpu(cmd->pdu_crc16);
Jens Axboe132159a2011-09-30 15:01:32 -0600117
Jens Axboefcee5ff2011-09-30 22:50:39 -0600118 crc = crc16(cmd, FIO_NET_CMD_CRC_SZ);
119 if (crc != cmd->cmd_crc16) {
Jens Axboe132159a2011-09-30 15:01:32 -0600120 log_err("fio: server bad crc on command (got %x, wanted %x)\n",
Jens Axboefcee5ff2011-09-30 22:50:39 -0600121 cmd->cmd_crc16, crc);
Jens Axboe132159a2011-09-30 15:01:32 -0600122 return 1;
123 }
124
125 cmd->version = le16_to_cpu(cmd->version);
126 cmd->opcode = le16_to_cpu(cmd->opcode);
127 cmd->flags = le32_to_cpu(cmd->flags);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200128 cmd->tag = le64_to_cpu(cmd->tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600129 cmd->pdu_len = le32_to_cpu(cmd->pdu_len);
130
131 switch (cmd->version) {
Jens Axboefa2ea802011-10-08 21:07:29 +0200132 case FIO_SERVER_VER:
Jens Axboe132159a2011-09-30 15:01:32 -0600133 break;
134 default:
135 log_err("fio: bad server cmd version %d\n", cmd->version);
136 return 1;
137 }
138
139 if (cmd->pdu_len > FIO_SERVER_MAX_PDU) {
140 log_err("fio: command payload too large: %u\n", cmd->pdu_len);
141 return 1;
142 }
143
144 return 0;
145}
146
Jens Axboea64e88d2011-10-03 14:20:01 +0200147/*
148 * Read (and defragment, if necessary) incoming commands
149 */
Jens Axboee951bdc2011-10-05 21:58:45 +0200150struct fio_net_cmd *fio_net_recv_cmd(int sk)
Jens Axboe132159a2011-09-30 15:01:32 -0600151{
Jens Axboea64e88d2011-10-03 14:20:01 +0200152 struct fio_net_cmd cmd, *cmdret = NULL;
153 size_t cmd_size = 0, pdu_offset = 0;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600154 uint16_t crc;
Jens Axboea64e88d2011-10-03 14:20:01 +0200155 int ret, first = 1;
156 void *pdu = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600157
Jens Axboea64e88d2011-10-03 14:20:01 +0200158 do {
159 ret = fio_recv_data(sk, &cmd, sizeof(cmd));
160 if (ret)
161 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600162
Jens Axboea64e88d2011-10-03 14:20:01 +0200163 /* We have a command, verify it and swap if need be */
164 ret = verify_convert_cmd(&cmd);
165 if (ret)
166 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600167
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200168 if (first) {
169 /* if this is text, add room for \0 at the end */
170 cmd_size = sizeof(cmd) + cmd.pdu_len + 1;
171 assert(!cmdret);
172 } else
Jens Axboea64e88d2011-10-03 14:20:01 +0200173 cmd_size += cmd.pdu_len;
Jens Axboe132159a2011-09-30 15:01:32 -0600174
Jens Axboea64e88d2011-10-03 14:20:01 +0200175 cmdret = realloc(cmdret, cmd_size);
Jens Axboe132159a2011-09-30 15:01:32 -0600176
Jens Axboea64e88d2011-10-03 14:20:01 +0200177 if (first)
178 memcpy(cmdret, &cmd, sizeof(cmd));
179 else
180 assert(cmdret->opcode == cmd.opcode);
181
182 if (!cmd.pdu_len)
183 break;
184
185 /* There's payload, get it */
186 pdu = (void *) cmdret->payload + pdu_offset;
187 ret = fio_recv_data(sk, pdu, cmd.pdu_len);
188 if (ret)
189 break;
190
191 /* Verify payload crc */
192 crc = crc16(pdu, cmd.pdu_len);
193 if (crc != cmd.pdu_crc16) {
194 log_err("fio: server bad crc on payload ");
195 log_err("(got %x, wanted %x)\n", cmd.pdu_crc16, crc);
196 ret = 1;
197 break;
198 }
199
200 pdu_offset += cmd.pdu_len;
Jens Axboe817f06b2011-10-03 15:03:08 +0200201 if (!first)
202 cmdret->pdu_len += cmd.pdu_len;
Jens Axboea64e88d2011-10-03 14:20:01 +0200203 first = 0;
204 } while (cmd.flags & FIO_NET_CMD_F_MORE);
205
206 if (ret) {
207 free(cmdret);
208 cmdret = NULL;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200209 } else if (cmdret) {
210 /* zero-terminate text input */
211 if (cmdret->pdu_len && (cmdret->opcode == FIO_NET_CMD_TEXT ||
212 cmdret->opcode == FIO_NET_CMD_JOB)) {
213 char *buf = (char *) cmdret->payload;
214
215 buf[cmdret->pdu_len ] = '\0';
216 }
217 /* frag flag is internal */
Jens Axboea64e88d2011-10-03 14:20:01 +0200218 cmdret->flags &= ~FIO_NET_CMD_F_MORE;
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200219 }
Jens Axboe132159a2011-09-30 15:01:32 -0600220
Jens Axboea64e88d2011-10-03 14:20:01 +0200221 return cmdret;
Jens Axboe132159a2011-09-30 15:01:32 -0600222}
223
224void fio_net_cmd_crc(struct fio_net_cmd *cmd)
225{
226 uint32_t pdu_len;
227
Jens Axboeddcc0b62011-10-03 14:45:27 +0200228 cmd->cmd_crc16 = __cpu_to_le16(crc16(cmd, FIO_NET_CMD_CRC_SZ));
Jens Axboe132159a2011-09-30 15:01:32 -0600229
230 pdu_len = le32_to_cpu(cmd->pdu_len);
231 if (pdu_len)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200232 cmd->pdu_crc16 = __cpu_to_le16(crc16(cmd->payload, pdu_len));
Jens Axboe132159a2011-09-30 15:01:32 -0600233}
234
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200235int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size,
236 uint64_t tag)
Jens Axboe794d69c2011-10-01 08:48:50 -0600237{
Jens Axboe7f868312011-10-10 09:55:21 +0200238 struct fio_net_cmd *cmd = NULL;
239 size_t this_len, cur_len = 0;
Jens Axboe794d69c2011-10-01 08:48:50 -0600240 int ret;
241
242 do {
243 this_len = size;
244 if (this_len > FIO_SERVER_MAX_PDU)
245 this_len = FIO_SERVER_MAX_PDU;
246
Jens Axboe7f868312011-10-10 09:55:21 +0200247 if (!cmd || cur_len < sizeof(*cmd) + this_len) {
248 if (cmd)
249 free(cmd);
250
251 cur_len = sizeof(*cmd) + this_len;
252 cmd = malloc(cur_len);
253 }
Jens Axboe794d69c2011-10-01 08:48:50 -0600254
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200255 fio_init_net_cmd(cmd, opcode, buf, this_len, tag);
Jens Axboe794d69c2011-10-01 08:48:50 -0600256
257 if (this_len < size)
Jens Axboeddcc0b62011-10-03 14:45:27 +0200258 cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE);
Jens Axboe794d69c2011-10-01 08:48:50 -0600259
260 fio_net_cmd_crc(cmd);
261
262 ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len);
Jens Axboe794d69c2011-10-01 08:48:50 -0600263 size -= this_len;
264 buf += this_len;
265 } while (!ret && size);
266
Jens Axboe7f868312011-10-10 09:55:21 +0200267 if (cmd)
268 free(cmd);
269
Jens Axboe794d69c2011-10-01 08:48:50 -0600270 return ret;
271}
272
Jens Axboe89c17072011-10-11 10:15:51 +0200273static int fio_net_send_simple_stack_cmd(int sk, uint16_t opcode, uint64_t tag)
Jens Axboe132159a2011-09-30 15:01:32 -0600274{
Jens Axboe178cde92011-10-05 22:14:31 +0200275 struct fio_net_cmd cmd;
Jens Axboe132159a2011-09-30 15:01:32 -0600276
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200277 fio_init_net_cmd(&cmd, opcode, NULL, 0, tag);
Jens Axboe132159a2011-09-30 15:01:32 -0600278 fio_net_cmd_crc(&cmd);
279
280 return fio_send_data(sk, &cmd, sizeof(cmd));
281}
282
Jens Axboe89c17072011-10-11 10:15:51 +0200283/*
284 * If 'list' is non-NULL, then allocate and store the sent command for
285 * later verification.
286 */
287int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag,
288 struct flist_head *list)
289{
290 struct fio_net_int_cmd *cmd;
291 int ret;
292
293 if (!list)
294 return fio_net_send_simple_stack_cmd(sk, opcode, tag);
295
296 cmd = malloc(sizeof(*cmd));
297
298 fio_init_net_cmd(&cmd->cmd, opcode, NULL, 0, (uint64_t) cmd);
299 fio_net_cmd_crc(&cmd->cmd);
300
301 INIT_FLIST_HEAD(&cmd->list);
302 gettimeofday(&cmd->tv, NULL);
303 cmd->saved_tag = tag;
304
305 ret = fio_send_data(sk, &cmd->cmd, sizeof(cmd->cmd));
306 if (ret) {
307 free(cmd);
308 return ret;
309 }
310
311 flist_add_tail(&cmd->list, list);
312 return 0;
313}
314
Jens Axboe9abea482011-10-04 13:21:54 +0200315static int fio_server_send_quit_cmd(void)
Jens Axboe437377e2011-10-01 08:31:30 -0600316{
Jens Axboe46c48f12011-10-01 12:50:51 -0600317 dprint(FD_NET, "server: sending quit\n");
Jens Axboe89c17072011-10-11 10:15:51 +0200318 return fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_QUIT, 0, NULL);
Jens Axboe437377e2011-10-01 08:31:30 -0600319}
320
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200321static int handle_job_cmd(struct fio_net_cmd *cmd)
Jens Axboe132159a2011-09-30 15:01:32 -0600322{
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200323 char *buf = (char *) cmd->payload;
Jens Axboea64e88d2011-10-03 14:20:01 +0200324 int ret;
Jens Axboe132159a2011-09-30 15:01:32 -0600325
Jens Axboee6d1c662011-10-05 20:41:06 +0200326 if (parse_jobs_ini(buf, 1, 0)) {
327 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200328 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200329 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200330
Jens Axboe89c17072011-10-11 10:15:51 +0200331 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200332
333 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200334 fio_server_send_quit_cmd();
Jens Axboe81179ee2011-10-04 12:42:06 +0200335 reset_fio_state();
336 return ret;
337}
338
339static int handle_jobline_cmd(struct fio_net_cmd *cmd)
340{
Jens Axboefa2ea802011-10-08 21:07:29 +0200341 void *pdu = cmd->payload;
342 struct cmd_single_line_pdu *cslp;
343 struct cmd_line_pdu *clp;
344 unsigned long offset;
345 char **argv;
Jens Axboe81179ee2011-10-04 12:42:06 +0200346 int ret, i;
347
Jens Axboefa2ea802011-10-08 21:07:29 +0200348 clp = pdu;
349 clp->lines = le16_to_cpu(clp->lines);
350 argv = malloc(clp->lines * sizeof(char *));
351 offset = sizeof(*clp);
Jens Axboe81179ee2011-10-04 12:42:06 +0200352
Jens Axboefa2ea802011-10-08 21:07:29 +0200353 dprint(FD_NET, "server: %d command line args\n", clp->lines);
Jens Axboe39e8e012011-10-04 13:46:08 +0200354
Jens Axboefa2ea802011-10-08 21:07:29 +0200355 for (i = 0; i < clp->lines; i++) {
356 cslp = pdu + offset;
357 argv[i] = (char *) cslp->text;
358
359 offset += sizeof(*cslp) + le16_to_cpu(cslp->len);
Jens Axboe39e8e012011-10-04 13:46:08 +0200360 dprint(FD_NET, "server: %d: %s\n", i, argv[i]);
361 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200362
Jens Axboefa2ea802011-10-08 21:07:29 +0200363 if (parse_cmd_line(clp->lines, argv)) {
Jens Axboee6d1c662011-10-05 20:41:06 +0200364 fio_server_send_quit_cmd();
Jens Axboefa2ea802011-10-08 21:07:29 +0200365 free(argv);
Jens Axboe81179ee2011-10-04 12:42:06 +0200366 return -1;
Jens Axboee6d1c662011-10-05 20:41:06 +0200367 }
Jens Axboe81179ee2011-10-04 12:42:06 +0200368
Jens Axboefa2ea802011-10-08 21:07:29 +0200369 free(argv);
370
Jens Axboe89c17072011-10-11 10:15:51 +0200371 fio_net_send_simple_cmd(server_fd, FIO_NET_CMD_START, 0, NULL);
Jens Axboe81179ee2011-10-04 12:42:06 +0200372
Jens Axboe794d69c2011-10-01 08:48:50 -0600373 ret = exec_run();
Jens Axboe9abea482011-10-04 13:21:54 +0200374 fio_server_send_quit_cmd();
Jens Axboe794d69c2011-10-01 08:48:50 -0600375 reset_fio_state();
Jens Axboe132159a2011-09-30 15:01:32 -0600376 return ret;
377}
378
Jens Axboec28e8e82011-10-04 08:57:39 +0200379static int handle_probe_cmd(struct fio_net_cmd *cmd)
380{
381 struct cmd_probe_pdu probe;
382
Jens Axboe89c17072011-10-11 10:15:51 +0200383 dprint(FD_NET, "server: sending probe reply\n");
384
Jens Axboec28e8e82011-10-04 08:57:39 +0200385 memset(&probe, 0, sizeof(probe));
386 gethostname((char *) probe.hostname, sizeof(probe.hostname));
Jens Axboe6eb24792011-10-04 15:00:30 +0200387#ifdef FIO_BIG_ENDIAN
388 probe.bigendian = 1;
389#endif
Jens Axboe81179ee2011-10-04 12:42:06 +0200390 probe.fio_major = FIO_MAJOR;
391 probe.fio_minor = FIO_MINOR;
392 probe.fio_patch = FIO_PATCH;
Jens Axboec28e8e82011-10-04 08:57:39 +0200393
Jens Axboecca84642011-10-07 12:47:57 +0200394 probe.os = FIO_OS;
395 probe.arch = FIO_ARCH;
396
Jens Axboe89c17072011-10-11 10:15:51 +0200397 return fio_net_send_cmd(server_fd, FIO_NET_CMD_PROBE, &probe, sizeof(probe), cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200398}
399
400static int handle_send_eta_cmd(struct fio_net_cmd *cmd)
401{
402 struct jobs_eta *je;
403 size_t size;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200404 int i;
405
406 size = sizeof(*je) + thread_number * sizeof(char);
Jens Axboe7f868312011-10-10 09:55:21 +0200407 je = malloc(size);
408 memset(je, 0, size);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200409
410 if (!calc_thread_status(je, 1)) {
411 free(je);
412 return 0;
413 }
414
415 dprint(FD_NET, "server sending status\n");
416
417 je->nr_running = cpu_to_le32(je->nr_running);
418 je->nr_ramp = cpu_to_le32(je->nr_ramp);
419 je->nr_pending = cpu_to_le32(je->nr_pending);
420 je->files_open = cpu_to_le32(je->files_open);
421 je->m_rate = cpu_to_le32(je->m_rate);
422 je->t_rate = cpu_to_le32(je->t_rate);
423 je->m_iops = cpu_to_le32(je->m_iops);
424 je->t_iops = cpu_to_le32(je->t_iops);
425
426 for (i = 0; i < 2; i++) {
427 je->rate[i] = cpu_to_le32(je->rate[i]);
428 je->iops[i] = cpu_to_le32(je->iops[i]);
429 }
430
431 je->elapsed_sec = cpu_to_le32(je->nr_running);
432 je->eta_sec = cpu_to_le64(je->eta_sec);
433
Jens Axboe7f868312011-10-10 09:55:21 +0200434 fio_net_send_cmd(server_fd, FIO_NET_CMD_ETA, je, size, cmd->tag);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200435 free(je);
436 return 0;
Jens Axboec28e8e82011-10-04 08:57:39 +0200437}
438
Jens Axboe132159a2011-09-30 15:01:32 -0600439static int handle_command(struct fio_net_cmd *cmd)
440{
441 int ret;
442
Jens Axboe89c17072011-10-11 10:15:51 +0200443 dprint(FD_NET, "server: got op [%s], pdu=%u, tag=%lx\n",
444 fio_server_op(cmd->opcode), cmd->pdu_len, cmd->tag);
Jens Axboe46c48f12011-10-01 12:50:51 -0600445
Jens Axboe132159a2011-09-30 15:01:32 -0600446 switch (cmd->opcode) {
447 case FIO_NET_CMD_QUIT:
Jens Axboecc0df002011-10-03 20:53:32 +0200448 fio_terminate_threads(TERMINATE_ALL);
Jens Axboec28e8e82011-10-04 08:57:39 +0200449 return -1;
Jens Axboed7959182011-10-03 11:48:39 +0200450 case FIO_NET_CMD_EXIT:
Jens Axboe132159a2011-09-30 15:01:32 -0600451 exit_backend = 1;
Jens Axboec28e8e82011-10-04 08:57:39 +0200452 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600453 case FIO_NET_CMD_JOB:
Jens Axboe0b8f30a2011-10-04 10:31:53 +0200454 ret = handle_job_cmd(cmd);
Jens Axboe132159a2011-09-30 15:01:32 -0600455 break;
Jens Axboe81179ee2011-10-04 12:42:06 +0200456 case FIO_NET_CMD_JOBLINE:
457 ret = handle_jobline_cmd(cmd);
458 break;
Jens Axboec28e8e82011-10-04 08:57:39 +0200459 case FIO_NET_CMD_PROBE:
460 ret = handle_probe_cmd(cmd);
461 break;
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200462 case FIO_NET_CMD_SEND_ETA:
463 ret = handle_send_eta_cmd(cmd);
464 break;
Jens Axboe132159a2011-09-30 15:01:32 -0600465 default:
Jens Axboe89c17072011-10-11 10:15:51 +0200466 log_err("fio: unknown opcode: %s\n",fio_server_op(cmd->opcode));
Jens Axboe132159a2011-09-30 15:01:32 -0600467 ret = 1;
468 }
469
470 return ret;
471}
472
Jens Axboe70e0c312011-10-04 09:18:30 +0200473static int handle_connection(int sk, int block)
Jens Axboe132159a2011-09-30 15:01:32 -0600474{
475 struct fio_net_cmd *cmd = NULL;
476 int ret = 0;
477
478 /* read forever */
479 while (!exit_backend) {
Jens Axboee951bdc2011-10-05 21:58:45 +0200480 struct pollfd pfd = {
481 .fd = sk,
482 .events = POLLIN,
483 };
484
485 ret = 0;
486 do {
487 ret = poll(&pfd, 1, 100);
488 if (ret < 0) {
489 if (errno == EINTR)
490 break;
491 log_err("fio: poll: %s\n", strerror(errno));
492 break;
Jens Axboe19c65172011-10-05 22:05:37 +0200493 } else if (!ret) {
494 if (!block)
495 return 0;
Jens Axboee951bdc2011-10-05 21:58:45 +0200496 continue;
Jens Axboe19c65172011-10-05 22:05:37 +0200497 }
Jens Axboee951bdc2011-10-05 21:58:45 +0200498
499 if (pfd.revents & POLLIN)
500 break;
501 if (pfd.revents & (POLLERR|POLLHUP)) {
502 ret = 1;
503 break;
504 }
Jens Axboe19c65172011-10-05 22:05:37 +0200505 } while (!exit_backend);
Jens Axboee951bdc2011-10-05 21:58:45 +0200506
507 if (ret < 0)
508 break;
509
510 cmd = fio_net_recv_cmd(sk);
Jens Axboe132159a2011-09-30 15:01:32 -0600511 if (!cmd) {
Jens Axboec28e8e82011-10-04 08:57:39 +0200512 ret = -1;
Jens Axboe132159a2011-09-30 15:01:32 -0600513 break;
514 }
515
Jens Axboe132159a2011-09-30 15:01:32 -0600516 ret = handle_command(cmd);
517 if (ret)
518 break;
519
520 free(cmd);
Jens Axboec77a99e2011-10-01 16:26:42 -0400521 cmd = NULL;
Jens Axboe132159a2011-09-30 15:01:32 -0600522 }
523
524 if (cmd)
525 free(cmd);
526
527 return ret;
528}
529
Jens Axboecc0df002011-10-03 20:53:32 +0200530void fio_server_idle_loop(void)
531{
532 if (server_fd != -1)
Jens Axboe70e0c312011-10-04 09:18:30 +0200533 handle_connection(server_fd, 0);
Jens Axboecc0df002011-10-03 20:53:32 +0200534}
535
Jens Axboe50d16972011-09-29 17:45:28 -0600536static int accept_loop(int listen_sk)
537{
Jens Axboebb447a22011-10-04 15:06:42 +0200538 struct sockaddr_in addr;
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200539 fio_socklen_t len = sizeof(addr);
Jens Axboe009b1be2011-09-29 18:27:02 -0600540 struct pollfd pfd;
Jens Axboe132159a2011-09-30 15:01:32 -0600541 int ret, sk, flags, exitval = 0;
Jens Axboe50d16972011-09-29 17:45:28 -0600542
Jens Axboe60efd142011-10-04 13:30:11 +0200543 dprint(FD_NET, "server enter accept loop\n");
544
Jens Axboe009b1be2011-09-29 18:27:02 -0600545 flags = fcntl(listen_sk, F_GETFL);
546 flags |= O_NONBLOCK;
547 fcntl(listen_sk, F_SETFL, flags);
Jens Axboe50d16972011-09-29 17:45:28 -0600548again:
Jens Axboe009b1be2011-09-29 18:27:02 -0600549 pfd.fd = listen_sk;
550 pfd.events = POLLIN;
551 do {
552 ret = poll(&pfd, 1, 100);
553 if (ret < 0) {
554 if (errno == EINTR)
555 break;
Jens Axboefcee5ff2011-09-30 22:50:39 -0600556 log_err("fio: poll: %s\n", strerror(errno));
Jens Axboe009b1be2011-09-29 18:27:02 -0600557 goto out;
558 } else if (!ret)
559 continue;
560
561 if (pfd.revents & POLLIN)
562 break;
563 } while (!exit_backend);
564
565 if (exit_backend)
566 goto out;
567
Jens Axboe6b976bf2011-10-04 15:07:43 +0200568 sk = accept(listen_sk, (struct sockaddr *) &addr, &len);
Jens Axboe50d16972011-09-29 17:45:28 -0600569 if (sk < 0) {
Jens Axboe690e09a2011-09-29 18:38:08 -0600570 log_err("fio: accept: %s\n", strerror(errno));
Jens Axboe50d16972011-09-29 17:45:28 -0600571 return -1;
572 }
573
Jens Axboebb447a22011-10-04 15:06:42 +0200574 dprint(FD_NET, "server: connect from %s\n", inet_ntoa(addr.sin_addr));
Jens Axboe46c48f12011-10-01 12:50:51 -0600575
Jens Axboe37db14f2011-09-30 17:00:42 -0600576 server_fd = sk;
577
Jens Axboe70e0c312011-10-04 09:18:30 +0200578 exitval = handle_connection(sk, 1);
Jens Axboe50d16972011-09-29 17:45:28 -0600579
Jens Axboe37db14f2011-09-30 17:00:42 -0600580 server_fd = -1;
Jens Axboe50d16972011-09-29 17:45:28 -0600581 close(sk);
Jens Axboe5c341e92011-09-29 18:00:35 -0600582
Jens Axboe009b1be2011-09-29 18:27:02 -0600583 if (!exit_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -0600584 goto again;
585
Jens Axboe009b1be2011-09-29 18:27:02 -0600586out:
Jens Axboe132159a2011-09-30 15:01:32 -0600587 return exitval;
Jens Axboe50d16972011-09-29 17:45:28 -0600588}
589
Jens Axboe13755d92011-10-10 19:51:26 +0200590int fio_server_text_output(const char *buf, size_t len)
Jens Axboe37db14f2011-09-30 17:00:42 -0600591{
Jens Axboe337d75a2011-10-01 12:36:32 -0600592 if (server_fd != -1)
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200593 return fio_net_send_cmd(server_fd, FIO_NET_CMD_TEXT, buf, len, 0);
Jens Axboe337d75a2011-10-01 12:36:32 -0600594
Jens Axboe13755d92011-10-10 19:51:26 +0200595 return log_local_buf(buf, len);
Jens Axboe142575e2011-09-30 17:35:45 -0600596}
597
Jens Axboea64e88d2011-10-03 14:20:01 +0200598static void convert_io_stat(struct io_stat *dst, struct io_stat *src)
599{
600 dst->max_val = cpu_to_le64(src->max_val);
601 dst->min_val = cpu_to_le64(src->min_val);
602 dst->samples = cpu_to_le64(src->samples);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200603
604 /*
605 * Encode to IEEE 754 for network transfer
606 */
607 dst->mean.u.i = __cpu_to_le64(fio_double_to_uint64(src->mean.u.f));
608 dst->S.u.i = __cpu_to_le64(fio_double_to_uint64(src->S.u.f));
Jens Axboea64e88d2011-10-03 14:20:01 +0200609}
610
611static void convert_gs(struct group_run_stats *dst, struct group_run_stats *src)
612{
613 int i;
614
615 for (i = 0; i < 2; i++) {
616 dst->max_run[i] = cpu_to_le64(src->max_run[i]);
617 dst->min_run[i] = cpu_to_le64(src->min_run[i]);
618 dst->max_bw[i] = cpu_to_le64(src->max_bw[i]);
619 dst->min_bw[i] = cpu_to_le64(src->min_bw[i]);
620 dst->io_kb[i] = cpu_to_le64(src->io_kb[i]);
621 dst->agg[i] = cpu_to_le64(src->agg[i]);
622 }
623
624 dst->kb_base = cpu_to_le32(src->kb_base);
625 dst->groupid = cpu_to_le32(src->groupid);
626}
627
628/*
629 * Send a CMD_TS, which packs struct thread_stat and group_run_stats
630 * into a single payload.
631 */
632void fio_server_send_ts(struct thread_stat *ts, struct group_run_stats *rs)
633{
634 struct cmd_ts_pdu p;
635 int i, j;
636
Jens Axboe60efd142011-10-04 13:30:11 +0200637 dprint(FD_NET, "server sending end stats\n");
638
Jens Axboe317b3c82011-10-03 21:47:27 +0200639 memset(&p, 0, sizeof(p));
640
Jens Axboea64e88d2011-10-03 14:20:01 +0200641 strcpy(p.ts.name, ts->name);
642 strcpy(p.ts.verror, ts->verror);
643 strcpy(p.ts.description, ts->description);
644
Jens Axboeddcc0b62011-10-03 14:45:27 +0200645 p.ts.error = cpu_to_le32(ts->error);
Jens Axboea64e88d2011-10-03 14:20:01 +0200646 p.ts.groupid = cpu_to_le32(ts->groupid);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200647 p.ts.pid = cpu_to_le32(ts->pid);
Jens Axboea64e88d2011-10-03 14:20:01 +0200648 p.ts.members = cpu_to_le32(ts->members);
649
650 for (i = 0; i < 2; i++) {
651 convert_io_stat(&p.ts.clat_stat[i], &ts->clat_stat[i]);
652 convert_io_stat(&p.ts.slat_stat[i], &ts->slat_stat[i]);
653 convert_io_stat(&p.ts.lat_stat[i], &ts->lat_stat[i]);
654 convert_io_stat(&p.ts.bw_stat[i], &ts->bw_stat[i]);
655 }
656
657 p.ts.usr_time = cpu_to_le64(ts->usr_time);
658 p.ts.sys_time = cpu_to_le64(ts->sys_time);
659 p.ts.ctx = cpu_to_le64(ts->ctx);
660 p.ts.minf = cpu_to_le64(ts->minf);
661 p.ts.majf = cpu_to_le64(ts->majf);
662 p.ts.clat_percentiles = cpu_to_le64(ts->clat_percentiles);
Jens Axboe802ad4a2011-10-05 09:51:58 +0200663
664 for (i = 0; i < FIO_IO_U_LIST_MAX_LEN; i++) {
665 fio_fp64_t *fp = &p.ts.percentile_list[i];
666
667 fp->u.i = __cpu_to_le64(fio_double_to_uint64(fp->u.f));
668 }
Jens Axboea64e88d2011-10-03 14:20:01 +0200669
670 for (i = 0; i < FIO_IO_U_MAP_NR; i++) {
671 p.ts.io_u_map[i] = cpu_to_le32(ts->io_u_map[i]);
672 p.ts.io_u_submit[i] = cpu_to_le32(ts->io_u_submit[i]);
673 p.ts.io_u_complete[i] = cpu_to_le32(ts->io_u_complete[i]);
674 }
675
676 for (i = 0; i < FIO_IO_U_LAT_U_NR; i++) {
677 p.ts.io_u_lat_u[i] = cpu_to_le32(ts->io_u_lat_u[i]);
678 p.ts.io_u_lat_m[i] = cpu_to_le32(ts->io_u_lat_m[i]);
679 }
680
681 for (i = 0; i < 2; i++)
682 for (j = 0; j < FIO_IO_U_PLAT_NR; j++)
683 p.ts.io_u_plat[i][j] = cpu_to_le32(ts->io_u_plat[i][j]);
684
685 for (i = 0; i < 3; i++) {
686 p.ts.total_io_u[i] = cpu_to_le64(ts->total_io_u[i]);
Jens Axboe93eee042011-10-03 21:08:48 +0200687 p.ts.short_io_u[i] = cpu_to_le64(ts->short_io_u[i]);
Jens Axboea64e88d2011-10-03 14:20:01 +0200688 }
689
Jens Axboe93eee042011-10-03 21:08:48 +0200690 p.ts.total_submit = cpu_to_le64(ts->total_submit);
Jens Axboea64e88d2011-10-03 14:20:01 +0200691 p.ts.total_complete = cpu_to_le64(ts->total_complete);
692
693 for (i = 0; i < 2; i++) {
694 p.ts.io_bytes[i] = cpu_to_le64(ts->io_bytes[i]);
695 p.ts.runtime[i] = cpu_to_le64(ts->runtime[i]);
696 }
697
698 p.ts.total_run_time = cpu_to_le64(ts->total_run_time);
699 p.ts.continue_on_error = cpu_to_le16(ts->continue_on_error);
700 p.ts.total_err_count = cpu_to_le64(ts->total_err_count);
Jens Axboeddcc0b62011-10-03 14:45:27 +0200701 p.ts.first_error = cpu_to_le32(ts->first_error);
702 p.ts.kb_base = cpu_to_le32(ts->kb_base);
Jens Axboea64e88d2011-10-03 14:20:01 +0200703
704 convert_gs(&p.rs, rs);
705
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200706 fio_net_send_cmd(server_fd, FIO_NET_CMD_TS, &p, sizeof(p), 0);
Jens Axboea64e88d2011-10-03 14:20:01 +0200707}
708
709void fio_server_send_gs(struct group_run_stats *rs)
710{
711 struct group_run_stats gs;
712
Jens Axboe60efd142011-10-04 13:30:11 +0200713 dprint(FD_NET, "server sending group run stats\n");
714
Jens Axboea64e88d2011-10-03 14:20:01 +0200715 convert_gs(&gs, rs);
Jens Axboeaf9c9fb2011-10-09 21:54:10 +0200716 fio_net_send_cmd(server_fd, FIO_NET_CMD_GS, &gs, sizeof(gs), 0);
Jens Axboecf451d12011-10-03 16:48:30 +0200717}
718
Jens Axboe142575e2011-09-30 17:35:45 -0600719int fio_server_log(const char *format, ...)
720{
721 char buffer[1024];
722 va_list args;
Jens Axboe82fa6b22011-09-30 22:29:08 -0600723 size_t len;
Jens Axboe142575e2011-09-30 17:35:45 -0600724
Jens Axboe60efd142011-10-04 13:30:11 +0200725 dprint(FD_NET, "server log\n");
726
Jens Axboe142575e2011-09-30 17:35:45 -0600727 va_start(args, format);
Jens Axboe82fa6b22011-09-30 22:29:08 -0600728 len = vsnprintf(buffer, sizeof(buffer), format, args);
Jens Axboe142575e2011-09-30 17:35:45 -0600729 va_end(args);
730
Jens Axboe82fa6b22011-09-30 22:29:08 -0600731 return fio_server_text_output(buffer, len);
Jens Axboe37db14f2011-09-30 17:00:42 -0600732}
Jens Axboee46d8092011-10-03 09:11:02 +0200733
Jens Axboe87aa8f12011-10-06 21:24:13 +0200734static int fio_init_server_ip(void)
Jens Axboe81179ee2011-10-04 12:42:06 +0200735{
Jens Axboe87aa8f12011-10-06 21:24:13 +0200736 int sk, opt;
Jens Axboe81179ee2011-10-04 12:42:06 +0200737
738 sk = socket(AF_INET, SOCK_STREAM, 0);
739 if (sk < 0) {
740 log_err("fio: socket: %s\n", strerror(errno));
741 return -1;
742 }
743
744 opt = 1;
745 if (setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
746 log_err("fio: setsockopt: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200747 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200748 return -1;
749 }
750#ifdef SO_REUSEPORT
Jens Axboe6eb24792011-10-04 15:00:30 +0200751 if (setsockopt(sk, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200752 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#endif
757
758 saddr_in.sin_family = AF_INET;
Jens Axboe81179ee2011-10-04 12:42:06 +0200759
760 if (bind(sk, (struct sockaddr *) &saddr_in, sizeof(saddr_in)) < 0) {
761 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200762 close(sk);
Jens Axboe81179ee2011-10-04 12:42:06 +0200763 return -1;
764 }
765
Jens Axboe87aa8f12011-10-06 21:24:13 +0200766 return sk;
767}
768
769static int fio_init_server_sock(void)
770{
771 struct sockaddr_un addr;
772 fio_socklen_t len;
773 mode_t mode;
774 int sk;
775
776 sk = socket(AF_UNIX, SOCK_STREAM, 0);
777 if (sk < 0) {
778 log_err("fio: socket: %s\n", strerror(errno));
779 return -1;
780 }
781
782 mode = umask(000);
783
784 memset(&addr, 0, sizeof(addr));
785 addr.sun_family = AF_UNIX;
786 strcpy(addr.sun_path, bind_sock);
787 unlink(bind_sock);
788
789 len = sizeof(addr.sun_family) + strlen(bind_sock) + 1;
790
791 if (bind(sk, (struct sockaddr *) &addr, len) < 0) {
792 log_err("fio: bind: %s\n", strerror(errno));
Jens Axboeb94cba42011-10-06 21:27:10 +0200793 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200794 return -1;
795 }
796
797 umask(mode);
798 return sk;
799}
800
801static int fio_init_server_connection(void)
802{
Jens Axboebebe6392011-10-07 10:00:51 +0200803 char bind_str[128];
Jens Axboe87aa8f12011-10-06 21:24:13 +0200804 int sk;
805
806 dprint(FD_NET, "starting server\n");
807
808 if (!bind_sock)
809 sk = fio_init_server_ip();
810 else
811 sk = fio_init_server_sock();
812
813 if (sk < 0)
814 return sk;
815
Jens Axboebebe6392011-10-07 10:00:51 +0200816 if (!bind_sock)
817 sprintf(bind_str, "%s:%u", inet_ntoa(saddr_in.sin_addr), fio_net_port);
818 else
819 strcpy(bind_str, bind_sock);
820
821 log_info("fio: server listening on %s\n", bind_str);
822
Jens Axboe89c17072011-10-11 10:15:51 +0200823 if (listen(sk, 0) < 0) {
Jens Axboe81179ee2011-10-04 12:42:06 +0200824 log_err("fio: listen: %s\n", strerror(errno));
825 return -1;
826 }
827
Jens Axboe87aa8f12011-10-06 21:24:13 +0200828 return sk;
829}
830
Jens Axboebebe6392011-10-07 10:00:51 +0200831int fio_server_parse_string(const char *str, char **ptr, int *is_sock,
832 int *port, struct in_addr *inp)
833{
834 *ptr = NULL;
835 *is_sock = 0;
Jens Axboe6d2cf392011-10-07 13:19:28 +0200836 *port = fio_net_port;
Jens Axboebebe6392011-10-07 10:00:51 +0200837
838 if (!strncmp(str, "sock:", 5)) {
839 *ptr = strdup(str + 5);
840 *is_sock = 1;
841 } else {
842 const char *host = str;
843 char *portp;
844 int lport = 0;
845
846 /*
847 * Is it ip:<ip or host>:port
848 */
849 if (!strncmp(host, "ip:", 3))
850 host += 3;
851 else if (host[0] == ':') {
852 /* String is :port */
853 host++;
854 lport = atoi(host);
855 if (!lport || lport > 65535) {
856 log_err("fio: bad server port %u\n", port);
857 return 1;
858 }
859 /* no hostname given, we are done */
860 *port = lport;
861 return 0;
862 }
863
864 /*
865 * If no port seen yet, check if there's a last ':' at the end
866 */
867 if (!lport) {
868 portp = strchr(host, ':');
869 if (portp) {
870 *portp = '\0';
871 portp++;
872 lport = atoi(portp);
873 if (!lport || lport > 65535) {
874 log_err("fio: bad server port %u\n", port);
875 return 1;
876 }
877 }
878 }
879
880 if (lport)
881 *port = lport;
882
883 *ptr = strdup(host);
884
885 if (inet_aton(host, inp) != 1) {
886 struct hostent *hent;
887
888 hent = gethostbyname(host);
889 if (!hent) {
Jens Axboebebe6392011-10-07 10:00:51 +0200890 free(*ptr);
891 *ptr = NULL;
892 return 1;
893 }
894
895 memcpy(inp, hent->h_addr, 4);
896 }
897 }
898
899 if (*port == 0)
900 *port = fio_net_port;
901
902 return 0;
903}
904
Jens Axboe87aa8f12011-10-06 21:24:13 +0200905/*
906 * Server arg should be one of:
907 *
908 * sock:/path/to/socket
909 * ip:1.2.3.4
910 * 1.2.3.4
911 *
912 * Where sock uses unix domain sockets, and ip binds the server to
913 * a specific interface. If no arguments are given to the server, it
914 * uses IP and binds to 0.0.0.0.
915 *
916 */
917static int fio_handle_server_arg(void)
918{
Jens Axboe6d2cf392011-10-07 13:19:28 +0200919 int port = fio_net_port;
Jens Axboea7de0a12011-10-07 12:55:14 +0200920 int is_sock, ret = 0;
Jens Axboebebe6392011-10-07 10:00:51 +0200921
Jens Axboe87aa8f12011-10-06 21:24:13 +0200922 saddr_in.sin_addr.s_addr = htonl(INADDR_ANY);
923
924 if (!fio_server_arg)
Jens Axboea7de0a12011-10-07 12:55:14 +0200925 goto out;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200926
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200927 ret = fio_server_parse_string(fio_server_arg, &bind_sock, &is_sock,
Jens Axboe6d2cf392011-10-07 13:19:28 +0200928 &port, &saddr_in.sin_addr);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200929
930 if (!is_sock && bind_sock) {
931 free(bind_sock);
932 bind_sock = NULL;
933 }
934
Jens Axboea7de0a12011-10-07 12:55:14 +0200935out:
Jens Axboe6d2cf392011-10-07 13:19:28 +0200936 fio_net_port = port;
937 saddr_in.sin_port = htons(port);
Jens Axboe4e5b8fb2011-10-07 10:03:44 +0200938 return ret;
Jens Axboe87aa8f12011-10-06 21:24:13 +0200939}
940
941static int fio_server(void)
942{
943 int sk, ret;
944
945 dprint(FD_NET, "starting server\n");
946
947 if (fio_handle_server_arg())
948 return -1;
949
950 sk = fio_init_server_connection();
951 if (sk < 0)
952 return -1;
Jens Axboe81179ee2011-10-04 12:42:06 +0200953
954 ret = accept_loop(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200955
Jens Axboe81179ee2011-10-04 12:42:06 +0200956 close(sk);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200957
958 if (fio_server_arg) {
959 free(fio_server_arg);
960 fio_server_arg = NULL;
961 }
Jens Axboebebe6392011-10-07 10:00:51 +0200962 if (bind_sock)
963 free(bind_sock);
Jens Axboe87aa8f12011-10-06 21:24:13 +0200964
Jens Axboe81179ee2011-10-04 12:42:06 +0200965 return ret;
966}
967
Jens Axboe9abea482011-10-04 13:21:54 +0200968static void sig_int(int sig)
969{
970 fio_terminate_threads(TERMINATE_ALL);
971 exit_backend = 1;
972}
973
974static void server_signal_handler(void)
975{
976 struct sigaction act;
977
978 memset(&act, 0, sizeof(act));
979 act.sa_handler = sig_int;
980 act.sa_flags = SA_RESTART;
981 sigaction(SIGINT, &act, NULL);
982
983 memset(&act, 0, sizeof(act));
984 act.sa_handler = sig_int;
985 act.sa_flags = SA_RESTART;
986 sigaction(SIGTERM, &act, NULL);
987}
988
Jens Axboe13755d92011-10-10 19:51:26 +0200989static int check_existing_pidfile(const char *pidfile)
990{
991 struct stat sb;
992 char buf[16];
993 pid_t pid;
994 FILE *f;
995
996 if (stat(pidfile, &sb))
997 return 0;
998
999 f = fopen(pidfile, "r");
1000 if (!f)
1001 return 0;
1002
Jens Axboebfc3b172011-10-10 21:16:55 +02001003 if (fread(buf, sb.st_size, 1, f) <= 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001004 fclose(f);
1005 return 1;
1006 }
1007 fclose(f);
1008
1009 pid = atoi(buf);
1010 if (kill(pid, SIGCONT) < 0)
1011 return 0;
1012
1013 return 1;
1014}
1015
1016static int write_pid(pid_t pid, const char *pidfile)
Jens Axboe402668f2011-10-10 15:28:58 +02001017{
1018 FILE *fpid;
1019
1020 fpid = fopen(pidfile, "w");
1021 if (!fpid) {
1022 log_err("fio: failed opening pid file %s\n", pidfile);
Jens Axboe13755d92011-10-10 19:51:26 +02001023 return 1;
Jens Axboe402668f2011-10-10 15:28:58 +02001024 }
1025
1026 fprintf(fpid, "%u\n", (unsigned int) pid);
Jens Axboe13755d92011-10-10 19:51:26 +02001027 fclose(fpid);
1028 return 0;
Jens Axboe402668f2011-10-10 15:28:58 +02001029}
1030
1031/*
1032 * If pidfile is specified, background us.
1033 */
1034int fio_start_server(char *pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001035{
1036 pid_t pid;
Jens Axboe402668f2011-10-10 15:28:58 +02001037 int ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001038
Jens Axboe9abea482011-10-04 13:21:54 +02001039 server_signal_handler();
1040
Jens Axboe402668f2011-10-10 15:28:58 +02001041 if (!pidfile)
Jens Axboee46d8092011-10-03 09:11:02 +02001042 return fio_server();
1043
Jens Axboe13755d92011-10-10 19:51:26 +02001044 if (check_existing_pidfile(pidfile)) {
1045 log_err("fio: pidfile %s exists and server appears alive\n",
1046 pidfile);
1047 return -1;
1048 }
1049
Jens Axboee46d8092011-10-03 09:11:02 +02001050 pid = fork();
1051 if (pid < 0) {
Jens Axboe13755d92011-10-10 19:51:26 +02001052 log_err("fio: failed server fork: %s", strerror(errno));
Jens Axboe402668f2011-10-10 15:28:58 +02001053 free(pidfile);
Jens Axboec28e8e82011-10-04 08:57:39 +02001054 return -1;
Jens Axboe402668f2011-10-10 15:28:58 +02001055 } else if (pid) {
Jens Axboe13755d92011-10-10 19:51:26 +02001056 int ret = write_pid(pid, pidfile);
1057
1058 exit(ret);
Jens Axboe402668f2011-10-10 15:28:58 +02001059 }
Jens Axboee46d8092011-10-03 09:11:02 +02001060
1061 setsid();
Jens Axboe13755d92011-10-10 19:51:26 +02001062 openlog("fio", LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_USER);
1063 log_syslog = 1;
Jens Axboee46d8092011-10-03 09:11:02 +02001064 close(STDIN_FILENO);
1065 close(STDOUT_FILENO);
1066 close(STDERR_FILENO);
1067 f_out = NULL;
1068 f_err = NULL;
Jens Axboe402668f2011-10-10 15:28:58 +02001069
1070 ret = fio_server();
1071
1072 closelog();
1073 unlink(pidfile);
1074 free(pidfile);
1075 return ret;
Jens Axboee46d8092011-10-03 09:11:02 +02001076}
Jens Axboe87aa8f12011-10-06 21:24:13 +02001077
Jens Axboebebe6392011-10-07 10:00:51 +02001078void fio_server_set_arg(const char *arg)
Jens Axboe87aa8f12011-10-06 21:24:13 +02001079{
1080 fio_server_arg = strdup(arg);
1081}