svr4_resource.c revision 76166
143412Snewton/*	Derived from:
243412Snewton *      $NetBSD: svr4_resource.c,v 1.3 1998/12/13 18:00:52 christos Exp $
343412Snewton */
443412Snewton
543412Snewton/*-
643412Snewton * Original copyright:
743412Snewton *
843412Snewton * Copyright (c) 1998 The NetBSD Foundation, Inc.
943412Snewton * All rights reserved.
1043412Snewton *
1143412Snewton * This code is derived from software contributed to The NetBSD Foundation
1243412Snewton * by Christos Zoulas.
1343412Snewton *
1443412Snewton * Redistribution and use in source and binary forms, with or without
1543412Snewton * modification, are permitted provided that the following conditions
1643412Snewton * are met:
1743412Snewton * 1. Redistributions of source code must retain the above copyright
1843412Snewton *    notice, this list of conditions and the following disclaimer.
1943412Snewton * 2. Redistributions in binary form must reproduce the above copyright
2043412Snewton *    notice, this list of conditions and the following disclaimer in the
2143412Snewton *    documentation and/or other materials provided with the distribution.
2243412Snewton * 3. All advertising materials mentioning features or use of this software
2343412Snewton *    must display the following acknowledgement:
2443412Snewton *        This product includes software developed by the NetBSD
2543412Snewton *        Foundation, Inc. and its contributors.
2643412Snewton * 4. Neither the name of The NetBSD Foundation nor the names of its
2743412Snewton *    contributors may be used to endorse or promote products derived
2843412Snewton *    from this software without specific prior written permission.
2943412Snewton *
3043412Snewton * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
3143412Snewton * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
3243412Snewton * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3343412Snewton * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3443412Snewton * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3543412Snewton * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3643412Snewton * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3743412Snewton * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3843412Snewton * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3943412Snewton * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4043412Snewton * POSSIBILITY OF SUCH DAMAGE.
4149267Snewton *
4250477Speter * $FreeBSD: head/sys/compat/svr4/svr4_resource.c 76166 2001-05-01 08:13:21Z markm $
4343412Snewton */
4443412Snewton
4543412Snewton/*
4643412Snewton * Portions of this software have been derived from software contributed
4743412Snewton * to the FreeBSD Project by Mark Newton.
4843412Snewton *
4943412Snewton * Copyright (c) 1999 Mark Newton
5043412Snewton * All rights reserved.
5143412Snewton *
5243412Snewton * Redistribution and use in source and binary forms, with or without
5343412Snewton * modification, are permitted provided that the following conditions
5443412Snewton * are met:
5543412Snewton * 1. Redistributions of source code must retain the above copyright
5643412Snewton *    notice, this list of conditions and the following disclaimer.
5743412Snewton * 2. Redistributions in binary form must reproduce the above copyright
5843412Snewton *    notice, this list of conditions and the following disclaimer in the
5943412Snewton *    documentation and/or other materials provided with the distribution.
6043412Snewton * 3. The name of the author may not be used to endorse or promote products
6143412Snewton *    derived from this software without specific prior written permission
6243412Snewton *
6343412Snewton * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
6443412Snewton * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6543412Snewton * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6643412Snewton * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
6743412Snewton * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
6843412Snewton * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6943412Snewton * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
7043412Snewton * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7143412Snewton * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
7243412Snewton * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7343412Snewton */
7443412Snewton
7543412Snewton#include <sys/param.h>
7643412Snewton#include <sys/systm.h>
7776166Smarkm#include <sys/file.h>
7876166Smarkm#include <sys/mutex.h>
7943412Snewton#include <sys/proc.h>
8043412Snewton#include <sys/resource.h>
8143412Snewton#include <sys/resourcevar.h>
8243412Snewton
8365302Sobrien#include <compat/svr4/svr4.h>
8465302Sobrien#include <compat/svr4/svr4_types.h>
8565302Sobrien#include <compat/svr4/svr4_resource.h>
8665302Sobrien#include <compat/svr4/svr4_signal.h>
8765302Sobrien#include <compat/svr4/svr4_proto.h>
8865302Sobrien#include <compat/svr4/svr4_util.h>
8943412Snewton
9043412Snewtonstatic __inline int svr4_to_native_rl __P((int));
9143412Snewton
9243412Snewtonstatic __inline int
9343412Snewtonsvr4_to_native_rl(rl)
9443412Snewton	int rl;
9543412Snewton{
9643412Snewton	switch (rl) {
9743412Snewton	case SVR4_RLIMIT_CPU:
9843412Snewton		return RLIMIT_CPU;
9943412Snewton	case SVR4_RLIMIT_FSIZE:
10043412Snewton		return RLIMIT_FSIZE;
10143412Snewton	case SVR4_RLIMIT_DATA:
10243412Snewton		return RLIMIT_DATA;
10343412Snewton	case SVR4_RLIMIT_STACK:
10443412Snewton		return RLIMIT_STACK;
10543412Snewton	case SVR4_RLIMIT_CORE:
10643412Snewton		return RLIMIT_CORE;
10743412Snewton	case SVR4_RLIMIT_NOFILE:
10843412Snewton		return RLIMIT_NOFILE;
10943412Snewton	case SVR4_RLIMIT_VMEM:
11043412Snewton		return RLIMIT_RSS;
11143412Snewton	default:
11243412Snewton		return -1;
11343412Snewton	}
11443412Snewton}
11543412Snewton
11643412Snewton/*
11743412Snewton * Check if the resource limit fits within the BSD range and it is not
11843412Snewton * one of the magic SVR4 limit values
11943412Snewton */
12043412Snewton#define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
12143412Snewton	((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
12243412Snewton	((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
12343412Snewton	((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
12443412Snewton
12543412Snewton#define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
12643412Snewton	((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
12743412Snewton	((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
12843412Snewton	((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
12943412Snewton
13043412Snewtonint
13143412Snewtonsvr4_sys_getrlimit(p, uap)
13243412Snewton	register struct proc *p;
13343412Snewton	struct svr4_sys_getrlimit_args *uap;
13443412Snewton{
13543412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
13643412Snewton	struct rlimit blim;
13743412Snewton	struct svr4_rlimit slim;
13843412Snewton
13943412Snewton	if (rl == -1)
14043412Snewton		return EINVAL;
14143412Snewton
14271454Sjhb	/* For p_rlimit. */
14371454Sjhb	mtx_assert(&Giant, MA_OWNED);
14443412Snewton	blim = p->p_rlimit[rl];
14543412Snewton
14643412Snewton	/*
14743412Snewton	 * Our infinity, is their maxfiles.
14843412Snewton	 */
14943412Snewton	if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
15043412Snewton		blim.rlim_max = maxfiles;
15143412Snewton
15243412Snewton	/*
15343412Snewton	 * If the limit can be be represented, it is returned.
15443412Snewton	 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
15543412Snewton	 * else return RLIM_SAVED_CUR
15643412Snewton	 */
15743412Snewton	if (blim.rlim_max == RLIM_INFINITY)
15843412Snewton		slim.rlim_max = SVR4_RLIM_INFINITY;
15943412Snewton	else if (OKLIMIT(blim.rlim_max))
16043412Snewton		slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
16143412Snewton	else
16243412Snewton		slim.rlim_max = SVR4_RLIM_SAVED_MAX;
16343412Snewton
16443412Snewton	if (blim.rlim_cur == RLIM_INFINITY)
16543412Snewton		slim.rlim_cur = SVR4_RLIM_INFINITY;
16643412Snewton	else if (OKLIMIT(blim.rlim_cur))
16743412Snewton		slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
16843412Snewton	else if (blim.rlim_max == blim.rlim_cur)
16943412Snewton		slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
17043412Snewton	else
17143412Snewton		slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
17243412Snewton
17343412Snewton	return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
17443412Snewton}
17543412Snewton
17643412Snewton
17743412Snewtonint
17843412Snewtonsvr4_sys_setrlimit(p, uap)
17943412Snewton	register struct proc *p;
18043412Snewton	struct svr4_sys_setrlimit_args *uap;
18143412Snewton{
18243412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
18343412Snewton	struct rlimit blim, *limp;
18443412Snewton	struct svr4_rlimit slim;
18543412Snewton	int error;
18643412Snewton
18743412Snewton	if (rl == -1)
18843412Snewton		return EINVAL;
18943412Snewton
19071454Sjhb	/* For p_rlimit. */
19171454Sjhb	mtx_assert(&Giant, MA_OWNED);
19243412Snewton	limp = &p->p_rlimit[rl];
19343412Snewton
19443412Snewton	if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
19543412Snewton		return error;
19643412Snewton
19743412Snewton	/*
19843412Snewton	 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
19943412Snewton	 * unlimited.
20043412Snewton	 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
20143412Snewton	 * new limit to the corresponding saved hard limit, and if
20243412Snewton	 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
20343412Snewton	 * corresponding saved soft limit.
20443412Snewton	 *
20543412Snewton	 */
20643412Snewton	if (slim.rlim_max == SVR4_RLIM_INFINITY)
20743412Snewton		blim.rlim_max = RLIM_INFINITY;
20843412Snewton	else if (OKLIMIT(slim.rlim_max))
20943412Snewton		blim.rlim_max = (rlim_t) slim.rlim_max;
21043412Snewton	else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
21143412Snewton		blim.rlim_max = limp->rlim_max;
21243412Snewton	else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
21343412Snewton		blim.rlim_max = limp->rlim_cur;
21443412Snewton
21543412Snewton	if (slim.rlim_cur == SVR4_RLIM_INFINITY)
21643412Snewton		blim.rlim_cur = RLIM_INFINITY;
21743412Snewton	else if (OKLIMIT(slim.rlim_cur))
21843412Snewton		blim.rlim_cur = (rlim_t) slim.rlim_cur;
21943412Snewton	else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
22043412Snewton		blim.rlim_cur = limp->rlim_max;
22143412Snewton	else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
22243412Snewton		blim.rlim_cur = limp->rlim_cur;
22343412Snewton
22443412Snewton	return dosetrlimit(p, rl, &blim);
22543412Snewton}
22643412Snewton
22743412Snewton
22843412Snewtonint
22943412Snewtonsvr4_sys_getrlimit64(p, uap)
23043412Snewton	register struct proc *p;
23143412Snewton	struct svr4_sys_getrlimit64_args *uap;
23243412Snewton{
23343412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
23443412Snewton	struct rlimit blim;
23543412Snewton	struct svr4_rlimit64 slim;
23643412Snewton
23743412Snewton	if (rl == -1)
23843412Snewton		return EINVAL;
23943412Snewton
24071454Sjhb	/* For p_rlimit. */
24171454Sjhb	mtx_assert(&Giant, MA_OWNED);
24243412Snewton	blim = p->p_rlimit[rl];
24343412Snewton
24443412Snewton	/*
24543412Snewton	 * Our infinity, is their maxfiles.
24643412Snewton	 */
24743412Snewton	if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
24843412Snewton		blim.rlim_max = maxfiles;
24943412Snewton
25043412Snewton	/*
25143412Snewton	 * If the limit can be be represented, it is returned.
25243412Snewton	 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
25343412Snewton	 * else return SVR4_RLIM_SAVED_CUR
25443412Snewton	 */
25543412Snewton	if (blim.rlim_max == RLIM_INFINITY)
25643412Snewton		slim.rlim_max = SVR4_RLIM64_INFINITY;
25743412Snewton	else if (OKLIMIT64(blim.rlim_max))
25843412Snewton		slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
25943412Snewton	else
26043412Snewton		slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
26143412Snewton
26243412Snewton	if (blim.rlim_cur == RLIM_INFINITY)
26343412Snewton		slim.rlim_cur = SVR4_RLIM64_INFINITY;
26443412Snewton	else if (OKLIMIT64(blim.rlim_cur))
26543412Snewton		slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
26643412Snewton	else if (blim.rlim_max == blim.rlim_cur)
26743412Snewton		slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
26843412Snewton	else
26943412Snewton		slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
27043412Snewton
27143412Snewton	return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
27243412Snewton}
27343412Snewton
27443412Snewton
27543412Snewtonint
27643412Snewtonsvr4_sys_setrlimit64(p, uap)
27743412Snewton	register struct proc *p;
27843412Snewton	struct svr4_sys_setrlimit64_args *uap;
27943412Snewton{
28043412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
28143412Snewton	struct rlimit blim, *limp;
28243412Snewton	struct svr4_rlimit64 slim;
28343412Snewton	int error;
28443412Snewton
28543412Snewton	if (rl == -1)
28643412Snewton		return EINVAL;
28743412Snewton
28871454Sjhb	/* For p_rlimit. */
28971454Sjhb	mtx_assert(&Giant, MA_OWNED);
29043412Snewton	limp = &p->p_rlimit[rl];
29143412Snewton
29243412Snewton	if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
29343412Snewton		return error;
29443412Snewton
29543412Snewton	/*
29643412Snewton	 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
29743412Snewton	 * unlimited.
29843412Snewton	 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
29943412Snewton	 * new limit to the corresponding saved hard limit, and if
30043412Snewton	 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
30143412Snewton	 * corresponding saved soft limit.
30243412Snewton	 *
30343412Snewton	 */
30443412Snewton	if (slim.rlim_max == SVR4_RLIM64_INFINITY)
30543412Snewton		blim.rlim_max = RLIM_INFINITY;
30643412Snewton	else if (OKLIMIT64(slim.rlim_max))
30743412Snewton		blim.rlim_max = (rlim_t) slim.rlim_max;
30843412Snewton	else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
30943412Snewton		blim.rlim_max = limp->rlim_max;
31043412Snewton	else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
31143412Snewton		blim.rlim_max = limp->rlim_cur;
31243412Snewton
31343412Snewton	if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
31443412Snewton		blim.rlim_cur = RLIM_INFINITY;
31543412Snewton	else if (OKLIMIT64(slim.rlim_cur))
31643412Snewton		blim.rlim_cur = (rlim_t) slim.rlim_cur;
31743412Snewton	else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
31843412Snewton		blim.rlim_cur = limp->rlim_max;
31943412Snewton	else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
32043412Snewton		blim.rlim_cur = limp->rlim_cur;
32143412Snewton
32243412Snewton	return dosetrlimit(p, rl, &blim);
32343412Snewton}
324