2012-09-24 16:54:51 +00:00
|
|
|
/*
|
|
|
|
* virprocess.c: interaction with processes
|
|
|
|
*
|
2015-01-08 14:36:54 +00:00
|
|
|
* Copyright (C) 2010-2015 Red Hat, Inc.
|
2012-09-24 16:54:51 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-12-21 16:50:18 +00:00
|
|
|
#include <fcntl.h>
|
2012-09-24 16:54:51 +00:00
|
|
|
#include <signal.h>
|
2012-09-24 17:10:37 +00:00
|
|
|
#include <sys/wait.h>
|
2014-09-10 09:47:19 +00:00
|
|
|
#include <unistd.h>
|
2016-11-10 13:55:48 +00:00
|
|
|
#if HAVE_SYS_MOUNT_H
|
|
|
|
# include <sys/mount.h>
|
|
|
|
#endif
|
2013-04-25 16:10:10 +00:00
|
|
|
#if HAVE_SETRLIMIT
|
|
|
|
# include <sys/time.h>
|
|
|
|
# include <sys/resource.h>
|
|
|
|
#endif
|
2015-01-08 14:36:54 +00:00
|
|
|
#if HAVE_SCHED_SETSCHEDULER
|
|
|
|
# include <sched.h>
|
|
|
|
#endif
|
2012-09-24 16:54:51 +00:00
|
|
|
|
2015-10-15 11:50:42 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || HAVE_BSD_CPU_AFFINITY
|
2013-04-25 16:05:00 +00:00
|
|
|
# include <sys/param.h>
|
Include param.h in case of HAVE_BSD_CPU_AFFINITY
This fixes compilation on kFreeBSD which otherwise fails like
CC util/libvirt_util_la-virprocess.lo
In file included from /usr/include/sys/cpuset.h:35:0,
from util/virprocess.c:43:
/usr/include/sys/_cpuset.h:49:43: error: 'NBBY' undeclared here (not in
a function)
long __bits[howmany(CPU_SETSIZE, _NCPUBITS)];
^
In file included from util/virprocess.c:43:0:
/usr/include/sys/cpuset.h:215:12: error: unknown type name 'cpusetid_t'
int cpuset(cpusetid_t *);
^
/usr/include/sys/cpuset.h:216:30: error: expected ')' before 'id_t'
int cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
^
/usr/include/sys/cpuset.h:217:42: error: expected ')' before 'id_t'
int cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
^
/usr/include/sys/cpuset.h:218:48: error: expected ')' before 'id_t'
int cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t
*);
^
/usr/include/sys/cpuset.h:219:48: error: expected ')' before 'id_t'
int cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const
cpuset_t *);
And it's the correct usage as documented in
http://www.freebsd.org/cgi/man.cgi?query=cpuset_setid
Also change the #ifdef HAVE_BSH_CPU_AFFINITY to #if for consistency.
2014-04-08 21:14:25 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-15 11:50:42 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2013-04-25 16:05:00 +00:00
|
|
|
# include <sys/sysctl.h>
|
|
|
|
# include <sys/user.h>
|
|
|
|
#endif
|
|
|
|
|
Include param.h in case of HAVE_BSD_CPU_AFFINITY
This fixes compilation on kFreeBSD which otherwise fails like
CC util/libvirt_util_la-virprocess.lo
In file included from /usr/include/sys/cpuset.h:35:0,
from util/virprocess.c:43:
/usr/include/sys/_cpuset.h:49:43: error: 'NBBY' undeclared here (not in
a function)
long __bits[howmany(CPU_SETSIZE, _NCPUBITS)];
^
In file included from util/virprocess.c:43:0:
/usr/include/sys/cpuset.h:215:12: error: unknown type name 'cpusetid_t'
int cpuset(cpusetid_t *);
^
/usr/include/sys/cpuset.h:216:30: error: expected ')' before 'id_t'
int cpuset_setid(cpuwhich_t, id_t, cpusetid_t);
^
/usr/include/sys/cpuset.h:217:42: error: expected ')' before 'id_t'
int cpuset_getid(cpulevel_t, cpuwhich_t, id_t, cpusetid_t *);
^
/usr/include/sys/cpuset.h:218:48: error: expected ')' before 'id_t'
int cpuset_getaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, cpuset_t
*);
^
/usr/include/sys/cpuset.h:219:48: error: expected ')' before 'id_t'
int cpuset_setaffinity(cpulevel_t, cpuwhich_t, id_t, size_t, const
cpuset_t *);
And it's the correct usage as documented in
http://www.freebsd.org/cgi/man.cgi?query=cpuset_setid
Also change the #ifdef HAVE_BSH_CPU_AFFINITY to #if for consistency.
2014-04-08 21:14:25 +00:00
|
|
|
#if HAVE_BSD_CPU_AFFINITY
|
2014-01-29 18:31:44 +00:00
|
|
|
# include <sys/cpuset.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-25 16:05:00 +00:00
|
|
|
#include "viratomic.h"
|
2012-09-24 16:54:51 +00:00
|
|
|
#include "virprocess.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-21 16:50:18 +00:00
|
|
|
#include "virfile.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2014-01-30 13:11:23 +00:00
|
|
|
#include "vircommand.h"
|
2012-09-24 17:10:37 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.process");
|
|
|
|
|
2014-10-02 03:18:52 +00:00
|
|
|
#ifdef __linux__
|
2014-09-10 09:47:19 +00:00
|
|
|
/*
|
|
|
|
* Workaround older glibc. While kernel may support the setns
|
|
|
|
* syscall, the glibc wrapper might not exist. If that's the
|
|
|
|
* case, use our own.
|
|
|
|
*/
|
2014-10-02 03:18:52 +00:00
|
|
|
# ifndef __NR_setns
|
|
|
|
# if defined(__x86_64__)
|
|
|
|
# define __NR_setns 308
|
|
|
|
# elif defined(__i386__)
|
|
|
|
# define __NR_setns 346
|
|
|
|
# elif defined(__arm__)
|
|
|
|
# define __NR_setns 375
|
|
|
|
# elif defined(__aarch64__)
|
|
|
|
# define __NR_setns 375
|
|
|
|
# elif defined(__powerpc__)
|
|
|
|
# define __NR_setns 350
|
|
|
|
# elif defined(__s390__)
|
|
|
|
# define __NR_setns 339
|
|
|
|
# endif
|
2014-09-10 09:47:19 +00:00
|
|
|
# endif
|
|
|
|
|
2014-10-02 03:18:52 +00:00
|
|
|
# ifndef HAVE_SETNS
|
|
|
|
# if defined(__NR_setns)
|
|
|
|
# include <sys/syscall.h>
|
2014-09-11 12:51:48 +00:00
|
|
|
|
2014-09-10 09:47:19 +00:00
|
|
|
static inline int setns(int fd, int nstype)
|
|
|
|
{
|
|
|
|
return syscall(__NR_setns, fd, nstype);
|
|
|
|
}
|
2014-10-02 03:18:52 +00:00
|
|
|
# else /* !__NR_setns */
|
|
|
|
# error Please determine the syscall number for setns on your architecture
|
|
|
|
# endif
|
|
|
|
# endif
|
|
|
|
#else /* !__linux__ */
|
2019-10-14 12:45:33 +00:00
|
|
|
static inline int setns(int fd G_GNUC_UNUSED, int nstype G_GNUC_UNUSED)
|
2014-09-11 12:51:48 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
2014-09-15 13:31:40 +00:00
|
|
|
_("Namespaces are not supported on this platform."));
|
2014-09-11 12:51:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-10-02 03:18:52 +00:00
|
|
|
#endif
|
2014-09-10 09:47:19 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virProcessSchedPolicy,
|
|
|
|
VIR_PROC_POLICY_LAST,
|
2015-01-08 14:36:54 +00:00
|
|
|
"none",
|
|
|
|
"batch",
|
|
|
|
"idle",
|
|
|
|
"fifo",
|
2019-01-20 16:30:15 +00:00
|
|
|
"rr",
|
|
|
|
);
|
2015-01-08 14:36:54 +00:00
|
|
|
|
2012-09-24 17:10:37 +00:00
|
|
|
/**
|
|
|
|
* virProcessTranslateStatus:
|
|
|
|
* @status: child exit status to translate
|
|
|
|
*
|
|
|
|
* Translate an exit status into a malloc'd string. Generic helper
|
|
|
|
* for virCommandRun(), virCommandWait(), virRun(), and virProcessWait()
|
|
|
|
* status argument, as well as raw waitpid().
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
virProcessTranslateStatus(int status)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
if (WIFEXITED(status)) {
|
2019-10-22 13:26:14 +00:00
|
|
|
buf = g_strdup_printf(_("exit status %d"),
|
|
|
|
WEXITSTATUS(status));
|
2012-09-24 17:10:37 +00:00
|
|
|
} else if (WIFSIGNALED(status)) {
|
2019-10-22 13:26:14 +00:00
|
|
|
buf = g_strdup_printf(_("fatal signal %d"),
|
|
|
|
WTERMSIG(status));
|
2012-09-24 17:10:37 +00:00
|
|
|
} else {
|
2019-10-22 13:26:14 +00:00
|
|
|
buf = g_strdup_printf(_("invalid value %d"), status);
|
2012-09-24 17:10:37 +00:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WIN32
|
|
|
|
/**
|
|
|
|
* virProcessAbort:
|
|
|
|
* @pid: child process to kill
|
|
|
|
*
|
|
|
|
* Abort a child process if PID is positive and that child is still
|
|
|
|
* running, without issuing any errors or affecting errno. Designed
|
|
|
|
* for error paths where some but not all paths to the cleanup code
|
|
|
|
* might have started the child process. If @pid is 0 or negative,
|
|
|
|
* this does nothing.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virProcessAbort(pid_t pid)
|
|
|
|
{
|
|
|
|
int saved_errno;
|
|
|
|
int ret;
|
|
|
|
int status;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *tmp = NULL;
|
2012-09-24 17:10:37 +00:00
|
|
|
|
|
|
|
if (pid <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* See if intermediate process has exited; if not, try a nice
|
|
|
|
* SIGTERM followed by a more severe SIGKILL.
|
|
|
|
*/
|
|
|
|
saved_errno = errno;
|
|
|
|
VIR_DEBUG("aborting child process %d", pid);
|
|
|
|
while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
|
|
|
|
errno == EINTR);
|
|
|
|
if (ret == pid) {
|
|
|
|
tmp = virProcessTranslateStatus(status);
|
|
|
|
VIR_DEBUG("process has ended: %s", tmp);
|
|
|
|
goto cleanup;
|
|
|
|
} else if (ret == 0) {
|
|
|
|
VIR_DEBUG("trying SIGTERM to child process %d", pid);
|
|
|
|
kill(pid, SIGTERM);
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(10 * 1000);
|
2012-09-24 17:10:37 +00:00
|
|
|
while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
|
|
|
|
errno == EINTR);
|
|
|
|
if (ret == pid) {
|
|
|
|
tmp = virProcessTranslateStatus(status);
|
|
|
|
VIR_DEBUG("process has ended: %s", tmp);
|
|
|
|
goto cleanup;
|
|
|
|
} else if (ret == 0) {
|
|
|
|
VIR_DEBUG("trying SIGKILL to child process %d", pid);
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
while ((ret = waitpid(pid, &status, 0)) == -1 &&
|
|
|
|
errno == EINTR);
|
|
|
|
if (ret == pid) {
|
|
|
|
tmp = virProcessTranslateStatus(status);
|
|
|
|
VIR_DEBUG("process has ended: %s", tmp);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-09-24 17:10:37 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
|
|
|
virProcessAbort(pid_t pid)
|
|
|
|
{
|
|
|
|
/* Not yet ported to mingw. Any volunteers? */
|
|
|
|
VIR_DEBUG("failed to reap child %lld, abandoning it", (long long)pid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virProcessWait:
|
|
|
|
* @pid: child to wait on
|
|
|
|
* @exitstatus: optional status collection
|
2014-02-20 03:23:44 +00:00
|
|
|
* @raw: whether to pass non-normal status back to caller
|
2012-09-24 17:10:37 +00:00
|
|
|
*
|
virFork: simplify semantics
The old semantics of virFork() violates the priciple of good
usability: it requires the caller to check the pid argument
after use, *even when virFork returned -1*, in order to properly
abort a child process that failed setup done immediately after
fork() - that is, the caller must call _exit() in the child.
While uses in virfile.c did this correctly, uses in 'virsh
lxc-enter-namespace' and 'virt-login-shell' would happily return
from the calling function in both the child and the parent,
leading to very confusing results. [Thankfully, I found the
problem by inspection, and can't actually trigger the double
return on error without an LD_PRELOAD library.]
It is much better if the semantics of virFork are impossible
to abuse. Looking at virFork(), the parent could only ever
return -1 with a non-negative pid if it misused pthread_sigmask,
but this never happens. Up until this patch series, the child
could return -1 with non-negative pid if it fails to set up
signals correctly, but we recently fixed that to make the child
call _exit() at that point instead of forcing the caller to do
it. Thus, the return value and contents of the pid argument are
now redundant (a -1 return now happens only for failure to fork,
a child 0 return only happens for a successful 0 pid, and a
parent 0 return only happens for a successful non-zero pid),
so we might as well return the pid directly rather than an
integer of whether it succeeded or failed; this is also good
from the interface design perspective as users are already
familiar with fork() semantics.
One last change in this patch: before returning the pid directly,
I found cases where using virProcessWait unconditionally on a
cleanup path of a virFork's -1 pid return would be nicer if there
were a way to avoid it overwriting an earlier message. While
such paths are a bit harder to come by with my change to a direct
pid return, I decided to keep the virProcessWait change in this
patch.
* src/util/vircommand.h (virFork): Change signature.
* src/util/vircommand.c (virFork): Guarantee that child will only
return on success, to simplify callers. Return pid rather than
status, now that the situations are always the same.
(virExec): Adjust caller, also avoid open-coding process death.
* src/util/virprocess.c (virProcessWait): Tweak semantics when pid
is -1.
(virProcessRunInMountNamespace): Adjust caller.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* tools/virt-login-shell.c (main): Likewise.
* tools/virsh-domain.c (cmdLxcEnterNamespace): Likewise.
* tests/commandtest.c (test23): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-22 00:54:33 +00:00
|
|
|
* Wait for a child process to complete. If @pid is -1, do nothing, but
|
|
|
|
* return -1 (useful for error cleanup, and assumes an earlier message was
|
|
|
|
* already issued). All other pids issue an error message on failure.
|
|
|
|
*
|
|
|
|
* If @exitstatus is NULL, then the child must exit normally with status 0.
|
|
|
|
* Otherwise, if @raw is false, the child must exit normally, and
|
|
|
|
* @exitstatus will contain the final exit status (no need for the caller
|
|
|
|
* to use WEXITSTATUS()). If @raw is true, then the result of waitpid() is
|
|
|
|
* returned in @exitstatus, and the caller must use WIFEXITED() and friends
|
|
|
|
* to decipher the child's status.
|
2014-02-20 03:23:44 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on a successful wait. Returns -1 on any error waiting for
|
|
|
|
* completion, or if the command completed with a status that cannot be
|
|
|
|
* reflected via the choice of @exitstatus and @raw.
|
2012-09-24 17:10:37 +00:00
|
|
|
*/
|
|
|
|
int
|
2014-02-20 03:23:44 +00:00
|
|
|
virProcessWait(pid_t pid, int *exitstatus, bool raw)
|
2012-09-24 17:10:37 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int status;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *st = NULL;
|
2012-09-24 17:10:37 +00:00
|
|
|
|
|
|
|
if (pid <= 0) {
|
virFork: simplify semantics
The old semantics of virFork() violates the priciple of good
usability: it requires the caller to check the pid argument
after use, *even when virFork returned -1*, in order to properly
abort a child process that failed setup done immediately after
fork() - that is, the caller must call _exit() in the child.
While uses in virfile.c did this correctly, uses in 'virsh
lxc-enter-namespace' and 'virt-login-shell' would happily return
from the calling function in both the child and the parent,
leading to very confusing results. [Thankfully, I found the
problem by inspection, and can't actually trigger the double
return on error without an LD_PRELOAD library.]
It is much better if the semantics of virFork are impossible
to abuse. Looking at virFork(), the parent could only ever
return -1 with a non-negative pid if it misused pthread_sigmask,
but this never happens. Up until this patch series, the child
could return -1 with non-negative pid if it fails to set up
signals correctly, but we recently fixed that to make the child
call _exit() at that point instead of forcing the caller to do
it. Thus, the return value and contents of the pid argument are
now redundant (a -1 return now happens only for failure to fork,
a child 0 return only happens for a successful 0 pid, and a
parent 0 return only happens for a successful non-zero pid),
so we might as well return the pid directly rather than an
integer of whether it succeeded or failed; this is also good
from the interface design perspective as users are already
familiar with fork() semantics.
One last change in this patch: before returning the pid directly,
I found cases where using virProcessWait unconditionally on a
cleanup path of a virFork's -1 pid return would be nicer if there
were a way to avoid it overwriting an earlier message. While
such paths are a bit harder to come by with my change to a direct
pid return, I decided to keep the virProcessWait change in this
patch.
* src/util/vircommand.h (virFork): Change signature.
* src/util/vircommand.c (virFork): Guarantee that child will only
return on success, to simplify callers. Return pid rather than
status, now that the situations are always the same.
(virExec): Adjust caller, also avoid open-coding process death.
* src/util/virprocess.c (virProcessWait): Tweak semantics when pid
is -1.
(virProcessRunInMountNamespace): Adjust caller.
* src/util/virfile.c (virFileAccessibleAs, virFileOpenForked)
(virDirCreate): Likewise.
* tools/virt-login-shell.c (main): Likewise.
* tools/virsh-domain.c (cmdLxcEnterNamespace): Likewise.
* tests/commandtest.c (test23): Likewise.
Signed-off-by: Eric Blake <eblake@redhat.com>
2013-12-22 00:54:33 +00:00
|
|
|
if (pid != -1)
|
|
|
|
virReportSystemError(EINVAL, _("unable to wait for process %lld"),
|
|
|
|
(long long) pid);
|
2012-09-24 17:10:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for intermediate process to exit */
|
|
|
|
while ((ret = waitpid(pid, &status, 0)) == -1 &&
|
|
|
|
errno == EINTR);
|
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
virReportSystemError(errno, _("unable to wait for process %lld"),
|
|
|
|
(long long) pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exitstatus == NULL) {
|
2014-02-20 03:23:44 +00:00
|
|
|
if (status != 0)
|
|
|
|
goto error;
|
|
|
|
} else if (raw) {
|
2012-09-24 17:10:37 +00:00
|
|
|
*exitstatus = status;
|
2014-02-20 03:23:44 +00:00
|
|
|
} else if (WIFEXITED(status)) {
|
|
|
|
*exitstatus = WEXITSTATUS(status);
|
|
|
|
} else {
|
|
|
|
goto error;
|
2012-09-24 17:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-02-20 03:23:44 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2018-07-28 18:01:44 +00:00
|
|
|
st = virProcessTranslateStatus(status);
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Child process (%lld) unexpected %s"),
|
|
|
|
(long long) pid, NULLSTR(st));
|
2014-02-20 03:23:44 +00:00
|
|
|
return -1;
|
2012-09-24 17:10:37 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 16:54:51 +00:00
|
|
|
|
|
|
|
/* send signal to a single process */
|
|
|
|
int virProcessKill(pid_t pid, int sig)
|
|
|
|
{
|
|
|
|
if (pid <= 1) {
|
|
|
|
errno = ESRCH;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
/* Mingw / Windows don't have many signals (AFAIK) */
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
/* This does a Ctrl+C equiv */
|
|
|
|
if (!GenerateConsoleCtrlEvent(CTRL_C_EVENT, pid)) {
|
|
|
|
errno = ESRCH;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SIGTERM:
|
|
|
|
/* Since TerminateProcess is closer to SIG_KILL, we do
|
|
|
|
* a Ctrl+Break equiv which is more pleasant like the
|
|
|
|
* good old unix SIGTERM/HUP
|
|
|
|
*/
|
|
|
|
if (!GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, pid)) {
|
|
|
|
errno = ESRCH;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
HANDLE proc;
|
|
|
|
proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
|
|
|
if (!proc) {
|
|
|
|
errno = ESRCH; /* Not entirely accurate, but close enough */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TerminateProcess is more or less equiv to SIG_KILL, in that
|
|
|
|
* a process can't trap / block it
|
|
|
|
*/
|
|
|
|
if (sig != 0 && !TerminateProcess(proc, sig)) {
|
|
|
|
errno = ESRCH;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CloseHandle(proc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
return kill(pid, sig);
|
|
|
|
#endif
|
|
|
|
}
|
2012-09-26 14:42:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to kill the process and verify it has exited
|
|
|
|
*
|
|
|
|
* Returns 0 if it was killed gracefully, 1 if it
|
2014-03-27 05:49:48 +00:00
|
|
|
* was killed forcibly, -1 if it is still alive,
|
2012-09-26 14:42:58 +00:00
|
|
|
* or another error occurred.
|
2018-08-02 07:05:18 +00:00
|
|
|
*
|
|
|
|
* Callers can proide an extra delay in seconds to
|
|
|
|
* wait longer than the default.
|
2012-09-26 14:42:58 +00:00
|
|
|
*/
|
|
|
|
int
|
2018-08-02 07:05:18 +00:00
|
|
|
virProcessKillPainfullyDelay(pid_t pid, bool force, unsigned int extradelay)
|
2012-09-26 14:42:58 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2018-08-02 07:05:18 +00:00
|
|
|
/* This is in 1/5th seconds since polling is on a 0.2s interval */
|
2018-08-06 10:10:38 +00:00
|
|
|
unsigned int polldelay = (force ? 200 : 75) + (extradelay*5);
|
2012-09-26 14:42:58 +00:00
|
|
|
const char *signame = "TERM";
|
|
|
|
|
2018-08-02 07:05:18 +00:00
|
|
|
VIR_DEBUG("vpid=%lld force=%d extradelay=%u",
|
|
|
|
(long long)pid, force, extradelay);
|
2012-09-26 14:42:58 +00:00
|
|
|
|
|
|
|
/* This loop sends SIGTERM, then waits a few iterations (10 seconds)
|
|
|
|
* to see if it dies. If the process still hasn't exited, and
|
|
|
|
* @force is requested, a SIGKILL will be sent, and this will
|
2018-08-06 10:10:38 +00:00
|
|
|
* wait up to 30 seconds more for the process to exit before
|
2012-09-26 14:42:58 +00:00
|
|
|
* returning.
|
|
|
|
*
|
2018-08-02 07:05:18 +00:00
|
|
|
* An extra delay can be passed by the caller for cases that are
|
|
|
|
* expected to clean up slower than usual.
|
|
|
|
*
|
2012-09-26 14:42:58 +00:00
|
|
|
* Note that setting @force could result in dataloss for the process.
|
|
|
|
*/
|
2018-08-02 07:05:18 +00:00
|
|
|
for (i = 0; i < polldelay; i++) {
|
2012-09-26 14:42:58 +00:00
|
|
|
int signum;
|
|
|
|
if (i == 0) {
|
|
|
|
signum = SIGTERM; /* kindly suggest it should exit */
|
2014-10-09 08:00:05 +00:00
|
|
|
} else if (i == 50 && force) {
|
2012-10-31 17:00:50 +00:00
|
|
|
VIR_DEBUG("Timed out waiting after SIGTERM to process %lld, "
|
|
|
|
"sending SIGKILL", (long long)pid);
|
2012-10-16 14:46:12 +00:00
|
|
|
/* No SIGKILL kill on Win32 ! Use SIGABRT instead which our
|
2012-10-16 15:37:26 +00:00
|
|
|
* virProcessKill proc will handle more or less like SIGKILL */
|
2012-10-16 14:46:12 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
signum = SIGABRT; /* kill it after a grace period */
|
|
|
|
signame = "ABRT";
|
|
|
|
#else
|
2012-09-26 14:42:58 +00:00
|
|
|
signum = SIGKILL; /* kill it after a grace period */
|
|
|
|
signame = "KILL";
|
2012-10-16 14:46:12 +00:00
|
|
|
#endif
|
2012-09-26 14:42:58 +00:00
|
|
|
} else {
|
|
|
|
signum = 0; /* Just check for existence */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virProcessKill(pid, signum) < 0) {
|
|
|
|
if (errno != ESRCH) {
|
|
|
|
virReportSystemError(errno,
|
2012-10-31 17:00:50 +00:00
|
|
|
_("Failed to terminate process %lld with SIG%s"),
|
|
|
|
(long long)pid, signame);
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2012-09-26 14:42:58 +00:00
|
|
|
}
|
2019-10-21 18:19:04 +00:00
|
|
|
return signum == SIGTERM ? 0 : 1;
|
2012-09-26 14:42:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(200 * 1000);
|
2012-09-26 14:42:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 14:22:46 +00:00
|
|
|
virReportSystemError(EBUSY,
|
|
|
|
_("Failed to terminate process %lld with SIG%s"),
|
|
|
|
(long long)pid, signame);
|
2012-09-26 14:42:58 +00:00
|
|
|
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2012-09-26 14:42:58 +00:00
|
|
|
}
|
2012-12-13 14:58:41 +00:00
|
|
|
|
|
|
|
|
2018-08-02 07:05:18 +00:00
|
|
|
int virProcessKillPainfully(pid_t pid, bool force)
|
|
|
|
{
|
|
|
|
return virProcessKillPainfullyDelay(pid, force, 0);
|
|
|
|
}
|
|
|
|
|
2012-12-13 14:58:41 +00:00
|
|
|
#if HAVE_SCHED_GETAFFINITY
|
|
|
|
|
|
|
|
int virProcessSetAffinity(pid_t pid, virBitmapPtr map)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2015-10-27 18:14:01 +00:00
|
|
|
VIR_DEBUG("Set process affinity on %lld", (long long)pid);
|
2012-12-13 14:58:41 +00:00
|
|
|
int numcpus = 1024;
|
|
|
|
size_t masklen;
|
|
|
|
cpu_set_t *mask;
|
|
|
|
|
|
|
|
/* Not only may the statically allocated cpu_set_t be too small,
|
|
|
|
* but there is no way to ask the kernel what size is large enough.
|
|
|
|
* So you have no option but to pick a size, try, catch EINVAL,
|
|
|
|
* enlarge, and re-try.
|
|
|
|
*
|
|
|
|
* http://lkml.org/lkml/2009/7/28/620
|
|
|
|
*/
|
2014-03-25 06:53:22 +00:00
|
|
|
realloc:
|
2012-12-13 14:58:41 +00:00
|
|
|
masklen = CPU_ALLOC_SIZE(numcpus);
|
|
|
|
mask = CPU_ALLOC(numcpus);
|
|
|
|
|
|
|
|
if (!mask) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CPU_ZERO_S(masklen, mask);
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < virBitmapSize(map); i++) {
|
2015-03-11 15:41:57 +00:00
|
|
|
if (virBitmapIsBitSet(map, i))
|
2012-12-13 14:58:41 +00:00
|
|
|
CPU_SET_S(i, masklen, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_setaffinity(pid, masklen, mask) < 0) {
|
|
|
|
CPU_FREE(mask);
|
|
|
|
if (errno == EINVAL &&
|
|
|
|
numcpus < (1024 << 8)) { /* 262144 cpus ought to be enough for anyone */
|
|
|
|
numcpus = numcpus << 2;
|
|
|
|
goto realloc;
|
|
|
|
}
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot set CPU affinity on process %d"), pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
CPU_FREE(mask);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
virBitmapPtr
|
|
|
|
virProcessGetAffinity(pid_t pid)
|
2012-12-13 14:58:41 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-12-13 14:58:41 +00:00
|
|
|
cpu_set_t *mask;
|
2015-06-02 06:34:50 +00:00
|
|
|
size_t masklen;
|
2015-06-04 12:13:15 +00:00
|
|
|
size_t ncpus;
|
2015-06-02 06:34:50 +00:00
|
|
|
virBitmapPtr ret = NULL;
|
2012-12-13 14:58:41 +00:00
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
/* 262144 cpus ought to be enough for anyone */
|
2015-06-04 12:13:15 +00:00
|
|
|
ncpus = 1024 << 8;
|
|
|
|
masklen = CPU_ALLOC_SIZE(ncpus);
|
|
|
|
mask = CPU_ALLOC(ncpus);
|
2012-12-13 14:58:41 +00:00
|
|
|
|
|
|
|
if (!mask) {
|
|
|
|
virReportOOMError();
|
2015-06-02 06:34:50 +00:00
|
|
|
return NULL;
|
2012-12-13 14:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPU_ZERO_S(masklen, mask);
|
2015-06-02 06:34:50 +00:00
|
|
|
|
2012-12-13 14:58:41 +00:00
|
|
|
if (sched_getaffinity(pid, masklen, mask) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot get CPU affinity of process %d"), pid);
|
2015-06-02 06:34:50 +00:00
|
|
|
goto cleanup;
|
2012-12-13 14:58:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 12:13:15 +00:00
|
|
|
if (!(ret = virBitmapNew(ncpus)))
|
2015-06-02 06:34:50 +00:00
|
|
|
goto cleanup;
|
2012-12-13 14:58:41 +00:00
|
|
|
|
2015-06-04 12:13:15 +00:00
|
|
|
for (i = 0; i < ncpus; i++) {
|
|
|
|
/* coverity[overrun-local] */
|
2012-12-13 14:58:41 +00:00
|
|
|
if (CPU_ISSET_S(i, masklen, mask))
|
2015-06-02 06:34:50 +00:00
|
|
|
ignore_value(virBitmapSetBit(ret, i));
|
2012-12-13 14:58:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
cleanup:
|
|
|
|
CPU_FREE(mask);
|
2012-12-13 14:58:41 +00:00
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
return ret;
|
2012-12-13 14:58:41 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 18:31:44 +00:00
|
|
|
#elif defined(HAVE_BSD_CPU_AFFINITY)
|
2012-12-13 14:58:41 +00:00
|
|
|
|
2015-06-02 06:36:21 +00:00
|
|
|
int virProcessSetAffinity(pid_t pid,
|
2012-12-13 14:58:41 +00:00
|
|
|
virBitmapPtr map)
|
|
|
|
{
|
2014-01-25 16:11:01 +00:00
|
|
|
size_t i;
|
|
|
|
cpuset_t mask;
|
|
|
|
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
for (i = 0; i < virBitmapSize(map); i++) {
|
2016-02-12 05:49:50 +00:00
|
|
|
if (virBitmapIsBitSet(map, i))
|
2014-01-25 16:11:01 +00:00
|
|
|
CPU_SET(i, &mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
|
|
|
|
sizeof(mask), &mask) != 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot set CPU affinity on process %d"), pid);
|
2012-12-13 14:58:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
virBitmapPtr
|
|
|
|
virProcessGetAffinity(pid_t pid)
|
2012-12-13 14:58:41 +00:00
|
|
|
{
|
2014-01-25 16:11:01 +00:00
|
|
|
size_t i;
|
|
|
|
cpuset_t mask;
|
2015-06-02 06:34:50 +00:00
|
|
|
virBitmapPtr ret = NULL;
|
2014-01-25 16:11:01 +00:00
|
|
|
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid,
|
|
|
|
sizeof(mask), &mask) != 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot get CPU affinity of process %d"), pid);
|
2015-06-02 06:34:50 +00:00
|
|
|
return NULL;
|
2014-01-25 16:11:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-08 07:13:41 +00:00
|
|
|
if (!(ret = virBitmapNew(sizeof(mask) * 8)))
|
2015-06-02 06:34:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(mask) * 8; i++)
|
2014-01-25 16:11:01 +00:00
|
|
|
if (CPU_ISSET(i, &mask))
|
2015-06-02 06:34:50 +00:00
|
|
|
ignore_value(virBitmapSetBit(ret, i));
|
2012-12-13 14:58:41 +00:00
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
return ret;
|
2012-12-13 14:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_SCHED_GETAFFINITY */
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
int virProcessSetAffinity(pid_t pid G_GNUC_UNUSED,
|
|
|
|
virBitmapPtr map G_GNUC_UNUSED)
|
2012-12-13 14:58:41 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Process CPU affinity is not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
virBitmapPtr
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessGetAffinity(pid_t pid G_GNUC_UNUSED)
|
2012-12-13 14:58:41 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Process CPU affinity is not supported on this platform"));
|
2015-06-02 06:34:50 +00:00
|
|
|
return NULL;
|
2012-12-13 14:58:41 +00:00
|
|
|
}
|
|
|
|
#endif /* HAVE_SCHED_GETAFFINITY */
|
2012-12-21 16:50:18 +00:00
|
|
|
|
|
|
|
|
2015-06-01 12:31:44 +00:00
|
|
|
int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
DIR *dir = NULL;
|
|
|
|
int value;
|
|
|
|
struct dirent *ent;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *taskPath = NULL;
|
2015-06-01 12:31:44 +00:00
|
|
|
|
|
|
|
*npids = 0;
|
|
|
|
*pids = NULL;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
taskPath = g_strdup_printf("/proc/%llu/task", (long long)pid);
|
2015-06-01 12:31:44 +00:00
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dir, taskPath) < 0)
|
2015-06-01 12:31:44 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
while ((value = virDirRead(dir, &ent, taskPath)) > 0) {
|
2015-08-17 23:03:40 +00:00
|
|
|
long long tmp;
|
2015-06-01 12:31:44 +00:00
|
|
|
pid_t tmp_pid;
|
|
|
|
|
2015-08-17 23:03:40 +00:00
|
|
|
if (virStrToLong_ll(ent->d_name, NULL, 10, &tmp) < 0)
|
2015-06-01 12:31:44 +00:00
|
|
|
goto cleanup;
|
2015-08-17 23:03:40 +00:00
|
|
|
tmp_pid = tmp;
|
2015-06-01 12:31:44 +00:00
|
|
|
|
|
|
|
if (VIR_APPEND_ELEMENT(*pids, *npids, tmp_pid) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dir);
|
2015-06-01 12:31:44 +00:00
|
|
|
if (ret < 0)
|
|
|
|
VIR_FREE(*pids);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-21 16:50:18 +00:00
|
|
|
int virProcessGetNamespaces(pid_t pid,
|
|
|
|
size_t *nfdlist,
|
|
|
|
int **fdlist)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2013-06-07 16:30:09 +00:00
|
|
|
size_t i = 0;
|
|
|
|
const char *ns[] = { "user", "ipc", "uts", "net", "pid", "mnt" };
|
2012-12-21 16:50:18 +00:00
|
|
|
|
|
|
|
*nfdlist = 0;
|
|
|
|
*fdlist = NULL;
|
|
|
|
|
2019-10-15 11:55:26 +00:00
|
|
|
for (i = 0; i < G_N_ELEMENTS(ns); i++) {
|
2012-12-21 16:50:18 +00:00
|
|
|
int fd;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *nsfile = NULL;
|
2012-12-21 16:50:18 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
nsfile = g_strdup_printf("/proc/%llu/ns/%s", (long long)pid, ns[i]);
|
2012-12-21 16:50:18 +00:00
|
|
|
|
2015-04-09 07:22:43 +00:00
|
|
|
if ((fd = open(nsfile, O_RDONLY)) >= 0) {
|
2013-06-07 16:30:09 +00:00
|
|
|
if (VIR_EXPAND_N(*fdlist, *nfdlist, 1) < 0) {
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-12-21 16:50:18 +00:00
|
|
|
|
2013-06-07 16:30:09 +00:00
|
|
|
(*fdlist)[(*nfdlist)-1] = fd;
|
2012-12-21 16:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-12-21 16:50:18 +00:00
|
|
|
if (ret < 0) {
|
2013-06-07 16:30:09 +00:00
|
|
|
for (i = 0; i < *nfdlist; i++)
|
2012-12-21 16:50:18 +00:00
|
|
|
VIR_FORCE_CLOSE((*fdlist)[i]);
|
|
|
|
VIR_FREE(*fdlist);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virProcessSetNamespaces(size_t nfdlist,
|
|
|
|
int *fdlist)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (nfdlist == 0) {
|
|
|
|
virReportInvalidArg(nfdlist, "%s",
|
2018-09-19 08:38:14 +00:00
|
|
|
_("Expected at least one file descriptor"));
|
2012-12-21 16:50:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < nfdlist; i++) {
|
2015-08-27 00:09:54 +00:00
|
|
|
if (fdlist[i] < 0)
|
|
|
|
continue;
|
|
|
|
|
2013-06-07 16:30:09 +00:00
|
|
|
/* We get EINVAL if new NS is same as the current
|
|
|
|
* NS, or if the fd namespace doesn't match the
|
|
|
|
* type passed to setns()'s second param. Since we
|
|
|
|
* pass 0, we know the EINVAL is harmless
|
|
|
|
*/
|
|
|
|
if (setns(fdlist[i], 0) < 0 &&
|
|
|
|
errno != EINVAL) {
|
2012-12-21 16:50:18 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to join domain namespace"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-25 16:10:10 +00:00
|
|
|
|
|
|
|
#if HAVE_PRLIMIT
|
|
|
|
static int
|
2015-11-20 16:21:11 +00:00
|
|
|
virProcessPrLimit(pid_t pid,
|
|
|
|
int resource,
|
|
|
|
const struct rlimit *new_limit,
|
|
|
|
struct rlimit *old_limit)
|
2013-04-25 16:10:10 +00:00
|
|
|
{
|
2015-11-20 16:21:11 +00:00
|
|
|
return prlimit(pid, resource, new_limit, old_limit);
|
2013-04-25 16:10:10 +00:00
|
|
|
}
|
2013-05-02 21:46:19 +00:00
|
|
|
#elif HAVE_SETRLIMIT
|
2013-04-25 16:10:10 +00:00
|
|
|
static int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessPrLimit(pid_t pid G_GNUC_UNUSED,
|
|
|
|
int resource G_GNUC_UNUSED,
|
|
|
|
const struct rlimit *new_limit G_GNUC_UNUSED,
|
|
|
|
struct rlimit *old_limit G_GNUC_UNUSED)
|
2013-04-25 16:10:10 +00:00
|
|
|
{
|
|
|
|
errno = ENOSYS;
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-02 21:46:19 +00:00
|
|
|
#endif
|
2013-04-25 16:10:10 +00:00
|
|
|
|
|
|
|
#if HAVE_SETRLIMIT && defined(RLIMIT_MEMLOCK)
|
|
|
|
int
|
|
|
|
virProcessSetMaxMemLock(pid_t pid, unsigned long long bytes)
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
if (bytes == 0)
|
|
|
|
return 0;
|
|
|
|
|
2017-03-22 12:44:45 +00:00
|
|
|
/* We use VIR_DOMAIN_MEMORY_PARAM_UNLIMITED internally to represent
|
|
|
|
* unlimited memory amounts, but setrlimit() and prlimit() use
|
|
|
|
* RLIM_INFINITY for the same purpose, so we need to translate between
|
|
|
|
* the two conventions */
|
|
|
|
if (virMemoryLimitIsSet(bytes))
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = bytes;
|
|
|
|
else
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
|
|
|
|
|
2013-04-25 16:10:10 +00:00
|
|
|
if (pid == 0) {
|
|
|
|
if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit locked memory to %llu"),
|
|
|
|
bytes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-20 16:21:11 +00:00
|
|
|
if (virProcessPrLimit(pid, RLIMIT_MEMLOCK, &rlim, NULL) < 0) {
|
2013-04-25 16:10:10 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit locked memory "
|
|
|
|
"of process %lld to %llu"),
|
|
|
|
(long long int)pid, bytes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 13:46:12 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Locked memory for process %lld limited to %llu bytes",
|
|
|
|
(long long int) pid, bytes);
|
|
|
|
|
2013-04-25 16:10:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* ! (HAVE_SETRLIMIT && defined(RLIMIT_MEMLOCK)) */
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessSetMaxMemLock(pid_t pid G_GNUC_UNUSED, unsigned long long bytes)
|
2013-04-25 16:10:10 +00:00
|
|
|
{
|
|
|
|
if (bytes == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! (HAVE_SETRLIMIT && defined(RLIMIT_MEMLOCK)) */
|
|
|
|
|
2015-11-20 16:06:41 +00:00
|
|
|
#if HAVE_GETRLIMIT && defined(RLIMIT_MEMLOCK)
|
|
|
|
int
|
|
|
|
virProcessGetMaxMemLock(pid_t pid,
|
|
|
|
unsigned long long *bytes)
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
if (getrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s",
|
|
|
|
_("cannot get locked memory limit"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virProcessPrLimit(pid, RLIMIT_MEMLOCK, NULL, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot get locked memory limit "
|
|
|
|
"of process %lld"),
|
|
|
|
(long long int) pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virProcessSetMaxMemLock() sets both rlim_cur and rlim_max to the
|
2017-03-22 12:44:45 +00:00
|
|
|
* same value, so we can retrieve just rlim_max here. We use
|
|
|
|
* VIR_DOMAIN_MEMORY_PARAM_UNLIMITED internally to represent unlimited
|
|
|
|
* memory amounts, but setrlimit() and prlimit() use RLIM_INFINITY for the
|
|
|
|
* same purpose, so we need to translate between the two conventions */
|
|
|
|
if (rlim.rlim_max == RLIM_INFINITY)
|
|
|
|
*bytes = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
else
|
|
|
|
*bytes = rlim.rlim_max;
|
2015-11-20 16:06:41 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* ! (HAVE_GETRLIMIT && defined(RLIMIT_MEMLOCK)) */
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessGetMaxMemLock(pid_t pid G_GNUC_UNUSED,
|
2015-11-20 16:06:41 +00:00
|
|
|
unsigned long long *bytes)
|
|
|
|
{
|
|
|
|
if (!bytes)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! (HAVE_GETRLIMIT && defined(RLIMIT_MEMLOCK)) */
|
2013-04-25 16:10:10 +00:00
|
|
|
|
|
|
|
#if HAVE_SETRLIMIT && defined(RLIMIT_NPROC)
|
|
|
|
int
|
|
|
|
virProcessSetMaxProcesses(pid_t pid, unsigned int procs)
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
if (procs == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = procs;
|
|
|
|
if (pid == 0) {
|
|
|
|
if (setrlimit(RLIMIT_NPROC, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit number of subprocesses to %u"),
|
|
|
|
procs);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-20 16:21:11 +00:00
|
|
|
if (virProcessPrLimit(pid, RLIMIT_NPROC, &rlim, NULL) < 0) {
|
2013-04-25 16:10:10 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit number of subprocesses "
|
|
|
|
"of process %lld to %u"),
|
|
|
|
(long long int)pid, procs);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* ! (HAVE_SETRLIMIT && defined(RLIMIT_NPROC)) */
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessSetMaxProcesses(pid_t pid G_GNUC_UNUSED, unsigned int procs)
|
2013-04-25 16:10:10 +00:00
|
|
|
{
|
|
|
|
if (procs == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! (HAVE_SETRLIMIT && defined(RLIMIT_NPROC)) */
|
|
|
|
|
|
|
|
#if HAVE_SETRLIMIT && defined(RLIMIT_NOFILE)
|
|
|
|
int
|
|
|
|
virProcessSetMaxFiles(pid_t pid, unsigned int files)
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
if (files == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Max number of opened files is one greater than actual limit. See
|
|
|
|
* man setrlimit.
|
|
|
|
*
|
|
|
|
* NB: That indicates to me that we would want the following code
|
|
|
|
* to say "files - 1", but the original of this code in
|
|
|
|
* qemu_process.c also had files + 1, so this preserves current
|
|
|
|
* behavior.
|
|
|
|
*/
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = files + 1;
|
|
|
|
if (pid == 0) {
|
|
|
|
if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit number of open files to %u"),
|
|
|
|
files);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-20 16:21:11 +00:00
|
|
|
if (virProcessPrLimit(pid, RLIMIT_NOFILE, &rlim, NULL) < 0) {
|
2013-04-25 16:10:10 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit number of open files "
|
|
|
|
"of process %lld to %u"),
|
|
|
|
(long long int)pid, files);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* ! (HAVE_SETRLIMIT && defined(RLIMIT_NOFILE)) */
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessSetMaxFiles(pid_t pid G_GNUC_UNUSED, unsigned int files)
|
2013-04-25 16:10:10 +00:00
|
|
|
{
|
|
|
|
if (files == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! (HAVE_SETRLIMIT && defined(RLIMIT_NOFILE)) */
|
2013-04-25 16:05:00 +00:00
|
|
|
|
2015-03-18 11:14:55 +00:00
|
|
|
#if HAVE_SETRLIMIT && defined(RLIMIT_CORE)
|
|
|
|
int
|
|
|
|
virProcessSetMaxCoreSize(pid_t pid, unsigned long long bytes)
|
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
|
|
|
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = bytes;
|
|
|
|
if (pid == 0) {
|
|
|
|
if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit core file size to %llu"),
|
|
|
|
bytes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virProcessPrLimit(pid, RLIMIT_CORE, &rlim, NULL) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit core file size "
|
|
|
|
"of process %lld to %llu"),
|
|
|
|
(long long int)pid, bytes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* ! (HAVE_SETRLIMIT && defined(RLIMIT_CORE)) */
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessSetMaxCoreSize(pid_t pid G_GNUC_UNUSED,
|
2015-03-18 11:14:55 +00:00
|
|
|
unsigned long long bytes)
|
|
|
|
{
|
|
|
|
if (bytes == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! (HAVE_SETRLIMIT && defined(RLIMIT_CORE)) */
|
|
|
|
|
|
|
|
|
2013-04-25 16:05:00 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
/*
|
|
|
|
* Port of code from polkitunixprocess.c under terms
|
|
|
|
* of the LGPLv2+
|
|
|
|
*/
|
|
|
|
int virProcessGetStartTime(pid_t pid,
|
|
|
|
unsigned long long *timestamp)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int len;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *filename = NULL;
|
|
|
|
g_autofree char *buf = NULL;
|
2019-02-22 14:01:02 +00:00
|
|
|
VIR_AUTOSTRINGLIST tokens = NULL;
|
2013-04-25 16:05:00 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
filename = g_strdup_printf("/proc/%llu/stat", (long long)pid);
|
2013-04-25 16:05:00 +00:00
|
|
|
|
|
|
|
if ((len = virFileReadAll(filename, 1024, &buf)) < 0)
|
2018-07-28 18:01:45 +00:00
|
|
|
return -1;
|
2013-04-25 16:05:00 +00:00
|
|
|
|
|
|
|
/* start time is the token at index 19 after the '(process name)' entry - since only this
|
|
|
|
* field can contain the ')' character, search backwards for this to avoid malicious
|
|
|
|
* processes trying to fool us
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!(tmp = strrchr(buf, ')'))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find start time in %s"),
|
|
|
|
filename);
|
2018-07-28 18:01:45 +00:00
|
|
|
return -1;
|
2013-04-25 16:05:00 +00:00
|
|
|
}
|
|
|
|
tmp += 2; /* skip ') ' */
|
|
|
|
if ((tmp - buf) >= len) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find start time in %s"),
|
|
|
|
filename);
|
2018-07-28 18:01:45 +00:00
|
|
|
return -1;
|
2013-04-25 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tokens = virStringSplit(tmp, " ", 0);
|
|
|
|
|
2016-02-09 17:18:49 +00:00
|
|
|
if (virStringListLength((const char * const *)tokens) < 20) {
|
2013-04-25 16:05:00 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find start time in %s"),
|
|
|
|
filename);
|
2018-07-28 18:01:45 +00:00
|
|
|
return -1;
|
2013-04-25 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ull(tokens[19],
|
|
|
|
NULL,
|
|
|
|
10,
|
|
|
|
timestamp) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot parse start time %s in %s"),
|
|
|
|
tokens[19], filename);
|
2018-07-28 18:01:45 +00:00
|
|
|
return -1;
|
2013-04-25 16:05:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 18:01:45 +00:00
|
|
|
return 0;
|
2013-04-25 16:05:00 +00:00
|
|
|
}
|
2015-10-15 11:50:42 +00:00
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2013-04-25 16:05:00 +00:00
|
|
|
int virProcessGetStartTime(pid_t pid,
|
|
|
|
unsigned long long *timestamp)
|
|
|
|
{
|
|
|
|
struct kinfo_proc p;
|
|
|
|
int mib[4];
|
|
|
|
size_t len = 4;
|
|
|
|
|
|
|
|
sysctlnametomib("kern.proc.pid", mib, &len);
|
|
|
|
|
|
|
|
len = sizeof(struct kinfo_proc);
|
|
|
|
mib[3] = pid;
|
|
|
|
|
|
|
|
if (sysctl(mib, 4, &p, &len, NULL, 0) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to query process ID start time"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*timestamp = (unsigned long long)p.ki_start.tv_sec;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int virProcessGetStartTime(pid_t pid,
|
|
|
|
unsigned long long *timestamp)
|
|
|
|
{
|
2014-10-28 18:38:04 +00:00
|
|
|
static int warned;
|
2020-01-09 12:54:51 +00:00
|
|
|
if (virAtomicIntInc(&warned) == 0) {
|
2016-10-06 14:54:41 +00:00
|
|
|
VIR_WARN("Process start time of pid %lld not available on this platform",
|
|
|
|
(long long) pid);
|
2013-04-25 16:05:00 +00:00
|
|
|
}
|
|
|
|
*timestamp = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-30 13:11:23 +00:00
|
|
|
|
|
|
|
|
2018-10-16 09:24:39 +00:00
|
|
|
typedef struct _virProcessNamespaceHelperData virProcessNamespaceHelperData;
|
|
|
|
struct _virProcessNamespaceHelperData {
|
|
|
|
pid_t pid;
|
|
|
|
virProcessNamespaceCallback cb;
|
|
|
|
void *opaque;
|
|
|
|
};
|
|
|
|
|
2019-10-14 12:45:33 +00:00
|
|
|
static int virProcessNamespaceHelper(pid_t pid G_GNUC_UNUSED,
|
2014-01-30 13:11:23 +00:00
|
|
|
void *opaque)
|
|
|
|
{
|
2018-10-16 09:24:39 +00:00
|
|
|
virProcessNamespaceHelperData *data = opaque;
|
2014-01-30 13:11:23 +00:00
|
|
|
int fd = -1;
|
|
|
|
int ret = -1;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2014-01-30 13:11:23 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
path = g_strdup_printf("/proc/%lld/ns/mnt", (long long)data->pid);
|
2014-01-30 13:11:23 +00:00
|
|
|
|
|
|
|
if ((fd = open(path, O_RDONLY)) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Kernel does not provide mount namespace"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setns(fd, 0) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to enter mount namespace"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-10-16 09:24:39 +00:00
|
|
|
ret = data->cb(data->pid, data->opaque);
|
2014-01-30 13:11:23 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run cb(opaque) in the mount namespace of pid. Return -1 with error
|
|
|
|
* message raised if we fail to run the child, if the child dies from
|
2014-02-19 19:39:44 +00:00
|
|
|
* a signal, or if the child has status EXIT_CANCELED; otherwise return
|
|
|
|
* the exit status of the child. The callback will be run in a child
|
|
|
|
* process so must be careful to only use async signal safe functions.
|
2014-01-30 13:11:23 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
virProcessRunInMountNamespace(pid_t pid,
|
|
|
|
virProcessNamespaceCallback cb,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2018-10-16 09:24:39 +00:00
|
|
|
virProcessNamespaceHelperData data = {.pid = pid, .cb = cb, .opaque = opaque};
|
2014-01-30 13:11:23 +00:00
|
|
|
|
2018-10-16 09:24:39 +00:00
|
|
|
return virProcessRunInFork(virProcessNamespaceHelper, &data);
|
2014-01-30 13:11:23 +00:00
|
|
|
}
|
2014-02-20 03:04:40 +00:00
|
|
|
|
|
|
|
|
2018-10-16 08:50:33 +00:00
|
|
|
static int
|
|
|
|
virProcessRunInForkHelper(int errfd,
|
|
|
|
pid_t ppid,
|
|
|
|
virProcessForkCallback cb,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
if (cb(ppid, opaque) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
if (err) {
|
|
|
|
size_t len = strlen(err->message) + 1;
|
|
|
|
ignore_value(safewrite(errfd, err->message, len));
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virProcessRunInFork:
|
|
|
|
* @cb: callback to run
|
|
|
|
* @opaque: opaque data to @cb
|
|
|
|
*
|
|
|
|
* Do the fork and run @cb in the child. This can be used when
|
|
|
|
* @cb does something thread unsafe, for instance. All signals
|
|
|
|
* will be reset to have their platform default handlers and
|
|
|
|
* unmasked. @cb must only use async signal safe functions. In
|
|
|
|
* particular no mutexes should be used in @cb, unless steps were
|
|
|
|
* taken before forking to guarantee a predictable state. @cb
|
|
|
|
* must not exec any external binaries, instead
|
|
|
|
* virCommand/virExec should be used for that purpose.
|
|
|
|
*
|
|
|
|
* On return, the returned value is either -1 with error message
|
|
|
|
* reported if something went bad in the parent, if child has
|
|
|
|
* died from a signal or if the child returned EXIT_CANCELED.
|
|
|
|
* Otherwise the returned value is the exit status of the child.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virProcessRunInFork(virProcessForkCallback cb,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
pid_t child = -1;
|
|
|
|
pid_t parent = getpid();
|
|
|
|
int errfd[2] = { -1, -1 };
|
|
|
|
|
|
|
|
if (pipe2(errfd, O_CLOEXEC) < 0) {
|
|
|
|
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 = virProcessRunInForkHelper(errfd[1], parent, cb, opaque);
|
|
|
|
VIR_FORCE_CLOSE(errfd[1]);
|
|
|
|
_exit(ret < 0 ? EXIT_CANCELED : ret);
|
|
|
|
} else {
|
|
|
|
int status;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *buf = NULL;
|
2018-10-16 08:50:33 +00:00
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(errfd[1]);
|
|
|
|
ignore_value(virFileReadHeaderFD(errfd[0], 1024, &buf));
|
|
|
|
ret = virProcessWait(child, &status, false);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = status == EXIT_CANCELED ? -1 : status;
|
|
|
|
if (ret) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("child reported (status=%d): %s"),
|
|
|
|
status, NULLSTR(buf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FORCE_CLOSE(errfd[0]);
|
|
|
|
VIR_FORCE_CLOSE(errfd[1]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-10 13:55:48 +00:00
|
|
|
#if defined(HAVE_SYS_MOUNT_H) && defined(HAVE_UNSHARE)
|
|
|
|
int
|
|
|
|
virProcessSetupPrivateMountNS(void)
|
|
|
|
{
|
|
|
|
if (unshare(CLONE_NEWNS) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Cannot unshare mount namespace"));
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2016-11-10 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 03:18:21 +00:00
|
|
|
if (mount("", "/", "none", MS_SLAVE|MS_REC, NULL) < 0) {
|
2016-11-10 13:55:48 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Failed to switch root mount into slave mode"));
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2016-11-10 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2016-11-10 13:55:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !defined(HAVE_SYS_MOUNT_H) || !defined(HAVE_UNSHARE) */
|
|
|
|
|
|
|
|
int
|
|
|
|
virProcessSetupPrivateMountNS(void)
|
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Namespaces are not supported on this platform."));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* !defined(HAVE_SYS_MOUNT_H) || !defined(HAVE_UNSHARE) */
|
|
|
|
|
2017-01-11 09:23:39 +00:00
|
|
|
#if defined(__linux__)
|
2019-10-14 12:07:39 +00:00
|
|
|
G_GNUC_NORETURN static int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessDummyChild(void *argv G_GNUC_UNUSED)
|
2017-01-11 09:23:39 +00:00
|
|
|
{
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virProcessNamespaceAvailable:
|
|
|
|
* @ns: what namespaces to check (bitwise-OR of virProcessNamespaceFlags)
|
|
|
|
*
|
|
|
|
* Check if given list of namespaces (@ns) is available.
|
|
|
|
* If not, appropriate error message is produced.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success (all the namespaces from @flags are available),
|
|
|
|
* -1 on error (with error message reported).
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virProcessNamespaceAvailable(unsigned int ns)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
int cpid;
|
|
|
|
char *childStack;
|
|
|
|
int stacksize = getpagesize() * 4;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *stack = NULL;
|
2017-01-11 09:23:39 +00:00
|
|
|
|
|
|
|
if (ns & VIR_PROCESS_NAMESPACE_MNT)
|
|
|
|
flags |= CLONE_NEWNS;
|
|
|
|
if (ns & VIR_PROCESS_NAMESPACE_IPC)
|
|
|
|
flags |= CLONE_NEWIPC;
|
|
|
|
if (ns & VIR_PROCESS_NAMESPACE_NET)
|
|
|
|
flags |= CLONE_NEWNET;
|
|
|
|
if (ns & VIR_PROCESS_NAMESPACE_PID)
|
|
|
|
flags |= CLONE_NEWPID;
|
|
|
|
if (ns & VIR_PROCESS_NAMESPACE_USER)
|
|
|
|
flags |= CLONE_NEWUSER;
|
|
|
|
if (ns & VIR_PROCESS_NAMESPACE_UTS)
|
|
|
|
flags |= CLONE_NEWUTS;
|
|
|
|
|
|
|
|
/* Signal parent as soon as the child dies. RIP. */
|
|
|
|
flags |= SIGCHLD;
|
|
|
|
|
|
|
|
if (VIR_ALLOC_N(stack, stacksize) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
childStack = stack + stacksize;
|
|
|
|
|
|
|
|
cpid = clone(virProcessDummyChild, childStack, flags, NULL);
|
2018-07-28 18:01:44 +00:00
|
|
|
|
2017-01-11 09:23:39 +00:00
|
|
|
if (cpid < 0) {
|
2019-10-14 12:45:33 +00:00
|
|
|
char ebuf[1024] G_GNUC_UNUSED;
|
2017-01-11 09:23:39 +00:00
|
|
|
VIR_DEBUG("clone call returned %s, container support is not enabled",
|
|
|
|
virStrerror(errno, ebuf, sizeof(ebuf)));
|
|
|
|
return -1;
|
|
|
|
} else if (virProcessWait(cpid, NULL, false) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("All namespaces (%x) are enabled", ns);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !defined(__linux__) */
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessNamespaceAvailable(unsigned int ns G_GNUC_UNUSED)
|
2017-01-11 09:23:39 +00:00
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Namespaces are not supported on this platform."));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* !defined(__linux__) */
|
2016-11-10 13:55:48 +00:00
|
|
|
|
2014-02-20 03:04:40 +00:00
|
|
|
/**
|
|
|
|
* virProcessExitWithStatus:
|
|
|
|
* @status: raw status to be reproduced when this process dies
|
|
|
|
*
|
|
|
|
* Given a raw status obtained by waitpid() or similar, attempt to
|
|
|
|
* make this process exit in the same manner. If the child died by
|
|
|
|
* signal, reset that signal handler to default and raise the same
|
|
|
|
* signal; if that doesn't kill this process, then exit with 128 +
|
|
|
|
* signal number. If @status can't be deciphered, use
|
|
|
|
* EXIT_CANNOT_INVOKE.
|
|
|
|
*
|
|
|
|
* Never returns.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virProcessExitWithStatus(int status)
|
|
|
|
{
|
|
|
|
int value = EXIT_CANNOT_INVOKE;
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
value = WEXITSTATUS(status);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
struct sigaction act;
|
|
|
|
sigset_t sigs;
|
|
|
|
|
|
|
|
if (sigemptyset(&sigs) == 0 &&
|
|
|
|
sigaddset(&sigs, WTERMSIG(status)) == 0)
|
|
|
|
sigprocmask(SIG_UNBLOCK, &sigs, NULL);
|
|
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
sigfillset(&act.sa_mask);
|
|
|
|
sigaction(WTERMSIG(status), &act, NULL);
|
|
|
|
raise(WTERMSIG(status));
|
|
|
|
value = 128 + WTERMSIG(status);
|
|
|
|
}
|
|
|
|
exit(value);
|
|
|
|
}
|
2015-01-08 14:36:54 +00:00
|
|
|
|
2016-11-18 07:12:12 +00:00
|
|
|
#if HAVE_SCHED_SETSCHEDULER
|
2015-01-08 14:36:54 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virProcessSchedTranslatePolicy(virProcessSchedPolicy policy)
|
|
|
|
{
|
|
|
|
switch (policy) {
|
|
|
|
case VIR_PROC_POLICY_NONE:
|
|
|
|
return SCHED_OTHER;
|
|
|
|
|
|
|
|
case VIR_PROC_POLICY_BATCH:
|
2016-11-18 07:12:12 +00:00
|
|
|
# ifdef SCHED_BATCH
|
2015-01-08 14:36:54 +00:00
|
|
|
return SCHED_BATCH;
|
2016-11-18 07:12:12 +00:00
|
|
|
# else
|
|
|
|
return -1;
|
|
|
|
# endif
|
2015-01-08 14:36:54 +00:00
|
|
|
|
|
|
|
case VIR_PROC_POLICY_IDLE:
|
2016-11-18 07:12:12 +00:00
|
|
|
# ifdef SCHED_IDLE
|
2015-01-08 14:36:54 +00:00
|
|
|
return SCHED_IDLE;
|
2016-11-18 07:12:12 +00:00
|
|
|
# else
|
|
|
|
return -1;
|
|
|
|
# endif
|
2015-01-08 14:36:54 +00:00
|
|
|
|
|
|
|
case VIR_PROC_POLICY_FIFO:
|
|
|
|
return SCHED_FIFO;
|
|
|
|
|
|
|
|
case VIR_PROC_POLICY_RR:
|
|
|
|
return SCHED_RR;
|
|
|
|
|
|
|
|
case VIR_PROC_POLICY_LAST:
|
|
|
|
/* nada */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-02-12 12:46:08 +00:00
|
|
|
virProcessSetScheduler(pid_t pid,
|
|
|
|
virProcessSchedPolicy policy,
|
|
|
|
int priority)
|
2015-01-08 14:36:54 +00:00
|
|
|
{
|
|
|
|
struct sched_param param = {0};
|
|
|
|
int pol = virProcessSchedTranslatePolicy(policy);
|
|
|
|
|
2016-11-20 20:46:21 +00:00
|
|
|
VIR_DEBUG("pid=%lld, policy=%d, priority=%u",
|
|
|
|
(long long) pid, policy, priority);
|
2015-01-08 14:36:54 +00:00
|
|
|
|
|
|
|
if (!policy)
|
|
|
|
return 0;
|
|
|
|
|
2016-11-18 07:12:12 +00:00
|
|
|
if (pol < 0) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Scheduler '%s' is not supported on this platform"),
|
|
|
|
virProcessSchedPolicyTypeToString(policy));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-08 14:36:54 +00:00
|
|
|
if (pol == SCHED_FIFO || pol == SCHED_RR) {
|
|
|
|
int min = 0;
|
|
|
|
int max = 0;
|
|
|
|
|
|
|
|
if ((min = sched_get_priority_min(pol)) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Cannot get minimum scheduler "
|
|
|
|
"priority value"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((max = sched_get_priority_max(pol)) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Cannot get maximum scheduler "
|
|
|
|
"priority value"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priority < min || priority > max) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Scheduler priority %d out of range [%d, %d]"),
|
|
|
|
priority, min, max);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
param.sched_priority = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_setscheduler(pid, pol, ¶m) < 0) {
|
|
|
|
virReportSystemError(errno,
|
2016-11-20 20:46:21 +00:00
|
|
|
_("Cannot set scheduler parameters for pid %lld"),
|
|
|
|
(long long) pid);
|
2015-01-08 14:36:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* ! HAVE_SCHED_SETSCHEDULER */
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virProcessSetScheduler(pid_t pid G_GNUC_UNUSED,
|
2015-02-12 12:46:08 +00:00
|
|
|
virProcessSchedPolicy policy,
|
2019-10-14 12:45:33 +00:00
|
|
|
int priority G_GNUC_UNUSED)
|
2015-01-08 14:36:54 +00:00
|
|
|
{
|
|
|
|
if (!policy)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Process CPU scheduling is not supported "
|
|
|
|
"on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAVE_SCHED_SETSCHEDULER */
|