Skip to content

Commit 52cb284

Browse files
problamebehlendorf
authored andcommitted
ztest: propagate -o to the zdb child process
I think this is the behavior that most users expect. Future work: have a separate flag, e.g., -O, to specify separate set_global_vars for the zdb child than for the ztest children. Reviewed-by: Matthew Ahrens <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Pavel Zakharov <[email protected]> Signed-off-by: Christian Schwarz <[email protected]> Closes #11602
1 parent 2801e68 commit 52cb284

File tree

1 file changed

+79
-23
lines changed

1 file changed

+79
-23
lines changed

cmd/ztest/ztest.c

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6392,6 +6392,75 @@ ztest_fletcher_incr(ztest_ds_t *zd, uint64_t id)
63926392
}
63936393
}
63946394

6395+
static int
6396+
ztest_set_global_vars(void)
6397+
{
6398+
for (size_t i = 0; i < ztest_opts.zo_gvars_count; i++) {
6399+
char *kv = ztest_opts.zo_gvars[i];
6400+
VERIFY3U(strlen(kv), <=, ZO_GVARS_MAX_ARGLEN);
6401+
VERIFY3U(strlen(kv), >, 0);
6402+
int err = set_global_var(kv);
6403+
if (ztest_opts.zo_verbose > 0) {
6404+
(void) printf("setting global var %s ... %s\n", kv,
6405+
err ? "failed" : "ok");
6406+
}
6407+
if (err != 0) {
6408+
(void) fprintf(stderr,
6409+
"failed to set global var '%s'\n", kv);
6410+
return (err);
6411+
}
6412+
}
6413+
return (0);
6414+
}
6415+
6416+
static char **
6417+
ztest_global_vars_to_zdb_args(void)
6418+
{
6419+
char **args = calloc(2*ztest_opts.zo_gvars_count + 1, sizeof (char *));
6420+
char **cur = args;
6421+
for (size_t i = 0; i < ztest_opts.zo_gvars_count; i++) {
6422+
char *kv = ztest_opts.zo_gvars[i];
6423+
*cur = "-o";
6424+
cur++;
6425+
*cur = strdup(kv);
6426+
cur++;
6427+
}
6428+
ASSERT3P(cur, ==, &args[2*ztest_opts.zo_gvars_count]);
6429+
*cur = NULL;
6430+
return (args);
6431+
}
6432+
6433+
/* The end of strings is indicated by a NULL element */
6434+
static char *
6435+
join_strings(char **strings, const char *sep)
6436+
{
6437+
size_t totallen = 0;
6438+
for (char **sp = strings; *sp != NULL; sp++) {
6439+
totallen += strlen(*sp);
6440+
totallen += strlen(sep);
6441+
}
6442+
if (totallen > 0) {
6443+
ASSERT(totallen >= strlen(sep));
6444+
totallen -= strlen(sep);
6445+
}
6446+
6447+
size_t buflen = totallen + 1;
6448+
char *o = malloc(buflen); /* trailing 0 byte */
6449+
o[0] = '\0';
6450+
for (char **sp = strings; *sp != NULL; sp++) {
6451+
size_t would;
6452+
would = strlcat(o, *sp, buflen);
6453+
VERIFY3U(would, <, buflen);
6454+
if (*(sp+1) == NULL) {
6455+
break;
6456+
}
6457+
would = strlcat(o, sep, buflen);
6458+
VERIFY3U(would, <, buflen);
6459+
}
6460+
ASSERT3S(strlen(o), ==, totallen);
6461+
return (o);
6462+
}
6463+
63956464
static int
63966465
ztest_check_path(char *path)
63976466
{
@@ -6620,13 +6689,21 @@ ztest_run_zdb(char *pool)
66206689

66216690
ztest_get_zdb_bin(bin, len);
66226691

6623-
(void) sprintf(zdb,
6624-
"%s -bcc%s%s -G -d -Y -e -y -p %s %s",
6692+
char **set_gvars_args = ztest_global_vars_to_zdb_args();
6693+
char *set_gvars_args_joined = join_strings(set_gvars_args, " ");
6694+
free(set_gvars_args);
6695+
6696+
size_t would = snprintf(zdb, len,
6697+
"%s -bcc%s%s -G -d -Y -e -y %s -p %s %s",
66256698
bin,
66266699
ztest_opts.zo_verbose >= 3 ? "s" : "",
66276700
ztest_opts.zo_verbose >= 4 ? "v" : "",
6701+
set_gvars_args_joined,
66286702
ztest_opts.zo_dir,
66296703
pool);
6704+
ASSERT3U(would, <, len);
6705+
6706+
free(set_gvars_args_joined);
66306707

66316708
if (ztest_opts.zo_verbose >= 5)
66326709
(void) printf("Executing %s\n", strstr(zdb, "zdb "));
@@ -7630,27 +7707,6 @@ setup_data(void)
76307707
ztest_shared_ds = (void *)&buf[offset];
76317708
}
76327709

7633-
static int
7634-
ztest_set_global_vars(void)
7635-
{
7636-
for (size_t i = 0; i < ztest_opts.zo_gvars_count; i++) {
7637-
char *kv = ztest_opts.zo_gvars[i];
7638-
VERIFY3U(strlen(kv), <=, ZO_GVARS_MAX_ARGLEN);
7639-
VERIFY3U(strlen(kv), >, 0);
7640-
int err = set_global_var(kv);
7641-
if (ztest_opts.zo_verbose > 0) {
7642-
(void) printf("setting global var %s ... %s\n", kv,
7643-
err ? "failed" : "ok");
7644-
}
7645-
if (err != 0) {
7646-
(void) fprintf(stderr,
7647-
"failed to set global var '%s'\n", kv);
7648-
return (err);
7649-
}
7650-
}
7651-
return (0);
7652-
}
7653-
76547710
static boolean_t
76557711
exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
76567712
{

0 commit comments

Comments
 (0)