Implement POSIX.1-2001 (XSI)'s ulimit(3).

Submitted by:	Kyle Martin <mkm@ieee.org>
This commit is contained in:
Mike Barcroft 2002-08-08 04:50:36 +00:00
parent 315009a483
commit af8c0bce98
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=101497
6 changed files with 211 additions and 4 deletions

View file

@ -21,7 +21,8 @@ INCS= a.out.h ar.h assert.h bitstring.h complex.h cpio.h ctype.h db.h \
search.h setjmp.h sgtty.h \
signal.h stab.h stdbool.h stddef.h stdio.h stdlib.h strhash.h \
string.h stringlist.h strings.h sysexits.h tar.h time.h timeconv.h \
timers.h ttyent.h unistd.h utime.h utmp.h vis.h wchar.h wctype.h
timers.h ttyent.h ulimit.h unistd.h utime.h utmp.h vis.h wchar.h \
wctype.h
MHDRS= float.h floatingpoint.h stdarg.h varargs.h

37
include/ulimit.h Normal file
View file

@ -0,0 +1,37 @@
/*-
* 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$
*/
#ifndef _ULIMIT_H_
#define _ULIMIT_H_
#define UL_GETSIZE 1
#define UL_SETSIZE 2
long ulimit(int, ...);
#endif /* !_ULIMIT_H_ */

View file

@ -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

100
lib/libc/gen/ulimit.3 Normal file
View file

@ -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.

68
lib/libc/gen/ulimit.c Normal file
View file

@ -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);
}
}

View file

@ -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