signal.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226
  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/ratelimit.h>
  25. #include <linux/tracehook.h>
  26. #include <linux/capability.h>
  27. #include <linux/freezer.h>
  28. #include <linux/pid_namespace.h>
  29. #include <linux/nsproxy.h>
  30. #define CREATE_TRACE_POINTS
  31. #include <trace/events/signal.h>
  32. #include <asm/param.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/unistd.h>
  35. #include <asm/siginfo.h>
  36. #include "audit.h" /* audit_signal_info() */
  37. /*
  38. * SLAB caches for signal bits.
  39. */
  40. static struct kmem_cache *sigqueue_cachep;
  41. int print_fatal_signals __read_mostly;
  42. static void __user *sig_handler(struct task_struct *t, int sig)
  43. {
  44. return t->sighand->action[sig - 1].sa.sa_handler;
  45. }
  46. static int sig_handler_ignored(void __user *handler, int sig)
  47. {
  48. /* Is it explicitly or implicitly ignored? */
  49. return handler == SIG_IGN ||
  50. (handler == SIG_DFL && sig_kernel_ignore(sig));
  51. }
  52. static int sig_task_ignored(struct task_struct *t, int sig,
  53. int from_ancestor_ns)
  54. {
  55. void __user *handler;
  56. handler = sig_handler(t, sig);
  57. if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
  58. handler == SIG_DFL && !from_ancestor_ns)
  59. return 1;
  60. return sig_handler_ignored(handler, sig);
  61. }
  62. static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
  63. {
  64. /*
  65. * Blocked signals are never ignored, since the
  66. * signal handler may change by the time it is
  67. * unblocked.
  68. */
  69. if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
  70. return 0;
  71. if (!sig_task_ignored(t, sig, from_ancestor_ns))
  72. return 0;
  73. /*
  74. * Tracers may want to know about even ignored signals.
  75. */
  76. return !tracehook_consider_ignored_signal(t, sig);
  77. }
  78. /*
  79. * Re-calculate pending state from the set of locally pending
  80. * signals, globally pending signals, and blocked signals.
  81. */
  82. static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
  83. {
  84. unsigned long ready;
  85. long i;
  86. switch (_NSIG_WORDS) {
  87. default:
  88. for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
  89. ready |= signal->sig[i] &~ blocked->sig[i];
  90. break;
  91. case 4: ready = signal->sig[3] &~ blocked->sig[3];
  92. ready |= signal->sig[2] &~ blocked->sig[2];
  93. ready |= signal->sig[1] &~ blocked->sig[1];
  94. ready |= signal->sig[0] &~ blocked->sig[0];
  95. break;
  96. case 2: ready = signal->sig[1] &~ blocked->sig[1];
  97. ready |= signal->sig[0] &~ blocked->sig[0];
  98. break;
  99. case 1: ready = signal->sig[0] &~ blocked->sig[0];
  100. }
  101. return ready != 0;
  102. }
  103. #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
  104. static int recalc_sigpending_tsk(struct task_struct *t)
  105. {
  106. if ((t->jobctl & JOBCTL_PENDING_MASK) ||
  107. PENDING(&t->pending, &t->blocked) ||
  108. PENDING(&t->signal->shared_pending, &t->blocked)) {
  109. set_tsk_thread_flag(t, TIF_SIGPENDING);
  110. return 1;
  111. }
  112. /*
  113. * We must never clear the flag in another thread, or in current
  114. * when it's possible the current syscall is returning -ERESTART*.
  115. * So we don't clear it here, and only callers who know they should do.
  116. */
  117. return 0;
  118. }
  119. /*
  120. * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
  121. * This is superfluous when called on current, the wakeup is a harmless no-op.
  122. */
  123. void recalc_sigpending_and_wake(struct task_struct *t)
  124. {
  125. if (recalc_sigpending_tsk(t))
  126. signal_wake_up(t, 0);
  127. }
  128. void recalc_sigpending(void)
  129. {
  130. if (!recalc_sigpending_tsk(current) && !freezing(current))
  131. clear_thread_flag(TIF_SIGPENDING);
  132. }
  133. /* Given the mask, find the first available signal that should be serviced. */
  134. #define SYNCHRONOUS_MASK \
  135. (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
  136. sigmask(SIGTRAP) | sigmask(SIGFPE))
  137. int next_signal(struct sigpending *pending, sigset_t *mask)
  138. {
  139. unsigned long i, *s, *m, x;
  140. int sig = 0;
  141. s = pending->signal.sig;
  142. m = mask->sig;
  143. /*
  144. * Handle the first word specially: it contains the
  145. * synchronous signals that need to be dequeued first.
  146. */
  147. x = *s &~ *m;
  148. if (x) {
  149. if (x & SYNCHRONOUS_MASK)
  150. x &= SYNCHRONOUS_MASK;
  151. sig = ffz(~x) + 1;
  152. return sig;
  153. }
  154. switch (_NSIG_WORDS) {
  155. default:
  156. for (i = 1; i < _NSIG_WORDS; ++i) {
  157. x = *++s &~ *++m;
  158. if (!x)
  159. continue;
  160. sig = ffz(~x) + i*_NSIG_BPW + 1;
  161. break;
  162. }
  163. break;
  164. case 2:
  165. x = s[1] &~ m[1];
  166. if (!x)
  167. break;
  168. sig = ffz(~x) + _NSIG_BPW + 1;
  169. break;
  170. case 1:
  171. /* Nothing to do */
  172. break;
  173. }
  174. return sig;
  175. }
  176. static inline void print_dropped_signal(int sig)
  177. {
  178. static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
  179. if (!print_fatal_signals)
  180. return;
  181. if (!__ratelimit(&ratelimit_state))
  182. return;
  183. printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
  184. current->comm, current->pid, sig);
  185. }
  186. /**
  187. * task_set_jobctl_pending - set jobctl pending bits
  188. * @task: target task
  189. * @mask: pending bits to set
  190. *
  191. * Clear @mask from @task->jobctl. @mask must be subset of
  192. * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
  193. * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
  194. * cleared. If @task is already being killed or exiting, this function
  195. * becomes noop.
  196. *
  197. * CONTEXT:
  198. * Must be called with @task->sighand->siglock held.
  199. *
  200. * RETURNS:
  201. * %true if @mask is set, %false if made noop because @task was dying.
  202. */
  203. bool task_set_jobctl_pending(struct task_struct *task, unsigned int mask)
  204. {
  205. BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
  206. JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
  207. BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
  208. if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
  209. return false;
  210. if (mask & JOBCTL_STOP_SIGMASK)
  211. task->jobctl &= ~JOBCTL_STOP_SIGMASK;
  212. task->jobctl |= mask;
  213. return true;
  214. }
  215. /**
  216. * task_clear_jobctl_trapping - clear jobctl trapping bit
  217. * @task: target task
  218. *
  219. * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
  220. * Clear it and wake up the ptracer. Note that we don't need any further
  221. * locking. @task->siglock guarantees that @task->parent points to the
  222. * ptracer.
  223. *
  224. * CONTEXT:
  225. * Must be called with @task->sighand->siglock held.
  226. */
  227. void task_clear_jobctl_trapping(struct task_struct *task)
  228. {
  229. if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
  230. task->jobctl &= ~JOBCTL_TRAPPING;
  231. wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
  232. }
  233. }
  234. /**
  235. * task_clear_jobctl_pending - clear jobctl pending bits
  236. * @task: target task
  237. * @mask: pending bits to clear
  238. *
  239. * Clear @mask from @task->jobctl. @mask must be subset of
  240. * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
  241. * STOP bits are cleared together.
  242. *
  243. * If clearing of @mask leaves no stop or trap pending, this function calls
  244. * task_clear_jobctl_trapping().
  245. *
  246. * CONTEXT:
  247. * Must be called with @task->sighand->siglock held.
  248. */
  249. void task_clear_jobctl_pending(struct task_struct *task, unsigned int mask)
  250. {
  251. BUG_ON(mask & ~JOBCTL_PENDING_MASK);
  252. if (mask & JOBCTL_STOP_PENDING)
  253. mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
  254. task->jobctl &= ~mask;
  255. if (!(task->jobctl & JOBCTL_PENDING_MASK))
  256. task_clear_jobctl_trapping(task);
  257. }
  258. /**
  259. * task_participate_group_stop - participate in a group stop
  260. * @task: task participating in a group stop
  261. *
  262. * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
  263. * Group stop states are cleared and the group stop count is consumed if
  264. * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
  265. * stop, the appropriate %SIGNAL_* flags are set.
  266. *
  267. * CONTEXT:
  268. * Must be called with @task->sighand->siglock held.
  269. *
  270. * RETURNS:
  271. * %true if group stop completion should be notified to the parent, %false
  272. * otherwise.
  273. */
  274. static bool task_participate_group_stop(struct task_struct *task)
  275. {
  276. struct signal_struct *sig = task->signal;
  277. bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
  278. WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
  279. task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
  280. if (!consume)
  281. return false;
  282. if (!WARN_ON_ONCE(sig->group_stop_count == 0))
  283. sig->group_stop_count--;
  284. /*
  285. * Tell the caller to notify completion iff we are entering into a
  286. * fresh group stop. Read comment in do_signal_stop() for details.
  287. */
  288. if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
  289. sig->flags = SIGNAL_STOP_STOPPED;
  290. return true;
  291. }
  292. return false;
  293. }
  294. /*
  295. * allocate a new signal queue record
  296. * - this may be called without locks if and only if t == current, otherwise an
  297. * appropriate lock must be held to stop the target task from exiting
  298. */
  299. static struct sigqueue *
  300. __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
  301. {
  302. struct sigqueue *q = NULL;
  303. struct user_struct *user;
  304. /*
  305. * Protect access to @t credentials. This can go away when all
  306. * callers hold rcu read lock.
  307. */
  308. rcu_read_lock();
  309. user = get_uid(__task_cred(t)->user);
  310. atomic_inc(&user->sigpending);
  311. rcu_read_unlock();
  312. if (override_rlimit ||
  313. atomic_read(&user->sigpending) <=
  314. task_rlimit(t, RLIMIT_SIGPENDING)) {
  315. q = kmem_cache_alloc(sigqueue_cachep, flags);
  316. } else {
  317. print_dropped_signal(sig);
  318. }
  319. if (unlikely(q == NULL)) {
  320. atomic_dec(&user->sigpending);
  321. free_uid(user);
  322. } else {
  323. INIT_LIST_HEAD(&q->list);
  324. q->flags = 0;
  325. q->user = user;
  326. }
  327. return q;
  328. }
  329. static void __sigqueue_free(struct sigqueue *q)
  330. {
  331. if (q->flags & SIGQUEUE_PREALLOC)
  332. return;
  333. atomic_dec(&q->user->sigpending);
  334. free_uid(q->user);
  335. kmem_cache_free(sigqueue_cachep, q);
  336. }
  337. void flush_sigqueue(struct sigpending *queue)
  338. {
  339. struct sigqueue *q;
  340. sigemptyset(&queue->signal);
  341. while (!list_empty(&queue->list)) {
  342. q = list_entry(queue->list.next, struct sigqueue , list);
  343. list_del_init(&q->list);
  344. __sigqueue_free(q);
  345. }
  346. }
  347. /*
  348. * Flush all pending signals for a task.
  349. */
  350. void __flush_signals(struct task_struct *t)
  351. {
  352. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  353. flush_sigqueue(&t->pending);
  354. flush_sigqueue(&t->signal->shared_pending);
  355. }
  356. void flush_signals(struct task_struct *t)
  357. {
  358. unsigned long flags;
  359. spin_lock_irqsave(&t->sighand->siglock, flags);
  360. __flush_signals(t);
  361. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  362. }
  363. static void __flush_itimer_signals(struct sigpending *pending)
  364. {
  365. sigset_t signal, retain;
  366. struct sigqueue *q, *n;
  367. signal = pending->signal;
  368. sigemptyset(&retain);
  369. list_for_each_entry_safe(q, n, &pending->list, list) {
  370. int sig = q->info.si_signo;
  371. if (likely(q->info.si_code != SI_TIMER)) {
  372. sigaddset(&retain, sig);
  373. } else {
  374. sigdelset(&signal, sig);
  375. list_del_init(&q->list);
  376. __sigqueue_free(q);
  377. }
  378. }
  379. sigorsets(&pending->signal, &signal, &retain);
  380. }
  381. void flush_itimer_signals(void)
  382. {
  383. struct task_struct *tsk = current;
  384. unsigned long flags;
  385. spin_lock_irqsave(&tsk->sighand->siglock, flags);
  386. __flush_itimer_signals(&tsk->pending);
  387. __flush_itimer_signals(&tsk->signal->shared_pending);
  388. spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
  389. }
  390. void ignore_signals(struct task_struct *t)
  391. {
  392. int i;
  393. for (i = 0; i < _NSIG; ++i)
  394. t->sighand->action[i].sa.sa_handler = SIG_IGN;
  395. flush_signals(t);
  396. }
  397. /*
  398. * Flush all handlers for a task.
  399. */
  400. void
  401. flush_signal_handlers(struct task_struct *t, int force_default)
  402. {
  403. int i;
  404. struct k_sigaction *ka = &t->sighand->action[0];
  405. for (i = _NSIG ; i != 0 ; i--) {
  406. if (force_default || ka->sa.sa_handler != SIG_IGN)
  407. ka->sa.sa_handler = SIG_DFL;
  408. ka->sa.sa_flags = 0;
  409. sigemptyset(&ka->sa.sa_mask);
  410. ka++;
  411. }
  412. }
  413. int unhandled_signal(struct task_struct *tsk, int sig)
  414. {
  415. void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
  416. if (is_global_init(tsk))
  417. return 1;
  418. if (handler != SIG_IGN && handler != SIG_DFL)
  419. return 0;
  420. return !tracehook_consider_fatal_signal(tsk, sig);
  421. }
  422. /*
  423. * Notify the system that a driver wants to block all signals for this
  424. * process, and wants to be notified if any signals at all were to be
  425. * sent/acted upon. If the notifier routine returns non-zero, then the
  426. * signal will be acted upon after all. If the notifier routine returns 0,
  427. * then then signal will be blocked. Only one block per process is
  428. * allowed. priv is a pointer to private data that the notifier routine
  429. * can use to determine if the signal should be blocked or not.
  430. */
  431. void
  432. block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
  433. {
  434. unsigned long flags;
  435. spin_lock_irqsave(&current->sighand->siglock, flags);
  436. current->notifier_mask = mask;
  437. current->notifier_data = priv;
  438. current->notifier = notifier;
  439. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  440. }
  441. /* Notify the system that blocking has ended. */
  442. void
  443. unblock_all_signals(void)
  444. {
  445. unsigned long flags;
  446. spin_lock_irqsave(&current->sighand->siglock, flags);
  447. current->notifier = NULL;
  448. current->notifier_data = NULL;
  449. recalc_sigpending();
  450. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  451. }
  452. static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  453. {
  454. struct sigqueue *q, *first = NULL;
  455. /*
  456. * Collect the siginfo appropriate to this signal. Check if
  457. * there is another siginfo for the same signal.
  458. */
  459. list_for_each_entry(q, &list->list, list) {
  460. if (q->info.si_signo == sig) {
  461. if (first)
  462. goto still_pending;
  463. first = q;
  464. }
  465. }
  466. sigdelset(&list->signal, sig);
  467. if (first) {
  468. still_pending:
  469. list_del_init(&first->list);
  470. copy_siginfo(info, &first->info);
  471. __sigqueue_free(first);
  472. } else {
  473. /*
  474. * Ok, it wasn't in the queue. This must be
  475. * a fast-pathed signal or we must have been
  476. * out of queue space. So zero out the info.
  477. */
  478. info->si_signo = sig;
  479. info->si_errno = 0;
  480. info->si_code = SI_USER;
  481. info->si_pid = 0;
  482. info->si_uid = 0;
  483. }
  484. }
  485. static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
  486. siginfo_t *info)
  487. {
  488. int sig = next_signal(pending, mask);
  489. if (sig) {
  490. if (current->notifier) {
  491. if (sigismember(current->notifier_mask, sig)) {
  492. if (!(current->notifier)(current->notifier_data)) {
  493. clear_thread_flag(TIF_SIGPENDING);
  494. return 0;
  495. }
  496. }
  497. }
  498. collect_signal(sig, pending, info);
  499. }
  500. return sig;
  501. }
  502. /*
  503. * Dequeue a signal and return the element to the caller, which is
  504. * expected to free it.
  505. *
  506. * All callers have to hold the siglock.
  507. */
  508. int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  509. {
  510. int signr;
  511. /* We only dequeue private signals from ourselves, we don't let
  512. * signalfd steal them
  513. */
  514. signr = __dequeue_signal(&tsk->pending, mask, info);
  515. if (!signr) {
  516. signr = __dequeue_signal(&tsk->signal->shared_pending,
  517. mask, info);
  518. /*
  519. * itimer signal ?
  520. *
  521. * itimers are process shared and we restart periodic
  522. * itimers in the signal delivery path to prevent DoS
  523. * attacks in the high resolution timer case. This is
  524. * compliant with the old way of self-restarting
  525. * itimers, as the SIGALRM is a legacy signal and only
  526. * queued once. Changing the restart behaviour to
  527. * restart the timer in the signal dequeue path is
  528. * reducing the timer noise on heavy loaded !highres
  529. * systems too.
  530. */
  531. if (unlikely(signr == SIGALRM)) {
  532. struct hrtimer *tmr = &tsk->signal->real_timer;
  533. if (!hrtimer_is_queued(tmr) &&
  534. tsk->signal->it_real_incr.tv64 != 0) {
  535. hrtimer_forward(tmr, tmr->base->get_time(),
  536. tsk->signal->it_real_incr);
  537. hrtimer_restart(tmr);
  538. }
  539. }
  540. }
  541. recalc_sigpending();
  542. if (!signr)
  543. return 0;
  544. if (unlikely(sig_kernel_stop(signr))) {
  545. /*
  546. * Set a marker that we have dequeued a stop signal. Our
  547. * caller might release the siglock and then the pending
  548. * stop signal it is about to process is no longer in the
  549. * pending bitmasks, but must still be cleared by a SIGCONT
  550. * (and overruled by a SIGKILL). So those cases clear this
  551. * shared flag after we've set it. Note that this flag may
  552. * remain set after the signal we return is ignored or
  553. * handled. That doesn't matter because its only purpose
  554. * is to alert stop-signal processing code when another
  555. * processor has come along and cleared the flag.
  556. */
  557. current->jobctl |= JOBCTL_STOP_DEQUEUED;
  558. }
  559. if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
  560. /*
  561. * Release the siglock to ensure proper locking order
  562. * of timer locks outside of siglocks. Note, we leave
  563. * irqs disabled here, since the posix-timers code is
  564. * about to disable them again anyway.
  565. */
  566. spin_unlock(&tsk->sighand->siglock);
  567. do_schedule_next_timer(info);
  568. spin_lock(&tsk->sighand->siglock);
  569. }
  570. return signr;
  571. }
  572. /*
  573. * Tell a process that it has a new active signal..
  574. *
  575. * NOTE! we rely on the previous spin_lock to
  576. * lock interrupts for us! We can only be called with
  577. * "siglock" held, and the local interrupt must
  578. * have been disabled when that got acquired!
  579. *
  580. * No need to set need_resched since signal event passing
  581. * goes through ->blocked
  582. */
  583. void signal_wake_up(struct task_struct *t, int resume)
  584. {
  585. unsigned int mask;
  586. set_tsk_thread_flag(t, TIF_SIGPENDING);
  587. /*
  588. * For SIGKILL, we want to wake it up in the stopped/traced/killable
  589. * case. We don't check t->state here because there is a race with it
  590. * executing another processor and just now entering stopped state.
  591. * By using wake_up_state, we ensure the process will wake up and
  592. * handle its death signal.
  593. */
  594. mask = TASK_INTERRUPTIBLE;
  595. if (resume)
  596. mask |= TASK_WAKEKILL;
  597. if (!wake_up_state(t, mask))
  598. kick_process(t);
  599. }
  600. /*
  601. * Remove signals in mask from the pending set and queue.
  602. * Returns 1 if any signals were found.
  603. *
  604. * All callers must be holding the siglock.
  605. *
  606. * This version takes a sigset mask and looks at all signals,
  607. * not just those in the first mask word.
  608. */
  609. static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
  610. {
  611. struct sigqueue *q, *n;
  612. sigset_t m;
  613. sigandsets(&m, mask, &s->signal);
  614. if (sigisemptyset(&m))
  615. return 0;
  616. sigandnsets(&s->signal, &s->signal, mask);
  617. list_for_each_entry_safe(q, n, &s->list, list) {
  618. if (sigismember(mask, q->info.si_signo)) {
  619. list_del_init(&q->list);
  620. __sigqueue_free(q);
  621. }
  622. }
  623. return 1;
  624. }
  625. /*
  626. * Remove signals in mask from the pending set and queue.
  627. * Returns 1 if any signals were found.
  628. *
  629. * All callers must be holding the siglock.
  630. */
  631. static int rm_from_queue(unsigned long mask, struct sigpending *s)
  632. {
  633. struct sigqueue *q, *n;
  634. if (!sigtestsetmask(&s->signal, mask))
  635. return 0;
  636. sigdelsetmask(&s->signal, mask);
  637. list_for_each_entry_safe(q, n, &s->list, list) {
  638. if (q->info.si_signo < SIGRTMIN &&
  639. (mask & sigmask(q->info.si_signo))) {
  640. list_del_init(&q->list);
  641. __sigqueue_free(q);
  642. }
  643. }
  644. return 1;
  645. }
  646. static inline int is_si_special(const struct siginfo *info)
  647. {
  648. return info <= SEND_SIG_FORCED;
  649. }
  650. static inline bool si_fromuser(const struct siginfo *info)
  651. {
  652. return info == SEND_SIG_NOINFO ||
  653. (!is_si_special(info) && SI_FROMUSER(info));
  654. }
  655. /*
  656. * called with RCU read lock from check_kill_permission()
  657. */
  658. static int kill_ok_by_cred(struct task_struct *t)
  659. {
  660. const struct cred *cred = current_cred();
  661. const struct cred *tcred = __task_cred(t);
  662. if (cred->user->user_ns == tcred->user->user_ns &&
  663. (cred->euid == tcred->suid ||
  664. cred->euid == tcred->uid ||
  665. cred->uid == tcred->suid ||
  666. cred->uid == tcred->uid))
  667. return 1;
  668. if (ns_capable(tcred->user->user_ns, CAP_KILL))
  669. return 1;
  670. return 0;
  671. }
  672. /*
  673. * Bad permissions for sending the signal
  674. * - the caller must hold the RCU read lock
  675. */
  676. static int check_kill_permission(int sig, struct siginfo *info,
  677. struct task_struct *t)
  678. {
  679. struct pid *sid;
  680. int error;
  681. if (!valid_signal(sig))
  682. return -EINVAL;
  683. if (!si_fromuser(info))
  684. return 0;
  685. error = audit_signal_info(sig, t); /* Let audit system see the signal */
  686. if (error)
  687. return error;
  688. if (!same_thread_group(current, t) &&
  689. !kill_ok_by_cred(t)) {
  690. switch (sig) {
  691. case SIGCONT:
  692. sid = task_session(t);
  693. /*
  694. * We don't return the error if sid == NULL. The
  695. * task was unhashed, the caller must notice this.
  696. */
  697. if (!sid || sid == task_session(current))
  698. break;
  699. default:
  700. return -EPERM;
  701. }
  702. }
  703. return security_task_kill(t, info, sig, 0);
  704. }
  705. /**
  706. * ptrace_trap_notify - schedule trap to notify ptracer
  707. * @t: tracee wanting to notify tracer
  708. *
  709. * This function schedules sticky ptrace trap which is cleared on the next
  710. * TRAP_STOP to notify ptracer of an event. @t must have been seized by
  711. * ptracer.
  712. *
  713. * If @t is running, STOP trap will be taken. If already trapped, STOP
  714. * trap will be eventually taken without returning to userland after the
  715. * existing traps are finished by PTRACE_CONT.
  716. *
  717. * CONTEXT:
  718. * Must be called with @task->sighand->siglock held.
  719. */
  720. static void ptrace_trap_notify(struct task_struct *t)
  721. {
  722. WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
  723. assert_spin_locked(&t->sighand->siglock);
  724. task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
  725. signal_wake_up(t, 0);
  726. }
  727. /*
  728. * Handle magic process-wide effects of stop/continue signals. Unlike
  729. * the signal actions, these happen immediately at signal-generation
  730. * time regardless of blocking, ignoring, or handling. This does the
  731. * actual continuing for SIGCONT, but not the actual stopping for stop
  732. * signals. The process stop is done as a signal action for SIG_DFL.
  733. *
  734. * Returns true if the signal should be actually delivered, otherwise
  735. * it should be dropped.
  736. */
  737. static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
  738. {
  739. struct signal_struct *signal = p->signal;
  740. struct task_struct *t;
  741. if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
  742. /*
  743. * The process is in the middle of dying, nothing to do.
  744. */
  745. } else if (sig_kernel_stop(sig)) {
  746. /*
  747. * This is a stop signal. Remove SIGCONT from all queues.
  748. */
  749. rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
  750. t = p;
  751. do {
  752. rm_from_queue(sigmask(SIGCONT), &t->pending);
  753. } while_each_thread(p, t);
  754. } else if (sig == SIGCONT) {
  755. unsigned int why;
  756. /*
  757. * Remove all stop signals from all queues, wake all threads.
  758. */
  759. rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
  760. t = p;
  761. do {
  762. task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
  763. rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
  764. if (likely(!(t->ptrace & PT_SEIZED)))
  765. wake_up_state(t, __TASK_STOPPED);
  766. else
  767. ptrace_trap_notify(t);
  768. } while_each_thread(p, t);
  769. /*
  770. * Notify the parent with CLD_CONTINUED if we were stopped.
  771. *
  772. * If we were in the middle of a group stop, we pretend it
  773. * was already finished, and then continued. Since SIGCHLD
  774. * doesn't queue we report only CLD_STOPPED, as if the next
  775. * CLD_CONTINUED was dropped.
  776. */
  777. why = 0;
  778. if (signal->flags & SIGNAL_STOP_STOPPED)
  779. why |= SIGNAL_CLD_CONTINUED;
  780. else if (signal->group_stop_count)
  781. why |= SIGNAL_CLD_STOPPED;
  782. if (why) {
  783. /*
  784. * The first thread which returns from do_signal_stop()
  785. * will take ->siglock, notice SIGNAL_CLD_MASK, and
  786. * notify its parent. See get_signal_to_deliver().
  787. */
  788. signal->flags = why | SIGNAL_STOP_CONTINUED;
  789. signal->group_stop_count = 0;
  790. signal->group_exit_code = 0;
  791. }
  792. }
  793. return !sig_ignored(p, sig, from_ancestor_ns);
  794. }
  795. /*
  796. * Test if P wants to take SIG. After we've checked all threads with this,
  797. * it's equivalent to finding no threads not blocking SIG. Any threads not
  798. * blocking SIG were ruled out because they are not running and already
  799. * have pending signals. Such threads will dequeue from the shared queue
  800. * as soon as they're available, so putting the signal on the shared queue
  801. * will be equivalent to sending it to one such thread.
  802. */
  803. static inline int wants_signal(int sig, struct task_struct *p)
  804. {
  805. if (sigismember(&p->blocked, sig))
  806. return 0;
  807. if (p->flags & PF_EXITING)
  808. return 0;
  809. if (sig == SIGKILL)
  810. return 1;
  811. if (task_is_stopped_or_traced(p))
  812. return 0;
  813. return task_curr(p) || !signal_pending(p);
  814. }
  815. static void complete_signal(int sig, struct task_struct *p, int group)
  816. {
  817. struct signal_struct *signal = p->signal;
  818. struct task_struct *t;
  819. /*
  820. * Now find a thread we can wake up to take the signal off the queue.
  821. *
  822. * If the main thread wants the signal, it gets first crack.
  823. * Probably the least surprising to the average bear.
  824. */
  825. if (wants_signal(sig, p))
  826. t = p;
  827. else if (!group || thread_group_empty(p))
  828. /*
  829. * There is just one thread and it does not need to be woken.
  830. * It will dequeue unblocked signals before it runs again.
  831. */
  832. return;
  833. else {
  834. /*
  835. * Otherwise try to find a suitable thread.
  836. */
  837. t = signal->curr_target;
  838. while (!wants_signal(sig, t)) {
  839. t = next_thread(t);
  840. if (t == signal->curr_target)
  841. /*
  842. * No thread needs to be woken.
  843. * Any eligible threads will see
  844. * the signal in the queue soon.
  845. */
  846. return;
  847. }
  848. signal->curr_target = t;
  849. }
  850. /*
  851. * Found a killable thread. If the signal will be fatal,
  852. * then start taking the whole group down immediately.
  853. */
  854. if (sig_fatal(p, sig) &&
  855. !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
  856. !sigismember(&t->real_blocked, sig) &&
  857. (sig == SIGKILL ||
  858. !tracehook_consider_fatal_signal(t, sig))) {
  859. /*
  860. * This signal will be fatal to the whole group.
  861. */
  862. if (!sig_kernel_coredump(sig)) {
  863. /*
  864. * Start a group exit and wake everybody up.
  865. * This way we don't have other threads
  866. * running and doing things after a slower
  867. * thread has the fatal signal pending.
  868. */
  869. signal->flags = SIGNAL_GROUP_EXIT;
  870. signal->group_exit_code = sig;
  871. signal->group_stop_count = 0;
  872. t = p;
  873. do {
  874. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  875. sigaddset(&t->pending.signal, SIGKILL);
  876. signal_wake_up(t, 1);
  877. } while_each_thread(p, t);
  878. return;
  879. }
  880. }
  881. /*
  882. * The signal is already in the shared-pending queue.
  883. * Tell the chosen thread to wake up and dequeue it.
  884. */
  885. signal_wake_up(t, sig == SIGKILL);
  886. return;
  887. }
  888. static inline int legacy_queue(struct sigpending *signals, int sig)
  889. {
  890. return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
  891. }
  892. static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
  893. int group, int from_ancestor_ns)
  894. {
  895. struct sigpending *pending;
  896. struct sigqueue *q;
  897. int override_rlimit;
  898. trace_signal_generate(sig, info, t);
  899. assert_spin_locked(&t->sighand->siglock);
  900. if (!prepare_signal(sig, t, from_ancestor_ns))
  901. return 0;
  902. pending = group ? &t->signal->shared_pending : &t->pending;
  903. /*
  904. * Short-circuit ignored signals and support queuing
  905. * exactly one non-rt signal, so that we can get more
  906. * detailed information about the cause of the signal.
  907. */
  908. if (legacy_queue(pending, sig))
  909. return 0;
  910. /*
  911. * fast-pathed signals for kernel-internal things like SIGSTOP
  912. * or SIGKILL.
  913. */
  914. if (info == SEND_SIG_FORCED)
  915. goto out_set;
  916. /*
  917. * Real-time signals must be queued if sent by sigqueue, or
  918. * some other real-time mechanism. It is implementation
  919. * defined whether kill() does so. We attempt to do so, on
  920. * the principle of least surprise, but since kill is not
  921. * allowed to fail with EAGAIN when low on memory we just
  922. * make sure at least one signal gets delivered and don't
  923. * pass on the info struct.
  924. */
  925. if (sig < SIGRTMIN)
  926. override_rlimit = (is_si_special(info) || info->si_code >= 0);
  927. else
  928. override_rlimit = 0;
  929. q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
  930. override_rlimit);
  931. if (q) {
  932. list_add_tail(&q->list, &pending->list);
  933. switch ((unsigned long) info) {
  934. case (unsigned long) SEND_SIG_NOINFO:
  935. q->info.si_signo = sig;
  936. q->info.si_errno = 0;
  937. q->info.si_code = SI_USER;
  938. q->info.si_pid = task_tgid_nr_ns(current,
  939. task_active_pid_ns(t));
  940. q->info.si_uid = current_uid();
  941. break;
  942. case (unsigned long) SEND_SIG_PRIV:
  943. q->info.si_signo = sig;
  944. q->info.si_errno = 0;
  945. q->info.si_code = SI_KERNEL;
  946. q->info.si_pid = 0;
  947. q->info.si_uid = 0;
  948. break;
  949. default:
  950. copy_siginfo(&q->info, info);
  951. if (from_ancestor_ns)
  952. q->info.si_pid = 0;
  953. break;
  954. }
  955. } else if (!is_si_special(info)) {
  956. if (sig >= SIGRTMIN && info->si_code != SI_USER) {
  957. /*
  958. * Queue overflow, abort. We may abort if the
  959. * signal was rt and sent by user using something
  960. * other than kill().
  961. */
  962. trace_signal_overflow_fail(sig, group, info);
  963. return -EAGAIN;
  964. } else {
  965. /*
  966. * This is a silent loss of information. We still
  967. * send the signal, but the *info bits are lost.
  968. */
  969. trace_signal_lose_info(sig, group, info);
  970. }
  971. }
  972. out_set:
  973. signalfd_notify(t, sig);
  974. sigaddset(&pending->signal, sig);
  975. complete_signal(sig, t, group);
  976. return 0;
  977. }
  978. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  979. int group)
  980. {
  981. int from_ancestor_ns = 0;
  982. #ifdef CONFIG_PID_NS
  983. from_ancestor_ns = si_fromuser(info) &&
  984. !task_pid_nr_ns(current, task_active_pid_ns(t));
  985. #endif
  986. return __send_signal(sig, info, t, group, from_ancestor_ns);
  987. }
  988. static void print_fatal_signal(struct pt_regs *regs, int signr)
  989. {
  990. printk("%s/%d: potentially unexpected fatal signal %d.\n",
  991. current->comm, task_pid_nr(current), signr);
  992. #if defined(__i386__) && !defined(__arch_um__)
  993. printk("code at %08lx: ", regs->ip);
  994. {
  995. int i;
  996. for (i = 0; i < 16; i++) {
  997. unsigned char insn;
  998. if (get_user(insn, (unsigned char *)(regs->ip + i)))
  999. break;
  1000. printk("%02x ", insn);
  1001. }
  1002. }
  1003. #endif
  1004. printk("\n");
  1005. preempt_disable();
  1006. show_regs(regs);
  1007. preempt_enable();
  1008. }
  1009. static int __init setup_print_fatal_signals(char *str)
  1010. {
  1011. get_option (&str, &print_fatal_signals);
  1012. return 1;
  1013. }
  1014. __setup("print-fatal-signals=", setup_print_fatal_signals);
  1015. int
  1016. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1017. {
  1018. return send_signal(sig, info, p, 1);
  1019. }
  1020. static int
  1021. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1022. {
  1023. return send_signal(sig, info, t, 0);
  1024. }
  1025. int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  1026. bool group)
  1027. {
  1028. unsigned long flags;
  1029. int ret = -ESRCH;
  1030. if (lock_task_sighand(p, &flags)) {
  1031. ret = send_signal(sig, info, p, group);
  1032. unlock_task_sighand(p, &flags);
  1033. }
  1034. return ret;
  1035. }
  1036. /*
  1037. * Force a signal that the process can't ignore: if necessary
  1038. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  1039. *
  1040. * Note: If we unblock the signal, we always reset it to SIG_DFL,
  1041. * since we do not want to have a signal handler that was blocked
  1042. * be invoked when user space had explicitly blocked it.
  1043. *
  1044. * We don't want to have recursive SIGSEGV's etc, for example,
  1045. * that is why we also clear SIGNAL_UNKILLABLE.
  1046. */
  1047. int
  1048. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1049. {
  1050. unsigned long int flags;
  1051. int ret, blocked, ignored;
  1052. struct k_sigaction *action;
  1053. spin_lock_irqsave(&t->sighand->siglock, flags);
  1054. action = &t->sighand->action[sig-1];
  1055. ignored = action->sa.sa_handler == SIG_IGN;
  1056. blocked = sigismember(&t->blocked, sig);
  1057. if (blocked || ignored) {
  1058. action->sa.sa_handler = SIG_DFL;
  1059. if (blocked) {
  1060. sigdelset(&t->blocked, sig);
  1061. recalc_sigpending_and_wake(t);
  1062. }
  1063. }
  1064. if (action->sa.sa_handler == SIG_DFL)
  1065. t->signal->flags &= ~SIGNAL_UNKILLABLE;
  1066. ret = specific_send_sig_info(sig, info, t);
  1067. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  1068. return ret;
  1069. }
  1070. /*
  1071. * Nuke all other threads in the group.
  1072. */
  1073. int zap_other_threads(struct task_struct *p)
  1074. {
  1075. struct task_struct *t = p;
  1076. int count = 0;
  1077. p->signal->group_stop_count = 0;
  1078. while_each_thread(p, t) {
  1079. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  1080. count++;
  1081. /* Don't bother with already dead threads */
  1082. if (t->exit_state)
  1083. continue;
  1084. sigaddset(&t->pending.signal, SIGKILL);
  1085. signal_wake_up(t, 1);
  1086. }
  1087. return count;
  1088. }
  1089. struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  1090. unsigned long *flags)
  1091. {
  1092. struct sighand_struct *sighand;
  1093. rcu_read_lock();
  1094. for (;;) {
  1095. sighand = rcu_dereference(tsk->sighand);
  1096. if (unlikely(sighand == NULL))
  1097. break;
  1098. spin_lock_irqsave(&sighand->siglock, *flags);
  1099. if (likely(sighand == tsk->sighand))
  1100. break;
  1101. spin_unlock_irqrestore(&sighand->siglock, *flags);
  1102. }
  1103. rcu_read_unlock();
  1104. return sighand;
  1105. }
  1106. /*
  1107. * send signal info to all the members of a group
  1108. */
  1109. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1110. {
  1111. int ret;
  1112. rcu_read_lock();
  1113. ret = check_kill_permission(sig, info, p);
  1114. rcu_read_unlock();
  1115. if (!ret && sig)
  1116. ret = do_send_sig_info(sig, info, p, true);
  1117. return ret;
  1118. }
  1119. /*
  1120. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  1121. * control characters do (^C, ^Z etc)
  1122. * - the caller must hold at least a readlock on tasklist_lock
  1123. */
  1124. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  1125. {
  1126. struct task_struct *p = NULL;
  1127. int retval, success;
  1128. success = 0;
  1129. retval = -ESRCH;
  1130. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  1131. int err = group_send_sig_info(sig, info, p);
  1132. success |= !err;
  1133. retval = err;
  1134. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  1135. return success ? 0 : retval;
  1136. }
  1137. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  1138. {
  1139. int error = -ESRCH;
  1140. struct task_struct *p;
  1141. rcu_read_lock();
  1142. retry:
  1143. p = pid_task(pid, PIDTYPE_PID);
  1144. if (p) {
  1145. error = group_send_sig_info(sig, info, p);
  1146. if (unlikely(error == -ESRCH))
  1147. /*
  1148. * The task was unhashed in between, try again.
  1149. * If it is dead, pid_task() will return NULL,
  1150. * if we race with de_thread() it will find the
  1151. * new leader.
  1152. */
  1153. goto retry;
  1154. }
  1155. rcu_read_unlock();
  1156. return error;
  1157. }
  1158. int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  1159. {
  1160. int error;
  1161. rcu_read_lock();
  1162. error = kill_pid_info(sig, info, find_vpid(pid));
  1163. rcu_read_unlock();
  1164. return error;
  1165. }
  1166. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  1167. int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
  1168. uid_t uid, uid_t euid, u32 secid)
  1169. {
  1170. int ret = -EINVAL;
  1171. struct task_struct *p;
  1172. const struct cred *pcred;
  1173. unsigned long flags;
  1174. if (!valid_signal(sig))
  1175. return ret;
  1176. rcu_read_lock();
  1177. p = pid_task(pid, PIDTYPE_PID);
  1178. if (!p) {
  1179. ret = -ESRCH;
  1180. goto out_unlock;
  1181. }
  1182. pcred = __task_cred(p);
  1183. if (si_fromuser(info) &&
  1184. euid != pcred->suid && euid != pcred->uid &&
  1185. uid != pcred->suid && uid != pcred->uid) {
  1186. ret = -EPERM;
  1187. goto out_unlock;
  1188. }
  1189. ret = security_task_kill(p, info, sig, secid);
  1190. if (ret)
  1191. goto out_unlock;
  1192. if (sig) {
  1193. if (lock_task_sighand(p, &flags)) {
  1194. ret = __send_signal(sig, info, p, 1, 0);
  1195. unlock_task_sighand(p, &flags);
  1196. } else
  1197. ret = -ESRCH;
  1198. }
  1199. out_unlock:
  1200. rcu_read_unlock();
  1201. return ret;
  1202. }
  1203. EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
  1204. /*
  1205. * kill_something_info() interprets pid in interesting ways just like kill(2).
  1206. *
  1207. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  1208. * is probably wrong. Should make it like BSD or SYSV.
  1209. */
  1210. static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
  1211. {
  1212. int ret;
  1213. if (pid > 0) {
  1214. rcu_read_lock();
  1215. ret = kill_pid_info(sig, info, find_vpid(pid));
  1216. rcu_read_unlock();
  1217. return ret;
  1218. }
  1219. read_lock(&tasklist_lock);
  1220. if (pid != -1) {
  1221. ret = __kill_pgrp_info(sig, info,
  1222. pid ? find_vpid(-pid) : task_pgrp(current));
  1223. } else {
  1224. int retval = 0, count = 0;
  1225. struct task_struct * p;
  1226. for_each_process(p) {
  1227. if (task_pid_vnr(p) > 1 &&
  1228. !same_thread_group(p, current)) {
  1229. int err = group_send_sig_info(sig, info, p);
  1230. ++count;
  1231. if (err != -EPERM)
  1232. retval = err;
  1233. }
  1234. }
  1235. ret = count ? retval : -ESRCH;
  1236. }
  1237. read_unlock(&tasklist_lock);
  1238. return ret;
  1239. }
  1240. /*
  1241. * These are for backward compatibility with the rest of the kernel source.
  1242. */
  1243. int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1244. {
  1245. /*
  1246. * Make sure legacy kernel users don't send in bad values
  1247. * (normal paths check this in check_kill_permission).
  1248. */
  1249. if (!valid_signal(sig))
  1250. return -EINVAL;
  1251. return do_send_sig_info(sig, info, p, false);
  1252. }
  1253. #define __si_special(priv) \
  1254. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1255. int
  1256. send_sig(int sig, struct task_struct *p, int priv)
  1257. {
  1258. return send_sig_info(sig, __si_special(priv), p);
  1259. }
  1260. void
  1261. force_sig(int sig, struct task_struct *p)
  1262. {
  1263. force_sig_info(sig, SEND_SIG_PRIV, p);
  1264. }
  1265. /*
  1266. * When things go south during signal handling, we
  1267. * will force a SIGSEGV. And if the signal that caused
  1268. * the problem was already a SIGSEGV, we'll want to
  1269. * make sure we don't even try to deliver the signal..
  1270. */
  1271. int
  1272. force_sigsegv(int sig, struct task_struct *p)
  1273. {
  1274. if (sig == SIGSEGV) {
  1275. unsigned long flags;
  1276. spin_lock_irqsave(&p->sighand->siglock, flags);
  1277. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1278. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1279. }
  1280. force_sig(SIGSEGV, p);
  1281. return 0;
  1282. }
  1283. int kill_pgrp(struct pid *pid, int sig, int priv)
  1284. {
  1285. int ret;
  1286. read_lock(&tasklist_lock);
  1287. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1288. read_unlock(&tasklist_lock);
  1289. return ret;
  1290. }
  1291. EXPORT_SYMBOL(kill_pgrp);
  1292. int kill_pid(struct pid *pid, int sig, int priv)
  1293. {
  1294. return kill_pid_info(sig, __si_special(priv), pid);
  1295. }
  1296. EXPORT_SYMBOL(kill_pid);
  1297. /*
  1298. * These functions support sending signals using preallocated sigqueue
  1299. * structures. This is needed "because realtime applications cannot
  1300. * afford to lose notifications of asynchronous events, like timer
  1301. * expirations or I/O completions". In the case of POSIX Timers
  1302. * we allocate the sigqueue structure from the timer_create. If this
  1303. * allocation fails we are able to report the failure to the application
  1304. * with an EAGAIN error.
  1305. */
  1306. struct sigqueue *sigqueue_alloc(void)
  1307. {
  1308. struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  1309. if (q)
  1310. q->flags |= SIGQUEUE_PREALLOC;
  1311. return q;
  1312. }
  1313. void sigqueue_free(struct sigqueue *q)
  1314. {
  1315. unsigned long flags;
  1316. spinlock_t *lock = &current->sighand->siglock;
  1317. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1318. /*
  1319. * We must hold ->siglock while testing q->list
  1320. * to serialize with collect_signal() or with
  1321. * __exit_signal()->flush_sigqueue().
  1322. */
  1323. spin_lock_irqsave(lock, flags);
  1324. q->flags &= ~SIGQUEUE_PREALLOC;
  1325. /*
  1326. * If it is queued it will be freed when dequeued,
  1327. * like the "regular" sigqueue.
  1328. */
  1329. if (!list_empty(&q->list))
  1330. q = NULL;
  1331. spin_unlock_irqrestore(lock, flags);
  1332. if (q)
  1333. __sigqueue_free(q);
  1334. }
  1335. int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
  1336. {
  1337. int sig = q->info.si_signo;
  1338. struct sigpending *pending;
  1339. unsigned long flags;
  1340. int ret;
  1341. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1342. ret = -1;
  1343. if (!likely(lock_task_sighand(t, &flags)))
  1344. goto ret;
  1345. ret = 1; /* the signal is ignored */
  1346. if (!prepare_signal(sig, t, 0))
  1347. goto out;
  1348. ret = 0;
  1349. if (unlikely(!list_empty(&q->list))) {
  1350. /*
  1351. * If an SI_TIMER entry is already queue just increment
  1352. * the overrun count.
  1353. */
  1354. BUG_ON(q->info.si_code != SI_TIMER);
  1355. q->info.si_overrun++;
  1356. goto out;
  1357. }
  1358. q->info.si_overrun = 0;
  1359. signalfd_notify(t, sig);
  1360. pending = group ? &t->signal->shared_pending : &t->pending;
  1361. list_add_tail(&q->list, &pending->list);
  1362. sigaddset(&pending->signal, sig);
  1363. complete_signal(sig, t, group);
  1364. out:
  1365. unlock_task_sighand(t, &flags);
  1366. ret:
  1367. return ret;
  1368. }
  1369. /*
  1370. * Let a parent know about the death of a child.
  1371. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1372. *
  1373. * Returns -1 if our parent ignored us and so we've switched to
  1374. * self-reaping, or else @sig.
  1375. */
  1376. int do_notify_parent(struct task_struct *tsk, int sig)
  1377. {
  1378. struct siginfo info;
  1379. unsigned long flags;
  1380. struct sighand_struct *psig;
  1381. int ret = sig;
  1382. BUG_ON(sig == -1);
  1383. /* do_notify_parent_cldstop should have been called instead. */
  1384. BUG_ON(task_is_stopped_or_traced(tsk));
  1385. BUG_ON(!task_ptrace(tsk) &&
  1386. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1387. info.si_signo = sig;
  1388. info.si_errno = 0;
  1389. /*
  1390. * we are under tasklist_lock here so our parent is tied to
  1391. * us and cannot exit and release its namespace.
  1392. *
  1393. * the only it can is to switch its nsproxy with sys_unshare,
  1394. * bu uncharing pid namespaces is not allowed, so we'll always
  1395. * see relevant namespace
  1396. *
  1397. * write_lock() currently calls preempt_disable() which is the
  1398. * same as rcu_read_lock(), but according to Oleg, this is not
  1399. * correct to rely on this
  1400. */
  1401. rcu_read_lock();
  1402. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1403. info.si_uid = __task_cred(tsk)->uid;
  1404. rcu_read_unlock();
  1405. info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
  1406. tsk->signal->utime));
  1407. info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
  1408. tsk->signal->stime));
  1409. info.si_status = tsk->exit_code & 0x7f;
  1410. if (tsk->exit_code & 0x80)
  1411. info.si_code = CLD_DUMPED;
  1412. else if (tsk->exit_code & 0x7f)
  1413. info.si_code = CLD_KILLED;
  1414. else {
  1415. info.si_code = CLD_EXITED;
  1416. info.si_status = tsk->exit_code >> 8;
  1417. }
  1418. psig = tsk->parent->sighand;
  1419. spin_lock_irqsave(&psig->siglock, flags);
  1420. if (!task_ptrace(tsk) && sig == SIGCHLD &&
  1421. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1422. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1423. /*
  1424. * We are exiting and our parent doesn't care. POSIX.1
  1425. * defines special semantics for setting SIGCHLD to SIG_IGN
  1426. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1427. * automatically and not left for our parent's wait4 call.
  1428. * Rather than having the parent do it as a magic kind of
  1429. * signal handler, we just set this to tell do_exit that we
  1430. * can be cleaned up without becoming a zombie. Note that
  1431. * we still call __wake_up_parent in this case, because a
  1432. * blocked sys_wait4 might now return -ECHILD.
  1433. *
  1434. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1435. * is implementation-defined: we do (if you don't want
  1436. * it, just use SIG_IGN instead).
  1437. */
  1438. ret = tsk->exit_signal = -1;
  1439. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1440. sig = -1;
  1441. }
  1442. if (valid_signal(sig) && sig > 0)
  1443. __group_send_sig_info(sig, &info, tsk->parent);
  1444. __wake_up_parent(tsk, tsk->parent);
  1445. spin_unlock_irqrestore(&psig->siglock, flags);
  1446. return ret;
  1447. }
  1448. /**
  1449. * do_notify_parent_cldstop - notify parent of stopped/continued state change
  1450. * @tsk: task reporting the state change
  1451. * @for_ptracer: the notification is for ptracer
  1452. * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
  1453. *
  1454. * Notify @tsk's parent that the stopped/continued state has changed. If
  1455. * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
  1456. * If %true, @tsk reports to @tsk->parent which should be the ptracer.
  1457. *
  1458. * CONTEXT:
  1459. * Must be called with tasklist_lock at least read locked.
  1460. */
  1461. static void do_notify_parent_cldstop(struct task_struct *tsk,
  1462. bool for_ptracer, int why)
  1463. {
  1464. struct siginfo info;
  1465. unsigned long flags;
  1466. struct task_struct *parent;
  1467. struct sighand_struct *sighand;
  1468. if (for_ptracer) {
  1469. parent = tsk->parent;
  1470. } else {
  1471. tsk = tsk->group_leader;
  1472. parent = tsk->real_parent;
  1473. }
  1474. info.si_signo = SIGCHLD;
  1475. info.si_errno = 0;
  1476. /*
  1477. * see comment in do_notify_parent() about the following 4 lines
  1478. */
  1479. rcu_read_lock();
  1480. info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
  1481. info.si_uid = __task_cred(tsk)->uid;
  1482. rcu_read_unlock();
  1483. info.si_utime = cputime_to_clock_t(tsk->utime);
  1484. info.si_stime = cputime_to_clock_t(tsk->stime);
  1485. info.si_code = why;
  1486. switch (why) {
  1487. case CLD_CONTINUED:
  1488. info.si_status = SIGCONT;
  1489. break;
  1490. case CLD_STOPPED:
  1491. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1492. break;
  1493. case CLD_TRAPPED:
  1494. info.si_status = tsk->exit_code & 0x7f;
  1495. break;
  1496. default:
  1497. BUG();
  1498. }
  1499. sighand = parent->sighand;
  1500. spin_lock_irqsave(&sighand->siglock, flags);
  1501. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1502. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1503. __group_send_sig_info(SIGCHLD, &info, parent);
  1504. /*
  1505. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1506. */
  1507. __wake_up_parent(tsk, parent);
  1508. spin_unlock_irqrestore(&sighand->siglock, flags);
  1509. }
  1510. static inline int may_ptrace_stop(void)
  1511. {
  1512. if (!likely(task_ptrace(current)))
  1513. return 0;
  1514. /*
  1515. * Are we in the middle of do_coredump?
  1516. * If so and our tracer is also part of the coredump stopping
  1517. * is a deadlock situation, and pointless because our tracer
  1518. * is dead so don't allow us to stop.
  1519. * If SIGKILL was already sent before the caller unlocked
  1520. * ->siglock we must see ->core_state != NULL. Otherwise it
  1521. * is safe to enter schedule().
  1522. */
  1523. if (unlikely(current->mm->core_state) &&
  1524. unlikely(current->mm == current->parent->mm))
  1525. return 0;
  1526. return 1;
  1527. }
  1528. /*
  1529. * Return non-zero if there is a SIGKILL that should be waking us up.
  1530. * Called with the siglock held.
  1531. */
  1532. static int sigkill_pending(struct task_struct *tsk)
  1533. {
  1534. return sigismember(&tsk->pending.signal, SIGKILL) ||
  1535. sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
  1536. }
  1537. /*
  1538. * Test whether the target task of the usual cldstop notification - the
  1539. * real_parent of @child - is in the same group as the ptracer.
  1540. */
  1541. static bool real_parent_is_ptracer(struct task_struct *child)
  1542. {
  1543. return same_thread_group(child->parent, child->real_parent);
  1544. }
  1545. /*
  1546. * This must be called with current->sighand->siglock held.
  1547. *
  1548. * This should be the path for all ptrace stops.
  1549. * We always set current->last_siginfo while stopped here.
  1550. * That makes it a way to test a stopped process for
  1551. * being ptrace-stopped vs being job-control-stopped.
  1552. *
  1553. * If we actually decide not to stop at all because the tracer
  1554. * is gone, we keep current->exit_code unless clear_code.
  1555. */
  1556. static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  1557. __releases(&current->sighand->siglock)
  1558. __acquires(&current->sighand->siglock)
  1559. {
  1560. bool gstop_done = false;
  1561. if (arch_ptrace_stop_needed(exit_code, info)) {
  1562. /*
  1563. * The arch code has something special to do before a
  1564. * ptrace stop. This is allowed to block, e.g. for faults
  1565. * on user stack pages. We can't keep the siglock while
  1566. * calling arch_ptrace_stop, so we must release it now.
  1567. * To preserve proper semantics, we must do this before
  1568. * any signal bookkeeping like checking group_stop_count.
  1569. * Meanwhile, a SIGKILL could come in before we retake the
  1570. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1571. * So after regaining the lock, we must check for SIGKILL.
  1572. */
  1573. spin_unlock_irq(&current->sighand->siglock);
  1574. arch_ptrace_stop(exit_code, info);
  1575. spin_lock_irq(&current->sighand->siglock);
  1576. if (sigkill_pending(current))
  1577. return;
  1578. }
  1579. /*
  1580. * We're committing to trapping. TRACED should be visible before
  1581. * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
  1582. * Also, transition to TRACED and updates to ->jobctl should be
  1583. * atomic with respect to siglock and should be done after the arch
  1584. * hook as siglock is released and regrabbed across it.
  1585. */
  1586. set_current_state(TASK_TRACED);
  1587. current->last_siginfo = info;
  1588. current->exit_code = exit_code;
  1589. /*
  1590. * If @why is CLD_STOPPED, we're trapping to participate in a group
  1591. * stop. Do the bookkeeping. Note that if SIGCONT was delievered
  1592. * across siglock relocks since INTERRUPT was scheduled, PENDING
  1593. * could be clear now. We act as if SIGCONT is received after
  1594. * TASK_TRACED is entered - ignore it.
  1595. */
  1596. if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
  1597. gstop_done = task_participate_group_stop(current);
  1598. /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
  1599. task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1600. if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
  1601. task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
  1602. /* entering a trap, clear TRAPPING */
  1603. task_clear_jobctl_trapping(current);
  1604. spin_unlock_irq(&current->sighand->siglock);
  1605. read_lock(&tasklist_lock);
  1606. if (may_ptrace_stop()) {
  1607. /*
  1608. * Notify parents of the stop.
  1609. *
  1610. * While ptraced, there are two parents - the ptracer and
  1611. * the real_parent of the group_leader. The ptracer should
  1612. * know about every stop while the real parent is only
  1613. * interested in the completion of group stop. The states
  1614. * for the two don't interact with each other. Notify
  1615. * separately unless they're gonna be duplicates.
  1616. */
  1617. do_notify_parent_cldstop(current, true, why);
  1618. if (gstop_done && !real_parent_is_ptracer(current))
  1619. do_notify_parent_cldstop(current, false, why);
  1620. /*
  1621. * Don't want to allow preemption here, because
  1622. * sys_ptrace() needs this task to be inactive.
  1623. *
  1624. * XXX: implement read_unlock_no_resched().
  1625. */
  1626. preempt_disable();
  1627. read_unlock(&tasklist_lock);
  1628. preempt_enable_no_resched();
  1629. schedule();
  1630. } else {
  1631. /*
  1632. * By the time we got the lock, our tracer went away.
  1633. * Don't drop the lock yet, another tracer may come.
  1634. *
  1635. * If @gstop_done, the ptracer went away between group stop
  1636. * completion and here. During detach, it would have set
  1637. * JOBCTL_STOP_PENDING on us and we'll re-enter
  1638. * TASK_STOPPED in do_signal_stop() on return, so notifying
  1639. * the real parent of the group stop completion is enough.
  1640. */
  1641. if (gstop_done)
  1642. do_notify_parent_cldstop(current, false, why);
  1643. __set_current_state(TASK_RUNNING);
  1644. if (clear_code)
  1645. current->exit_code = 0;
  1646. read_unlock(&tasklist_lock);
  1647. }
  1648. /*
  1649. * While in TASK_TRACED, we were considered "frozen enough".
  1650. * Now that we woke up, it's crucial if we're supposed to be
  1651. * frozen that we freeze now before running anything substantial.
  1652. */
  1653. try_to_freeze();
  1654. /*
  1655. * We are back. Now reacquire the siglock before touching
  1656. * last_siginfo, so that we are sure to have synchronized with
  1657. * any signal-sending on another CPU that wants to examine it.
  1658. */
  1659. spin_lock_irq(&current->sighand->siglock);
  1660. current->last_siginfo = NULL;
  1661. /*
  1662. * Queued signals ignored us while we were stopped for tracing.
  1663. * So check for any that we should take before resuming user mode.
  1664. * This sets TIF_SIGPENDING, but never clears it.
  1665. */
  1666. recalc_sigpending_tsk(current);
  1667. }
  1668. static void ptrace_do_notify(int signr, int exit_code, int why)
  1669. {
  1670. siginfo_t info;
  1671. memset(&info, 0, sizeof info);
  1672. info.si_signo = signr;
  1673. info.si_code = exit_code;
  1674. info.si_pid = task_pid_vnr(current);
  1675. info.si_uid = current_uid();
  1676. /* Let the debugger run. */
  1677. ptrace_stop(exit_code, why, 1, &info);
  1678. }
  1679. void ptrace_notify(int exit_code)
  1680. {
  1681. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1682. spin_lock_irq(&current->sighand->siglock);
  1683. ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
  1684. spin_unlock_irq(&current->sighand->siglock);
  1685. }
  1686. /**
  1687. * do_signal_stop - handle group stop for SIGSTOP and other stop signals
  1688. * @signr: signr causing group stop if initiating
  1689. *
  1690. * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
  1691. * and participate in it. If already set, participate in the existing
  1692. * group stop. If participated in a group stop (and thus slept), %true is
  1693. * returned with siglock released.
  1694. *
  1695. * If ptraced, this function doesn't handle stop itself. Instead,
  1696. * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
  1697. * untouched. The caller must ensure that INTERRUPT trap handling takes
  1698. * places afterwards.
  1699. *
  1700. * CONTEXT:
  1701. * Must be called with @current->sighand->siglock held, which is released
  1702. * on %true return.
  1703. *
  1704. * RETURNS:
  1705. * %false if group stop is already cancelled or ptrace trap is scheduled.
  1706. * %true if participated in group stop.
  1707. */
  1708. static bool do_signal_stop(int signr)
  1709. __releases(&current->sighand->siglock)
  1710. {
  1711. struct signal_struct *sig = current->signal;
  1712. if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
  1713. unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
  1714. struct task_struct *t;
  1715. /* signr will be recorded in task->jobctl for retries */
  1716. WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
  1717. if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
  1718. unlikely(signal_group_exit(sig)))
  1719. return false;
  1720. /*
  1721. * There is no group stop already in progress. We must
  1722. * initiate one now.
  1723. *
  1724. * While ptraced, a task may be resumed while group stop is
  1725. * still in effect and then receive a stop signal and
  1726. * initiate another group stop. This deviates from the
  1727. * usual behavior as two consecutive stop signals can't
  1728. * cause two group stops when !ptraced. That is why we
  1729. * also check !task_is_stopped(t) below.
  1730. *
  1731. * The condition can be distinguished by testing whether
  1732. * SIGNAL_STOP_STOPPED is already set. Don't generate
  1733. * group_exit_code in such case.
  1734. *
  1735. * This is not necessary for SIGNAL_STOP_CONTINUED because
  1736. * an intervening stop signal is required to cause two
  1737. * continued events regardless of ptrace.
  1738. */
  1739. if (!(sig->flags & SIGNAL_STOP_STOPPED))
  1740. sig->group_exit_code = signr;
  1741. else
  1742. WARN_ON_ONCE(!task_ptrace(current));
  1743. sig->group_stop_count = 0;
  1744. if (task_set_jobctl_pending(current, signr | gstop))
  1745. sig->group_stop_count++;
  1746. for (t = next_thread(current); t != current;
  1747. t = next_thread(t)) {
  1748. /*
  1749. * Setting state to TASK_STOPPED for a group
  1750. * stop is always done with the siglock held,
  1751. * so this check has no races.
  1752. */
  1753. if (!task_is_stopped(t) &&
  1754. task_set_jobctl_pending(t, signr | gstop)) {
  1755. sig->group_stop_count++;
  1756. if (likely(!(t->ptrace & PT_SEIZED)))
  1757. signal_wake_up(t, 0);
  1758. else
  1759. ptrace_trap_notify(t);
  1760. }
  1761. }
  1762. }
  1763. if (likely(!task_ptrace(current))) {
  1764. int notify = 0;
  1765. /*
  1766. * If there are no other threads in the group, or if there
  1767. * is a group stop in progress and we are the last to stop,
  1768. * report to the parent.
  1769. */
  1770. if (task_participate_group_stop(current))
  1771. notify = CLD_STOPPED;
  1772. __set_current_state(TASK_STOPPED);
  1773. spin_unlock_irq(&current->sighand->siglock);
  1774. /*
  1775. * Notify the parent of the group stop completion. Because
  1776. * we're not holding either the siglock or tasklist_lock
  1777. * here, ptracer may attach inbetween; however, this is for
  1778. * group stop and should always be delivered to the real
  1779. * parent of the group leader. The new ptracer will get
  1780. * its notification when this task transitions into
  1781. * TASK_TRACED.
  1782. */
  1783. if (notify) {
  1784. read_lock(&tasklist_lock);
  1785. do_notify_parent_cldstop(current, false, notify);
  1786. read_unlock(&tasklist_lock);
  1787. }
  1788. /* Now we don't run again until woken by SIGCONT or SIGKILL */
  1789. schedule();
  1790. return true;
  1791. } else {
  1792. /*
  1793. * While ptraced, group stop is handled by STOP trap.
  1794. * Schedule it and let the caller deal with it.
  1795. */
  1796. task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1797. return false;
  1798. }
  1799. }
  1800. /**
  1801. * do_jobctl_trap - take care of ptrace jobctl traps
  1802. *
  1803. * When PT_SEIZED, it's used for both group stop and explicit
  1804. * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
  1805. * accompanying siginfo. If stopped, lower eight bits of exit_code contain
  1806. * the stop signal; otherwise, %SIGTRAP.
  1807. *
  1808. * When !PT_SEIZED, it's used only for group stop trap with stop signal
  1809. * number as exit_code and no siginfo.
  1810. *
  1811. * CONTEXT:
  1812. * Must be called with @current->sighand->siglock held, which may be
  1813. * released and re-acquired before returning with intervening sleep.
  1814. */
  1815. static void do_jobctl_trap(void)
  1816. {
  1817. struct signal_struct *signal = current->signal;
  1818. int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
  1819. if (current->ptrace & PT_SEIZED) {
  1820. if (!signal->group_stop_count &&
  1821. !(signal->flags & SIGNAL_STOP_STOPPED))
  1822. signr = SIGTRAP;
  1823. WARN_ON_ONCE(!signr);
  1824. ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
  1825. CLD_STOPPED);
  1826. } else {
  1827. WARN_ON_ONCE(!signr);
  1828. ptrace_stop(signr, CLD_STOPPED, 0, NULL);
  1829. current->exit_code = 0;
  1830. }
  1831. }
  1832. static int ptrace_signal(int signr, siginfo_t *info,
  1833. struct pt_regs *regs, void *cookie)
  1834. {
  1835. if (!task_ptrace(current))
  1836. return signr;
  1837. ptrace_signal_deliver(regs, cookie);
  1838. /* Let the debugger run. */
  1839. ptrace_stop(signr, CLD_TRAPPED, 0, info);
  1840. /* We're back. Did the debugger cancel the sig? */
  1841. signr = current->exit_code;
  1842. if (signr == 0)
  1843. return signr;
  1844. current->exit_code = 0;
  1845. /*
  1846. * Update the siginfo structure if the signal has
  1847. * changed. If the debugger wanted something
  1848. * specific in the siginfo structure then it should
  1849. * have updated *info via PTRACE_SETSIGINFO.
  1850. */
  1851. if (signr != info->si_signo) {
  1852. info->si_signo = signr;
  1853. info->si_errno = 0;
  1854. info->si_code = SI_USER;
  1855. info->si_pid = task_pid_vnr(current->parent);
  1856. info->si_uid = task_uid(current->parent);
  1857. }
  1858. /* If the (new) signal is now blocked, requeue it. */
  1859. if (sigismember(&current->blocked, signr)) {
  1860. specific_send_sig_info(signr, info, current);
  1861. signr = 0;
  1862. }
  1863. return signr;
  1864. }
  1865. int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
  1866. struct pt_regs *regs, void *cookie)
  1867. {
  1868. struct sighand_struct *sighand = current->sighand;
  1869. struct signal_struct *signal = current->signal;
  1870. int signr;
  1871. relock:
  1872. /*
  1873. * We'll jump back here after any time we were stopped in TASK_STOPPED.
  1874. * While in TASK_STOPPED, we were considered "frozen enough".
  1875. * Now that we woke up, it's crucial if we're supposed to be
  1876. * frozen that we freeze now before running anything substantial.
  1877. */
  1878. try_to_freeze();
  1879. spin_lock_irq(&sighand->siglock);
  1880. /*
  1881. * Every stopped thread goes here after wakeup. Check to see if
  1882. * we should notify the parent, prepare_signal(SIGCONT) encodes
  1883. * the CLD_ si_code into SIGNAL_CLD_MASK bits.
  1884. */
  1885. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  1886. struct task_struct *leader;
  1887. int why;
  1888. if (signal->flags & SIGNAL_CLD_CONTINUED)
  1889. why = CLD_CONTINUED;
  1890. else
  1891. why = CLD_STOPPED;
  1892. signal->flags &= ~SIGNAL_CLD_MASK;
  1893. spin_unlock_irq(&sighand->siglock);
  1894. /*
  1895. * Notify the parent that we're continuing. This event is
  1896. * always per-process and doesn't make whole lot of sense
  1897. * for ptracers, who shouldn't consume the state via
  1898. * wait(2) either, but, for backward compatibility, notify
  1899. * the ptracer of the group leader too unless it's gonna be
  1900. * a duplicate.
  1901. */
  1902. read_lock(&tasklist_lock);
  1903. do_notify_parent_cldstop(current, false, why);
  1904. leader = current->group_leader;
  1905. if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
  1906. do_notify_parent_cldstop(leader, true, why);
  1907. read_unlock(&tasklist_lock);
  1908. goto relock;
  1909. }
  1910. for (;;) {
  1911. struct k_sigaction *ka;
  1912. if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
  1913. do_signal_stop(0))
  1914. goto relock;
  1915. if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
  1916. do_jobctl_trap();
  1917. spin_unlock_irq(&sighand->siglock);
  1918. goto relock;
  1919. }
  1920. signr = dequeue_signal(current, &current->blocked, info);
  1921. if (!signr)
  1922. break; /* will return 0 */
  1923. if (signr != SIGKILL) {
  1924. signr = ptrace_signal(signr, info,
  1925. regs, cookie);
  1926. if (!signr)
  1927. continue;
  1928. }
  1929. ka = &sighand->action[signr-1];
  1930. /* Trace actually delivered signals. */
  1931. trace_signal_deliver(signr, info, ka);
  1932. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1933. continue;
  1934. if (ka->sa.sa_handler != SIG_DFL) {
  1935. /* Run the handler. */
  1936. *return_ka = *ka;
  1937. if (ka->sa.sa_flags & SA_ONESHOT)
  1938. ka->sa.sa_handler = SIG_DFL;
  1939. break; /* will return non-zero "signr" value */
  1940. }
  1941. /*
  1942. * Now we are doing the default action for this signal.
  1943. */
  1944. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  1945. continue;
  1946. /*
  1947. * Global init gets no signals it doesn't want.
  1948. * Container-init gets no signals it doesn't want from same
  1949. * container.
  1950. *
  1951. * Note that if global/container-init sees a sig_kernel_only()
  1952. * signal here, the signal must have been generated internally
  1953. * or must have come from an ancestor namespace. In either
  1954. * case, the signal cannot be dropped.
  1955. */
  1956. if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
  1957. !sig_kernel_only(signr))
  1958. continue;
  1959. if (sig_kernel_stop(signr)) {
  1960. /*
  1961. * The default action is to stop all threads in
  1962. * the thread group. The job control signals
  1963. * do nothing in an orphaned pgrp, but SIGSTOP
  1964. * always works. Note that siglock needs to be
  1965. * dropped during the call to is_orphaned_pgrp()
  1966. * because of lock ordering with tasklist_lock.
  1967. * This allows an intervening SIGCONT to be posted.
  1968. * We need to check for that and bail out if necessary.
  1969. */
  1970. if (signr != SIGSTOP) {
  1971. spin_unlock_irq(&sighand->siglock);
  1972. /* signals can be posted during this window */
  1973. if (is_current_pgrp_orphaned())
  1974. goto relock;
  1975. spin_lock_irq(&sighand->siglock);
  1976. }
  1977. if (likely(do_signal_stop(info->si_signo))) {
  1978. /* It released the siglock. */
  1979. goto relock;
  1980. }
  1981. /*
  1982. * We didn't actually stop, due to a race
  1983. * with SIGCONT or something like that.
  1984. */
  1985. continue;
  1986. }
  1987. spin_unlock_irq(&sighand->siglock);
  1988. /*
  1989. * Anything else is fatal, maybe with a core dump.
  1990. */
  1991. current->flags |= PF_SIGNALED;
  1992. if (sig_kernel_coredump(signr)) {
  1993. if (print_fatal_signals)
  1994. print_fatal_signal(regs, info->si_signo);
  1995. /*
  1996. * If it was able to dump core, this kills all
  1997. * other threads in the group and synchronizes with
  1998. * their demise. If we lost the race with another
  1999. * thread getting here, it set group_exit_code
  2000. * first and our do_group_exit call below will use
  2001. * that value and ignore the one we pass it.
  2002. */
  2003. do_coredump(info->si_signo, info->si_signo, regs);
  2004. }
  2005. /*
  2006. * Death signals, no core dump.
  2007. */
  2008. do_group_exit(info->si_signo);
  2009. /* NOTREACHED */
  2010. }
  2011. spin_unlock_irq(&sighand->siglock);
  2012. return signr;
  2013. }
  2014. /*
  2015. * It could be that complete_signal() picked us to notify about the
  2016. * group-wide signal. Other threads should be notified now to take
  2017. * the shared signals in @which since we will not.
  2018. */
  2019. static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
  2020. {
  2021. sigset_t retarget;
  2022. struct task_struct *t;
  2023. sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
  2024. if (sigisemptyset(&retarget))
  2025. return;
  2026. t = tsk;
  2027. while_each_thread(tsk, t) {
  2028. if (t->flags & PF_EXITING)
  2029. continue;
  2030. if (!has_pending_signals(&retarget, &t->blocked))
  2031. continue;
  2032. /* Remove the signals this thread can handle. */
  2033. sigandsets(&retarget, &retarget, &t->blocked);
  2034. if (!signal_pending(t))
  2035. signal_wake_up(t, 0);
  2036. if (sigisemptyset(&retarget))
  2037. break;
  2038. }
  2039. }
  2040. void exit_signals(struct task_struct *tsk)
  2041. {
  2042. int group_stop = 0;
  2043. sigset_t unblocked;
  2044. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  2045. tsk->flags |= PF_EXITING;
  2046. return;
  2047. }
  2048. spin_lock_irq(&tsk->sighand->siglock);
  2049. /*
  2050. * From now this task is not visible for group-wide signals,
  2051. * see wants_signal(), do_signal_stop().
  2052. */
  2053. tsk->flags |= PF_EXITING;
  2054. if (!signal_pending(tsk))
  2055. goto out;
  2056. unblocked = tsk->blocked;
  2057. signotset(&unblocked);
  2058. retarget_shared_pending(tsk, &unblocked);
  2059. if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
  2060. task_participate_group_stop(tsk))
  2061. group_stop = CLD_STOPPED;
  2062. out:
  2063. spin_unlock_irq(&tsk->sighand->siglock);
  2064. /*
  2065. * If group stop has completed, deliver the notification. This
  2066. * should always go to the real parent of the group leader.
  2067. */
  2068. if (unlikely(group_stop)) {
  2069. read_lock(&tasklist_lock);
  2070. do_notify_parent_cldstop(tsk, false, group_stop);
  2071. read_unlock(&tasklist_lock);
  2072. }
  2073. }
  2074. EXPORT_SYMBOL(recalc_sigpending);
  2075. EXPORT_SYMBOL_GPL(dequeue_signal);
  2076. EXPORT_SYMBOL(flush_signals);
  2077. EXPORT_SYMBOL(force_sig);
  2078. EXPORT_SYMBOL(send_sig);
  2079. EXPORT_SYMBOL(send_sig_info);
  2080. EXPORT_SYMBOL(sigprocmask);
  2081. EXPORT_SYMBOL(block_all_signals);
  2082. EXPORT_SYMBOL(unblock_all_signals);
  2083. /*
  2084. * System call entry points.
  2085. */
  2086. /**
  2087. * sys_restart_syscall - restart a system call
  2088. */
  2089. SYSCALL_DEFINE0(restart_syscall)
  2090. {
  2091. struct restart_block *restart = &current_thread_info()->restart_block;
  2092. return restart->fn(restart);
  2093. }
  2094. long do_no_restart_syscall(struct restart_block *param)
  2095. {
  2096. return -EINTR;
  2097. }
  2098. static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
  2099. {
  2100. if (signal_pending(tsk) && !thread_group_empty(tsk)) {
  2101. sigset_t newblocked;
  2102. /* A set of now blocked but previously unblocked signals. */
  2103. sigandnsets(&newblocked, newset, &current->blocked);
  2104. retarget_shared_pending(tsk, &newblocked);
  2105. }
  2106. tsk->blocked = *newset;
  2107. recalc_sigpending();
  2108. }
  2109. /**
  2110. * set_current_blocked - change current->blocked mask
  2111. * @newset: new mask
  2112. *
  2113. * It is wrong to change ->blocked directly, this helper should be used
  2114. * to ensure the process can't miss a shared signal we are going to block.
  2115. */
  2116. void set_current_blocked(const sigset_t *newset)
  2117. {
  2118. struct task_struct *tsk = current;
  2119. spin_lock_irq(&tsk->sighand->siglock);
  2120. __set_task_blocked(tsk, newset);
  2121. spin_unlock_irq(&tsk->sighand->siglock);
  2122. }
  2123. /*
  2124. * This is also useful for kernel threads that want to temporarily
  2125. * (or permanently) block certain signals.
  2126. *
  2127. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  2128. * interface happily blocks "unblockable" signals like SIGKILL
  2129. * and friends.
  2130. */
  2131. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  2132. {
  2133. struct task_struct *tsk = current;
  2134. sigset_t newset;
  2135. /* Lockless, only current can change ->blocked, never from irq */
  2136. if (oldset)
  2137. *oldset = tsk->blocked;
  2138. switch (how) {
  2139. case SIG_BLOCK:
  2140. sigorsets(&newset, &tsk->blocked, set);
  2141. break;
  2142. case SIG_UNBLOCK:
  2143. sigandnsets(&newset, &tsk->blocked, set);
  2144. break;
  2145. case SIG_SETMASK:
  2146. newset = *set;
  2147. break;
  2148. default:
  2149. return -EINVAL;
  2150. }
  2151. set_current_blocked(&newset);
  2152. return 0;
  2153. }
  2154. /**
  2155. * sys_rt_sigprocmask - change the list of currently blocked signals
  2156. * @how: whether to add, remove, or set signals
  2157. * @set: stores pending signals
  2158. * @oset: previous value of signal mask if non-null
  2159. * @sigsetsize: size of sigset_t type
  2160. */
  2161. SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
  2162. sigset_t __user *, oset, size_t, sigsetsize)
  2163. {
  2164. sigset_t old_set, new_set;
  2165. int error;
  2166. /* XXX: Don't preclude handling different sized sigset_t's. */
  2167. if (sigsetsize != sizeof(sigset_t))
  2168. return -EINVAL;
  2169. old_set = current->blocked;
  2170. if (nset) {
  2171. if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
  2172. return -EFAULT;
  2173. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2174. error = sigprocmask(how, &new_set, NULL);
  2175. if (error)
  2176. return error;
  2177. }
  2178. if (oset) {
  2179. if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
  2180. return -EFAULT;
  2181. }
  2182. return 0;
  2183. }
  2184. long do_sigpending(void __user *set, unsigned long sigsetsize)
  2185. {
  2186. long error = -EINVAL;
  2187. sigset_t pending;
  2188. if (sigsetsize > sizeof(sigset_t))
  2189. goto out;
  2190. spin_lock_irq(&current->sighand->siglock);
  2191. sigorsets(&pending, &current->pending.signal,
  2192. &current->signal->shared_pending.signal);
  2193. spin_unlock_irq(&current->sighand->siglock);
  2194. /* Outside the lock because only this thread touches it. */
  2195. sigandsets(&pending, &current->blocked, &pending);
  2196. error = -EFAULT;
  2197. if (!copy_to_user(set, &pending, sigsetsize))
  2198. error = 0;
  2199. out:
  2200. return error;
  2201. }
  2202. /**
  2203. * sys_rt_sigpending - examine a pending signal that has been raised
  2204. * while blocked
  2205. * @set: stores pending signals
  2206. * @sigsetsize: size of sigset_t type or larger
  2207. */
  2208. SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
  2209. {
  2210. return do_sigpending(set, sigsetsize);
  2211. }
  2212. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  2213. int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
  2214. {
  2215. int err;
  2216. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  2217. return -EFAULT;
  2218. if (from->si_code < 0)
  2219. return __copy_to_user(to, from, sizeof(siginfo_t))
  2220. ? -EFAULT : 0;
  2221. /*
  2222. * If you change siginfo_t structure, please be sure
  2223. * this code is fixed accordingly.
  2224. * Please remember to update the signalfd_copyinfo() function
  2225. * inside fs/signalfd.c too, in case siginfo_t changes.
  2226. * It should never copy any pad contained in the structure
  2227. * to avoid security leaks, but must copy the generic
  2228. * 3 ints plus the relevant union member.
  2229. */
  2230. err = __put_user(from->si_signo, &to->si_signo);
  2231. err |= __put_user(from->si_errno, &to->si_errno);
  2232. err |= __put_user((short)from->si_code, &to->si_code);
  2233. switch (from->si_code & __SI_MASK) {
  2234. case __SI_KILL:
  2235. err |= __put_user(from->si_pid, &to->si_pid);
  2236. err |= __put_user(from->si_uid, &to->si_uid);
  2237. break;
  2238. case __SI_TIMER:
  2239. err |= __put_user(from->si_tid, &to->si_tid);
  2240. err |= __put_user(from->si_overrun, &to->si_overrun);
  2241. err |= __put_user(from->si_ptr, &to->si_ptr);
  2242. break;
  2243. case __SI_POLL:
  2244. err |= __put_user(from->si_band, &to->si_band);
  2245. err |= __put_user(from->si_fd, &to->si_fd);
  2246. break;
  2247. case __SI_FAULT:
  2248. err |= __put_user(from->si_addr, &to->si_addr);
  2249. #ifdef __ARCH_SI_TRAPNO
  2250. err |= __put_user(from->si_trapno, &to->si_trapno);
  2251. #endif
  2252. #ifdef BUS_MCEERR_AO
  2253. /*
  2254. * Other callers might not initialize the si_lsb field,
  2255. * so check explicitly for the right codes here.
  2256. */
  2257. if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
  2258. err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
  2259. #endif
  2260. break;
  2261. case __SI_CHLD:
  2262. err |= __put_user(from->si_pid, &to->si_pid);
  2263. err |= __put_user(from->si_uid, &to->si_uid);
  2264. err |= __put_user(from->si_status, &to->si_status);
  2265. err |= __put_user(from->si_utime, &to->si_utime);
  2266. err |= __put_user(from->si_stime, &to->si_stime);
  2267. break;
  2268. case __SI_RT: /* This is not generated by the kernel as of now. */
  2269. case __SI_MESGQ: /* But this is */
  2270. err |= __put_user(from->si_pid, &to->si_pid);
  2271. err |= __put_user(from->si_uid, &to->si_uid);
  2272. err |= __put_user(from->si_ptr, &to->si_ptr);
  2273. break;
  2274. default: /* this is just in case for now ... */
  2275. err |= __put_user(from->si_pid, &to->si_pid);
  2276. err |= __put_user(from->si_uid, &to->si_uid);
  2277. break;
  2278. }
  2279. return err;
  2280. }
  2281. #endif
  2282. /**
  2283. * do_sigtimedwait - wait for queued signals specified in @which
  2284. * @which: queued signals to wait for
  2285. * @info: if non-null, the signal's siginfo is returned here
  2286. * @ts: upper bound on process time suspension
  2287. */
  2288. int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
  2289. const struct timespec *ts)
  2290. {
  2291. struct task_struct *tsk = current;
  2292. long timeout = MAX_SCHEDULE_TIMEOUT;
  2293. sigset_t mask = *which;
  2294. int sig;
  2295. if (ts) {
  2296. if (!timespec_valid(ts))
  2297. return -EINVAL;
  2298. timeout = timespec_to_jiffies(ts);
  2299. /*
  2300. * We can be close to the next tick, add another one
  2301. * to ensure we will wait at least the time asked for.
  2302. */
  2303. if (ts->tv_sec || ts->tv_nsec)
  2304. timeout++;
  2305. }
  2306. /*
  2307. * Invert the set of allowed signals to get those we want to block.
  2308. */
  2309. sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  2310. signotset(&mask);
  2311. spin_lock_irq(&tsk->sighand->siglock);
  2312. sig = dequeue_signal(tsk, &mask, info);
  2313. if (!sig && timeout) {
  2314. /*
  2315. * None ready, temporarily unblock those we're interested
  2316. * while we are sleeping in so that we'll be awakened when
  2317. * they arrive. Unblocking is always fine, we can avoid
  2318. * set_current_blocked().
  2319. */
  2320. tsk->real_blocked = tsk->blocked;
  2321. sigandsets(&tsk->blocked, &tsk->blocked, &mask);
  2322. recalc_sigpending();
  2323. spin_unlock_irq(&tsk->sighand->siglock);
  2324. timeout = schedule_timeout_interruptible(timeout);
  2325. spin_lock_irq(&tsk->sighand->siglock);
  2326. __set_task_blocked(tsk, &tsk->real_blocked);
  2327. siginitset(&tsk->real_blocked, 0);
  2328. sig = dequeue_signal(tsk, &mask, info);
  2329. }
  2330. spin_unlock_irq(&tsk->sighand->siglock);
  2331. if (sig)
  2332. return sig;
  2333. return timeout ? -EINTR : -EAGAIN;
  2334. }
  2335. /**
  2336. * sys_rt_sigtimedwait - synchronously wait for queued signals specified
  2337. * in @uthese
  2338. * @uthese: queued signals to wait for
  2339. * @uinfo: if non-null, the signal's siginfo is returned here
  2340. * @uts: upper bound on process time suspension
  2341. * @sigsetsize: size of sigset_t type
  2342. */
  2343. SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
  2344. siginfo_t __user *, uinfo, const struct timespec __user *, uts,
  2345. size_t, sigsetsize)
  2346. {
  2347. sigset_t these;
  2348. struct timespec ts;
  2349. siginfo_t info;
  2350. int ret;
  2351. /* XXX: Don't preclude handling different sized sigset_t's. */
  2352. if (sigsetsize != sizeof(sigset_t))
  2353. return -EINVAL;
  2354. if (copy_from_user(&these, uthese, sizeof(these)))
  2355. return -EFAULT;
  2356. if (uts) {
  2357. if (copy_from_user(&ts, uts, sizeof(ts)))
  2358. return -EFAULT;
  2359. }
  2360. ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
  2361. if (ret > 0 && uinfo) {
  2362. if (copy_siginfo_to_user(uinfo, &info))
  2363. ret = -EFAULT;
  2364. }
  2365. return ret;
  2366. }
  2367. /**
  2368. * sys_kill - send a signal to a process
  2369. * @pid: the PID of the process
  2370. * @sig: signal to be sent
  2371. */
  2372. SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
  2373. {
  2374. struct siginfo info;
  2375. info.si_signo = sig;
  2376. info.si_errno = 0;
  2377. info.si_code = SI_USER;
  2378. info.si_pid = task_tgid_vnr(current);
  2379. info.si_uid = current_uid();
  2380. return kill_something_info(sig, &info, pid);
  2381. }
  2382. static int
  2383. do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
  2384. {
  2385. struct task_struct *p;
  2386. int error = -ESRCH;
  2387. rcu_read_lock();
  2388. p = find_task_by_vpid(pid);
  2389. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  2390. error = check_kill_permission(sig, info, p);
  2391. /*
  2392. * The null signal is a permissions and process existence
  2393. * probe. No signal is actually delivered.
  2394. */
  2395. if (!error && sig) {
  2396. error = do_send_sig_info(sig, info, p, false);
  2397. /*
  2398. * If lock_task_sighand() failed we pretend the task
  2399. * dies after receiving the signal. The window is tiny,
  2400. * and the signal is private anyway.
  2401. */
  2402. if (unlikely(error == -ESRCH))
  2403. error = 0;
  2404. }
  2405. }
  2406. rcu_read_unlock();
  2407. return error;
  2408. }
  2409. static int do_tkill(pid_t tgid, pid_t pid, int sig)
  2410. {
  2411. struct siginfo info;
  2412. info.si_signo = sig;
  2413. info.si_errno = 0;
  2414. info.si_code = SI_TKILL;
  2415. info.si_pid = task_tgid_vnr(current);
  2416. info.si_uid = current_uid();
  2417. return do_send_specific(tgid, pid, sig, &info);
  2418. }
  2419. /**
  2420. * sys_tgkill - send signal to one specific thread
  2421. * @tgid: the thread group ID of the thread
  2422. * @pid: the PID of the thread
  2423. * @sig: signal to be sent
  2424. *
  2425. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  2426. * exists but it's not belonging to the target process anymore. This
  2427. * method solves the problem of threads exiting and PIDs getting reused.
  2428. */
  2429. SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
  2430. {
  2431. /* This is only valid for single tasks */
  2432. if (pid <= 0 || tgid <= 0)
  2433. return -EINVAL;
  2434. return do_tkill(tgid, pid, sig);
  2435. }
  2436. /**
  2437. * sys_tkill - send signal to one specific task
  2438. * @pid: the PID of the task
  2439. * @sig: signal to be sent
  2440. *
  2441. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  2442. */
  2443. SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
  2444. {
  2445. /* This is only valid for single tasks */
  2446. if (pid <= 0)
  2447. return -EINVAL;
  2448. return do_tkill(0, pid, sig);
  2449. }
  2450. /**
  2451. * sys_rt_sigqueueinfo - send signal information to a signal
  2452. * @pid: the PID of the thread
  2453. * @sig: signal to be sent
  2454. * @uinfo: signal info to be sent
  2455. */
  2456. SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
  2457. siginfo_t __user *, uinfo)
  2458. {
  2459. siginfo_t info;
  2460. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2461. return -EFAULT;
  2462. /* Not even root can pretend to send signals from the kernel.
  2463. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2464. */
  2465. if (info.si_code >= 0 || info.si_code == SI_TKILL) {
  2466. /* We used to allow any < 0 si_code */
  2467. WARN_ON_ONCE(info.si_code < 0);
  2468. return -EPERM;
  2469. }
  2470. info.si_signo = sig;
  2471. /* POSIX.1b doesn't mention process groups. */
  2472. return kill_proc_info(sig, &info, pid);
  2473. }
  2474. long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
  2475. {
  2476. /* This is only valid for single tasks */
  2477. if (pid <= 0 || tgid <= 0)
  2478. return -EINVAL;
  2479. /* Not even root can pretend to send signals from the kernel.
  2480. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2481. */
  2482. if (info->si_code >= 0 || info->si_code == SI_TKILL) {
  2483. /* We used to allow any < 0 si_code */
  2484. WARN_ON_ONCE(info->si_code < 0);
  2485. return -EPERM;
  2486. }
  2487. info->si_signo = sig;
  2488. return do_send_specific(tgid, pid, sig, info);
  2489. }
  2490. SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
  2491. siginfo_t __user *, uinfo)
  2492. {
  2493. siginfo_t info;
  2494. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2495. return -EFAULT;
  2496. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2497. }
  2498. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  2499. {
  2500. struct task_struct *t = current;
  2501. struct k_sigaction *k;
  2502. sigset_t mask;
  2503. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  2504. return -EINVAL;
  2505. k = &t->sighand->action[sig-1];
  2506. spin_lock_irq(&current->sighand->siglock);
  2507. if (oact)
  2508. *oact = *k;
  2509. if (act) {
  2510. sigdelsetmask(&act->sa.sa_mask,
  2511. sigmask(SIGKILL) | sigmask(SIGSTOP));
  2512. *k = *act;
  2513. /*
  2514. * POSIX 3.3.1.3:
  2515. * "Setting a signal action to SIG_IGN for a signal that is
  2516. * pending shall cause the pending signal to be discarded,
  2517. * whether or not it is blocked."
  2518. *
  2519. * "Setting a signal action to SIG_DFL for a signal that is
  2520. * pending and whose default action is to ignore the signal
  2521. * (for example, SIGCHLD), shall cause the pending signal to
  2522. * be discarded, whether or not it is blocked"
  2523. */
  2524. if (sig_handler_ignored(sig_handler(t, sig), sig)) {
  2525. sigemptyset(&mask);
  2526. sigaddset(&mask, sig);
  2527. rm_from_queue_full(&mask, &t->signal->shared_pending);
  2528. do {
  2529. rm_from_queue_full(&mask, &t->pending);
  2530. t = next_thread(t);
  2531. } while (t != current);
  2532. }
  2533. }
  2534. spin_unlock_irq(&current->sighand->siglock);
  2535. return 0;
  2536. }
  2537. int
  2538. do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
  2539. {
  2540. stack_t oss;
  2541. int error;
  2542. oss.ss_sp = (void __user *) current->sas_ss_sp;
  2543. oss.ss_size = current->sas_ss_size;
  2544. oss.ss_flags = sas_ss_flags(sp);
  2545. if (uss) {
  2546. void __user *ss_sp;
  2547. size_t ss_size;
  2548. int ss_flags;
  2549. error = -EFAULT;
  2550. if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
  2551. goto out;
  2552. error = __get_user(ss_sp, &uss->ss_sp) |
  2553. __get_user(ss_flags, &uss->ss_flags) |
  2554. __get_user(ss_size, &uss->ss_size);
  2555. if (error)
  2556. goto out;
  2557. error = -EPERM;
  2558. if (on_sig_stack(sp))
  2559. goto out;
  2560. error = -EINVAL;
  2561. /*
  2562. * Note - this code used to test ss_flags incorrectly:
  2563. * old code may have been written using ss_flags==0
  2564. * to mean ss_flags==SS_ONSTACK (as this was the only
  2565. * way that worked) - this fix preserves that older
  2566. * mechanism.
  2567. */
  2568. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  2569. goto out;
  2570. if (ss_flags == SS_DISABLE) {
  2571. ss_size = 0;
  2572. ss_sp = NULL;
  2573. } else {
  2574. error = -ENOMEM;
  2575. if (ss_size < MINSIGSTKSZ)
  2576. goto out;
  2577. }
  2578. current->sas_ss_sp = (unsigned long) ss_sp;
  2579. current->sas_ss_size = ss_size;
  2580. }
  2581. error = 0;
  2582. if (uoss) {
  2583. error = -EFAULT;
  2584. if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
  2585. goto out;
  2586. error = __put_user(oss.ss_sp, &uoss->ss_sp) |
  2587. __put_user(oss.ss_size, &uoss->ss_size) |
  2588. __put_user(oss.ss_flags, &uoss->ss_flags);
  2589. }
  2590. out:
  2591. return error;
  2592. }
  2593. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2594. /**
  2595. * sys_sigpending - examine pending signals
  2596. * @set: where mask of pending signal is returned
  2597. */
  2598. SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
  2599. {
  2600. return do_sigpending(set, sizeof(*set));
  2601. }
  2602. #endif
  2603. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2604. /**
  2605. * sys_sigprocmask - examine and change blocked signals
  2606. * @how: whether to add, remove, or set signals
  2607. * @nset: signals to add or remove (if non-null)
  2608. * @oset: previous value of signal mask if non-null
  2609. *
  2610. * Some platforms have their own version with special arguments;
  2611. * others support only sys_rt_sigprocmask.
  2612. */
  2613. SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
  2614. old_sigset_t __user *, oset)
  2615. {
  2616. old_sigset_t old_set, new_set;
  2617. sigset_t new_blocked;
  2618. old_set = current->blocked.sig[0];
  2619. if (nset) {
  2620. if (copy_from_user(&new_set, nset, sizeof(*nset)))
  2621. return -EFAULT;
  2622. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  2623. new_blocked = current->blocked;
  2624. switch (how) {
  2625. case SIG_BLOCK:
  2626. sigaddsetmask(&new_blocked, new_set);
  2627. break;
  2628. case SIG_UNBLOCK:
  2629. sigdelsetmask(&new_blocked, new_set);
  2630. break;
  2631. case SIG_SETMASK:
  2632. new_blocked.sig[0] = new_set;
  2633. break;
  2634. default:
  2635. return -EINVAL;
  2636. }
  2637. set_current_blocked(&new_blocked);
  2638. }
  2639. if (oset) {
  2640. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2641. return -EFAULT;
  2642. }
  2643. return 0;
  2644. }
  2645. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2646. #ifdef __ARCH_WANT_SYS_RT_SIGACTION
  2647. /**
  2648. * sys_rt_sigaction - alter an action taken by a process
  2649. * @sig: signal to be sent
  2650. * @act: new sigaction
  2651. * @oact: used to save the previous sigaction
  2652. * @sigsetsize: size of sigset_t type
  2653. */
  2654. SYSCALL_DEFINE4(rt_sigaction, int, sig,
  2655. const struct sigaction __user *, act,
  2656. struct sigaction __user *, oact,
  2657. size_t, sigsetsize)
  2658. {
  2659. struct k_sigaction new_sa, old_sa;
  2660. int ret = -EINVAL;
  2661. /* XXX: Don't preclude handling different sized sigset_t's. */
  2662. if (sigsetsize != sizeof(sigset_t))
  2663. goto out;
  2664. if (act) {
  2665. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2666. return -EFAULT;
  2667. }
  2668. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2669. if (!ret && oact) {
  2670. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2671. return -EFAULT;
  2672. }
  2673. out:
  2674. return ret;
  2675. }
  2676. #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
  2677. #ifdef __ARCH_WANT_SYS_SGETMASK
  2678. /*
  2679. * For backwards compatibility. Functionality superseded by sigprocmask.
  2680. */
  2681. SYSCALL_DEFINE0(sgetmask)
  2682. {
  2683. /* SMP safe */
  2684. return current->blocked.sig[0];
  2685. }
  2686. SYSCALL_DEFINE1(ssetmask, int, newmask)
  2687. {
  2688. int old;
  2689. spin_lock_irq(&current->sighand->siglock);
  2690. old = current->blocked.sig[0];
  2691. siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
  2692. sigmask(SIGSTOP)));
  2693. recalc_sigpending();
  2694. spin_unlock_irq(&current->sighand->siglock);
  2695. return old;
  2696. }
  2697. #endif /* __ARCH_WANT_SGETMASK */
  2698. #ifdef __ARCH_WANT_SYS_SIGNAL
  2699. /*
  2700. * For backwards compatibility. Functionality superseded by sigaction.
  2701. */
  2702. SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
  2703. {
  2704. struct k_sigaction new_sa, old_sa;
  2705. int ret;
  2706. new_sa.sa.sa_handler = handler;
  2707. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  2708. sigemptyset(&new_sa.sa.sa_mask);
  2709. ret = do_sigaction(sig, &new_sa, &old_sa);
  2710. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  2711. }
  2712. #endif /* __ARCH_WANT_SYS_SIGNAL */
  2713. #ifdef __ARCH_WANT_SYS_PAUSE
  2714. SYSCALL_DEFINE0(pause)
  2715. {
  2716. while (!signal_pending(current)) {
  2717. current->state = TASK_INTERRUPTIBLE;
  2718. schedule();
  2719. }
  2720. return -ERESTARTNOHAND;
  2721. }
  2722. #endif
  2723. #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
  2724. /**
  2725. * sys_rt_sigsuspend - replace the signal mask for a value with the
  2726. * @unewset value until a signal is received
  2727. * @unewset: new signal mask value
  2728. * @sigsetsize: size of sigset_t type
  2729. */
  2730. SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
  2731. {
  2732. sigset_t newset;
  2733. /* XXX: Don't preclude handling different sized sigset_t's. */
  2734. if (sigsetsize != sizeof(sigset_t))
  2735. return -EINVAL;
  2736. if (copy_from_user(&newset, unewset, sizeof(newset)))
  2737. return -EFAULT;
  2738. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2739. spin_lock_irq(&current->sighand->siglock);
  2740. current->saved_sigmask = current->blocked;
  2741. current->blocked = newset;
  2742. recalc_sigpending();
  2743. spin_unlock_irq(&current->sighand->siglock);
  2744. current->state = TASK_INTERRUPTIBLE;
  2745. schedule();
  2746. set_restore_sigmask();
  2747. return -ERESTARTNOHAND;
  2748. }
  2749. #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
  2750. __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
  2751. {
  2752. return NULL;
  2753. }
  2754. void __init signals_init(void)
  2755. {
  2756. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  2757. }
  2758. #ifdef CONFIG_KGDB_KDB
  2759. #include <linux/kdb.h>
  2760. /*
  2761. * kdb_send_sig_info - Allows kdb to send signals without exposing
  2762. * signal internals. This function checks if the required locks are
  2763. * available before calling the main signal code, to avoid kdb
  2764. * deadlocks.
  2765. */
  2766. void
  2767. kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
  2768. {
  2769. static struct task_struct *kdb_prev_t;
  2770. int sig, new_t;
  2771. if (!spin_trylock(&t->sighand->siglock)) {
  2772. kdb_printf("Can't do kill command now.\n"
  2773. "The sigmask lock is held somewhere else in "
  2774. "kernel, try again later\n");
  2775. return;
  2776. }
  2777. spin_unlock(&t->sighand->siglock);
  2778. new_t = kdb_prev_t != t;
  2779. kdb_prev_t = t;
  2780. if (t->state != TASK_RUNNING && new_t) {
  2781. kdb_printf("Process is not RUNNING, sending a signal from "
  2782. "kdb risks deadlock\n"
  2783. "on the run queue locks. "
  2784. "The signal has _not_ been sent.\n"
  2785. "Reissue the kill command if you want to risk "
  2786. "the deadlock.\n");
  2787. return;
  2788. }
  2789. sig = info->si_signo;
  2790. if (send_sig_info(sig, info, t))
  2791. kdb_printf("Fail to deliver Signal %d to process %d.\n",
  2792. sig, t->pid);
  2793. else
  2794. kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
  2795. }
  2796. #endif /* CONFIG_KGDB_KDB */