Skip to content

Commit 2801e68

Browse files
problamebehlendorf
authored andcommitted
ztest: fix -o by calling set_global_var in child processes
Without set_global_var() in the child processes the -o option provides little use. Before this change set_global_var() was called as a side-effect of getopt processing which only happens for the parent ztest process. This change limits the set of options that can be set and makes them available to the child through ztest_shared_opts_t. Future work: support arbitrary option count and length. 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 edc508a commit 2801e68

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

cmd/ztest/ztest.c

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ enum ztest_class_state {
158158
ZTEST_VDEV_CLASS_RND
159159
};
160160

161+
#define ZO_GVARS_MAX_ARGLEN ((size_t)64)
162+
#define ZO_GVARS_MAX_COUNT ((size_t)10)
163+
161164
typedef struct ztest_shared_opts {
162165
char zo_pool[ZFS_MAX_DATASET_NAME_LEN];
163166
char zo_dir[ZFS_MAX_DATASET_NAME_LEN];
@@ -185,6 +188,8 @@ typedef struct ztest_shared_opts {
185188
int zo_mmp_test;
186189
int zo_special_vdevs;
187190
int zo_dump_dbgmsg;
191+
int zo_gvars_count;
192+
char zo_gvars[ZO_GVARS_MAX_COUNT][ZO_GVARS_MAX_ARGLEN];
188193
} ztest_shared_opts_t;
189194

190195
static const ztest_shared_opts_t ztest_opts_defaults = {
@@ -212,6 +217,7 @@ static const ztest_shared_opts_t ztest_opts_defaults = {
212217
.zo_maxloops = 50, /* max loops during spa_freeze() */
213218
.zo_metaslab_force_ganging = 64 << 10,
214219
.zo_special_vdevs = ZTEST_VDEV_CLASS_RND,
220+
.zo_gvars_count = 0,
215221
};
216222

217223
extern uint64_t metaslab_force_ganging;
@@ -918,8 +924,21 @@ process_options(int argc, char **argv)
918924
ztest_parse_name_value(optarg, zo);
919925
break;
920926
case 'o':
921-
if (set_global_var(optarg) != 0)
927+
if (zo->zo_gvars_count >= ZO_GVARS_MAX_COUNT) {
928+
(void) fprintf(stderr,
929+
"max global var count (%zu) exceeded\n",
930+
ZO_GVARS_MAX_COUNT);
931+
usage(B_FALSE);
932+
}
933+
char *v = zo->zo_gvars[zo->zo_gvars_count];
934+
if (strlcpy(v, optarg, ZO_GVARS_MAX_ARGLEN) >=
935+
ZO_GVARS_MAX_ARGLEN) {
936+
(void) fprintf(stderr,
937+
"global var option '%s' is too long\n",
938+
optarg);
922939
usage(B_FALSE);
940+
}
941+
zo->zo_gvars_count++;
923942
break;
924943
case 'G':
925944
zo->zo_dump_dbgmsg = 1;
@@ -7611,6 +7630,27 @@ setup_data(void)
76117630
ztest_shared_ds = (void *)&buf[offset];
76127631
}
76137632

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+
76147654
static boolean_t
76157655
exec_child(char *cmd, char *libpath, boolean_t ignorekill, int *statusp)
76167656
{
@@ -7727,7 +7767,7 @@ main(int argc, char **argv)
77277767
char numbuf[NN_NUMBUF_SZ];
77287768
char *cmd;
77297769
boolean_t hasalt;
7730-
int f;
7770+
int f, err;
77317771
char *fd_data_str = getenv("ZTEST_FD_DATA");
77327772
struct sigaction action;
77337773

@@ -7794,6 +7834,15 @@ main(int argc, char **argv)
77947834
}
77957835
ASSERT3U(ztest_opts.zo_datasets, ==, ztest_shared_hdr->zh_ds_count);
77967836

7837+
err = ztest_set_global_vars();
7838+
if (err != 0 && !fd_data_str) {
7839+
/* error message done by ztest_set_global_vars */
7840+
exit(EXIT_FAILURE);
7841+
} else {
7842+
/* children should not be spawned if setting gvars fails */
7843+
VERIFY3S(err, ==, 0);
7844+
}
7845+
77977846
/* Override location of zpool.cache */
77987847
VERIFY3S(asprintf((char **)&spa_config_path, "%s/zpool.cache",
77997848
ztest_opts.zo_dir), !=, -1);

0 commit comments

Comments
 (0)