signal.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318
  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_ns == tcred->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_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. rcu_read_lock();
  911. info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
  912. make_kuid(current_user_ns(), info->si_uid));
  913. rcu_read_unlock();
  914. }
  915. #else
  916. static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
  917. {
  918. return;
  919. }
  920. #endif
  921. static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
  922. int group, int from_ancestor_ns)
  923. {
  924. struct sigpending *pending;
  925. struct sigqueue *q;
  926. int override_rlimit;
  927. int ret = 0, result;
  928. assert_spin_locked(&t->sighand->siglock);
  929. result = TRACE_SIGNAL_IGNORED;
  930. if (!prepare_signal(sig, t,
  931. from_ancestor_ns || (info == SEND_SIG_FORCED)))
  932. goto ret;
  933. pending = group ? &t->signal->shared_pending : &t->pending;
  934. /*
  935. * Short-circuit ignored signals and support queuing
  936. * exactly one non-rt signal, so that we can get more
  937. * detailed information about the cause of the signal.
  938. */
  939. result = TRACE_SIGNAL_ALREADY_PENDING;
  940. if (legacy_queue(pending, sig))
  941. goto ret;
  942. result = TRACE_SIGNAL_DELIVERED;
  943. /*
  944. * fast-pathed signals for kernel-internal things like SIGSTOP
  945. * or SIGKILL.
  946. */
  947. if (info == SEND_SIG_FORCED)
  948. goto out_set;
  949. /*
  950. * Real-time signals must be queued if sent by sigqueue, or
  951. * some other real-time mechanism. It is implementation
  952. * defined whether kill() does so. We attempt to do so, on
  953. * the principle of least surprise, but since kill is not
  954. * allowed to fail with EAGAIN when low on memory we just
  955. * make sure at least one signal gets delivered and don't
  956. * pass on the info struct.
  957. */
  958. if (sig < SIGRTMIN)
  959. override_rlimit = (is_si_special(info) || info->si_code >= 0);
  960. else
  961. override_rlimit = 0;
  962. q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
  963. override_rlimit);
  964. if (q) {
  965. list_add_tail(&q->list, &pending->list);
  966. switch ((unsigned long) info) {
  967. case (unsigned long) SEND_SIG_NOINFO:
  968. q->info.si_signo = sig;
  969. q->info.si_errno = 0;
  970. q->info.si_code = SI_USER;
  971. q->info.si_pid = task_tgid_nr_ns(current,
  972. task_active_pid_ns(t));
  973. q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  974. break;
  975. case (unsigned long) SEND_SIG_PRIV:
  976. q->info.si_signo = sig;
  977. q->info.si_errno = 0;
  978. q->info.si_code = SI_KERNEL;
  979. q->info.si_pid = 0;
  980. q->info.si_uid = 0;
  981. break;
  982. default:
  983. copy_siginfo(&q->info, info);
  984. if (from_ancestor_ns)
  985. q->info.si_pid = 0;
  986. break;
  987. }
  988. userns_fixup_signal_uid(&q->info, t);
  989. } else if (!is_si_special(info)) {
  990. if (sig >= SIGRTMIN && info->si_code != SI_USER) {
  991. /*
  992. * Queue overflow, abort. We may abort if the
  993. * signal was rt and sent by user using something
  994. * other than kill().
  995. */
  996. result = TRACE_SIGNAL_OVERFLOW_FAIL;
  997. ret = -EAGAIN;
  998. goto ret;
  999. } else {
  1000. /*
  1001. * This is a silent loss of information. We still
  1002. * send the signal, but the *info bits are lost.
  1003. */
  1004. result = TRACE_SIGNAL_LOSE_INFO;
  1005. }
  1006. }
  1007. out_set:
  1008. signalfd_notify(t, sig);
  1009. sigaddset(&pending->signal, sig);
  1010. complete_signal(sig, t, group);
  1011. ret:
  1012. trace_signal_generate(sig, info, t, group, result);
  1013. return ret;
  1014. }
  1015. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  1016. int group)
  1017. {
  1018. int from_ancestor_ns = 0;
  1019. #ifdef CONFIG_PID_NS
  1020. from_ancestor_ns = si_fromuser(info) &&
  1021. !task_pid_nr_ns(current, task_active_pid_ns(t));
  1022. #endif
  1023. return __send_signal(sig, info, t, group, from_ancestor_ns);
  1024. }
  1025. static void print_fatal_signal(struct pt_regs *regs, int signr)
  1026. {
  1027. printk("%s/%d: potentially unexpected fatal signal %d.\n",
  1028. current->comm, task_pid_nr(current), signr);
  1029. #if defined(__i386__) && !defined(__arch_um__)
  1030. printk("code at %08lx: ", regs->ip);
  1031. {
  1032. int i;
  1033. for (i = 0; i < 16; i++) {
  1034. unsigned char insn;
  1035. if (get_user(insn, (unsigned char *)(regs->ip + i)))
  1036. break;
  1037. printk("%02x ", insn);
  1038. }
  1039. }
  1040. #endif
  1041. printk("\n");
  1042. preempt_disable();
  1043. show_regs(regs);
  1044. preempt_enable();
  1045. }
  1046. static int __init setup_print_fatal_signals(char *str)
  1047. {
  1048. get_option (&str, &print_fatal_signals);
  1049. return 1;
  1050. }
  1051. __setup("print-fatal-signals=", setup_print_fatal_signals);
  1052. int
  1053. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1054. {
  1055. return send_signal(sig, info, p, 1);
  1056. }
  1057. static int
  1058. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1059. {
  1060. return send_signal(sig, info, t, 0);
  1061. }
  1062. int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
  1063. bool group)
  1064. {
  1065. unsigned long flags;
  1066. int ret = -ESRCH;
  1067. if (lock_task_sighand(p, &flags)) {
  1068. ret = send_signal(sig, info, p, group);
  1069. unlock_task_sighand(p, &flags);
  1070. }
  1071. return ret;
  1072. }
  1073. /*
  1074. * Force a signal that the process can't ignore: if necessary
  1075. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  1076. *
  1077. * Note: If we unblock the signal, we always reset it to SIG_DFL,
  1078. * since we do not want to have a signal handler that was blocked
  1079. * be invoked when user space had explicitly blocked it.
  1080. *
  1081. * We don't want to have recursive SIGSEGV's etc, for example,
  1082. * that is why we also clear SIGNAL_UNKILLABLE.
  1083. */
  1084. int
  1085. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  1086. {
  1087. unsigned long int flags;
  1088. int ret, blocked, ignored;
  1089. struct k_sigaction *action;
  1090. spin_lock_irqsave(&t->sighand->siglock, flags);
  1091. action = &t->sighand->action[sig-1];
  1092. ignored = action->sa.sa_handler == SIG_IGN;
  1093. blocked = sigismember(&t->blocked, sig);
  1094. if (blocked || ignored) {
  1095. action->sa.sa_handler = SIG_DFL;
  1096. if (blocked) {
  1097. sigdelset(&t->blocked, sig);
  1098. recalc_sigpending_and_wake(t);
  1099. }
  1100. }
  1101. if (action->sa.sa_handler == SIG_DFL)
  1102. t->signal->flags &= ~SIGNAL_UNKILLABLE;
  1103. ret = specific_send_sig_info(sig, info, t);
  1104. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  1105. return ret;
  1106. }
  1107. /*
  1108. * Nuke all other threads in the group.
  1109. */
  1110. int zap_other_threads(struct task_struct *p)
  1111. {
  1112. struct task_struct *t = p;
  1113. int count = 0;
  1114. p->signal->group_stop_count = 0;
  1115. while_each_thread(p, t) {
  1116. task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
  1117. count++;
  1118. /* Don't bother with already dead threads */
  1119. if (t->exit_state)
  1120. continue;
  1121. sigaddset(&t->pending.signal, SIGKILL);
  1122. signal_wake_up(t, 1);
  1123. }
  1124. return count;
  1125. }
  1126. struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
  1127. unsigned long *flags)
  1128. {
  1129. struct sighand_struct *sighand;
  1130. for (;;) {
  1131. local_irq_save(*flags);
  1132. rcu_read_lock();
  1133. sighand = rcu_dereference(tsk->sighand);
  1134. if (unlikely(sighand == NULL)) {
  1135. rcu_read_unlock();
  1136. local_irq_restore(*flags);
  1137. break;
  1138. }
  1139. spin_lock(&sighand->siglock);
  1140. if (likely(sighand == tsk->sighand)) {
  1141. rcu_read_unlock();
  1142. break;
  1143. }
  1144. spin_unlock(&sighand->siglock);
  1145. rcu_read_unlock();
  1146. local_irq_restore(*flags);
  1147. }
  1148. return sighand;
  1149. }
  1150. /*
  1151. * send signal info to all the members of a group
  1152. */
  1153. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1154. {
  1155. int ret;
  1156. rcu_read_lock();
  1157. ret = check_kill_permission(sig, info, p);
  1158. rcu_read_unlock();
  1159. if (!ret && sig)
  1160. ret = do_send_sig_info(sig, info, p, true);
  1161. return ret;
  1162. }
  1163. /*
  1164. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  1165. * control characters do (^C, ^Z etc)
  1166. * - the caller must hold at least a readlock on tasklist_lock
  1167. */
  1168. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  1169. {
  1170. struct task_struct *p = NULL;
  1171. int retval, success;
  1172. success = 0;
  1173. retval = -ESRCH;
  1174. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  1175. int err = group_send_sig_info(sig, info, p);
  1176. success |= !err;
  1177. retval = err;
  1178. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  1179. return success ? 0 : retval;
  1180. }
  1181. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  1182. {
  1183. int error = -ESRCH;
  1184. struct task_struct *p;
  1185. rcu_read_lock();
  1186. retry:
  1187. p = pid_task(pid, PIDTYPE_PID);
  1188. if (p) {
  1189. error = group_send_sig_info(sig, info, p);
  1190. if (unlikely(error == -ESRCH))
  1191. /*
  1192. * The task was unhashed in between, try again.
  1193. * If it is dead, pid_task() will return NULL,
  1194. * if we race with de_thread() it will find the
  1195. * new leader.
  1196. */
  1197. goto retry;
  1198. }
  1199. rcu_read_unlock();
  1200. return error;
  1201. }
  1202. int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  1203. {
  1204. int error;
  1205. rcu_read_lock();
  1206. error = kill_pid_info(sig, info, find_vpid(pid));
  1207. rcu_read_unlock();
  1208. return error;
  1209. }
  1210. static int kill_as_cred_perm(const struct cred *cred,
  1211. struct task_struct *target)
  1212. {
  1213. const struct cred *pcred = __task_cred(target);
  1214. if (cred->user_ns != pcred->user_ns)
  1215. return 0;
  1216. if (cred->euid != pcred->suid && cred->euid != pcred->uid &&
  1217. cred->uid != pcred->suid && cred->uid != pcred->uid)
  1218. return 0;
  1219. return 1;
  1220. }
  1221. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  1222. int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
  1223. const struct cred *cred, u32 secid)
  1224. {
  1225. int ret = -EINVAL;
  1226. struct task_struct *p;
  1227. unsigned long flags;
  1228. if (!valid_signal(sig))
  1229. return ret;
  1230. rcu_read_lock();
  1231. p = pid_task(pid, PIDTYPE_PID);
  1232. if (!p) {
  1233. ret = -ESRCH;
  1234. goto out_unlock;
  1235. }
  1236. if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
  1237. ret = -EPERM;
  1238. goto out_unlock;
  1239. }
  1240. ret = security_task_kill(p, info, sig, secid);
  1241. if (ret)
  1242. goto out_unlock;
  1243. if (sig) {
  1244. if (lock_task_sighand(p, &flags)) {
  1245. ret = __send_signal(sig, info, p, 1, 0);
  1246. unlock_task_sighand(p, &flags);
  1247. } else
  1248. ret = -ESRCH;
  1249. }
  1250. out_unlock:
  1251. rcu_read_unlock();
  1252. return ret;
  1253. }
  1254. EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
  1255. /*
  1256. * kill_something_info() interprets pid in interesting ways just like kill(2).
  1257. *
  1258. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  1259. * is probably wrong. Should make it like BSD or SYSV.
  1260. */
  1261. static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
  1262. {
  1263. int ret;
  1264. if (pid > 0) {
  1265. rcu_read_lock();
  1266. ret = kill_pid_info(sig, info, find_vpid(pid));
  1267. rcu_read_unlock();
  1268. return ret;
  1269. }
  1270. read_lock(&tasklist_lock);
  1271. if (pid != -1) {
  1272. ret = __kill_pgrp_info(sig, info,
  1273. pid ? find_vpid(-pid) : task_pgrp(current));
  1274. } else {
  1275. int retval = 0, count = 0;
  1276. struct task_struct * p;
  1277. for_each_process(p) {
  1278. if (task_pid_vnr(p) > 1 &&
  1279. !same_thread_group(p, current)) {
  1280. int err = group_send_sig_info(sig, info, p);
  1281. ++count;
  1282. if (err != -EPERM)
  1283. retval = err;
  1284. }
  1285. }
  1286. ret = count ? retval : -ESRCH;
  1287. }
  1288. read_unlock(&tasklist_lock);
  1289. return ret;
  1290. }
  1291. /*
  1292. * These are for backward compatibility with the rest of the kernel source.
  1293. */
  1294. int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1295. {
  1296. /*
  1297. * Make sure legacy kernel users don't send in bad values
  1298. * (normal paths check this in check_kill_permission).
  1299. */
  1300. if (!valid_signal(sig))
  1301. return -EINVAL;
  1302. return do_send_sig_info(sig, info, p, false);
  1303. }
  1304. #define __si_special(priv) \
  1305. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1306. int
  1307. send_sig(int sig, struct task_struct *p, int priv)
  1308. {
  1309. return send_sig_info(sig, __si_special(priv), p);
  1310. }
  1311. void
  1312. force_sig(int sig, struct task_struct *p)
  1313. {
  1314. force_sig_info(sig, SEND_SIG_PRIV, p);
  1315. }
  1316. /*
  1317. * When things go south during signal handling, we
  1318. * will force a SIGSEGV. And if the signal that caused
  1319. * the problem was already a SIGSEGV, we'll want to
  1320. * make sure we don't even try to deliver the signal..
  1321. */
  1322. int
  1323. force_sigsegv(int sig, struct task_struct *p)
  1324. {
  1325. if (sig == SIGSEGV) {
  1326. unsigned long flags;
  1327. spin_lock_irqsave(&p->sighand->siglock, flags);
  1328. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1329. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1330. }
  1331. force_sig(SIGSEGV, p);
  1332. return 0;
  1333. }
  1334. int kill_pgrp(struct pid *pid, int sig, int priv)
  1335. {
  1336. int ret;
  1337. read_lock(&tasklist_lock);
  1338. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1339. read_unlock(&tasklist_lock);
  1340. return ret;
  1341. }
  1342. EXPORT_SYMBOL(kill_pgrp);
  1343. int kill_pid(struct pid *pid, int sig, int priv)
  1344. {
  1345. return kill_pid_info(sig, __si_special(priv), pid);
  1346. }
  1347. EXPORT_SYMBOL(kill_pid);
  1348. /*
  1349. * These functions support sending signals using preallocated sigqueue
  1350. * structures. This is needed "because realtime applications cannot
  1351. * afford to lose notifications of asynchronous events, like timer
  1352. * expirations or I/O completions". In the case of POSIX Timers
  1353. * we allocate the sigqueue structure from the timer_create. If this
  1354. * allocation fails we are able to report the failure to the application
  1355. * with an EAGAIN error.
  1356. */
  1357. struct sigqueue *sigqueue_alloc(void)
  1358. {
  1359. struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
  1360. if (q)
  1361. q->flags |= SIGQUEUE_PREALLOC;
  1362. return q;
  1363. }
  1364. void sigqueue_free(struct sigqueue *q)
  1365. {
  1366. unsigned long flags;
  1367. spinlock_t *lock = &current->sighand->siglock;
  1368. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1369. /*
  1370. * We must hold ->siglock while testing q->list
  1371. * to serialize with collect_signal() or with
  1372. * __exit_signal()->flush_sigqueue().
  1373. */
  1374. spin_lock_irqsave(lock, flags);
  1375. q->flags &= ~SIGQUEUE_PREALLOC;
  1376. /*
  1377. * If it is queued it will be freed when dequeued,
  1378. * like the "regular" sigqueue.
  1379. */
  1380. if (!list_empty(&q->list))
  1381. q = NULL;
  1382. spin_unlock_irqrestore(lock, flags);
  1383. if (q)
  1384. __sigqueue_free(q);
  1385. }
  1386. int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
  1387. {
  1388. int sig = q->info.si_signo;
  1389. struct sigpending *pending;
  1390. unsigned long flags;
  1391. int ret, result;
  1392. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1393. ret = -1;
  1394. if (!likely(lock_task_sighand(t, &flags)))
  1395. goto ret;
  1396. ret = 1; /* the signal is ignored */
  1397. result = TRACE_SIGNAL_IGNORED;
  1398. if (!prepare_signal(sig, t, false))
  1399. goto out;
  1400. ret = 0;
  1401. if (unlikely(!list_empty(&q->list))) {
  1402. /*
  1403. * If an SI_TIMER entry is already queue just increment
  1404. * the overrun count.
  1405. */
  1406. BUG_ON(q->info.si_code != SI_TIMER);
  1407. q->info.si_overrun++;
  1408. result = TRACE_SIGNAL_ALREADY_PENDING;
  1409. goto out;
  1410. }
  1411. q->info.si_overrun = 0;
  1412. signalfd_notify(t, sig);
  1413. pending = group ? &t->signal->shared_pending : &t->pending;
  1414. list_add_tail(&q->list, &pending->list);
  1415. sigaddset(&pending->signal, sig);
  1416. complete_signal(sig, t, group);
  1417. result = TRACE_SIGNAL_DELIVERED;
  1418. out:
  1419. trace_signal_generate(sig, &q->info, t, group, result);
  1420. unlock_task_sighand(t, &flags);
  1421. ret:
  1422. return ret;
  1423. }
  1424. /*
  1425. * Let a parent know about the death of a child.
  1426. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1427. *
  1428. * Returns true if our parent ignored us and so we've switched to
  1429. * self-reaping.
  1430. */
  1431. bool do_notify_parent(struct task_struct *tsk, int sig)
  1432. {
  1433. struct siginfo info;
  1434. unsigned long flags;
  1435. struct sighand_struct *psig;
  1436. bool autoreap = false;
  1437. BUG_ON(sig == -1);
  1438. /* do_notify_parent_cldstop should have been called instead. */
  1439. BUG_ON(task_is_stopped_or_traced(tsk));
  1440. BUG_ON(!tsk->ptrace &&
  1441. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1442. if (sig != SIGCHLD) {
  1443. /*
  1444. * This is only possible if parent == real_parent.
  1445. * Check if it has changed security domain.
  1446. */
  1447. if (tsk->parent_exec_id != tsk->parent->self_exec_id)
  1448. sig = SIGCHLD;
  1449. }
  1450. info.si_signo = sig;
  1451. info.si_errno = 0;
  1452. /*
  1453. * we are under tasklist_lock here so our parent is tied to
  1454. * us and cannot exit and release its namespace.
  1455. *
  1456. * the only it can is to switch its nsproxy with sys_unshare,
  1457. * bu uncharing pid namespaces is not allowed, so we'll always
  1458. * see relevant namespace
  1459. *
  1460. * write_lock() currently calls preempt_disable() which is the
  1461. * same as rcu_read_lock(), but according to Oleg, this is not
  1462. * correct to rely on this
  1463. */
  1464. rcu_read_lock();
  1465. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1466. info.si_uid = map_cred_ns(__task_cred(tsk),
  1467. task_cred_xxx(tsk->parent, user_ns));
  1468. rcu_read_unlock();
  1469. info.si_utime = cputime_to_clock_t(tsk->utime + tsk->signal->utime);
  1470. info.si_stime = cputime_to_clock_t(tsk->stime + tsk->signal->stime);
  1471. info.si_status = tsk->exit_code & 0x7f;
  1472. if (tsk->exit_code & 0x80)
  1473. info.si_code = CLD_DUMPED;
  1474. else if (tsk->exit_code & 0x7f)
  1475. info.si_code = CLD_KILLED;
  1476. else {
  1477. info.si_code = CLD_EXITED;
  1478. info.si_status = tsk->exit_code >> 8;
  1479. }
  1480. psig = tsk->parent->sighand;
  1481. spin_lock_irqsave(&psig->siglock, flags);
  1482. if (!tsk->ptrace && sig == SIGCHLD &&
  1483. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1484. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1485. /*
  1486. * We are exiting and our parent doesn't care. POSIX.1
  1487. * defines special semantics for setting SIGCHLD to SIG_IGN
  1488. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1489. * automatically and not left for our parent's wait4 call.
  1490. * Rather than having the parent do it as a magic kind of
  1491. * signal handler, we just set this to tell do_exit that we
  1492. * can be cleaned up without becoming a zombie. Note that
  1493. * we still call __wake_up_parent in this case, because a
  1494. * blocked sys_wait4 might now return -ECHILD.
  1495. *
  1496. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1497. * is implementation-defined: we do (if you don't want
  1498. * it, just use SIG_IGN instead).
  1499. */
  1500. autoreap = true;
  1501. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1502. sig = 0;
  1503. }
  1504. if (valid_signal(sig) && sig)
  1505. __group_send_sig_info(sig, &info, tsk->parent);
  1506. __wake_up_parent(tsk, tsk->parent);
  1507. spin_unlock_irqrestore(&psig->siglock, flags);
  1508. return autoreap;
  1509. }
  1510. /**
  1511. * do_notify_parent_cldstop - notify parent of stopped/continued state change
  1512. * @tsk: task reporting the state change
  1513. * @for_ptracer: the notification is for ptracer
  1514. * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
  1515. *
  1516. * Notify @tsk's parent that the stopped/continued state has changed. If
  1517. * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
  1518. * If %true, @tsk reports to @tsk->parent which should be the ptracer.
  1519. *
  1520. * CONTEXT:
  1521. * Must be called with tasklist_lock at least read locked.
  1522. */
  1523. static void do_notify_parent_cldstop(struct task_struct *tsk,
  1524. bool for_ptracer, int why)
  1525. {
  1526. struct siginfo info;
  1527. unsigned long flags;
  1528. struct task_struct *parent;
  1529. struct sighand_struct *sighand;
  1530. if (for_ptracer) {
  1531. parent = tsk->parent;
  1532. } else {
  1533. tsk = tsk->group_leader;
  1534. parent = tsk->real_parent;
  1535. }
  1536. info.si_signo = SIGCHLD;
  1537. info.si_errno = 0;
  1538. /*
  1539. * see comment in do_notify_parent() about the following 4 lines
  1540. */
  1541. rcu_read_lock();
  1542. info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
  1543. info.si_uid = map_cred_ns(__task_cred(tsk),
  1544. task_cred_xxx(parent, user_ns));
  1545. rcu_read_unlock();
  1546. info.si_utime = cputime_to_clock_t(tsk->utime);
  1547. info.si_stime = cputime_to_clock_t(tsk->stime);
  1548. info.si_code = why;
  1549. switch (why) {
  1550. case CLD_CONTINUED:
  1551. info.si_status = SIGCONT;
  1552. break;
  1553. case CLD_STOPPED:
  1554. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1555. break;
  1556. case CLD_TRAPPED:
  1557. info.si_status = tsk->exit_code & 0x7f;
  1558. break;
  1559. default:
  1560. BUG();
  1561. }
  1562. sighand = parent->sighand;
  1563. spin_lock_irqsave(&sighand->siglock, flags);
  1564. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1565. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1566. __group_send_sig_info(SIGCHLD, &info, parent);
  1567. /*
  1568. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1569. */
  1570. __wake_up_parent(tsk, parent);
  1571. spin_unlock_irqrestore(&sighand->siglock, flags);
  1572. }
  1573. static inline int may_ptrace_stop(void)
  1574. {
  1575. if (!likely(current->ptrace))
  1576. return 0;
  1577. /*
  1578. * Are we in the middle of do_coredump?
  1579. * If so and our tracer is also part of the coredump stopping
  1580. * is a deadlock situation, and pointless because our tracer
  1581. * is dead so don't allow us to stop.
  1582. * If SIGKILL was already sent before the caller unlocked
  1583. * ->siglock we must see ->core_state != NULL. Otherwise it
  1584. * is safe to enter schedule().
  1585. */
  1586. if (unlikely(current->mm->core_state) &&
  1587. unlikely(current->mm == current->parent->mm))
  1588. return 0;
  1589. return 1;
  1590. }
  1591. /*
  1592. * Return non-zero if there is a SIGKILL that should be waking us up.
  1593. * Called with the siglock held.
  1594. */
  1595. static int sigkill_pending(struct task_struct *tsk)
  1596. {
  1597. return sigismember(&tsk->pending.signal, SIGKILL) ||
  1598. sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
  1599. }
  1600. /*
  1601. * This must be called with current->sighand->siglock held.
  1602. *
  1603. * This should be the path for all ptrace stops.
  1604. * We always set current->last_siginfo while stopped here.
  1605. * That makes it a way to test a stopped process for
  1606. * being ptrace-stopped vs being job-control-stopped.
  1607. *
  1608. * If we actually decide not to stop at all because the tracer
  1609. * is gone, we keep current->exit_code unless clear_code.
  1610. */
  1611. static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
  1612. __releases(&current->sighand->siglock)
  1613. __acquires(&current->sighand->siglock)
  1614. {
  1615. bool gstop_done = false;
  1616. if (arch_ptrace_stop_needed(exit_code, info)) {
  1617. /*
  1618. * The arch code has something special to do before a
  1619. * ptrace stop. This is allowed to block, e.g. for faults
  1620. * on user stack pages. We can't keep the siglock while
  1621. * calling arch_ptrace_stop, so we must release it now.
  1622. * To preserve proper semantics, we must do this before
  1623. * any signal bookkeeping like checking group_stop_count.
  1624. * Meanwhile, a SIGKILL could come in before we retake the
  1625. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1626. * So after regaining the lock, we must check for SIGKILL.
  1627. */
  1628. spin_unlock_irq(&current->sighand->siglock);
  1629. arch_ptrace_stop(exit_code, info);
  1630. spin_lock_irq(&current->sighand->siglock);
  1631. if (sigkill_pending(current))
  1632. return;
  1633. }
  1634. /*
  1635. * We're committing to trapping. TRACED should be visible before
  1636. * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
  1637. * Also, transition to TRACED and updates to ->jobctl should be
  1638. * atomic with respect to siglock and should be done after the arch
  1639. * hook as siglock is released and regrabbed across it.
  1640. */
  1641. set_current_state(TASK_TRACED);
  1642. current->last_siginfo = info;
  1643. current->exit_code = exit_code;
  1644. /*
  1645. * If @why is CLD_STOPPED, we're trapping to participate in a group
  1646. * stop. Do the bookkeeping. Note that if SIGCONT was delievered
  1647. * across siglock relocks since INTERRUPT was scheduled, PENDING
  1648. * could be clear now. We act as if SIGCONT is received after
  1649. * TASK_TRACED is entered - ignore it.
  1650. */
  1651. if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
  1652. gstop_done = task_participate_group_stop(current);
  1653. /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
  1654. task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1655. if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
  1656. task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
  1657. /* entering a trap, clear TRAPPING */
  1658. task_clear_jobctl_trapping(current);
  1659. spin_unlock_irq(&current->sighand->siglock);
  1660. read_lock(&tasklist_lock);
  1661. if (may_ptrace_stop()) {
  1662. /*
  1663. * Notify parents of the stop.
  1664. *
  1665. * While ptraced, there are two parents - the ptracer and
  1666. * the real_parent of the group_leader. The ptracer should
  1667. * know about every stop while the real parent is only
  1668. * interested in the completion of group stop. The states
  1669. * for the two don't interact with each other. Notify
  1670. * separately unless they're gonna be duplicates.
  1671. */
  1672. do_notify_parent_cldstop(current, true, why);
  1673. if (gstop_done && ptrace_reparented(current))
  1674. do_notify_parent_cldstop(current, false, why);
  1675. /*
  1676. * Don't want to allow preemption here, because
  1677. * sys_ptrace() needs this task to be inactive.
  1678. *
  1679. * XXX: implement read_unlock_no_resched().
  1680. */
  1681. preempt_disable();
  1682. read_unlock(&tasklist_lock);
  1683. preempt_enable_no_resched();
  1684. schedule();
  1685. } else {
  1686. /*
  1687. * By the time we got the lock, our tracer went away.
  1688. * Don't drop the lock yet, another tracer may come.
  1689. *
  1690. * If @gstop_done, the ptracer went away between group stop
  1691. * completion and here. During detach, it would have set
  1692. * JOBCTL_STOP_PENDING on us and we'll re-enter
  1693. * TASK_STOPPED in do_signal_stop() on return, so notifying
  1694. * the real parent of the group stop completion is enough.
  1695. */
  1696. if (gstop_done)
  1697. do_notify_parent_cldstop(current, false, why);
  1698. __set_current_state(TASK_RUNNING);
  1699. if (clear_code)
  1700. current->exit_code = 0;
  1701. read_unlock(&tasklist_lock);
  1702. }
  1703. /*
  1704. * While in TASK_TRACED, we were considered "frozen enough".
  1705. * Now that we woke up, it's crucial if we're supposed to be
  1706. * frozen that we freeze now before running anything substantial.
  1707. */
  1708. try_to_freeze();
  1709. /*
  1710. * We are back. Now reacquire the siglock before touching
  1711. * last_siginfo, so that we are sure to have synchronized with
  1712. * any signal-sending on another CPU that wants to examine it.
  1713. */
  1714. spin_lock_irq(&current->sighand->siglock);
  1715. current->last_siginfo = NULL;
  1716. /* LISTENING can be set only during STOP traps, clear it */
  1717. current->jobctl &= ~JOBCTL_LISTENING;
  1718. /*
  1719. * Queued signals ignored us while we were stopped for tracing.
  1720. * So check for any that we should take before resuming user mode.
  1721. * This sets TIF_SIGPENDING, but never clears it.
  1722. */
  1723. recalc_sigpending_tsk(current);
  1724. }
  1725. static void ptrace_do_notify(int signr, int exit_code, int why)
  1726. {
  1727. siginfo_t info;
  1728. memset(&info, 0, sizeof info);
  1729. info.si_signo = signr;
  1730. info.si_code = exit_code;
  1731. info.si_pid = task_pid_vnr(current);
  1732. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  1733. /* Let the debugger run. */
  1734. ptrace_stop(exit_code, why, 1, &info);
  1735. }
  1736. void ptrace_notify(int exit_code)
  1737. {
  1738. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1739. spin_lock_irq(&current->sighand->siglock);
  1740. ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
  1741. spin_unlock_irq(&current->sighand->siglock);
  1742. }
  1743. /**
  1744. * do_signal_stop - handle group stop for SIGSTOP and other stop signals
  1745. * @signr: signr causing group stop if initiating
  1746. *
  1747. * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
  1748. * and participate in it. If already set, participate in the existing
  1749. * group stop. If participated in a group stop (and thus slept), %true is
  1750. * returned with siglock released.
  1751. *
  1752. * If ptraced, this function doesn't handle stop itself. Instead,
  1753. * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
  1754. * untouched. The caller must ensure that INTERRUPT trap handling takes
  1755. * places afterwards.
  1756. *
  1757. * CONTEXT:
  1758. * Must be called with @current->sighand->siglock held, which is released
  1759. * on %true return.
  1760. *
  1761. * RETURNS:
  1762. * %false if group stop is already cancelled or ptrace trap is scheduled.
  1763. * %true if participated in group stop.
  1764. */
  1765. static bool do_signal_stop(int signr)
  1766. __releases(&current->sighand->siglock)
  1767. {
  1768. struct signal_struct *sig = current->signal;
  1769. if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
  1770. unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
  1771. struct task_struct *t;
  1772. /* signr will be recorded in task->jobctl for retries */
  1773. WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
  1774. if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
  1775. unlikely(signal_group_exit(sig)))
  1776. return false;
  1777. /*
  1778. * There is no group stop already in progress. We must
  1779. * initiate one now.
  1780. *
  1781. * While ptraced, a task may be resumed while group stop is
  1782. * still in effect and then receive a stop signal and
  1783. * initiate another group stop. This deviates from the
  1784. * usual behavior as two consecutive stop signals can't
  1785. * cause two group stops when !ptraced. That is why we
  1786. * also check !task_is_stopped(t) below.
  1787. *
  1788. * The condition can be distinguished by testing whether
  1789. * SIGNAL_STOP_STOPPED is already set. Don't generate
  1790. * group_exit_code in such case.
  1791. *
  1792. * This is not necessary for SIGNAL_STOP_CONTINUED because
  1793. * an intervening stop signal is required to cause two
  1794. * continued events regardless of ptrace.
  1795. */
  1796. if (!(sig->flags & SIGNAL_STOP_STOPPED))
  1797. sig->group_exit_code = signr;
  1798. sig->group_stop_count = 0;
  1799. if (task_set_jobctl_pending(current, signr | gstop))
  1800. sig->group_stop_count++;
  1801. for (t = next_thread(current); t != current;
  1802. t = next_thread(t)) {
  1803. /*
  1804. * Setting state to TASK_STOPPED for a group
  1805. * stop is always done with the siglock held,
  1806. * so this check has no races.
  1807. */
  1808. if (!task_is_stopped(t) &&
  1809. task_set_jobctl_pending(t, signr | gstop)) {
  1810. sig->group_stop_count++;
  1811. if (likely(!(t->ptrace & PT_SEIZED)))
  1812. signal_wake_up(t, 0);
  1813. else
  1814. ptrace_trap_notify(t);
  1815. }
  1816. }
  1817. }
  1818. if (likely(!current->ptrace)) {
  1819. int notify = 0;
  1820. /*
  1821. * If there are no other threads in the group, or if there
  1822. * is a group stop in progress and we are the last to stop,
  1823. * report to the parent.
  1824. */
  1825. if (task_participate_group_stop(current))
  1826. notify = CLD_STOPPED;
  1827. __set_current_state(TASK_STOPPED);
  1828. spin_unlock_irq(&current->sighand->siglock);
  1829. /*
  1830. * Notify the parent of the group stop completion. Because
  1831. * we're not holding either the siglock or tasklist_lock
  1832. * here, ptracer may attach inbetween; however, this is for
  1833. * group stop and should always be delivered to the real
  1834. * parent of the group leader. The new ptracer will get
  1835. * its notification when this task transitions into
  1836. * TASK_TRACED.
  1837. */
  1838. if (notify) {
  1839. read_lock(&tasklist_lock);
  1840. do_notify_parent_cldstop(current, false, notify);
  1841. read_unlock(&tasklist_lock);
  1842. }
  1843. /* Now we don't run again until woken by SIGCONT or SIGKILL */
  1844. schedule();
  1845. return true;
  1846. } else {
  1847. /*
  1848. * While ptraced, group stop is handled by STOP trap.
  1849. * Schedule it and let the caller deal with it.
  1850. */
  1851. task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
  1852. return false;
  1853. }
  1854. }
  1855. /**
  1856. * do_jobctl_trap - take care of ptrace jobctl traps
  1857. *
  1858. * When PT_SEIZED, it's used for both group stop and explicit
  1859. * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
  1860. * accompanying siginfo. If stopped, lower eight bits of exit_code contain
  1861. * the stop signal; otherwise, %SIGTRAP.
  1862. *
  1863. * When !PT_SEIZED, it's used only for group stop trap with stop signal
  1864. * number as exit_code and no siginfo.
  1865. *
  1866. * CONTEXT:
  1867. * Must be called with @current->sighand->siglock held, which may be
  1868. * released and re-acquired before returning with intervening sleep.
  1869. */
  1870. static void do_jobctl_trap(void)
  1871. {
  1872. struct signal_struct *signal = current->signal;
  1873. int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
  1874. if (current->ptrace & PT_SEIZED) {
  1875. if (!signal->group_stop_count &&
  1876. !(signal->flags & SIGNAL_STOP_STOPPED))
  1877. signr = SIGTRAP;
  1878. WARN_ON_ONCE(!signr);
  1879. ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
  1880. CLD_STOPPED);
  1881. } else {
  1882. WARN_ON_ONCE(!signr);
  1883. ptrace_stop(signr, CLD_STOPPED, 0, NULL);
  1884. current->exit_code = 0;
  1885. }
  1886. }
  1887. static int ptrace_signal(int signr, siginfo_t *info,
  1888. struct pt_regs *regs, void *cookie)
  1889. {
  1890. ptrace_signal_deliver(regs, cookie);
  1891. /*
  1892. * We do not check sig_kernel_stop(signr) but set this marker
  1893. * unconditionally because we do not know whether debugger will
  1894. * change signr. This flag has no meaning unless we are going
  1895. * to stop after return from ptrace_stop(). In this case it will
  1896. * be checked in do_signal_stop(), we should only stop if it was
  1897. * not cleared by SIGCONT while we were sleeping. See also the
  1898. * comment in dequeue_signal().
  1899. */
  1900. current->jobctl |= JOBCTL_STOP_DEQUEUED;
  1901. ptrace_stop(signr, CLD_TRAPPED, 0, info);
  1902. /* We're back. Did the debugger cancel the sig? */
  1903. signr = current->exit_code;
  1904. if (signr == 0)
  1905. return signr;
  1906. current->exit_code = 0;
  1907. /*
  1908. * Update the siginfo structure if the signal has
  1909. * changed. If the debugger wanted something
  1910. * specific in the siginfo structure then it should
  1911. * have updated *info via PTRACE_SETSIGINFO.
  1912. */
  1913. if (signr != info->si_signo) {
  1914. info->si_signo = signr;
  1915. info->si_errno = 0;
  1916. info->si_code = SI_USER;
  1917. rcu_read_lock();
  1918. info->si_pid = task_pid_vnr(current->parent);
  1919. info->si_uid = map_cred_ns(__task_cred(current->parent),
  1920. current_user_ns());
  1921. rcu_read_unlock();
  1922. }
  1923. /* If the (new) signal is now blocked, requeue it. */
  1924. if (sigismember(&current->blocked, signr)) {
  1925. specific_send_sig_info(signr, info, current);
  1926. signr = 0;
  1927. }
  1928. return signr;
  1929. }
  1930. int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
  1931. struct pt_regs *regs, void *cookie)
  1932. {
  1933. struct sighand_struct *sighand = current->sighand;
  1934. struct signal_struct *signal = current->signal;
  1935. int signr;
  1936. relock:
  1937. /*
  1938. * We'll jump back here after any time we were stopped in TASK_STOPPED.
  1939. * While in TASK_STOPPED, we were considered "frozen enough".
  1940. * Now that we woke up, it's crucial if we're supposed to be
  1941. * frozen that we freeze now before running anything substantial.
  1942. */
  1943. try_to_freeze();
  1944. spin_lock_irq(&sighand->siglock);
  1945. /*
  1946. * Every stopped thread goes here after wakeup. Check to see if
  1947. * we should notify the parent, prepare_signal(SIGCONT) encodes
  1948. * the CLD_ si_code into SIGNAL_CLD_MASK bits.
  1949. */
  1950. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  1951. int why;
  1952. if (signal->flags & SIGNAL_CLD_CONTINUED)
  1953. why = CLD_CONTINUED;
  1954. else
  1955. why = CLD_STOPPED;
  1956. signal->flags &= ~SIGNAL_CLD_MASK;
  1957. spin_unlock_irq(&sighand->siglock);
  1958. /*
  1959. * Notify the parent that we're continuing. This event is
  1960. * always per-process and doesn't make whole lot of sense
  1961. * for ptracers, who shouldn't consume the state via
  1962. * wait(2) either, but, for backward compatibility, notify
  1963. * the ptracer of the group leader too unless it's gonna be
  1964. * a duplicate.
  1965. */
  1966. read_lock(&tasklist_lock);
  1967. do_notify_parent_cldstop(current, false, why);
  1968. if (ptrace_reparented(current->group_leader))
  1969. do_notify_parent_cldstop(current->group_leader,
  1970. true, why);
  1971. read_unlock(&tasklist_lock);
  1972. goto relock;
  1973. }
  1974. for (;;) {
  1975. struct k_sigaction *ka;
  1976. if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
  1977. do_signal_stop(0))
  1978. goto relock;
  1979. if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
  1980. do_jobctl_trap();
  1981. spin_unlock_irq(&sighand->siglock);
  1982. goto relock;
  1983. }
  1984. signr = dequeue_signal(current, &current->blocked, info);
  1985. if (!signr)
  1986. break; /* will return 0 */
  1987. if (unlikely(current->ptrace) && signr != SIGKILL) {
  1988. signr = ptrace_signal(signr, info,
  1989. regs, cookie);
  1990. if (!signr)
  1991. continue;
  1992. }
  1993. ka = &sighand->action[signr-1];
  1994. /* Trace actually delivered signals. */
  1995. trace_signal_deliver(signr, info, ka);
  1996. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1997. continue;
  1998. if (ka->sa.sa_handler != SIG_DFL) {
  1999. /* Run the handler. */
  2000. *return_ka = *ka;
  2001. if (ka->sa.sa_flags & SA_ONESHOT)
  2002. ka->sa.sa_handler = SIG_DFL;
  2003. break; /* will return non-zero "signr" value */
  2004. }
  2005. /*
  2006. * Now we are doing the default action for this signal.
  2007. */
  2008. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  2009. continue;
  2010. /*
  2011. * Global init gets no signals it doesn't want.
  2012. * Container-init gets no signals it doesn't want from same
  2013. * container.
  2014. *
  2015. * Note that if global/container-init sees a sig_kernel_only()
  2016. * signal here, the signal must have been generated internally
  2017. * or must have come from an ancestor namespace. In either
  2018. * case, the signal cannot be dropped.
  2019. */
  2020. if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
  2021. !sig_kernel_only(signr))
  2022. continue;
  2023. if (sig_kernel_stop(signr)) {
  2024. /*
  2025. * The default action is to stop all threads in
  2026. * the thread group. The job control signals
  2027. * do nothing in an orphaned pgrp, but SIGSTOP
  2028. * always works. Note that siglock needs to be
  2029. * dropped during the call to is_orphaned_pgrp()
  2030. * because of lock ordering with tasklist_lock.
  2031. * This allows an intervening SIGCONT to be posted.
  2032. * We need to check for that and bail out if necessary.
  2033. */
  2034. if (signr != SIGSTOP) {
  2035. spin_unlock_irq(&sighand->siglock);
  2036. /* signals can be posted during this window */
  2037. if (is_current_pgrp_orphaned())
  2038. goto relock;
  2039. spin_lock_irq(&sighand->siglock);
  2040. }
  2041. if (likely(do_signal_stop(info->si_signo))) {
  2042. /* It released the siglock. */
  2043. goto relock;
  2044. }
  2045. /*
  2046. * We didn't actually stop, due to a race
  2047. * with SIGCONT or something like that.
  2048. */
  2049. continue;
  2050. }
  2051. spin_unlock_irq(&sighand->siglock);
  2052. /*
  2053. * Anything else is fatal, maybe with a core dump.
  2054. */
  2055. current->flags |= PF_SIGNALED;
  2056. if (sig_kernel_coredump(signr)) {
  2057. if (print_fatal_signals)
  2058. print_fatal_signal(regs, info->si_signo);
  2059. /*
  2060. * If it was able to dump core, this kills all
  2061. * other threads in the group and synchronizes with
  2062. * their demise. If we lost the race with another
  2063. * thread getting here, it set group_exit_code
  2064. * first and our do_group_exit call below will use
  2065. * that value and ignore the one we pass it.
  2066. */
  2067. do_coredump(info->si_signo, info->si_signo, regs);
  2068. }
  2069. /*
  2070. * Death signals, no core dump.
  2071. */
  2072. do_group_exit(info->si_signo);
  2073. /* NOTREACHED */
  2074. }
  2075. spin_unlock_irq(&sighand->siglock);
  2076. return signr;
  2077. }
  2078. /**
  2079. * block_sigmask - add @ka's signal mask to current->blocked
  2080. * @ka: action for @signr
  2081. * @signr: signal that has been successfully delivered
  2082. *
  2083. * This function should be called when a signal has succesfully been
  2084. * delivered. It adds the mask of signals for @ka to current->blocked
  2085. * so that they are blocked during the execution of the signal
  2086. * handler. In addition, @signr will be blocked unless %SA_NODEFER is
  2087. * set in @ka->sa.sa_flags.
  2088. */
  2089. void block_sigmask(struct k_sigaction *ka, int signr)
  2090. {
  2091. sigset_t blocked;
  2092. sigorsets(&blocked, &current->blocked, &ka->sa.sa_mask);
  2093. if (!(ka->sa.sa_flags & SA_NODEFER))
  2094. sigaddset(&blocked, signr);
  2095. set_current_blocked(&blocked);
  2096. }
  2097. /*
  2098. * It could be that complete_signal() picked us to notify about the
  2099. * group-wide signal. Other threads should be notified now to take
  2100. * the shared signals in @which since we will not.
  2101. */
  2102. static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
  2103. {
  2104. sigset_t retarget;
  2105. struct task_struct *t;
  2106. sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
  2107. if (sigisemptyset(&retarget))
  2108. return;
  2109. t = tsk;
  2110. while_each_thread(tsk, t) {
  2111. if (t->flags & PF_EXITING)
  2112. continue;
  2113. if (!has_pending_signals(&retarget, &t->blocked))
  2114. continue;
  2115. /* Remove the signals this thread can handle. */
  2116. sigandsets(&retarget, &retarget, &t->blocked);
  2117. if (!signal_pending(t))
  2118. signal_wake_up(t, 0);
  2119. if (sigisemptyset(&retarget))
  2120. break;
  2121. }
  2122. }
  2123. void exit_signals(struct task_struct *tsk)
  2124. {
  2125. int group_stop = 0;
  2126. sigset_t unblocked;
  2127. /*
  2128. * @tsk is about to have PF_EXITING set - lock out users which
  2129. * expect stable threadgroup.
  2130. */
  2131. threadgroup_change_begin(tsk);
  2132. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  2133. tsk->flags |= PF_EXITING;
  2134. threadgroup_change_end(tsk);
  2135. return;
  2136. }
  2137. spin_lock_irq(&tsk->sighand->siglock);
  2138. /*
  2139. * From now this task is not visible for group-wide signals,
  2140. * see wants_signal(), do_signal_stop().
  2141. */
  2142. tsk->flags |= PF_EXITING;
  2143. threadgroup_change_end(tsk);
  2144. if (!signal_pending(tsk))
  2145. goto out;
  2146. unblocked = tsk->blocked;
  2147. signotset(&unblocked);
  2148. retarget_shared_pending(tsk, &unblocked);
  2149. if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
  2150. task_participate_group_stop(tsk))
  2151. group_stop = CLD_STOPPED;
  2152. out:
  2153. spin_unlock_irq(&tsk->sighand->siglock);
  2154. /*
  2155. * If group stop has completed, deliver the notification. This
  2156. * should always go to the real parent of the group leader.
  2157. */
  2158. if (unlikely(group_stop)) {
  2159. read_lock(&tasklist_lock);
  2160. do_notify_parent_cldstop(tsk, false, group_stop);
  2161. read_unlock(&tasklist_lock);
  2162. }
  2163. }
  2164. EXPORT_SYMBOL(recalc_sigpending);
  2165. EXPORT_SYMBOL_GPL(dequeue_signal);
  2166. EXPORT_SYMBOL(flush_signals);
  2167. EXPORT_SYMBOL(force_sig);
  2168. EXPORT_SYMBOL(send_sig);
  2169. EXPORT_SYMBOL(send_sig_info);
  2170. EXPORT_SYMBOL(sigprocmask);
  2171. EXPORT_SYMBOL(block_all_signals);
  2172. EXPORT_SYMBOL(unblock_all_signals);
  2173. /*
  2174. * System call entry points.
  2175. */
  2176. /**
  2177. * sys_restart_syscall - restart a system call
  2178. */
  2179. SYSCALL_DEFINE0(restart_syscall)
  2180. {
  2181. struct restart_block *restart = &current_thread_info()->restart_block;
  2182. return restart->fn(restart);
  2183. }
  2184. long do_no_restart_syscall(struct restart_block *param)
  2185. {
  2186. return -EINTR;
  2187. }
  2188. static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
  2189. {
  2190. if (signal_pending(tsk) && !thread_group_empty(tsk)) {
  2191. sigset_t newblocked;
  2192. /* A set of now blocked but previously unblocked signals. */
  2193. sigandnsets(&newblocked, newset, &current->blocked);
  2194. retarget_shared_pending(tsk, &newblocked);
  2195. }
  2196. tsk->blocked = *newset;
  2197. recalc_sigpending();
  2198. }
  2199. /**
  2200. * set_current_blocked - change current->blocked mask
  2201. * @newset: new mask
  2202. *
  2203. * It is wrong to change ->blocked directly, this helper should be used
  2204. * to ensure the process can't miss a shared signal we are going to block.
  2205. */
  2206. void set_current_blocked(const sigset_t *newset)
  2207. {
  2208. struct task_struct *tsk = current;
  2209. spin_lock_irq(&tsk->sighand->siglock);
  2210. __set_task_blocked(tsk, newset);
  2211. spin_unlock_irq(&tsk->sighand->siglock);
  2212. }
  2213. /*
  2214. * This is also useful for kernel threads that want to temporarily
  2215. * (or permanently) block certain signals.
  2216. *
  2217. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  2218. * interface happily blocks "unblockable" signals like SIGKILL
  2219. * and friends.
  2220. */
  2221. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  2222. {
  2223. struct task_struct *tsk = current;
  2224. sigset_t newset;
  2225. /* Lockless, only current can change ->blocked, never from irq */
  2226. if (oldset)
  2227. *oldset = tsk->blocked;
  2228. switch (how) {
  2229. case SIG_BLOCK:
  2230. sigorsets(&newset, &tsk->blocked, set);
  2231. break;
  2232. case SIG_UNBLOCK:
  2233. sigandnsets(&newset, &tsk->blocked, set);
  2234. break;
  2235. case SIG_SETMASK:
  2236. newset = *set;
  2237. break;
  2238. default:
  2239. return -EINVAL;
  2240. }
  2241. set_current_blocked(&newset);
  2242. return 0;
  2243. }
  2244. /**
  2245. * sys_rt_sigprocmask - change the list of currently blocked signals
  2246. * @how: whether to add, remove, or set signals
  2247. * @nset: stores pending signals
  2248. * @oset: previous value of signal mask if non-null
  2249. * @sigsetsize: size of sigset_t type
  2250. */
  2251. SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
  2252. sigset_t __user *, oset, size_t, sigsetsize)
  2253. {
  2254. sigset_t old_set, new_set;
  2255. int error;
  2256. /* XXX: Don't preclude handling different sized sigset_t's. */
  2257. if (sigsetsize != sizeof(sigset_t))
  2258. return -EINVAL;
  2259. old_set = current->blocked;
  2260. if (nset) {
  2261. if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
  2262. return -EFAULT;
  2263. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2264. error = sigprocmask(how, &new_set, NULL);
  2265. if (error)
  2266. return error;
  2267. }
  2268. if (oset) {
  2269. if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
  2270. return -EFAULT;
  2271. }
  2272. return 0;
  2273. }
  2274. long do_sigpending(void __user *set, unsigned long sigsetsize)
  2275. {
  2276. long error = -EINVAL;
  2277. sigset_t pending;
  2278. if (sigsetsize > sizeof(sigset_t))
  2279. goto out;
  2280. spin_lock_irq(&current->sighand->siglock);
  2281. sigorsets(&pending, &current->pending.signal,
  2282. &current->signal->shared_pending.signal);
  2283. spin_unlock_irq(&current->sighand->siglock);
  2284. /* Outside the lock because only this thread touches it. */
  2285. sigandsets(&pending, &current->blocked, &pending);
  2286. error = -EFAULT;
  2287. if (!copy_to_user(set, &pending, sigsetsize))
  2288. error = 0;
  2289. out:
  2290. return error;
  2291. }
  2292. /**
  2293. * sys_rt_sigpending - examine a pending signal that has been raised
  2294. * while blocked
  2295. * @set: stores pending signals
  2296. * @sigsetsize: size of sigset_t type or larger
  2297. */
  2298. SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
  2299. {
  2300. return do_sigpending(set, sigsetsize);
  2301. }
  2302. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  2303. int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
  2304. {
  2305. int err;
  2306. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  2307. return -EFAULT;
  2308. if (from->si_code < 0)
  2309. return __copy_to_user(to, from, sizeof(siginfo_t))
  2310. ? -EFAULT : 0;
  2311. /*
  2312. * If you change siginfo_t structure, please be sure
  2313. * this code is fixed accordingly.
  2314. * Please remember to update the signalfd_copyinfo() function
  2315. * inside fs/signalfd.c too, in case siginfo_t changes.
  2316. * It should never copy any pad contained in the structure
  2317. * to avoid security leaks, but must copy the generic
  2318. * 3 ints plus the relevant union member.
  2319. */
  2320. err = __put_user(from->si_signo, &to->si_signo);
  2321. err |= __put_user(from->si_errno, &to->si_errno);
  2322. err |= __put_user((short)from->si_code, &to->si_code);
  2323. switch (from->si_code & __SI_MASK) {
  2324. case __SI_KILL:
  2325. err |= __put_user(from->si_pid, &to->si_pid);
  2326. err |= __put_user(from->si_uid, &to->si_uid);
  2327. break;
  2328. case __SI_TIMER:
  2329. err |= __put_user(from->si_tid, &to->si_tid);
  2330. err |= __put_user(from->si_overrun, &to->si_overrun);
  2331. err |= __put_user(from->si_ptr, &to->si_ptr);
  2332. break;
  2333. case __SI_POLL:
  2334. err |= __put_user(from->si_band, &to->si_band);
  2335. err |= __put_user(from->si_fd, &to->si_fd);
  2336. break;
  2337. case __SI_FAULT:
  2338. err |= __put_user(from->si_addr, &to->si_addr);
  2339. #ifdef __ARCH_SI_TRAPNO
  2340. err |= __put_user(from->si_trapno, &to->si_trapno);
  2341. #endif
  2342. #ifdef BUS_MCEERR_AO
  2343. /*
  2344. * Other callers might not initialize the si_lsb field,
  2345. * so check explicitly for the right codes here.
  2346. */
  2347. if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
  2348. err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
  2349. #endif
  2350. break;
  2351. case __SI_CHLD:
  2352. err |= __put_user(from->si_pid, &to->si_pid);
  2353. err |= __put_user(from->si_uid, &to->si_uid);
  2354. err |= __put_user(from->si_status, &to->si_status);
  2355. err |= __put_user(from->si_utime, &to->si_utime);
  2356. err |= __put_user(from->si_stime, &to->si_stime);
  2357. break;
  2358. case __SI_RT: /* This is not generated by the kernel as of now. */
  2359. case __SI_MESGQ: /* But this is */
  2360. err |= __put_user(from->si_pid, &to->si_pid);
  2361. err |= __put_user(from->si_uid, &to->si_uid);
  2362. err |= __put_user(from->si_ptr, &to->si_ptr);
  2363. break;
  2364. default: /* this is just in case for now ... */
  2365. err |= __put_user(from->si_pid, &to->si_pid);
  2366. err |= __put_user(from->si_uid, &to->si_uid);
  2367. break;
  2368. }
  2369. return err;
  2370. }
  2371. #endif
  2372. /**
  2373. * do_sigtimedwait - wait for queued signals specified in @which
  2374. * @which: queued signals to wait for
  2375. * @info: if non-null, the signal's siginfo is returned here
  2376. * @ts: upper bound on process time suspension
  2377. */
  2378. int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
  2379. const struct timespec *ts)
  2380. {
  2381. struct task_struct *tsk = current;
  2382. long timeout = MAX_SCHEDULE_TIMEOUT;
  2383. sigset_t mask = *which;
  2384. int sig;
  2385. if (ts) {
  2386. if (!timespec_valid(ts))
  2387. return -EINVAL;
  2388. timeout = timespec_to_jiffies(ts);
  2389. /*
  2390. * We can be close to the next tick, add another one
  2391. * to ensure we will wait at least the time asked for.
  2392. */
  2393. if (ts->tv_sec || ts->tv_nsec)
  2394. timeout++;
  2395. }
  2396. /*
  2397. * Invert the set of allowed signals to get those we want to block.
  2398. */
  2399. sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
  2400. signotset(&mask);
  2401. spin_lock_irq(&tsk->sighand->siglock);
  2402. sig = dequeue_signal(tsk, &mask, info);
  2403. if (!sig && timeout) {
  2404. /*
  2405. * None ready, temporarily unblock those we're interested
  2406. * while we are sleeping in so that we'll be awakened when
  2407. * they arrive. Unblocking is always fine, we can avoid
  2408. * set_current_blocked().
  2409. */
  2410. tsk->real_blocked = tsk->blocked;
  2411. sigandsets(&tsk->blocked, &tsk->blocked, &mask);
  2412. recalc_sigpending();
  2413. spin_unlock_irq(&tsk->sighand->siglock);
  2414. timeout = schedule_timeout_interruptible(timeout);
  2415. spin_lock_irq(&tsk->sighand->siglock);
  2416. __set_task_blocked(tsk, &tsk->real_blocked);
  2417. siginitset(&tsk->real_blocked, 0);
  2418. sig = dequeue_signal(tsk, &mask, info);
  2419. }
  2420. spin_unlock_irq(&tsk->sighand->siglock);
  2421. if (sig)
  2422. return sig;
  2423. return timeout ? -EINTR : -EAGAIN;
  2424. }
  2425. /**
  2426. * sys_rt_sigtimedwait - synchronously wait for queued signals specified
  2427. * in @uthese
  2428. * @uthese: queued signals to wait for
  2429. * @uinfo: if non-null, the signal's siginfo is returned here
  2430. * @uts: upper bound on process time suspension
  2431. * @sigsetsize: size of sigset_t type
  2432. */
  2433. SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
  2434. siginfo_t __user *, uinfo, const struct timespec __user *, uts,
  2435. size_t, sigsetsize)
  2436. {
  2437. sigset_t these;
  2438. struct timespec ts;
  2439. siginfo_t info;
  2440. int ret;
  2441. /* XXX: Don't preclude handling different sized sigset_t's. */
  2442. if (sigsetsize != sizeof(sigset_t))
  2443. return -EINVAL;
  2444. if (copy_from_user(&these, uthese, sizeof(these)))
  2445. return -EFAULT;
  2446. if (uts) {
  2447. if (copy_from_user(&ts, uts, sizeof(ts)))
  2448. return -EFAULT;
  2449. }
  2450. ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
  2451. if (ret > 0 && uinfo) {
  2452. if (copy_siginfo_to_user(uinfo, &info))
  2453. ret = -EFAULT;
  2454. }
  2455. return ret;
  2456. }
  2457. /**
  2458. * sys_kill - send a signal to a process
  2459. * @pid: the PID of the process
  2460. * @sig: signal to be sent
  2461. */
  2462. SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
  2463. {
  2464. struct siginfo info;
  2465. info.si_signo = sig;
  2466. info.si_errno = 0;
  2467. info.si_code = SI_USER;
  2468. info.si_pid = task_tgid_vnr(current);
  2469. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  2470. return kill_something_info(sig, &info, pid);
  2471. }
  2472. static int
  2473. do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
  2474. {
  2475. struct task_struct *p;
  2476. int error = -ESRCH;
  2477. rcu_read_lock();
  2478. p = find_task_by_vpid(pid);
  2479. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  2480. error = check_kill_permission(sig, info, p);
  2481. /*
  2482. * The null signal is a permissions and process existence
  2483. * probe. No signal is actually delivered.
  2484. */
  2485. if (!error && sig) {
  2486. error = do_send_sig_info(sig, info, p, false);
  2487. /*
  2488. * If lock_task_sighand() failed we pretend the task
  2489. * dies after receiving the signal. The window is tiny,
  2490. * and the signal is private anyway.
  2491. */
  2492. if (unlikely(error == -ESRCH))
  2493. error = 0;
  2494. }
  2495. }
  2496. rcu_read_unlock();
  2497. return error;
  2498. }
  2499. static int do_tkill(pid_t tgid, pid_t pid, int sig)
  2500. {
  2501. struct siginfo info;
  2502. info.si_signo = sig;
  2503. info.si_errno = 0;
  2504. info.si_code = SI_TKILL;
  2505. info.si_pid = task_tgid_vnr(current);
  2506. info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
  2507. return do_send_specific(tgid, pid, sig, &info);
  2508. }
  2509. /**
  2510. * sys_tgkill - send signal to one specific thread
  2511. * @tgid: the thread group ID of the thread
  2512. * @pid: the PID of the thread
  2513. * @sig: signal to be sent
  2514. *
  2515. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  2516. * exists but it's not belonging to the target process anymore. This
  2517. * method solves the problem of threads exiting and PIDs getting reused.
  2518. */
  2519. SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
  2520. {
  2521. /* This is only valid for single tasks */
  2522. if (pid <= 0 || tgid <= 0)
  2523. return -EINVAL;
  2524. return do_tkill(tgid, pid, sig);
  2525. }
  2526. /**
  2527. * sys_tkill - send signal to one specific task
  2528. * @pid: the PID of the task
  2529. * @sig: signal to be sent
  2530. *
  2531. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  2532. */
  2533. SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
  2534. {
  2535. /* This is only valid for single tasks */
  2536. if (pid <= 0)
  2537. return -EINVAL;
  2538. return do_tkill(0, pid, sig);
  2539. }
  2540. /**
  2541. * sys_rt_sigqueueinfo - send signal information to a signal
  2542. * @pid: the PID of the thread
  2543. * @sig: signal to be sent
  2544. * @uinfo: signal info to be sent
  2545. */
  2546. SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
  2547. siginfo_t __user *, uinfo)
  2548. {
  2549. siginfo_t info;
  2550. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2551. return -EFAULT;
  2552. /* Not even root can pretend to send signals from the kernel.
  2553. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2554. */
  2555. if (info.si_code >= 0 || info.si_code == SI_TKILL) {
  2556. /* We used to allow any < 0 si_code */
  2557. WARN_ON_ONCE(info.si_code < 0);
  2558. return -EPERM;
  2559. }
  2560. info.si_signo = sig;
  2561. /* POSIX.1b doesn't mention process groups. */
  2562. return kill_proc_info(sig, &info, pid);
  2563. }
  2564. long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
  2565. {
  2566. /* This is only valid for single tasks */
  2567. if (pid <= 0 || tgid <= 0)
  2568. return -EINVAL;
  2569. /* Not even root can pretend to send signals from the kernel.
  2570. * Nor can they impersonate a kill()/tgkill(), which adds source info.
  2571. */
  2572. if (info->si_code >= 0 || info->si_code == SI_TKILL) {
  2573. /* We used to allow any < 0 si_code */
  2574. WARN_ON_ONCE(info->si_code < 0);
  2575. return -EPERM;
  2576. }
  2577. info->si_signo = sig;
  2578. return do_send_specific(tgid, pid, sig, info);
  2579. }
  2580. SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
  2581. siginfo_t __user *, uinfo)
  2582. {
  2583. siginfo_t info;
  2584. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  2585. return -EFAULT;
  2586. return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
  2587. }
  2588. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  2589. {
  2590. struct task_struct *t = current;
  2591. struct k_sigaction *k;
  2592. sigset_t mask;
  2593. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  2594. return -EINVAL;
  2595. k = &t->sighand->action[sig-1];
  2596. spin_lock_irq(&current->sighand->siglock);
  2597. if (oact)
  2598. *oact = *k;
  2599. if (act) {
  2600. sigdelsetmask(&act->sa.sa_mask,
  2601. sigmask(SIGKILL) | sigmask(SIGSTOP));
  2602. *k = *act;
  2603. /*
  2604. * POSIX 3.3.1.3:
  2605. * "Setting a signal action to SIG_IGN for a signal that is
  2606. * pending shall cause the pending signal to be discarded,
  2607. * whether or not it is blocked."
  2608. *
  2609. * "Setting a signal action to SIG_DFL for a signal that is
  2610. * pending and whose default action is to ignore the signal
  2611. * (for example, SIGCHLD), shall cause the pending signal to
  2612. * be discarded, whether or not it is blocked"
  2613. */
  2614. if (sig_handler_ignored(sig_handler(t, sig), sig)) {
  2615. sigemptyset(&mask);
  2616. sigaddset(&mask, sig);
  2617. rm_from_queue_full(&mask, &t->signal->shared_pending);
  2618. do {
  2619. rm_from_queue_full(&mask, &t->pending);
  2620. t = next_thread(t);
  2621. } while (t != current);
  2622. }
  2623. }
  2624. spin_unlock_irq(&current->sighand->siglock);
  2625. return 0;
  2626. }
  2627. int
  2628. do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
  2629. {
  2630. stack_t oss;
  2631. int error;
  2632. oss.ss_sp = (void __user *) current->sas_ss_sp;
  2633. oss.ss_size = current->sas_ss_size;
  2634. oss.ss_flags = sas_ss_flags(sp);
  2635. if (uss) {
  2636. void __user *ss_sp;
  2637. size_t ss_size;
  2638. int ss_flags;
  2639. error = -EFAULT;
  2640. if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
  2641. goto out;
  2642. error = __get_user(ss_sp, &uss->ss_sp) |
  2643. __get_user(ss_flags, &uss->ss_flags) |
  2644. __get_user(ss_size, &uss->ss_size);
  2645. if (error)
  2646. goto out;
  2647. error = -EPERM;
  2648. if (on_sig_stack(sp))
  2649. goto out;
  2650. error = -EINVAL;
  2651. /*
  2652. * Note - this code used to test ss_flags incorrectly:
  2653. * old code may have been written using ss_flags==0
  2654. * to mean ss_flags==SS_ONSTACK (as this was the only
  2655. * way that worked) - this fix preserves that older
  2656. * mechanism.
  2657. */
  2658. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  2659. goto out;
  2660. if (ss_flags == SS_DISABLE) {
  2661. ss_size = 0;
  2662. ss_sp = NULL;
  2663. } else {
  2664. error = -ENOMEM;
  2665. if (ss_size < MINSIGSTKSZ)
  2666. goto out;
  2667. }
  2668. current->sas_ss_sp = (unsigned long) ss_sp;
  2669. current->sas_ss_size = ss_size;
  2670. }
  2671. error = 0;
  2672. if (uoss) {
  2673. error = -EFAULT;
  2674. if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
  2675. goto out;
  2676. error = __put_user(oss.ss_sp, &uoss->ss_sp) |
  2677. __put_user(oss.ss_size, &uoss->ss_size) |
  2678. __put_user(oss.ss_flags, &uoss->ss_flags);
  2679. }
  2680. out:
  2681. return error;
  2682. }
  2683. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2684. /**
  2685. * sys_sigpending - examine pending signals
  2686. * @set: where mask of pending signal is returned
  2687. */
  2688. SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
  2689. {
  2690. return do_sigpending(set, sizeof(*set));
  2691. }
  2692. #endif
  2693. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2694. /**
  2695. * sys_sigprocmask - examine and change blocked signals
  2696. * @how: whether to add, remove, or set signals
  2697. * @nset: signals to add or remove (if non-null)
  2698. * @oset: previous value of signal mask if non-null
  2699. *
  2700. * Some platforms have their own version with special arguments;
  2701. * others support only sys_rt_sigprocmask.
  2702. */
  2703. SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
  2704. old_sigset_t __user *, oset)
  2705. {
  2706. old_sigset_t old_set, new_set;
  2707. sigset_t new_blocked;
  2708. old_set = current->blocked.sig[0];
  2709. if (nset) {
  2710. if (copy_from_user(&new_set, nset, sizeof(*nset)))
  2711. return -EFAULT;
  2712. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  2713. new_blocked = current->blocked;
  2714. switch (how) {
  2715. case SIG_BLOCK:
  2716. sigaddsetmask(&new_blocked, new_set);
  2717. break;
  2718. case SIG_UNBLOCK:
  2719. sigdelsetmask(&new_blocked, new_set);
  2720. break;
  2721. case SIG_SETMASK:
  2722. new_blocked.sig[0] = new_set;
  2723. break;
  2724. default:
  2725. return -EINVAL;
  2726. }
  2727. set_current_blocked(&new_blocked);
  2728. }
  2729. if (oset) {
  2730. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2731. return -EFAULT;
  2732. }
  2733. return 0;
  2734. }
  2735. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2736. #ifdef __ARCH_WANT_SYS_RT_SIGACTION
  2737. /**
  2738. * sys_rt_sigaction - alter an action taken by a process
  2739. * @sig: signal to be sent
  2740. * @act: new sigaction
  2741. * @oact: used to save the previous sigaction
  2742. * @sigsetsize: size of sigset_t type
  2743. */
  2744. SYSCALL_DEFINE4(rt_sigaction, int, sig,
  2745. const struct sigaction __user *, act,
  2746. struct sigaction __user *, oact,
  2747. size_t, sigsetsize)
  2748. {
  2749. struct k_sigaction new_sa, old_sa;
  2750. int ret = -EINVAL;
  2751. /* XXX: Don't preclude handling different sized sigset_t's. */
  2752. if (sigsetsize != sizeof(sigset_t))
  2753. goto out;
  2754. if (act) {
  2755. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2756. return -EFAULT;
  2757. }
  2758. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2759. if (!ret && oact) {
  2760. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2761. return -EFAULT;
  2762. }
  2763. out:
  2764. return ret;
  2765. }
  2766. #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
  2767. #ifdef __ARCH_WANT_SYS_SGETMASK
  2768. /*
  2769. * For backwards compatibility. Functionality superseded by sigprocmask.
  2770. */
  2771. SYSCALL_DEFINE0(sgetmask)
  2772. {
  2773. /* SMP safe */
  2774. return current->blocked.sig[0];
  2775. }
  2776. SYSCALL_DEFINE1(ssetmask, int, newmask)
  2777. {
  2778. int old = current->blocked.sig[0];
  2779. sigset_t newset;
  2780. siginitset(&newset, newmask & ~(sigmask(SIGKILL) | sigmask(SIGSTOP)));
  2781. set_current_blocked(&newset);
  2782. return old;
  2783. }
  2784. #endif /* __ARCH_WANT_SGETMASK */
  2785. #ifdef __ARCH_WANT_SYS_SIGNAL
  2786. /*
  2787. * For backwards compatibility. Functionality superseded by sigaction.
  2788. */
  2789. SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
  2790. {
  2791. struct k_sigaction new_sa, old_sa;
  2792. int ret;
  2793. new_sa.sa.sa_handler = handler;
  2794. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  2795. sigemptyset(&new_sa.sa.sa_mask);
  2796. ret = do_sigaction(sig, &new_sa, &old_sa);
  2797. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  2798. }
  2799. #endif /* __ARCH_WANT_SYS_SIGNAL */
  2800. #ifdef __ARCH_WANT_SYS_PAUSE
  2801. SYSCALL_DEFINE0(pause)
  2802. {
  2803. while (!signal_pending(current)) {
  2804. current->state = TASK_INTERRUPTIBLE;
  2805. schedule();
  2806. }
  2807. return -ERESTARTNOHAND;
  2808. }
  2809. #endif
  2810. #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
  2811. /**
  2812. * sys_rt_sigsuspend - replace the signal mask for a value with the
  2813. * @unewset value until a signal is received
  2814. * @unewset: new signal mask value
  2815. * @sigsetsize: size of sigset_t type
  2816. */
  2817. SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
  2818. {
  2819. sigset_t newset;
  2820. /* XXX: Don't preclude handling different sized sigset_t's. */
  2821. if (sigsetsize != sizeof(sigset_t))
  2822. return -EINVAL;
  2823. if (copy_from_user(&newset, unewset, sizeof(newset)))
  2824. return -EFAULT;
  2825. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2826. current->saved_sigmask = current->blocked;
  2827. set_current_blocked(&newset);
  2828. current->state = TASK_INTERRUPTIBLE;
  2829. schedule();
  2830. set_restore_sigmask();
  2831. return -ERESTARTNOHAND;
  2832. }
  2833. #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
  2834. __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
  2835. {
  2836. return NULL;
  2837. }
  2838. void __init signals_init(void)
  2839. {
  2840. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  2841. }
  2842. #ifdef CONFIG_KGDB_KDB
  2843. #include <linux/kdb.h>
  2844. /*
  2845. * kdb_send_sig_info - Allows kdb to send signals without exposing
  2846. * signal internals. This function checks if the required locks are
  2847. * available before calling the main signal code, to avoid kdb
  2848. * deadlocks.
  2849. */
  2850. void
  2851. kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
  2852. {
  2853. static struct task_struct *kdb_prev_t;
  2854. int sig, new_t;
  2855. if (!spin_trylock(&t->sighand->siglock)) {
  2856. kdb_printf("Can't do kill command now.\n"
  2857. "The sigmask lock is held somewhere else in "
  2858. "kernel, try again later\n");
  2859. return;
  2860. }
  2861. spin_unlock(&t->sighand->siglock);
  2862. new_t = kdb_prev_t != t;
  2863. kdb_prev_t = t;
  2864. if (t->state != TASK_RUNNING && new_t) {
  2865. kdb_printf("Process is not RUNNING, sending a signal from "
  2866. "kdb risks deadlock\n"
  2867. "on the run queue locks. "
  2868. "The signal has _not_ been sent.\n"
  2869. "Reissue the kill command if you want to risk "
  2870. "the deadlock.\n");
  2871. return;
  2872. }
  2873. sig = info->si_signo;
  2874. if (send_sig_info(sig, info, t))
  2875. kdb_printf("Fail to deliver Signal %d to process %d.\n",
  2876. sig, t->pid);
  2877. else
  2878. kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
  2879. }
  2880. #endif /* CONFIG_KGDB_KDB */