signal.c 85 KB

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