Re: [libvirt] [PATCH 1/4] util: new virCommandSetMax(MemLock|Processes|Files)

2013-04-26 Thread Daniel P. Berrange
On Thu, Apr 25, 2013 at 09:44:30PM -0400, Laine Stump wrote:
 This patch adds two sets of functions:
 
 1) lower level virProcessSet*() functions that will immediately set
 the RLIMIT_MEMLOCK. RLIMIT_NPROC, or RLIMIT_NOFILE of either the
 current process (using setrlimit()) or any other process (using
 prlimit()). current process is indicated by passing a 0 for pid.
 
 2) functions for virCommand* that will setup a virCommand object to
 set those limits at a later time just after it has forked a new
 process, but before it execs the new program.
 
 configure.ac has prlimit and setrlimit added to the list of functions
 to check for, and the low level functions log an unsupported error)
 on platforms that don't support those functions.
 ---
  configure.ac |   2 +-
  src/libvirt_private.syms |   6 ++
  src/util/vircommand.c|  38 
  src/util/vircommand.h|   4 ++
  src/util/virprocess.c| 152 
 ++-
  src/util/virprocess.h|   5 +-
  6 files changed, 204 insertions(+), 3 deletions(-)

ACK


Daniel
-- 
|: http://berrange.com  -o-http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org  -o- http://virt-manager.org :|
|: http://autobuild.org   -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org   -o-   http://live.gnome.org/gtk-vnc :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH 1/4] util: new virCommandSetMax(MemLock|Processes|Files)

2013-04-25 Thread Laine Stump
This patch adds two sets of functions:

1) lower level virProcessSet*() functions that will immediately set
the RLIMIT_MEMLOCK. RLIMIT_NPROC, or RLIMIT_NOFILE of either the
current process (using setrlimit()) or any other process (using
prlimit()). current process is indicated by passing a 0 for pid.

2) functions for virCommand* that will setup a virCommand object to
set those limits at a later time just after it has forked a new
process, but before it execs the new program.

configure.ac has prlimit and setrlimit added to the list of functions
to check for, and the low level functions log an unsupported error)
on platforms that don't support those functions.
---
 configure.ac |   2 +-
 src/libvirt_private.syms |   6 ++
 src/util/vircommand.c|  38 
 src/util/vircommand.h|   4 ++
 src/util/virprocess.c| 152 ++-
 src/util/virprocess.h|   5 +-
 6 files changed, 204 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 89dae3d..23c24d2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -194,7 +194,7 @@ dnl Availability of various common functions (non-fatal if 
missing),
 dnl and various less common threadsafe functions
 AC_CHECK_FUNCS_ONCE([cfmakeraw geteuid getgid getgrnam_r getmntent_r \
   getpwuid_r getuid initgroups kill mmap newlocale posix_fallocate \
-  posix_memalign regexec sched_getaffinity setns symlink])
+  posix_memalign prlimit regexec sched_getaffinity setns setrlimit symlink])
 
 dnl Availability of pthread functions (if missing, win32 threading is
 dnl assumed).  Because of $LIB_PTHREAD, we cannot use AC_CHECK_FUNCS_ONCE.
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 2a2c40e..0bb6f5f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1185,6 +1185,9 @@ virCommandSetErrorFD;
 virCommandSetGID;
 virCommandSetInputBuffer;
 virCommandSetInputFD;
+virCommandSetMaxFiles;
+virCommandSetMaxMemLock;
+virCommandSetMaxProcesses;
 virCommandSetOutputBuffer;
 virCommandSetOutputFD;
 virCommandSetPidFile;
@@ -1668,6 +1671,9 @@ virProcessGetNamespaces;
 virProcessKill;
 virProcessKillPainfully;
 virProcessSetAffinity;
+virProcessSetMaxFiles;
+virProcessSetMaxMemLock;
+virProcessSetMaxProcesses;
 virProcessSetNamespaces;
 virProcessTranslateStatus;
 virProcessWait;
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index ac56a63..98521ec 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -107,6 +107,10 @@ struct _virCommand {
 char *pidfile;
 bool reap;
 
+unsigned long long maxMemLock;
+unsigned int maxProcesses;
+unsigned int maxFiles;
+
 uid_t uid;
 gid_t gid;
 unsigned long long capabilities;
@@ -598,6 +602,13 @@ virExec(virCommandPtr cmd)
 goto fork_error;
 }
 
+if (virProcessSetMaxMemLock(0, cmd-maxMemLock)  0)
+goto fork_error;
+if (virProcessSetMaxProcesses(0, cmd-maxProcesses)  0)
+goto fork_error;
+if (virProcessSetMaxFiles(0, cmd-maxFiles)  0)
+goto fork_error;
+
 if (cmd-hook) {
 VIR_DEBUG(Run hook %p %p, cmd-hook, cmd-opaque);
 ret = cmd-hook(cmd-opaque);
@@ -958,6 +969,33 @@ virCommandSetUID(virCommandPtr cmd, uid_t uid)
 cmd-uid = uid;
 }
 
+void
+virCommandSetMaxMemLock(virCommandPtr cmd, unsigned long long bytes)
+{
+if (!cmd || cmd-has_error)
+return;
+
+cmd-maxMemLock = bytes;
+}
+
+void
+virCommandSetMaxProcesses(virCommandPtr cmd, unsigned int procs)
+{
+if (!cmd || cmd-has_error)
+return;
+
+cmd-maxProcesses = procs;
+}
+
+void
+virCommandSetMaxFiles(virCommandPtr cmd, unsigned int files)
+{
+if (!cmd || cmd-has_error)
+return;
+
+cmd-maxFiles = files;
+}
+
 /**
  * virCommandClearCaps:
  * @cmd: the command to modify
diff --git a/src/util/vircommand.h b/src/util/vircommand.h
index 6c13795..18568fe 100644
--- a/src/util/vircommand.h
+++ b/src/util/vircommand.h
@@ -65,6 +65,10 @@ void virCommandSetGID(virCommandPtr cmd, gid_t gid);
 
 void virCommandSetUID(virCommandPtr cmd, uid_t uid);
 
+void virCommandSetMaxMemLock(virCommandPtr cmd, unsigned long long bytes);
+void virCommandSetMaxProcesses(virCommandPtr cmd, unsigned int procs);
+void virCommandSetMaxFiles(virCommandPtr cmd, unsigned int files);
+
 void virCommandClearCaps(virCommandPtr cmd);
 
 void virCommandAllowCap(virCommandPtr cmd,
diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index a492bd1..fb81805 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -1,7 +1,7 @@
 /*
  * virprocess.c: interaction with processes
  *
- * Copyright (C) 2010-2012 Red Hat, Inc.
+ * Copyright (C) 2010-2013 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -27,6 +27,10 @@
 #include signal.h
 #include errno.h
 #include sys/wait.h
+#if HAVE_SETRLIMIT
+# include sys/time.h
+# include sys/resource.h
+#endif