xref: /freebsd/lib/libc/gen/ulimit.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1af8c0bceSMike Barcroft /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
4af8c0bceSMike Barcroft  * Copyright (c) 2002 Kyle Martin <mkm@ieee.org>
5af8c0bceSMike Barcroft  * All rights reserved.
6af8c0bceSMike Barcroft  *
7af8c0bceSMike Barcroft  * Redistribution and use in source and binary forms, with or without
8af8c0bceSMike Barcroft  * modification, are permitted provided that the following conditions
9af8c0bceSMike Barcroft  * are met:
10af8c0bceSMike Barcroft  * 1. Redistributions of source code must retain the above copyright
11af8c0bceSMike Barcroft  *    notice, this list of conditions and the following disclaimer.
12af8c0bceSMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
13af8c0bceSMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
14af8c0bceSMike Barcroft  *    documentation and/or other materials provided with the distribution.
15af8c0bceSMike Barcroft  *
16af8c0bceSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17af8c0bceSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18af8c0bceSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19af8c0bceSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20af8c0bceSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21af8c0bceSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22af8c0bceSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23af8c0bceSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24af8c0bceSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25af8c0bceSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26af8c0bceSMike Barcroft  * SUCH DAMAGE.
27af8c0bceSMike Barcroft  */
28af8c0bceSMike Barcroft 
29af8c0bceSMike Barcroft #include <sys/types.h>
30af8c0bceSMike Barcroft #include <sys/time.h>
31af8c0bceSMike Barcroft #include <sys/resource.h>
32af8c0bceSMike Barcroft 
33af8c0bceSMike Barcroft #include <errno.h>
34af8c0bceSMike Barcroft #include <limits.h>
35af8c0bceSMike Barcroft #include <stdarg.h>
36af8c0bceSMike Barcroft #include <ulimit.h>
37af8c0bceSMike Barcroft 
38af8c0bceSMike Barcroft long
ulimit(int cmd,...)39af8c0bceSMike Barcroft ulimit(int cmd, ...)
40af8c0bceSMike Barcroft {
41af8c0bceSMike Barcroft 	struct rlimit limit;
42af8c0bceSMike Barcroft 	va_list ap;
43d2f78330SPedro F. Giffuni 	rlim_t arg;
44af8c0bceSMike Barcroft 
4586e1d472STim J. Robbins 	if (cmd == UL_GETFSIZE) {
46af8c0bceSMike Barcroft 		if (getrlimit(RLIMIT_FSIZE, &limit) == -1)
47af8c0bceSMike Barcroft 			return (-1);
48af8c0bceSMike Barcroft 		limit.rlim_cur /= 512;
49af8c0bceSMike Barcroft 		if (limit.rlim_cur > LONG_MAX)
50af8c0bceSMike Barcroft 			return (LONG_MAX);
51af8c0bceSMike Barcroft 		return ((long)limit.rlim_cur);
5286e1d472STim J. Robbins 	} else if (cmd == UL_SETFSIZE) {
53af8c0bceSMike Barcroft 		va_start(ap, cmd);
54d2f78330SPedro F. Giffuni 		arg = va_arg(ap, long);
55af8c0bceSMike Barcroft 		va_end(ap);
56d2f78330SPedro F. Giffuni 		if (arg < 0)
57d2f78330SPedro F. Giffuni 			arg = LONG_MAX;
58d2f78330SPedro F. Giffuni 		if (arg > RLIM_INFINITY / 512)
59d2f78330SPedro F. Giffuni 			arg = RLIM_INFINITY / 512;
60d2f78330SPedro F. Giffuni 		limit.rlim_max = limit.rlim_cur = arg * 512;
61af8c0bceSMike Barcroft 
62af8c0bceSMike Barcroft 		/* The setrlimit() function sets errno to EPERM if needed. */
63af8c0bceSMike Barcroft 		if (setrlimit(RLIMIT_FSIZE, &limit) == -1)
64af8c0bceSMike Barcroft 			return (-1);
65d2f78330SPedro F. Giffuni 		return ((long)arg);
66af8c0bceSMike Barcroft 	} else {
67af8c0bceSMike Barcroft 		errno = EINVAL;
68af8c0bceSMike Barcroft 		return (-1);
69af8c0bceSMike Barcroft 	}
70af8c0bceSMike Barcroft }
71