bind-mount: Factor out bind_mount_result_to_string()
Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
97
bind-mount.c
97
bind-mount.c
@@ -463,6 +463,64 @@ bind_mount (int proc_fd,
|
|||||||
return BIND_MOUNT_SUCCESS;
|
return BIND_MOUNT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
const char *string;
|
||||||
|
bool want_errno = TRUE;
|
||||||
|
|
||||||
|
switch (res)
|
||||||
|
{
|
||||||
|
case BIND_MOUNT_ERROR_MOUNT:
|
||||||
|
string = "Unable to mount source on destination";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BIND_MOUNT_ERROR_REALPATH_DEST:
|
||||||
|
string = "realpath(destination)";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BIND_MOUNT_ERROR_REOPEN_DEST:
|
||||||
|
string = "open(destination, O_PATH)";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BIND_MOUNT_ERROR_READLINK_DEST_PROC_FD:
|
||||||
|
string = "readlink(/proc/self/fd/<destination>)";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BIND_MOUNT_ERROR_FIND_DEST_MOUNT:
|
||||||
|
string = "Unable to find destination in mount table";
|
||||||
|
want_errno = FALSE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BIND_MOUNT_ERROR_REMOUNT_DEST:
|
||||||
|
string = "Unable to remount destination with correct flags";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BIND_MOUNT_ERROR_REMOUNT_SUBMOUNT:
|
||||||
|
string = "Unable to remount recursively with correct flags";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BIND_MOUNT_SUCCESS:
|
||||||
|
string = "Success";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
string = "(unknown/invalid bind_mount_result)";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (want_errno_p != NULL)
|
||||||
|
*want_errno_p = want_errno;
|
||||||
|
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
die_with_bind_result (bind_mount_result res,
|
die_with_bind_result (bind_mount_result res,
|
||||||
int saved_errno,
|
int saved_errno,
|
||||||
@@ -478,44 +536,7 @@ die_with_bind_result (bind_mount_result res,
|
|||||||
vfprintf (stderr, format, args);
|
vfprintf (stderr, format, args);
|
||||||
va_end (args);
|
va_end (args);
|
||||||
|
|
||||||
fprintf (stderr, ": ");
|
fprintf (stderr, ": %s", bind_mount_result_to_string (res, &want_errno));
|
||||||
|
|
||||||
switch (res)
|
|
||||||
{
|
|
||||||
case BIND_MOUNT_ERROR_MOUNT:
|
|
||||||
fprintf (stderr, "Unable to mount source on destination");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BIND_MOUNT_ERROR_REALPATH_DEST:
|
|
||||||
fprintf (stderr, "realpath(destination)");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BIND_MOUNT_ERROR_REOPEN_DEST:
|
|
||||||
fprintf (stderr, "open(destination, O_PATH)");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BIND_MOUNT_ERROR_READLINK_DEST_PROC_FD:
|
|
||||||
fprintf (stderr, "readlink(/proc/self/fd/<destination>)");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BIND_MOUNT_ERROR_FIND_DEST_MOUNT:
|
|
||||||
fprintf (stderr, "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");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BIND_MOUNT_ERROR_REMOUNT_SUBMOUNT:
|
|
||||||
fprintf (stderr, "Unable to remount recursively with correct flags");
|
|
||||||
break;
|
|
||||||
|
|
||||||
case BIND_MOUNT_SUCCESS:
|
|
||||||
default:
|
|
||||||
fprintf (stderr, "(unknown error %d)", res);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (want_errno)
|
if (want_errno)
|
||||||
fprintf (stderr, ": %s", strerror (saved_errno));
|
fprintf (stderr, ": %s", strerror (saved_errno));
|
||||||
|
@@ -18,6 +18,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
BIND_READONLY = (1 << 0),
|
BIND_READONLY = (1 << 0),
|
||||||
BIND_DEVICES = (1 << 2),
|
BIND_DEVICES = (1 << 2),
|
||||||
@@ -41,6 +43,9 @@ bind_mount_result bind_mount (int proc_fd,
|
|||||||
const char *dest,
|
const char *dest,
|
||||||
bind_option_t options);
|
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,
|
void die_with_bind_result (bind_mount_result res,
|
||||||
int saved_errno,
|
int saved_errno,
|
||||||
const char *format,
|
const char *format,
|
||||||
|
Reference in New Issue
Block a user