summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authormike <mike@FreeBSD.org>2002-08-08 04:50:36 +0000
committermike <mike@FreeBSD.org>2002-08-08 04:50:36 +0000
commita72d8585b7d6c0e0b3cd0824b229a07724c33288 (patch)
treecbb393f0b70086e915644ca4cf3a98a13e26cb56 /lib
parent586a522666b49914c4ff437eeea75730cbeefec1 (diff)
downloadFreeBSD-src-a72d8585b7d6c0e0b3cd0824b229a07724c33288.zip
FreeBSD-src-a72d8585b7d6c0e0b3cd0824b229a07724c33288.tar.gz
Implement POSIX.1-2001 (XSI)'s ulimit(3).
Submitted by: Kyle Martin <mkm@ieee.org>
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/gen/Makefile.inc6
-rw-r--r--lib/libc/gen/ulimit.3100
-rw-r--r--lib/libc/gen/ulimit.c68
-rw-r--r--lib/libc/sys/getrlimit.21
4 files changed, 172 insertions, 3 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc
index c27ffd1..f15f6e0 100644
--- a/lib/libc/gen/Makefile.inc
+++ b/lib/libc/gen/Makefile.inc
@@ -28,8 +28,8 @@ SRCS+= __xuname.c _pthread_stubs.c _rand48.c _spinlock_stub.c _thread_init.c \
sigsetops.c sleep.c srand48.c statvfs.c stringlist.c strtofflags.c \
sysconf.c sysctl.c sysctlbyname.c sysctlnametomib.c \
syslog.c telldir.c termios.c time.c times.c timezone.c ttyname.c \
- ttyslot.c ualarm.c uname.c unvis.c usleep.c utime.c valloc.c vis.c \
- wait.c wait3.c waitpid.c
+ ttyslot.c ualarm.c ulimit.c uname.c unvis.c usleep.c utime.c \
+ valloc.c vis.c wait.c wait3.c waitpid.c
# machine-dependent gen sources
.if exists(${.CURDIR}/../libc/${MACHINE_ARCH}/gen/Makefile.inc)
@@ -56,7 +56,7 @@ MAN+= alarm.3 arc4random.3 \
siginterrupt.3 signal.3 sigsetops.3 sleep.3 statvfs.3 stringlist.3 \
strtofflags.3 sysconf.3 sysctl.3 syslog.3 tcgetpgrp.3 \
tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 time.3 times.3 timezone.3 \
- ttyname.3 tzset.3 ualarm.3 uname.3 unvis.3 usleep.3 utime.3 \
+ ttyname.3 tzset.3 ualarm.3 ulimit.3 uname.3 unvis.3 usleep.3 utime.3 \
valloc.3 vis.3
MLINKS+=arc4random.3 arc4random_addrandom.3 arc4random.3 arc4random_stir.3
diff --git a/lib/libc/gen/ulimit.3 b/lib/libc/gen/ulimit.3
new file mode 100644
index 0000000..5451b4b
--- /dev/null
+++ b/lib/libc/gen/ulimit.3
@@ -0,0 +1,100 @@
+.\" Copyright (c) 2002 Kyle Martin. All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd August 8, 2002
+.Dt ULIMIT 3
+.Os
+.Sh NAME
+.Nm ulimit
+.Nd get and set process limits
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In ulimit.h
+.Ft long
+.Fn ulimit "int cmd" "..."
+.Sh DESCRIPTION
+The
+.Fn ulimit
+function will get and set process limits.
+Currently this is limited to the maximum file size.
+The
+.Va cmd
+parameter is one of the following:
+.Pp
+.Bl -tag -offset -width UL_GETSIZE
+.It Dv UL_GETSIZE
+will return the maximum file size in units of 512 blocks of
+the current process.
+.Pp
+.It Dv UL_SETSIZE
+will attempt to set the maximum file size of the current
+process and its children with the second argument expressed as a long.
+.El
+.Sh RETURN VALUES
+Upon successful completion,
+.Fn ulimit
+returns the value requested;
+otherwise the value -1 is returned and
+.Ar errno
+is set to indicate the error.
+.Sh ERRORS
+The
+.Fn ulimit
+function will fail if:
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The command specified was invalid.
+.It Bq Er EPERM
+The limit specified to
+.Fn ulimit
+would have raised the maximum limit value,
+and the caller is not the super-user.
+.El
+.Sh SEE ALSO
+.Xr getrlimit 2
+.Sh STANDARDS
+The
+.Fn ulimit
+function conforms to
+.St -p1003.1-2001 .
+.Sh HISTORY
+The
+.Fn ulimit
+function first appeared in
+.Fx 5.0 .
+.Sh BUGS
+The
+.Fn ulimit
+function provides limited precision for
+setting and retrieving process limits.
+If there is a need for greater precision than the
+type
+.Vt long
+provides, the
+.Xr getrlimit 2
+and
+.Xr setrlimit 2
+functions should be considered.
diff --git a/lib/libc/gen/ulimit.c b/lib/libc/gen/ulimit.c
new file mode 100644
index 0000000..806c587
--- /dev/null
+++ b/lib/libc/gen/ulimit.c
@@ -0,0 +1,68 @@
+/*-
+ * Copyright (c) 2002 Kyle Martin <mkm@ieee.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+
+#include <errno.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <ulimit.h>
+
+long
+ulimit(int cmd, ...)
+{
+ struct rlimit limit;
+ va_list ap;
+ long arg, size;
+
+ if (cmd == UL_GETSIZE) {
+ if (getrlimit(RLIMIT_FSIZE, &limit) == -1)
+ return (-1);
+ limit.rlim_cur /= 512;
+ if (limit.rlim_cur > LONG_MAX)
+ return (LONG_MAX);
+ return ((long)limit.rlim_cur);
+ } else if (cmd == UL_SETSIZE) {
+ va_start(ap, cmd);
+ arg = va_arg(ap, long);
+ va_end(ap);
+ limit.rlim_max = limit.rlim_cur = (rlim_t)arg * 512;
+
+ /* The setrlimit() function sets errno to EPERM if needed. */
+ if (setrlimit(RLIMIT_FSIZE, &limit) == -1)
+ return (-1);
+ if (arg * 512 > LONG_MAX)
+ return (LONG_MAX);
+ return (arg);
+ } else {
+ errno = EINVAL;
+ return (-1);
+ }
+}
diff --git a/lib/libc/sys/getrlimit.2 b/lib/libc/sys/getrlimit.2
index 5b5c149..401325d 100644
--- a/lib/libc/sys/getrlimit.2
+++ b/lib/libc/sys/getrlimit.2
@@ -178,6 +178,7 @@ raised the maximum limit value, and the caller is not the super-user.
.Xr quotactl 2 ,
.Xr sigaltstack 2 ,
.Xr sigvec 2 ,
+.Xr ulimit 3 ,
.Xr sysctl 3
.Sh HISTORY
The
OpenPOWER on IntegriCloud