signal.c 83 KB

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