signal.c 65 KB

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