aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorнаб <[email protected]>2022-04-19 20:38:30 +0200
committerBrian Behlendorf <[email protected]>2022-06-29 14:08:54 -0700
commita926aab902ac5c680f4766568d19674b80fb58bb (patch)
tree2f505528a4dea156b50c5602dbcefe48b9e902d7 /tests
parente7d90362e5d5f873e1272519da96780cf00a0e28 (diff)
Enable -Wwrite-strings
Also, fix leak from ztest_global_vars_to_zdb_args() Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Ahelenia Ziemiańska <[email protected]> Closes #13348
Diffstat (limited to 'tests')
-rw-r--r--tests/zfs-tests/cmd/ctime.c6
-rw-r--r--tests/zfs-tests/cmd/dir_rd_update.c2
-rw-r--r--tests/zfs-tests/cmd/draid.c10
-rw-r--r--tests/zfs-tests/cmd/get_diff.c5
-rw-r--r--tests/zfs-tests/cmd/libzfs_input_check.c4
-rw-r--r--tests/zfs-tests/cmd/mkbusy.c4
-rw-r--r--tests/zfs-tests/cmd/mkfiles.c18
-rw-r--r--tests/zfs-tests/cmd/mktree.c2
-rw-r--r--tests/zfs-tests/cmd/nvlist_to_lua.c6
-rw-r--r--tests/zfs-tests/cmd/readmmap.c2
-rw-r--r--tests/zfs-tests/cmd/stride_dd.c2
-rw-r--r--tests/zfs-tests/cmd/xattrtest.c18
-rw-r--r--tests/zfs-tests/tests/functional/hkdf/hkdf_test.c14
-rw-r--r--tests/zfs-tests/tests/functional/tmpfile/tmpfile_001_pos.c81
-rw-r--r--tests/zfs-tests/tests/functional/tmpfile/tmpfile_002_pos.c70
-rw-r--r--tests/zfs-tests/tests/functional/tmpfile/tmpfile_003_pos.c33
-rw-r--r--tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c50
17 files changed, 126 insertions, 201 deletions
diff --git a/tests/zfs-tests/cmd/ctime.c b/tests/zfs-tests/cmd/ctime.c
index cb2be72ee..6d9cf9608 100644
--- a/tests/zfs-tests/cmd/ctime.c
+++ b/tests/zfs-tests/cmd/ctime.c
@@ -51,7 +51,7 @@
typedef struct timetest {
int type;
- char *name;
+ const char *name;
int (*func)(const char *pfile);
} timetest_t;
@@ -260,7 +260,7 @@ static int
do_xattr(const char *pfile)
{
int ret = 0;
- char *value = "user.value";
+ const char *value = "user.value";
if (pfile == NULL) {
return (-1);
@@ -306,7 +306,7 @@ int
main(void)
{
int i, ret, fd;
- char *penv[] = {"TESTDIR", "TESTFILE0"};
+ const char *penv[] = {"TESTDIR", "TESTFILE0"};
(void) atexit(cleanup);
diff --git a/tests/zfs-tests/cmd/dir_rd_update.c b/tests/zfs-tests/cmd/dir_rd_update.c
index ea46f047a..0f460fbec 100644
--- a/tests/zfs-tests/cmd/dir_rd_update.c
+++ b/tests/zfs-tests/cmd/dir_rd_update.c
@@ -47,7 +47,7 @@ static char dirpath[256];
int
main(int argc, char **argv)
{
- char *cp1 = "";
+ const char *cp1 = "";
int i = 0;
int ret = 0;
int testdd = 0;
diff --git a/tests/zfs-tests/cmd/draid.c b/tests/zfs-tests/cmd/draid.c
index 618cb9c4d..d1183b438 100644
--- a/tests/zfs-tests/cmd/draid.c
+++ b/tests/zfs-tests/cmd/draid.c
@@ -130,7 +130,7 @@ read_map(const char *filename, nvlist_t **allcfgs)
* for freeing the configuration returned.
*/
static int
-read_map_key(const char *filename, char *key, nvlist_t **cfg)
+read_map_key(const char *filename, const char *key, nvlist_t **cfg)
{
nvlist_t *allcfgs, *foundcfg = NULL;
int error;
@@ -320,8 +320,8 @@ write_map_key(const char *filename, char *key, draid_map_t *map,
}
static void
-dump_map(draid_map_t *map, char *key, double worst_ratio, double avg_ratio,
- int verbose)
+dump_map(draid_map_t *map, const char *key, double worst_ratio,
+ double avg_ratio, int verbose)
{
if (verbose == 0) {
return;
@@ -363,7 +363,7 @@ dump_map(draid_map_t *map, char *key, double worst_ratio, double avg_ratio,
}
static void
-dump_map_nv(char *key, nvlist_t *cfg, int verbose)
+dump_map_nv(const char *key, nvlist_t *cfg, int verbose)
{
draid_map_t map;
uint_t c;
@@ -385,7 +385,7 @@ dump_map_nv(char *key, nvlist_t *cfg, int verbose)
* Print a summary of the mapping.
*/
static int
-dump_map_key(const char *filename, char *key, int verbose)
+dump_map_key(const char *filename, const char *key, int verbose)
{
nvlist_t *cfg;
int error;
diff --git a/tests/zfs-tests/cmd/get_diff.c b/tests/zfs-tests/cmd/get_diff.c
index 2799f46b0..3f8fe787f 100644
--- a/tests/zfs-tests/cmd/get_diff.c
+++ b/tests/zfs-tests/cmd/get_diff.c
@@ -25,10 +25,9 @@
#include <errno.h>
static void
-usage(char *msg, int exit_value)
+usage(const char *msg, int exit_value)
{
- (void) fprintf(stderr, "get_diff file redacted_file\n");
- (void) fprintf(stderr, "%s\n", msg);
+ (void) fprintf(stderr, "usage: get_diff file redacted_file\n%s\n", msg);
exit(exit_value);
}
diff --git a/tests/zfs-tests/cmd/libzfs_input_check.c b/tests/zfs-tests/cmd/libzfs_input_check.c
index bfa450be5..e84a00273 100644
--- a/tests/zfs-tests/cmd/libzfs_input_check.c
+++ b/tests/zfs-tests/cmd/libzfs_input_check.c
@@ -596,13 +596,13 @@ test_channel_program(const char *pool)
"arg = ...\n"
"argv = arg[\"argv\"]\n"
"return argv[1]";
- char *const argv[1] = { "Hello World!" };
+ const char *const argv[1] = { "Hello World!" };
nvlist_t *required = fnvlist_alloc();
nvlist_t *optional = fnvlist_alloc();
nvlist_t *args = fnvlist_alloc();
fnvlist_add_string(required, "program", program);
- fnvlist_add_string_array(args, "argv", (const char * const *)argv, 1);
+ fnvlist_add_string_array(args, "argv", argv, 1);
fnvlist_add_nvlist(required, "arg", args);
fnvlist_add_boolean_value(optional, "sync", B_TRUE);
diff --git a/tests/zfs-tests/cmd/mkbusy.c b/tests/zfs-tests/cmd/mkbusy.c
index 75b5736e5..cc4a6cfcb 100644
--- a/tests/zfs-tests/cmd/mkbusy.c
+++ b/tests/zfs-tests/cmd/mkbusy.c
@@ -32,14 +32,14 @@
static __attribute__((noreturn)) void
-usage(char *progname)
+usage(const char *progname)
{
(void) fprintf(stderr, "Usage: %s <dirname|filename>\n", progname);
exit(1);
}
static __attribute__((noreturn)) void
-fail(char *err)
+fail(const char *err)
{
perror(err);
exit(1);
diff --git a/tests/zfs-tests/cmd/mkfiles.c b/tests/zfs-tests/cmd/mkfiles.c
index 32abfd0c3..76be4dadb 100644
--- a/tests/zfs-tests/cmd/mkfiles.c
+++ b/tests/zfs-tests/cmd/mkfiles.c
@@ -21,13 +21,11 @@
#include <unistd.h>
#include <sys/param.h>
-#define MAX_INT_LENGTH 10
-
static void
-usage(char *msg, int exit_value)
+usage(const char *msg, int exit_value)
{
- (void) fprintf(stderr, "mkfiles basename max_file [min_file]\n");
- (void) fprintf(stderr, "%s\n", msg);
+ (void) fprintf(stderr, "usage: mkfiles basename max_file [min_file]\n"
+ "%s\n", msg);
exit(exit_value);
}
@@ -40,13 +38,13 @@ main(int argc, char **argv)
char buf[MAXPATHLEN];
if (argc < 3 || argc > 4)
- usage("Invalid number of arguments", -1);
+ usage("Invalid number of arguments", 1);
if (sscanf(argv[2], "%u", &numfiles) != 1)
- usage("Invalid maximum file", -2);
+ usage("Invalid maximum file", 2);
if (argc == 4 && sscanf(argv[3], "%u", &first_file) != 1)
- usage("Invalid first file", -3);
+ usage("Invalid first file", 3);
for (i = first_file; i < first_file + numfiles; i++) {
int fd;
@@ -54,11 +52,11 @@ main(int argc, char **argv)
if ((fd = open(buf, O_CREAT | O_EXCL, O_RDWR)) == -1) {
(void) fprintf(stderr, "Failed to create %s %s\n", buf,
strerror(errno));
- return (-4);
+ return (4);
} else if (fchown(fd, getuid(), getgid()) < 0) {
(void) fprintf(stderr, "Failed to chown %s %s\n", buf,
strerror(errno));
- return (-5);
+ return (5);
}
(void) close(fd);
}
diff --git a/tests/zfs-tests/cmd/mktree.c b/tests/zfs-tests/cmd/mktree.c
index 25b26c9e1..8ee86e5a0 100644
--- a/tests/zfs-tests/cmd/mktree.c
+++ b/tests/zfs-tests/cmd/mktree.c
@@ -153,7 +153,7 @@ crtfile(char *pname)
{
int fd = -1;
int i, size;
- char *context = "0123456789ABCDF";
+ const char *context = "0123456789ABCDF";
char *pbuf;
if (pname == NULL) {
diff --git a/tests/zfs-tests/cmd/nvlist_to_lua.c b/tests/zfs-tests/cmd/nvlist_to_lua.c
index 4d2abfeb1..b65b3fd26 100644
--- a/tests/zfs-tests/cmd/nvlist_to_lua.c
+++ b/tests/zfs-tests/cmd/nvlist_to_lua.c
@@ -84,7 +84,7 @@ nvlist_equal(nvlist_t *nvla, nvlist_t *nvlb)
static void
test(const char *testname, boolean_t expect_success, boolean_t expect_match)
{
- char *progstr = "input = ...; return {output=input}";
+ const char *progstr = "input = ...; return {output=input}";
nvlist_t *outnvl;
@@ -230,8 +230,8 @@ run_tests(void)
test("uint64_array", B_FALSE, B_FALSE);
}
{
- char *const val[2] = { "0", "1" };
- fnvlist_add_string_array(nvl, key, (const char **)val, 2);
+ const char *val[2] = { "0", "1" };
+ fnvlist_add_string_array(nvl, key, val, 2);
test("string_array", B_TRUE, B_FALSE);
}
{
diff --git a/tests/zfs-tests/cmd/readmmap.c b/tests/zfs-tests/cmd/readmmap.c
index e21c2c867..f119e114d 100644
--- a/tests/zfs-tests/cmd/readmmap.c
+++ b/tests/zfs-tests/cmd/readmmap.c
@@ -49,7 +49,7 @@
int
main(int argc, char **argv)
{
- char *filename = "badfile";
+ const char *filename = "badfile";
size_t size = 4395;
size_t idx = 0;
char *buf = NULL;
diff --git a/tests/zfs-tests/cmd/stride_dd.c b/tests/zfs-tests/cmd/stride_dd.c
index 88bd53292..732ac9f47 100644
--- a/tests/zfs-tests/cmd/stride_dd.c
+++ b/tests/zfs-tests/cmd/stride_dd.c
@@ -27,7 +27,7 @@ static char *ifile = NULL;
static char *ofile = NULL;
static int stride = 0;
static int seek = 0;
-static char *execname = "stride_dd";
+static const char *execname = "stride_dd";
static void usage(void);
static void parse_options(int argc, char *argv[]);
diff --git a/tests/zfs-tests/cmd/xattrtest.c b/tests/zfs-tests/cmd/xattrtest.c
index 49b6629ba..4f82facb0 100644
--- a/tests/zfs-tests/cmd/xattrtest.c
+++ b/tests/zfs-tests/cmd/xattrtest.c
@@ -91,8 +91,8 @@ static int size_is_random = 0;
static int value_is_random = 0;
static int keep_files = 0;
static int phase = PHASE_ALL;
-static char path[PATH_MAX] = "/tmp/xattrtest";
-static char script[PATH_MAX] = "/bin/true";
+static const char *path = "/tmp/xattrtest";
+static const char *script = "/bin/true";
static char xattrbytes[XATTR_SIZE_MAX];
static int
@@ -161,8 +161,7 @@ parse_args(int argc, char **argv)
}
break;
case 'p':
- strncpy(path, optarg, PATH_MAX);
- path[PATH_MAX - 1] = '\0';
+ path = optarg;
break;
case 'c':
synccaches = 1;
@@ -171,8 +170,7 @@ parse_args(int argc, char **argv)
dropcaches = 1;
break;
case 't':
- strncpy(script, optarg, PATH_MAX);
- script[PATH_MAX - 1] = '\0';
+ script = optarg;
break;
case 'e':
seed = strtol(optarg, NULL, 0);
@@ -291,9 +289,9 @@ run_process(const char *path, char *argv[])
}
static int
-post_hook(char *phase)
+post_hook(const char *phase)
{
- char *argv[3] = { script, phase, (char *)0 };
+ char *argv[3] = { (char *)script, (char *)phase, NULL };
int rc;
if (synccaches)
@@ -517,9 +515,9 @@ getxattrs(void)
int i, j, rnd_size, shift, rc = 0;
char name[XATTR_NAME_MAX];
char *verify_value = NULL;
- char *verify_string;
+ const char *verify_string;
char *value = NULL;
- char *value_string;
+ const char *value_string;
char *file = NULL;
struct timeval start, stop;
double seconds;
diff --git a/tests/zfs-tests/tests/functional/hkdf/hkdf_test.c b/tests/zfs-tests/tests/functional/hkdf/hkdf_test.c
index 473a9ba9a..24aeb0b22 100644
--- a/tests/zfs-tests/tests/functional/hkdf/hkdf_test.c
+++ b/tests/zfs-tests/tests/functional/hkdf/hkdf_test.c
@@ -29,16 +29,16 @@
*/
typedef struct hkdf_tv {
/* test vector input values */
- char *ikm;
+ const char *ikm;
uint_t ikm_len;
- char *salt;
+ const char *salt;
uint_t salt_len;
- char *info;
+ const char *info;
uint_t info_len;
uint_t okm_len;
/* expected output */
- char *okm;
+ const char *okm;
} hkdf_tv_t;
/*
@@ -48,7 +48,7 @@ typedef struct hkdf_tv {
* The current vectors were taken from:
* https://www.kullo.net/blog/hkdf-sha-512-test-vectors/
*/
-static hkdf_tv_t test_vectors[] = {
+static const hkdf_tv_t test_vectors[] = {
{
.ikm = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
@@ -170,7 +170,7 @@ static hkdf_tv_t test_vectors[] = {
};
static void
-hexdump(char *str, uint8_t *src, uint_t len)
+hexdump(const char *str, uint8_t *src, uint_t len)
{
printf("\t%s\t", str);
for (int i = 0; i < len; i++)
@@ -179,7 +179,7 @@ hexdump(char *str, uint8_t *src, uint_t len)
}
static int
-run_test(int i, hkdf_tv_t *tv)
+run_test(int i, const hkdf_tv_t *tv)
{
int ret;
uint8_t good[SHA512_DIGEST_LENGTH];
diff --git a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_001_pos.c b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_001_pos.c
index d40da0d2b..503861656 100644
--- a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_001_pos.c
+++ b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_001_pos.c
@@ -8,6 +8,7 @@
#include <errno.h>
#include <string.h>
#include <time.h>
+#include <err.h>
/* backward compat in case it's not defined */
#ifndef O_TMPFILE
@@ -33,75 +34,45 @@ fill_random(char *buf, int len)
{
srand(time(NULL));
for (int i = 0; i < len; i++)
- buf[i] = (char)rand();
+ buf[i] = (char)(rand() % 0xFF);
}
int
main(void)
{
- int i, fd;
- char buf1[BSZ], buf2[BSZ] = {};
- char *penv[] = {"TESTDIR"};
+ char buf1[BSZ], buf2[BSZ] = {0};
(void) fprintf(stdout, "Verify O_TMPFILE is working properly.\n");
- /*
- * Get the environment variable values.
- */
- for (i = 0; i < sizeof (penv) / sizeof (char *); i++) {
- if ((penv[i] = getenv(penv[i])) == NULL) {
- (void) fprintf(stderr, "getenv(penv[%d])\n", i);
- exit(1);
- }
- }
+ const char *testdir = getenv("TESTDIR");
+ if (testdir == NULL)
+ errx(1, "getenv(\"TESTDIR\")");
fill_random(buf1, BSZ);
- fd = open(penv[0], O_RDWR|O_TMPFILE, 0666);
- if (fd < 0) {
- perror("open");
- exit(2);
- }
-
- if (write(fd, buf1, BSZ) < 0) {
- perror("write");
- close(fd);
- exit(3);
- }
-
- if (pread(fd, buf2, BSZ, 0) < 0) {
- perror("pread");
- close(fd);
- exit(4);
- }
-
- if (memcmp(buf1, buf2, BSZ) != 0) {
- fprintf(stderr, "data corrupted\n");
- close(fd);
- exit(5);
- }
+ int fd = open(testdir, O_RDWR|O_TMPFILE, 0666);
+ if (fd < 0)
+ err(2, "open(%s)", testdir);
+
+ if (write(fd, buf1, BSZ) < 0)
+ err(3, "write");
+
+ if (pread(fd, buf2, BSZ, 0) < 0)
+ err(4, "pread");
+
+ if (memcmp(buf1, buf2, BSZ) != 0)
+ errx(5, "data corrupted");
memset(buf2, 0, BSZ);
- if (fsetxattr(fd, "user.test", buf1, BSZ, 0) < 0) {
- perror("fsetxattr");
- close(fd);
- exit(6);
- }
-
- if (fgetxattr(fd, "user.test", buf2, BSZ) < 0) {
- perror("fgetxattr");
- close(fd);
- exit(7);
- }
-
- if (memcmp(buf1, buf2, BSZ) != 0) {
- fprintf(stderr, "xattr corrupted\n");
- close(fd);
- exit(8);
- }
-
- close(fd);
+ if (fsetxattr(fd, "user.test", buf1, BSZ, 0) < 0)
+ err(6, "pread");
+
+ if (fgetxattr(fd, "user.test", buf2, BSZ) < 0)
+ err(7, "fgetxattr");
+
+ if (memcmp(buf1, buf2, BSZ) != 0)
+ errx(8, "xattr corrupted\n");
return (0);
}
diff --git a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_002_pos.c b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_002_pos.c
index 39a72c22a..424231d11 100644
--- a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_002_pos.c
+++ b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_002_pos.c
@@ -6,6 +6,7 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
+#include <err.h>
/* backward compat in case it's not defined */
#ifndef O_TMPFILE
@@ -24,12 +25,27 @@
*
*/
+static void
+run(const char *op)
+{
+ int ret;
+ char buf[50];
+ sprintf(buf, "sudo -E zpool %s $TESTPOOL", op);
+ if ((ret = system(buf)) != 0) {
+ if (ret == -1)
+ err(4, "system \"zpool %s\"", op);
+ else
+ errx(4, "zpool %s exited %d\n",
+ op, WEXITSTATUS(ret));
+ }
+}
+
int
main(void)
{
- int i, fd, ret;
+ int i, fd;
char spath[1024], dpath[1024];
- char *penv[] = {"TESTDIR", "TESTFILE0"};
+ const char *penv[] = {"TESTDIR", "TESTFILE0"};
struct stat sbuf;
(void) fprintf(stdout, "Verify O_TMPFILE file can be linked.\n");
@@ -37,55 +53,25 @@ main(void)
/*
* Get the environment variable values.
*/
- for (i = 0; i < sizeof (penv) / sizeof (char *); i++) {
- if ((penv[i] = getenv(penv[i])) == NULL) {
- (void) fprintf(stderr, "getenv(penv[%d])\n", i);
- exit(1);
- }
- }
+ for (i = 0; i < ARRAY_SIZE(penv); i++)
+ if ((penv[i] = getenv(penv[i])) == NULL)
+ errx(1, "getenv(penv[%d])", i);
fd = open(penv[0], O_RDWR|O_TMPFILE, 0666);
- if (fd < 0) {
- perror("open");
- exit(2);
- }
+ if (fd < 0)
+ err(2, "open(%s)", penv[0]);
snprintf(spath, 1024, "/proc/self/fd/%d", fd);
snprintf(dpath, 1024, "%s/%s", penv[0], penv[1]);
- if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) < 0) {
- perror("linkat");
- close(fd);
- exit(3);
- }
+ if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) < 0)
+ err(3, "linkat");
- if ((ret = system("sudo -E zpool freeze $TESTPOOL"))) {
- if (ret == -1)
- perror("system \"zpool freeze\"");
- else
- fprintf(stderr, "zpool freeze exits with %d\n",
- WEXITSTATUS(ret));
- exit(4);
- }
+ run("freeze");
close(fd);
- if ((ret = system("sudo -E zpool export $TESTPOOL"))) {
- if (ret == -1)
- perror("system \"zpool export\"");
- else
- fprintf(stderr, "zpool export exits with %d\n",
- WEXITSTATUS(ret));
- exit(4);
- }
-
- if ((ret = system("sudo -E zpool import $TESTPOOL"))) {
- if (ret == -1)
- perror("system \"zpool import\"");
- else
- fprintf(stderr, "zpool import exits with %d\n",
- WEXITSTATUS(ret));
- exit(4);
- }
+ run("export");
+ run("import");
if (stat(dpath, &sbuf) < 0) {
perror("stat");
diff --git a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_003_pos.c b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_003_pos.c
index 58aa90512..463b96c99 100644
--- a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_003_pos.c
+++ b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_003_pos.c
@@ -6,6 +6,7 @@
#include <fcntl.h>
#include <errno.h>
#include <string.h>
+#include <err.h>
/* backward compat in case it's not defined */
#ifndef O_TMPFILE
@@ -28,7 +29,7 @@ main(void)
{
int i, fd;
char spath[1024], dpath[1024];
- char *penv[] = {"TESTDIR", "TESTFILE0"};
+ const char *penv[] = {"TESTDIR", "TESTFILE0"};
struct stat sbuf;
(void) fprintf(stdout, "Verify O_EXCL tmpfile cannot be linked.\n");
@@ -36,33 +37,21 @@ main(void)
/*
* Get the environment variable values.
*/
- for (i = 0; i < sizeof (penv) / sizeof (char *); i++) {
- if ((penv[i] = getenv(penv[i])) == NULL) {
- (void) fprintf(stderr, "getenv(penv[%d])\n", i);
- exit(1);
- }
- }
+ for (i = 0; i < ARRAY_SIZE(penv); i++)
+ if ((penv[i] = getenv(penv[i])) == NULL)
+ errx(1, "getenv(penv[%d])", i);
fd = open(penv[0], O_RDWR|O_TMPFILE|O_EXCL, 0666);
- if (fd < 0) {
- perror("open");
- exit(2);
- }
+ if (fd < 0)
+ err(2, "open(%s)", penv[0]);
snprintf(spath, 1024, "/proc/self/fd/%d", fd);
snprintf(dpath, 1024, "%s/%s", penv[0], penv[1]);
- if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) == 0) {
- fprintf(stderr, "linkat returns successfully\n");
- close(fd);
- exit(3);
- }
+ if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) == 0)
+ errx(3, "linkat returned successfully\n");
- if (stat(dpath, &sbuf) == 0) {
- fprintf(stderr, "stat returns successfully\n");
- close(fd);
- exit(4);
- }
- close(fd);
+ if (stat(dpath, &sbuf) == 0)
+ errx(4, "stat returned successfully\n");
return (0);
}
diff --git a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c
index c72ea2bb6..893d2639a 100644
--- a/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c
+++ b/tests/zfs-tests/tests/functional/tmpfile/tmpfile_stat_mode.c
@@ -28,6 +28,7 @@
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
+#include <err.h>
/* backward compat in case it's not defined */
#ifndef O_TMPFILE
@@ -50,63 +51,46 @@ test_stat_mode(mode_t mask)
struct stat st, fst;
int i, fd;
char spath[1024], dpath[1024];
- char *penv[] = {"TESTDIR", "TESTFILE0"};
+ const char *penv[] = {"TESTDIR", "TESTFILE0"};
mode_t masked = 0777 & ~mask;
mode_t mode;
/*
* Get the environment variable values.
*/
- for (i = 0; i < sizeof (penv) / sizeof (char *); i++) {
- if ((penv[i] = getenv(penv[i])) == NULL) {
- fprintf(stderr, "getenv(penv[%d])\n", i);
- exit(1);
- }
- }
+ for (i = 0; i < ARRAY_SIZE(penv); i++)
+ if ((penv[i] = getenv(penv[i])) == NULL)
+ errx(1, "getenv(penv[%d])", i);
umask(mask);
fd = open(penv[0], O_RDWR|O_TMPFILE, 0777);
- if (fd == -1) {
- perror("open");
- exit(2);
- }
+ if (fd == -1)
+ err(2, "open(%s)", penv[0]);
- if (fstat(fd, &fst) == -1) {
- perror("fstat");
- close(fd);
- exit(3);
- }
+ if (fstat(fd, &fst) == -1)
+ err(3, "open");
snprintf(spath, sizeof (spath), "/proc/self/fd/%d", fd);
snprintf(dpath, sizeof (dpath), "%s/%s", penv[0], penv[1]);
unlink(dpath);
- if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) == -1) {
- perror("linkat");
- close(fd);
- exit(4);
- }
+ if (linkat(AT_FDCWD, spath, AT_FDCWD, dpath, AT_SYMLINK_FOLLOW) == -1)
+ err(4, "linkat");
close(fd);
- if (stat(dpath, &st) == -1) {
- perror("stat");
- exit(5);
- }
+ if (stat(dpath, &st) == -1)
+ err(5, "stat");
unlink(dpath);
/* Verify fstat(2) result */
mode = fst.st_mode & 0777;
- if (mode != masked) {
- fprintf(stderr, "fstat(2) %o != %o\n", mode, masked);
- exit(6);
- }
+ if (mode != masked)
+ errx(6, "fstat(2) %o != %o\n", mode, masked);
/* Verify stat(2) result */
mode = st.st_mode & 0777;
- if (mode != masked) {
- fprintf(stderr, "stat(2) %o != %o\n", mode, masked);
- exit(7);
- }
+ if (mode != masked)
+ errx(7, "stat(2) %o != %o\n", mode, masked);
}
int