svr4_resource.c revision 49267
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 *
4249267Snewton * $Id$
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>
7743412Snewton#include <sys/proc.h>
7843412Snewton#include <sys/file.h>
7943412Snewton#include <sys/resource.h>
8043412Snewton#include <sys/resourcevar.h>
8143412Snewton
8243412Snewton#include <svr4/svr4_types.h>
8343412Snewton#include <svr4/svr4_resource.h>
8443412Snewton#include <svr4/svr4_signal.h>
8543412Snewton#include <svr4/svr4_proto.h>
8643412Snewton#include <svr4/svr4_util.h>
8743412Snewton
8843412Snewtonstatic __inline int svr4_to_native_rl __P((int));
8943412Snewton
9043412Snewtonstatic __inline int
9143412Snewtonsvr4_to_native_rl(rl)
9243412Snewton	int rl;
9343412Snewton{
9443412Snewton	switch (rl) {
9543412Snewton	case SVR4_RLIMIT_CPU:
9643412Snewton		return RLIMIT_CPU;
9743412Snewton	case SVR4_RLIMIT_FSIZE:
9843412Snewton		return RLIMIT_FSIZE;
9943412Snewton	case SVR4_RLIMIT_DATA:
10043412Snewton		return RLIMIT_DATA;
10143412Snewton	case SVR4_RLIMIT_STACK:
10243412Snewton		return RLIMIT_STACK;
10343412Snewton	case SVR4_RLIMIT_CORE:
10443412Snewton		return RLIMIT_CORE;
10543412Snewton	case SVR4_RLIMIT_NOFILE:
10643412Snewton		return RLIMIT_NOFILE;
10743412Snewton	case SVR4_RLIMIT_VMEM:
10843412Snewton		return RLIMIT_RSS;
10943412Snewton	default:
11043412Snewton		return -1;
11143412Snewton	}
11243412Snewton}
11343412Snewton
11443412Snewton/*
11543412Snewton * Check if the resource limit fits within the BSD range and it is not
11643412Snewton * one of the magic SVR4 limit values
11743412Snewton */
11843412Snewton#define OKLIMIT(l) (((int32_t)(l)) >= 0 && ((int32_t)(l)) < 0x7fffffff && \
11943412Snewton	((svr4_rlim_t)(l)) != SVR4_RLIM_INFINITY && \
12043412Snewton	((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_CUR && \
12143412Snewton	((svr4_rlim_t)(l)) != SVR4_RLIM_SAVED_MAX)
12243412Snewton
12343412Snewton#define OKLIMIT64(l) (((rlim_t)(l)) >= 0 && ((rlim_t)(l)) < RLIM_INFINITY && \
12443412Snewton	((svr4_rlim64_t)(l)) != SVR4_RLIM64_INFINITY && \
12543412Snewton	((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_CUR && \
12643412Snewton	((svr4_rlim64_t)(l)) != SVR4_RLIM64_SAVED_MAX)
12743412Snewton
12843412Snewtonint
12943412Snewtonsvr4_sys_getrlimit(p, uap)
13043412Snewton	register struct proc *p;
13143412Snewton	struct svr4_sys_getrlimit_args *uap;
13243412Snewton{
13343412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
13443412Snewton	struct rlimit blim;
13543412Snewton	struct svr4_rlimit slim;
13643412Snewton
13743412Snewton	if (rl == -1)
13843412Snewton		return EINVAL;
13943412Snewton
14043412Snewton	blim = p->p_rlimit[rl];
14143412Snewton
14243412Snewton	/*
14343412Snewton	 * Our infinity, is their maxfiles.
14443412Snewton	 */
14543412Snewton	if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
14643412Snewton		blim.rlim_max = maxfiles;
14743412Snewton
14843412Snewton	/*
14943412Snewton	 * If the limit can be be represented, it is returned.
15043412Snewton	 * Otherwise, if rlim_cur == rlim_max, return RLIM_SAVED_MAX
15143412Snewton	 * else return RLIM_SAVED_CUR
15243412Snewton	 */
15343412Snewton	if (blim.rlim_max == RLIM_INFINITY)
15443412Snewton		slim.rlim_max = SVR4_RLIM_INFINITY;
15543412Snewton	else if (OKLIMIT(blim.rlim_max))
15643412Snewton		slim.rlim_max = (svr4_rlim_t) blim.rlim_max;
15743412Snewton	else
15843412Snewton		slim.rlim_max = SVR4_RLIM_SAVED_MAX;
15943412Snewton
16043412Snewton	if (blim.rlim_cur == RLIM_INFINITY)
16143412Snewton		slim.rlim_cur = SVR4_RLIM_INFINITY;
16243412Snewton	else if (OKLIMIT(blim.rlim_cur))
16343412Snewton		slim.rlim_cur = (svr4_rlim_t) blim.rlim_cur;
16443412Snewton	else if (blim.rlim_max == blim.rlim_cur)
16543412Snewton		slim.rlim_cur = SVR4_RLIM_SAVED_MAX;
16643412Snewton	else
16743412Snewton		slim.rlim_cur = SVR4_RLIM_SAVED_CUR;
16843412Snewton
16943412Snewton	return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
17043412Snewton}
17143412Snewton
17243412Snewton
17343412Snewtonint
17443412Snewtonsvr4_sys_setrlimit(p, uap)
17543412Snewton	register struct proc *p;
17643412Snewton	struct svr4_sys_setrlimit_args *uap;
17743412Snewton{
17843412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
17943412Snewton	struct rlimit blim, *limp;
18043412Snewton	struct svr4_rlimit slim;
18143412Snewton	int error;
18243412Snewton
18343412Snewton	if (rl == -1)
18443412Snewton		return EINVAL;
18543412Snewton
18643412Snewton	limp = &p->p_rlimit[rl];
18743412Snewton
18843412Snewton	if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
18943412Snewton		return error;
19043412Snewton
19143412Snewton	/*
19243412Snewton	 * if the limit is SVR4_RLIM_INFINITY, then we set it to our
19343412Snewton	 * unlimited.
19443412Snewton	 * We should also: If it is SVR4_RLIM_SAVED_MAX, we should set the
19543412Snewton	 * new limit to the corresponding saved hard limit, and if
19643412Snewton	 * it is equal to SVR4_RLIM_SAVED_CUR, we should set it to the
19743412Snewton	 * corresponding saved soft limit.
19843412Snewton	 *
19943412Snewton	 */
20043412Snewton	if (slim.rlim_max == SVR4_RLIM_INFINITY)
20143412Snewton		blim.rlim_max = RLIM_INFINITY;
20243412Snewton	else if (OKLIMIT(slim.rlim_max))
20343412Snewton		blim.rlim_max = (rlim_t) slim.rlim_max;
20443412Snewton	else if (slim.rlim_max == SVR4_RLIM_SAVED_MAX)
20543412Snewton		blim.rlim_max = limp->rlim_max;
20643412Snewton	else if (slim.rlim_max == SVR4_RLIM_SAVED_CUR)
20743412Snewton		blim.rlim_max = limp->rlim_cur;
20843412Snewton
20943412Snewton	if (slim.rlim_cur == SVR4_RLIM_INFINITY)
21043412Snewton		blim.rlim_cur = RLIM_INFINITY;
21143412Snewton	else if (OKLIMIT(slim.rlim_cur))
21243412Snewton		blim.rlim_cur = (rlim_t) slim.rlim_cur;
21343412Snewton	else if (slim.rlim_cur == SVR4_RLIM_SAVED_MAX)
21443412Snewton		blim.rlim_cur = limp->rlim_max;
21543412Snewton	else if (slim.rlim_cur == SVR4_RLIM_SAVED_CUR)
21643412Snewton		blim.rlim_cur = limp->rlim_cur;
21743412Snewton
21843412Snewton	return dosetrlimit(p, rl, &blim);
21943412Snewton}
22043412Snewton
22143412Snewton
22243412Snewtonint
22343412Snewtonsvr4_sys_getrlimit64(p, uap)
22443412Snewton	register struct proc *p;
22543412Snewton	struct svr4_sys_getrlimit64_args *uap;
22643412Snewton{
22743412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
22843412Snewton	struct rlimit blim;
22943412Snewton	struct svr4_rlimit64 slim;
23043412Snewton
23143412Snewton	if (rl == -1)
23243412Snewton		return EINVAL;
23343412Snewton
23443412Snewton	blim = p->p_rlimit[rl];
23543412Snewton
23643412Snewton	/*
23743412Snewton	 * Our infinity, is their maxfiles.
23843412Snewton	 */
23943412Snewton	if (rl == RLIMIT_NOFILE && blim.rlim_max == RLIM_INFINITY)
24043412Snewton		blim.rlim_max = maxfiles;
24143412Snewton
24243412Snewton	/*
24343412Snewton	 * If the limit can be be represented, it is returned.
24443412Snewton	 * Otherwise, if rlim_cur == rlim_max, return SVR4_RLIM_SAVED_MAX
24543412Snewton	 * else return SVR4_RLIM_SAVED_CUR
24643412Snewton	 */
24743412Snewton	if (blim.rlim_max == RLIM_INFINITY)
24843412Snewton		slim.rlim_max = SVR4_RLIM64_INFINITY;
24943412Snewton	else if (OKLIMIT64(blim.rlim_max))
25043412Snewton		slim.rlim_max = (svr4_rlim64_t) blim.rlim_max;
25143412Snewton	else
25243412Snewton		slim.rlim_max = SVR4_RLIM64_SAVED_MAX;
25343412Snewton
25443412Snewton	if (blim.rlim_cur == RLIM_INFINITY)
25543412Snewton		slim.rlim_cur = SVR4_RLIM64_INFINITY;
25643412Snewton	else if (OKLIMIT64(blim.rlim_cur))
25743412Snewton		slim.rlim_cur = (svr4_rlim64_t) blim.rlim_cur;
25843412Snewton	else if (blim.rlim_max == blim.rlim_cur)
25943412Snewton		slim.rlim_cur = SVR4_RLIM64_SAVED_MAX;
26043412Snewton	else
26143412Snewton		slim.rlim_cur = SVR4_RLIM64_SAVED_CUR;
26243412Snewton
26343412Snewton	return copyout(&slim, SCARG(uap, rlp), sizeof(*SCARG(uap, rlp)));
26443412Snewton}
26543412Snewton
26643412Snewton
26743412Snewtonint
26843412Snewtonsvr4_sys_setrlimit64(p, uap)
26943412Snewton	register struct proc *p;
27043412Snewton	struct svr4_sys_setrlimit64_args *uap;
27143412Snewton{
27243412Snewton	int rl = svr4_to_native_rl(SCARG(uap, which));
27343412Snewton	struct rlimit blim, *limp;
27443412Snewton	struct svr4_rlimit64 slim;
27543412Snewton	int error;
27643412Snewton
27743412Snewton	if (rl == -1)
27843412Snewton		return EINVAL;
27943412Snewton
28043412Snewton	limp = &p->p_rlimit[rl];
28143412Snewton
28243412Snewton	if ((error = copyin(SCARG(uap, rlp), &slim, sizeof(slim))) != 0)
28343412Snewton		return error;
28443412Snewton
28543412Snewton	/*
28643412Snewton	 * if the limit is SVR4_RLIM64_INFINITY, then we set it to our
28743412Snewton	 * unlimited.
28843412Snewton	 * We should also: If it is SVR4_RLIM64_SAVED_MAX, we should set the
28943412Snewton	 * new limit to the corresponding saved hard limit, and if
29043412Snewton	 * it is equal to SVR4_RLIM64_SAVED_CUR, we should set it to the
29143412Snewton	 * corresponding saved soft limit.
29243412Snewton	 *
29343412Snewton	 */
29443412Snewton	if (slim.rlim_max == SVR4_RLIM64_INFINITY)
29543412Snewton		blim.rlim_max = RLIM_INFINITY;
29643412Snewton	else if (OKLIMIT64(slim.rlim_max))
29743412Snewton		blim.rlim_max = (rlim_t) slim.rlim_max;
29843412Snewton	else if (slim.rlim_max == SVR4_RLIM64_SAVED_MAX)
29943412Snewton		blim.rlim_max = limp->rlim_max;
30043412Snewton	else if (slim.rlim_max == SVR4_RLIM64_SAVED_CUR)
30143412Snewton		blim.rlim_max = limp->rlim_cur;
30243412Snewton
30343412Snewton	if (slim.rlim_cur == SVR4_RLIM64_INFINITY)
30443412Snewton		blim.rlim_cur = RLIM_INFINITY;
30543412Snewton	else if (OKLIMIT64(slim.rlim_cur))
30643412Snewton		blim.rlim_cur = (rlim_t) slim.rlim_cur;
30743412Snewton	else if (slim.rlim_cur == SVR4_RLIM64_SAVED_MAX)
30843412Snewton		blim.rlim_cur = limp->rlim_max;
30943412Snewton	else if (slim.rlim_cur == SVR4_RLIM64_SAVED_CUR)
31043412Snewton		blim.rlim_cur = limp->rlim_cur;
31143412Snewton
31243412Snewton	return dosetrlimit(p, rl, &blim);
31343412Snewton}
314