1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2025 James Gritton.
5 * All rights reserved.
6 *
7 * This software was developed at the University of Cambridge Computer
8 * Laboratory with support from a grant from Google, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef _SYS_JAILDESC_H_
33#define _SYS_JAILDESC_H_
34
35#ifdef _KERNEL
36
37#include <sys/queue.h>
38#include <sys/selinfo.h>
39#include <sys/_lock.h>
40#include <sys/_mutex.h>
41#include <sys/_types.h>
42
43struct prison;
44
45/*-
46 * struct jaildesc describes a jail descriptor, which points to a struct
47 * prison. struct prison in turn has a linked list of struct jaildesc.
48 *
49 * Locking key:
50 * (c) set on creation, remains unchanged
51 * (d) jd_lock
52 * (p) jd_prison->pr_mtx
53 */
54struct jaildesc {
55 LIST_ENTRY(jaildesc) jd_list; /* (d,p) this prison's descs */
56 struct prison *jd_prison; /* (d) the prison */
57 struct mtx jd_lock;
58 struct selinfo jd_selinfo; /* (d) event notification */
59 unsigned jd_flags; /* (d) JDF_* flags */
60};
61
62/*
63 * Locking macros for the jaildesc.
64 */
65#define JAILDESC_LOCK_DESTROY(jd) mtx_destroy(&(jd)->jd_lock)
66#define JAILDESC_LOCK_INIT(jd) mtx_init(&(jd)->jd_lock, "jaildesc", \
67 NULL, MTX_DEF)
68#define JAILDESC_LOCK(jd) mtx_lock(&(jd)->jd_lock)
69#define JAILDESC_UNLOCK(jd) mtx_unlock(&(jd)->jd_lock)
70
71/*
72 * Flags for the jd_flags field
73 */
74#define JDF_SELECTED 0x00000001 /* issue selwakeup() */
75#define JDF_REMOVED 0x00000002 /* jail was removed */
76#define JDF_OWNING 0x00000004 /* closing descriptor removes jail */
77
78int jaildesc_find(struct thread *td, int fd, struct prison **prp,
79 struct ucred **ucredp);
80int jaildesc_alloc(struct thread *td, struct file **fpp, int *fdp, int owning);
81void jaildesc_set_prison(struct file *jd, struct prison *pr);
82void jaildesc_prison_cleanup(struct prison *pr);
83void jaildesc_knote(struct prison *pr, long hint);
84
85#endif /* _KERNEL */
86
87#endif /* !_SYS_JAILDESC_H_ */