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>
|
|
|
|
#include <errno.h>
|
2014-02-20 03:04:40 +00:00
|
|
|
#include <stdlib.h>
|
2012-09-24 17:10:37 +00:00
|
|
|
#include <sys/wait.h>
|
2014-09-10 09:47:19 +00:00
|
|
|
#include <unistd.h>
|
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
|
|
|
|
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 defined(__FreeBSD__) || 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
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
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__ */
|
2014-09-11 12:51:48 +00:00
|
|
|
static inline int setns(int fd ATTRIBUTE_UNUSED, int nstype ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-01-08 14:36:54 +00:00
|
|
|
VIR_ENUM_IMPL(virProcessSchedPolicy, VIR_PROC_POLICY_LAST,
|
|
|
|
"none",
|
|
|
|
"batch",
|
|
|
|
"idle",
|
|
|
|
"fifo",
|
|
|
|
"rr");
|
|
|
|
|
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)) {
|
2014-09-09 15:09:58 +00:00
|
|
|
ignore_value(virAsprintfQuiet(&buf, _("exit status %d"),
|
|
|
|
WEXITSTATUS(status)));
|
2012-09-24 17:10:37 +00:00
|
|
|
} else if (WIFSIGNALED(status)) {
|
2014-09-09 15:09:58 +00:00
|
|
|
ignore_value(virAsprintfQuiet(&buf, _("fatal signal %d"),
|
|
|
|
WTERMSIG(status)));
|
2012-09-24 17:10:37 +00:00
|
|
|
} else {
|
2014-09-09 15:09:58 +00:00
|
|
|
ignore_value(virAsprintfQuiet(&buf, _("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;
|
|
|
|
char *tmp = NULL;
|
|
|
|
|
|
|
|
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);
|
|
|
|
usleep(10 * 1000);
|
|
|
|
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
|
|
|
VIR_FREE(tmp);
|
|
|
|
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;
|
|
|
|
|
|
|
|
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:
|
2014-02-20 03:23:44 +00:00
|
|
|
{
|
|
|
|
char *st = virProcessTranslateStatus(status);
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Child process (%lld) unexpected %s"),
|
|
|
|
(long long) pid, NULLSTR(st));
|
|
|
|
VIR_FREE(st);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
virProcessKillPainfully(pid_t pid, bool force)
|
|
|
|
{
|
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;
|
|
|
|
int ret = -1;
|
2012-09-26 14:42:58 +00:00
|
|
|
const char *signame = "TERM";
|
|
|
|
|
2012-10-31 17:00:50 +00:00
|
|
|
VIR_DEBUG("vpid=%lld force=%d", (long long)pid, force);
|
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
|
2015-03-19 15:53:00 +00:00
|
|
|
* wait up to 5 seconds more for the process to exit before
|
2012-09-26 14:42:58 +00:00
|
|
|
* returning.
|
|
|
|
*
|
|
|
|
* Note that setting @force could result in dataloss for the process.
|
|
|
|
*/
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < 75; 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);
|
2012-09-26 14:42:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
ret = signum == SIGTERM ? 0 : 1;
|
|
|
|
goto cleanup; /* process is dead */
|
|
|
|
}
|
|
|
|
|
|
|
|
usleep(200 * 1000);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-09-26 14:42:58 +00:00
|
|
|
return ret;
|
|
|
|
}
|
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;
|
2014-12-18 16:34:39 +00:00
|
|
|
VIR_DEBUG("Set process affinity on %lld\n", (long long)pid);
|
2012-12-13 14:58:41 +00:00
|
|
|
# ifdef CPU_ALLOC
|
|
|
|
/* New method dynamically allocates cpu mask, allowing unlimted cpus */
|
|
|
|
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);
|
|
|
|
# else
|
|
|
|
/* Legacy method uses a fixed size cpu mask, only allows up to 1024 cpus */
|
|
|
|
cpu_set_t mask;
|
|
|
|
|
|
|
|
CPU_ZERO(&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(i, &mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sched_setaffinity(pid, sizeof(mask), &mask) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot set CPU affinity on process %d"), pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
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
|
|
|
# ifdef CPU_ALLOC
|
|
|
|
/* 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
|
|
|
# else
|
2015-06-04 12:13:15 +00:00
|
|
|
ncpus = 1024;
|
2015-06-02 06:34:50 +00:00
|
|
|
if (VIR_ALLOC(mask) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
masklen = sizeof(*mask);
|
|
|
|
CPU_ZERO(mask);
|
|
|
|
# endif
|
|
|
|
|
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++) {
|
2015-06-02 06:34:50 +00:00
|
|
|
# ifdef CPU_ALLOC
|
2015-06-04 12:13:15 +00:00
|
|
|
/* 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
|
|
|
# else
|
2015-06-02 06:34:50 +00:00
|
|
|
if (CPU_ISSET(i, mask))
|
|
|
|
ignore_value(virBitmapSetBit(ret, i));
|
|
|
|
# endif
|
2012-12-13 14:58:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
cleanup:
|
|
|
|
# ifdef CPU_ALLOC
|
|
|
|
CPU_FREE(mask);
|
|
|
|
# else
|
|
|
|
VIR_FREE(mask);
|
2012-12-13 14:58:41 +00:00
|
|
|
# endif
|
|
|
|
|
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;
|
|
|
|
bool set = false;
|
|
|
|
|
|
|
|
CPU_ZERO(&mask);
|
|
|
|
for (i = 0; i < virBitmapSize(map); i++) {
|
|
|
|
if (virBitmapGetBit(map, i, &set) < 0)
|
|
|
|
return -1;
|
|
|
|
if (set)
|
|
|
|
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 */
|
|
|
|
|
|
|
|
int virProcessSetAffinity(pid_t pid ATTRIBUTE_UNUSED,
|
|
|
|
virBitmapPtr map ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Process CPU affinity is not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-02 06:34:50 +00:00
|
|
|
virBitmapPtr
|
|
|
|
virProcessGetAffinity(pid_t pid ATTRIBUTE_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;
|
|
|
|
char *taskPath = NULL;
|
|
|
|
DIR *dir = NULL;
|
|
|
|
int value;
|
|
|
|
struct dirent *ent;
|
|
|
|
|
|
|
|
*npids = 0;
|
|
|
|
*pids = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&taskPath, "/proc/%llu/task",
|
|
|
|
(unsigned long long)pid) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(dir = opendir(taskPath)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
while ((value = virDirRead(dir, &ent, taskPath)) > 0) {
|
|
|
|
pid_t tmp_pid;
|
|
|
|
|
|
|
|
/* Skip . and .. */
|
|
|
|
if (STRPREFIX(ent->d_name, "."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (virStrToLong_i(ent->d_name, NULL, 10, &tmp_pid) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (VIR_APPEND_ELEMENT(*pids, *npids, tmp_pid) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2015-06-16 13:15:10 +00:00
|
|
|
if (dir)
|
2015-06-01 12:31:44 +00:00
|
|
|
closedir(dir);
|
|
|
|
VIR_FREE(taskPath);
|
|
|
|
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;
|
|
|
|
char *nsfile = NULL;
|
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;
|
|
|
|
|
2013-06-07 16:30:09 +00:00
|
|
|
for (i = 0; i < ARRAY_CARDINALITY(ns); i++) {
|
2012-12-21 16:50:18 +00:00
|
|
|
int fd;
|
|
|
|
|
2013-06-07 16:30:09 +00:00
|
|
|
if (virAsprintf(&nsfile, "/proc/%llu/ns/%s",
|
|
|
|
(unsigned long long)pid,
|
2013-07-04 10:17:18 +00:00
|
|
|
ns[i]) < 0)
|
2012-12-21 16:50:18 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
VIR_FREE(nsfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-12-21 16:50:18 +00:00
|
|
|
VIR_FREE(nsfile);
|
|
|
|
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",
|
|
|
|
_("Expected at least one file descriptor"));
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-21 07:58:16 +00:00
|
|
|
for (i = 0; i < nfdlist; i++) {
|
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
|
|
|
|
virProcessPrLimit(pid_t pid, int resource, struct rlimit *rlim)
|
|
|
|
{
|
|
|
|
return prlimit(pid, resource, rlim, NULL);
|
|
|
|
}
|
2013-05-02 21:46:19 +00:00
|
|
|
#elif HAVE_SETRLIMIT
|
2013-04-25 16:10:10 +00:00
|
|
|
static int
|
|
|
|
virProcessPrLimit(pid_t pid ATTRIBUTE_UNUSED,
|
|
|
|
int resource ATTRIBUTE_UNUSED,
|
|
|
|
struct rlimit *rlim ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = bytes;
|
|
|
|
if (pid == 0) {
|
|
|
|
if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit locked memory to %llu"),
|
|
|
|
bytes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (virProcessPrLimit(pid, RLIMIT_MEMLOCK, &rlim) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("cannot limit locked memory "
|
|
|
|
"of process %lld to %llu"),
|
|
|
|
(long long int)pid, bytes);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else /* ! (HAVE_SETRLIMIT && defined(RLIMIT_MEMLOCK)) */
|
|
|
|
int
|
|
|
|
virProcessSetMaxMemLock(pid_t pid ATTRIBUTE_UNUSED, unsigned long long bytes)
|
|
|
|
{
|
|
|
|
if (bytes == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s", _("Not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif /* ! (HAVE_SETRLIMIT && defined(RLIMIT_MEMLOCK)) */
|
|
|
|
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
if (virProcessPrLimit(pid, RLIMIT_NPROC, &rlim) < 0) {
|
|
|
|
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
|
|
|
|
virProcessSetMaxProcesses(pid_t pid ATTRIBUTE_UNUSED, unsigned int procs)
|
|
|
|
{
|
|
|
|
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 {
|
|
|
|
if (virProcessPrLimit(pid, RLIMIT_NOFILE, &rlim) < 0) {
|
|
|
|
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
|
|
|
|
virProcessSetMaxFiles(pid_t pid ATTRIBUTE_UNUSED, unsigned int files)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
/*
|
|
|
|
* Port of code from polkitunixprocess.c under terms
|
|
|
|
* of the LGPLv2+
|
|
|
|
*/
|
|
|
|
int virProcessGetStartTime(pid_t pid,
|
|
|
|
unsigned long long *timestamp)
|
|
|
|
{
|
|
|
|
char *filename = NULL;
|
|
|
|
char *buf = NULL;
|
|
|
|
char *tmp;
|
|
|
|
int ret = -1;
|
|
|
|
int len;
|
|
|
|
char **tokens = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&filename, "/proc/%llu/stat",
|
2013-07-04 10:17:18 +00:00
|
|
|
(unsigned long long)pid) < 0)
|
2013-04-25 16:05:00 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((len = virFileReadAll(filename, 1024, &buf)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
tmp += 2; /* skip ') ' */
|
|
|
|
if ((tmp - buf) >= len) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find start time in %s"),
|
|
|
|
filename);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = virStringSplit(tmp, " ", 0);
|
|
|
|
|
|
|
|
if (virStringListLength(tokens) < 20) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find start time in %s"),
|
|
|
|
filename);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ull(tokens[19],
|
|
|
|
NULL,
|
|
|
|
10,
|
|
|
|
timestamp) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot parse start time %s in %s"),
|
|
|
|
tokens[19], filename);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-04-25 16:05:00 +00:00
|
|
|
virStringFreeList(tokens);
|
|
|
|
VIR_FREE(filename);
|
|
|
|
VIR_FREE(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
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;
|
2013-04-25 16:05:00 +00:00
|
|
|
if (virAtomicIntInc(&warned) == 1) {
|
|
|
|
VIR_WARN("Process start time of pid %llu not available on this platform",
|
|
|
|
(unsigned long long)pid);
|
|
|
|
}
|
|
|
|
*timestamp = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-01-30 13:11:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int virProcessNamespaceHelper(int errfd,
|
|
|
|
pid_t pid,
|
|
|
|
virProcessNamespaceCallback cb,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
int fd = -1;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (virAsprintf(&path, "/proc/%llu/ns/mnt", (unsigned long long)pid) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = cb(pid, opaque);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (ret < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
if (err) {
|
|
|
|
size_t len = strlen(err->message) + 1;
|
|
|
|
ignore_value(safewrite(errfd, err->message, len));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VIR_FREE(path);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
pid_t child = -1;
|
|
|
|
int errfd[2] = { -1, -1 };
|
|
|
|
|
2014-02-19 19:39:44 +00:00
|
|
|
if (pipe2(errfd, O_CLOEXEC) < 0) {
|
2014-01-30 13:11:23 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Cannot create pipe for child"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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 ((child = virFork()) < 0)
|
2014-01-30 13:11:23 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (child == 0) {
|
|
|
|
VIR_FORCE_CLOSE(errfd[0]);
|
|
|
|
ret = virProcessNamespaceHelper(errfd[1], pid,
|
|
|
|
cb, opaque);
|
|
|
|
VIR_FORCE_CLOSE(errfd[1]);
|
2014-02-19 19:39:44 +00:00
|
|
|
_exit(ret < 0 ? EXIT_CANCELED : ret);
|
2014-01-30 13:11:23 +00:00
|
|
|
} else {
|
|
|
|
char *buf = NULL;
|
2014-02-19 19:39:44 +00:00
|
|
|
int status;
|
2014-01-30 13:11:23 +00:00
|
|
|
|
2014-02-19 19:39:44 +00:00
|
|
|
VIR_FORCE_CLOSE(errfd[1]);
|
2014-01-30 13:11:23 +00:00
|
|
|
ignore_value(virFileReadHeaderFD(errfd[0], 1024, &buf));
|
2014-02-20 03:23:44 +00:00
|
|
|
ret = virProcessWait(child, &status, false);
|
2014-02-19 19:39:44 +00:00
|
|
|
if (!ret)
|
|
|
|
ret = status == EXIT_CANCELED ? -1 : status;
|
2014-01-30 13:11:23 +00:00
|
|
|
VIR_FREE(buf);
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2014-01-30 13:11:23 +00:00
|
|
|
VIR_FORCE_CLOSE(errfd[0]);
|
|
|
|
VIR_FORCE_CLOSE(errfd[1]);
|
|
|
|
return ret;
|
|
|
|
}
|
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
|
|
|
|
2015-02-12 12:46:08 +00:00
|
|
|
#if HAVE_SCHED_SETSCHEDULER && defined(SCHED_BATCH) && defined(SCHED_IDLE)
|
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:
|
|
|
|
return SCHED_BATCH;
|
|
|
|
|
|
|
|
case VIR_PROC_POLICY_IDLE:
|
|
|
|
return SCHED_IDLE;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
VIR_DEBUG("pid=%d, policy=%d, priority=%u", pid, policy, priority);
|
|
|
|
|
|
|
|
if (!policy)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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,
|
|
|
|
_("Cannot set scheduler parameters for pid %d"),
|
|
|
|
pid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* ! HAVE_SCHED_SETSCHEDULER */
|
|
|
|
|
|
|
|
int
|
|
|
|
virProcessSetScheduler(pid_t pid ATTRIBUTE_UNUSED,
|
2015-02-12 12:46:08 +00:00
|
|
|
virProcessSchedPolicy policy,
|
2015-01-08 14:36:54 +00:00
|
|
|
int priority ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
if (!policy)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("Process CPU scheduling is not supported "
|
|
|
|
"on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAVE_SCHED_SETSCHEDULER */
|