xref: /illumos-gate/usr/src/uts/common/sys/rctl.h (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_RCTL_H
28 #define	_SYS_RCTL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/kmem.h>
33 #include <sys/resource.h>
34 #include <sys/types.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /*
41  * Available local actions and flags.
42  */
43 #define	RCTL_LOCAL_NOACTION		0x00000000
44 #define	RCTL_LOCAL_SIGNAL		0x00000001
45 #define	RCTL_LOCAL_DENY			0x00000002
46 
47 #define	RCTL_LOCAL_MAXIMAL		0x80000000
48 
49 #define	RCTL_LOCAL_ACTION_MASK		0xffff0000
50 #define	RCTL_LOCAL_MASK			0x80000003
51 
52 /*
53  * Available global actions and flags.
54  */
55 #define	RCTL_GLOBAL_NOACTION		0x00000000
56 #define	RCTL_GLOBAL_SYSLOG		0x00000001
57 
58 #define	RCTL_GLOBAL_NOBASIC		0x80000000
59 #define	RCTL_GLOBAL_LOWERABLE		0x40000000
60 #define	RCTL_GLOBAL_DENY_ALWAYS		0x20000000
61 #define	RCTL_GLOBAL_DENY_NEVER		0x10000000
62 #define	RCTL_GLOBAL_FILE_SIZE		0x08000000
63 #define	RCTL_GLOBAL_CPU_TIME		0x04000000
64 #define	RCTL_GLOBAL_SIGNAL_NEVER	0x02000000
65 #define	RCTL_GLOBAL_NOLOCALACTION	RCTL_GLOBAL_SIGNAL_NEVER
66 #define	RCTL_GLOBAL_INFINITE		0x01000000
67 #define	RCTL_GLOBAL_UNOBSERVABLE	0x00800000
68 
69 #define	RCTL_GLOBAL_BYTES		0x00400000
70 #define	RCTL_GLOBAL_SECONDS		0x00200000
71 #define	RCTL_GLOBAL_COUNT		0x00100000
72 
73 #define	RCTL_GLOBAL_ACTION_MASK		0xffff0000
74 #define	RCTL_GLOBAL_MASK		0xff700001
75 
76 /*
77  * getrctl(2) flag values
78  */
79 #define	RCTL_FIRST		0x00000000
80 #define	RCTL_NEXT		0x00000001
81 #define	RCTL_USAGE		0x00000002
82 
83 /*
84  * setrctl(2) flag values
85  */
86 
87 #define	RCTL_INSERT		0x00000000
88 #define	RCTL_DELETE		0x00000001
89 #define	RCTL_REPLACE		0x00000002
90 
91 #define	RCTL_USE_RECIPIENT_PID	0x10000000
92 
93 #define	RCTLSYS_ACTION_MASK 	0xffff0000
94 #define	RCTLSYS_MASK		0x10000003
95 
96 /*
97  * rctl_priv_t: rctl privilege defined values
98  *   A large amount of space has been deliberately left between these privileges
99  *   to permit future enrichment of the control privilege value.
100  */
101 #define	RCPRIV_BASIC		0x01000000
102 #define	RCPRIV_PRIVILEGED	0x04000000
103 #define	RCPRIV_SYSTEM		0x07000000
104 
105 typedef u_longlong_t rctl_qty_t; /* resource control numerical values   */
106 typedef int rctl_priv_t;
107 
108 typedef struct rctlblk rctlblk_t;
109 
110 extern int setrctl(const char *, rctlblk_t *, rctlblk_t *, int);
111 extern int getrctl(const char *, rctlblk_t *, rctlblk_t *, int);
112 
113 typedef enum {
114 	RCENTITY_PROCESS,
115 	RCENTITY_TASK,
116 	RCENTITY_PROJECT,
117 	RCENTITY_ZONE
118 } rctl_entity_t;
119 #define	RC_MAX_ENTITY RCENTITY_ZONE
120 
121 #ifndef _KERNEL
122 
123 typedef struct rctl_set rctl_set_t;
124 
125 #else /* _KERNEL */
126 
127 #include <sys/mutex.h>
128 
129 /*
130  * rctl_test return bitfield
131  */
132 #define	RCT_NONE		0x00000000
133 #define	RCT_DENY		0x00000001
134 #define	RCT_SIGNAL		0x00000002
135 #define	RCT_STRLOG		0x00000004
136 
137 #define	RCT_LK_ABANDONED	0x80000000
138 
139 /*
140  * rctl_set_dup flags
141  */
142 #define	RCD_DUP			0x1
143 #define	RCD_CALLBACK		0x2
144 
145 /*
146  * rctl_action/rctl_test action safety states
147  */
148 #define	RCA_SAFE		0x0 /* safe for signal and siginfo delivery */
149 #define	RCA_UNSAFE_SIGINFO	0x1 /* not safe to allocate for siginfo */
150 #define	RCA_UNSAFE_ALL		0x2 /* not safe to send signal */
151 
152 typedef struct rctl_val {
153 	struct rctl_val *rcv_prev;		/* previous (lower) value */
154 	struct rctl_val *rcv_next;		/* next (higher) value */
155 	rctl_priv_t	rcv_privilege;		/* appropriate RCPRIV_* cst */
156 	rctl_qty_t	rcv_value;		/* enforced value of control */
157 	uint_t		rcv_flagaction;		/* properties and actions */
158 	int		rcv_action_signal;	/* signal to send as action */
159 	struct proc	*rcv_action_recipient;	/* process to receive signal */
160 	id_t		rcv_action_recip_pid;	/* pid of that process */
161 	hrtime_t	rcv_firing_time;	/* time rctl_val last fired */
162 } rctl_val_t;
163 
164 typedef int rctl_hndl_t;
165 
166 struct rctl;
167 struct proc;
168 struct task;
169 struct kproject;
170 struct zone;
171 
172 typedef struct rctl_entity_p_struct {
173 	rctl_entity_t rcep_t;
174 	union {
175 		struct proc *proc;
176 		struct task *task;
177 		struct kproject *proj;
178 		struct zone *zone;
179 	} rcep_p;
180 } rctl_entity_p_t;
181 
182 typedef struct rctl_ops {
183 	void		(*rco_action)(struct rctl *, struct proc *,
184 	    rctl_entity_p_t *);
185 	rctl_qty_t	(*rco_get_usage)(struct rctl *, struct proc *);
186 	int		(*rco_set)(struct rctl *, struct proc *,
187 	    rctl_entity_p_t *, rctl_qty_t);
188 	int		(*rco_test)(struct rctl *, struct proc *,
189 	    rctl_entity_p_t *, rctl_val_t *, rctl_qty_t, uint_t);
190 } rctl_ops_t;
191 
192 #define	RCTLOP_ACTION(r, p, e) (r->rc_dict_entry->rcd_ops->rco_action(r, p, e))
193 #define	RCTLOP_GET_USAGE(r, p) (r->rc_dict_entry->rcd_ops->rco_get_usage(r, p))
194 #define	RCTLOP_SET(r, p, e, v) (r->rc_dict_entry->rcd_ops->rco_set(r, p, e, v))
195 #define	RCTLOP_TEST(r, p, e, v, i, f) \
196 	(r->rc_dict_entry->rcd_ops->rco_test(r, p, e, v, i, f))
197 
198 /*
199  * Default resource control callback functions.
200  */
201 void rcop_no_action(struct rctl *, struct proc *, rctl_entity_p_t *);
202 rctl_qty_t rcop_no_usage(struct rctl *, struct proc *);
203 int rcop_no_set(struct rctl *, struct proc *, rctl_entity_p_t *, rctl_qty_t);
204 int rcop_no_test(struct rctl *, struct proc *, rctl_entity_p_t *,
205     struct rctl_val *, rctl_qty_t, uint_t);
206 int rcop_absolute_test(struct rctl *, struct proc *, rctl_entity_p_t *,
207     struct rctl_val *, rctl_qty_t, uint_t);
208 
209 extern rctl_ops_t rctl_default_ops;
210 extern rctl_ops_t rctl_absolute_ops;
211 
212 typedef struct rctl {
213 	struct rctl	*rc_next;		/* next in set hash chain    */
214 	rctl_val_t	*rc_values;		/* list of enforced value    */
215 	rctl_val_t	*rc_cursor;		/* currently enforced value  */
216 	struct rctl_dict_entry *rc_dict_entry;	/* global control properties */
217 	rctl_hndl_t	rc_id;			/* control handle (hash key) */
218 } rctl_t;
219 
220 /*
221  * The rctl_set is the collection of resource controls associated with an
222  * individual entity within the system.  All of the controls are applicable to
223  * the same entity, which we call out explicitly in rcs_entity.
224  */
225 typedef struct rctl_set {
226 	kmutex_t	rcs_lock;		/* global set lock	  */
227 	rctl_entity_t	rcs_entity;		/* entity type		  */
228 	rctl_t		**rcs_ctls;		/* hash table of controls */
229 } rctl_set_t;
230 
231 typedef struct rctl_dict_entry {
232 	struct rctl_dict_entry *rcd_next;	/* next in dict hash chain */
233 	char		*rcd_name;		/* resource control name */
234 	rctl_val_t	*rcd_default_value;	/* system control value */
235 	rctl_ops_t	*rcd_ops;		/* callback operations */
236 	rctl_hndl_t	rcd_id;			/* control handle */
237 	rctl_entity_t	rcd_entity;		/* entity type */
238 	int		rcd_flagaction;		/* global properties/actions */
239 	int		rcd_syslog_level;	/* event syslog level */
240 	int		rcd_strlog_flags;	/* derived from syslog level */
241 	rctl_qty_t	rcd_max_native;		/* native model "infinity" */
242 	rctl_qty_t	rcd_max_ilp32;		/* ILP32 model "infinity" */
243 } rctl_dict_entry_t;
244 
245 typedef struct rctl_alloc_gp {
246 	uint_t	rcag_nctls;	/* number of rctls needed/allocated */
247 	uint_t	rcag_nvals;	/* number of rctl values needed/allocated */
248 	rctl_t	*rcag_ctls;	/* list of allocated rctls */
249 	rctl_val_t *rcag_vals;	/* list of allocated rctl values */
250 } rctl_alloc_gp_t;
251 
252 extern kmem_cache_t *rctl_cache;	/* kmem cache for rctl structures */
253 extern kmem_cache_t *rctl_val_cache;	/* kmem cache for rctl values */
254 
255 extern rctl_hndl_t rctlproc_legacy[];
256 extern uint_t rctlproc_flags[];
257 extern int rctlproc_signals[];
258 
259 void rctl_init(void);
260 void rctlproc_init(void);
261 void rctlproc_default_init(struct proc *, rctl_alloc_gp_t *);
262 
263 rctl_hndl_t rctl_register(const char *, rctl_entity_t, int, rctl_qty_t,
264     rctl_qty_t, rctl_ops_t *);
265 
266 rctl_hndl_t rctl_hndl_lookup(const char *);
267 rctl_dict_entry_t *rctl_dict_lookup(const char *);
268 rctl_dict_entry_t *rctl_dict_lookup_hndl(rctl_hndl_t);
269 void rctl_add_default_limit(const char *, rctl_qty_t, rctl_priv_t, uint_t);
270 void rctl_add_legacy_limit(const char *, const char *, const char *,
271     rctl_qty_t, rctl_qty_t);
272 
273 rctl_qty_t rctl_model_maximum(rctl_dict_entry_t *, struct proc *);
274 rctl_qty_t rctl_model_value(rctl_dict_entry_t *, struct proc *, rctl_qty_t);
275 
276 int rctl_invalid_value(rctl_dict_entry_t *, rctl_val_t *);
277 rctl_qty_t rctl_enforced_value(rctl_hndl_t, rctl_set_t *, struct proc *);
278 
279 int rctl_test(rctl_hndl_t, rctl_set_t *, struct proc *, rctl_qty_t, uint_t);
280 int rctl_action(rctl_hndl_t, rctl_set_t *, struct proc *, uint_t);
281 
282 int rctl_test_entity(rctl_hndl_t, rctl_set_t *, struct proc *,
283     rctl_entity_p_t *, rctl_qty_t, uint_t);
284 int rctl_action_entity(rctl_hndl_t, rctl_set_t *, struct proc *,
285     rctl_entity_p_t *, uint_t);
286 
287 int rctl_val_cmp(rctl_val_t *, rctl_val_t *, int);
288 int rctl_val_list_insert(rctl_val_t **, rctl_val_t *);
289 
290 rctl_set_t *rctl_set_create(void);
291 rctl_alloc_gp_t *rctl_set_init_prealloc(rctl_entity_t);
292 rctl_set_t *rctl_set_init(rctl_entity_t, struct proc *, rctl_entity_p_t *,
293     rctl_set_t *, rctl_alloc_gp_t *);
294 rctl_alloc_gp_t *rctl_set_dup_prealloc(rctl_set_t *);
295 int rctl_set_dup_ready(rctl_set_t *, rctl_alloc_gp_t *);
296 rctl_set_t *rctl_set_dup(rctl_set_t *, struct proc *, struct proc *,
297     rctl_entity_p_t *, rctl_set_t *, rctl_alloc_gp_t *, int);
298 void rctl_set_reset(rctl_set_t *, struct proc *, rctl_entity_p_t *);
299 void rctl_set_tearoff(rctl_set_t *, struct proc *);
300 void rctl_set_free(rctl_set_t *);
301 
302 void rctl_prealloc_destroy(rctl_alloc_gp_t *);
303 
304 size_t rctl_build_name_buf(char **);
305 
306 int rctl_global_get(const char *name, rctl_dict_entry_t *);
307 int rctl_global_set(const char *name, rctl_dict_entry_t *);
308 
309 int rctl_local_delete(rctl_hndl_t, rctl_val_t *, struct proc *p);
310 int rctl_local_insert(rctl_hndl_t, rctl_val_t *, struct proc *p);
311 int rctl_local_get(rctl_hndl_t, rctl_val_t *, rctl_val_t *, struct proc *p);
312 int rctl_local_replace(rctl_hndl_t, rctl_val_t *, rctl_val_t *,
313     struct proc *p);
314 
315 /* tag declaration to appease the compiler */
316 struct cred;
317 rctl_alloc_gp_t *rctl_rlimit_set_prealloc(uint_t);
318 int rctl_rlimit_set(rctl_hndl_t, struct proc *, struct rlimit64 *,
319     rctl_alloc_gp_t *, int, int, const struct cred *);
320 int rctl_rlimit_get(rctl_hndl_t, struct proc *, struct rlimit64 *);
321 
322 #endif /* _KERNEL */
323 
324 #ifdef	__cplusplus
325 }
326 #endif
327 
328 #endif	/* _SYS_RCTL_H */
329