diff --git a/include/Makefile b/include/Makefile index ae4247f22ba7..14037a07fa15 100644 --- a/include/Makefile +++ b/include/Makefile @@ -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 diff --git a/include/ulimit.h b/include/ulimit.h new file mode 100644 index 000000000000..67fcf4c80d3a --- /dev/null +++ b/include/ulimit.h @@ -0,0 +1,37 @@ +/*- + * 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 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_ */ diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index c27ffd100fd0..f15f6e03d430 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 000000000000..5451b4b1a7d0 --- /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 000000000000..806c587c7036 --- /dev/null +++ b/lib/libc/gen/ulimit.c @@ -0,0 +1,68 @@ +/*- + * 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 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 +#include +#include + +#include +#include +#include +#include + +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 5b5c1490347b..401325d605e1 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