On Thu, Apr 25, 2013 at 01:57:56PM -0400, Laine Stump wrote:
diff --git a/src/util/virutil.c b/src/util/virutil.c
index b9de33c..058a069 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -38,6 +38,10 @@
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
+#if HAVE_SETRLIMIT
+# include <sys/time.h>
+# include <sys/resource.h>
+#endif
#if HAVE_MMAP
# include <sys/mman.h>
#endif
@@ -2992,6 +2996,148 @@ virGetGroupName(gid_t gid ATTRIBUTE_UNUSED)
}
#endif /* HAVE_GETPWUID_R */
+#if HAVE_SETRLIMIT
+
+# if HAVE_PRLIMIT
+static int
+virPrLimit(pid_t pid, int resource, struct rlimit *rlim)
+{
+ return prlimit(pid, resource, rlim, NULL);
+}
+# else
+static int
+virPrLimit(pid_t pid ATTRIBUTE_UNUSED,
+ int resource ATTRIBUTE_UNUSED,
+ struct rlimit *rlim ATTRIBUTE_UNUSED)
+{
+ virReportSystemError(ENOSYS, "%s", _("Not supported on this
platform"));
+ return -1;
+}
+# endif
+
+int
+virSetMaxMemLock(pid_t pid, unsigned long long bytes)
+{
+ struct rlimit rlim;
+
+ if (bytes == 0)
+ return 0;
+
+ rlim.rlim_cur = rlim.rlim_max = bytes;
+ if (pid == (pid_t)-1) {
+ if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
+ virReportSystemError(errno,
+ _("cannot limit locked memory to %llu"),
+ bytes);
+ return -1;
+ }
+ } else {
+ if (virPrLimit(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;
+}
+
+int
+virSetMaxProcesses(pid_t pid, unsigned int procs)
+{
+ struct rlimit rlim;
+
+ if (procs == 0)
+ return 0;
+
+ rlim.rlim_cur = rlim.rlim_max = procs;
+ if (pid == (pid_t)-1) {
+ if (setrlimit(RLIMIT_NPROC, &rlim) < 0) {
+ virReportSystemError(errno,
+ _("cannot limit number of subprocesses to
%u"),
+ procs);
+ return -1;
+ }
+ } else {
+ if (virPrLimit(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;
+}
+
+int
+virSetMaxFiles(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 == (pid_t)-1) {
+ if (setrlimit(RLIMIT_NOFILE, &rlim) < 0) {
+ virReportSystemError(errno,
+ _("cannot limit number of open files to
%u"),
+ files);
+ return -1;
+ }
+ } else {
+ if (virPrLimit(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
+int
+virSetMaxMemLock(pid_t pid ATTRIBUTE_UNUSED, unsigned long long bytes)
+{
+ if (bytes == 0)
+ return 0;
+
+ virReportSystemError(ENOSYS, "%s", _("Not supported on this
platform"));
+ return -1;
+}
+
+int
+virSetMaxProcesses(pid_t pid ATTRIBUTE_UNUSED, unsigned int procs)
+{
+ if (procs == 0)
+ return 0;
+
+ virReportSystemError(ENOSYS, "%s", _("Not supported on this
platform"));
+ return -1;
+}
+
+int
+virSetMaxFiles(pid_t pid ATTRIBUTE_UNUSED, unsigned int files)
+{
+ if (files == 0)
+ return 0;
+
+ virReportSystemError(ENOSYS, "%s", _("Not supported on this
platform"));
+ return -1;
+}
+#endif
Since these functions all take pid_t as their first arg,
they should all be named virProcessXXXXX and be located
in virprocess.{c,h} rather than virutil.{c,h}
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 :|