Signed-off-by: Praveen K Paladugu <prapal(a)linux.microsoft.com>
---
src/util/virprocess.c | 501 +++++++++++++++++++++---------------------
1 file changed, 249 insertions(+), 252 deletions(-)
diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index 7b0ad9c97b..8288e71f67 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -25,36 +25,36 @@
#include <fcntl.h>
#include <signal.h>
#ifndef WIN32
-# include <sys/wait.h>
+#include <sys/wait.h>
#endif
#include <unistd.h>
#if WITH_SYS_MOUNT_H
-# include <sys/mount.h>
+#include <sys/mount.h>
#endif
#if WITH_SETRLIMIT
-# include <sys/time.h>
-# include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/resource.h>
#endif
#if WITH_SCHED_H
-# include <sched.h>
+#include <sched.h>
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || WITH_BSD_CPU_AFFINITY
-# include <sys/param.h>
+#include <sys/param.h>
#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
-# include <sys/sysctl.h>
-# include <sys/user.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
#endif
#if WITH_BSD_CPU_AFFINITY
-# include <sys/cpuset.h>
+#include <sys/cpuset.h>
#endif
#ifdef WIN32
-# define WIN32_LEAN_AND_MEAN
-# include <windows.h>
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
#endif
#include "virprocess.h"
@@ -71,41 +71,44 @@
VIR_LOG_INIT("util.process");
#ifdef __linux__
+
/*
* Workaround older glibc. While kernel may support the setns
* syscall, the glibc wrapper might not exist. If that's the
* case, use our own.
*/
-# 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
-# endif
-
-# ifndef WITH_SETNS
-# if defined(__NR_setns)
-# include <sys/syscall.h>
-
-static inline int setns(int fd, int nstype)
+#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
+#endif
+
+#ifndef WITH_SETNS
+#if defined(__NR_setns)
+#include <sys/syscall.h>
+
+static inline int
+setns(int fd, int nstype)
{
return syscall(__NR_setns, fd, nstype);
}
-# else /* !__NR_setns */
-# error Please determine the syscall number for setns on your architecture
-# endif
-# endif
+#else /* !__NR_setns */
+#error Please determine the syscall number for setns on your architecture
+#endif
+#endif
#else /* !__linux__ */
-static inline int setns(int fd G_GNUC_UNUSED, int nstype G_GNUC_UNUSED)
+static inline int
+setns(int fd G_GNUC_UNUSED, int nstype G_GNUC_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
_("Namespaces are not supported on this platform."));
@@ -115,15 +118,11 @@ static inline int setns(int fd G_GNUC_UNUSED, int nstype
G_GNUC_UNUSED)
VIR_ENUM_IMPL(virProcessSchedPolicy,
VIR_PROC_POLICY_LAST,
- "none",
- "batch",
- "idle",
- "fifo",
- "rr",
-);
+ "none", "batch", "idle", "fifo",
"rr",);
#ifndef WIN32
+
/**
* virProcessTranslateStatus:
* @status: child exit status to translate
@@ -136,12 +135,11 @@ char *
virProcessTranslateStatus(int status)
{
char *buf;
+
if (WIFEXITED(status)) {
- buf = g_strdup_printf(_("exit status %d"),
- WEXITSTATUS(status));
+ buf = g_strdup_printf(_("exit status %d"), WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
- buf = g_strdup_printf(_("fatal signal %d"),
- WTERMSIG(status));
+ buf = g_strdup_printf(_("fatal signal %d"), WTERMSIG(status));
} else {
buf = g_strdup_printf(_("invalid value %d"), status);
}
@@ -175,8 +173,7 @@ virProcessAbort(pid_t pid)
*/
saved_errno = errno;
VIR_DEBUG("aborting child process %d", pid);
- while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
- errno == EINTR);
+ while ((ret = waitpid(pid, &status, WNOHANG)) == -1 && errno == EINTR);
if (ret == pid) {
tmp = virProcessTranslateStatus(status);
VIR_DEBUG("process has ended: %s", tmp);
@@ -185,8 +182,7 @@ virProcessAbort(pid_t pid)
VIR_DEBUG("trying SIGTERM to child process %d", pid);
kill(pid, SIGTERM);
g_usleep(10 * 1000);
- while ((ret = waitpid(pid, &status, WNOHANG)) == -1 &&
- errno == EINTR);
+ while ((ret = waitpid(pid, &status, WNOHANG)) == -1 && errno ==
EINTR);
if (ret == pid) {
tmp = virProcessTranslateStatus(status);
VIR_DEBUG("process has ended: %s", tmp);
@@ -194,8 +190,7 @@ virProcessAbort(pid_t pid)
} 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);
+ while ((ret = waitpid(pid, &status, 0)) == -1 && errno ==
EINTR);
if (ret == pid) {
tmp = virProcessTranslateStatus(status);
VIR_DEBUG("process has ended: %s", tmp);
@@ -246,8 +241,7 @@ virProcessWait(pid_t pid, int *exitstatus, bool raw)
}
/* Wait for intermediate process to exit */
- while ((ret = waitpid(pid, &status, 0)) == -1 &&
- errno == EINTR);
+ while ((ret = waitpid(pid, &status, 0)) == -1 && errno == EINTR);
if (ret == -1) {
virReportSystemError(errno, _("unable to wait for process %lld"),
@@ -289,7 +283,7 @@ void
virProcessAbort(pid_t pid)
{
/* Not yet ported to mingw. Any volunteers? */
- VIR_DEBUG("failed to reap child %lld, abandoning it", (long long)pid);
+ VIR_DEBUG("failed to reap child %lld, abandoning it", (long long) pid);
}
@@ -305,7 +299,8 @@ virProcessWait(pid_t pid, int *exitstatus G_GNUC_UNUSED, bool raw
G_GNUC_UNUSED)
/* send signal to a single process */
-int virProcessKill(pid_t pid, int sig)
+int
+virProcessKill(pid_t pid, int sig)
{
if (pid <= 1) {
errno = ESRCH;
@@ -315,44 +310,45 @@ int virProcessKill(pid_t pid, int sig)
#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 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;
- 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;
- }
+ default:
+ {
+ HANDLE proc;
- /*
- * 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);
- }
+ 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
@@ -362,7 +358,8 @@ int virProcessKill(pid_t pid, int sig)
/* send signal to a process group */
-int virProcessGroupKill(pid_t pid, int sig G_GNUC_UNUSED)
+int
+virProcessGroupKill(pid_t pid, int sig G_GNUC_UNUSED)
{
if (pid <= 1) {
errno = ESRCH;
@@ -379,7 +376,8 @@ int virProcessGroupKill(pid_t pid, int sig G_GNUC_UNUSED)
/* get process group from a pid */
-pid_t virProcessGroupGet(pid_t pid)
+pid_t
+virProcessGroupGet(pid_t pid)
{
if (pid <= 1) {
errno = ESRCH;
@@ -406,15 +404,17 @@ pid_t virProcessGroupGet(pid_t pid)
* wait longer than the default.
*/
int
-virProcessKillPainfullyDelay(pid_t pid, bool force, unsigned int extradelay, bool group)
+virProcessKillPainfullyDelay(pid_t pid, bool force, unsigned int extradelay,
+ bool group)
{
size_t i;
+
/* This is in 1/5th seconds since polling is on a 0.2s interval */
- unsigned int polldelay = (force ? 200 : 75) + (extradelay*5);
+ unsigned int polldelay = (force ? 200 : 75) + (extradelay * 5);
const char *signame = "TERM";
VIR_DEBUG("vpid=%lld force=%d extradelay=%u group=%d",
- (long long)pid, force, extradelay, group);
+ (long long) pid, force, extradelay, group);
/* This loop sends SIGTERM, then waits a few iterations (10 seconds)
* to see if it dies. If the process still hasn't exited, and
@@ -432,21 +432,21 @@ virProcessKillPainfullyDelay(pid_t pid, bool force, unsigned int
extradelay, boo
int rc;
if (i == 0) {
- signum = SIGTERM; /* kindly suggest it should exit */
+ signum = SIGTERM; /* kindly suggest it should exit */
} else if (i == 50 && force) {
VIR_DEBUG("Timed out waiting after SIGTERM to process %lld, "
- "sending SIGKILL", (long long)pid);
+ "sending SIGKILL", (long long) pid);
/* No SIGKILL kill on Win32 ! Use SIGABRT instead which our
* virProcessKill proc will handle more or less like SIGKILL */
#ifdef WIN32
- signum = SIGABRT; /* kill it after a grace period */
+ signum = SIGABRT; /* kill it after a grace period */
signame = "ABRT";
#else
- signum = SIGKILL; /* kill it after a grace period */
+ signum = SIGKILL; /* kill it after a grace period */
signame = "KILL";
#endif
} else {
- signum = 0; /* Just check for existence */
+ signum = 0; /* Just check for existence */
}
if (group)
@@ -457,8 +457,9 @@ virProcessKillPainfullyDelay(pid_t pid, bool force, unsigned int
extradelay, boo
if (rc < 0) {
if (errno != ESRCH) {
virReportSystemError(errno,
- _("Failed to terminate process %lld with
SIG%s"),
- (long long)pid, signame);
+ _
+ ("Failed to terminate process %lld with
SIG%s"),
+ (long long) pid, signame);
return -1;
}
return signum == SIGTERM ? 0 : 1;
@@ -469,20 +470,22 @@ virProcessKillPainfullyDelay(pid_t pid, bool force, unsigned int
extradelay, boo
virReportSystemError(EBUSY,
_("Failed to terminate process %lld with SIG%s"),
- (long long)pid, signame);
+ (long long) pid, signame);
return 0;
}
-int virProcessKillPainfully(pid_t pid, bool force)
+int
+virProcessKillPainfully(pid_t pid, bool force)
{
return virProcessKillPainfullyDelay(pid, force, 0, false);
}
#if WITH_DECL_CPU_SET_T
-int virProcessSetAffinity(pid_t pid, virBitmap *map, bool quiet)
+int
+virProcessSetAffinity(pid_t pid, virBitmap * map, bool quiet)
{
size_t i;
int numcpus = 1024;
@@ -490,7 +493,7 @@ int virProcessSetAffinity(pid_t pid, virBitmap *map, bool quiet)
cpu_set_t *mask;
int rv = -1;
- VIR_DEBUG("Set process affinity on %lld", (long long)pid);
+ VIR_DEBUG("Set process affinity on %lld", (long long) pid);
/* 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.
@@ -516,8 +519,7 @@ int virProcessSetAffinity(pid_t pid, virBitmap *map, bool quiet)
CPU_FREE(mask);
if (rv < 0) {
- if (errno == EINVAL &&
- numcpus < (1024 << 8)) { /* 262144 cpus ought to be enough for
anyone */
+ if (errno == EINVAL && numcpus < (1024 << 8)) { /* 262144 cpus
ought to be enough for anyone */
numcpus = numcpus << 2;
goto realloc;
}
@@ -526,7 +528,8 @@ int virProcessSetAffinity(pid_t pid, virBitmap *map, bool quiet)
pid, g_strerror(errno));
} else {
virReportSystemError(errno,
- _("cannot set CPU affinity on process %d"),
pid);
+ _("cannot set CPU affinity on process %d"),
+ pid);
return -1;
}
}
@@ -574,9 +577,8 @@ virProcessGetAffinity(pid_t pid)
#elif defined(WITH_BSD_CPU_AFFINITY)
-int virProcessSetAffinity(pid_t pid,
- virBitmap *map,
- bool quiet)
+int
+virProcessSetAffinity(pid_t pid, virBitmap * map, bool quiet)
{
size_t i;
cpuset_t mask;
@@ -594,7 +596,8 @@ int virProcessSetAffinity(pid_t pid,
pid, g_strerror(errno));
} else {
virReportSystemError(errno,
- _("cannot set CPU affinity on process %d"),
pid);
+ _("cannot set CPU affinity on process %d"),
+ pid);
return -1;
}
}
@@ -628,14 +631,15 @@ virProcessGetAffinity(pid_t pid)
#else /* WITH_DECL_CPU_SET_T */
-int virProcessSetAffinity(pid_t pid G_GNUC_UNUSED,
- virBitmap *map G_GNUC_UNUSED,
- bool quiet G_GNUC_UNUSED)
+int
+virProcessSetAffinity(pid_t pid G_GNUC_UNUSED,
+ virBitmap * map G_GNUC_UNUSED, bool quiet G_GNUC_UNUSED)
{
/* The @quiet parameter is ignored here, it is used only for silencing
* actual failures. */
virReportSystemError(ENOSYS, "%s",
- _("Process CPU affinity is not supported on this
platform"));
+ _
+ ("Process CPU affinity is not supported on this
platform"));
return -1;
}
@@ -643,15 +647,18 @@ virBitmap *
virProcessGetAffinity(pid_t pid G_GNUC_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
- _("Process CPU affinity is not supported on this
platform"));
+ _
+ ("Process CPU affinity is not supported on this
platform"));
return NULL;
}
#endif /* WITH_DECL_CPU_SET_T */
-int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
+int
+virProcessGetPids(pid_t pid, size_t *npids, pid_t ** pids)
{
int ret = -1;
+
g_autoptr(DIR) dir = NULL;
int value;
struct dirent *ent;
@@ -660,7 +667,7 @@ int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
*npids = 0;
*pids = NULL;
- taskPath = g_strdup_printf("/proc/%llu/task", (long long)pid);
+ taskPath = g_strdup_printf("/proc/%llu/task", (long long) pid);
if (virDirOpen(&dir, taskPath) < 0)
goto cleanup;
@@ -688,9 +695,8 @@ int virProcessGetPids(pid_t pid, size_t *npids, pid_t **pids)
}
-int virProcessGetNamespaces(pid_t pid,
- size_t *nfdlist,
- int **fdlist)
+int
+virProcessGetNamespaces(pid_t pid, size_t *nfdlist, int **fdlist)
{
size_t i = 0;
const char *ns[] = { "user", "ipc", "uts",
"net", "pid", "mnt" };
@@ -702,11 +708,11 @@ int virProcessGetNamespaces(pid_t pid,
int fd;
g_autofree char *nsfile = NULL;
- nsfile = g_strdup_printf("/proc/%llu/ns/%s", (long long)pid, ns[i]);
+ nsfile = g_strdup_printf("/proc/%llu/ns/%s", (long long) pid, ns[i]);
if ((fd = open(nsfile, O_RDONLY)) >= 0) {
VIR_EXPAND_N(*fdlist, *nfdlist, 1);
- (*fdlist)[(*nfdlist)-1] = fd;
+ (*fdlist)[(*nfdlist) - 1] = fd;
}
}
@@ -714,8 +720,8 @@ int virProcessGetNamespaces(pid_t pid,
}
-int virProcessSetNamespaces(size_t nfdlist,
- int *fdlist)
+int
+virProcessSetNamespaces(size_t nfdlist, int *fdlist)
{
size_t i;
@@ -733,8 +739,7 @@ int virProcessSetNamespaces(size_t nfdlist,
* 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) {
+ if (setns(fdlist[i], 0) < 0 && errno != EINVAL) {
virReportSystemError(errno, "%s",
_("Unable to join domain namespace"));
return -1;
@@ -747,8 +752,7 @@ int virProcessSetNamespaces(size_t nfdlist,
static int
virProcessPrLimit(pid_t pid,
int resource,
- const struct rlimit *new_limit,
- struct rlimit *old_limit)
+ const struct rlimit *new_limit, struct rlimit *old_limit)
{
return prlimit(pid, resource, new_limit, old_limit);
}
@@ -766,8 +770,7 @@ virProcessPrLimit(pid_t pid G_GNUC_UNUSED,
#if WITH_GETRLIMIT
static int
-virProcessGetRLimit(int resource,
- struct rlimit *old_limit)
+virProcessGetRLimit(int resource, struct rlimit *old_limit)
{
return getrlimit(resource, old_limit);
}
@@ -775,51 +778,49 @@ virProcessGetRLimit(int resource,
#if WITH_SETRLIMIT
static int
-virProcessSetRLimit(int resource,
- const struct rlimit *new_limit)
+virProcessSetRLimit(int resource, const struct rlimit *new_limit)
{
return setrlimit(resource, new_limit);
}
#endif /* WITH_SETRLIMIT */
#if WITH_GETRLIMIT
-static const char*
+static const char *
virProcessLimitResourceToLabel(int resource)
{
switch (resource) {
-# if defined(RLIMIT_MEMLOCK)
+#if defined(RLIMIT_MEMLOCK)
case RLIMIT_MEMLOCK:
return "Max locked memory";
-# endif /* defined(RLIMIT_MEMLOCK) */
+#endif /* defined(RLIMIT_MEMLOCK) */
-# if defined(RLIMIT_NPROC)
+#if defined(RLIMIT_NPROC)
case RLIMIT_NPROC:
return "Max processes";
-# endif /* defined(RLIMIT_NPROC) */
+#endif /* defined(RLIMIT_NPROC) */
-# if defined(RLIMIT_NOFILE)
+#if defined(RLIMIT_NOFILE)
case RLIMIT_NOFILE:
return "Max open files";
-# endif /* defined(RLIMIT_NOFILE) */
+#endif /* defined(RLIMIT_NOFILE) */
-# if defined(RLIMIT_CORE)
+#if defined(RLIMIT_CORE)
case RLIMIT_CORE:
return "Max core file size";
-# endif /* defined(RLIMIT_CORE) */
+#endif /* defined(RLIMIT_CORE) */
default:
return NULL;
}
}
-# if defined(__linux__)
+#if defined(__linux__)
static int
-virProcessGetLimitFromProc(pid_t pid,
- int resource,
- struct rlimit *limit)
+virProcessGetLimitFromProc(pid_t pid, int resource, struct rlimit *limit)
{
g_autofree char *procfile = NULL;
g_autofree char *buf = NULL;
+
g_auto(GStrv) lines = NULL;
const char *label;
size_t i;
@@ -829,7 +830,7 @@ virProcessGetLimitFromProc(pid_t pid,
return -1;
}
- procfile = g_strdup_printf("/proc/%lld/limits", (long long)pid);
+ procfile = g_strdup_printf("/proc/%lld/limits", (long long) pid);
if (virFileReadAllQuiet(procfile, 2048, &buf) < 0) {
/* virFileReadAllQuiet() already sets errno, so don't overwrite
@@ -873,7 +874,7 @@ virProcessGetLimitFromProc(pid_t pid,
errno = EIO;
return -1;
}
-# else /* !defined(__linux__) */
+#else /* !defined(__linux__) */
static int
virProcessGetLimitFromProc(pid_t pid G_GNUC_UNUSED,
int resource G_GNUC_UNUSED,
@@ -882,12 +883,10 @@ virProcessGetLimitFromProc(pid_t pid G_GNUC_UNUSED,
errno = ENOSYS;
return -1;
}
-# endif /* !defined(__linux__) */
+#endif /* !defined(__linux__) */
static int
-virProcessGetLimit(pid_t pid,
- int resource,
- struct rlimit *old_limit)
+virProcessGetLimit(pid_t pid, int resource, struct rlimit *old_limit)
{
pid_t current_pid = getpid();
bool same_process = (pid == current_pid);
@@ -913,9 +912,7 @@ virProcessGetLimit(pid_t pid,
#if WITH_SETRLIMIT
static int
-virProcessSetLimit(pid_t pid,
- int resource,
- const struct rlimit *new_limit)
+virProcessSetLimit(pid_t pid, int resource, const struct rlimit *new_limit)
{
pid_t current_pid = getpid();
bool same_process = (pid == current_pid);
@@ -931,6 +928,7 @@ virProcessSetLimit(pid_t pid,
#endif /* WITH_SETRLIMIT */
#if WITH_SETRLIMIT && defined(RLIMIT_MEMLOCK)
+
/**
* virProcessSetMaxMemLock:
* @pid: process to be changed
@@ -958,7 +956,7 @@ virProcessSetMaxMemLock(pid_t pid, unsigned long long bytes)
virReportSystemError(errno,
_("cannot limit locked memory "
"of process %lld to %llu"),
- (long long int)pid, bytes);
+ (long long int) pid, bytes);
}
VIR_DEBUG("Locked memory for process %lld limited to %llu bytes",
@@ -977,6 +975,7 @@ virProcessSetMaxMemLock(pid_t pid G_GNUC_UNUSED,
#endif /* ! (WITH_SETRLIMIT && defined(RLIMIT_MEMLOCK)) */
#if WITH_GETRLIMIT && defined(RLIMIT_MEMLOCK)
+
/**
* virProcessGetMaxMemLock:
* @pid: process to be queried
@@ -987,8 +986,7 @@ virProcessSetMaxMemLock(pid_t pid G_GNUC_UNUSED,
* Returns: 0 on success, <0 on failure.
*/
int
-virProcessGetMaxMemLock(pid_t pid,
- unsigned long long *bytes)
+virProcessGetMaxMemLock(pid_t pid, unsigned long long *bytes)
{
struct rlimit rlim;
@@ -998,8 +996,7 @@ virProcessGetMaxMemLock(pid_t pid,
if (virProcessGetLimit(pid, RLIMIT_MEMLOCK, &rlim) < 0) {
virReportSystemError(errno,
_("cannot get locked memory limit "
- "of process %lld"),
- (long long int) pid);
+ "of process %lld"), (long long int) pid);
return -1;
}
@@ -1026,6 +1023,7 @@ virProcessGetMaxMemLock(pid_t pid G_GNUC_UNUSED,
#endif /* ! (WITH_GETRLIMIT && defined(RLIMIT_MEMLOCK)) */
#if WITH_SETRLIMIT && defined(RLIMIT_NPROC)
+
/**
* virProcessSetMaxProcesses:
* @pid: process to be changed
@@ -1045,9 +1043,9 @@ virProcessSetMaxProcesses(pid_t pid, unsigned int procs)
if (virProcessSetLimit(pid, RLIMIT_NPROC, &rlim) < 0) {
virReportSystemError(errno,
- _("cannot limit number of subprocesses "
- "of process %lld to %u"),
- (long long int)pid, procs);
+ _("cannot limit number of subprocesses "
+ "of process %lld to %u"),
+ (long long int) pid, procs);
return -1;
}
return 0;
@@ -1063,6 +1061,7 @@ virProcessSetMaxProcesses(pid_t pid G_GNUC_UNUSED,
#endif /* ! (WITH_SETRLIMIT && defined(RLIMIT_NPROC)) */
#if WITH_SETRLIMIT && defined(RLIMIT_NOFILE)
+
/**
* virProcessSetMaxFiles:
* @pid: process to be changed
@@ -1077,21 +1076,21 @@ virProcessSetMaxFiles(pid_t pid, unsigned int files)
{
struct rlimit rlim;
- /* 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.
- */
+ /* 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 (virProcessSetLimit(pid, RLIMIT_NOFILE, &rlim) < 0) {
virReportSystemError(errno,
_("cannot limit number of open files "
"of process %lld to %u"),
- (long long int)pid, files);
+ (long long int) pid, files);
return -1;
}
@@ -1099,8 +1098,7 @@ virProcessSetMaxFiles(pid_t pid, unsigned int files)
}
#else /* ! (WITH_SETRLIMIT && defined(RLIMIT_NOFILE)) */
int
-virProcessSetMaxFiles(pid_t pid G_GNUC_UNUSED,
- unsigned int files G_GNUC_UNUSED)
+virProcessSetMaxFiles(pid_t pid G_GNUC_UNUSED, unsigned int files G_GNUC_UNUSED)
{
virReportSystemError(ENOSYS, "%s", _("Not supported on this
platform"));
return -1;
@@ -1108,6 +1106,7 @@ virProcessSetMaxFiles(pid_t pid G_GNUC_UNUSED,
#endif /* ! (WITH_SETRLIMIT && defined(RLIMIT_NOFILE)) */
#if WITH_SETRLIMIT && defined(RLIMIT_CORE)
+
/**
* virProcessSetMaxCoreSize:
* @pid: process to be changed
@@ -1126,9 +1125,9 @@ virProcessSetMaxCoreSize(pid_t pid, unsigned long long bytes)
if (virProcessSetLimit(pid, RLIMIT_CORE, &rlim) < 0) {
virReportSystemError(errno,
- _("cannot limit core file size "
- "of process %lld to %llu"),
- (long long int)pid, bytes);
+ _("cannot limit core file size "
+ "of process %lld to %llu"),
+ (long long int) pid, bytes);
return -1;
}
@@ -1146,19 +1145,20 @@ virProcessSetMaxCoreSize(pid_t pid G_GNUC_UNUSED,
#ifdef __linux__
+
/*
* Port of code from polkitunixprocess.c under terms
* of the LGPLv2+
*/
-int virProcessGetStartTime(pid_t pid,
- unsigned long long *timestamp)
+int
+virProcessGetStartTime(pid_t pid, unsigned long long *timestamp)
{
g_auto(GStrv) proc_stat = virProcessGetStat(pid, 0);
const char *starttime_str = NULL;
if (!proc_stat || g_strv_length(proc_stat) < 22) {
virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Cannot find start time for pid %d"), (int)pid);
+ _("Cannot find start time for pid %d"), (int) pid);
return -1;
}
@@ -1166,14 +1166,14 @@ int virProcessGetStartTime(pid_t pid,
if (virStrToLong_ull(starttime_str, NULL, 10, timestamp) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Cannot parse start time %s for pid %d"),
- starttime_str, (int)pid);
+ starttime_str, (int) pid);
return -1;
}
return 0;
}
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
-int virProcessGetStartTime(pid_t pid,
- unsigned long long *timestamp)
+int
+virProcessGetStartTime(pid_t pid, unsigned long long *timestamp)
{
struct kinfo_proc p;
int mib[4];
@@ -1190,19 +1190,21 @@ int virProcessGetStartTime(pid_t pid,
return -1;
}
- *timestamp = (unsigned long long)p.ki_start.tv_sec;
+ *timestamp = (unsigned long long) p.ki_start.tv_sec;
return 0;
}
#else
-int virProcessGetStartTime(pid_t pid,
- unsigned long long *timestamp)
+int
+virProcessGetStartTime(pid_t pid, unsigned long long *timestamp)
{
static int warned;
+
if (g_atomic_int_add(&warned, 1) == 0) {
- VIR_WARN("Process start time of pid %lld not available on this
platform",
- (long long) pid);
+ VIR_WARN
+ ("Process start time of pid %lld not available on this platform",
+ (long long) pid);
}
*timestamp = 0;
return 0;
@@ -1218,15 +1220,15 @@ struct _virProcessNamespaceHelperData {
void *opaque;
};
-static int virProcessNamespaceHelper(pid_t pid G_GNUC_UNUSED,
- void *opaque)
+static int
+virProcessNamespaceHelper(pid_t pid G_GNUC_UNUSED, void *opaque)
{
virProcessNamespaceHelperData *data = opaque;
int fd = -1;
int ret = -1;
g_autofree char *path = NULL;
- path = g_strdup_printf("/proc/%lld/ns/mnt", (long long)data->pid);
+ path = g_strdup_printf("/proc/%lld/ns/mnt", (long long) data->pid);
if ((fd = open(path, O_RDONLY)) < 0) {
virReportSystemError(errno, "%s",
@@ -1235,8 +1237,7 @@ static int virProcessNamespaceHelper(pid_t pid G_GNUC_UNUSED,
}
if (setns(fd, 0) < 0) {
- virReportSystemError(errno, "%s",
- _("Unable to enter mount namespace"));
+ virReportSystemError(errno, "%s", _("Unable to enter mount
namespace"));
goto cleanup;
}
@@ -1255,10 +1256,10 @@ static int virProcessNamespaceHelper(pid_t pid G_GNUC_UNUSED,
*/
int
virProcessRunInMountNamespace(pid_t pid,
- virProcessNamespaceCallback cb,
- void *opaque)
+ virProcessNamespaceCallback cb, void *opaque)
{
- virProcessNamespaceHelperData data = {.pid = pid, .cb = cb, .opaque = opaque};
+ virProcessNamespaceHelperData data = {.pid = pid, .cb = cb,
+ .opaque = opaque};
return virProcessRunInFork(virProcessNamespaceHelper, &data);
}
@@ -1279,8 +1280,9 @@ virProcessRunInMountNamespace(pid_t pid G_GNUC_UNUSED,
#ifndef WIN32
+
/* We assume that error messages will fit into 1024 chars */
-# define VIR_PROCESS_ERROR_MAX_LENGTH 1024
+#define VIR_PROCESS_ERROR_MAX_LENGTH 1024
typedef struct {
int code;
int domain;
@@ -1296,13 +1298,12 @@ typedef struct {
typedef union {
errorData data;
char bindata[sizeof(errorData)];
-} errorDataBin;
+}
+errorDataBin;
static int
virProcessRunInForkHelper(int errfd,
- pid_t ppid,
- virProcessForkCallback cb,
- void *opaque)
+ pid_t ppid, virProcessForkCallback cb, void *opaque)
{
int ret = 0;
@@ -1355,8 +1356,7 @@ virProcessRunInForkHelper(int errfd,
* Otherwise the returned value is the retval of the callback.
*/
int
-virProcessRunInFork(virProcessForkCallback cb,
- void *opaque)
+virProcessRunInFork(virProcessForkCallback cb, void *opaque)
{
int ret = -1;
pid_t child = -1;
@@ -1402,8 +1402,7 @@ virProcessRunInFork(virProcessForkCallback cb,
bin->data.str2,
bin->data.str3,
bin->data.int1,
- bin->data.int2,
- "%s", bin->data.message);
+ bin->data.int2, "%s",
bin->data.message);
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("child didn't write error
(status=%d)"),
@@ -1426,7 +1425,8 @@ virProcessRunInFork(virProcessForkCallback cb G_GNUC_UNUSED,
void *opaque G_GNUC_UNUSED)
{
virReportSystemError(ENOSYS, "%s",
- _("Process spawning is not supported on this
platform"));
+ _
+ ("Process spawning is not supported on this
platform"));
return -1;
}
@@ -1438,14 +1438,14 @@ int
virProcessSetupPrivateMountNS(void)
{
if (unshare(CLONE_NEWNS) < 0) {
- virReportSystemError(errno, "%s",
- _("Cannot unshare mount namespace"));
+ virReportSystemError(errno, "%s", _("Cannot unshare mount
namespace"));
return -1;
}
- if (mount("", "/", "none", MS_SLAVE|MS_REC, NULL) <
0) {
+ if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)
< 0) {
virReportSystemError(errno, "%s",
- _("Failed disable mount propagation out of the root
filesystem"));
+ _
+ ("Failed disable mount propagation out of the root
filesystem"));
return -1;
}
@@ -1558,8 +1558,7 @@ virProcessExitWithStatus(int status)
struct sigaction act;
sigset_t sigs;
- if (sigemptyset(&sigs) == 0 &&
- sigaddset(&sigs, WTERMSIG(status)) == 0)
+ if (sigemptyset(&sigs) == 0 && sigaddset(&sigs, WTERMSIG(status))
== 0)
sigprocmask(SIG_UNBLOCK, &sigs, NULL);
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_DFL;
@@ -1569,7 +1568,7 @@ virProcessExitWithStatus(int status)
value = 128 + WTERMSIG(status);
}
#else /* WIN32 */
- (void)status;
+ (void) status;
#endif /* WIN32 */
exit(value);
}
@@ -1580,43 +1579,41 @@ static int
virProcessSchedTranslatePolicy(virProcessSchedPolicy policy)
{
switch (policy) {
- case VIR_PROC_POLICY_NONE:
- return SCHED_OTHER;
+ case VIR_PROC_POLICY_NONE:
+ return SCHED_OTHER;
- case VIR_PROC_POLICY_BATCH:
-# ifdef SCHED_BATCH
- return SCHED_BATCH;
-# else
- return -1;
-# endif
+ case VIR_PROC_POLICY_BATCH:
+#ifdef SCHED_BATCH
+ return SCHED_BATCH;
+#else
+ return -1;
+#endif
- case VIR_PROC_POLICY_IDLE:
-# ifdef SCHED_IDLE
- return SCHED_IDLE;
-# else
- return -1;
-# endif
+ case VIR_PROC_POLICY_IDLE:
+#ifdef SCHED_IDLE
+ return SCHED_IDLE;
+#else
+ return -1;
+#endif
- case VIR_PROC_POLICY_FIFO:
- return SCHED_FIFO;
+ case VIR_PROC_POLICY_FIFO:
+ return SCHED_FIFO;
- case VIR_PROC_POLICY_RR:
- return SCHED_RR;
+ case VIR_PROC_POLICY_RR:
+ return SCHED_RR;
- case VIR_PROC_POLICY_LAST:
- /* nada */
- break;
+ case VIR_PROC_POLICY_LAST:
+ /* nada */
+ break;
}
return -1;
}
int
-virProcessSetScheduler(pid_t pid,
- virProcessSchedPolicy policy,
- int priority)
+virProcessSetScheduler(pid_t pid, virProcessSchedPolicy policy, int priority)
{
- struct sched_param param = {0};
+ struct sched_param param = { 0 };
int pol = virProcessSchedTranslatePolicy(policy);
VIR_DEBUG("pid=%lld, policy=%d, priority=%u",
@@ -1674,8 +1671,7 @@ virProcessSetScheduler(pid_t pid,
int
virProcessSetScheduler(pid_t pid G_GNUC_UNUSED,
- virProcessSchedPolicy policy,
- int priority G_GNUC_UNUSED)
+ virProcessSchedPolicy policy, int priority G_GNUC_UNUSED)
{
if (!policy)
return 0;
@@ -1697,10 +1693,9 @@ virProcessSetScheduler(pid_t pid G_GNUC_UNUSED,
* and return them as array of strings.
*/
GStrv
-virProcessGetStat(pid_t pid,
- pid_t tid)
+virProcessGetStat(pid_t pid, pid_t tid)
{
- int len = 10 * 1024; /* 10kB ought to be enough for everyone */
+ int len = 10 * 1024; /* 10kB ought to be enough for everyone */
g_autofree char *buf = NULL;
g_autofree char *path = NULL;
GStrv rest = NULL;
@@ -1711,12 +1706,13 @@ virProcessGetStat(pid_t pid,
if (pid) {
if (tid)
- path = g_strdup_printf("/proc/%d/task/%d/stat", (int)pid,
(int)tid);
+ path =
+ g_strdup_printf("/proc/%d/task/%d/stat", (int) pid, (int)
tid);
else
- path = g_strdup_printf("/proc/%d/stat", (int)pid);
+ path = g_strdup_printf("/proc/%d/stat", (int) pid);
} else {
if (tid)
- path = g_strdup_printf("/proc/self/task/%d/stat", (int)tid);
+ path = g_strdup_printf("/proc/self/task/%d/stat", (int) tid);
else
path = g_strdup("/proc/self/stat");
}
@@ -1727,7 +1723,7 @@ virProcessGetStat(pid_t pid,
/* eliminate trailing spaces */
while (len > 0 && g_ascii_isspace(buf[--len]))
- buf[len] = '\0';
+ buf[len] = '\0';
/* Find end of the first field */
if (!(comm = strchr(buf, ' ')))
@@ -1755,6 +1751,7 @@ virProcessGetStat(pid_t pid,
rest = g_strsplit(rparen + 2, " ", 0);
nrest = g_strv_length(rest);
ret = g_new0(char *, nrest + 3);
+
ret[0] = g_strdup(buf);
ret[1] = g_strdup(comm);
memcpy(ret + 2, rest, nrest * sizeof(char *));
--
2.27.0