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