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
This commit is contained in:
William Manley 2016-08-30 22:36:20 +01:00 committed by Atomic Bot
parent 42dab85728
commit a128abd9bc
4 changed files with 29 additions and 135 deletions

View File

@ -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;
}

View File

@ -21,6 +21,4 @@
#pragma once
int perrorv (const char *format, ...) __attribute__ ((format (printf, 1, 2)));
int path_is_on_readonly_fs (char *path);

View File

@ -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
{

View File

@ -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);
}
}