xref: /freebsd/sys/sys/rctl.h (revision 9768746b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010 The FreeBSD Foundation
5  *
6  * This software was developed by Edward Tomasz Napierala under sponsorship
7  * from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 /*
34  * Resource Limits.
35  */
36 
37 #ifndef _RCTL_H_
38 #define	_RCTL_H_
39 
40 #include <sys/cdefs.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/_task.h>
44 
45 struct proc;
46 struct uidinfo;
47 struct loginclass;
48 struct prison_racct;
49 struct ucred;
50 struct rctl_rule_link;
51 
52 #ifdef _KERNEL
53 
54 /*
55  * Rules describe an action to be taken when conditions defined
56  * in the rule are met.  There is no global list of rules; instead,
57  * rules are linked to by the racct structures for all the subjects
58  * they apply to - for example, a rule of type "user" is linked to the
59  * appropriate struct uidinfo, and to all the processes of that user.
60  *
61  * 'rr_refcount' is equal to the number of rctl_rule_link structures
62  * pointing to the rule.
63  *
64  * This structure must never change after being added, via rctl_rule_link
65  * structures, to subjects.  In order to change a rule, add a new rule
66  * and remove the previous one.
67  */
68 struct rctl_rule {
69 	int		rr_subject_type;
70 	union {
71 		struct proc		*rs_proc;
72 		struct uidinfo		*rs_uip;
73 		struct loginclass	*rs_loginclass;
74 		struct prison_racct	*rs_prison_racct;
75 	} rr_subject;
76 	int		rr_per;
77 	int		rr_resource;
78 	int		rr_action;
79 	int64_t		rr_amount;
80 	u_int		rr_refcount;
81 	struct task	rr_task;
82 };
83 
84 /*
85  * Allowed values for rr_subject_type and rr_per fields.
86  */
87 #define	RCTL_SUBJECT_TYPE_UNDEFINED	-1
88 #define	RCTL_SUBJECT_TYPE_PROCESS	0x0000
89 #define	RCTL_SUBJECT_TYPE_USER		0x0001
90 #define	RCTL_SUBJECT_TYPE_LOGINCLASS	0x0003
91 #define	RCTL_SUBJECT_TYPE_JAIL		0x0004
92 #define	RCTL_SUBJECT_TYPE_MAX		RCTL_SUBJECT_TYPE_JAIL
93 
94 /*
95  * Allowed values for rr_action field.
96  */
97 #define	RCTL_ACTION_UNDEFINED		-1
98 #define	RCTL_ACTION_SIGHUP		SIGHUP
99 #define	RCTL_ACTION_SIGINT		SIGINT
100 #define	RCTL_ACTION_SIGQUIT		SIGQUIT
101 #define	RCTL_ACTION_SIGILL		SIGILL
102 #define	RCTL_ACTION_SIGTRAP		SIGTRAP
103 #define	RCTL_ACTION_SIGABRT		SIGABRT
104 #define	RCTL_ACTION_SIGEMT		SIGEMT
105 #define	RCTL_ACTION_SIGFPE		SIGFPE
106 #define	RCTL_ACTION_SIGKILL		SIGKILL
107 #define	RCTL_ACTION_SIGBUS		SIGBUS
108 #define	RCTL_ACTION_SIGSEGV		SIGSEGV
109 #define	RCTL_ACTION_SIGSYS		SIGSYS
110 #define	RCTL_ACTION_SIGPIPE		SIGPIPE
111 #define	RCTL_ACTION_SIGALRM		SIGALRM
112 #define	RCTL_ACTION_SIGTERM		SIGTERM
113 #define	RCTL_ACTION_SIGURG		SIGURG
114 #define	RCTL_ACTION_SIGSTOP		SIGSTOP
115 #define	RCTL_ACTION_SIGTSTP		SIGTSTP
116 #define	RCTL_ACTION_SIGCHLD		SIGCHLD
117 #define	RCTL_ACTION_SIGTTIN		SIGTTIN
118 #define	RCTL_ACTION_SIGTTOU		SIGTTOU
119 #define	RCTL_ACTION_SIGIO		SIGIO
120 #define	RCTL_ACTION_SIGXCPU		SIGXCPU
121 #define	RCTL_ACTION_SIGXFSZ		SIGXFSZ
122 #define	RCTL_ACTION_SIGVTALRM		SIGVTALRM
123 #define	RCTL_ACTION_SIGPROF		SIGPROF
124 #define	RCTL_ACTION_SIGWINCH		SIGWINCH
125 #define	RCTL_ACTION_SIGINFO		SIGINFO
126 #define	RCTL_ACTION_SIGUSR1		SIGUSR1
127 #define	RCTL_ACTION_SIGUSR2		SIGUSR2
128 #define	RCTL_ACTION_SIGTHR		SIGTHR
129 #define	RCTL_ACTION_SIGNAL_MAX		RCTL_ACTION_SIGTHR
130 #define	RCTL_ACTION_DENY		(RCTL_ACTION_SIGNAL_MAX + 1)
131 #define	RCTL_ACTION_LOG			(RCTL_ACTION_SIGNAL_MAX + 2)
132 #define	RCTL_ACTION_DEVCTL		(RCTL_ACTION_SIGNAL_MAX + 3)
133 #define	RCTL_ACTION_THROTTLE		(RCTL_ACTION_SIGNAL_MAX + 4)
134 #define	RCTL_ACTION_MAX			RCTL_ACTION_THROTTLE
135 
136 #define	RCTL_AMOUNT_UNDEFINED		-1
137 
138 struct rctl_rule *rctl_rule_alloc(int flags);
139 struct rctl_rule *rctl_rule_duplicate(const struct rctl_rule *rule, int flags);
140 void	rctl_rule_acquire(struct rctl_rule *rule);
141 void	rctl_rule_release(struct rctl_rule *rule);
142 int	rctl_rule_add(struct rctl_rule *rule);
143 int	rctl_rule_remove(struct rctl_rule *filter);
144 int	rctl_enforce(struct proc *p, int resource, uint64_t amount);
145 void	rctl_throttle_decay(struct racct *racct, int resource);
146 int64_t	rctl_pcpu_available(const struct proc *p);
147 uint64_t rctl_get_limit(struct proc *p, int resource);
148 uint64_t rctl_get_available(struct proc *p, int resource);
149 const char *rctl_resource_name(int resource);
150 void	rctl_proc_ucred_changed(struct proc *p, struct ucred *newcred);
151 int	rctl_proc_fork(struct proc *parent, struct proc *child);
152 void	rctl_racct_release(struct racct *racct);
153 #else /* !_KERNEL */
154 
155 /*
156  * Syscall interface.
157  */
158 __BEGIN_DECLS
159 int	rctl_get_racct(const char *inbufp, size_t inbuflen, char *outbufp,
160 	    size_t outbuflen);
161 int	rctl_get_rules(const char *inbufp, size_t inbuflen, char *outbufp,
162 	    size_t outbuflen);
163 int	rctl_get_limits(const char *inbufp, size_t inbuflen, char *outbufp,
164 	    size_t outbuflen);
165 int	rctl_add_rule(const char *inbufp, size_t inbuflen, char *outbufp,
166 	    size_t outbuflen);
167 int	rctl_remove_rule(const char *inbufp, size_t inbuflen, char *outbufp,
168 	    size_t outbuflen);
169 __END_DECLS
170 
171 #endif /* !_KERNEL */
172 
173 #endif /* !_RCTL_H_ */
174