Deleted Added
full compact
kern_sig.c (199136) kern_sig.c (199355)
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
35 */
36
37#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.

--- 21 unchanged lines hidden (view full) ---

30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/sys/kern/kern_sig.c 199136 2009-11-10 11:46:53Z kib $");
38__FBSDID("$FreeBSD: head/sys/kern/kern_sig.c 199355 2009-11-17 11:39:15Z kib $");
39
40#include "opt_compat.h"
41#include "opt_kdtrace.h"
42#include "opt_ktrace.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/signalvar.h>

--- 47 unchanged lines hidden (view full) ---

94SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
95SDT_PROBE_DEFINE(proc, kernel, , signal_discard);
96SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
97SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
98SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
99
100static int coredump(struct thread *);
101static char *expand_name(const char *, uid_t, pid_t);
39
40#include "opt_compat.h"
41#include "opt_kdtrace.h"
42#include "opt_ktrace.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/signalvar.h>

--- 47 unchanged lines hidden (view full) ---

94SDT_PROBE_ARGTYPE(proc, kernel, , signal_clear, 1, "ksiginfo_t *");
95SDT_PROBE_DEFINE(proc, kernel, , signal_discard);
96SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 0, "struct thread *");
97SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 1, "struct proc *");
98SDT_PROBE_ARGTYPE(proc, kernel, , signal_discard, 2, "int");
99
100static int coredump(struct thread *);
101static char *expand_name(const char *, uid_t, pid_t);
102static int killpg1(struct thread *td, int sig, int pgid, int all);
102static int killpg1(struct thread *td, int sig, int pgid, int all,
103 ksiginfo_t *ksi);
103static int issignal(struct thread *td, int stop_allowed);
104static int sigprop(int sig);
105static void tdsigwakeup(struct thread *, int, sig_t, int);
106static void sig_suspend_threads(struct thread *, struct proc *, int);
107static int filt_sigattach(struct knote *kn);
108static void filt_sigdetach(struct knote *kn);
109static int filt_signal(struct knote *kn, long hint);
110static struct thread *sigtd(struct proc *p, int sig, int prop);

--- 265 unchanged lines hidden (view full) ---

376 if (p != NULL)
377 p->p_pendingcnt++;
378 ksiginfo_copy(si, ksi);
379 ksi->ksi_signo = signo;
380 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
381 ksi->ksi_sigq = sq;
382 }
383
104static int issignal(struct thread *td, int stop_allowed);
105static int sigprop(int sig);
106static void tdsigwakeup(struct thread *, int, sig_t, int);
107static void sig_suspend_threads(struct thread *, struct proc *, int);
108static int filt_sigattach(struct knote *kn);
109static void filt_sigdetach(struct knote *kn);
110static int filt_signal(struct knote *kn, long hint);
111static struct thread *sigtd(struct proc *p, int sig, int prop);

--- 265 unchanged lines hidden (view full) ---

377 if (p != NULL)
378 p->p_pendingcnt++;
379 ksiginfo_copy(si, ksi);
380 ksi->ksi_signo = signo;
381 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
382 ksi->ksi_sigq = sq;
383 }
384
384 if ((si->ksi_flags & KSI_TRAP) != 0) {
385 if ((si->ksi_flags & KSI_TRAP) != 0 ||
386 (si->ksi_flags & KSI_SIGQ) == 0) {
385 if (ret != 0)
386 SIGADDSET(sq->sq_kill, signo);
387 ret = 0;
388 goto out_set_bit;
389 }
390
391 if (ret != 0)
392 return (ret);

--- 1213 unchanged lines hidden (view full) ---

1606 return (0);
1607}
1608
1609/*
1610 * Common code for kill process group/broadcast kill.
1611 * cp is calling process.
1612 */
1613static int
387 if (ret != 0)
388 SIGADDSET(sq->sq_kill, signo);
389 ret = 0;
390 goto out_set_bit;
391 }
392
393 if (ret != 0)
394 return (ret);

--- 1213 unchanged lines hidden (view full) ---

1608 return (0);
1609}
1610
1611/*
1612 * Common code for kill process group/broadcast kill.
1613 * cp is calling process.
1614 */
1615static int
1614killpg1(td, sig, pgid, all)
1615 register struct thread *td;
1616 int sig, pgid, all;
1616killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1617{
1617{
1618 register struct proc *p;
1618 struct proc *p;
1619 struct pgrp *pgrp;
1620 int nfound = 0;
1621
1622 if (all) {
1623 /*
1624 * broadcast
1625 */
1626 sx_slock(&allproc_lock);
1627 FOREACH_PROC_IN_SYSTEM(p) {
1628 PROC_LOCK(p);
1629 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1630 p == td->td_proc || p->p_state == PRS_NEW) {
1631 PROC_UNLOCK(p);
1632 continue;
1633 }
1634 if (p_cansignal(td, p, sig) == 0) {
1635 nfound++;
1636 if (sig)
1619 struct pgrp *pgrp;
1620 int nfound = 0;
1621
1622 if (all) {
1623 /*
1624 * broadcast
1625 */
1626 sx_slock(&allproc_lock);
1627 FOREACH_PROC_IN_SYSTEM(p) {
1628 PROC_LOCK(p);
1629 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1630 p == td->td_proc || p->p_state == PRS_NEW) {
1631 PROC_UNLOCK(p);
1632 continue;
1633 }
1634 if (p_cansignal(td, p, sig) == 0) {
1635 nfound++;
1636 if (sig)
1637 psignal(p, sig);
1637 pksignal(p, sig, ksi);
1638 }
1639 PROC_UNLOCK(p);
1640 }
1641 sx_sunlock(&allproc_lock);
1642 } else {
1643 sx_slock(&proctree_lock);
1644 if (pgid == 0) {
1645 /*

--- 14 unchanged lines hidden (view full) ---

1660 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1661 p->p_state == PRS_NEW ) {
1662 PROC_UNLOCK(p);
1663 continue;
1664 }
1665 if (p_cansignal(td, p, sig) == 0) {
1666 nfound++;
1667 if (sig)
1638 }
1639 PROC_UNLOCK(p);
1640 }
1641 sx_sunlock(&allproc_lock);
1642 } else {
1643 sx_slock(&proctree_lock);
1644 if (pgid == 0) {
1645 /*

--- 14 unchanged lines hidden (view full) ---

1660 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1661 p->p_state == PRS_NEW ) {
1662 PROC_UNLOCK(p);
1663 continue;
1664 }
1665 if (p_cansignal(td, p, sig) == 0) {
1666 nfound++;
1667 if (sig)
1668 psignal(p, sig);
1668 pksignal(p, sig, ksi);
1669 }
1670 PROC_UNLOCK(p);
1671 }
1672 PGRP_UNLOCK(pgrp);
1673 }
1674 return (nfound ? 0 : ESRCH);
1675}
1676
1677#ifndef _SYS_SYSPROTO_H_
1678struct kill_args {
1679 int pid;
1680 int signum;
1681};
1682#endif
1683/* ARGSUSED */
1684int
1669 }
1670 PROC_UNLOCK(p);
1671 }
1672 PGRP_UNLOCK(pgrp);
1673 }
1674 return (nfound ? 0 : ESRCH);
1675}
1676
1677#ifndef _SYS_SYSPROTO_H_
1678struct kill_args {
1679 int pid;
1680 int signum;
1681};
1682#endif
1683/* ARGSUSED */
1684int
1685kill(td, uap)
1686 register struct thread *td;
1687 register struct kill_args *uap;
1685kill(struct thread *td, struct kill_args *uap)
1688{
1686{
1689 register struct proc *p;
1687 ksiginfo_t ksi;
1688 struct proc *p;
1690 int error;
1691
1692 AUDIT_ARG_SIGNUM(uap->signum);
1693 AUDIT_ARG_PID(uap->pid);
1694 if ((u_int)uap->signum > _SIG_MAXSIG)
1695 return (EINVAL);
1696
1689 int error;
1690
1691 AUDIT_ARG_SIGNUM(uap->signum);
1692 AUDIT_ARG_PID(uap->pid);
1693 if ((u_int)uap->signum > _SIG_MAXSIG)
1694 return (EINVAL);
1695
1696 ksiginfo_init(&ksi);
1697 ksi.ksi_signo = uap->signum;
1698 ksi.ksi_code = SI_USER;
1699 ksi.ksi_pid = td->td_proc->p_pid;
1700 ksi.ksi_uid = td->td_ucred->cr_ruid;
1701
1697 if (uap->pid > 0) {
1698 /* kill single process */
1699 if ((p = pfind(uap->pid)) == NULL) {
1700 if ((p = zpfind(uap->pid)) == NULL)
1701 return (ESRCH);
1702 }
1703 AUDIT_ARG_PROCESS(p);
1704 error = p_cansignal(td, p, uap->signum);
1705 if (error == 0 && uap->signum)
1702 if (uap->pid > 0) {
1703 /* kill single process */
1704 if ((p = pfind(uap->pid)) == NULL) {
1705 if ((p = zpfind(uap->pid)) == NULL)
1706 return (ESRCH);
1707 }
1708 AUDIT_ARG_PROCESS(p);
1709 error = p_cansignal(td, p, uap->signum);
1710 if (error == 0 && uap->signum)
1706 psignal(p, uap->signum);
1711 pksignal(p, uap->signum, &ksi);
1707 PROC_UNLOCK(p);
1708 return (error);
1709 }
1710 switch (uap->pid) {
1711 case -1: /* broadcast signal */
1712 PROC_UNLOCK(p);
1713 return (error);
1714 }
1715 switch (uap->pid) {
1716 case -1: /* broadcast signal */
1712 return (killpg1(td, uap->signum, 0, 1));
1717 return (killpg1(td, uap->signum, 0, 1, &ksi));
1713 case 0: /* signal own process group */
1718 case 0: /* signal own process group */
1714 return (killpg1(td, uap->signum, 0, 0));
1719 return (killpg1(td, uap->signum, 0, 0, &ksi));
1715 default: /* negative explicit process group */
1720 default: /* negative explicit process group */
1716 return (killpg1(td, uap->signum, -uap->pid, 0));
1721 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1717 }
1718 /* NOTREACHED */
1719}
1720
1721#if defined(COMPAT_43)
1722#ifndef _SYS_SYSPROTO_H_
1723struct okillpg_args {
1724 int pgid;
1725 int signum;
1726};
1727#endif
1728/* ARGSUSED */
1729int
1722 }
1723 /* NOTREACHED */
1724}
1725
1726#if defined(COMPAT_43)
1727#ifndef _SYS_SYSPROTO_H_
1728struct okillpg_args {
1729 int pgid;
1730 int signum;
1731};
1732#endif
1733/* ARGSUSED */
1734int
1730okillpg(td, uap)
1731 struct thread *td;
1732 register struct okillpg_args *uap;
1735okillpg(struct thread *td, struct okillpg_args *uap)
1733{
1736{
1737 ksiginfo_t ksi;
1734
1735 AUDIT_ARG_SIGNUM(uap->signum);
1736 AUDIT_ARG_PID(uap->pgid);
1737 if ((u_int)uap->signum > _SIG_MAXSIG)
1738 return (EINVAL);
1739
1738
1739 AUDIT_ARG_SIGNUM(uap->signum);
1740 AUDIT_ARG_PID(uap->pgid);
1741 if ((u_int)uap->signum > _SIG_MAXSIG)
1742 return (EINVAL);
1743
1740 return (killpg1(td, uap->signum, uap->pgid, 0));
1744 ksiginfo_init(&ksi);
1745 ksi.ksi_signo = uap->signum;
1746 ksi.ksi_code = SI_USER;
1747 ksi.ksi_pid = td->td_proc->p_pid;
1748 ksi.ksi_uid = td->td_ucred->cr_ruid;
1749 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1741}
1742#endif /* COMPAT_43 */
1743
1744#ifndef _SYS_SYSPROTO_H_
1745struct sigqueue_args {
1746 pid_t pid;
1747 int signum;
1748 /* union sigval */ void *value;

--- 18 unchanged lines hidden (view full) ---

1767
1768 if ((p = pfind(uap->pid)) == NULL) {
1769 if ((p = zpfind(uap->pid)) == NULL)
1770 return (ESRCH);
1771 }
1772 error = p_cansignal(td, p, uap->signum);
1773 if (error == 0 && uap->signum != 0) {
1774 ksiginfo_init(&ksi);
1750}
1751#endif /* COMPAT_43 */
1752
1753#ifndef _SYS_SYSPROTO_H_
1754struct sigqueue_args {
1755 pid_t pid;
1756 int signum;
1757 /* union sigval */ void *value;

--- 18 unchanged lines hidden (view full) ---

1776
1777 if ((p = pfind(uap->pid)) == NULL) {
1778 if ((p = zpfind(uap->pid)) == NULL)
1779 return (ESRCH);
1780 }
1781 error = p_cansignal(td, p, uap->signum);
1782 if (error == 0 && uap->signum != 0) {
1783 ksiginfo_init(&ksi);
1784 ksi.ksi_flags = KSI_SIGQ;
1775 ksi.ksi_signo = uap->signum;
1776 ksi.ksi_code = SI_QUEUE;
1777 ksi.ksi_pid = td->td_proc->p_pid;
1778 ksi.ksi_uid = td->td_ucred->cr_ruid;
1779 ksi.ksi_value.sival_ptr = uap->value;
1780 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1781 }
1782 PROC_UNLOCK(p);
1783 return (error);
1784}
1785
1786/*
1787 * Send a signal to a process group.
1788 */
1789void
1785 ksi.ksi_signo = uap->signum;
1786 ksi.ksi_code = SI_QUEUE;
1787 ksi.ksi_pid = td->td_proc->p_pid;
1788 ksi.ksi_uid = td->td_ucred->cr_ruid;
1789 ksi.ksi_value.sival_ptr = uap->value;
1790 error = tdsignal(p, NULL, ksi.ksi_signo, &ksi);
1791 }
1792 PROC_UNLOCK(p);
1793 return (error);
1794}
1795
1796/*
1797 * Send a signal to a process group.
1798 */
1799void
1790gsignal(pgid, sig)
1791 int pgid, sig;
1800gsignal(int pgid, int sig, ksiginfo_t *ksi)
1792{
1793 struct pgrp *pgrp;
1794
1795 if (pgid != 0) {
1796 sx_slock(&proctree_lock);
1797 pgrp = pgfind(pgid);
1798 sx_sunlock(&proctree_lock);
1799 if (pgrp != NULL) {
1801{
1802 struct pgrp *pgrp;
1803
1804 if (pgid != 0) {
1805 sx_slock(&proctree_lock);
1806 pgrp = pgfind(pgid);
1807 sx_sunlock(&proctree_lock);
1808 if (pgrp != NULL) {
1800 pgsignal(pgrp, sig, 0);
1809 pgsignal(pgrp, sig, 0, ksi);
1801 PGRP_UNLOCK(pgrp);
1802 }
1803 }
1804}
1805
1806/*
1807 * Send a signal to a process group. If checktty is 1,
1808 * limit to members which have a controlling terminal.
1809 */
1810void
1810 PGRP_UNLOCK(pgrp);
1811 }
1812 }
1813}
1814
1815/*
1816 * Send a signal to a process group. If checktty is 1,
1817 * limit to members which have a controlling terminal.
1818 */
1819void
1811pgsignal(pgrp, sig, checkctty)
1812 struct pgrp *pgrp;
1813 int sig, checkctty;
1820pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1814{
1821{
1815 register struct proc *p;
1822 struct proc *p;
1816
1817 if (pgrp) {
1818 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1819 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1820 PROC_LOCK(p);
1821 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1823
1824 if (pgrp) {
1825 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1826 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1827 PROC_LOCK(p);
1828 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1822 psignal(p, sig);
1829 pksignal(p, sig, ksi);
1823 PROC_UNLOCK(p);
1824 }
1825 }
1826}
1827
1828/*
1829 * Send a signal caused by a trap to the current thread. If it will be
1830 * caught immediately, deliver it with correct code. Otherwise, post it

--- 104 unchanged lines hidden (view full) ---

1935 *
1936 * NB: This function may be entered from the debugger via the "kill" DDB
1937 * command. There is little that can be done to mitigate the possibly messy
1938 * side effects of this unwise possibility.
1939 */
1940void
1941psignal(struct proc *p, int sig)
1942{
1830 PROC_UNLOCK(p);
1831 }
1832 }
1833}
1834
1835/*
1836 * Send a signal caused by a trap to the current thread. If it will be
1837 * caught immediately, deliver it with correct code. Otherwise, post it

--- 104 unchanged lines hidden (view full) ---

1942 *
1943 * NB: This function may be entered from the debugger via the "kill" DDB
1944 * command. There is little that can be done to mitigate the possibly messy
1945 * side effects of this unwise possibility.
1946 */
1947void
1948psignal(struct proc *p, int sig)
1949{
1943 (void) tdsignal(p, NULL, sig, NULL);
1950 ksiginfo_t ksi;
1951
1952 ksiginfo_init(&ksi);
1953 ksi.ksi_signo = sig;
1954 ksi.ksi_code = SI_KERNEL;
1955 (void) tdsignal(p, NULL, sig, &ksi);
1944}
1945
1956}
1957
1958void
1959pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
1960{
1961
1962 (void) tdsignal(p, NULL, sig, ksi);
1963}
1964
1946int
1947psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
1948{
1949 struct thread *td = NULL;
1950
1951 PROC_LOCK_ASSERT(p, MA_OWNED);
1952
1953 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));

--- 1184 unchanged lines hidden (view full) ---

3138 * Send a SIGIO or SIGURG signal to a process or process group using stored
3139 * credentials rather than those of the current process.
3140 */
3141void
3142pgsigio(sigiop, sig, checkctty)
3143 struct sigio **sigiop;
3144 int sig, checkctty;
3145{
1965int
1966psignal_event(struct proc *p, struct sigevent *sigev, ksiginfo_t *ksi)
1967{
1968 struct thread *td = NULL;
1969
1970 PROC_LOCK_ASSERT(p, MA_OWNED);
1971
1972 KASSERT(!KSI_ONQ(ksi), ("psignal_event: ksi on queue"));

--- 1184 unchanged lines hidden (view full) ---

3157 * Send a SIGIO or SIGURG signal to a process or process group using stored
3158 * credentials rather than those of the current process.
3159 */
3160void
3161pgsigio(sigiop, sig, checkctty)
3162 struct sigio **sigiop;
3163 int sig, checkctty;
3164{
3165 ksiginfo_t ksi;
3146 struct sigio *sigio;
3147
3166 struct sigio *sigio;
3167
3168 ksiginfo_init(&ksi);
3169 ksi.ksi_signo = sig;
3170 ksi.ksi_code = SI_KERNEL;
3171
3148 SIGIO_LOCK();
3149 sigio = *sigiop;
3150 if (sigio == NULL) {
3151 SIGIO_UNLOCK();
3152 return;
3153 }
3154 if (sigio->sio_pgid > 0) {
3155 PROC_LOCK(sigio->sio_proc);

--- 112 unchanged lines hidden ---
3172 SIGIO_LOCK();
3173 sigio = *sigiop;
3174 if (sigio == NULL) {
3175 SIGIO_UNLOCK();
3176 return;
3177 }
3178 if (sigio->sio_pgid > 0) {
3179 PROC_LOCK(sigio->sio_proc);

--- 112 unchanged lines hidden ---