signal.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:28k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/kernel/signal.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  *  1997-11-02  Modified for POSIX.1b signals by Richard Henderson
  7.  */
  8. #include <linux/config.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/unistd.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/init.h>
  14. #include <linux/sched.h>
  15. #include <asm/uaccess.h>
  16. /*
  17.  * SLAB caches for signal bits.
  18.  */
  19. static kmem_cache_t *sigqueue_cachep;
  20. atomic_t nr_queued_signals;
  21. int max_queued_signals = 1024;
  22. void __init signals_init(void)
  23. {
  24. sigqueue_cachep =
  25. kmem_cache_create("sigqueue",
  26.   sizeof(struct sigqueue),
  27.   __alignof__(struct sigqueue),
  28.   0, NULL, NULL);
  29. if (!sigqueue_cachep)
  30. panic("signals_init(): cannot create sigqueue SLAB cache");
  31. }
  32. /* Given the mask, find the first available signal that should be serviced. */
  33. static int
  34. next_signal(struct task_struct *tsk, sigset_t *mask)
  35. {
  36. unsigned long i, *s, *m, x;
  37. int sig = 0;
  38. s = tsk->pending.signal.sig;
  39. m = mask->sig;
  40. switch (_NSIG_WORDS) {
  41. default:
  42. for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
  43. if ((x = *s &~ *m) != 0) {
  44. sig = ffz(~x) + i*_NSIG_BPW + 1;
  45. break;
  46. }
  47. break;
  48. case 2: if ((x = s[0] &~ m[0]) != 0)
  49. sig = 1;
  50. else if ((x = s[1] &~ m[1]) != 0)
  51. sig = _NSIG_BPW + 1;
  52. else
  53. break;
  54. sig += ffz(~x);
  55. break;
  56. case 1: if ((x = *s &~ *m) != 0)
  57. sig = ffz(~x) + 1;
  58. break;
  59. }
  60. return sig;
  61. }
  62. static void flush_sigqueue(struct sigpending *queue)
  63. {
  64. struct sigqueue *q, *n;
  65. sigemptyset(&queue->signal);
  66. q = queue->head;
  67. queue->head = NULL;
  68. queue->tail = &queue->head;
  69. while (q) {
  70. n = q->next;
  71. kmem_cache_free(sigqueue_cachep, q);
  72. atomic_dec(&nr_queued_signals);
  73. q = n;
  74. }
  75. }
  76. /*
  77.  * Flush all pending signals for a task.
  78.  */
  79. void
  80. flush_signals(struct task_struct *t)
  81. {
  82. t->sigpending = 0;
  83. flush_sigqueue(&t->pending);
  84. }
  85. void exit_sighand(struct task_struct *tsk)
  86. {
  87. struct signal_struct * sig = tsk->sig;
  88. spin_lock_irq(&tsk->sigmask_lock);
  89. if (sig) {
  90. tsk->sig = NULL;
  91. if (atomic_dec_and_test(&sig->count))
  92. kmem_cache_free(sigact_cachep, sig);
  93. }
  94. tsk->sigpending = 0;
  95. flush_sigqueue(&tsk->pending);
  96. spin_unlock_irq(&tsk->sigmask_lock);
  97. }
  98. /*
  99.  * Flush all handlers for a task.
  100.  */
  101. void
  102. flush_signal_handlers(struct task_struct *t)
  103. {
  104. int i;
  105. struct k_sigaction *ka = &t->sig->action[0];
  106. for (i = _NSIG ; i != 0 ; i--) {
  107. if (ka->sa.sa_handler != SIG_IGN)
  108. ka->sa.sa_handler = SIG_DFL;
  109. ka->sa.sa_flags = 0;
  110. sigemptyset(&ka->sa.sa_mask);
  111. ka++;
  112. }
  113. }
  114. /*
  115.  * sig_exit - cause the current task to exit due to a signal.
  116.  */
  117. void
  118. sig_exit(int sig, int exit_code, struct siginfo *info)
  119. {
  120. struct task_struct *t;
  121. sigaddset(&current->pending.signal, sig);
  122. recalc_sigpending(current);
  123. current->flags |= PF_SIGNALED;
  124. /* Propagate the signal to all the tasks in
  125.  *  our thread group
  126.  */
  127. if (info && (unsigned long)info != 1
  128.     && info->si_code != SI_TKILL) {
  129. read_lock(&tasklist_lock);
  130. for_each_thread(t) {
  131. force_sig_info(sig, info, t);
  132. }
  133. read_unlock(&tasklist_lock);
  134. }
  135. do_exit(exit_code);
  136. /* NOTREACHED */
  137. }
  138. /* Notify the system that a driver wants to block all signals for this
  139.  * process, and wants to be notified if any signals at all were to be
  140.  * sent/acted upon.  If the notifier routine returns non-zero, then the
  141.  * signal will be acted upon after all.  If the notifier routine returns 0,
  142.  * then then signal will be blocked.  Only one block per process is
  143.  * allowed.  priv is a pointer to private data that the notifier routine
  144.  * can use to determine if the signal should be blocked or not.  */
  145. void
  146. block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&current->sigmask_lock, flags);
  150. current->notifier_mask = mask;
  151. current->notifier_data = priv;
  152. current->notifier = notifier;
  153. spin_unlock_irqrestore(&current->sigmask_lock, flags);
  154. }
  155. /* Notify the system that blocking has ended. */
  156. void
  157. unblock_all_signals(void)
  158. {
  159. unsigned long flags;
  160. spin_lock_irqsave(&current->sigmask_lock, flags);
  161. current->notifier = NULL;
  162. current->notifier_data = NULL;
  163. recalc_sigpending(current);
  164. spin_unlock_irqrestore(&current->sigmask_lock, flags);
  165. }
  166. static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  167. {
  168. if (sigismember(&list->signal, sig)) {
  169. /* Collect the siginfo appropriate to this signal.  */
  170. struct sigqueue *q, **pp;
  171. pp = &list->head;
  172. while ((q = *pp) != NULL) {
  173. if (q->info.si_signo == sig)
  174. goto found_it;
  175. pp = &q->next;
  176. }
  177. /* Ok, it wasn't in the queue.  We must have
  178.    been out of queue space.  So zero out the
  179.    info.  */
  180. sigdelset(&list->signal, sig);
  181. info->si_signo = sig;
  182. info->si_errno = 0;
  183. info->si_code = 0;
  184. info->si_pid = 0;
  185. info->si_uid = 0;
  186. return 1;
  187. found_it:
  188. if ((*pp = q->next) == NULL)
  189. list->tail = pp;
  190. /* Copy the sigqueue information and free the queue entry */
  191. copy_siginfo(info, &q->info);
  192. kmem_cache_free(sigqueue_cachep,q);
  193. atomic_dec(&nr_queued_signals);
  194. /* Non-RT signals can exist multiple times.. */
  195. if (sig >= SIGRTMIN) {
  196. while ((q = *pp) != NULL) {
  197. if (q->info.si_signo == sig)
  198. goto found_another;
  199. pp = &q->next;
  200. }
  201. }
  202. sigdelset(&list->signal, sig);
  203. found_another:
  204. return 1;
  205. }
  206. return 0;
  207. }
  208. /*
  209.  * Dequeue a signal and return the element to the caller, which is 
  210.  * expected to free it.
  211.  *
  212.  * All callers must be holding current->sigmask_lock.
  213.  */
  214. int
  215. dequeue_signal(sigset_t *mask, siginfo_t *info)
  216. {
  217. int sig = 0;
  218. sig = next_signal(current, mask);
  219. if (sig) {
  220. if (current->notifier) {
  221. if (sigismember(current->notifier_mask, sig)) {
  222. if (!(current->notifier)(current->notifier_data)) {
  223. current->sigpending = 0;
  224. return 0;
  225. }
  226. }
  227. }
  228. if (!collect_signal(sig, &current->pending, info))
  229. sig = 0;
  230. /* XXX: Once POSIX.1b timers are in, if si_code == SI_TIMER,
  231.    we need to xchg out the timer overrun values.  */
  232. }
  233. recalc_sigpending(current);
  234. return sig;
  235. }
  236. static int rm_from_queue(int sig, struct sigpending *s)
  237. {
  238. struct sigqueue *q, **pp;
  239. if (!sigismember(&s->signal, sig))
  240. return 0;
  241. sigdelset(&s->signal, sig);
  242. pp = &s->head;
  243. while ((q = *pp) != NULL) {
  244. if (q->info.si_signo == sig) {
  245. if ((*pp = q->next) == NULL)
  246. s->tail = pp;
  247. kmem_cache_free(sigqueue_cachep,q);
  248. atomic_dec(&nr_queued_signals);
  249. continue;
  250. }
  251. pp = &q->next;
  252. }
  253. return 1;
  254. }
  255. /*
  256.  * Remove signal sig from t->pending.
  257.  * Returns 1 if sig was found.
  258.  *
  259.  * All callers must be holding t->sigmask_lock.
  260.  */
  261. static int rm_sig_from_queue(int sig, struct task_struct *t)
  262. {
  263. return rm_from_queue(sig, &t->pending);
  264. }
  265. /*
  266.  * Bad permissions for sending the signal
  267.  */
  268. int bad_signal(int sig, struct siginfo *info, struct task_struct *t)
  269. {
  270. return (!info || ((unsigned long)info != 1 && SI_FROMUSER(info)))
  271.     && ((sig != SIGCONT) || (current->session != t->session))
  272.     && (current->euid ^ t->suid) && (current->euid ^ t->uid)
  273.     && (current->uid ^ t->suid) && (current->uid ^ t->uid)
  274.     && !capable(CAP_KILL);
  275. }
  276. /*
  277.  * Signal type:
  278.  *    < 0 : global action (kill - spread to all non-blocked threads)
  279.  *    = 0 : ignored
  280.  *    > 0 : wake up.
  281.  */
  282. static int signal_type(int sig, struct signal_struct *signals)
  283. {
  284. unsigned long handler;
  285. if (!signals)
  286. return 0;
  287. handler = (unsigned long) signals->action[sig-1].sa.sa_handler;
  288. if (handler > 1)
  289. return 1;
  290. /* "Ignore" handler.. Illogical, but that has an implicit handler for SIGCHLD */
  291. if (handler == 1)
  292. return sig == SIGCHLD;
  293. /* Default handler. Normally lethal, but.. */
  294. switch (sig) {
  295. /* Ignored */
  296. case SIGCONT: case SIGWINCH:
  297. case SIGCHLD: case SIGURG:
  298. return 0;
  299. /* Implicit behaviour */
  300. case SIGTSTP: case SIGTTIN: case SIGTTOU:
  301. return 1;
  302. /* Implicit actions (kill or do special stuff) */
  303. default:
  304. return -1;
  305. }
  306. }
  307. /*
  308.  * Determine whether a signal should be posted or not.
  309.  *
  310.  * Signals with SIG_IGN can be ignored, except for the
  311.  * special case of a SIGCHLD. 
  312.  *
  313.  * Some signals with SIG_DFL default to a non-action.
  314.  */
  315. static int ignored_signal(int sig, struct task_struct *t)
  316. {
  317. /* Don't ignore traced or blocked signals */
  318. if ((t->ptrace & PT_PTRACED) || sigismember(&t->blocked, sig))
  319. return 0;
  320. return signal_type(sig, t->sig) == 0;
  321. }
  322. /*
  323.  * Handle TASK_STOPPED cases etc implicit behaviour
  324.  * of certain magical signals.
  325.  *
  326.  * SIGKILL gets spread out to every thread. 
  327.  */
  328. static void handle_stop_signal(int sig, struct task_struct *t)
  329. {
  330. switch (sig) {
  331. case SIGKILL: case SIGCONT:
  332. /* Wake up the process if stopped.  */
  333. if (t->state == TASK_STOPPED)
  334. wake_up_process(t);
  335. t->exit_code = 0;
  336. rm_sig_from_queue(SIGSTOP, t);
  337. rm_sig_from_queue(SIGTSTP, t);
  338. rm_sig_from_queue(SIGTTOU, t);
  339. rm_sig_from_queue(SIGTTIN, t);
  340. break;
  341. case SIGSTOP: case SIGTSTP:
  342. case SIGTTIN: case SIGTTOU:
  343. /* If we're stopping again, cancel SIGCONT */
  344. rm_sig_from_queue(SIGCONT, t);
  345. break;
  346. }
  347. }
  348. static int send_signal(int sig, struct siginfo *info, struct sigpending *signals)
  349. {
  350. struct sigqueue * q = NULL;
  351. /* Real-time signals must be queued if sent by sigqueue, or
  352.    some other real-time mechanism.  It is implementation
  353.    defined whether kill() does so.  We attempt to do so, on
  354.    the principle of least surprise, but since kill is not
  355.    allowed to fail with EAGAIN when low on memory we just
  356.    make sure at least one signal gets delivered and don't
  357.    pass on the info struct.  */
  358. if (atomic_read(&nr_queued_signals) < max_queued_signals) {
  359. q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
  360. }
  361. if (q) {
  362. atomic_inc(&nr_queued_signals);
  363. q->next = NULL;
  364. *signals->tail = q;
  365. signals->tail = &q->next;
  366. switch ((unsigned long) info) {
  367. case 0:
  368. q->info.si_signo = sig;
  369. q->info.si_errno = 0;
  370. q->info.si_code = SI_USER;
  371. q->info.si_pid = current->pid;
  372. q->info.si_uid = current->uid;
  373. break;
  374. case 1:
  375. q->info.si_signo = sig;
  376. q->info.si_errno = 0;
  377. q->info.si_code = SI_KERNEL;
  378. q->info.si_pid = 0;
  379. q->info.si_uid = 0;
  380. break;
  381. default:
  382. copy_siginfo(&q->info, info);
  383. break;
  384. }
  385. } else if (sig >= SIGRTMIN && info && (unsigned long)info != 1
  386.    && info->si_code != SI_USER) {
  387. /*
  388.  * Queue overflow, abort.  We may abort if the signal was rt
  389.  * and sent by user using something other than kill().
  390.  */
  391. return -EAGAIN;
  392. }
  393. sigaddset(&signals->signal, sig);
  394. return 0;
  395. }
  396. /*
  397.  * Tell a process that it has a new active signal..
  398.  *
  399.  * NOTE! we rely on the previous spin_lock to
  400.  * lock interrupts for us! We can only be called with
  401.  * "sigmask_lock" held, and the local interrupt must
  402.  * have been disabled when that got acquired!
  403.  *
  404.  * No need to set need_resched since signal event passing
  405.  * goes through ->blocked
  406.  */
  407. static inline void signal_wake_up(struct task_struct *t)
  408. {
  409. t->sigpending = 1;
  410. #ifdef CONFIG_SMP
  411. /*
  412.  * If the task is running on a different CPU 
  413.  * force a reschedule on the other CPU to make
  414.  * it notice the new signal quickly.
  415.  *
  416.  * The code below is a tad loose and might occasionally
  417.  * kick the wrong CPU if we catch the process in the
  418.  * process of changing - but no harm is done by that
  419.  * other than doing an extra (lightweight) IPI interrupt.
  420.  */
  421. spin_lock(&runqueue_lock);
  422. if (task_has_cpu(t) && t->processor != smp_processor_id())
  423. smp_send_reschedule(t->processor);
  424. spin_unlock(&runqueue_lock);
  425. #endif /* CONFIG_SMP */
  426. if (t->state & TASK_INTERRUPTIBLE) {
  427. wake_up_process(t);
  428. return;
  429. }
  430. }
  431. static int deliver_signal(int sig, struct siginfo *info, struct task_struct *t)
  432. {
  433. int retval = send_signal(sig, info, &t->pending);
  434. if (!retval && !sigismember(&t->blocked, sig))
  435. signal_wake_up(t);
  436. return retval;
  437. }
  438. int
  439. send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  440. {
  441. unsigned long flags;
  442. int ret;
  443. ret = -EINVAL;
  444. if (sig < 0 || sig > _NSIG)
  445. goto out_nolock;
  446. /* The somewhat baroque permissions check... */
  447. ret = -EPERM;
  448. if (bad_signal(sig, info, t))
  449. goto out_nolock;
  450. /* The null signal is a permissions and process existence probe.
  451.    No signal is actually delivered.  Same goes for zombies. */
  452. ret = 0;
  453. if (!sig || !t->sig)
  454. goto out_nolock;
  455. spin_lock_irqsave(&t->sigmask_lock, flags);
  456. handle_stop_signal(sig, t);
  457. /* Optimize away the signal, if it's a signal that can be
  458.    handled immediately (ie non-blocked and untraced) and
  459.    that is ignored (either explicitly or by default).  */
  460. if (ignored_signal(sig, t))
  461. goto out;
  462. /* Support queueing exactly one non-rt signal, so that we
  463.    can get more detailed information about the cause of
  464.    the signal. */
  465. if (sig < SIGRTMIN && sigismember(&t->pending.signal, sig))
  466. goto out;
  467. ret = deliver_signal(sig, info, t);
  468. out:
  469. spin_unlock_irqrestore(&t->sigmask_lock, flags);
  470. out_nolock:
  471. return ret;
  472. }
  473. /*
  474.  * Force a signal that the process can't ignore: if necessary
  475.  * we unblock the signal and change any SIG_IGN to SIG_DFL.
  476.  */
  477. int
  478. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  479. {
  480. unsigned long int flags;
  481. spin_lock_irqsave(&t->sigmask_lock, flags);
  482. if (t->sig == NULL) {
  483. spin_unlock_irqrestore(&t->sigmask_lock, flags);
  484. return -ESRCH;
  485. }
  486. if (t->sig->action[sig-1].sa.sa_handler == SIG_IGN)
  487. t->sig->action[sig-1].sa.sa_handler = SIG_DFL;
  488. sigdelset(&t->blocked, sig);
  489. recalc_sigpending(t);
  490. spin_unlock_irqrestore(&t->sigmask_lock, flags);
  491. return send_sig_info(sig, info, t);
  492. }
  493. /*
  494.  * kill_pg_info() sends a signal to a process group: this is what the tty
  495.  * control characters do (^C, ^Z etc)
  496.  */
  497. int
  498. kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
  499. {
  500. int retval = -EINVAL;
  501. if (pgrp > 0) {
  502. struct task_struct *p;
  503. retval = -ESRCH;
  504. read_lock(&tasklist_lock);
  505. for_each_task(p) {
  506. if (p->pgrp == pgrp && thread_group_leader(p)) {
  507. int err = send_sig_info(sig, info, p);
  508. if (retval)
  509. retval = err;
  510. }
  511. }
  512. read_unlock(&tasklist_lock);
  513. }
  514. return retval;
  515. }
  516. /*
  517.  * kill_sl_info() sends a signal to the session leader: this is used
  518.  * to send SIGHUP to the controlling process of a terminal when
  519.  * the connection is lost.
  520.  */
  521. int
  522. kill_sl_info(int sig, struct siginfo *info, pid_t sess)
  523. {
  524. int retval = -EINVAL;
  525. if (sess > 0) {
  526. struct task_struct *p;
  527. retval = -ESRCH;
  528. read_lock(&tasklist_lock);
  529. for_each_task(p) {
  530. if (p->leader && p->session == sess) {
  531. int err = send_sig_info(sig, info, p);
  532. if (retval)
  533. retval = err;
  534. }
  535. }
  536. read_unlock(&tasklist_lock);
  537. }
  538. return retval;
  539. }
  540. inline int
  541. kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  542. {
  543. int error;
  544. struct task_struct *p;
  545. read_lock(&tasklist_lock);
  546. p = find_task_by_pid(pid);
  547. error = -ESRCH;
  548. if (p) {
  549. if (!thread_group_leader(p)) {
  550.                        struct task_struct *tg;
  551.                        tg = find_task_by_pid(p->tgid);
  552.                        if (tg)
  553.                                p = tg;
  554.                 }
  555. error = send_sig_info(sig, info, p);
  556. }
  557. read_unlock(&tasklist_lock);
  558. return error;
  559. }
  560. /*
  561.  * kill_something_info() interprets pid in interesting ways just like kill(2).
  562.  *
  563.  * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  564.  * is probably wrong.  Should make it like BSD or SYSV.
  565.  */
  566. static int kill_something_info(int sig, struct siginfo *info, int pid)
  567. {
  568. if (!pid) {
  569. return kill_pg_info(sig, info, current->pgrp);
  570. } else if (pid == -1) {
  571. int retval = 0, count = 0;
  572. struct task_struct * p;
  573. read_lock(&tasklist_lock);
  574. for_each_task(p) {
  575. if (p->pid > 1 && p != current && thread_group_leader(p)) {
  576. int err = send_sig_info(sig, info, p);
  577. ++count;
  578. if (err != -EPERM)
  579. retval = err;
  580. }
  581. }
  582. read_unlock(&tasklist_lock);
  583. return count ? retval : -ESRCH;
  584. } else if (pid < 0) {
  585. return kill_pg_info(sig, info, -pid);
  586. } else {
  587. return kill_proc_info(sig, info, pid);
  588. }
  589. }
  590. /*
  591.  * These are for backward compatibility with the rest of the kernel source.
  592.  */
  593. int
  594. send_sig(int sig, struct task_struct *p, int priv)
  595. {
  596. return send_sig_info(sig, (void*)(long)(priv != 0), p);
  597. }
  598. void
  599. force_sig(int sig, struct task_struct *p)
  600. {
  601. force_sig_info(sig, (void*)1L, p);
  602. }
  603. int
  604. kill_pg(pid_t pgrp, int sig, int priv)
  605. {
  606. return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
  607. }
  608. int
  609. kill_sl(pid_t sess, int sig, int priv)
  610. {
  611. return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
  612. }
  613. int
  614. kill_proc(pid_t pid, int sig, int priv)
  615. {
  616. return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
  617. }
  618. /*
  619.  * Joy. Or not. Pthread wants us to wake up every thread
  620.  * in our parent group.
  621.  */
  622. static void wake_up_parent(struct task_struct *parent)
  623. {
  624. struct task_struct *tsk = parent;
  625. do {
  626. wake_up_interruptible(&tsk->wait_chldexit);
  627. tsk = next_thread(tsk);
  628. } while (tsk != parent);
  629. }
  630. /*
  631.  * Let a parent know about a status change of a child.
  632.  */
  633. void do_notify_parent(struct task_struct *tsk, int sig)
  634. {
  635. struct siginfo info;
  636. int why, status;
  637. info.si_signo = sig;
  638. info.si_errno = 0;
  639. info.si_pid = tsk->pid;
  640. info.si_uid = tsk->uid;
  641. /* FIXME: find out whether or not this is supposed to be c*time. */
  642. info.si_utime = tsk->times.tms_utime;
  643. info.si_stime = tsk->times.tms_stime;
  644. status = tsk->exit_code & 0x7f;
  645. why = SI_KERNEL; /* shouldn't happen */
  646. switch (tsk->state) {
  647. case TASK_STOPPED:
  648. /* FIXME -- can we deduce CLD_TRAPPED or CLD_CONTINUED? */
  649. if (tsk->ptrace & PT_PTRACED)
  650. why = CLD_TRAPPED;
  651. else
  652. why = CLD_STOPPED;
  653. break;
  654. default:
  655. if (tsk->exit_code & 0x80)
  656. why = CLD_DUMPED;
  657. else if (tsk->exit_code & 0x7f)
  658. why = CLD_KILLED;
  659. else {
  660. why = CLD_EXITED;
  661. status = tsk->exit_code >> 8;
  662. }
  663. break;
  664. }
  665. info.si_code = why;
  666. info.si_status = status;
  667. send_sig_info(sig, &info, tsk->p_pptr);
  668. wake_up_parent(tsk->p_pptr);
  669. }
  670. /*
  671.  * We need the tasklist lock because it's the only
  672.  * thing that protects out "parent" pointer.
  673.  *
  674.  * exit.c calls "do_notify_parent()" directly, because
  675.  * it already has the tasklist lock.
  676.  */
  677. void
  678. notify_parent(struct task_struct *tsk, int sig)
  679. {
  680. read_lock(&tasklist_lock);
  681. do_notify_parent(tsk, sig);
  682. read_unlock(&tasklist_lock);
  683. }
  684. EXPORT_SYMBOL(dequeue_signal);
  685. EXPORT_SYMBOL(flush_signals);
  686. EXPORT_SYMBOL(force_sig);
  687. EXPORT_SYMBOL(force_sig_info);
  688. EXPORT_SYMBOL(kill_pg);
  689. EXPORT_SYMBOL(kill_pg_info);
  690. EXPORT_SYMBOL(kill_proc);
  691. EXPORT_SYMBOL(kill_proc_info);
  692. EXPORT_SYMBOL(kill_sl);
  693. EXPORT_SYMBOL(kill_sl_info);
  694. EXPORT_SYMBOL(notify_parent);
  695. EXPORT_SYMBOL(recalc_sigpending);
  696. EXPORT_SYMBOL(send_sig);
  697. EXPORT_SYMBOL(send_sig_info);
  698. EXPORT_SYMBOL(block_all_signals);
  699. EXPORT_SYMBOL(unblock_all_signals);
  700. /*
  701.  * System call entry points.
  702.  */
  703. /*
  704.  * We don't need to get the kernel lock - this is all local to this
  705.  * particular thread.. (and that's good, because this is _heavily_
  706.  * used by various programs)
  707.  */
  708. asmlinkage long
  709. sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset, size_t sigsetsize)
  710. {
  711. int error = -EINVAL;
  712. sigset_t old_set, new_set;
  713. /* XXX: Don't preclude handling different sized sigset_t's.  */
  714. if (sigsetsize != sizeof(sigset_t))
  715. goto out;
  716. if (set) {
  717. error = -EFAULT;
  718. if (copy_from_user(&new_set, set, sizeof(*set)))
  719. goto out;
  720. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  721. spin_lock_irq(&current->sigmask_lock);
  722. old_set = current->blocked;
  723. error = 0;
  724. switch (how) {
  725. default:
  726. error = -EINVAL;
  727. break;
  728. case SIG_BLOCK:
  729. sigorsets(&new_set, &old_set, &new_set);
  730. break;
  731. case SIG_UNBLOCK:
  732. signandsets(&new_set, &old_set, &new_set);
  733. break;
  734. case SIG_SETMASK:
  735. break;
  736. }
  737. current->blocked = new_set;
  738. recalc_sigpending(current);
  739. spin_unlock_irq(&current->sigmask_lock);
  740. if (error)
  741. goto out;
  742. if (oset)
  743. goto set_old;
  744. } else if (oset) {
  745. spin_lock_irq(&current->sigmask_lock);
  746. old_set = current->blocked;
  747. spin_unlock_irq(&current->sigmask_lock);
  748. set_old:
  749. error = -EFAULT;
  750. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  751. goto out;
  752. }
  753. error = 0;
  754. out:
  755. return error;
  756. }
  757. long do_sigpending(void *set, unsigned long sigsetsize)
  758. {
  759. long error = -EINVAL;
  760. sigset_t pending;
  761. if (sigsetsize > sizeof(sigset_t))
  762. goto out;
  763. spin_lock_irq(&current->sigmask_lock);
  764. sigandsets(&pending, &current->blocked, &current->pending.signal);
  765. spin_unlock_irq(&current->sigmask_lock);
  766. error = -EFAULT;
  767. if (!copy_to_user(set, &pending, sigsetsize))
  768. error = 0;
  769. out:
  770. return error;
  771. }
  772. asmlinkage long
  773. sys_rt_sigpending(sigset_t *set, size_t sigsetsize)
  774. {
  775. return do_sigpending(set, sigsetsize);
  776. }
  777. asmlinkage long
  778. sys_rt_sigtimedwait(const sigset_t *uthese, siginfo_t *uinfo,
  779.     const struct timespec *uts, size_t sigsetsize)
  780. {
  781. int ret, sig;
  782. sigset_t these;
  783. struct timespec ts;
  784. siginfo_t info;
  785. long timeout = 0;
  786. /* XXX: Don't preclude handling different sized sigset_t's.  */
  787. if (sigsetsize != sizeof(sigset_t))
  788. return -EINVAL;
  789. if (copy_from_user(&these, uthese, sizeof(these)))
  790. return -EFAULT;
  791. /*
  792.  * Invert the set of allowed signals to get those we
  793.  * want to block.
  794.  */
  795. sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
  796. signotset(&these);
  797. if (uts) {
  798. if (copy_from_user(&ts, uts, sizeof(ts)))
  799. return -EFAULT;
  800. if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
  801.     || ts.tv_sec < 0)
  802. return -EINVAL;
  803. }
  804. spin_lock_irq(&current->sigmask_lock);
  805. sig = dequeue_signal(&these, &info);
  806. if (!sig) {
  807. timeout = MAX_SCHEDULE_TIMEOUT;
  808. if (uts)
  809. timeout = (timespec_to_jiffies(&ts)
  810.    + (ts.tv_sec || ts.tv_nsec));
  811. if (timeout) {
  812. /* None ready -- temporarily unblock those we're
  813.  * interested while we are sleeping in so that we'll
  814.  * be awakened when they arrive.  */
  815. sigset_t oldblocked = current->blocked;
  816. sigandsets(&current->blocked, &current->blocked, &these);
  817. recalc_sigpending(current);
  818. spin_unlock_irq(&current->sigmask_lock);
  819. current->state = TASK_INTERRUPTIBLE;
  820. timeout = schedule_timeout(timeout);
  821. spin_lock_irq(&current->sigmask_lock);
  822. sig = dequeue_signal(&these, &info);
  823. current->blocked = oldblocked;
  824. recalc_sigpending(current);
  825. }
  826. }
  827. spin_unlock_irq(&current->sigmask_lock);
  828. if (sig) {
  829. ret = sig;
  830. if (uinfo) {
  831. if (copy_siginfo_to_user(uinfo, &info))
  832. ret = -EFAULT;
  833. }
  834. } else {
  835. ret = -EAGAIN;
  836. if (timeout)
  837. ret = -EINTR;
  838. }
  839. return ret;
  840. }
  841. asmlinkage long
  842. sys_kill(int pid, int sig)
  843. {
  844. struct siginfo info;
  845. info.si_signo = sig;
  846. info.si_errno = 0;
  847. info.si_code = SI_USER;
  848. info.si_pid = current->pid;
  849. info.si_uid = current->uid;
  850. return kill_something_info(sig, &info, pid);
  851. }
  852. /*
  853.  *  Kill only one task, even if it's a CLONE_THREAD task.
  854.  */
  855. asmlinkage long
  856. sys_tkill(int pid, int sig)
  857. {
  858.        struct siginfo info;
  859.        int error;
  860.        struct task_struct *p;
  861.        /* This is only valid for single tasks */
  862.        if (pid <= 0)
  863.            return -EINVAL;
  864.        info.si_signo = sig;
  865.        info.si_errno = 0;
  866.        info.si_code = SI_TKILL;
  867.        info.si_pid = current->pid;
  868.        info.si_uid = current->uid;
  869.        read_lock(&tasklist_lock);
  870.        p = find_task_by_pid(pid);
  871.        error = -ESRCH;
  872.        if (p) {
  873.                error = send_sig_info(sig, &info, p);
  874.        }
  875.        read_unlock(&tasklist_lock);
  876.        return error;
  877. }
  878. asmlinkage long
  879. sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo)
  880. {
  881. siginfo_t info;
  882. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  883. return -EFAULT;
  884. /* Not even root can pretend to send signals from the kernel.
  885.    Nor can they impersonate a kill(), which adds source info.  */
  886. if (info.si_code >= 0)
  887. return -EPERM;
  888. info.si_signo = sig;
  889. /* POSIX.1b doesn't mention process groups.  */
  890. return kill_proc_info(sig, &info, pid);
  891. }
  892. int
  893. do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
  894. {
  895. struct k_sigaction *k;
  896. if (sig < 1 || sig > _NSIG ||
  897.     (act && (sig == SIGKILL || sig == SIGSTOP)))
  898. return -EINVAL;
  899. k = &current->sig->action[sig-1];
  900. spin_lock(&current->sig->siglock);
  901. if (oact)
  902. *oact = *k;
  903. if (act) {
  904. *k = *act;
  905. sigdelsetmask(&k->sa.sa_mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  906. /*
  907.  * POSIX 3.3.1.3:
  908.  *  "Setting a signal action to SIG_IGN for a signal that is
  909.  *   pending shall cause the pending signal to be discarded,
  910.  *   whether or not it is blocked."
  911.  *
  912.  *  "Setting a signal action to SIG_DFL for a signal that is
  913.  *   pending and whose default action is to ignore the signal
  914.  *   (for example, SIGCHLD), shall cause the pending signal to
  915.  *   be discarded, whether or not it is blocked"
  916.  *
  917.  * Note the silly behaviour of SIGCHLD: SIG_IGN means that the
  918.  * signal isn't actually ignored, but does automatic child
  919.  * reaping, while SIG_DFL is explicitly said by POSIX to force
  920.  * the signal to be ignored.
  921.  */
  922. if (k->sa.sa_handler == SIG_IGN
  923.     || (k->sa.sa_handler == SIG_DFL
  924. && (sig == SIGCONT ||
  925.     sig == SIGCHLD ||
  926.     sig == SIGURG ||
  927.     sig == SIGWINCH))) {
  928. spin_lock_irq(&current->sigmask_lock);
  929. if (rm_sig_from_queue(sig, current))
  930. recalc_sigpending(current);
  931. spin_unlock_irq(&current->sigmask_lock);
  932. }
  933. }
  934. spin_unlock(&current->sig->siglock);
  935. return 0;
  936. }
  937. int 
  938. do_sigaltstack (const stack_t *uss, stack_t *uoss, unsigned long sp)
  939. {
  940. stack_t oss;
  941. int error;
  942. if (uoss) {
  943. oss.ss_sp = (void *) current->sas_ss_sp;
  944. oss.ss_size = current->sas_ss_size;
  945. oss.ss_flags = sas_ss_flags(sp);
  946. }
  947. if (uss) {
  948. void *ss_sp;
  949. size_t ss_size;
  950. int ss_flags;
  951. error = -EFAULT;
  952. if (verify_area(VERIFY_READ, uss, sizeof(*uss))
  953.     || __get_user(ss_sp, &uss->ss_sp)
  954.     || __get_user(ss_flags, &uss->ss_flags)
  955.     || __get_user(ss_size, &uss->ss_size))
  956. goto out;
  957. error = -EPERM;
  958. if (on_sig_stack (sp))
  959. goto out;
  960. error = -EINVAL;
  961. /*
  962.  *
  963.  * Note - this code used to test ss_flags incorrectly
  964.  *     old code may have been written using ss_flags==0
  965.  *   to mean ss_flags==SS_ONSTACK (as this was the only
  966.  *   way that worked) - this fix preserves that older
  967.  *   mechanism
  968.  */
  969. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  970. goto out;
  971. if (ss_flags == SS_DISABLE) {
  972. ss_size = 0;
  973. ss_sp = NULL;
  974. } else {
  975. error = -ENOMEM;
  976. if (ss_size < MINSIGSTKSZ)
  977. goto out;
  978. }
  979. current->sas_ss_sp = (unsigned long) ss_sp;
  980. current->sas_ss_size = ss_size;
  981. }
  982. if (uoss) {
  983. error = -EFAULT;
  984. if (copy_to_user(uoss, &oss, sizeof(oss)))
  985. goto out;
  986. }
  987. error = 0;
  988. out:
  989. return error;
  990. }
  991. asmlinkage long
  992. sys_sigpending(old_sigset_t *set)
  993. {
  994. return do_sigpending(set, sizeof(*set));
  995. }
  996. #if !defined(__alpha__)
  997. /* Alpha has its own versions with special arguments.  */
  998. asmlinkage long
  999. sys_sigprocmask(int how, old_sigset_t *set, old_sigset_t *oset)
  1000. {
  1001. int error;
  1002. old_sigset_t old_set, new_set;
  1003. if (set) {
  1004. error = -EFAULT;
  1005. if (copy_from_user(&new_set, set, sizeof(*set)))
  1006. goto out;
  1007. new_set &= ~(sigmask(SIGKILL)|sigmask(SIGSTOP));
  1008. spin_lock_irq(&current->sigmask_lock);
  1009. old_set = current->blocked.sig[0];
  1010. error = 0;
  1011. switch (how) {
  1012. default:
  1013. error = -EINVAL;
  1014. break;
  1015. case SIG_BLOCK:
  1016. sigaddsetmask(&current->blocked, new_set);
  1017. break;
  1018. case SIG_UNBLOCK:
  1019. sigdelsetmask(&current->blocked, new_set);
  1020. break;
  1021. case SIG_SETMASK:
  1022. current->blocked.sig[0] = new_set;
  1023. break;
  1024. }
  1025. recalc_sigpending(current);
  1026. spin_unlock_irq(&current->sigmask_lock);
  1027. if (error)
  1028. goto out;
  1029. if (oset)
  1030. goto set_old;
  1031. } else if (oset) {
  1032. old_set = current->blocked.sig[0];
  1033. set_old:
  1034. error = -EFAULT;
  1035. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  1036. goto out;
  1037. }
  1038. error = 0;
  1039. out:
  1040. return error;
  1041. }
  1042. #ifndef __sparc__
  1043. asmlinkage long
  1044. sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
  1045.  size_t sigsetsize)
  1046. {
  1047. struct k_sigaction new_sa, old_sa;
  1048. int ret = -EINVAL;
  1049. /* XXX: Don't preclude handling different sized sigset_t's.  */
  1050. if (sigsetsize != sizeof(sigset_t))
  1051. goto out;
  1052. if (act) {
  1053. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  1054. return -EFAULT;
  1055. }
  1056. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  1057. if (!ret && oact) {
  1058. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  1059. return -EFAULT;
  1060. }
  1061. out:
  1062. return ret;
  1063. }
  1064. #endif /* __sparc__ */
  1065. #endif
  1066. #if !defined(__alpha__) && !defined(__ia64__)
  1067. /*
  1068.  * For backwards compatibility.  Functionality superseded by sigprocmask.
  1069.  */
  1070. asmlinkage long
  1071. sys_sgetmask(void)
  1072. {
  1073. /* SMP safe */
  1074. return current->blocked.sig[0];
  1075. }
  1076. asmlinkage long
  1077. sys_ssetmask(int newmask)
  1078. {
  1079. int old;
  1080. spin_lock_irq(&current->sigmask_lock);
  1081. old = current->blocked.sig[0];
  1082. siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
  1083.   sigmask(SIGSTOP)));
  1084. recalc_sigpending(current);
  1085. spin_unlock_irq(&current->sigmask_lock);
  1086. return old;
  1087. }
  1088. #endif /* !defined(__alpha__) */
  1089. #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__)
  1090. /*
  1091.  * For backwards compatibility.  Functionality superseded by sigaction.
  1092.  */
  1093. asmlinkage unsigned long
  1094. sys_signal(int sig, __sighandler_t handler)
  1095. {
  1096. struct k_sigaction new_sa, old_sa;
  1097. int ret;
  1098. new_sa.sa.sa_handler = handler;
  1099. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  1100. ret = do_sigaction(sig, &new_sa, &old_sa);
  1101. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  1102. }
  1103. #endif /* !alpha && !__ia64__ && !defined(__mips__) */