signal.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551
  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. * 2003-06-02 Jim Houston - Concurrent Computer Corp.
  9. * Changes to use preallocated sigqueue structures
  10. * to allow signals to be sent reliably.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/smp_lock.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/fs.h>
  19. #include <linux/tty.h>
  20. #include <linux/binfmts.h>
  21. #include <linux/security.h>
  22. #include <linux/syscalls.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/signal.h>
  25. #include <linux/audit.h>
  26. #include <linux/capability.h>
  27. #include <asm/param.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/unistd.h>
  30. #include <asm/siginfo.h>
  31. /*
  32. * SLAB caches for signal bits.
  33. */
  34. static kmem_cache_t *sigqueue_cachep;
  35. /*
  36. * In POSIX a signal is sent either to a specific thread (Linux task)
  37. * or to the process as a whole (Linux thread group). How the signal
  38. * is sent determines whether it's to one thread or the whole group,
  39. * which determines which signal mask(s) are involved in blocking it
  40. * from being delivered until later. When the signal is delivered,
  41. * either it's caught or ignored by a user handler or it has a default
  42. * effect that applies to the whole thread group (POSIX process).
  43. *
  44. * The possible effects an unblocked signal set to SIG_DFL can have are:
  45. * ignore - Nothing Happens
  46. * terminate - kill the process, i.e. all threads in the group,
  47. * similar to exit_group. The group leader (only) reports
  48. * WIFSIGNALED status to its parent.
  49. * coredump - write a core dump file describing all threads using
  50. * the same mm and then kill all those threads
  51. * stop - stop all the threads in the group, i.e. TASK_STOPPED state
  52. *
  53. * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
  54. * Other signals when not blocked and set to SIG_DFL behaves as follows.
  55. * The job control signals also have other special effects.
  56. *
  57. * +--------------------+------------------+
  58. * | POSIX signal | default action |
  59. * +--------------------+------------------+
  60. * | SIGHUP | terminate |
  61. * | SIGINT | terminate |
  62. * | SIGQUIT | coredump |
  63. * | SIGILL | coredump |
  64. * | SIGTRAP | coredump |
  65. * | SIGABRT/SIGIOT | coredump |
  66. * | SIGBUS | coredump |
  67. * | SIGFPE | coredump |
  68. * | SIGKILL | terminate(+) |
  69. * | SIGUSR1 | terminate |
  70. * | SIGSEGV | coredump |
  71. * | SIGUSR2 | terminate |
  72. * | SIGPIPE | terminate |
  73. * | SIGALRM | terminate |
  74. * | SIGTERM | terminate |
  75. * | SIGCHLD | ignore |
  76. * | SIGCONT | ignore(*) |
  77. * | SIGSTOP | stop(*)(+) |
  78. * | SIGTSTP | stop(*) |
  79. * | SIGTTIN | stop(*) |
  80. * | SIGTTOU | stop(*) |
  81. * | SIGURG | ignore |
  82. * | SIGXCPU | coredump |
  83. * | SIGXFSZ | coredump |
  84. * | SIGVTALRM | terminate |
  85. * | SIGPROF | terminate |
  86. * | SIGPOLL/SIGIO | terminate |
  87. * | SIGSYS/SIGUNUSED | coredump |
  88. * | SIGSTKFLT | terminate |
  89. * | SIGWINCH | ignore |
  90. * | SIGPWR | terminate |
  91. * | SIGRTMIN-SIGRTMAX | terminate |
  92. * +--------------------+------------------+
  93. * | non-POSIX signal | default action |
  94. * +--------------------+------------------+
  95. * | SIGEMT | coredump |
  96. * +--------------------+------------------+
  97. *
  98. * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
  99. * (*) Special job control effects:
  100. * When SIGCONT is sent, it resumes the process (all threads in the group)
  101. * from TASK_STOPPED state and also clears any pending/queued stop signals
  102. * (any of those marked with "stop(*)"). This happens regardless of blocking,
  103. * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
  104. * any pending/queued SIGCONT signals; this happens regardless of blocking,
  105. * catching, or ignored the stop signal, though (except for SIGSTOP) the
  106. * default action of stopping the process may happen later or never.
  107. */
  108. #ifdef SIGEMT
  109. #define M_SIGEMT M(SIGEMT)
  110. #else
  111. #define M_SIGEMT 0
  112. #endif
  113. #if SIGRTMIN > BITS_PER_LONG
  114. #define M(sig) (1ULL << ((sig)-1))
  115. #else
  116. #define M(sig) (1UL << ((sig)-1))
  117. #endif
  118. #define T(sig, mask) (M(sig) & (mask))
  119. #define SIG_KERNEL_ONLY_MASK (\
  120. M(SIGKILL) | M(SIGSTOP) )
  121. #define SIG_KERNEL_STOP_MASK (\
  122. M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
  123. #define SIG_KERNEL_COREDUMP_MASK (\
  124. M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
  125. M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
  126. M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
  127. #define SIG_KERNEL_IGNORE_MASK (\
  128. M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
  129. #define sig_kernel_only(sig) \
  130. (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
  131. #define sig_kernel_coredump(sig) \
  132. (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
  133. #define sig_kernel_ignore(sig) \
  134. (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
  135. #define sig_kernel_stop(sig) \
  136. (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
  137. #define sig_needs_tasklist(sig) ((sig) == SIGCONT)
  138. #define sig_user_defined(t, signr) \
  139. (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
  140. ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
  141. #define sig_fatal(t, signr) \
  142. (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
  143. (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
  144. static int sig_ignored(struct task_struct *t, int sig)
  145. {
  146. void __user * handler;
  147. /*
  148. * Tracers always want to know about signals..
  149. */
  150. if (t->ptrace & PT_PTRACED)
  151. return 0;
  152. /*
  153. * Blocked signals are never ignored, since the
  154. * signal handler may change by the time it is
  155. * unblocked.
  156. */
  157. if (sigismember(&t->blocked, sig))
  158. return 0;
  159. /* Is it explicitly or implicitly ignored? */
  160. handler = t->sighand->action[sig-1].sa.sa_handler;
  161. return handler == SIG_IGN ||
  162. (handler == SIG_DFL && sig_kernel_ignore(sig));
  163. }
  164. /*
  165. * Re-calculate pending state from the set of locally pending
  166. * signals, globally pending signals, and blocked signals.
  167. */
  168. static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
  169. {
  170. unsigned long ready;
  171. long i;
  172. switch (_NSIG_WORDS) {
  173. default:
  174. for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
  175. ready |= signal->sig[i] &~ blocked->sig[i];
  176. break;
  177. case 4: ready = signal->sig[3] &~ blocked->sig[3];
  178. ready |= signal->sig[2] &~ blocked->sig[2];
  179. ready |= signal->sig[1] &~ blocked->sig[1];
  180. ready |= signal->sig[0] &~ blocked->sig[0];
  181. break;
  182. case 2: ready = signal->sig[1] &~ blocked->sig[1];
  183. ready |= signal->sig[0] &~ blocked->sig[0];
  184. break;
  185. case 1: ready = signal->sig[0] &~ blocked->sig[0];
  186. }
  187. return ready != 0;
  188. }
  189. #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
  190. fastcall void recalc_sigpending_tsk(struct task_struct *t)
  191. {
  192. if (t->signal->group_stop_count > 0 ||
  193. (freezing(t)) ||
  194. PENDING(&t->pending, &t->blocked) ||
  195. PENDING(&t->signal->shared_pending, &t->blocked))
  196. set_tsk_thread_flag(t, TIF_SIGPENDING);
  197. else
  198. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  199. }
  200. void recalc_sigpending(void)
  201. {
  202. recalc_sigpending_tsk(current);
  203. }
  204. /* Given the mask, find the first available signal that should be serviced. */
  205. static int
  206. next_signal(struct sigpending *pending, sigset_t *mask)
  207. {
  208. unsigned long i, *s, *m, x;
  209. int sig = 0;
  210. s = pending->signal.sig;
  211. m = mask->sig;
  212. switch (_NSIG_WORDS) {
  213. default:
  214. for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
  215. if ((x = *s &~ *m) != 0) {
  216. sig = ffz(~x) + i*_NSIG_BPW + 1;
  217. break;
  218. }
  219. break;
  220. case 2: if ((x = s[0] &~ m[0]) != 0)
  221. sig = 1;
  222. else if ((x = s[1] &~ m[1]) != 0)
  223. sig = _NSIG_BPW + 1;
  224. else
  225. break;
  226. sig += ffz(~x);
  227. break;
  228. case 1: if ((x = *s &~ *m) != 0)
  229. sig = ffz(~x) + 1;
  230. break;
  231. }
  232. return sig;
  233. }
  234. static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
  235. int override_rlimit)
  236. {
  237. struct sigqueue *q = NULL;
  238. atomic_inc(&t->user->sigpending);
  239. if (override_rlimit ||
  240. atomic_read(&t->user->sigpending) <=
  241. t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
  242. q = kmem_cache_alloc(sigqueue_cachep, flags);
  243. if (unlikely(q == NULL)) {
  244. atomic_dec(&t->user->sigpending);
  245. } else {
  246. INIT_LIST_HEAD(&q->list);
  247. q->flags = 0;
  248. q->user = get_uid(t->user);
  249. }
  250. return(q);
  251. }
  252. static void __sigqueue_free(struct sigqueue *q)
  253. {
  254. if (q->flags & SIGQUEUE_PREALLOC)
  255. return;
  256. atomic_dec(&q->user->sigpending);
  257. free_uid(q->user);
  258. kmem_cache_free(sigqueue_cachep, q);
  259. }
  260. void flush_sigqueue(struct sigpending *queue)
  261. {
  262. struct sigqueue *q;
  263. sigemptyset(&queue->signal);
  264. while (!list_empty(&queue->list)) {
  265. q = list_entry(queue->list.next, struct sigqueue , list);
  266. list_del_init(&q->list);
  267. __sigqueue_free(q);
  268. }
  269. }
  270. /*
  271. * Flush all pending signals for a task.
  272. */
  273. void flush_signals(struct task_struct *t)
  274. {
  275. unsigned long flags;
  276. spin_lock_irqsave(&t->sighand->siglock, flags);
  277. clear_tsk_thread_flag(t,TIF_SIGPENDING);
  278. flush_sigqueue(&t->pending);
  279. flush_sigqueue(&t->signal->shared_pending);
  280. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  281. }
  282. /*
  283. * Flush all handlers for a task.
  284. */
  285. void
  286. flush_signal_handlers(struct task_struct *t, int force_default)
  287. {
  288. int i;
  289. struct k_sigaction *ka = &t->sighand->action[0];
  290. for (i = _NSIG ; i != 0 ; i--) {
  291. if (force_default || ka->sa.sa_handler != SIG_IGN)
  292. ka->sa.sa_handler = SIG_DFL;
  293. ka->sa.sa_flags = 0;
  294. sigemptyset(&ka->sa.sa_mask);
  295. ka++;
  296. }
  297. }
  298. /* Notify the system that a driver wants to block all signals for this
  299. * process, and wants to be notified if any signals at all were to be
  300. * sent/acted upon. If the notifier routine returns non-zero, then the
  301. * signal will be acted upon after all. If the notifier routine returns 0,
  302. * then then signal will be blocked. Only one block per process is
  303. * allowed. priv is a pointer to private data that the notifier routine
  304. * can use to determine if the signal should be blocked or not. */
  305. void
  306. block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
  307. {
  308. unsigned long flags;
  309. spin_lock_irqsave(&current->sighand->siglock, flags);
  310. current->notifier_mask = mask;
  311. current->notifier_data = priv;
  312. current->notifier = notifier;
  313. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  314. }
  315. /* Notify the system that blocking has ended. */
  316. void
  317. unblock_all_signals(void)
  318. {
  319. unsigned long flags;
  320. spin_lock_irqsave(&current->sighand->siglock, flags);
  321. current->notifier = NULL;
  322. current->notifier_data = NULL;
  323. recalc_sigpending();
  324. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  325. }
  326. static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  327. {
  328. struct sigqueue *q, *first = NULL;
  329. int still_pending = 0;
  330. if (unlikely(!sigismember(&list->signal, sig)))
  331. return 0;
  332. /*
  333. * Collect the siginfo appropriate to this signal. Check if
  334. * there is another siginfo for the same signal.
  335. */
  336. list_for_each_entry(q, &list->list, list) {
  337. if (q->info.si_signo == sig) {
  338. if (first) {
  339. still_pending = 1;
  340. break;
  341. }
  342. first = q;
  343. }
  344. }
  345. if (first) {
  346. list_del_init(&first->list);
  347. copy_siginfo(info, &first->info);
  348. __sigqueue_free(first);
  349. if (!still_pending)
  350. sigdelset(&list->signal, sig);
  351. } else {
  352. /* Ok, it wasn't in the queue. This must be
  353. a fast-pathed signal or we must have been
  354. out of queue space. So zero out the info.
  355. */
  356. sigdelset(&list->signal, sig);
  357. info->si_signo = sig;
  358. info->si_errno = 0;
  359. info->si_code = 0;
  360. info->si_pid = 0;
  361. info->si_uid = 0;
  362. }
  363. return 1;
  364. }
  365. static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
  366. siginfo_t *info)
  367. {
  368. int sig = 0;
  369. sig = next_signal(pending, mask);
  370. if (sig) {
  371. if (current->notifier) {
  372. if (sigismember(current->notifier_mask, sig)) {
  373. if (!(current->notifier)(current->notifier_data)) {
  374. clear_thread_flag(TIF_SIGPENDING);
  375. return 0;
  376. }
  377. }
  378. }
  379. if (!collect_signal(sig, pending, info))
  380. sig = 0;
  381. }
  382. recalc_sigpending();
  383. return sig;
  384. }
  385. /*
  386. * Dequeue a signal and return the element to the caller, which is
  387. * expected to free it.
  388. *
  389. * All callers have to hold the siglock.
  390. */
  391. int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  392. {
  393. int signr = __dequeue_signal(&tsk->pending, mask, info);
  394. if (!signr)
  395. signr = __dequeue_signal(&tsk->signal->shared_pending,
  396. mask, info);
  397. if (signr && unlikely(sig_kernel_stop(signr))) {
  398. /*
  399. * Set a marker that we have dequeued a stop signal. Our
  400. * caller might release the siglock and then the pending
  401. * stop signal it is about to process is no longer in the
  402. * pending bitmasks, but must still be cleared by a SIGCONT
  403. * (and overruled by a SIGKILL). So those cases clear this
  404. * shared flag after we've set it. Note that this flag may
  405. * remain set after the signal we return is ignored or
  406. * handled. That doesn't matter because its only purpose
  407. * is to alert stop-signal processing code when another
  408. * processor has come along and cleared the flag.
  409. */
  410. if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT))
  411. tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
  412. }
  413. if ( signr &&
  414. ((info->si_code & __SI_MASK) == __SI_TIMER) &&
  415. info->si_sys_private){
  416. /*
  417. * Release the siglock to ensure proper locking order
  418. * of timer locks outside of siglocks. Note, we leave
  419. * irqs disabled here, since the posix-timers code is
  420. * about to disable them again anyway.
  421. */
  422. spin_unlock(&tsk->sighand->siglock);
  423. do_schedule_next_timer(info);
  424. spin_lock(&tsk->sighand->siglock);
  425. }
  426. return signr;
  427. }
  428. /*
  429. * Tell a process that it has a new active signal..
  430. *
  431. * NOTE! we rely on the previous spin_lock to
  432. * lock interrupts for us! We can only be called with
  433. * "siglock" held, and the local interrupt must
  434. * have been disabled when that got acquired!
  435. *
  436. * No need to set need_resched since signal event passing
  437. * goes through ->blocked
  438. */
  439. void signal_wake_up(struct task_struct *t, int resume)
  440. {
  441. unsigned int mask;
  442. set_tsk_thread_flag(t, TIF_SIGPENDING);
  443. /*
  444. * For SIGKILL, we want to wake it up in the stopped/traced case.
  445. * We don't check t->state here because there is a race with it
  446. * executing another processor and just now entering stopped state.
  447. * By using wake_up_state, we ensure the process will wake up and
  448. * handle its death signal.
  449. */
  450. mask = TASK_INTERRUPTIBLE;
  451. if (resume)
  452. mask |= TASK_STOPPED | TASK_TRACED;
  453. if (!wake_up_state(t, mask))
  454. kick_process(t);
  455. }
  456. /*
  457. * Remove signals in mask from the pending set and queue.
  458. * Returns 1 if any signals were found.
  459. *
  460. * All callers must be holding the siglock.
  461. *
  462. * This version takes a sigset mask and looks at all signals,
  463. * not just those in the first mask word.
  464. */
  465. static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
  466. {
  467. struct sigqueue *q, *n;
  468. sigset_t m;
  469. sigandsets(&m, mask, &s->signal);
  470. if (sigisemptyset(&m))
  471. return 0;
  472. signandsets(&s->signal, &s->signal, mask);
  473. list_for_each_entry_safe(q, n, &s->list, list) {
  474. if (sigismember(mask, q->info.si_signo)) {
  475. list_del_init(&q->list);
  476. __sigqueue_free(q);
  477. }
  478. }
  479. return 1;
  480. }
  481. /*
  482. * Remove signals in mask from the pending set and queue.
  483. * Returns 1 if any signals were found.
  484. *
  485. * All callers must be holding the siglock.
  486. */
  487. static int rm_from_queue(unsigned long mask, struct sigpending *s)
  488. {
  489. struct sigqueue *q, *n;
  490. if (!sigtestsetmask(&s->signal, mask))
  491. return 0;
  492. sigdelsetmask(&s->signal, mask);
  493. list_for_each_entry_safe(q, n, &s->list, list) {
  494. if (q->info.si_signo < SIGRTMIN &&
  495. (mask & sigmask(q->info.si_signo))) {
  496. list_del_init(&q->list);
  497. __sigqueue_free(q);
  498. }
  499. }
  500. return 1;
  501. }
  502. /*
  503. * Bad permissions for sending the signal
  504. */
  505. static int check_kill_permission(int sig, struct siginfo *info,
  506. struct task_struct *t)
  507. {
  508. int error = -EINVAL;
  509. if (!valid_signal(sig))
  510. return error;
  511. error = -EPERM;
  512. if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
  513. && ((sig != SIGCONT) ||
  514. (current->signal->session != t->signal->session))
  515. && (current->euid ^ t->suid) && (current->euid ^ t->uid)
  516. && (current->uid ^ t->suid) && (current->uid ^ t->uid)
  517. && !capable(CAP_KILL))
  518. return error;
  519. error = security_task_kill(t, info, sig);
  520. if (!error)
  521. audit_signal_info(sig, t); /* Let audit system see the signal */
  522. return error;
  523. }
  524. /* forward decl */
  525. static void do_notify_parent_cldstop(struct task_struct *tsk, int why);
  526. /*
  527. * Handle magic process-wide effects of stop/continue signals.
  528. * Unlike the signal actions, these happen immediately at signal-generation
  529. * time regardless of blocking, ignoring, or handling. This does the
  530. * actual continuing for SIGCONT, but not the actual stopping for stop
  531. * signals. The process stop is done as a signal action for SIG_DFL.
  532. */
  533. static void handle_stop_signal(int sig, struct task_struct *p)
  534. {
  535. struct task_struct *t;
  536. if (p->signal->flags & SIGNAL_GROUP_EXIT)
  537. /*
  538. * The process is in the middle of dying already.
  539. */
  540. return;
  541. if (sig_kernel_stop(sig)) {
  542. /*
  543. * This is a stop signal. Remove SIGCONT from all queues.
  544. */
  545. rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
  546. t = p;
  547. do {
  548. rm_from_queue(sigmask(SIGCONT), &t->pending);
  549. t = next_thread(t);
  550. } while (t != p);
  551. } else if (sig == SIGCONT) {
  552. /*
  553. * Remove all stop signals from all queues,
  554. * and wake all threads.
  555. */
  556. if (unlikely(p->signal->group_stop_count > 0)) {
  557. /*
  558. * There was a group stop in progress. We'll
  559. * pretend it finished before we got here. We are
  560. * obliged to report it to the parent: if the
  561. * SIGSTOP happened "after" this SIGCONT, then it
  562. * would have cleared this pending SIGCONT. If it
  563. * happened "before" this SIGCONT, then the parent
  564. * got the SIGCHLD about the stop finishing before
  565. * the continue happened. We do the notification
  566. * now, and it's as if the stop had finished and
  567. * the SIGCHLD was pending on entry to this kill.
  568. */
  569. p->signal->group_stop_count = 0;
  570. p->signal->flags = SIGNAL_STOP_CONTINUED;
  571. spin_unlock(&p->sighand->siglock);
  572. do_notify_parent_cldstop(p, CLD_STOPPED);
  573. spin_lock(&p->sighand->siglock);
  574. }
  575. rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
  576. t = p;
  577. do {
  578. unsigned int state;
  579. rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
  580. /*
  581. * If there is a handler for SIGCONT, we must make
  582. * sure that no thread returns to user mode before
  583. * we post the signal, in case it was the only
  584. * thread eligible to run the signal handler--then
  585. * it must not do anything between resuming and
  586. * running the handler. With the TIF_SIGPENDING
  587. * flag set, the thread will pause and acquire the
  588. * siglock that we hold now and until we've queued
  589. * the pending signal.
  590. *
  591. * Wake up the stopped thread _after_ setting
  592. * TIF_SIGPENDING
  593. */
  594. state = TASK_STOPPED;
  595. if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
  596. set_tsk_thread_flag(t, TIF_SIGPENDING);
  597. state |= TASK_INTERRUPTIBLE;
  598. }
  599. wake_up_state(t, state);
  600. t = next_thread(t);
  601. } while (t != p);
  602. if (p->signal->flags & SIGNAL_STOP_STOPPED) {
  603. /*
  604. * We were in fact stopped, and are now continued.
  605. * Notify the parent with CLD_CONTINUED.
  606. */
  607. p->signal->flags = SIGNAL_STOP_CONTINUED;
  608. p->signal->group_exit_code = 0;
  609. spin_unlock(&p->sighand->siglock);
  610. do_notify_parent_cldstop(p, CLD_CONTINUED);
  611. spin_lock(&p->sighand->siglock);
  612. } else {
  613. /*
  614. * We are not stopped, but there could be a stop
  615. * signal in the middle of being processed after
  616. * being removed from the queue. Clear that too.
  617. */
  618. p->signal->flags = 0;
  619. }
  620. } else if (sig == SIGKILL) {
  621. /*
  622. * Make sure that any pending stop signal already dequeued
  623. * is undone by the wakeup for SIGKILL.
  624. */
  625. p->signal->flags = 0;
  626. }
  627. }
  628. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  629. struct sigpending *signals)
  630. {
  631. struct sigqueue * q = NULL;
  632. int ret = 0;
  633. /*
  634. * fast-pathed signals for kernel-internal things like SIGSTOP
  635. * or SIGKILL.
  636. */
  637. if (info == SEND_SIG_FORCED)
  638. goto out_set;
  639. /* Real-time signals must be queued if sent by sigqueue, or
  640. some other real-time mechanism. It is implementation
  641. defined whether kill() does so. We attempt to do so, on
  642. the principle of least surprise, but since kill is not
  643. allowed to fail with EAGAIN when low on memory we just
  644. make sure at least one signal gets delivered and don't
  645. pass on the info struct. */
  646. q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
  647. (is_si_special(info) ||
  648. info->si_code >= 0)));
  649. if (q) {
  650. list_add_tail(&q->list, &signals->list);
  651. switch ((unsigned long) info) {
  652. case (unsigned long) SEND_SIG_NOINFO:
  653. q->info.si_signo = sig;
  654. q->info.si_errno = 0;
  655. q->info.si_code = SI_USER;
  656. q->info.si_pid = current->pid;
  657. q->info.si_uid = current->uid;
  658. break;
  659. case (unsigned long) SEND_SIG_PRIV:
  660. q->info.si_signo = sig;
  661. q->info.si_errno = 0;
  662. q->info.si_code = SI_KERNEL;
  663. q->info.si_pid = 0;
  664. q->info.si_uid = 0;
  665. break;
  666. default:
  667. copy_siginfo(&q->info, info);
  668. break;
  669. }
  670. } else if (!is_si_special(info)) {
  671. if (sig >= SIGRTMIN && info->si_code != SI_USER)
  672. /*
  673. * Queue overflow, abort. We may abort if the signal was rt
  674. * and sent by user using something other than kill().
  675. */
  676. return -EAGAIN;
  677. }
  678. out_set:
  679. sigaddset(&signals->signal, sig);
  680. return ret;
  681. }
  682. #define LEGACY_QUEUE(sigptr, sig) \
  683. (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
  684. static int
  685. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  686. {
  687. int ret = 0;
  688. BUG_ON(!irqs_disabled());
  689. assert_spin_locked(&t->sighand->siglock);
  690. /* Short-circuit ignored signals. */
  691. if (sig_ignored(t, sig))
  692. goto out;
  693. /* Support queueing exactly one non-rt signal, so that we
  694. can get more detailed information about the cause of
  695. the signal. */
  696. if (LEGACY_QUEUE(&t->pending, sig))
  697. goto out;
  698. ret = send_signal(sig, info, t, &t->pending);
  699. if (!ret && !sigismember(&t->blocked, sig))
  700. signal_wake_up(t, sig == SIGKILL);
  701. out:
  702. return ret;
  703. }
  704. /*
  705. * Force a signal that the process can't ignore: if necessary
  706. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  707. */
  708. int
  709. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  710. {
  711. unsigned long int flags;
  712. int ret;
  713. spin_lock_irqsave(&t->sighand->siglock, flags);
  714. if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
  715. t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
  716. }
  717. if (sigismember(&t->blocked, sig)) {
  718. sigdelset(&t->blocked, sig);
  719. }
  720. recalc_sigpending_tsk(t);
  721. ret = specific_send_sig_info(sig, info, t);
  722. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  723. return ret;
  724. }
  725. void
  726. force_sig_specific(int sig, struct task_struct *t)
  727. {
  728. force_sig_info(sig, SEND_SIG_FORCED, t);
  729. }
  730. /*
  731. * Test if P wants to take SIG. After we've checked all threads with this,
  732. * it's equivalent to finding no threads not blocking SIG. Any threads not
  733. * blocking SIG were ruled out because they are not running and already
  734. * have pending signals. Such threads will dequeue from the shared queue
  735. * as soon as they're available, so putting the signal on the shared queue
  736. * will be equivalent to sending it to one such thread.
  737. */
  738. static inline int wants_signal(int sig, struct task_struct *p)
  739. {
  740. if (sigismember(&p->blocked, sig))
  741. return 0;
  742. if (p->flags & PF_EXITING)
  743. return 0;
  744. if (sig == SIGKILL)
  745. return 1;
  746. if (p->state & (TASK_STOPPED | TASK_TRACED))
  747. return 0;
  748. return task_curr(p) || !signal_pending(p);
  749. }
  750. static void
  751. __group_complete_signal(int sig, struct task_struct *p)
  752. {
  753. struct task_struct *t;
  754. /*
  755. * Now find a thread we can wake up to take the signal off the queue.
  756. *
  757. * If the main thread wants the signal, it gets first crack.
  758. * Probably the least surprising to the average bear.
  759. */
  760. if (wants_signal(sig, p))
  761. t = p;
  762. else if (thread_group_empty(p))
  763. /*
  764. * There is just one thread and it does not need to be woken.
  765. * It will dequeue unblocked signals before it runs again.
  766. */
  767. return;
  768. else {
  769. /*
  770. * Otherwise try to find a suitable thread.
  771. */
  772. t = p->signal->curr_target;
  773. if (t == NULL)
  774. /* restart balancing at this thread */
  775. t = p->signal->curr_target = p;
  776. while (!wants_signal(sig, t)) {
  777. t = next_thread(t);
  778. if (t == p->signal->curr_target)
  779. /*
  780. * No thread needs to be woken.
  781. * Any eligible threads will see
  782. * the signal in the queue soon.
  783. */
  784. return;
  785. }
  786. p->signal->curr_target = t;
  787. }
  788. /*
  789. * Found a killable thread. If the signal will be fatal,
  790. * then start taking the whole group down immediately.
  791. */
  792. if (sig_fatal(p, sig) && !(p->signal->flags & SIGNAL_GROUP_EXIT) &&
  793. !sigismember(&t->real_blocked, sig) &&
  794. (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
  795. /*
  796. * This signal will be fatal to the whole group.
  797. */
  798. if (!sig_kernel_coredump(sig)) {
  799. /*
  800. * Start a group exit and wake everybody up.
  801. * This way we don't have other threads
  802. * running and doing things after a slower
  803. * thread has the fatal signal pending.
  804. */
  805. p->signal->flags = SIGNAL_GROUP_EXIT;
  806. p->signal->group_exit_code = sig;
  807. p->signal->group_stop_count = 0;
  808. t = p;
  809. do {
  810. sigaddset(&t->pending.signal, SIGKILL);
  811. signal_wake_up(t, 1);
  812. t = next_thread(t);
  813. } while (t != p);
  814. return;
  815. }
  816. /*
  817. * There will be a core dump. We make all threads other
  818. * than the chosen one go into a group stop so that nothing
  819. * happens until it gets scheduled, takes the signal off
  820. * the shared queue, and does the core dump. This is a
  821. * little more complicated than strictly necessary, but it
  822. * keeps the signal state that winds up in the core dump
  823. * unchanged from the death state, e.g. which thread had
  824. * the core-dump signal unblocked.
  825. */
  826. rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
  827. rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
  828. p->signal->group_stop_count = 0;
  829. p->signal->group_exit_task = t;
  830. t = p;
  831. do {
  832. p->signal->group_stop_count++;
  833. signal_wake_up(t, 0);
  834. t = next_thread(t);
  835. } while (t != p);
  836. wake_up_process(p->signal->group_exit_task);
  837. return;
  838. }
  839. /*
  840. * The signal is already in the shared-pending queue.
  841. * Tell the chosen thread to wake up and dequeue it.
  842. */
  843. signal_wake_up(t, sig == SIGKILL);
  844. return;
  845. }
  846. int
  847. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  848. {
  849. int ret = 0;
  850. assert_spin_locked(&p->sighand->siglock);
  851. handle_stop_signal(sig, p);
  852. /* Short-circuit ignored signals. */
  853. if (sig_ignored(p, sig))
  854. return ret;
  855. if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
  856. /* This is a non-RT signal and we already have one queued. */
  857. return ret;
  858. /*
  859. * Put this signal on the shared-pending queue, or fail with EAGAIN.
  860. * We always use the shared queue for process-wide signals,
  861. * to avoid several races.
  862. */
  863. ret = send_signal(sig, info, p, &p->signal->shared_pending);
  864. if (unlikely(ret))
  865. return ret;
  866. __group_complete_signal(sig, p);
  867. return 0;
  868. }
  869. /*
  870. * Nuke all other threads in the group.
  871. */
  872. void zap_other_threads(struct task_struct *p)
  873. {
  874. struct task_struct *t;
  875. p->signal->flags = SIGNAL_GROUP_EXIT;
  876. p->signal->group_stop_count = 0;
  877. if (thread_group_empty(p))
  878. return;
  879. for (t = next_thread(p); t != p; t = next_thread(t)) {
  880. /*
  881. * Don't bother with already dead threads
  882. */
  883. if (t->exit_state)
  884. continue;
  885. /*
  886. * We don't want to notify the parent, since we are
  887. * killed as part of a thread group due to another
  888. * thread doing an execve() or similar. So set the
  889. * exit signal to -1 to allow immediate reaping of
  890. * the process. But don't detach the thread group
  891. * leader.
  892. */
  893. if (t != p->group_leader)
  894. t->exit_signal = -1;
  895. /* SIGKILL will be handled before any pending SIGSTOP */
  896. sigaddset(&t->pending.signal, SIGKILL);
  897. signal_wake_up(t, 1);
  898. }
  899. }
  900. /*
  901. * Must be called under rcu_read_lock() or with tasklist_lock read-held.
  902. */
  903. struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
  904. {
  905. struct sighand_struct *sighand;
  906. for (;;) {
  907. sighand = rcu_dereference(tsk->sighand);
  908. if (unlikely(sighand == NULL))
  909. break;
  910. spin_lock_irqsave(&sighand->siglock, *flags);
  911. if (likely(sighand == tsk->sighand))
  912. break;
  913. spin_unlock_irqrestore(&sighand->siglock, *flags);
  914. }
  915. return sighand;
  916. }
  917. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  918. {
  919. unsigned long flags;
  920. int ret;
  921. ret = check_kill_permission(sig, info, p);
  922. if (!ret && sig) {
  923. ret = -ESRCH;
  924. if (lock_task_sighand(p, &flags)) {
  925. ret = __group_send_sig_info(sig, info, p);
  926. unlock_task_sighand(p, &flags);
  927. }
  928. }
  929. return ret;
  930. }
  931. /*
  932. * kill_pg_info() sends a signal to a process group: this is what the tty
  933. * control characters do (^C, ^Z etc)
  934. */
  935. int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
  936. {
  937. struct task_struct *p = NULL;
  938. int retval, success;
  939. if (pgrp <= 0)
  940. return -EINVAL;
  941. success = 0;
  942. retval = -ESRCH;
  943. do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
  944. int err = group_send_sig_info(sig, info, p);
  945. success |= !err;
  946. retval = err;
  947. } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
  948. return success ? 0 : retval;
  949. }
  950. int
  951. kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
  952. {
  953. int retval;
  954. read_lock(&tasklist_lock);
  955. retval = __kill_pg_info(sig, info, pgrp);
  956. read_unlock(&tasklist_lock);
  957. return retval;
  958. }
  959. int
  960. kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  961. {
  962. int error;
  963. int acquired_tasklist_lock = 0;
  964. struct task_struct *p;
  965. rcu_read_lock();
  966. if (unlikely(sig_needs_tasklist(sig))) {
  967. read_lock(&tasklist_lock);
  968. acquired_tasklist_lock = 1;
  969. }
  970. p = find_task_by_pid(pid);
  971. error = -ESRCH;
  972. if (p)
  973. error = group_send_sig_info(sig, info, p);
  974. if (unlikely(acquired_tasklist_lock))
  975. read_unlock(&tasklist_lock);
  976. rcu_read_unlock();
  977. return error;
  978. }
  979. /* like kill_proc_info(), but doesn't use uid/euid of "current" */
  980. int kill_proc_info_as_uid(int sig, struct siginfo *info, pid_t pid,
  981. uid_t uid, uid_t euid)
  982. {
  983. int ret = -EINVAL;
  984. struct task_struct *p;
  985. if (!valid_signal(sig))
  986. return ret;
  987. read_lock(&tasklist_lock);
  988. p = find_task_by_pid(pid);
  989. if (!p) {
  990. ret = -ESRCH;
  991. goto out_unlock;
  992. }
  993. if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
  994. && (euid != p->suid) && (euid != p->uid)
  995. && (uid != p->suid) && (uid != p->uid)) {
  996. ret = -EPERM;
  997. goto out_unlock;
  998. }
  999. if (sig && p->sighand) {
  1000. unsigned long flags;
  1001. spin_lock_irqsave(&p->sighand->siglock, flags);
  1002. ret = __group_send_sig_info(sig, info, p);
  1003. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1004. }
  1005. out_unlock:
  1006. read_unlock(&tasklist_lock);
  1007. return ret;
  1008. }
  1009. EXPORT_SYMBOL_GPL(kill_proc_info_as_uid);
  1010. /*
  1011. * kill_something_info() interprets pid in interesting ways just like kill(2).
  1012. *
  1013. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  1014. * is probably wrong. Should make it like BSD or SYSV.
  1015. */
  1016. static int kill_something_info(int sig, struct siginfo *info, int pid)
  1017. {
  1018. if (!pid) {
  1019. return kill_pg_info(sig, info, process_group(current));
  1020. } else if (pid == -1) {
  1021. int retval = 0, count = 0;
  1022. struct task_struct * p;
  1023. read_lock(&tasklist_lock);
  1024. for_each_process(p) {
  1025. if (p->pid > 1 && p->tgid != current->tgid) {
  1026. int err = group_send_sig_info(sig, info, p);
  1027. ++count;
  1028. if (err != -EPERM)
  1029. retval = err;
  1030. }
  1031. }
  1032. read_unlock(&tasklist_lock);
  1033. return count ? retval : -ESRCH;
  1034. } else if (pid < 0) {
  1035. return kill_pg_info(sig, info, -pid);
  1036. } else {
  1037. return kill_proc_info(sig, info, pid);
  1038. }
  1039. }
  1040. /*
  1041. * These are for backward compatibility with the rest of the kernel source.
  1042. */
  1043. /*
  1044. * These two are the most common entry points. They send a signal
  1045. * just to the specific thread.
  1046. */
  1047. int
  1048. send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1049. {
  1050. int ret;
  1051. unsigned long flags;
  1052. /*
  1053. * Make sure legacy kernel users don't send in bad values
  1054. * (normal paths check this in check_kill_permission).
  1055. */
  1056. if (!valid_signal(sig))
  1057. return -EINVAL;
  1058. /*
  1059. * We need the tasklist lock even for the specific
  1060. * thread case (when we don't need to follow the group
  1061. * lists) in order to avoid races with "p->sighand"
  1062. * going away or changing from under us.
  1063. */
  1064. read_lock(&tasklist_lock);
  1065. spin_lock_irqsave(&p->sighand->siglock, flags);
  1066. ret = specific_send_sig_info(sig, info, p);
  1067. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1068. read_unlock(&tasklist_lock);
  1069. return ret;
  1070. }
  1071. #define __si_special(priv) \
  1072. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1073. int
  1074. send_sig(int sig, struct task_struct *p, int priv)
  1075. {
  1076. return send_sig_info(sig, __si_special(priv), p);
  1077. }
  1078. /*
  1079. * This is the entry point for "process-wide" signals.
  1080. * They will go to an appropriate thread in the thread group.
  1081. */
  1082. int
  1083. send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1084. {
  1085. int ret;
  1086. read_lock(&tasklist_lock);
  1087. ret = group_send_sig_info(sig, info, p);
  1088. read_unlock(&tasklist_lock);
  1089. return ret;
  1090. }
  1091. void
  1092. force_sig(int sig, struct task_struct *p)
  1093. {
  1094. force_sig_info(sig, SEND_SIG_PRIV, p);
  1095. }
  1096. /*
  1097. * When things go south during signal handling, we
  1098. * will force a SIGSEGV. And if the signal that caused
  1099. * the problem was already a SIGSEGV, we'll want to
  1100. * make sure we don't even try to deliver the signal..
  1101. */
  1102. int
  1103. force_sigsegv(int sig, struct task_struct *p)
  1104. {
  1105. if (sig == SIGSEGV) {
  1106. unsigned long flags;
  1107. spin_lock_irqsave(&p->sighand->siglock, flags);
  1108. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1109. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1110. }
  1111. force_sig(SIGSEGV, p);
  1112. return 0;
  1113. }
  1114. int
  1115. kill_pg(pid_t pgrp, int sig, int priv)
  1116. {
  1117. return kill_pg_info(sig, __si_special(priv), pgrp);
  1118. }
  1119. int
  1120. kill_proc(pid_t pid, int sig, int priv)
  1121. {
  1122. return kill_proc_info(sig, __si_special(priv), pid);
  1123. }
  1124. /*
  1125. * These functions support sending signals using preallocated sigqueue
  1126. * structures. This is needed "because realtime applications cannot
  1127. * afford to lose notifications of asynchronous events, like timer
  1128. * expirations or I/O completions". In the case of Posix Timers
  1129. * we allocate the sigqueue structure from the timer_create. If this
  1130. * allocation fails we are able to report the failure to the application
  1131. * with an EAGAIN error.
  1132. */
  1133. struct sigqueue *sigqueue_alloc(void)
  1134. {
  1135. struct sigqueue *q;
  1136. if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
  1137. q->flags |= SIGQUEUE_PREALLOC;
  1138. return(q);
  1139. }
  1140. void sigqueue_free(struct sigqueue *q)
  1141. {
  1142. unsigned long flags;
  1143. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1144. /*
  1145. * If the signal is still pending remove it from the
  1146. * pending queue.
  1147. */
  1148. if (unlikely(!list_empty(&q->list))) {
  1149. spinlock_t *lock = &current->sighand->siglock;
  1150. read_lock(&tasklist_lock);
  1151. spin_lock_irqsave(lock, flags);
  1152. if (!list_empty(&q->list))
  1153. list_del_init(&q->list);
  1154. spin_unlock_irqrestore(lock, flags);
  1155. read_unlock(&tasklist_lock);
  1156. }
  1157. q->flags &= ~SIGQUEUE_PREALLOC;
  1158. __sigqueue_free(q);
  1159. }
  1160. int send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
  1161. {
  1162. unsigned long flags;
  1163. int ret = 0;
  1164. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1165. /*
  1166. * The rcu based delayed sighand destroy makes it possible to
  1167. * run this without tasklist lock held. The task struct itself
  1168. * cannot go away as create_timer did get_task_struct().
  1169. *
  1170. * We return -1, when the task is marked exiting, so
  1171. * posix_timer_event can redirect it to the group leader
  1172. */
  1173. rcu_read_lock();
  1174. if (!likely(lock_task_sighand(p, &flags))) {
  1175. ret = -1;
  1176. goto out_err;
  1177. }
  1178. if (unlikely(!list_empty(&q->list))) {
  1179. /*
  1180. * If an SI_TIMER entry is already queue just increment
  1181. * the overrun count.
  1182. */
  1183. BUG_ON(q->info.si_code != SI_TIMER);
  1184. q->info.si_overrun++;
  1185. goto out;
  1186. }
  1187. /* Short-circuit ignored signals. */
  1188. if (sig_ignored(p, sig)) {
  1189. ret = 1;
  1190. goto out;
  1191. }
  1192. list_add_tail(&q->list, &p->pending.list);
  1193. sigaddset(&p->pending.signal, sig);
  1194. if (!sigismember(&p->blocked, sig))
  1195. signal_wake_up(p, sig == SIGKILL);
  1196. out:
  1197. unlock_task_sighand(p, &flags);
  1198. out_err:
  1199. rcu_read_unlock();
  1200. return ret;
  1201. }
  1202. int
  1203. send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
  1204. {
  1205. unsigned long flags;
  1206. int ret = 0;
  1207. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1208. read_lock(&tasklist_lock);
  1209. /* Since it_lock is held, p->sighand cannot be NULL. */
  1210. spin_lock_irqsave(&p->sighand->siglock, flags);
  1211. handle_stop_signal(sig, p);
  1212. /* Short-circuit ignored signals. */
  1213. if (sig_ignored(p, sig)) {
  1214. ret = 1;
  1215. goto out;
  1216. }
  1217. if (unlikely(!list_empty(&q->list))) {
  1218. /*
  1219. * If an SI_TIMER entry is already queue just increment
  1220. * the overrun count. Other uses should not try to
  1221. * send the signal multiple times.
  1222. */
  1223. BUG_ON(q->info.si_code != SI_TIMER);
  1224. q->info.si_overrun++;
  1225. goto out;
  1226. }
  1227. /*
  1228. * Put this signal on the shared-pending queue.
  1229. * We always use the shared queue for process-wide signals,
  1230. * to avoid several races.
  1231. */
  1232. list_add_tail(&q->list, &p->signal->shared_pending.list);
  1233. sigaddset(&p->signal->shared_pending.signal, sig);
  1234. __group_complete_signal(sig, p);
  1235. out:
  1236. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1237. read_unlock(&tasklist_lock);
  1238. return ret;
  1239. }
  1240. /*
  1241. * Wake up any threads in the parent blocked in wait* syscalls.
  1242. */
  1243. static inline void __wake_up_parent(struct task_struct *p,
  1244. struct task_struct *parent)
  1245. {
  1246. wake_up_interruptible_sync(&parent->signal->wait_chldexit);
  1247. }
  1248. /*
  1249. * Let a parent know about the death of a child.
  1250. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1251. */
  1252. void do_notify_parent(struct task_struct *tsk, int sig)
  1253. {
  1254. struct siginfo info;
  1255. unsigned long flags;
  1256. struct sighand_struct *psig;
  1257. BUG_ON(sig == -1);
  1258. /* do_notify_parent_cldstop should have been called instead. */
  1259. BUG_ON(tsk->state & (TASK_STOPPED|TASK_TRACED));
  1260. BUG_ON(!tsk->ptrace &&
  1261. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1262. info.si_signo = sig;
  1263. info.si_errno = 0;
  1264. info.si_pid = tsk->pid;
  1265. info.si_uid = tsk->uid;
  1266. /* FIXME: find out whether or not this is supposed to be c*time. */
  1267. info.si_utime = cputime_to_jiffies(cputime_add(tsk->utime,
  1268. tsk->signal->utime));
  1269. info.si_stime = cputime_to_jiffies(cputime_add(tsk->stime,
  1270. tsk->signal->stime));
  1271. info.si_status = tsk->exit_code & 0x7f;
  1272. if (tsk->exit_code & 0x80)
  1273. info.si_code = CLD_DUMPED;
  1274. else if (tsk->exit_code & 0x7f)
  1275. info.si_code = CLD_KILLED;
  1276. else {
  1277. info.si_code = CLD_EXITED;
  1278. info.si_status = tsk->exit_code >> 8;
  1279. }
  1280. psig = tsk->parent->sighand;
  1281. spin_lock_irqsave(&psig->siglock, flags);
  1282. if (!tsk->ptrace && sig == SIGCHLD &&
  1283. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1284. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1285. /*
  1286. * We are exiting and our parent doesn't care. POSIX.1
  1287. * defines special semantics for setting SIGCHLD to SIG_IGN
  1288. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1289. * automatically and not left for our parent's wait4 call.
  1290. * Rather than having the parent do it as a magic kind of
  1291. * signal handler, we just set this to tell do_exit that we
  1292. * can be cleaned up without becoming a zombie. Note that
  1293. * we still call __wake_up_parent in this case, because a
  1294. * blocked sys_wait4 might now return -ECHILD.
  1295. *
  1296. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1297. * is implementation-defined: we do (if you don't want
  1298. * it, just use SIG_IGN instead).
  1299. */
  1300. tsk->exit_signal = -1;
  1301. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1302. sig = 0;
  1303. }
  1304. if (valid_signal(sig) && sig > 0)
  1305. __group_send_sig_info(sig, &info, tsk->parent);
  1306. __wake_up_parent(tsk, tsk->parent);
  1307. spin_unlock_irqrestore(&psig->siglock, flags);
  1308. }
  1309. static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
  1310. {
  1311. struct siginfo info;
  1312. unsigned long flags;
  1313. struct task_struct *parent;
  1314. struct sighand_struct *sighand;
  1315. if (tsk->ptrace & PT_PTRACED)
  1316. parent = tsk->parent;
  1317. else {
  1318. tsk = tsk->group_leader;
  1319. parent = tsk->real_parent;
  1320. }
  1321. info.si_signo = SIGCHLD;
  1322. info.si_errno = 0;
  1323. info.si_pid = tsk->pid;
  1324. info.si_uid = tsk->uid;
  1325. /* FIXME: find out whether or not this is supposed to be c*time. */
  1326. info.si_utime = cputime_to_jiffies(tsk->utime);
  1327. info.si_stime = cputime_to_jiffies(tsk->stime);
  1328. info.si_code = why;
  1329. switch (why) {
  1330. case CLD_CONTINUED:
  1331. info.si_status = SIGCONT;
  1332. break;
  1333. case CLD_STOPPED:
  1334. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1335. break;
  1336. case CLD_TRAPPED:
  1337. info.si_status = tsk->exit_code & 0x7f;
  1338. break;
  1339. default:
  1340. BUG();
  1341. }
  1342. sighand = parent->sighand;
  1343. spin_lock_irqsave(&sighand->siglock, flags);
  1344. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1345. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1346. __group_send_sig_info(SIGCHLD, &info, parent);
  1347. /*
  1348. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1349. */
  1350. __wake_up_parent(tsk, parent);
  1351. spin_unlock_irqrestore(&sighand->siglock, flags);
  1352. }
  1353. /*
  1354. * This must be called with current->sighand->siglock held.
  1355. *
  1356. * This should be the path for all ptrace stops.
  1357. * We always set current->last_siginfo while stopped here.
  1358. * That makes it a way to test a stopped process for
  1359. * being ptrace-stopped vs being job-control-stopped.
  1360. *
  1361. * If we actually decide not to stop at all because the tracer is gone,
  1362. * we leave nostop_code in current->exit_code.
  1363. */
  1364. static void ptrace_stop(int exit_code, int nostop_code, siginfo_t *info)
  1365. {
  1366. /*
  1367. * If there is a group stop in progress,
  1368. * we must participate in the bookkeeping.
  1369. */
  1370. if (current->signal->group_stop_count > 0)
  1371. --current->signal->group_stop_count;
  1372. current->last_siginfo = info;
  1373. current->exit_code = exit_code;
  1374. /* Let the debugger run. */
  1375. set_current_state(TASK_TRACED);
  1376. spin_unlock_irq(&current->sighand->siglock);
  1377. try_to_freeze();
  1378. read_lock(&tasklist_lock);
  1379. if (likely(current->ptrace & PT_PTRACED) &&
  1380. likely(current->parent != current->real_parent ||
  1381. !(current->ptrace & PT_ATTACHED)) &&
  1382. (likely(current->parent->signal != current->signal) ||
  1383. !unlikely(current->signal->flags & SIGNAL_GROUP_EXIT))) {
  1384. do_notify_parent_cldstop(current, CLD_TRAPPED);
  1385. read_unlock(&tasklist_lock);
  1386. schedule();
  1387. } else {
  1388. /*
  1389. * By the time we got the lock, our tracer went away.
  1390. * Don't stop here.
  1391. */
  1392. read_unlock(&tasklist_lock);
  1393. set_current_state(TASK_RUNNING);
  1394. current->exit_code = nostop_code;
  1395. }
  1396. /*
  1397. * We are back. Now reacquire the siglock before touching
  1398. * last_siginfo, so that we are sure to have synchronized with
  1399. * any signal-sending on another CPU that wants to examine it.
  1400. */
  1401. spin_lock_irq(&current->sighand->siglock);
  1402. current->last_siginfo = NULL;
  1403. /*
  1404. * Queued signals ignored us while we were stopped for tracing.
  1405. * So check for any that we should take before resuming user mode.
  1406. */
  1407. recalc_sigpending();
  1408. }
  1409. void ptrace_notify(int exit_code)
  1410. {
  1411. siginfo_t info;
  1412. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1413. memset(&info, 0, sizeof info);
  1414. info.si_signo = SIGTRAP;
  1415. info.si_code = exit_code;
  1416. info.si_pid = current->pid;
  1417. info.si_uid = current->uid;
  1418. /* Let the debugger run. */
  1419. spin_lock_irq(&current->sighand->siglock);
  1420. ptrace_stop(exit_code, 0, &info);
  1421. spin_unlock_irq(&current->sighand->siglock);
  1422. }
  1423. static void
  1424. finish_stop(int stop_count)
  1425. {
  1426. /*
  1427. * If there are no other threads in the group, or if there is
  1428. * a group stop in progress and we are the last to stop,
  1429. * report to the parent. When ptraced, every thread reports itself.
  1430. */
  1431. if (stop_count == 0 || (current->ptrace & PT_PTRACED)) {
  1432. read_lock(&tasklist_lock);
  1433. do_notify_parent_cldstop(current, CLD_STOPPED);
  1434. read_unlock(&tasklist_lock);
  1435. }
  1436. schedule();
  1437. /*
  1438. * Now we don't run again until continued.
  1439. */
  1440. current->exit_code = 0;
  1441. }
  1442. /*
  1443. * This performs the stopping for SIGSTOP and other stop signals.
  1444. * We have to stop all threads in the thread group.
  1445. * Returns nonzero if we've actually stopped and released the siglock.
  1446. * Returns zero if we didn't stop and still hold the siglock.
  1447. */
  1448. static int do_signal_stop(int signr)
  1449. {
  1450. struct signal_struct *sig = current->signal;
  1451. int stop_count;
  1452. if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED))
  1453. return 0;
  1454. if (sig->group_stop_count > 0) {
  1455. /*
  1456. * There is a group stop in progress. We don't need to
  1457. * start another one.
  1458. */
  1459. stop_count = --sig->group_stop_count;
  1460. } else {
  1461. /*
  1462. * There is no group stop already in progress.
  1463. * We must initiate one now.
  1464. */
  1465. struct task_struct *t;
  1466. sig->group_exit_code = signr;
  1467. stop_count = 0;
  1468. for (t = next_thread(current); t != current; t = next_thread(t))
  1469. /*
  1470. * Setting state to TASK_STOPPED for a group
  1471. * stop is always done with the siglock held,
  1472. * so this check has no races.
  1473. */
  1474. if (!t->exit_state &&
  1475. !(t->state & (TASK_STOPPED|TASK_TRACED))) {
  1476. stop_count++;
  1477. signal_wake_up(t, 0);
  1478. }
  1479. sig->group_stop_count = stop_count;
  1480. }
  1481. if (stop_count == 0)
  1482. sig->flags = SIGNAL_STOP_STOPPED;
  1483. current->exit_code = sig->group_exit_code;
  1484. __set_current_state(TASK_STOPPED);
  1485. spin_unlock_irq(&current->sighand->siglock);
  1486. finish_stop(stop_count);
  1487. return 1;
  1488. }
  1489. /*
  1490. * Do appropriate magic when group_stop_count > 0.
  1491. * We return nonzero if we stopped, after releasing the siglock.
  1492. * We return zero if we still hold the siglock and should look
  1493. * for another signal without checking group_stop_count again.
  1494. */
  1495. static int handle_group_stop(void)
  1496. {
  1497. int stop_count;
  1498. if (current->signal->group_exit_task == current) {
  1499. /*
  1500. * Group stop is so we can do a core dump,
  1501. * We are the initiating thread, so get on with it.
  1502. */
  1503. current->signal->group_exit_task = NULL;
  1504. return 0;
  1505. }
  1506. if (current->signal->flags & SIGNAL_GROUP_EXIT)
  1507. /*
  1508. * Group stop is so another thread can do a core dump,
  1509. * or else we are racing against a death signal.
  1510. * Just punt the stop so we can get the next signal.
  1511. */
  1512. return 0;
  1513. /*
  1514. * There is a group stop in progress. We stop
  1515. * without any associated signal being in our queue.
  1516. */
  1517. stop_count = --current->signal->group_stop_count;
  1518. if (stop_count == 0)
  1519. current->signal->flags = SIGNAL_STOP_STOPPED;
  1520. current->exit_code = current->signal->group_exit_code;
  1521. set_current_state(TASK_STOPPED);
  1522. spin_unlock_irq(&current->sighand->siglock);
  1523. finish_stop(stop_count);
  1524. return 1;
  1525. }
  1526. int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
  1527. struct pt_regs *regs, void *cookie)
  1528. {
  1529. sigset_t *mask = &current->blocked;
  1530. int signr = 0;
  1531. try_to_freeze();
  1532. relock:
  1533. spin_lock_irq(&current->sighand->siglock);
  1534. for (;;) {
  1535. struct k_sigaction *ka;
  1536. if (unlikely(current->signal->group_stop_count > 0) &&
  1537. handle_group_stop())
  1538. goto relock;
  1539. signr = dequeue_signal(current, mask, info);
  1540. if (!signr)
  1541. break; /* will return 0 */
  1542. if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
  1543. ptrace_signal_deliver(regs, cookie);
  1544. /* Let the debugger run. */
  1545. ptrace_stop(signr, signr, info);
  1546. /* We're back. Did the debugger cancel the sig? */
  1547. signr = current->exit_code;
  1548. if (signr == 0)
  1549. continue;
  1550. current->exit_code = 0;
  1551. /* Update the siginfo structure if the signal has
  1552. changed. If the debugger wanted something
  1553. specific in the siginfo structure then it should
  1554. have updated *info via PTRACE_SETSIGINFO. */
  1555. if (signr != info->si_signo) {
  1556. info->si_signo = signr;
  1557. info->si_errno = 0;
  1558. info->si_code = SI_USER;
  1559. info->si_pid = current->parent->pid;
  1560. info->si_uid = current->parent->uid;
  1561. }
  1562. /* If the (new) signal is now blocked, requeue it. */
  1563. if (sigismember(&current->blocked, signr)) {
  1564. specific_send_sig_info(signr, info, current);
  1565. continue;
  1566. }
  1567. }
  1568. ka = &current->sighand->action[signr-1];
  1569. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1570. continue;
  1571. if (ka->sa.sa_handler != SIG_DFL) {
  1572. /* Run the handler. */
  1573. *return_ka = *ka;
  1574. if (ka->sa.sa_flags & SA_ONESHOT)
  1575. ka->sa.sa_handler = SIG_DFL;
  1576. break; /* will return non-zero "signr" value */
  1577. }
  1578. /*
  1579. * Now we are doing the default action for this signal.
  1580. */
  1581. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  1582. continue;
  1583. /* Init gets no signals it doesn't want. */
  1584. if (current == child_reaper)
  1585. continue;
  1586. if (sig_kernel_stop(signr)) {
  1587. /*
  1588. * The default action is to stop all threads in
  1589. * the thread group. The job control signals
  1590. * do nothing in an orphaned pgrp, but SIGSTOP
  1591. * always works. Note that siglock needs to be
  1592. * dropped during the call to is_orphaned_pgrp()
  1593. * because of lock ordering with tasklist_lock.
  1594. * This allows an intervening SIGCONT to be posted.
  1595. * We need to check for that and bail out if necessary.
  1596. */
  1597. if (signr != SIGSTOP) {
  1598. spin_unlock_irq(&current->sighand->siglock);
  1599. /* signals can be posted during this window */
  1600. if (is_orphaned_pgrp(process_group(current)))
  1601. goto relock;
  1602. spin_lock_irq(&current->sighand->siglock);
  1603. }
  1604. if (likely(do_signal_stop(signr))) {
  1605. /* It released the siglock. */
  1606. goto relock;
  1607. }
  1608. /*
  1609. * We didn't actually stop, due to a race
  1610. * with SIGCONT or something like that.
  1611. */
  1612. continue;
  1613. }
  1614. spin_unlock_irq(&current->sighand->siglock);
  1615. /*
  1616. * Anything else is fatal, maybe with a core dump.
  1617. */
  1618. current->flags |= PF_SIGNALED;
  1619. if (sig_kernel_coredump(signr)) {
  1620. /*
  1621. * If it was able to dump core, this kills all
  1622. * other threads in the group and synchronizes with
  1623. * their demise. If we lost the race with another
  1624. * thread getting here, it set group_exit_code
  1625. * first and our do_group_exit call below will use
  1626. * that value and ignore the one we pass it.
  1627. */
  1628. do_coredump((long)signr, signr, regs);
  1629. }
  1630. /*
  1631. * Death signals, no core dump.
  1632. */
  1633. do_group_exit(signr);
  1634. /* NOTREACHED */
  1635. }
  1636. spin_unlock_irq(&current->sighand->siglock);
  1637. return signr;
  1638. }
  1639. EXPORT_SYMBOL(recalc_sigpending);
  1640. EXPORT_SYMBOL_GPL(dequeue_signal);
  1641. EXPORT_SYMBOL(flush_signals);
  1642. EXPORT_SYMBOL(force_sig);
  1643. EXPORT_SYMBOL(kill_pg);
  1644. EXPORT_SYMBOL(kill_proc);
  1645. EXPORT_SYMBOL(ptrace_notify);
  1646. EXPORT_SYMBOL(send_sig);
  1647. EXPORT_SYMBOL(send_sig_info);
  1648. EXPORT_SYMBOL(sigprocmask);
  1649. EXPORT_SYMBOL(block_all_signals);
  1650. EXPORT_SYMBOL(unblock_all_signals);
  1651. /*
  1652. * System call entry points.
  1653. */
  1654. asmlinkage long sys_restart_syscall(void)
  1655. {
  1656. struct restart_block *restart = &current_thread_info()->restart_block;
  1657. return restart->fn(restart);
  1658. }
  1659. long do_no_restart_syscall(struct restart_block *param)
  1660. {
  1661. return -EINTR;
  1662. }
  1663. /*
  1664. * We don't need to get the kernel lock - this is all local to this
  1665. * particular thread.. (and that's good, because this is _heavily_
  1666. * used by various programs)
  1667. */
  1668. /*
  1669. * This is also useful for kernel threads that want to temporarily
  1670. * (or permanently) block certain signals.
  1671. *
  1672. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  1673. * interface happily blocks "unblockable" signals like SIGKILL
  1674. * and friends.
  1675. */
  1676. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  1677. {
  1678. int error;
  1679. spin_lock_irq(&current->sighand->siglock);
  1680. if (oldset)
  1681. *oldset = current->blocked;
  1682. error = 0;
  1683. switch (how) {
  1684. case SIG_BLOCK:
  1685. sigorsets(&current->blocked, &current->blocked, set);
  1686. break;
  1687. case SIG_UNBLOCK:
  1688. signandsets(&current->blocked, &current->blocked, set);
  1689. break;
  1690. case SIG_SETMASK:
  1691. current->blocked = *set;
  1692. break;
  1693. default:
  1694. error = -EINVAL;
  1695. }
  1696. recalc_sigpending();
  1697. spin_unlock_irq(&current->sighand->siglock);
  1698. return error;
  1699. }
  1700. asmlinkage long
  1701. sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
  1702. {
  1703. int error = -EINVAL;
  1704. sigset_t old_set, new_set;
  1705. /* XXX: Don't preclude handling different sized sigset_t's. */
  1706. if (sigsetsize != sizeof(sigset_t))
  1707. goto out;
  1708. if (set) {
  1709. error = -EFAULT;
  1710. if (copy_from_user(&new_set, set, sizeof(*set)))
  1711. goto out;
  1712. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1713. error = sigprocmask(how, &new_set, &old_set);
  1714. if (error)
  1715. goto out;
  1716. if (oset)
  1717. goto set_old;
  1718. } else if (oset) {
  1719. spin_lock_irq(&current->sighand->siglock);
  1720. old_set = current->blocked;
  1721. spin_unlock_irq(&current->sighand->siglock);
  1722. set_old:
  1723. error = -EFAULT;
  1724. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  1725. goto out;
  1726. }
  1727. error = 0;
  1728. out:
  1729. return error;
  1730. }
  1731. long do_sigpending(void __user *set, unsigned long sigsetsize)
  1732. {
  1733. long error = -EINVAL;
  1734. sigset_t pending;
  1735. if (sigsetsize > sizeof(sigset_t))
  1736. goto out;
  1737. spin_lock_irq(&current->sighand->siglock);
  1738. sigorsets(&pending, &current->pending.signal,
  1739. &current->signal->shared_pending.signal);
  1740. spin_unlock_irq(&current->sighand->siglock);
  1741. /* Outside the lock because only this thread touches it. */
  1742. sigandsets(&pending, &current->blocked, &pending);
  1743. error = -EFAULT;
  1744. if (!copy_to_user(set, &pending, sigsetsize))
  1745. error = 0;
  1746. out:
  1747. return error;
  1748. }
  1749. asmlinkage long
  1750. sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
  1751. {
  1752. return do_sigpending(set, sigsetsize);
  1753. }
  1754. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  1755. int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
  1756. {
  1757. int err;
  1758. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  1759. return -EFAULT;
  1760. if (from->si_code < 0)
  1761. return __copy_to_user(to, from, sizeof(siginfo_t))
  1762. ? -EFAULT : 0;
  1763. /*
  1764. * If you change siginfo_t structure, please be sure
  1765. * this code is fixed accordingly.
  1766. * It should never copy any pad contained in the structure
  1767. * to avoid security leaks, but must copy the generic
  1768. * 3 ints plus the relevant union member.
  1769. */
  1770. err = __put_user(from->si_signo, &to->si_signo);
  1771. err |= __put_user(from->si_errno, &to->si_errno);
  1772. err |= __put_user((short)from->si_code, &to->si_code);
  1773. switch (from->si_code & __SI_MASK) {
  1774. case __SI_KILL:
  1775. err |= __put_user(from->si_pid, &to->si_pid);
  1776. err |= __put_user(from->si_uid, &to->si_uid);
  1777. break;
  1778. case __SI_TIMER:
  1779. err |= __put_user(from->si_tid, &to->si_tid);
  1780. err |= __put_user(from->si_overrun, &to->si_overrun);
  1781. err |= __put_user(from->si_ptr, &to->si_ptr);
  1782. break;
  1783. case __SI_POLL:
  1784. err |= __put_user(from->si_band, &to->si_band);
  1785. err |= __put_user(from->si_fd, &to->si_fd);
  1786. break;
  1787. case __SI_FAULT:
  1788. err |= __put_user(from->si_addr, &to->si_addr);
  1789. #ifdef __ARCH_SI_TRAPNO
  1790. err |= __put_user(from->si_trapno, &to->si_trapno);
  1791. #endif
  1792. break;
  1793. case __SI_CHLD:
  1794. err |= __put_user(from->si_pid, &to->si_pid);
  1795. err |= __put_user(from->si_uid, &to->si_uid);
  1796. err |= __put_user(from->si_status, &to->si_status);
  1797. err |= __put_user(from->si_utime, &to->si_utime);
  1798. err |= __put_user(from->si_stime, &to->si_stime);
  1799. break;
  1800. case __SI_RT: /* This is not generated by the kernel as of now. */
  1801. case __SI_MESGQ: /* But this is */
  1802. err |= __put_user(from->si_pid, &to->si_pid);
  1803. err |= __put_user(from->si_uid, &to->si_uid);
  1804. err |= __put_user(from->si_ptr, &to->si_ptr);
  1805. break;
  1806. default: /* this is just in case for now ... */
  1807. err |= __put_user(from->si_pid, &to->si_pid);
  1808. err |= __put_user(from->si_uid, &to->si_uid);
  1809. break;
  1810. }
  1811. return err;
  1812. }
  1813. #endif
  1814. asmlinkage long
  1815. sys_rt_sigtimedwait(const sigset_t __user *uthese,
  1816. siginfo_t __user *uinfo,
  1817. const struct timespec __user *uts,
  1818. size_t sigsetsize)
  1819. {
  1820. int ret, sig;
  1821. sigset_t these;
  1822. struct timespec ts;
  1823. siginfo_t info;
  1824. long timeout = 0;
  1825. /* XXX: Don't preclude handling different sized sigset_t's. */
  1826. if (sigsetsize != sizeof(sigset_t))
  1827. return -EINVAL;
  1828. if (copy_from_user(&these, uthese, sizeof(these)))
  1829. return -EFAULT;
  1830. /*
  1831. * Invert the set of allowed signals to get those we
  1832. * want to block.
  1833. */
  1834. sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1835. signotset(&these);
  1836. if (uts) {
  1837. if (copy_from_user(&ts, uts, sizeof(ts)))
  1838. return -EFAULT;
  1839. if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
  1840. || ts.tv_sec < 0)
  1841. return -EINVAL;
  1842. }
  1843. spin_lock_irq(&current->sighand->siglock);
  1844. sig = dequeue_signal(current, &these, &info);
  1845. if (!sig) {
  1846. timeout = MAX_SCHEDULE_TIMEOUT;
  1847. if (uts)
  1848. timeout = (timespec_to_jiffies(&ts)
  1849. + (ts.tv_sec || ts.tv_nsec));
  1850. if (timeout) {
  1851. /* None ready -- temporarily unblock those we're
  1852. * interested while we are sleeping in so that we'll
  1853. * be awakened when they arrive. */
  1854. current->real_blocked = current->blocked;
  1855. sigandsets(&current->blocked, &current->blocked, &these);
  1856. recalc_sigpending();
  1857. spin_unlock_irq(&current->sighand->siglock);
  1858. timeout = schedule_timeout_interruptible(timeout);
  1859. spin_lock_irq(&current->sighand->siglock);
  1860. sig = dequeue_signal(current, &these, &info);
  1861. current->blocked = current->real_blocked;
  1862. siginitset(&current->real_blocked, 0);
  1863. recalc_sigpending();
  1864. }
  1865. }
  1866. spin_unlock_irq(&current->sighand->siglock);
  1867. if (sig) {
  1868. ret = sig;
  1869. if (uinfo) {
  1870. if (copy_siginfo_to_user(uinfo, &info))
  1871. ret = -EFAULT;
  1872. }
  1873. } else {
  1874. ret = -EAGAIN;
  1875. if (timeout)
  1876. ret = -EINTR;
  1877. }
  1878. return ret;
  1879. }
  1880. asmlinkage long
  1881. sys_kill(int pid, int sig)
  1882. {
  1883. struct siginfo info;
  1884. info.si_signo = sig;
  1885. info.si_errno = 0;
  1886. info.si_code = SI_USER;
  1887. info.si_pid = current->tgid;
  1888. info.si_uid = current->uid;
  1889. return kill_something_info(sig, &info, pid);
  1890. }
  1891. static int do_tkill(int tgid, int pid, int sig)
  1892. {
  1893. int error;
  1894. struct siginfo info;
  1895. struct task_struct *p;
  1896. error = -ESRCH;
  1897. info.si_signo = sig;
  1898. info.si_errno = 0;
  1899. info.si_code = SI_TKILL;
  1900. info.si_pid = current->tgid;
  1901. info.si_uid = current->uid;
  1902. read_lock(&tasklist_lock);
  1903. p = find_task_by_pid(pid);
  1904. if (p && (tgid <= 0 || p->tgid == tgid)) {
  1905. error = check_kill_permission(sig, &info, p);
  1906. /*
  1907. * The null signal is a permissions and process existence
  1908. * probe. No signal is actually delivered.
  1909. */
  1910. if (!error && sig && p->sighand) {
  1911. spin_lock_irq(&p->sighand->siglock);
  1912. handle_stop_signal(sig, p);
  1913. error = specific_send_sig_info(sig, &info, p);
  1914. spin_unlock_irq(&p->sighand->siglock);
  1915. }
  1916. }
  1917. read_unlock(&tasklist_lock);
  1918. return error;
  1919. }
  1920. /**
  1921. * sys_tgkill - send signal to one specific thread
  1922. * @tgid: the thread group ID of the thread
  1923. * @pid: the PID of the thread
  1924. * @sig: signal to be sent
  1925. *
  1926. * This syscall also checks the tgid and returns -ESRCH even if the PID
  1927. * exists but it's not belonging to the target process anymore. This
  1928. * method solves the problem of threads exiting and PIDs getting reused.
  1929. */
  1930. asmlinkage long sys_tgkill(int tgid, int pid, int sig)
  1931. {
  1932. /* This is only valid for single tasks */
  1933. if (pid <= 0 || tgid <= 0)
  1934. return -EINVAL;
  1935. return do_tkill(tgid, pid, sig);
  1936. }
  1937. /*
  1938. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  1939. */
  1940. asmlinkage long
  1941. sys_tkill(int pid, int sig)
  1942. {
  1943. /* This is only valid for single tasks */
  1944. if (pid <= 0)
  1945. return -EINVAL;
  1946. return do_tkill(0, pid, sig);
  1947. }
  1948. asmlinkage long
  1949. sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
  1950. {
  1951. siginfo_t info;
  1952. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  1953. return -EFAULT;
  1954. /* Not even root can pretend to send signals from the kernel.
  1955. Nor can they impersonate a kill(), which adds source info. */
  1956. if (info.si_code >= 0)
  1957. return -EPERM;
  1958. info.si_signo = sig;
  1959. /* POSIX.1b doesn't mention process groups. */
  1960. return kill_proc_info(sig, &info, pid);
  1961. }
  1962. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  1963. {
  1964. struct k_sigaction *k;
  1965. sigset_t mask;
  1966. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  1967. return -EINVAL;
  1968. k = &current->sighand->action[sig-1];
  1969. spin_lock_irq(&current->sighand->siglock);
  1970. if (signal_pending(current)) {
  1971. /*
  1972. * If there might be a fatal signal pending on multiple
  1973. * threads, make sure we take it before changing the action.
  1974. */
  1975. spin_unlock_irq(&current->sighand->siglock);
  1976. return -ERESTARTNOINTR;
  1977. }
  1978. if (oact)
  1979. *oact = *k;
  1980. if (act) {
  1981. sigdelsetmask(&act->sa.sa_mask,
  1982. sigmask(SIGKILL) | sigmask(SIGSTOP));
  1983. *k = *act;
  1984. /*
  1985. * POSIX 3.3.1.3:
  1986. * "Setting a signal action to SIG_IGN for a signal that is
  1987. * pending shall cause the pending signal to be discarded,
  1988. * whether or not it is blocked."
  1989. *
  1990. * "Setting a signal action to SIG_DFL for a signal that is
  1991. * pending and whose default action is to ignore the signal
  1992. * (for example, SIGCHLD), shall cause the pending signal to
  1993. * be discarded, whether or not it is blocked"
  1994. */
  1995. if (act->sa.sa_handler == SIG_IGN ||
  1996. (act->sa.sa_handler == SIG_DFL && sig_kernel_ignore(sig))) {
  1997. struct task_struct *t = current;
  1998. sigemptyset(&mask);
  1999. sigaddset(&mask, sig);
  2000. rm_from_queue_full(&mask, &t->signal->shared_pending);
  2001. do {
  2002. rm_from_queue_full(&mask, &t->pending);
  2003. recalc_sigpending_tsk(t);
  2004. t = next_thread(t);
  2005. } while (t != current);
  2006. }
  2007. }
  2008. spin_unlock_irq(&current->sighand->siglock);
  2009. return 0;
  2010. }
  2011. int
  2012. do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
  2013. {
  2014. stack_t oss;
  2015. int error;
  2016. if (uoss) {
  2017. oss.ss_sp = (void __user *) current->sas_ss_sp;
  2018. oss.ss_size = current->sas_ss_size;
  2019. oss.ss_flags = sas_ss_flags(sp);
  2020. }
  2021. if (uss) {
  2022. void __user *ss_sp;
  2023. size_t ss_size;
  2024. int ss_flags;
  2025. error = -EFAULT;
  2026. if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
  2027. || __get_user(ss_sp, &uss->ss_sp)
  2028. || __get_user(ss_flags, &uss->ss_flags)
  2029. || __get_user(ss_size, &uss->ss_size))
  2030. goto out;
  2031. error = -EPERM;
  2032. if (on_sig_stack(sp))
  2033. goto out;
  2034. error = -EINVAL;
  2035. /*
  2036. *
  2037. * Note - this code used to test ss_flags incorrectly
  2038. * old code may have been written using ss_flags==0
  2039. * to mean ss_flags==SS_ONSTACK (as this was the only
  2040. * way that worked) - this fix preserves that older
  2041. * mechanism
  2042. */
  2043. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  2044. goto out;
  2045. if (ss_flags == SS_DISABLE) {
  2046. ss_size = 0;
  2047. ss_sp = NULL;
  2048. } else {
  2049. error = -ENOMEM;
  2050. if (ss_size < MINSIGSTKSZ)
  2051. goto out;
  2052. }
  2053. current->sas_ss_sp = (unsigned long) ss_sp;
  2054. current->sas_ss_size = ss_size;
  2055. }
  2056. if (uoss) {
  2057. error = -EFAULT;
  2058. if (copy_to_user(uoss, &oss, sizeof(oss)))
  2059. goto out;
  2060. }
  2061. error = 0;
  2062. out:
  2063. return error;
  2064. }
  2065. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2066. asmlinkage long
  2067. sys_sigpending(old_sigset_t __user *set)
  2068. {
  2069. return do_sigpending(set, sizeof(*set));
  2070. }
  2071. #endif
  2072. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2073. /* Some platforms have their own version with special arguments others
  2074. support only sys_rt_sigprocmask. */
  2075. asmlinkage long
  2076. sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
  2077. {
  2078. int error;
  2079. old_sigset_t old_set, new_set;
  2080. if (set) {
  2081. error = -EFAULT;
  2082. if (copy_from_user(&new_set, set, sizeof(*set)))
  2083. goto out;
  2084. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  2085. spin_lock_irq(&current->sighand->siglock);
  2086. old_set = current->blocked.sig[0];
  2087. error = 0;
  2088. switch (how) {
  2089. default:
  2090. error = -EINVAL;
  2091. break;
  2092. case SIG_BLOCK:
  2093. sigaddsetmask(&current->blocked, new_set);
  2094. break;
  2095. case SIG_UNBLOCK:
  2096. sigdelsetmask(&current->blocked, new_set);
  2097. break;
  2098. case SIG_SETMASK:
  2099. current->blocked.sig[0] = new_set;
  2100. break;
  2101. }
  2102. recalc_sigpending();
  2103. spin_unlock_irq(&current->sighand->siglock);
  2104. if (error)
  2105. goto out;
  2106. if (oset)
  2107. goto set_old;
  2108. } else if (oset) {
  2109. old_set = current->blocked.sig[0];
  2110. set_old:
  2111. error = -EFAULT;
  2112. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2113. goto out;
  2114. }
  2115. error = 0;
  2116. out:
  2117. return error;
  2118. }
  2119. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2120. #ifdef __ARCH_WANT_SYS_RT_SIGACTION
  2121. asmlinkage long
  2122. sys_rt_sigaction(int sig,
  2123. const struct sigaction __user *act,
  2124. struct sigaction __user *oact,
  2125. size_t sigsetsize)
  2126. {
  2127. struct k_sigaction new_sa, old_sa;
  2128. int ret = -EINVAL;
  2129. /* XXX: Don't preclude handling different sized sigset_t's. */
  2130. if (sigsetsize != sizeof(sigset_t))
  2131. goto out;
  2132. if (act) {
  2133. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2134. return -EFAULT;
  2135. }
  2136. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2137. if (!ret && oact) {
  2138. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2139. return -EFAULT;
  2140. }
  2141. out:
  2142. return ret;
  2143. }
  2144. #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
  2145. #ifdef __ARCH_WANT_SYS_SGETMASK
  2146. /*
  2147. * For backwards compatibility. Functionality superseded by sigprocmask.
  2148. */
  2149. asmlinkage long
  2150. sys_sgetmask(void)
  2151. {
  2152. /* SMP safe */
  2153. return current->blocked.sig[0];
  2154. }
  2155. asmlinkage long
  2156. sys_ssetmask(int newmask)
  2157. {
  2158. int old;
  2159. spin_lock_irq(&current->sighand->siglock);
  2160. old = current->blocked.sig[0];
  2161. siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
  2162. sigmask(SIGSTOP)));
  2163. recalc_sigpending();
  2164. spin_unlock_irq(&current->sighand->siglock);
  2165. return old;
  2166. }
  2167. #endif /* __ARCH_WANT_SGETMASK */
  2168. #ifdef __ARCH_WANT_SYS_SIGNAL
  2169. /*
  2170. * For backwards compatibility. Functionality superseded by sigaction.
  2171. */
  2172. asmlinkage unsigned long
  2173. sys_signal(int sig, __sighandler_t handler)
  2174. {
  2175. struct k_sigaction new_sa, old_sa;
  2176. int ret;
  2177. new_sa.sa.sa_handler = handler;
  2178. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  2179. sigemptyset(&new_sa.sa.sa_mask);
  2180. ret = do_sigaction(sig, &new_sa, &old_sa);
  2181. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  2182. }
  2183. #endif /* __ARCH_WANT_SYS_SIGNAL */
  2184. #ifdef __ARCH_WANT_SYS_PAUSE
  2185. asmlinkage long
  2186. sys_pause(void)
  2187. {
  2188. current->state = TASK_INTERRUPTIBLE;
  2189. schedule();
  2190. return -ERESTARTNOHAND;
  2191. }
  2192. #endif
  2193. #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
  2194. asmlinkage long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
  2195. {
  2196. sigset_t newset;
  2197. /* XXX: Don't preclude handling different sized sigset_t's. */
  2198. if (sigsetsize != sizeof(sigset_t))
  2199. return -EINVAL;
  2200. if (copy_from_user(&newset, unewset, sizeof(newset)))
  2201. return -EFAULT;
  2202. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2203. spin_lock_irq(&current->sighand->siglock);
  2204. current->saved_sigmask = current->blocked;
  2205. current->blocked = newset;
  2206. recalc_sigpending();
  2207. spin_unlock_irq(&current->sighand->siglock);
  2208. current->state = TASK_INTERRUPTIBLE;
  2209. schedule();
  2210. set_thread_flag(TIF_RESTORE_SIGMASK);
  2211. return -ERESTARTNOHAND;
  2212. }
  2213. #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
  2214. void __init signals_init(void)
  2215. {
  2216. sigqueue_cachep =
  2217. kmem_cache_create("sigqueue",
  2218. sizeof(struct sigqueue),
  2219. __alignof__(struct sigqueue),
  2220. SLAB_PANIC, NULL, NULL);
  2221. }