From 6cd3ad1d11f4e45979813abd5f9b3ea4ec7ade6d Mon Sep 17 00:00:00 2001 From: jtc Date: Fri, 4 Nov 1994 17:13:27 +0000 Subject: [PATCH] ulimit builtin (PR #388) This public domain code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with ash. --- bin/sh/Makefile | 4 +- bin/sh/builtins.def | 3 +- bin/sh/ulimit.c | 140 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 bin/sh/ulimit.c diff --git a/bin/sh/Makefile b/bin/sh/Makefile index 9aca32319cfd..5e44b1941559 100644 --- a/bin/sh/Makefile +++ b/bin/sh/Makefile @@ -1,11 +1,11 @@ # from: @(#)Makefile 8.1 (Berkeley) 6/8/93 -# $Id: Makefile,v 1.17 1994/06/24 07:33:12 jtc Exp $ +# $Id: Makefile,v 1.18 1994/11/04 17:13:27 jtc Exp $ PROG= sh SRCS= alias.c builtins.c cd.c echo.c error.c eval.c exec.c expand.c \ histedit.c input.c jobs.c mail.c main.c memalloc.c miscbltin.c \ mystring.c nodes.c options.c parser.c redir.c show.c \ - syntax.c trap.c output.c var.c + syntax.c trap.c output.c ulimit.c var.c OBJS+= init.o arith.o arith_lex.o LDADD+= -ll -ledit -ltermcap DPADD+= ${LIBL} ${LIBEDIT} ${LIBTERMCAP} diff --git a/bin/sh/builtins.def b/bin/sh/builtins.def index 430956f39db0..f3509eb1cebb 100644 --- a/bin/sh/builtins.def +++ b/bin/sh/builtins.def @@ -35,7 +35,7 @@ # SUCH DAMAGE. # # from: @(#)builtins 8.1 (Berkeley) 5/31/93 -# $Id: builtins.def,v 1.9 1994/06/11 16:11:43 mycroft Exp $ +# $Id: builtins.def,v 1.10 1994/11/04 17:13:30 jtc Exp $ # # This file lists all the builtin commands. The first column is the name @@ -92,3 +92,4 @@ unsetcmd unset waitcmd wait #foocmd foo aliascmd alias +ulimitcmd ulimit diff --git a/bin/sh/ulimit.c b/bin/sh/ulimit.c new file mode 100644 index 000000000000..3bbc445baef7 --- /dev/null +++ b/bin/sh/ulimit.c @@ -0,0 +1,140 @@ +/* + * ulimit builtin + * + * This code, originally by Doug Gwyn, Doug Kingston, Eric Gisin, and + * Michael Rendell was ripped from pdksh 5.0.8 and hacked for use with + * ash by J.T. Conklin. + * + * Public domain. + */ + +#include +#include +#include + +#include "shell.h" +#include "options.h" +#include "output.h" + +struct limits { + const char *name; + int cmd; + int factor; /* multiply by to get rlim_{cur,max} values */ + char option; +}; + +static const struct limits limits[] = { + { "time(seconds)", RLIMIT_CPU, 1, 't' }, + { "file(blocks)", RLIMIT_FSIZE, 512, 'f' }, + { "data(kbytes)", RLIMIT_DATA, 1024, 'd' }, + { "stack(kbytes)", RLIMIT_STACK, 1024, 's' }, + { "memory(kbytes)", RLIMIT_RSS, 1024, 'm' }, + { "coredump(blocks)", RLIMIT_CORE, 512, 'c' }, + { "nofiles(descriptors)", RLIMIT_NOFILE, 1, 'n' }, +#ifdef RLIMIT_VMEM + { "vmemory(kbytes)", RLIMIT_VMEM, 1024, 'v' }, +#endif +#ifdef RLIMIT_SWAP + { "swap(kbytes)", RLIMIT_SWAP, 1024, 'w' }, +#endif + { (char *) 0 } +}; + +int +ulimitcmd(argc, argv) + int argc; + char **argv; +{ + register int c; + quad_t val; + enum { SOFT = 0x1, HARD = 0x2 } + how = SOFT | HARD; + const struct limits *l; + int set, all = 0; + int optc, what; + struct rlimit limit; + + what = 'f'; + while ((optc = nextopt("HSatfdsmcn")) != '\0') + switch (optc) { + case 'H': + how = HARD; + break; + case 'S': + how = SOFT; + break; + case 'a': + all = 1; + break; + default: + what = optc; + } + + for (l = limits; l->name && l->option != what; l++) + ; + if (!l->name) + error("ulimit: internal error (%c)\n", what); + + set = *argptr ? 1 : 0; + if (set) { + char *p = *argptr; + + if (all || argptr[1]) + error("ulimit: too many arguments\n"); + val = (quad_t) 0; + while ((c = *p++) >= '0' && c <= '9') + { + val = (val * 10) + (long)(c - '0'); + if (val < (quad_t) 0) + break; + } + if (c) + error("ulimit: bad number\n"); + val *= l->factor; + } + if (all) { + for (l = limits; l->name; l++) { + getrlimit(l->cmd, &limit); + if (how & SOFT) + val = limit.rlim_cur; + else if (how & HARD) + val = limit.rlim_max; + + out1fmt("%-20s ", l->name); + if (val == RLIM_INFINITY) + out1fmt("unlimited\n"); + else + { + val /= l->factor; + out1fmt("%ld\n", (long) val); + } + } + return 0; + } + + getrlimit(l->cmd, &limit); + if (set) { + if (how & SOFT) + limit.rlim_cur = val; + if (how & HARD) + limit.rlim_max = val; + if (setrlimit(l->cmd, &limit) < 0) + error("ulimit: bad limit\n"); + } else { + if (how & SOFT) + val = limit.rlim_cur; + else if (how & HARD) + val = limit.rlim_max; + } + + if (!set) { + if (val == RLIM_INFINITY) + out1fmt("unlimited\n"); + else + { + val /= l->factor; + out1fmt("%ld\n", (long) val); + } + } + return 0; +}