signal.c 85 KB

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