1*8f021900Sryo /* $NetBSD: linux32_resource.c,v 1.13 2021/12/02 04:29:49 ryo Exp $ */
2ee0c5b44Smanu
3ee0c5b44Smanu /*-
4ee0c5b44Smanu * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5ee0c5b44Smanu *
6ee0c5b44Smanu * Redistribution and use in source and binary forms, with or without
7ee0c5b44Smanu * modification, are permitted provided that the following conditions
8ee0c5b44Smanu * are met:
9ee0c5b44Smanu * 1. Redistributions of source code must retain the above copyright
10ee0c5b44Smanu * notice, this list of conditions and the following disclaimer.
11ee0c5b44Smanu * 2. Redistributions in binary form must reproduce the above copyright
12ee0c5b44Smanu * notice, this list of conditions and the following disclaimer in the
13ee0c5b44Smanu * documentation and/or other materials provided with the distribution.
14ee0c5b44Smanu * 3. All advertising materials mentioning features or use of this software
15ee0c5b44Smanu * must display the following acknowledgement:
16ee0c5b44Smanu * This product includes software developed by Emmanuel Dreyfus
17ee0c5b44Smanu * 4. The name of the author may not be used to endorse or promote
18ee0c5b44Smanu * products derived from this software without specific prior written
19ee0c5b44Smanu * permission.
20ee0c5b44Smanu *
21ee0c5b44Smanu * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22ee0c5b44Smanu * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23ee0c5b44Smanu * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24ee0c5b44Smanu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25ee0c5b44Smanu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26ee0c5b44Smanu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27ee0c5b44Smanu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28ee0c5b44Smanu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29ee0c5b44Smanu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30ee0c5b44Smanu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31ee0c5b44Smanu * POSSIBILITY OF SUCH DAMAGE.
32ee0c5b44Smanu */
33ee0c5b44Smanu
34ee0c5b44Smanu #include <sys/cdefs.h>
35ee0c5b44Smanu
36*8f021900Sryo __KERNEL_RCSID(0, "$NetBSD: linux32_resource.c,v 1.13 2021/12/02 04:29:49 ryo Exp $");
37ee0c5b44Smanu
38ee0c5b44Smanu #include <sys/types.h>
39ee0c5b44Smanu #include <sys/param.h>
40ee0c5b44Smanu #include <sys/fstypes.h>
41ee0c5b44Smanu #include <sys/signal.h>
42ee0c5b44Smanu #include <sys/dirent.h>
43ee0c5b44Smanu #include <sys/kernel.h>
44ee0c5b44Smanu #include <sys/fcntl.h>
45ee0c5b44Smanu #include <sys/select.h>
46ee0c5b44Smanu #include <sys/proc.h>
478beba9d0Sdsl #include <sys/resourcevar.h>
48ee0c5b44Smanu #include <sys/ucred.h>
49ee0c5b44Smanu #include <sys/swap.h>
50ee0c5b44Smanu
51ee0c5b44Smanu #include <machine/types.h>
52ee0c5b44Smanu
53ee0c5b44Smanu #include <sys/syscallargs.h>
54ee0c5b44Smanu
55ee0c5b44Smanu #include <compat/netbsd32/netbsd32.h>
56ee0c5b44Smanu #include <compat/netbsd32/netbsd32_conv.h>
57ee0c5b44Smanu #include <compat/netbsd32/netbsd32_syscallargs.h>
58ee0c5b44Smanu
59ee0c5b44Smanu #include <compat/linux/common/linux_types.h>
60ee0c5b44Smanu #include <compat/linux/common/linux_signal.h>
61ee0c5b44Smanu #include <compat/linux/common/linux_machdep.h>
62ee0c5b44Smanu #include <compat/linux/common/linux_misc.h>
63ee0c5b44Smanu #include <compat/linux/common/linux_limit.h>
64ee0c5b44Smanu #include <compat/linux/common/linux_oldolduname.h>
6592ce8c6aSad #include <compat/linux/common/linux_ipc.h>
6692ce8c6aSad #include <compat/linux/common/linux_sem.h>
67ee0c5b44Smanu #include <compat/linux/linux_syscallargs.h>
68ee0c5b44Smanu
69ee0c5b44Smanu #include <compat/linux32/common/linux32_types.h>
70ee0c5b44Smanu #include <compat/linux32/common/linux32_signal.h>
71ee0c5b44Smanu #include <compat/linux32/common/linux32_machdep.h>
72ee0c5b44Smanu #include <compat/linux32/common/linux32_sysctl.h>
73ee0c5b44Smanu #include <compat/linux32/common/linux32_socketcall.h>
74ee0c5b44Smanu #include <compat/linux32/linux32_syscallargs.h>
75ee0c5b44Smanu
76ee0c5b44Smanu int
linux32_sys_getrlimit(struct lwp * l,const struct linux32_sys_getrlimit_args * uap,register_t * retval)777e2790cfSdsl linux32_sys_getrlimit(struct lwp *l, const struct linux32_sys_getrlimit_args *uap, register_t *retval)
78ee0c5b44Smanu {
797e2790cfSdsl /* {
80ee0c5b44Smanu syscallarg(int) which;
81ee0c5b44Smanu syscallarg(netbsd32_orlimitp_t) rlp;
827e2790cfSdsl } */
83ee0c5b44Smanu struct orlimit orl;
848beba9d0Sdsl int which;
85ee0c5b44Smanu
868beba9d0Sdsl which = linux_to_bsd_limit(SCARG(uap, which));
878beba9d0Sdsl if (which < 0)
888beba9d0Sdsl return -which;
89ee0c5b44Smanu
9041aa5859Sriastradh memset(&orl, 0, sizeof(orl));
918beba9d0Sdsl bsd_to_linux_rlimit(&orl, &l->l_proc->p_rlimit[which]);
92ee0c5b44Smanu
93d364d308Sdsl return copyout(&orl, SCARG_P32(uap, rlp), sizeof(orl));
94ee0c5b44Smanu }
95ee0c5b44Smanu
96ee0c5b44Smanu int
linux32_sys_setrlimit(struct lwp * l,const struct linux32_sys_setrlimit_args * uap,register_t * retval)977e2790cfSdsl linux32_sys_setrlimit(struct lwp *l, const struct linux32_sys_setrlimit_args *uap, register_t *retval)
98ee0c5b44Smanu {
997e2790cfSdsl /* {
100ee0c5b44Smanu syscallarg(int) which;
101ee0c5b44Smanu syscallarg(netbsd32_orlimitp_t) rlp;
1027e2790cfSdsl } */
103ee0c5b44Smanu struct rlimit rl;
104ee0c5b44Smanu struct orlimit orl;
105ee0c5b44Smanu int error;
1068beba9d0Sdsl int which;
107ee0c5b44Smanu
108d364d308Sdsl if ((error = copyin(SCARG_P32(uap, rlp), &orl, sizeof(orl))) != 0)
109ee0c5b44Smanu return error;
110ee0c5b44Smanu
1118beba9d0Sdsl which = linux_to_bsd_limit(SCARG(uap, which));
1128beba9d0Sdsl if (which < 0)
1138beba9d0Sdsl return -which;
1148beba9d0Sdsl
115511ffdebSjoerg linux32_to_bsd_rlimit(&rl, &orl);
116ee0c5b44Smanu
1178beba9d0Sdsl return dosetrlimit(l, l->l_proc, which, &rl);
118ee0c5b44Smanu }
119ee0c5b44Smanu
120ee0c5b44Smanu int
linux32_sys_ugetrlimit(struct lwp * l,const struct linux32_sys_ugetrlimit_args * uap,register_t * retval)1217e2790cfSdsl linux32_sys_ugetrlimit(struct lwp *l, const struct linux32_sys_ugetrlimit_args *uap, register_t *retval)
122ee0c5b44Smanu {
1237e2790cfSdsl return linux32_sys_getrlimit(l, (const void *)uap, retval);
124ee0c5b44Smanu }
125b7432513Smanu
126b7432513Smanu int
linux32_sys_prlimit64(struct lwp * l,const struct linux32_sys_prlimit64_args * uap,register_t * retval)127*8f021900Sryo linux32_sys_prlimit64(struct lwp *l, const struct linux32_sys_prlimit64_args *uap, register_t *retval)
128*8f021900Sryo {
129*8f021900Sryo /* {
130*8f021900Sryo syscallarg(pid_t) pid;
131*8f021900Sryo syscallarg(int) which;
132*8f021900Sryo syscallarg(netbsd32_rlimitp_t) new_rlp;
133*8f021900Sryo syscallarg(netbsd32_rlimitp_t) old_rlp;
134*8f021900Sryo }; */
135*8f021900Sryo struct rlimit rl, nrl, orl;
136*8f021900Sryo struct rlimit *p;
137*8f021900Sryo int which;
138*8f021900Sryo int error;
139*8f021900Sryo
140*8f021900Sryo /* XXX: Cannot operate any process other than its own */
141*8f021900Sryo if (SCARG(uap, pid) != 0)
142*8f021900Sryo return EPERM;
143*8f021900Sryo
144*8f021900Sryo which = linux_to_bsd_limit(SCARG(uap, which));
145*8f021900Sryo if (which < 0)
146*8f021900Sryo return -which;
147*8f021900Sryo
148*8f021900Sryo p = SCARG_P32(uap, old_rlp);
149*8f021900Sryo if (p != NULL) {
150*8f021900Sryo memset(&orl, 0, sizeof(orl));
151*8f021900Sryo bsd_to_linux_rlimit64(&orl, &l->l_proc->p_rlimit[which]);
152*8f021900Sryo if ((error = copyout(&orl, p, sizeof(orl))) != 0)
153*8f021900Sryo return error;
154*8f021900Sryo }
155*8f021900Sryo
156*8f021900Sryo p = SCARG_P32(uap, new_rlp);
157*8f021900Sryo if (p != NULL) {
158*8f021900Sryo if ((error = copyin(p, &nrl, sizeof(nrl))) != 0)
159*8f021900Sryo return error;
160*8f021900Sryo
161*8f021900Sryo linux_to_bsd_rlimit(&rl, &nrl);
162*8f021900Sryo return dosetrlimit(l, l->l_proc, which, &rl);
163*8f021900Sryo }
164*8f021900Sryo
165*8f021900Sryo return 0;
166*8f021900Sryo }
167*8f021900Sryo
168*8f021900Sryo int
linux32_sys_getpriority(struct lwp * l,const struct linux32_sys_getpriority_args * uap,register_t * retval)1697e2790cfSdsl linux32_sys_getpriority(struct lwp *l, const struct linux32_sys_getpriority_args *uap, register_t *retval)
170b7432513Smanu {
1717e2790cfSdsl /* {
172b7432513Smanu syscallarg(int) which;
173b7432513Smanu syscallarg(int) who;
1747e2790cfSdsl } */
175b7432513Smanu struct sys_getpriority_args bsa;
176b7432513Smanu int error;
177b7432513Smanu
178b7432513Smanu SCARG(&bsa, which) = SCARG(uap, which);
179b7432513Smanu SCARG(&bsa, who) = SCARG(uap, who);
180b7432513Smanu
181b7432513Smanu if ((error = sys_getpriority(l, &bsa, retval)))
182b7432513Smanu return error;
183b7432513Smanu
184b7432513Smanu *retval = NZERO - *retval;
185b7432513Smanu
186b7432513Smanu return 0;
187b7432513Smanu }
188