client: parse env variables before sending job-file contents to server
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Fri, 15 Jun 2018 14:58:22 +0000 (08:58 -0600)
committerJens Axboe <axboe@kernel.dk>
Fri, 15 Jun 2018 14:58:22 +0000 (08:58 -0600)
Fixes "fio environment var bug".
https://www.spinics.net/lists/fio/msg07093.html

Add read_ini_data() to parse and expand env variables within job-file
before sending to server. By doing this, clients can control parameters
embedded within the job-file, without server side having to set them
separately.

Reported-by: Jeff Furlong <jeff.furlong@wdc.com>
Signed-off-by: Tomohiro Kusumi <kusumi.tomohiro@gmail.com>
Minor fixups from Jens.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
client.c
fio.h
options.c

index 96cc35e66bdc9c44f8991aec8ce9ab7bdc44dcf2..60f7c6ab82767cab31cf30443e07affa15c32b51 100644 (file)
--- a/client.c
+++ b/client.c
@@ -118,6 +118,49 @@ static int read_data(int fd, void *data, size_t size)
        return 0;
 }
 
+static int read_ini_data(int fd, void *data, size_t size)
+{
+       char *p = data;
+       int ret = 0;
+       FILE *fp;
+
+       fp = fdopen(dup(fd), "r");
+       if (!fp)
+               return errno;
+
+       while (1) {
+               ssize_t len;
+               char buf[OPT_LEN_MAX+1], *sub;
+
+               if (!fgets(buf, sizeof(buf), fp)) {
+                       if (ferror(fp)) {
+                               if (errno == EAGAIN || errno == EINTR)
+                                       continue;
+                               ret = errno;
+                       }
+                       break;
+               }
+
+               sub = fio_option_dup_subs(buf);
+               len = strlen(sub);
+               if (len + 1 > size) {
+                       log_err("fio: no space left to read data\n");
+                       free(sub);
+                       ret = ENOSPC;
+                       break;
+               }
+
+               memcpy(p, sub, len);
+               free(sub);
+               p += len;
+               *p = '\0';
+               size -= len;
+       }
+
+       fclose(fp);
+       return ret;
+}
+
 static void fio_client_json_init(void)
 {
        char time_buf[32];
@@ -763,13 +806,17 @@ static int __fio_client_send_local_ini(struct fio_client *client,
                return ret;
        }
 
+       /*
+        * Add extra space for variable expansion, but doesn't guarantee.
+        */
+       sb.st_size += OPT_LEN_MAX;
        p_size = sb.st_size + sizeof(*pdu);
        pdu = malloc(p_size);
        buf = pdu->buf;
 
        len = sb.st_size;
        p = buf;
-       if (read_data(fd, p, len)) {
+       if (read_ini_data(fd, p, len)) {
                log_err("fio: failed reading job file %s\n", filename);
                close(fd);
                free(pdu);
diff --git a/fio.h b/fio.h
index 9f3140a97af6c26bf0fbff22be3429d938686c8b..9727f6c65556c4f937adad573cc2429a02ce6aa2 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -567,6 +567,7 @@ extern void fio_fill_default_options(struct thread_data *);
 extern int fio_show_option_help(const char *);
 extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
 extern void fio_options_dup_and_init(struct option *);
+extern char *fio_option_dup_subs(const char *);
 extern void fio_options_mem_dupe(struct thread_data *);
 extern void td_fill_rand_seeds(struct thread_data *);
 extern void td_fill_verify_state_seed(struct thread_data *);
index 9fbcd96d9ee1f4987fbbfad8e5eac896e6dd8dad..0c4f89c4d9abdec07633629d7ce1bee64e601aa4 100644 (file)
--- a/options.c
+++ b/options.c
@@ -4790,7 +4790,7 @@ static char *bc_calc(char *str)
  * substitution always occurs, even if VARNAME is empty or the corresponding
  * environment variable undefined.
  */
-static char *option_dup_subs(const char *opt)
+char *fio_option_dup_subs(const char *opt)
 {
        char out[OPT_LEN_MAX+1];
        char in[OPT_LEN_MAX+1];
@@ -4895,7 +4895,7 @@ static char **dup_and_sub_options(char **opts, int num_opts)
        int i;
        char **opts_copy = malloc(num_opts * sizeof(*opts));
        for (i = 0; i < num_opts; i++) {
-               opts_copy[i] = option_dup_subs(opts[i]);
+               opts_copy[i] = fio_option_dup_subs(opts[i]);
                if (!opts_copy[i])
                        continue;
                opts_copy[i] = fio_keyword_replace(opts_copy[i]);