signalvar.h revision 246417
154803Srwatson/*- 2160146Srwatson * Copyright (c) 1991, 1993 354803Srwatson * The Regents of the University of California. All rights reserved. 454803Srwatson * 585845Srwatson * Redistribution and use in source and binary forms, with or without 685845Srwatson * modification, are permitted provided that the following conditions 754803Srwatson * are met: 854803Srwatson * 1. Redistributions of source code must retain the above copyright 954803Srwatson * notice, this list of conditions and the following disclaimer. 1054803Srwatson * 2. Redistributions in binary form must reproduce the above copyright 1154803Srwatson * notice, this list of conditions and the following disclaimer in the 1254803Srwatson * documentation and/or other materials provided with the distribution. 1354803Srwatson * 4. Neither the name of the University nor the names of its contributors 1454803Srwatson * may be used to endorse or promote products derived from this software 1554803Srwatson * without specific prior written permission. 1654803Srwatson * 1754803Srwatson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1854803Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1954803Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2054803Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2154803Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2254803Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2354803Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2454803Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2554803Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2654803Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2754803Srwatson * SUCH DAMAGE. 2854803Srwatson * 2973890Srwatson * @(#)signalvar.h 8.6 (Berkeley) 2/19/95 30160146Srwatson * $FreeBSD: head/sys/sys/signalvar.h 246417 2013-02-06 17:06:51Z jhb $ 31160146Srwatson */ 32160146Srwatson 3354803Srwatson#ifndef _SYS_SIGNALVAR_H_ 3454803Srwatson#define _SYS_SIGNALVAR_H_ 35116182Sobrien 36116182Sobrien#include <sys/queue.h> 37116182Sobrien#include <sys/_lock.h> 3854803Srwatson#include <sys/_mutex.h> 3954803Srwatson#include <sys/signal.h> 4054803Srwatson 41224778Srwatson/* 42177785Skib * Kernel signal definitions and data structures. 4354803Srwatson */ 4454803Srwatson 45150262Scsjp/* 4654803Srwatson * Logical process signal actions and state, needed only within the process 4754803Srwatson * The mapping between sigacts and proc structures is 1:1 except for rfork() 4882713Sdillon * processes masquerading as threads which use one structure for the whole 4954803Srwatson * group. All members are locked by the included mutex. The reference count 5054803Srwatson * and mutex must be last for the bcopy in sigacts_copy() to work. 51108524Salfred */ 5254803Srwatsonstruct sigacts { 5354803Srwatson sig_t ps_sigact[_SIG_MAXSIG]; /* Disposition of signals. */ 5454803Srwatson sigset_t ps_catchmask[_SIG_MAXSIG]; /* Signals to be blocked. */ 5554803Srwatson sigset_t ps_sigonstack; /* Signals to take on sigstack. */ 56163606Srwatson sigset_t ps_sigintr; /* Signals that interrupt syscalls. */ 57163606Srwatson sigset_t ps_sigreset; /* Signals that reset when caught. */ 58192586Strasz sigset_t ps_signodefer; /* Signals not masked while handled. */ 5954803Srwatson sigset_t ps_siginfo; /* Signals that want SA_SIGINFO args. */ 60192586Strasz sigset_t ps_sigignore; /* Signals being ignored. */ 61192586Strasz sigset_t ps_sigcatch; /* Signals being caught by user. */ 6298927Srwatson sigset_t ps_freebsd4; /* Signals using freebsd4 ucontext. */ 6398927Srwatson sigset_t ps_osigset; /* Signals using <= 3.x osigset_t. */ 6498927Srwatson sigset_t ps_usertramp; /* SunOS compat; libc sigtramp. XXX */ 6598927Srwatson int ps_flag; 6685582Srwatson int ps_refcnt; 6798927Srwatson struct mtx ps_mtx; 6854803Srwatson}; 69192586Strasz 70192586Strasz#define PS_NOCLDWAIT 0x0001 /* No zombies if child dies */ 71192586Strasz#define PS_NOCLDSTOP 0x0002 /* No SIGCHLD when children stop. */ 72192586Strasz#define PS_CLDSIGIGN 0x0004 /* The SIGCHLD handler is SIG_IGN. */ 73192586Strasz 74192586Strasz#ifdef _KERNEL 75192586Strasz 76192586Strasz#ifdef COMPAT_43 77192586Strasztypedef struct { 78192586Strasz struct osigcontext si_sc; 79192586Strasz int si_signo; 80192586Strasz int si_code; 81192586Strasz union sigval si_value; 82192586Strasz} osiginfo_t; 83192586Strasz 84192586Straszstruct osigaction { 85192586Strasz union { 86192586Strasz void (*__sa_handler)(int); 87192586Strasz void (*__sa_sigaction)(int, osiginfo_t *, void *); 88192586Strasz } __sigaction_u; /* signal handler */ 89192586Strasz osigset_t sa_mask; /* signal mask to apply */ 90192586Strasz int sa_flags; /* see signal options below */ 91192586Strasz}; 92192586Strasz 93192586Strasztypedef void __osiginfohandler_t(int, osiginfo_t *, void *); 94192586Strasz#endif /* COMPAT_43 */ 95192586Strasz 96208781Strasz/* additional signal action values, used only temporarily/internally */ 97192586Strasz#define SIG_CATCH ((__sighandler_t *)2) 98192586Strasz/* #define SIG_HOLD ((__sighandler_t *)3) See signal.h */ 99192586Strasz 100192586Strasz/* 101192586Strasz * get signal action for process and signal; currently only for current process 102192586Strasz */ 103192586Strasz#define SIGACTION(p, sig) (p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) 104192586Strasz 105192586Strasz#endif /* _KERNEL */ 106192586Strasz 107192586Strasz/* 108192586Strasz * sigset_t manipulation macros. 109192586Strasz */ 110192586Strasz#define SIGADDSET(set, signo) \ 111192586Strasz ((set).__bits[_SIG_WORD(signo)] |= _SIG_BIT(signo)) 11254803Srwatson 113192586Strasz#define SIGDELSET(set, signo) \ 114192586Strasz ((set).__bits[_SIG_WORD(signo)] &= ~_SIG_BIT(signo)) 115192586Strasz 116192586Strasz#define SIGEMPTYSET(set) \ 117192586Strasz do { \ 118192586Strasz int __i; \ 119192586Strasz for (__i = 0; __i < _SIG_WORDS; __i++) \ 120192586Strasz (set).__bits[__i] = 0; \ 121192586Strasz } while (0) 122192586Strasz 123192586Strasz#define SIGFILLSET(set) \ 124192586Strasz do { \ 125192586Strasz int __i; \ 126192586Strasz for (__i = 0; __i < _SIG_WORDS; __i++) \ 127192586Strasz (set).__bits[__i] = ~0U; \ 128192586Strasz } while (0) 129192586Strasz 130192586Strasz#define SIGISMEMBER(set, signo) \ 131192586Strasz ((set).__bits[_SIG_WORD(signo)] & _SIG_BIT(signo)) 132192586Strasz 133192586Strasz#define SIGISEMPTY(set) (__sigisempty(&(set))) 134192586Strasz#define SIGNOTEMPTY(set) (!__sigisempty(&(set))) 135192586Strasz 136192586Strasz#define SIGSETEQ(set1, set2) (__sigseteq(&(set1), &(set2))) 137192586Strasz#define SIGSETNEQ(set1, set2) (!__sigseteq(&(set1), &(set2))) 138192586Strasz 139192586Strasz#define SIGSETOR(set1, set2) \ 140192586Strasz do { \ 141192586Strasz int __i; \ 142192586Strasz for (__i = 0; __i < _SIG_WORDS; __i++) \ 143192586Strasz (set1).__bits[__i] |= (set2).__bits[__i]; \ 144192586Strasz } while (0) 145192586Strasz 146192586Strasz#define SIGSETAND(set1, set2) \ 147192586Strasz do { \ 148192586Strasz int __i; \ 149192586Strasz for (__i = 0; __i < _SIG_WORDS; __i++) \ 150192586Strasz (set1).__bits[__i] &= (set2).__bits[__i]; \ 151274648Skib } while (0) 152192586Strasz 153192586Strasz#define SIGSETNAND(set1, set2) \ 154192586Strasz do { \ 155192586Strasz int __i; \ 156192586Strasz for (__i = 0; __i < _SIG_WORDS; __i++) \ 157192586Strasz (set1).__bits[__i] &= ~(set2).__bits[__i]; \ 158192586Strasz } while (0) 159192586Strasz 160192586Strasz#define SIGSETLO(set1, set2) ((set1).__bits[0] = (set2).__bits[0]) 161192586Strasz#define SIGSETOLD(set, oset) ((set).__bits[0] = (oset)) 162192586Strasz 163192586Strasz#define SIG_CANTMASK(set) \ 164192586Strasz SIGDELSET(set, SIGKILL), SIGDELSET(set, SIGSTOP) 165192586Strasz 166274648Skib#define SIG_STOPSIGMASK(set) \ 167274648Skib SIGDELSET(set, SIGSTOP), SIGDELSET(set, SIGTSTP), \ 168274648Skib SIGDELSET(set, SIGTTIN), SIGDELSET(set, SIGTTOU) 169274648Skib 170274648Skib#define SIG_CONTSIGMASK(set) \ 171192586Strasz SIGDELSET(set, SIGCONT) 172192586Strasz 173192586Strasz#define sigcantmask (sigmask(SIGKILL) | sigmask(SIGSTOP)) 174192586Strasz 175192586Strasz#define SIG2OSIG(sig, osig) (osig = (sig).__bits[0]) 176192586Strasz#define OSIG2SIG(osig, sig) SIGEMPTYSET(sig); (sig).__bits[0] = osig 177192586Strasz 178192586Straszstatic __inline int 179192586Strasz__sigisempty(sigset_t *set) 180192586Strasz{ 181200058Strasz int i; 182192586Strasz 183192586Strasz for (i = 0; i < _SIG_WORDS; i++) { 184192586Strasz if (set->__bits[i]) 185192586Strasz return (0); 186192586Strasz } 187192586Strasz return (1); 188192586Strasz} 189192586Strasz 190192586Straszstatic __inline int 191192586Strasz__sigseteq(sigset_t *set1, sigset_t *set2) 192192586Strasz{ 193192586Strasz int i; 194192586Strasz 195192586Strasz for (i = 0; i < _SIG_WORDS; i++) { 196192586Strasz if (set1->__bits[i] != set2->__bits[i]) 197192586Strasz return (0); 198192586Strasz } 199192586Strasz return (1); 200192586Strasz} 201165983Srwatson 202165983Srwatsonstruct osigevent { 203165983Srwatson int sigev_notify; /* Notification type */ 204165983Srwatson union { 205165983Srwatson int __sigev_signo; /* Signal number */ 20654803Srwatson int __sigev_notify_kqueue; 20754803Srwatson } __sigev_u; 20854803Srwatson union sigval sigev_value; /* Signal value */ 20954803Srwatson}; 21054803Srwatson 21154803Srwatsontypedef struct ksiginfo { 21285582Srwatson TAILQ_ENTRY(ksiginfo) ksi_link; 21356272Srwatson siginfo_t ksi_info; 21454803Srwatson int ksi_flags; 215191249Strasz struct sigqueue *ksi_sigq; 21690202Srwatson} ksiginfo_t; 21754803Srwatson 21854803Srwatson#define ksi_signo ksi_info.si_signo 219191249Strasz#define ksi_errno ksi_info.si_errno 220192586Strasz#define ksi_code ksi_info.si_code 221198875Strasz#define ksi_pid ksi_info.si_pid 222191249Strasz#define ksi_uid ksi_info.si_uid 22390202Srwatson#define ksi_status ksi_info.si_status 22490202Srwatson#define ksi_addr ksi_info.si_addr 225191249Strasz#define ksi_value ksi_info.si_value 226175202Sattilio#define ksi_band ksi_info.si_band 227101122Srwatson#define ksi_trapno ksi_info.si_trapno 228191249Strasz#define ksi_overrun ksi_info.si_overrun 229101122Srwatson#define ksi_timerid ksi_info.si_timerid 230191249Strasz#define ksi_mqd ksi_info.si_mqd 231101122Srwatson 232192586Strasz/* bits for ksi_flags */ 233192586Strasz#define KSI_TRAP 0x01 /* Generated by trap. */ 234101122Srwatson#define KSI_EXT 0x02 /* Externally managed ksi. */ 235191249Strasz#define KSI_INS 0x04 /* Directly insert ksi, not the copy */ 236101122Srwatson#define KSI_SIGQ 0x08 /* Generated by sigqueue, might ret EGAIN. */ 237175294Sattilio#define KSI_HEAD 0x10 /* Insert into head, not tail. */ 23890202Srwatson#define KSI_COPYMASK (KSI_TRAP|KSI_SIGQ) 239191249Strasz 240191249Strasz#define KSI_ONQ(ksi) ((ksi)->ksi_sigq != NULL) 241198875Strasz 24254803Srwatsontypedef struct sigqueue { 24354803Srwatson sigset_t sq_signals; /* All pending signals. */ 24454803Srwatson sigset_t sq_kill; /* Legacy depth 1 queue. */ 24554803Srwatson TAILQ_HEAD(, ksiginfo) sq_list;/* Queued signal info. */ 24654803Srwatson struct proc *sq_proc; 24754803Srwatson int sq_flags; 24885582Srwatson} sigqueue_t; 24954803Srwatson 25054803Srwatson/* Flags for ksi_flags */ 251191249Strasz#define SQ_INIT 0x01 25254803Srwatson 25354803Srwatson#ifdef _KERNEL 254246412Spluknet 255175202Sattilio/* Return nonzero if process p has an unmasked pending signal. */ 256101122Srwatson#define SIGPENDING(td) \ 257172930Srwatson ((!SIGISEMPTY((td)->td_siglist) && \ 258101122Srwatson !sigsetmasked(&(td)->td_siglist, &(td)->td_sigmask)) || \ 259101122Srwatson (!SIGISEMPTY((td)->td_proc->p_siglist) && \ 260101122Srwatson !sigsetmasked(&(td)->td_proc->p_siglist, &(td)->td_sigmask))) 261192586Strasz/* 262192586Strasz * Return the value of the pseudo-expression ((*set & ~*mask) != 0). This 263192586Strasz * is an optimized version of SIGISEMPTY() on a temporary variable 264101122Srwatson * containing SIGSETNAND(*set, *mask). 265101122Srwatson */ 266101122Srwatsonstatic __inline int 267175294Sattiliosigsetmasked(sigset_t *set, sigset_t *mask) 26854803Srwatson{ 269192586Strasz int i; 270191249Strasz 27154803Srwatson for (i = 0; i < _SIG_WORDS; i++) { 27254803Srwatson if (set->__bits[i] & ~mask->__bits[i]) 27354803Srwatson return (0); 27454803Srwatson } 27554803Srwatson return (1); 27654803Srwatson} 27754803Srwatson 27885582Srwatson#define ksiginfo_init(ksi) \ 27954803Srwatsondo { \ 28090202Srwatson bzero(ksi, sizeof(ksiginfo_t)); \ 28154803Srwatson} while(0) 28254803Srwatson 28390202Srwatson#define ksiginfo_init_trap(ksi) \ 284198875Straszdo { \ 28590202Srwatson ksiginfo_t *kp = ksi; \ 286175202Sattilio bzero(kp, sizeof(ksiginfo_t)); \ 287101122Srwatson kp->ksi_flags |= KSI_TRAP; \ 288172930Srwatson} while(0) 289198875Strasz 290101122Srwatsonstatic __inline void 291101122Srwatsonksiginfo_copy(ksiginfo_t *src, ksiginfo_t *dst) 292192586Strasz{ 293101122Srwatson (dst)->ksi_info = src->ksi_info; 294101122Srwatson (dst)->ksi_flags = (src->ksi_flags & KSI_COPYMASK); 295101122Srwatson} 296175294Sattilio 29790202Srwatsonstatic __inline void 29854803Srwatsonksiginfo_set_sigev(ksiginfo_t *dst, struct sigevent *sigev) 29954803Srwatson{ 30054803Srwatson dst->ksi_signo = sigev->sigev_signo; 30154803Srwatson dst->ksi_value = sigev->sigev_value; 30254803Srwatson} 30354803Srwatson 30454803Srwatsonstruct pgrp; 30585582Srwatsonstruct proc; 30654803Srwatsonstruct sigio; 30754803Srwatsonstruct thread; 308191249Strasz 30954803Srwatson/* 31054803Srwatson * Lock the pointers for a sigio object in the underlying objects of 311191249Strasz * a file descriptor. 312192586Strasz */ 313198875Strasz#define SIGIO_LOCK() mtx_lock(&sigio_lock) 314191249Strasz#define SIGIO_TRYLOCK() mtx_trylock(&sigio_lock) 315200058Strasz#define SIGIO_UNLOCK() mtx_unlock(&sigio_lock) 316200058Strasz#define SIGIO_LOCKED() mtx_owned(&sigio_lock) 317191249Strasz#define SIGIO_ASSERT(type) mtx_assert(&sigio_lock, type) 318191249Strasz 31954803Srwatsonextern struct mtx sigio_lock; 32054803Srwatson 32154803Srwatson/* Values for stop_allowed parameter for cursig(). */ 32254803Srwatson#define SIG_STOP_ALLOWED 100 323165983Srwatson#define SIG_STOP_NOT_ALLOWED 101 324165983Srwatson 32554803Srwatson/* Flags for kern_sigprocmask(). */ 32654803Srwatson#define SIGPROCMASK_OLD 0x0001 32754803Srwatson#define SIGPROCMASK_PROC_LOCKED 0x0002 32854803Srwatson#define SIGPROCMASK_PS_LOCKED 0x0004 32954803Srwatson 33054803Srwatsonint cursig(struct thread *td, int stop_allowed); 331225617Skmacyvoid sigdeferstop(struct thread *td); 33254803Srwatsonvoid sigallowstop(struct thread *td); 33354803Srwatsonvoid execsigs(struct proc *p); 334241896Skibvoid gsignal(int pgid, int sig, ksiginfo_t *ksi); 33554803Srwatsonvoid killproc(struct proc *p, char *why); 336241896Skibksiginfo_t * ksiginfo_alloc(int wait); 33754803Srwatsonvoid ksiginfo_free(ksiginfo_t *ksi); 33882713Sdillonint pksignal(struct proc *p, int sig, ksiginfo_t *ksi); 339107855Salfredvoid pgsigio(struct sigio **sigiop, int sig, int checkctty); 34082713Sdillonvoid pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi); 34182713Sdillonint postsig(int sig); 34254803Srwatsonvoid kern_psignal(struct proc *p, int sig); 34354803Srwatsonint ptracestop(struct thread *td, int sig); 34454803Srwatsonvoid sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *retmask); 34554803Srwatsonstruct sigacts *sigacts_alloc(void); 346108407Srwatsonvoid sigacts_copy(struct sigacts *dest, struct sigacts *src); 347108407Srwatsonvoid sigacts_free(struct sigacts *ps); 348108407Srwatsonstruct sigacts *sigacts_hold(struct sigacts *ps); 349225617Skmacyint sigacts_shared(struct sigacts *ps); 350108407Srwatsonvoid sigexit(struct thread *td, int sig) __dead2; 351108407Srwatsonint sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **); 352241896Skibint sig_ffs(sigset_t *set); 353108407Srwatsonvoid siginit(struct proc *p); 354241896Skibvoid signotify(struct thread *td); 355108407Srwatsonvoid sigqueue_delete(struct sigqueue *queue, int sig); 356108407Srwatsonvoid sigqueue_delete_proc(struct proc *p, int sig); 357108407Srwatsonvoid sigqueue_flush(struct sigqueue *queue); 358108407Srwatsonvoid sigqueue_init(struct sigqueue *queue, struct proc *p); 359108407Srwatsonvoid sigqueue_take(ksiginfo_t *ksi); 360108407Srwatsonvoid tdksignal(struct thread *td, int sig, ksiginfo_t *ksi); 361108407Srwatsonint tdsendsignal(struct proc *p, struct thread *td, int sig, 362108407Srwatson ksiginfo_t *ksi); 363108407Srwatsonvoid tdsigcleanup(struct thread *td); 364167211Srwatsonvoid tdsignal(struct thread *td, int sig); 36554803Srwatsonvoid trapsignal(struct thread *td, ksiginfo_t *ksi); 36654803Srwatson 367225617Skmacy#endif /* _KERNEL */ 36854803Srwatson 36954803Srwatson#endif /* !_SYS_SIGNALVAR_H_ */ 370241896Skib