| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * |
| 4 | * Copyright (c) 1991, 1993 |
| 5 | *	The Regents of the University of California. All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. Neither the name of the University nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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_SIGNALVAR_H_ |
| 33 | #define	_SYS_SIGNALVAR_H_ |
| 34 | |
| 35 | #include <sys/queue.h> |
| 36 | #include <sys/_lock.h> |
| 37 | #include <sys/_mutex.h> |
| 38 | #include <sys/signal.h> |
| 39 | |
| 40 | /* |
| 41 | * Kernel signal definitions and data structures. |
| 42 | */ |
| 43 | |
| 44 | /* |
| 45 | * Logical process signal actions and state, needed only within the process |
| 46 | * The mapping between sigacts and proc structures is 1:1 except for rfork() |
| 47 | * processes masquerading as threads which use one structure for the whole |
| 48 | * group. All members are locked by the included mutex. The reference count |
| 49 | * and mutex must be last for the bcopy in sigacts_copy() to work. |
| 50 | */ |
| 51 | struct sigacts { |
| 52 | 	sig_t	ps_sigact[_SIG_MAXSIG];	/* Disposition of signals. */ |
| 53 | 	sigset_t ps_catchmask[_SIG_MAXSIG];	/* Signals to be blocked. */ |
| 54 | 	sigset_t ps_sigonstack;		/* Signals to take on sigstack. */ |
| 55 | 	sigset_t ps_sigintr;		/* Signals that interrupt syscalls. */ |
| 56 | 	sigset_t ps_sigreset;		/* Signals that reset when caught. */ |
| 57 | 	sigset_t ps_signodefer;		/* Signals not masked while handled. */ |
| 58 | 	sigset_t ps_siginfo;		/* Signals that want SA_SIGINFO args. */ |
| 59 | 	sigset_t ps_sigignore;		/* Signals being ignored. */ |
| 60 | 	sigset_t ps_sigcatch;		/* Signals being caught by user. */ |
| 61 | 	sigset_t ps_freebsd4;		/* Signals using freebsd4 ucontext. */ |
| 62 | 	sigset_t ps_osigset;		/* Signals using <= 3.x osigset_t. */ |
| 63 | 	sigset_t ps_usertramp;		/* SunOS compat; libc sigtramp. XXX */ |
| 64 | 	int	ps_flag; |
| 65 | 	u_int	ps_refcnt; |
| 66 | 	struct mtx ps_mtx; |
| 67 | }; |
| 68 | |
| 69 | #define	PS_NOCLDWAIT	0x0001	/* No zombies if child dies */ |
| 70 | #define	PS_NOCLDSTOP	0x0002	/* No SIGCHLD when children stop. */ |
| 71 | #define	PS_CLDSIGIGN	0x0004	/* The SIGCHLD handler is SIG_IGN. */ |
| 72 | |
| 73 | #ifdef _KERNEL |
| 74 | |
| 75 | #ifdef COMPAT_43 |
| 76 | typedef struct { |
| 77 | 	struct osigcontext si_sc; |
| 78 | 	int		si_signo; |
| 79 | 	int		si_code; |
| 80 | 	union sigval	si_value; |
| 81 | } osiginfo_t; |
| 82 | |
| 83 | struct osigaction { |
| 84 | 	union { |
| 85 | 		void (*__sa_handler)(int); |
| 86 | 		void (*__sa_sigaction)(int, osiginfo_t *, void *); |
| 87 | 	} __sigaction_u;		/* signal handler */ |
| 88 | 	osigset_t	sa_mask;	/* signal mask to apply */ |
| 89 | 	int		sa_flags;	/* see signal options below */ |
| 90 | }; |
| 91 | |
| 92 | typedef void __osiginfohandler_t(int, osiginfo_t *, void *); |
| 93 | #endif /* COMPAT_43 */ |
| 94 | |
| 95 | /* additional signal action values, used only temporarily/internally */ |
| 96 | #define	SIG_CATCH	((__sighandler_t *)2) |
| 97 | /* #define SIG_HOLD ((__sighandler_t *)3) See signal.h */ |
| 98 | |
| 99 | /* |
| 100 | * get signal action for process and signal; currently only for current process |
| 101 | */ |
| 102 | #define	SIGACTION(p, sig)	(p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) |
| 103 | |
| 104 | #endif /* _KERNEL */ |
| 105 | |
| 106 | /* |
| 107 | * sigset_t manipulation macros. |
| 108 | */ |
| 109 | #define	SIGADDSET(set, signo)						\ |
| 110 | 	((set).__bits[_SIG_WORD(signo)] |= _SIG_BIT(signo)) |
| 111 | |
| 112 | #define	SIGDELSET(set, signo)						\ |
| 113 | 	((set).__bits[_SIG_WORD(signo)] &= ~_SIG_BIT(signo)) |
| 114 | |
| 115 | #define	SIGEMPTYSET(set)						\ |
| 116 | 	do {								\ |
| 117 | 		int __i;						\ |
| 118 | 		for (__i = 0; __i < _SIG_WORDS; __i++)			\ |
| 119 | 			(set).__bits[__i] = 0;				\ |
| 120 | 	} while (0) |
| 121 | |
| 122 | #define	SIGFILLSET(set)							\ |
| 123 | 	do {								\ |
| 124 | 		int __i;						\ |
| 125 | 		for (__i = 0; __i < _SIG_WORDS; __i++)			\ |
| 126 | 			(set).__bits[__i] = ~0U;			\ |
| 127 | 	} while (0) |
| 128 | |
| 129 | #define	SIGISMEMBER(set, signo)						\ |
| 130 | 	((set).__bits[_SIG_WORD(signo)] & _SIG_BIT(signo)) |
| 131 | |
| 132 | #define	SIGISEMPTY(set)		(__sigisempty(&(set))) |
| 133 | #define	SIGNOTEMPTY(set)	(!__sigisempty(&(set))) |
| 134 | |
| 135 | #define	SIGSETEQ(set1, set2)	(__sigseteq(&(set1), &(set2))) |
| 136 | #define	SIGSETNEQ(set1, set2)	(!__sigseteq(&(set1), &(set2))) |
| 137 | |
| 138 | #define	SIGSETOR(set1, set2)						\ |
| 139 | 	do {								\ |
| 140 | 		int __i;						\ |
| 141 | 		for (__i = 0; __i < _SIG_WORDS; __i++)			\ |
| 142 | 			(set1).__bits[__i] |= (set2).__bits[__i];	\ |
| 143 | 	} while (0) |
| 144 | |
| 145 | #define	SIGSETAND(set1, set2)						\ |
| 146 | 	do {								\ |
| 147 | 		int __i;						\ |
| 148 | 		for (__i = 0; __i < _SIG_WORDS; __i++)			\ |
| 149 | 			(set1).__bits[__i] &= (set2).__bits[__i];	\ |
| 150 | 	} while (0) |
| 151 | |
| 152 | #define	SIGSETNAND(set1, set2)						\ |
| 153 | 	do {								\ |
| 154 | 		int __i;						\ |
| 155 | 		for (__i = 0; __i < _SIG_WORDS; __i++)			\ |
| 156 | 			(set1).__bits[__i] &= ~(set2).__bits[__i];	\ |
| 157 | 	} while (0) |
| 158 | |
| 159 | #define	SIGSETLO(set1, set2)	((set1).__bits[0] = (set2).__bits[0]) |
| 160 | #define	SIGSETOLD(set, oset)	((set).__bits[0] = (oset)) |
| 161 | |
| 162 | #define	SIG_CANTMASK(set)						\ |
| 163 | 	SIGDELSET(set, SIGKILL), SIGDELSET(set, SIGSTOP) |
| 164 | |
| 165 | #define	SIG_STOPSIGMASK(set)						\ |
| 166 | 	SIGDELSET(set, SIGSTOP), SIGDELSET(set, SIGTSTP),		\ |
| 167 | 	SIGDELSET(set, SIGTTIN), SIGDELSET(set, SIGTTOU) |
| 168 | |
| 169 | #define	SIG_CONTSIGMASK(set)						\ |
| 170 | 	SIGDELSET(set, SIGCONT) |
| 171 | |
| 172 | #define	sigcantmask	(sigmask(SIGKILL) | sigmask(SIGSTOP)) |
| 173 | |
| 174 | #define	SIG2OSIG(sig, osig)	(osig = (sig).__bits[0]) |
| 175 | #define	OSIG2SIG(osig, sig)	SIGEMPTYSET(sig); (sig).__bits[0] = osig |
| 176 | |
| 177 | static __inline int |
| 178 | __sigisempty(sigset_t *set) |
| 179 | { |
| 180 | 	int i; |
| 181 | |
| 182 | 	for (i = 0; i < _SIG_WORDS; i++) { |
| 183 | 		if (set->__bits[i]) |
| 184 | 			return (0); |
| 185 | 	} |
| 186 | 	return (1); |
| 187 | } |
| 188 | |
| 189 | static __inline int |
| 190 | __sigseteq(sigset_t *set1, sigset_t *set2) |
| 191 | { |
| 192 | 	int i; |
| 193 | |
| 194 | 	for (i = 0; i < _SIG_WORDS; i++) { |
| 195 | 		if (set1->__bits[i] != set2->__bits[i]) |
| 196 | 			return (0); |
| 197 | 	} |
| 198 | 	return (1); |
| 199 | } |
| 200 | |
| 201 | #ifdef COMPAT_FREEBSD6 |
| 202 | struct osigevent { |
| 203 | 	int	sigev_notify;		/* Notification type */ |
| 204 | 	union { |
| 205 | 		int	__sigev_signo;	/* Signal number */ |
| 206 | 		int	__sigev_notify_kqueue; |
| 207 | 	} __sigev_u; |
| 208 | 	union sigval sigev_value;	/* Signal value */ |
| 209 | }; |
| 210 | #endif |
| 211 | |
| 212 | typedef struct ksiginfo { |
| 213 | 	TAILQ_ENTRY(ksiginfo)	ksi_link; |
| 214 | 	siginfo_t		ksi_info; |
| 215 | 	int			ksi_flags; |
| 216 | 	struct sigqueue		*ksi_sigq; |
| 217 | } ksiginfo_t; |
| 218 | |
| 219 | #define	ksi_signo	ksi_info.si_signo |
| 220 | #define	ksi_errno	ksi_info.si_errno |
| 221 | #define	ksi_code	ksi_info.si_code |
| 222 | #define	ksi_pid		ksi_info.si_pid |
| 223 | #define	ksi_uid		ksi_info.si_uid |
| 224 | #define	ksi_status ksi_info.si_status |
| 225 | #define	ksi_addr ksi_info.si_addr |
| 226 | #define	ksi_value	ksi_info.si_value |
| 227 | #define	ksi_band	ksi_info.si_band |
| 228 | #define	ksi_trapno	ksi_info.si_trapno |
| 229 | #define	ksi_overrun	ksi_info.si_overrun |
| 230 | #define	ksi_timerid	ksi_info.si_timerid |
| 231 | #define	ksi_mqd		ksi_info.si_mqd |
| 232 | |
| 233 | /* bits for ksi_flags */ |
| 234 | #define	KSI_TRAP	0x01	/* Generated by trap. */ |
| 235 | #define	KSI_EXT		0x02	/* Externally managed ksi. */ |
| 236 | #define	KSI_INS		0x04	/* Directly insert ksi, not the copy */ |
| 237 | #define	KSI_SIGQ	0x08	/* Generated by sigqueue, might ret EAGAIN. */ |
| 238 | #define	KSI_HEAD	0x10	/* Insert into head, not tail. */ |
| 239 | #define	KSI_PTRACE	0x20	/* Generated by ptrace. */ |
| 240 | #define	KSI_COPYMASK	(KSI_TRAP | KSI_SIGQ | KSI_PTRACE) |
| 241 | |
| 242 | #define	KSI_ONQ(ksi)	((ksi)->ksi_sigq != NULL) |
| 243 | |
| 244 | typedef struct sigqueue { |
| 245 | 	sigset_t	sq_signals;	/* All pending signals. */ |
| 246 | 	sigset_t	sq_kill;	/* Legacy depth 1 queue. */ |
| 247 | 	sigset_t	sq_ptrace;	/* Depth 1 queue for ptrace(2). */ |
| 248 | 	TAILQ_HEAD(, ksiginfo)	sq_list;/* Queued signal info. */ |
| 249 | 	struct proc	*sq_proc; |
| 250 | 	int		sq_flags; |
| 251 | } sigqueue_t; |
| 252 | |
| 253 | /* Flags for ksi_flags */ |
| 254 | #define	SQ_INIT	0x01 |
| 255 | |
| 256 | /* |
| 257 | * Fast_sigblock |
| 258 | */ |
| 259 | #define	SIGFASTBLOCK_SETPTR	1 |
| 260 | #define	SIGFASTBLOCK_UNBLOCK	2 |
| 261 | #define	SIGFASTBLOCK_UNSETPTR	3 |
| 262 | |
| 263 | #define	SIGFASTBLOCK_PEND	0x1 |
| 264 | #define	SIGFASTBLOCK_FLAGS	0xf |
| 265 | #define	SIGFASTBLOCK_INC	0x10 |
| 266 | |
| 267 | #ifndef _KERNEL |
| 268 | int __sys_sigfastblock(int cmd, void *ptr); |
| 269 | #endif |
| 270 | |
| 271 | #ifdef _KERNEL |
| 272 | extern bool sigfastblock_fetch_always; |
| 273 | extern bool pt_attach_transparent; |
| 274 | |
| 275 | /* Return nonzero if process p has an unmasked pending signal. */ |
| 276 | #define	SIGPENDING(td)							\ |
| 277 | 	((!SIGISEMPTY((td)->td_siglist) &&				\ |
| 278 | 	 !sigsetmasked(&(td)->td_siglist, &(td)->td_sigmask)) ||	\ |
| 279 | 	 (!SIGISEMPTY((td)->td_proc->p_siglist) &&			\ |
| 280 | 	 !sigsetmasked(&(td)->td_proc->p_siglist, &(td)->td_sigmask))) |
| 281 | /* |
| 282 | * Return the value of the pseudo-expression ((*set & ~*mask) == 0). This |
| 283 | * is an optimized version of SIGISEMPTY() on a temporary variable |
| 284 | * containing SIGSETNAND(*set, *mask). |
| 285 | */ |
| 286 | static __inline bool |
| 287 | sigsetmasked(sigset_t *set, sigset_t *mask) |
| 288 | { |
| 289 | 	int i; |
| 290 | |
| 291 | 	for (i = 0; i < _SIG_WORDS; i++) { |
| 292 | 		if (set->__bits[i] & ~mask->__bits[i]) |
| 293 | 			return (false); |
| 294 | 	} |
| 295 | 	return (true); |
| 296 | } |
| 297 | |
| 298 | #define	ksiginfo_init(ksi)			\ |
| 299 | do {						\ |
| 300 | 	bzero(ksi, sizeof(ksiginfo_t));		\ |
| 301 | } while (0) |
| 302 | |
| 303 | #define	ksiginfo_init_trap(ksi)			\ |
| 304 | do {						\ |
| 305 | 	ksiginfo_t *kp = ksi;			\ |
| 306 | 	bzero(kp, sizeof(ksiginfo_t));		\ |
| 307 | 	kp->ksi_flags |= KSI_TRAP;		\ |
| 308 | } while (0) |
| 309 | |
| 310 | static __inline void |
| 311 | ksiginfo_copy(ksiginfo_t *src, ksiginfo_t *dst) |
| 312 | { |
| 313 | 	(dst)->ksi_info = src->ksi_info; |
| 314 | 	(dst)->ksi_flags = (src->ksi_flags & KSI_COPYMASK); |
| 315 | } |
| 316 | |
| 317 | static __inline void |
| 318 | ksiginfo_set_sigev(ksiginfo_t *dst, struct sigevent *sigev) |
| 319 | { |
| 320 | 	dst->ksi_signo = sigev->sigev_signo; |
| 321 | 	dst->ksi_value = sigev->sigev_value; |
| 322 | } |
| 323 | |
| 324 | struct pgrp; |
| 325 | struct proc; |
| 326 | struct sigio; |
| 327 | struct thread; |
| 328 | |
| 329 | /* |
| 330 | * Lock the pointers for a sigio object in the underlying objects of |
| 331 | * a file descriptor. |
| 332 | */ |
| 333 | #define	SIGIO_LOCK()	mtx_lock(&sigio_lock) |
| 334 | #define	SIGIO_TRYLOCK()	mtx_trylock(&sigio_lock) |
| 335 | #define	SIGIO_UNLOCK()	mtx_unlock(&sigio_lock) |
| 336 | #define	SIGIO_LOCKED()	mtx_owned(&sigio_lock) |
| 337 | #define	SIGIO_ASSERT_LOCKED() mtx_assert(&sigio_lock, MA_OWNED) |
| 338 | |
| 339 | extern struct mtx	sigio_lock; |
| 340 | |
| 341 | /* Flags for kern_sigprocmask(). */ |
| 342 | #define	SIGPROCMASK_OLD		0x0001 |
| 343 | #define	SIGPROCMASK_PROC_LOCKED	0x0002 |
| 344 | #define	SIGPROCMASK_PS_LOCKED	0x0004 |
| 345 | #define	SIGPROCMASK_FASTBLK	0x0008 |
| 346 | |
| 347 | /* |
| 348 | * Modes for sigdeferstop(). Manages behaviour of |
| 349 | * thread_suspend_check() in the region delimited by |
| 350 | * sigdeferstop()/sigallowstop(). Must be restored to |
| 351 | * SIGDEFERSTOP_OFF before returning to userspace. |
| 352 | */ |
| 353 | #define	SIGDEFERSTOP_NOP	0 /* continue doing whatever is done now */ |
| 354 | #define	SIGDEFERSTOP_OFF	1 /* stop ignoring STOPs */ |
| 355 | #define	SIGDEFERSTOP_SILENT	2 /* silently ignore STOPs */ |
| 356 | #define	SIGDEFERSTOP_EINTR	3 /* ignore STOPs, return EINTR */ |
| 357 | #define	SIGDEFERSTOP_ERESTART	4 /* ignore STOPs, return ERESTART */ |
| 358 | |
| 359 | #define	SIGDEFERSTOP_VAL_NCHG	(-1) /* placeholder indicating no state change */ |
| 360 | int	sigdeferstop_impl(int mode); |
| 361 | void	sigallowstop_impl(int prev); |
| 362 | |
| 363 | static inline int |
| 364 | sigdeferstop(int mode) |
| 365 | { |
| 366 | |
| 367 | 	if (__predict_false(mode == SIGDEFERSTOP_NOP)) |
| 368 | 		return (SIGDEFERSTOP_VAL_NCHG); |
| 369 | 	return (sigdeferstop_impl(mode)); |
| 370 | } |
| 371 | |
| 372 | static inline void |
| 373 | sigallowstop(int prev) |
| 374 | { |
| 375 | |
| 376 | 	if (__predict_true(prev == SIGDEFERSTOP_VAL_NCHG)) |
| 377 | 		return; |
| 378 | 	sigallowstop_impl(prev); |
| 379 | } |
| 380 | |
| 381 | int	cursig(struct thread *td); |
| 382 | void	execsigs(struct proc *p); |
| 383 | void	killproc(struct proc *p, const char *why); |
| 384 | ksiginfo_t *ksiginfo_alloc(int mwait); |
| 385 | void	ksiginfo_free(ksiginfo_t *ksi); |
| 386 | int	pksignal(struct proc *p, int sig, ksiginfo_t *ksi); |
| 387 | void	pgsigio(struct sigio **sigiop, int sig, int checkctty); |
| 388 | void	pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi); |
| 389 | int	postsig(int sig); |
| 390 | void	kern_psignal(struct proc *p, int sig); |
| 391 | int	ptracestop(struct thread *td, int sig, ksiginfo_t *si); |
| 392 | void	sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *retmask); |
| 393 | struct sigacts *sigacts_alloc(void); |
| 394 | void	sigacts_copy(struct sigacts *dest, struct sigacts *src); |
| 395 | void	sigacts_free(struct sigacts *ps); |
| 396 | struct sigacts *sigacts_hold(struct sigacts *ps); |
| 397 | int	sigacts_shared(struct sigacts *ps); |
| 398 | int	sig_ast_checksusp(struct thread *td); |
| 399 | int	sig_ast_needsigchk(struct thread *td); |
| 400 | void	sig_drop_caught(struct proc *p); |
| 401 | void	sigexit(struct thread *td, int sig) __dead2; |
| 402 | int	sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **); |
| 403 | void	sigfastblock_clear(struct thread *td); |
| 404 | void	sigfastblock_fetch(struct thread *td); |
| 405 | int	sig_intr(void); |
| 406 | bool	sig_do_core(int); |
| 407 | void	siginit(struct proc *p); |
| 408 | void	signotify(struct thread *td); |
| 409 | void	sigqueue_delete(struct sigqueue *queue, int sig); |
| 410 | void	sigqueue_delete_proc(struct proc *p, int sig); |
| 411 | void	sigqueue_flush(struct sigqueue *queue); |
| 412 | void	sigqueue_init(struct sigqueue *queue, struct proc *p); |
| 413 | void	sigqueue_take(ksiginfo_t *ksi); |
| 414 | void	tdksignal(struct thread *td, int sig, ksiginfo_t *ksi); |
| 415 | int	tdsendsignal(struct proc *p, struct thread *td, int sig, |
| 416 | 	 ksiginfo_t *ksi); |
| 417 | void	tdsigcleanup(struct thread *td); |
| 418 | void	tdsignal(struct thread *td, int sig); |
| 419 | void	trapsignal(struct thread *td, ksiginfo_t *ksi); |
| 420 | |
| 421 | #endif /* _KERNEL */ |
| 422 | |
| 423 | #endif /* !_SYS_SIGNALVAR_H_ */ |