diff --git a/tools/cfs-fuse.c b/tools/cfs-fuse.c index 5e24bfa3..c38ff360 100644 --- a/tools/cfs-fuse.c +++ b/tools/cfs-fuse.c @@ -5,6 +5,7 @@ #include "config.h" #include +#include #include #include #include @@ -48,18 +49,6 @@ uint64_t erofs_build_time; uint32_t erofs_build_time_nsec; int basedir_fd; -static void printexit(const char *format, ...) __attribute__((noreturn)); -static void printexit(const char *format, ...) -{ - va_list args; - - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); - - exit(1); -} - struct cfs_data { const char *source; const char *basedir; @@ -1171,34 +1160,34 @@ int main(int argc, char *argv[]) fd = open(data.source, O_RDONLY | O_CLOEXEC); if (fd < 0) { - printexit("Failed to open %s\n", data.source); + errx(EXIT_FAILURE, "Failed to open %s\n", data.source); } r = fstat(fd, &s); if (r < 0) { - printexit("Failed to stat %s\n", data.source); + errx(EXIT_FAILURE, "Failed to stat %s\n", data.source); } erofs_data_size = s.st_size; if (erofs_data_size < EROFS_BLKSIZ) { - printexit("To small image\n"); + errx(EXIT_FAILURE, "To small image\n"); } /* Memory-map the file. */ erofs_data = mmap(0, erofs_data_size, PROT_READ, MAP_PRIVATE, fd, 0); if (erofs_data == MAP_FAILED) { - printexit("Failed to mmap %s\n", argv[1]); + errx(EXIT_FAILURE, "Failed to mmap %s\n", argv[1]); } close(fd); basedir_fd = open(data.basedir, O_RDONLY | O_PATH); if (basedir_fd < 0) { - printexit("Failed to open basedir %s\n", data.basedir); + errx(EXIT_FAILURE, "Failed to open basedir %s\n", data.basedir); } cfs_header = (struct lcfs_erofs_header_s *)(erofs_data); if (lcfs_u32_from_file(cfs_header->magic) != LCFS_EROFS_MAGIC) { - printexit("Wrong cfs magic"); + errx(EXIT_FAILURE, "Wrong cfs magic"); } cfs_flags = lcfs_u32_from_file(cfs_header->flags); @@ -1208,7 +1197,7 @@ int main(int argc, char *argv[]) erofs_super = (struct erofs_super_block *)(erofs_data + EROFS_SUPER_OFFSET); if (lcfs_u32_from_file(erofs_super->magic) != EROFS_SUPER_MAGIC_V1) { - printexit("Wrong erofs magic"); + errx(EXIT_FAILURE, "Wrong erofs magic"); } erofs_metadata = erofs_data + lcfs_u32_from_file(erofs_super->meta_blkaddr) * diff --git a/tools/mountcomposefs.c b/tools/mountcomposefs.c index 931fbc71..9d16efa5 100644 --- a/tools/mountcomposefs.c +++ b/tools/mountcomposefs.c @@ -19,6 +19,7 @@ #include "config.h" +#include #include #include #include @@ -37,18 +38,6 @@ #include "libcomposefs/lcfs-mount.h" -static void printexit(const char *format, ...) __attribute__((noreturn)); -static void printexit(const char *format, ...) -{ - va_list args; - - va_start(args, format); - vfprintf(stderr, format, args); - va_end(args); - - exit(1); -} - static void usage(const char *argv0) { const char *bin = basename(argv0); @@ -136,7 +125,8 @@ int main(int argc, char **argv) switch (opt) { case 't': if (strcmp(optarg, "composefs") != 0) - printexit("Unsupported fs type '%s'\n", optarg); + errx(EXIT_FAILURE, "Unsupported fs type '%s'\n", + optarg); break; case 'o': mount_options = optarg; @@ -173,32 +163,37 @@ int main(int argc, char **argv) if (strcmp("basedir", key) == 0) { if (value == NULL) - printexit("No value specified for basedir option\n"); + errx(EXIT_FAILURE, + "No value specified for basedir option\n"); opt_basedir = value; } else if (strcmp("digest", key) == 0) { if (value == NULL) - printexit("No value specified for digest option\n"); + errx(EXIT_FAILURE, + "No value specified for digest option\n"); opt_digest = value; } else if (strcmp("verity", key) == 0) { opt_verity = true; } else if (strcmp("upperdir", key) == 0) { if (value == NULL) - printexit("No value specified for upperdir option\n"); + errx(EXIT_FAILURE, + "No value specified for upperdir option\n"); opt_upperdir = value; } else if (strcmp("workdir", key) == 0) { if (value == NULL) - printexit("No value specified for workdir option\n"); + errx(EXIT_FAILURE, + "No value specified for workdir option\n"); opt_workdir = value; } else if (strcmp("idmap", key) == 0) { if (value == NULL) - printexit("No value specified for workdir option\n"); + errx(EXIT_FAILURE, + "No value specified for workdir option\n"); opt_idmap = value; } else if (strcmp("rw", key) == 0) { opt_ro = false; } else if (strcmp("ro", key) == 0) { opt_ro = true; } else { - printexit("Unsupported option %s\n", key); + errx(EXIT_FAILURE, "Unsupported option %s\n", key); } } @@ -214,7 +209,7 @@ int main(int argc, char **argv) options.objdirs = calloc(options.n_objdirs, sizeof(char *)); if (options.objdirs == NULL) - printexit("Out of memory\n"); + errx(EXIT_FAILURE, "Out of memory\n"); for (i = 0, str = (char *)opt_basedir;; i++, str = NULL) { token = strtok_r(str, ":", &saveptr); @@ -231,7 +226,8 @@ int main(int argc, char **argv) } if ((opt_upperdir && !opt_workdir) || (!opt_upperdir && opt_workdir)) { - printexit("Both workdir and upperdir must be specified if used\n"); + errx(EXIT_FAILURE, + "Both workdir and upperdir must be specified if used\n"); } options.upperdir = opt_upperdir; options.workdir = opt_workdir; @@ -246,32 +242,36 @@ int main(int argc, char **argv) if (opt_idmap != NULL) { userns_fd = open(opt_idmap, O_RDONLY | O_CLOEXEC | O_NOCTTY); if (userns_fd < 0) - printexit("Failed to open userns %s: %s\n", opt_idmap, - strerror(errno)); + errx(EXIT_FAILURE, "Failed to open userns %s: %s\n", + opt_idmap, strerror(errno)); options.flags |= LCFS_MOUNT_FLAGS_IDMAP; options.idmap_fd = userns_fd; } fd = open(image_path, O_RDONLY | O_CLOEXEC); if (fd < 0) - printexit("Failed to open %s: %s\n", image_path, strerror(errno)); + errx(EXIT_FAILURE, "Failed to open %s: %s\n", image_path, + strerror(errno)); res = lcfs_mount_fd(fd, mount_path, &options); if (res < 0) { int errsv = errno; if (errsv == ENOVERITY) - printexit("Failed to mount composefs %s: Image has no fs-verity\n", - image_path); + errx(EXIT_FAILURE, + "Failed to mount composefs %s: Image has no fs-verity\n", + image_path); else if (errsv == EWRONGVERITY) - printexit("Failed to mount composefs %s: Image has wrong fs-verity\n", - image_path); + errx(EXIT_FAILURE, + "Failed to mount composefs %s: Image has wrong fs-verity\n", + image_path); else if (errsv == ENOSIGNATURE) - printexit("Failed to mount composefs %s: Image was not signed\n", - image_path); + errx(EXIT_FAILURE, + "Failed to mount composefs %s: Image was not signed\n", + image_path); - printexit("Failed to mount composefs %s: %s\n", image_path, - strerror(errno)); + errx(EXIT_FAILURE, "Failed to mount composefs %s: %s\n", + image_path, strerror(errno)); } free(options.objdirs);