From 90b12bd71bb9fc79a4640b9112c13ef529d0196a Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 5 Dec 2014 22:26:11 +0000 Subject: Initial commit --- src/daemontools-extras/s6-softlimit.c | 117 ++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/daemontools-extras/s6-softlimit.c (limited to 'src/daemontools-extras/s6-softlimit.c') 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 +#include +#include +#include +#include +#include +#include +#include + +#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]) ; +} -- cgit v1.2.3