mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
virprocess: Make virProcessRunInMountNamespace use virProcessRunInFork
Both virProcessRunInMountNamespace() and virProcessRunInFork() look very similar. De-duplicate the code and make virProcessRunInMountNamespace() call virProcessRunInFork(). Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
parent
9e8e74f463
commit
0f464afde1
@ -1073,16 +1073,22 @@ int virProcessGetStartTime(pid_t pid,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static int virProcessNamespaceHelper(int errfd,
|
typedef struct _virProcessNamespaceHelperData virProcessNamespaceHelperData;
|
||||||
pid_t pid,
|
struct _virProcessNamespaceHelperData {
|
||||||
virProcessNamespaceCallback cb,
|
pid_t pid;
|
||||||
|
virProcessNamespaceCallback cb;
|
||||||
|
void *opaque;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int virProcessNamespaceHelper(pid_t pid ATTRIBUTE_UNUSED,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
|
virProcessNamespaceHelperData *data = opaque;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
VIR_AUTOFREE(char *) path = NULL;
|
VIR_AUTOFREE(char *) path = NULL;
|
||||||
|
|
||||||
if (virAsprintf(&path, "/proc/%lld/ns/mnt", (long long) pid) < 0)
|
if (virAsprintf(&path, "/proc/%lld/ns/mnt", (long long) data->pid) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if ((fd = open(path, O_RDONLY)) < 0) {
|
if ((fd = open(path, O_RDONLY)) < 0) {
|
||||||
@ -1097,16 +1103,9 @@ static int virProcessNamespaceHelper(int errfd,
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = cb(pid, opaque);
|
ret = data->cb(data->pid, data->opaque);
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
if (ret < 0) {
|
|
||||||
virErrorPtr err = virGetLastError();
|
|
||||||
if (err) {
|
|
||||||
size_t len = strlen(err->message) + 1;
|
|
||||||
ignore_value(safewrite(errfd, err->message, len));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -1122,46 +1121,9 @@ virProcessRunInMountNamespace(pid_t pid,
|
|||||||
virProcessNamespaceCallback cb,
|
virProcessNamespaceCallback cb,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
virProcessNamespaceHelperData data = {.pid = pid, .cb = cb, .opaque = opaque};
|
||||||
pid_t child = -1;
|
|
||||||
int errfd[2] = { -1, -1 };
|
|
||||||
|
|
||||||
if (pipe2(errfd, O_CLOEXEC) < 0) {
|
return virProcessRunInFork(virProcessNamespaceHelper, &data);
|
||||||
virReportSystemError(errno, "%s",
|
|
||||||
_("Cannot create pipe for child"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((child = virFork()) < 0)
|
|
||||||
goto cleanup;
|
|
||||||
|
|
||||||
if (child == 0) {
|
|
||||||
VIR_FORCE_CLOSE(errfd[0]);
|
|
||||||
ret = virProcessNamespaceHelper(errfd[1], pid,
|
|
||||||
cb, opaque);
|
|
||||||
VIR_FORCE_CLOSE(errfd[1]);
|
|
||||||
_exit(ret < 0 ? EXIT_CANCELED : ret);
|
|
||||||
} else {
|
|
||||||
int status;
|
|
||||||
VIR_AUTOFREE(char *) buf = NULL;
|
|
||||||
|
|
||||||
VIR_FORCE_CLOSE(errfd[1]);
|
|
||||||
ignore_value(virFileReadHeaderFD(errfd[0], 1024, &buf));
|
|
||||||
ret = virProcessWait(child, &status, false);
|
|
||||||
if (!ret) {
|
|
||||||
ret = status == EXIT_CANCELED ? -1 : status;
|
|
||||||
if (ret) {
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
_("child reported: %s"),
|
|
||||||
NULLSTR(buf));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FORCE_CLOSE(errfd[0]);
|
|
||||||
VIR_FORCE_CLOSE(errfd[1]);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user