diff options
author | Simon McVittie <smcv@collabora.com> | 2021-06-23 18:56:38 +0100 |
---|---|---|
committer | Simon McVittie <smcv@collabora.com> | 2021-06-23 19:01:57 +0100 |
commit | 6ceba45306fdbb0fb9a11dc557e8412edf45c83c (patch) | |
tree | b26889ca45d39c88ce04b401d810bcaf0d63ecb7 | |
parent | 7f38ab6cf1752700bea6cf1b2f056f74ee4960ae (diff) | |
download | bubblewrap-6ceba45306fdbb0fb9a11dc557e8412edf45c83c.tar.gz |
bind-mount: Factor out bind_mount_result_to_string()
Signed-off-by: Simon McVittie <smcv@collabora.com>
-rw-r--r-- | bind-mount.c | 65 | ||||
-rw-r--r-- | bind-mount.h | 5 |
2 files changed, 48 insertions, 22 deletions
diff --git a/bind-mount.c b/bind-mount.c index 9bbd61b..4246c15 100644 --- a/bind-mount.c +++ b/bind-mount.c @@ -463,60 +463,81 @@ bind_mount (int proc_fd, return BIND_MOUNT_SUCCESS; } -void -die_with_bind_result (bind_mount_result res, - int saved_errno, - const char *format, - ...) +/** + * Return a string representing bind_mount_result, like strerror(). + * If want_errno_p is non-NULL, *want_errno_p is used to indicate whether + * it would make sense to print strerror(saved_errno). + */ +const char * +bind_mount_result_to_string (bind_mount_result res, + bool *want_errno_p) { - va_list args; + const char *string; bool want_errno = TRUE; - fprintf (stderr, "bwrap: "); - - va_start (args, format); - vfprintf (stderr, format, args); - va_end (args); - - fprintf (stderr, ": "); - switch (res) { case BIND_MOUNT_ERROR_MOUNT: - fprintf (stderr, "Unable to mount source on destination"); + string = "Unable to mount source on destination"; break; case BIND_MOUNT_ERROR_REALPATH_DEST: - fprintf (stderr, "realpath(destination)"); + string = "realpath(destination)"; break; case BIND_MOUNT_ERROR_REOPEN_DEST: - fprintf (stderr, "open(destination, O_PATH)"); + string = "open(destination, O_PATH)"; break; case BIND_MOUNT_ERROR_READLINK_DEST_PROC_FD: - fprintf (stderr, "readlink(/proc/self/fd/<destination>)"); + string = "readlink(/proc/self/fd/<destination>)"; break; case BIND_MOUNT_ERROR_FIND_DEST_MOUNT: - fprintf (stderr, "Unable to find destination in mount table"); + string = "Unable to find destination in mount table"; want_errno = FALSE; break; case BIND_MOUNT_ERROR_REMOUNT_DEST: - fprintf (stderr, "Unable to remount destination with correct flags"); + string = "Unable to remount destination with correct flags"; break; case BIND_MOUNT_ERROR_REMOUNT_SUBMOUNT: - fprintf (stderr, "Unable to remount recursively with correct flags"); + string = "Unable to remount recursively with correct flags"; break; case BIND_MOUNT_SUCCESS: + string = "Success"; + break; + default: - fprintf (stderr, "(unknown error %d)", res); + string = "(unknown/invalid bind_mount_result)"; break; } + if (want_errno_p != NULL) + *want_errno_p = want_errno; + + return string; +} + +void +die_with_bind_result (bind_mount_result res, + int saved_errno, + const char *format, + ...) +{ + va_list args; + bool want_errno = TRUE; + + fprintf (stderr, "bwrap: "); + + va_start (args, format); + vfprintf (stderr, format, args); + va_end (args); + + fprintf (stderr, ": %s", bind_mount_result_to_string (res, &want_errno)); + if (want_errno) fprintf (stderr, ": %s", strerror (saved_errno)); diff --git a/bind-mount.h b/bind-mount.h index 6b23ae3..2ec9031 100644 --- a/bind-mount.h +++ b/bind-mount.h @@ -18,6 +18,8 @@ #pragma once +#include "utils.h" + typedef enum { BIND_READONLY = (1 << 0), BIND_DEVICES = (1 << 2), @@ -41,6 +43,9 @@ bind_mount_result bind_mount (int proc_fd, const char *dest, bind_option_t options); +const char *bind_mount_result_to_string (bind_mount_result res, + bool *want_errno); + void die_with_bind_result (bind_mount_result res, int saved_errno, const char *format, |