summaryrefslogtreecommitdiff
path: root/src/daemontools-extras/s6-softlimit.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-12-05 22:26:11 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-12-05 22:26:11 +0000
commit90b12bd71bb9fc79a4640b9112c13ef529d0196a (patch)
tree523b3f4ee2969e7a729bab2ba749c4b924ae62af /src/daemontools-extras/s6-softlimit.c
downloads6-90b12bd71bb9fc79a4640b9112c13ef529d0196a.tar.xz
Initial commit
Diffstat (limited to 'src/daemontools-extras/s6-softlimit.c')
-rw-r--r--src/daemontools-extras/s6-softlimit.c117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/daemontools-extras/s6-softlimit.c b/src/daemontools-extras/s6-softlimit.c
new file mode 100644
index 0000000..61e0e4d
--- /dev/null
+++ b/src/daemontools-extras/s6-softlimit.c
@@ -0,0 +1,117 @@
+/* ISC license. */
+
+#include <sys/types.h>
+#include <time.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <skalibs/strerr2.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/uint64.h>
+#include <skalibs/djbunix.h>
+
+#define USAGE "s6-softlimit [ -a allbytes ] [ -c corebytes ] [ -d databytes ] [ -f filebytes ] [ -l lockbytes ] [ -m membytes ] [ -o openfiles ] [ -p processes ] [ -r residentbytes ] [ -s stackbytes ] [ -t cpusecs ] prog..."
+
+static void doit (int res, char const *arg)
+{
+ struct rlimit r ;
+ if (getrlimit(res, &r) < 0) strerr_diefu1sys(111, "getrlimit") ;
+ if ((arg[0] == '=') && !arg[1]) r.rlim_cur = r.rlim_max ;
+ else
+ {
+ uint64 n ;
+ if (!uint640_scan(arg, &n)) strerr_dieusage(100, USAGE) ;
+ if (n > (uint64)r.rlim_max) n = (uint64)r.rlim_max ;
+ r.rlim_cur = (rlim_t)n ;
+ }
+ if (setrlimit(res, &r) == -1) strerr_diefu1sys(111, "setrlimit") ;
+}
+
+int main (int argc, char const *const *argv, char const *const *envp)
+{
+ PROG = "s6-softlimit" ;
+ for (;;)
+ {
+ register int opt = sgetopt(argc, argv, "a:c:d:f:l:m:o:p:r:s:t:") ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'a' :
+#ifdef RLIMIT_AS
+ doit(RLIMIT_AS, subgetopt_here.arg) ;
+#endif
+#ifdef RLIMIT_VMEM
+ doit(RLIMIT_VMEM, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'c' :
+#ifdef RLIMIT_CORE
+ doit(RLIMIT_CORE, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'd' :
+#ifdef RLIMIT_DATA
+ doit(RLIMIT_DATA, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'f' :
+#ifdef RLIMIT_FSIZE
+ doit(RLIMIT_FSIZE, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'l' :
+#ifdef RLIMIT_MEMLOCK
+ doit(RLIMIT_MEMLOCK, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'm' :
+#ifdef RLIMIT_DATA
+ doit(RLIMIT_DATA, subgetopt_here.arg) ;
+#endif
+#ifdef RLIMIT_STACK
+ doit(RLIMIT_STACK, subgetopt_here.arg) ;
+#endif
+#ifdef RLIMIT_MEMLOCK
+ doit(RLIMIT_MEMLOCK, subgetopt_here.arg) ;
+#endif
+#ifdef RLIMIT_VMEM
+ doit(RLIMIT_VMEM, subgetopt_here.arg) ;
+#endif
+#ifdef RLIMIT_AS
+ doit(RLIMIT_AS, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'o' :
+#ifdef RLIMIT_NOFILE
+ doit(RLIMIT_NOFILE, subgetopt_here.arg) ;
+#endif
+#ifdef RLIMIT_OFILE
+ doit(RLIMIT_OFILE, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'p' :
+#ifdef RLIMIT_NPROC
+ doit(RLIMIT_NPROC, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 'r' :
+#ifdef RLIMIT_RSS
+ doit(RLIMIT_RSS, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 's' :
+#ifdef RLIMIT_STACK
+ doit(RLIMIT_STACK, subgetopt_here.arg) ;
+#endif
+ break ;
+ case 't' :
+#ifdef RLIMIT_CPU
+ doit(RLIMIT_CPU, subgetopt_here.arg) ;
+#endif
+ break ;
+ }
+ }
+ argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ;
+ if (!argc) strerr_dieusage(100, USAGE) ;
+ pathexec_run(argv[0], argv, envp) ;
+ strerr_dieexec(111, argv[0]) ;
+}