server: debug fixes
[fio.git] / server.c
index f393873f44486f949ff397408a31cdd53e431a07..79c10410a316b05a21ccd83241e529bfa91e24f3 100644 (file)
--- a/server.c
+++ b/server.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <limits.h>
 #include <errno.h>
@@ -24,6 +25,8 @@ static char *job_buf;
 static unsigned int job_cur_len;
 static unsigned int job_max_len;
 
+static int server_fd;
+
 int fio_send_data(int sk, const void *p, unsigned int len)
 {
        do {
@@ -106,7 +109,7 @@ static int verify_convert_cmd(struct fio_net_cmd *cmd)
        return 0;
 }
 
-static struct fio_net_cmd *read_command(int sk)
+struct fio_net_cmd *fio_net_cmd_read(int sk)
 {
        struct fio_net_cmd cmd, *ret = NULL;
        uint32_t crc;
@@ -140,7 +143,6 @@ static struct fio_net_cmd *read_command(int sk)
                return NULL;
        }
 
-
        return ret;
 }
 
@@ -242,7 +244,7 @@ static int handle_connection(int sk)
 
        /* read forever */
        while (!exit_backend) {
-               cmd = read_command(sk);
+               cmd = fio_net_cmd_read(sk);
                if (!cmd) {
                        ret = 1;
                        break;
@@ -301,8 +303,11 @@ again:
                return -1;
        }
 
+       server_fd = sk;
+
        exitval = handle_connection(sk);
 
+       server_fd = -1;
        close(sk);
 
        if (!exit_backend)
@@ -361,3 +366,30 @@ int fio_server(void)
        close(sk);
        return ret;
 }
+
+int fio_server_text_output(const char *buf, unsigned int len)
+{
+       struct fio_net_cmd *cmd;
+       int size = sizeof(*cmd) + len;
+
+       cmd = malloc(size);
+       fio_init_net_cmd(cmd, FIO_NET_CMD_TEXT, buf, len);
+       fio_net_cmd_crc(cmd);
+
+       fio_send_data(server_fd, cmd, size);
+       free(cmd);
+       return size;
+}
+
+int fio_server_log(const char *format, ...)
+{
+       char buffer[1024];
+       va_list args;
+       size_t len;
+
+       va_start(args, format);
+       len = vsnprintf(buffer, sizeof(buffer), format, args);
+       va_end(args);
+
+       return fio_server_text_output(buffer, len);
+}