1 /*	$OpenBSD: linux_resource.c,v 1.4 2003/06/03 20:49:28 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Niklas Hallqvist
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * Linux "resource" syscall emulation
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mount.h>
35 #include <sys/proc.h>
36 #include <sys/resource.h>
37 #include <sys/resourcevar.h>
38 #include <sys/syscallargs.h>
39 
40 #include <compat/linux/linux_types.h>
41 #include <compat/linux/linux_resource.h>
42 #include <compat/linux/linux_signal.h>
43 #include <compat/linux/linux_syscallargs.h>
44 
45 /* linux_resource.c */
46 int	linux_to_bsd_rlimit(u_int);
47 int	linux_dogetrlimit(struct proc *, void *, register_t *, rlim_t);
48 
49 static u_int linux_to_bsd_rlimit_map[] = {
50 	RLIMIT_CPU,
51 	RLIMIT_FSIZE,
52 	RLIMIT_DATA,
53 	RLIMIT_STACK,
54 	RLIMIT_CORE,
55 	RLIMIT_RSS,
56 	RLIMIT_NPROC,
57 	RLIMIT_NOFILE,
58 	RLIMIT_MEMLOCK,
59 	RLIM_NLIMITS		/* LINUX_RLIMIT_AS not supported */
60 };
61 
62 int
linux_to_bsd_rlimit(which)63 linux_to_bsd_rlimit(which)
64 	u_int which;
65 {
66 	if (which >= LINUX_RLIM_NLIMITS)
67 		return (RLIM_NLIMITS);
68 	return (linux_to_bsd_rlimit_map[which]);
69 }
70 
71 int
linux_sys_setrlimit(p,v,retval)72 linux_sys_setrlimit(p, v, retval)
73 	struct proc *p;
74 	void *v;
75 	register_t *retval;
76 {
77 	struct linux_sys_setrlimit_args /* {
78 		syscallarg(u_int) which;
79 		syscallarg(struct linux_rlimit *) rlp;
80 	} */ *uap = v;
81 
82 	SCARG(uap, which) = linux_to_bsd_rlimit(SCARG(uap, which));
83 	if (SCARG(uap, which) == RLIM_NLIMITS)
84 		return (EINVAL);
85 	return (compat_43_sys_setrlimit(p, v, retval));
86 }
87 
88 int
linux_dogetrlimit(p,v,retval,max)89 linux_dogetrlimit(p, v, retval, max)
90 	struct proc *p;
91 	void *v;
92 	register_t *retval;
93 	rlim_t max;
94 {
95 	struct linux_sys_getrlimit_args /* {
96 		syscallarg(u_int) which;
97 		syscallarg(struct linux_rlimit *) rlp;
98 	} */ *uap = v;
99 	u_int which;
100 	struct linux_rlimit rlim;
101 
102 	which = linux_to_bsd_rlimit(SCARG(uap, which));
103 	if (which == RLIM_NLIMITS)
104 		return (EINVAL);
105 
106 	rlim.rlim_cur = MIN(p->p_rlimit[which].rlim_cur, max);
107 	rlim.rlim_max = MIN(p->p_rlimit[which].rlim_max, max);
108 	return (copyout(&rlim, SCARG(uap, rlp), sizeof rlim));
109 }
110 
111 int
linux_sys_getrlimit(p,v,retval)112 linux_sys_getrlimit(p, v, retval)
113 	struct proc *p;
114 	void *v;
115 	register_t *retval;
116 {
117 	return (linux_dogetrlimit(p, v, retval, LINUX_OLD_RLIM_INFINITY));
118 }
119 
120 int
linux_sys_ugetrlimit(p,v,retval)121 linux_sys_ugetrlimit(p, v, retval)
122 	struct proc *p;
123 	void *v;
124 	register_t *retval;
125 {
126 	return (linux_dogetrlimit(p, v, retval, LINUX_RLIM_INFINITY));
127 }
128