xref: /f-stack/freebsd/sys/procdesc.h (revision 22ce4aff)
1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 2009 Robert N. M. Watson
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * This software was developed at the University of Cambridge Computer
8a9643ea8Slogwang  * Laboratory with support from a grant from Google, Inc.
9a9643ea8Slogwang  *
10a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
11a9643ea8Slogwang  * modification, are permitted provided that the following conditions
12a9643ea8Slogwang  * are met:
13a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
14a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
15a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
16a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
17a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
18a9643ea8Slogwang  *
19a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a9643ea8Slogwang  * SUCH DAMAGE.
30a9643ea8Slogwang  *
31a9643ea8Slogwang  * $FreeBSD$
32a9643ea8Slogwang  */
33a9643ea8Slogwang 
34a9643ea8Slogwang #ifndef _SYS_PROCDESC_H_
35a9643ea8Slogwang #define	_SYS_PROCDESC_H_
36a9643ea8Slogwang 
37a9643ea8Slogwang #ifdef _KERNEL
38a9643ea8Slogwang 
39a9643ea8Slogwang #include <sys/selinfo.h>	/* struct selinfo */
40a9643ea8Slogwang #include <sys/_lock.h>
41a9643ea8Slogwang #include <sys/_mutex.h>
42a9643ea8Slogwang 
43a9643ea8Slogwang /*-
44a9643ea8Slogwang  * struct procdesc describes a process descriptor, and essentially consists
45a9643ea8Slogwang  * of two pointers -- one to the file descriptor, and one to the process.
46a9643ea8Slogwang  * When both become NULL, the process descriptor will be freed.  An important
47a9643ea8Slogwang  * invariant is that there is only ever one process descriptor for a process,
48a9643ea8Slogwang  * so a single file pointer will suffice.
49a9643ea8Slogwang  *
50a9643ea8Slogwang  * Locking key:
51a9643ea8Slogwang  * (c) - Constant after initial setup.
52a9643ea8Slogwang  * (p) - Protected by the process descriptor mutex.
53a9643ea8Slogwang  * (r) - Atomic reference count.
54a9643ea8Slogwang  * (s) - Protected by selinfo.
55a9643ea8Slogwang  * (t) - Protected by the proctree_lock
56a9643ea8Slogwang  */
57a9643ea8Slogwang struct proc;
58a9643ea8Slogwang struct sigio;
59a9643ea8Slogwang struct procdesc {
60a9643ea8Slogwang 	/*
61a9643ea8Slogwang 	 * Basic process descriptor state: the process, a cache of its pid to
62a9643ea8Slogwang 	 * satisfy queries after the process exits, and process descriptor
63a9643ea8Slogwang 	 * refcount.
64a9643ea8Slogwang 	 */
65a9643ea8Slogwang 	struct proc	*pd_proc;		/* (t) Process. */
66a9643ea8Slogwang 	pid_t		 pd_pid;		/* (c) Cached pid. */
67a9643ea8Slogwang 	u_int		 pd_refcount;		/* (r) Reference count. */
68a9643ea8Slogwang 
69a9643ea8Slogwang 	/*
70a9643ea8Slogwang 	 * In-flight data and notification of events.
71a9643ea8Slogwang 	 */
72a9643ea8Slogwang 	int		 pd_flags;		/* (p) PD_ flags. */
73a9643ea8Slogwang 	u_short		 pd_xstat;		/* (p) Exit status. */
74a9643ea8Slogwang 	struct selinfo	 pd_selinfo;		/* (p) Event notification. */
75a9643ea8Slogwang 	struct mtx	 pd_lock;		/* Protect data + events. */
76a9643ea8Slogwang };
77a9643ea8Slogwang 
78a9643ea8Slogwang /*
79a9643ea8Slogwang  * Locking macros for the procdesc itself.
80a9643ea8Slogwang  */
81a9643ea8Slogwang #define	PROCDESC_LOCK_DESTROY(pd)	mtx_destroy(&(pd)->pd_lock)
82a9643ea8Slogwang #define	PROCDESC_LOCK_INIT(pd)	mtx_init(&(pd)->pd_lock, "procdesc", NULL, \
83a9643ea8Slogwang 				    MTX_DEF)
84a9643ea8Slogwang #define	PROCDESC_LOCK(pd)	mtx_lock(&(pd)->pd_lock)
85a9643ea8Slogwang #define	PROCDESC_UNLOCK(pd)	mtx_unlock(&(pd)->pd_lock)
86a9643ea8Slogwang 
87a9643ea8Slogwang /*
88a9643ea8Slogwang  * Flags for the pd_flags field.
89a9643ea8Slogwang  */
90a9643ea8Slogwang #define	PDF_CLOSED	0x00000001	/* Descriptor has closed. */
91a9643ea8Slogwang #define	PDF_SELECTED	0x00000002	/* Issue selwakeup(). */
92a9643ea8Slogwang #define	PDF_EXITED	0x00000004	/* Process exited. */
93a9643ea8Slogwang #define	PDF_DAEMON	0x00000008	/* Don't exit when procdesc closes. */
94a9643ea8Slogwang 
95a9643ea8Slogwang /*
96a9643ea8Slogwang  * In-kernel interfaces to process descriptors.
97a9643ea8Slogwang  */
98a9643ea8Slogwang int	 procdesc_exit(struct proc *);
99a9643ea8Slogwang int	 procdesc_find(struct thread *, int fd, cap_rights_t *, struct proc **);
100a9643ea8Slogwang int	 kern_pdgetpid(struct thread *, int fd, cap_rights_t *, pid_t *pidp);
101a9643ea8Slogwang void	 procdesc_new(struct proc *, int);
102a9643ea8Slogwang void	 procdesc_finit(struct procdesc *, struct file *);
103a9643ea8Slogwang pid_t	 procdesc_pid(struct file *);
104a9643ea8Slogwang void	 procdesc_reap(struct proc *);
105a9643ea8Slogwang 
106a9643ea8Slogwang int	 procdesc_falloc(struct thread *, struct file **, int *, int,
107a9643ea8Slogwang 	    struct filecaps *);
108a9643ea8Slogwang 
109a9643ea8Slogwang #else /* !_KERNEL */
110a9643ea8Slogwang 
111a9643ea8Slogwang #include <sys/_types.h>
112a9643ea8Slogwang 
113a9643ea8Slogwang #ifndef _PID_T_DECLARED
114a9643ea8Slogwang typedef	__pid_t		pid_t;
115a9643ea8Slogwang #define	_PID_T_DECLARED
116a9643ea8Slogwang #endif
117a9643ea8Slogwang 
118a9643ea8Slogwang struct rusage;
119a9643ea8Slogwang 
120a9643ea8Slogwang /*
121a9643ea8Slogwang  * Process descriptor system calls.
122a9643ea8Slogwang  */
123a9643ea8Slogwang __BEGIN_DECLS
124a9643ea8Slogwang pid_t	 pdfork(int *, int);
125a9643ea8Slogwang int	 pdkill(int, int);
126a9643ea8Slogwang int	 pdgetpid(int, pid_t *);
127a9643ea8Slogwang __END_DECLS
128a9643ea8Slogwang 
129a9643ea8Slogwang #endif /* _KERNEL */
130a9643ea8Slogwang 
131a9643ea8Slogwang /*
132a9643ea8Slogwang  * Flags which can be passed to pdfork(2).
133a9643ea8Slogwang  */
134a9643ea8Slogwang #define	PD_DAEMON	0x00000001	/* Don't exit when procdesc closes. */
135a9643ea8Slogwang #define	PD_CLOEXEC	0x00000002	/* Close file descriptor on exec. */
136a9643ea8Slogwang 
137a9643ea8Slogwang #define	PD_ALLOWED_AT_FORK	(PD_DAEMON | PD_CLOEXEC)
138a9643ea8Slogwang 
139a9643ea8Slogwang #endif /* !_SYS_PROCDESC_H_ */
140