xref: /netbsd-src/sys/compat/linux/common/linux_limit.h (revision eab380320695b8901777f834601151f48dd29933)
1 /* 	$NetBSD: linux_limit.h,v 1.10 2024/10/01 16:41:29 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _LINUX_LIMIT_H_
34 #define _LINUX_LIMIT_H_
35 
36 static int linux_to_bsd_limit(int);
37 
38 #define bsd_to_linux_rlimit64_1(l, b, f) \
39     (l)->f = ((b)->f == RLIM_INFINITY || \
40 	     ((b)->f & 0x8000000000000000UL) != 0) ? \
41     LINUX_RLIM_INFINITY : (b)->f
42 #define bsd_to_linux_rlimit32_1(l, b, f) \
43     (l)->f = ((b)->f == RLIM_INFINITY || \
44 	     ((b)->f & 0xffffffff00000000ULL) != 0) ? \
45     LINUX_RLIM_INFINITY : (int32_t)(b)->f
46 
47 #define bsd_to_linux_rlimit64(l, b) \
48     bsd_to_linux_rlimit64_1(l, b, rlim_cur); \
49     bsd_to_linux_rlimit64_1(l, b, rlim_max)
50 #define bsd_to_linux_rlimit32(l, b) \
51     bsd_to_linux_rlimit32_1(l, b, rlim_cur); \
52     bsd_to_linux_rlimit32_1(l, b, rlim_max)
53 
54 #ifdef LINUX_LARGEFILE64
55 #define bsd_to_linux_rlimit(l, b)	bsd_to_linux_rlimit64(l, b)
56 #else
57 #define bsd_to_linux_rlimit(l, b)	bsd_to_linux_rlimit32(l, b)
58 #endif
59 
60 #define linux_to_bsd_rlimit1(b, l, f) \
61     (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (l)->f
62 #define linux_to_bsd_rlimit(b, l) \
63     linux_to_bsd_rlimit1(b, l, rlim_cur); \
64     linux_to_bsd_rlimit1(b, l, rlim_max)
65 
66 #define linux32_to_bsd_rlimit1(b, l, f) \
67     (b)->f = (l)->f == LINUX32_RLIM_INFINITY ? RLIM_INFINITY : (uint32_t)(l)->f
68 #define linux32_to_bsd_rlimit(b, l) \
69     linux32_to_bsd_rlimit1(b, l, rlim_cur); \
70     linux32_to_bsd_rlimit1(b, l, rlim_max)
71 
72 static int
73 linux_to_bsd_limit(int lim)
74 {
75 	switch (lim) {
76 	case LINUX_RLIMIT_CPU:
77 		return RLIMIT_CPU;
78 	case LINUX_RLIMIT_FSIZE:
79 		return RLIMIT_FSIZE;
80 	case LINUX_RLIMIT_DATA:
81 		return RLIMIT_DATA;
82 	case LINUX_RLIMIT_STACK:
83 		return RLIMIT_STACK;
84 	case LINUX_RLIMIT_CORE:
85 		return RLIMIT_CORE;
86 	case LINUX_RLIMIT_RSS:
87 		return RLIMIT_RSS;
88 	case LINUX_RLIMIT_NPROC:
89 		return RLIMIT_NPROC;
90 	case LINUX_RLIMIT_NOFILE:
91 		return RLIMIT_NOFILE;
92 	case LINUX_RLIMIT_MEMLOCK:
93 		return RLIMIT_MEMLOCK;
94 	case LINUX_RLIMIT_AS:
95 		return RLIMIT_AS;
96 	case LINUX_RLIMIT_LOCKS:
97 	case LINUX_RLIMIT_SIGPENDING:
98 	case LINUX_RLIMIT_MSGQUEUE:
99 	case LINUX_RLIMIT_NICE:
100 	case LINUX_RLIMIT_RTPRIO:
101 	case LINUX_RLIMIT_RTTIME:
102 		return -EOPNOTSUPP;
103 	default:
104 		return -EINVAL;
105 	}
106 }
107 
108 
109 #endif /* _LINUX_LIMIT_H_ */
110