diff options
author | William Manley <will@williammanley.net> | 2016-08-30 22:36:20 +0100 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2016-08-30 22:41:27 +0000 |
commit | a128abd9bcf9842ae7cd61ef951b5345573c3df6 (patch) | |
tree | 8962d85d785a94bcabffe0dfdde6af75ca4f8a21 /src/switchroot | |
parent | 42dab85728385eaf538b36ed56e9db7ff3f96faa (diff) | |
download | ostree-a128abd9bcf9842ae7cd61ef951b5345573c3df6.tar.gz |
switchroot: Replace custom error printing with err/warn functions from libc
The `warn()` libc extension has exactly the same behaviour as our own
`perrorv` function, but is available in (at least) glibc and musl. As an
added bonus the similar function `err()` which will exit with an error
code afterwards.
This implementation is tidier and allows us to get rid of our own
`perrorv`. It paves the way to removing `ostree-mount-util.c` to simplify
the build scripts.
Original idea by @cgwalters in #477.
Closes: #478
Approved by: cgwalters
Diffstat (limited to 'src/switchroot')
-rw-r--r-- | src/switchroot/ostree-mount-util.c | 38 | ||||
-rw-r--r-- | src/switchroot/ostree-mount-util.h | 2 | ||||
-rw-r--r-- | src/switchroot/ostree-prepare-root.c | 109 | ||||
-rw-r--r-- | src/switchroot/ostree-remount.c | 15 |
4 files changed, 29 insertions, 135 deletions
diff --git a/src/switchroot/ostree-mount-util.c b/src/switchroot/ostree-mount-util.c index 9789bd86..a26d2bd6 100644 --- a/src/switchroot/ostree-mount-util.c +++ b/src/switchroot/ostree-mount-util.c @@ -22,53 +22,19 @@ #include "config.h" -#include <stdarg.h> -#include <errno.h> -#include <string.h> -#include <stdio.h> -#include <sys/mount.h> -#include <unistd.h> +#include <err.h> #include <stdlib.h> #include <sys/statvfs.h> #include "ostree-mount-util.h" int -perrorv (const char *format, ...) -{ - va_list args; - char buf[1024]; - char *p; - -#ifdef _GNU_SOURCE - p = strerror_r (errno, buf, sizeof (buf)); -#else - strerror_r (errno, buf, sizeof (buf)); - buf[sizeof (buf) - 1] = '\0'; - p = buf; -#endif /* _GNU_SOURCE */ - - va_start (args, format); - - vfprintf (stderr, format, args); - fprintf (stderr, ": %s\n", p); - fflush (stderr); - - va_end (args); - - return 0; -} - -int path_is_on_readonly_fs (char *path) { struct statvfs stvfsbuf; if (statvfs (path, &stvfsbuf) == -1) - { - perrorv ("statvfs(%s): ", path); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "statvfs(%s)", path); return (stvfsbuf.f_flag & ST_RDONLY) != 0; } diff --git a/src/switchroot/ostree-mount-util.h b/src/switchroot/ostree-mount-util.h index 475b2cab..eb233d2f 100644 --- a/src/switchroot/ostree-mount-util.h +++ b/src/switchroot/ostree-mount-util.h @@ -21,6 +21,4 @@ #pragma once -int perrorv (const char *format, ...) __attribute__ ((format (printf, 1, 2))); - int path_is_on_readonly_fs (char *path); diff --git a/src/switchroot/ostree-prepare-root.c b/src/switchroot/ostree-prepare-root.c index 895b2e51..d866f88e 100644 --- a/src/switchroot/ostree-prepare-root.c +++ b/src/switchroot/ostree-prepare-root.c @@ -40,6 +40,7 @@ #include <stdlib.h> #include <unistd.h> #include <string.h> +#include <err.h> #include <errno.h> #include <ctype.h> @@ -86,27 +87,18 @@ parse_ostree_cmdline (void) { // Mount proc if (mount ("proc", "/proc", "proc", 0, NULL) < 0) - { - perrorv ("failed to mount proc on /proc: "); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to mount proc on /proc"); cmdline = read_proc_cmdline (); tmp_errno = errno; /* Leave the filesystem in the state that we found it: */ if (umount ("/proc")) - { - perrorv ("failed to umount proc from /proc: "); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to umount proc from /proc"); errno = tmp_errno; if (!cmdline) - { - perrorv ("failed to read /proc/cmdline: "); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to read /proc/cmdline"); } iter = cmdline; @@ -158,29 +150,17 @@ resolve_deploy_path (const char * root_mountpoint) ostree_target = parse_ostree_cmdline (); if (!ostree_target) - { - fprintf (stderr, "No OSTree target; expected ostree=/ostree/boot.N/...\n"); - exit (EXIT_FAILURE); - } + errx (EXIT_FAILURE, "No OSTree target; expected ostree=/ostree/boot.N/..."); snprintf (destpath, sizeof(destpath), "%s/%s", root_mountpoint, ostree_target); printf ("Examining %s\n", destpath); if (lstat (destpath, &stbuf) < 0) - { - perrorv ("Couldn't find specified OSTree root '%s': ", destpath); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "Couldn't find specified OSTree root '%s'", destpath); if (!S_ISLNK (stbuf.st_mode)) - { - fprintf (stderr, "OSTree target is not a symbolic link: %s\n", destpath); - exit (EXIT_FAILURE); - } + errx (EXIT_FAILURE, "OSTree target is not a symbolic link: %s", destpath); deploy_path = realpath (destpath, NULL); if (deploy_path == NULL) - { - perrorv ("realpath(%s) failed: ", destpath); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "realpath(%s) failed", destpath); printf ("Resolved OSTree target to: %s\n", deploy_path); return deploy_path; } @@ -214,33 +194,21 @@ main(int argc, char *argv[]) * * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */ if (mount (NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0) - { - perrorv ("failed to make \"/\" private mount: %m"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to make \"/\" private mount"); /* Make deploy_path a bind mount, so we can move it later */ if (mount (deploy_path, deploy_path, NULL, MS_BIND, NULL) < 0) - { - perrorv ("failed to make initial bind mount %s", deploy_path); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to make initial bind mount %s", deploy_path); /* chdir to our new root. We need to do this after bind-mounting it over * itself otherwise our cwd is still on the non-bind-mounted filesystem * below. */ if (chdir (deploy_path) < 0) - { - perrorv ("failed to chdir to deploy_path"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to chdir to deploy_path"); /* Link to the deployment's /var */ if (mount ("../../var", "var", NULL, MS_MGC_VAL|MS_BIND, NULL) < 0) - { - perrorv ("failed to bind mount ../../var to var"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to bind mount ../../var to var"); /* If /boot is on the same partition, use a bind mount to make it visible * at /boot inside the deployment. */ @@ -251,10 +219,7 @@ main(int argc, char *argv[]) { snprintf (srcpath, sizeof(srcpath), "%s/boot", root_mountpoint); if (mount (srcpath, "boot", NULL, MS_BIND, NULL) < 0) - { - perrorv ("failed to bind mount %s to boot", srcpath); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to bind mount %s to boot", srcpath); } } @@ -271,31 +236,19 @@ main(int argc, char *argv[]) if (path_is_on_readonly_fs (".")) { if (mount (".", ".", NULL, MS_REMOUNT | MS_SILENT, NULL) < 0) - { - perrorv ("failed to remount rootfs writable (for overlayfs)"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to remount rootfs writable (for overlayfs)"); } if (mount ("overlay", "usr", "overlay", 0, usr_ovl_options) < 0) - { - perrorv ("failed to mount /usr overlayfs"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to mount /usr overlayfs"); } else { /* Otherwise, a read-only bind mount for /usr */ if (mount ("usr", "usr", NULL, MS_BIND, NULL) < 0) - { - perrorv ("failed to bind mount (class:readonly) /usr"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to bind mount (class:readonly) /usr"); if (mount ("usr", "usr", NULL, MS_BIND | MS_REMOUNT | MS_RDONLY, NULL) < 0) - { - perrorv ("failed to bind mount (class:readonly) /usr"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to bind mount (class:readonly) /usr"); } touch_run_ostree (); @@ -309,10 +262,7 @@ main(int argc, char *argv[]) * sysroot, so moving sysroot would also move the deploy location. In * reality attempting mount --move would fail with EBUSY. */ if (pivot_root (".", "sysroot") < 0) - { - perrorv ("failed to pivot_root to deployment"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to pivot_root to deployment"); } else { @@ -330,35 +280,22 @@ main(int argc, char *argv[]) * 3. /sysroot.tmp -> /sysroot */ if (mkdir ("/sysroot.tmp", 0755) < 0) - { - perrorv ("couldn't create temporary sysroot /sysroot.tmp: "); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "couldn't create temporary sysroot /sysroot.tmp"); if (mount (deploy_path, "/sysroot.tmp", NULL, MS_MOVE, NULL) < 0) - { - perrorv ("failed to MS_MOVE '%s' to '/sysroot.tmp'", deploy_path); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to MS_MOVE '%s' to '/sysroot.tmp'", deploy_path); if (mount (root_mountpoint, "sysroot", NULL, MS_MOVE, NULL) < 0) - { - perrorv ("failed to MS_MOVE '%s' to 'sysroot'", root_mountpoint); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to MS_MOVE '%s' to 'sysroot'", root_mountpoint); if (mount (".", root_mountpoint, NULL, MS_MOVE, NULL) < 0) - { - perrorv ("failed to MS_MOVE %s to %s", deploy_path, root_mountpoint); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to MS_MOVE %s to %s", deploy_path, root_mountpoint); } if (getpid() == 1) { execl ("/sbin/init", "/sbin/init", NULL); - perrorv ("failed to exec init inside ostree"); - exit (EXIT_FAILURE); + err (EXIT_FAILURE, "failed to exec init inside ostree"); } else { diff --git a/src/switchroot/ostree-remount.c b/src/switchroot/ostree-remount.c index aecaf9a8..589a3867 100644 --- a/src/switchroot/ostree-remount.c +++ b/src/switchroot/ostree-remount.c @@ -33,6 +33,7 @@ #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> +#include <err.h> #include <errno.h> #include "ostree-mount-util.h" @@ -49,15 +50,10 @@ maybe_mount_tmpfs_on_var (void) return; if (umount ("/var") < 0 && errno != EINVAL) - { - perror ("failed to unmount /var prior to mounting tmpfs, mounting over"); - } + warn ("failed to unmount /var prior to mounting tmpfs, mounting over"); if (mount ("tmpfs", "/var", "tmpfs", 0, NULL) < 0) - { - perror ("failed to mount tmpfs on /var"); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to mount tmpfs on /var"); } int @@ -94,10 +90,7 @@ main(int argc, char *argv[]) * already, then assume things are OK. */ if (errno != EINVAL) - { - perrorv ("failed to remount %s", target); - exit (EXIT_FAILURE); - } + err (EXIT_FAILURE, "failed to remount %s", target); } } |