signal.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. /*
  2. * linux/kernel/signal.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
  7. *
  8. * 2003-06-02 Jim Houston - Concurrent Computer Corp.
  9. * Changes to use preallocated sigqueue structures
  10. * to allow signals to be sent reliably.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/sched.h>
  16. #include <linux/fs.h>
  17. #include <linux/tty.h>
  18. #include <linux/binfmts.h>
  19. #include <linux/security.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/ptrace.h>
  22. #include <linux/signal.h>
  23. #include <linux/signalfd.h>
  24. #include <linux/capability.h>
  25. #include <linux/freezer.h>
  26. #include <linux/pid_namespace.h>
  27. #include <linux/nsproxy.h>
  28. #include <asm/param.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/unistd.h>
  31. #include <asm/siginfo.h>
  32. #include "audit.h" /* audit_signal_info() */
  33. /*
  34. * SLAB caches for signal bits.
  35. */
  36. static struct kmem_cache *sigqueue_cachep;
  37. static int __sig_ignored(struct task_struct *t, int sig)
  38. {
  39. void __user *handler;
  40. /* Is it explicitly or implicitly ignored? */
  41. handler = t->sighand->action[sig - 1].sa.sa_handler;
  42. return handler == SIG_IGN ||
  43. (handler == SIG_DFL && sig_kernel_ignore(sig));
  44. }
  45. static int sig_ignored(struct task_struct *t, int sig)
  46. {
  47. /*
  48. * Tracers always want to know about signals..
  49. */
  50. if (t->ptrace & PT_PTRACED)
  51. return 0;
  52. /*
  53. * Blocked signals are never ignored, since the
  54. * signal handler may change by the time it is
  55. * unblocked.
  56. */
  57. if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
  58. return 0;
  59. return __sig_ignored(t, sig);
  60. }
  61. /*
  62. * Re-calculate pending state from the set of locally pending
  63. * signals, globally pending signals, and blocked signals.
  64. */
  65. static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
  66. {
  67. unsigned long ready;
  68. long i;
  69. switch (_NSIG_WORDS) {
  70. default:
  71. for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
  72. ready |= signal->sig[i] &~ blocked->sig[i];
  73. break;
  74. case 4: ready = signal->sig[3] &~ blocked->sig[3];
  75. ready |= signal->sig[2] &~ blocked->sig[2];
  76. ready |= signal->sig[1] &~ blocked->sig[1];
  77. ready |= signal->sig[0] &~ blocked->sig[0];
  78. break;
  79. case 2: ready = signal->sig[1] &~ blocked->sig[1];
  80. ready |= signal->sig[0] &~ blocked->sig[0];
  81. break;
  82. case 1: ready = signal->sig[0] &~ blocked->sig[0];
  83. }
  84. return ready != 0;
  85. }
  86. #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
  87. static int recalc_sigpending_tsk(struct task_struct *t)
  88. {
  89. if (t->signal->group_stop_count > 0 ||
  90. PENDING(&t->pending, &t->blocked) ||
  91. PENDING(&t->signal->shared_pending, &t->blocked)) {
  92. set_tsk_thread_flag(t, TIF_SIGPENDING);
  93. return 1;
  94. }
  95. /*
  96. * We must never clear the flag in another thread, or in current
  97. * when it's possible the current syscall is returning -ERESTART*.
  98. * So we don't clear it here, and only callers who know they should do.
  99. */
  100. return 0;
  101. }
  102. /*
  103. * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
  104. * This is superfluous when called on current, the wakeup is a harmless no-op.
  105. */
  106. void recalc_sigpending_and_wake(struct task_struct *t)
  107. {
  108. if (recalc_sigpending_tsk(t))
  109. signal_wake_up(t, 0);
  110. }
  111. void recalc_sigpending(void)
  112. {
  113. if (!recalc_sigpending_tsk(current) && !freezing(current))
  114. clear_thread_flag(TIF_SIGPENDING);
  115. }
  116. /* Given the mask, find the first available signal that should be serviced. */
  117. int next_signal(struct sigpending *pending, sigset_t *mask)
  118. {
  119. unsigned long i, *s, *m, x;
  120. int sig = 0;
  121. s = pending->signal.sig;
  122. m = mask->sig;
  123. switch (_NSIG_WORDS) {
  124. default:
  125. for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
  126. if ((x = *s &~ *m) != 0) {
  127. sig = ffz(~x) + i*_NSIG_BPW + 1;
  128. break;
  129. }
  130. break;
  131. case 2: if ((x = s[0] &~ m[0]) != 0)
  132. sig = 1;
  133. else if ((x = s[1] &~ m[1]) != 0)
  134. sig = _NSIG_BPW + 1;
  135. else
  136. break;
  137. sig += ffz(~x);
  138. break;
  139. case 1: if ((x = *s &~ *m) != 0)
  140. sig = ffz(~x) + 1;
  141. break;
  142. }
  143. return sig;
  144. }
  145. static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
  146. int override_rlimit)
  147. {
  148. struct sigqueue *q = NULL;
  149. struct user_struct *user;
  150. /*
  151. * In order to avoid problems with "switch_user()", we want to make
  152. * sure that the compiler doesn't re-load "t->user"
  153. */
  154. user = t->user;
  155. barrier();
  156. atomic_inc(&user->sigpending);
  157. if (override_rlimit ||
  158. atomic_read(&user->sigpending) <=
  159. t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
  160. q = kmem_cache_alloc(sigqueue_cachep, flags);
  161. if (unlikely(q == NULL)) {
  162. atomic_dec(&user->sigpending);
  163. } else {
  164. INIT_LIST_HEAD(&q->list);
  165. q->flags = 0;
  166. q->user = get_uid(user);
  167. }
  168. return(q);
  169. }
  170. static void __sigqueue_free(struct sigqueue *q)
  171. {
  172. if (q->flags & SIGQUEUE_PREALLOC)
  173. return;
  174. atomic_dec(&q->user->sigpending);
  175. free_uid(q->user);
  176. kmem_cache_free(sigqueue_cachep, q);
  177. }
  178. void flush_sigqueue(struct sigpending *queue)
  179. {
  180. struct sigqueue *q;
  181. sigemptyset(&queue->signal);
  182. while (!list_empty(&queue->list)) {
  183. q = list_entry(queue->list.next, struct sigqueue , list);
  184. list_del_init(&q->list);
  185. __sigqueue_free(q);
  186. }
  187. }
  188. /*
  189. * Flush all pending signals for a task.
  190. */
  191. void flush_signals(struct task_struct *t)
  192. {
  193. unsigned long flags;
  194. spin_lock_irqsave(&t->sighand->siglock, flags);
  195. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  196. flush_sigqueue(&t->pending);
  197. flush_sigqueue(&t->signal->shared_pending);
  198. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  199. }
  200. void ignore_signals(struct task_struct *t)
  201. {
  202. int i;
  203. for (i = 0; i < _NSIG; ++i)
  204. t->sighand->action[i].sa.sa_handler = SIG_IGN;
  205. flush_signals(t);
  206. }
  207. /*
  208. * Flush all handlers for a task.
  209. */
  210. void
  211. flush_signal_handlers(struct task_struct *t, int force_default)
  212. {
  213. int i;
  214. struct k_sigaction *ka = &t->sighand->action[0];
  215. for (i = _NSIG ; i != 0 ; i--) {
  216. if (force_default || ka->sa.sa_handler != SIG_IGN)
  217. ka->sa.sa_handler = SIG_DFL;
  218. ka->sa.sa_flags = 0;
  219. sigemptyset(&ka->sa.sa_mask);
  220. ka++;
  221. }
  222. }
  223. int unhandled_signal(struct task_struct *tsk, int sig)
  224. {
  225. if (is_global_init(tsk))
  226. return 1;
  227. if (tsk->ptrace & PT_PTRACED)
  228. return 0;
  229. return (tsk->sighand->action[sig-1].sa.sa_handler == SIG_IGN) ||
  230. (tsk->sighand->action[sig-1].sa.sa_handler == SIG_DFL);
  231. }
  232. /* Notify the system that a driver wants to block all signals for this
  233. * process, and wants to be notified if any signals at all were to be
  234. * sent/acted upon. If the notifier routine returns non-zero, then the
  235. * signal will be acted upon after all. If the notifier routine returns 0,
  236. * then then signal will be blocked. Only one block per process is
  237. * allowed. priv is a pointer to private data that the notifier routine
  238. * can use to determine if the signal should be blocked or not. */
  239. void
  240. block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
  241. {
  242. unsigned long flags;
  243. spin_lock_irqsave(&current->sighand->siglock, flags);
  244. current->notifier_mask = mask;
  245. current->notifier_data = priv;
  246. current->notifier = notifier;
  247. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  248. }
  249. /* Notify the system that blocking has ended. */
  250. void
  251. unblock_all_signals(void)
  252. {
  253. unsigned long flags;
  254. spin_lock_irqsave(&current->sighand->siglock, flags);
  255. current->notifier = NULL;
  256. current->notifier_data = NULL;
  257. recalc_sigpending();
  258. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  259. }
  260. static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
  261. {
  262. struct sigqueue *q, *first = NULL;
  263. int still_pending = 0;
  264. if (unlikely(!sigismember(&list->signal, sig)))
  265. return 0;
  266. /*
  267. * Collect the siginfo appropriate to this signal. Check if
  268. * there is another siginfo for the same signal.
  269. */
  270. list_for_each_entry(q, &list->list, list) {
  271. if (q->info.si_signo == sig) {
  272. if (first) {
  273. still_pending = 1;
  274. break;
  275. }
  276. first = q;
  277. }
  278. }
  279. if (first) {
  280. list_del_init(&first->list);
  281. copy_siginfo(info, &first->info);
  282. __sigqueue_free(first);
  283. if (!still_pending)
  284. sigdelset(&list->signal, sig);
  285. } else {
  286. /* Ok, it wasn't in the queue. This must be
  287. a fast-pathed signal or we must have been
  288. out of queue space. So zero out the info.
  289. */
  290. sigdelset(&list->signal, sig);
  291. info->si_signo = sig;
  292. info->si_errno = 0;
  293. info->si_code = 0;
  294. info->si_pid = 0;
  295. info->si_uid = 0;
  296. }
  297. return 1;
  298. }
  299. static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
  300. siginfo_t *info)
  301. {
  302. int sig = next_signal(pending, mask);
  303. if (sig) {
  304. if (current->notifier) {
  305. if (sigismember(current->notifier_mask, sig)) {
  306. if (!(current->notifier)(current->notifier_data)) {
  307. clear_thread_flag(TIF_SIGPENDING);
  308. return 0;
  309. }
  310. }
  311. }
  312. if (!collect_signal(sig, pending, info))
  313. sig = 0;
  314. }
  315. return sig;
  316. }
  317. /*
  318. * Dequeue a signal and return the element to the caller, which is
  319. * expected to free it.
  320. *
  321. * All callers have to hold the siglock.
  322. */
  323. int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
  324. {
  325. int signr;
  326. /* We only dequeue private signals from ourselves, we don't let
  327. * signalfd steal them
  328. */
  329. signr = __dequeue_signal(&tsk->pending, mask, info);
  330. if (!signr) {
  331. signr = __dequeue_signal(&tsk->signal->shared_pending,
  332. mask, info);
  333. /*
  334. * itimer signal ?
  335. *
  336. * itimers are process shared and we restart periodic
  337. * itimers in the signal delivery path to prevent DoS
  338. * attacks in the high resolution timer case. This is
  339. * compliant with the old way of self restarting
  340. * itimers, as the SIGALRM is a legacy signal and only
  341. * queued once. Changing the restart behaviour to
  342. * restart the timer in the signal dequeue path is
  343. * reducing the timer noise on heavy loaded !highres
  344. * systems too.
  345. */
  346. if (unlikely(signr == SIGALRM)) {
  347. struct hrtimer *tmr = &tsk->signal->real_timer;
  348. if (!hrtimer_is_queued(tmr) &&
  349. tsk->signal->it_real_incr.tv64 != 0) {
  350. hrtimer_forward(tmr, tmr->base->get_time(),
  351. tsk->signal->it_real_incr);
  352. hrtimer_restart(tmr);
  353. }
  354. }
  355. }
  356. recalc_sigpending();
  357. if (!signr)
  358. return 0;
  359. if (unlikely(sig_kernel_stop(signr))) {
  360. /*
  361. * Set a marker that we have dequeued a stop signal. Our
  362. * caller might release the siglock and then the pending
  363. * stop signal it is about to process is no longer in the
  364. * pending bitmasks, but must still be cleared by a SIGCONT
  365. * (and overruled by a SIGKILL). So those cases clear this
  366. * shared flag after we've set it. Note that this flag may
  367. * remain set after the signal we return is ignored or
  368. * handled. That doesn't matter because its only purpose
  369. * is to alert stop-signal processing code when another
  370. * processor has come along and cleared the flag.
  371. */
  372. if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT))
  373. tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
  374. }
  375. if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
  376. /*
  377. * Release the siglock to ensure proper locking order
  378. * of timer locks outside of siglocks. Note, we leave
  379. * irqs disabled here, since the posix-timers code is
  380. * about to disable them again anyway.
  381. */
  382. spin_unlock(&tsk->sighand->siglock);
  383. do_schedule_next_timer(info);
  384. spin_lock(&tsk->sighand->siglock);
  385. }
  386. return signr;
  387. }
  388. /*
  389. * Tell a process that it has a new active signal..
  390. *
  391. * NOTE! we rely on the previous spin_lock to
  392. * lock interrupts for us! We can only be called with
  393. * "siglock" held, and the local interrupt must
  394. * have been disabled when that got acquired!
  395. *
  396. * No need to set need_resched since signal event passing
  397. * goes through ->blocked
  398. */
  399. void signal_wake_up(struct task_struct *t, int resume)
  400. {
  401. unsigned int mask;
  402. set_tsk_thread_flag(t, TIF_SIGPENDING);
  403. /*
  404. * For SIGKILL, we want to wake it up in the stopped/traced/killable
  405. * case. We don't check t->state here because there is a race with it
  406. * executing another processor and just now entering stopped state.
  407. * By using wake_up_state, we ensure the process will wake up and
  408. * handle its death signal.
  409. */
  410. mask = TASK_INTERRUPTIBLE;
  411. if (resume)
  412. mask |= TASK_WAKEKILL;
  413. if (!wake_up_state(t, mask))
  414. kick_process(t);
  415. }
  416. /*
  417. * Remove signals in mask from the pending set and queue.
  418. * Returns 1 if any signals were found.
  419. *
  420. * All callers must be holding the siglock.
  421. *
  422. * This version takes a sigset mask and looks at all signals,
  423. * not just those in the first mask word.
  424. */
  425. static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
  426. {
  427. struct sigqueue *q, *n;
  428. sigset_t m;
  429. sigandsets(&m, mask, &s->signal);
  430. if (sigisemptyset(&m))
  431. return 0;
  432. signandsets(&s->signal, &s->signal, mask);
  433. list_for_each_entry_safe(q, n, &s->list, list) {
  434. if (sigismember(mask, q->info.si_signo)) {
  435. list_del_init(&q->list);
  436. __sigqueue_free(q);
  437. }
  438. }
  439. return 1;
  440. }
  441. /*
  442. * Remove signals in mask from the pending set and queue.
  443. * Returns 1 if any signals were found.
  444. *
  445. * All callers must be holding the siglock.
  446. */
  447. static int rm_from_queue(unsigned long mask, struct sigpending *s)
  448. {
  449. struct sigqueue *q, *n;
  450. if (!sigtestsetmask(&s->signal, mask))
  451. return 0;
  452. sigdelsetmask(&s->signal, mask);
  453. list_for_each_entry_safe(q, n, &s->list, list) {
  454. if (q->info.si_signo < SIGRTMIN &&
  455. (mask & sigmask(q->info.si_signo))) {
  456. list_del_init(&q->list);
  457. __sigqueue_free(q);
  458. }
  459. }
  460. return 1;
  461. }
  462. /*
  463. * Bad permissions for sending the signal
  464. */
  465. static int check_kill_permission(int sig, struct siginfo *info,
  466. struct task_struct *t)
  467. {
  468. int error;
  469. if (!valid_signal(sig))
  470. return -EINVAL;
  471. if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
  472. return 0;
  473. error = audit_signal_info(sig, t); /* Let audit system see the signal */
  474. if (error)
  475. return error;
  476. if (((sig != SIGCONT) || (task_session_nr(current) != task_session_nr(t)))
  477. && (current->euid ^ t->suid) && (current->euid ^ t->uid)
  478. && (current->uid ^ t->suid) && (current->uid ^ t->uid)
  479. && !capable(CAP_KILL))
  480. return -EPERM;
  481. return security_task_kill(t, info, sig, 0);
  482. }
  483. /* forward decl */
  484. static void do_notify_parent_cldstop(struct task_struct *tsk, int why);
  485. /*
  486. * Handle magic process-wide effects of stop/continue signals.
  487. * Unlike the signal actions, these happen immediately at signal-generation
  488. * time regardless of blocking, ignoring, or handling. This does the
  489. * actual continuing for SIGCONT, but not the actual stopping for stop
  490. * signals. The process stop is done as a signal action for SIG_DFL.
  491. */
  492. static void handle_stop_signal(int sig, struct task_struct *p)
  493. {
  494. struct signal_struct *signal = p->signal;
  495. struct task_struct *t;
  496. if (signal->flags & SIGNAL_GROUP_EXIT)
  497. /*
  498. * The process is in the middle of dying already.
  499. */
  500. return;
  501. if (sig_kernel_stop(sig)) {
  502. /*
  503. * This is a stop signal. Remove SIGCONT from all queues.
  504. */
  505. rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
  506. t = p;
  507. do {
  508. rm_from_queue(sigmask(SIGCONT), &t->pending);
  509. } while_each_thread(p, t);
  510. } else if (sig == SIGCONT) {
  511. unsigned int why;
  512. /*
  513. * Remove all stop signals from all queues,
  514. * and wake all threads.
  515. */
  516. rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
  517. t = p;
  518. do {
  519. unsigned int state;
  520. rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
  521. /*
  522. * If there is a handler for SIGCONT, we must make
  523. * sure that no thread returns to user mode before
  524. * we post the signal, in case it was the only
  525. * thread eligible to run the signal handler--then
  526. * it must not do anything between resuming and
  527. * running the handler. With the TIF_SIGPENDING
  528. * flag set, the thread will pause and acquire the
  529. * siglock that we hold now and until we've queued
  530. * the pending signal.
  531. *
  532. * Wake up the stopped thread _after_ setting
  533. * TIF_SIGPENDING
  534. */
  535. state = __TASK_STOPPED;
  536. if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
  537. set_tsk_thread_flag(t, TIF_SIGPENDING);
  538. state |= TASK_INTERRUPTIBLE;
  539. }
  540. wake_up_state(t, state);
  541. } while_each_thread(p, t);
  542. /*
  543. * Notify the parent with CLD_CONTINUED if we were stopped.
  544. *
  545. * If we were in the middle of a group stop, we pretend it
  546. * was already finished, and then continued. Since SIGCHLD
  547. * doesn't queue we report only CLD_STOPPED, as if the next
  548. * CLD_CONTINUED was dropped.
  549. */
  550. why = 0;
  551. if (signal->flags & SIGNAL_STOP_STOPPED)
  552. why |= SIGNAL_CLD_CONTINUED;
  553. else if (signal->group_stop_count)
  554. why |= SIGNAL_CLD_STOPPED;
  555. if (why) {
  556. signal->flags = why | SIGNAL_STOP_CONTINUED;
  557. signal->group_stop_count = 0;
  558. signal->group_exit_code = 0;
  559. } else {
  560. /*
  561. * We are not stopped, but there could be a stop
  562. * signal in the middle of being processed after
  563. * being removed from the queue. Clear that too.
  564. */
  565. signal->flags &= ~SIGNAL_STOP_DEQUEUED;
  566. }
  567. } else if (sig == SIGKILL) {
  568. /*
  569. * Make sure that any pending stop signal already dequeued
  570. * is undone by the wakeup for SIGKILL.
  571. */
  572. signal->flags &= ~SIGNAL_STOP_DEQUEUED;
  573. }
  574. }
  575. static inline int legacy_queue(struct sigpending *signals, int sig)
  576. {
  577. return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
  578. }
  579. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  580. struct sigpending *signals)
  581. {
  582. struct sigqueue *q;
  583. assert_spin_locked(&t->sighand->siglock);
  584. handle_stop_signal(sig, t);
  585. /*
  586. * Short-circuit ignored signals and support queuing
  587. * exactly one non-rt signal, so that we can get more
  588. * detailed information about the cause of the signal.
  589. */
  590. if (sig_ignored(t, sig) || legacy_queue(signals, sig))
  591. return 0;
  592. /*
  593. * Deliver the signal to listening signalfds. This must be called
  594. * with the sighand lock held.
  595. */
  596. signalfd_notify(t, sig);
  597. /*
  598. * fast-pathed signals for kernel-internal things like SIGSTOP
  599. * or SIGKILL.
  600. */
  601. if (info == SEND_SIG_FORCED)
  602. goto out_set;
  603. /* Real-time signals must be queued if sent by sigqueue, or
  604. some other real-time mechanism. It is implementation
  605. defined whether kill() does so. We attempt to do so, on
  606. the principle of least surprise, but since kill is not
  607. allowed to fail with EAGAIN when low on memory we just
  608. make sure at least one signal gets delivered and don't
  609. pass on the info struct. */
  610. q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
  611. (is_si_special(info) ||
  612. info->si_code >= 0)));
  613. if (q) {
  614. list_add_tail(&q->list, &signals->list);
  615. switch ((unsigned long) info) {
  616. case (unsigned long) SEND_SIG_NOINFO:
  617. q->info.si_signo = sig;
  618. q->info.si_errno = 0;
  619. q->info.si_code = SI_USER;
  620. q->info.si_pid = task_pid_vnr(current);
  621. q->info.si_uid = current->uid;
  622. break;
  623. case (unsigned long) SEND_SIG_PRIV:
  624. q->info.si_signo = sig;
  625. q->info.si_errno = 0;
  626. q->info.si_code = SI_KERNEL;
  627. q->info.si_pid = 0;
  628. q->info.si_uid = 0;
  629. break;
  630. default:
  631. copy_siginfo(&q->info, info);
  632. break;
  633. }
  634. } else if (!is_si_special(info)) {
  635. if (sig >= SIGRTMIN && info->si_code != SI_USER)
  636. /*
  637. * Queue overflow, abort. We may abort if the signal was rt
  638. * and sent by user using something other than kill().
  639. */
  640. return -EAGAIN;
  641. }
  642. out_set:
  643. sigaddset(&signals->signal, sig);
  644. return 1;
  645. }
  646. int print_fatal_signals;
  647. static void print_fatal_signal(struct pt_regs *regs, int signr)
  648. {
  649. printk("%s/%d: potentially unexpected fatal signal %d.\n",
  650. current->comm, task_pid_nr(current), signr);
  651. #if defined(__i386__) && !defined(__arch_um__)
  652. printk("code at %08lx: ", regs->ip);
  653. {
  654. int i;
  655. for (i = 0; i < 16; i++) {
  656. unsigned char insn;
  657. __get_user(insn, (unsigned char *)(regs->ip + i));
  658. printk("%02x ", insn);
  659. }
  660. }
  661. #endif
  662. printk("\n");
  663. show_regs(regs);
  664. }
  665. static int __init setup_print_fatal_signals(char *str)
  666. {
  667. get_option (&str, &print_fatal_signals);
  668. return 1;
  669. }
  670. __setup("print-fatal-signals=", setup_print_fatal_signals);
  671. static int
  672. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  673. {
  674. int ret;
  675. ret = send_signal(sig, info, t, &t->pending);
  676. if (ret <= 0)
  677. return ret;
  678. if (!sigismember(&t->blocked, sig))
  679. signal_wake_up(t, sig == SIGKILL);
  680. return 0;
  681. }
  682. /*
  683. * Force a signal that the process can't ignore: if necessary
  684. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  685. *
  686. * Note: If we unblock the signal, we always reset it to SIG_DFL,
  687. * since we do not want to have a signal handler that was blocked
  688. * be invoked when user space had explicitly blocked it.
  689. *
  690. * We don't want to have recursive SIGSEGV's etc, for example.
  691. */
  692. int
  693. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  694. {
  695. unsigned long int flags;
  696. int ret, blocked, ignored;
  697. struct k_sigaction *action;
  698. spin_lock_irqsave(&t->sighand->siglock, flags);
  699. action = &t->sighand->action[sig-1];
  700. ignored = action->sa.sa_handler == SIG_IGN;
  701. blocked = sigismember(&t->blocked, sig);
  702. if (blocked || ignored) {
  703. action->sa.sa_handler = SIG_DFL;
  704. if (blocked) {
  705. sigdelset(&t->blocked, sig);
  706. recalc_sigpending_and_wake(t);
  707. }
  708. }
  709. ret = specific_send_sig_info(sig, info, t);
  710. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  711. return ret;
  712. }
  713. void
  714. force_sig_specific(int sig, struct task_struct *t)
  715. {
  716. force_sig_info(sig, SEND_SIG_FORCED, t);
  717. }
  718. /*
  719. * Test if P wants to take SIG. After we've checked all threads with this,
  720. * it's equivalent to finding no threads not blocking SIG. Any threads not
  721. * blocking SIG were ruled out because they are not running and already
  722. * have pending signals. Such threads will dequeue from the shared queue
  723. * as soon as they're available, so putting the signal on the shared queue
  724. * will be equivalent to sending it to one such thread.
  725. */
  726. static inline int wants_signal(int sig, struct task_struct *p)
  727. {
  728. if (sigismember(&p->blocked, sig))
  729. return 0;
  730. if (p->flags & PF_EXITING)
  731. return 0;
  732. if (sig == SIGKILL)
  733. return 1;
  734. if (task_is_stopped_or_traced(p))
  735. return 0;
  736. return task_curr(p) || !signal_pending(p);
  737. }
  738. static void
  739. __group_complete_signal(int sig, struct task_struct *p)
  740. {
  741. struct signal_struct *signal = p->signal;
  742. struct task_struct *t;
  743. /*
  744. * Now find a thread we can wake up to take the signal off the queue.
  745. *
  746. * If the main thread wants the signal, it gets first crack.
  747. * Probably the least surprising to the average bear.
  748. */
  749. if (wants_signal(sig, p))
  750. t = p;
  751. else if (thread_group_empty(p))
  752. /*
  753. * There is just one thread and it does not need to be woken.
  754. * It will dequeue unblocked signals before it runs again.
  755. */
  756. return;
  757. else {
  758. /*
  759. * Otherwise try to find a suitable thread.
  760. */
  761. t = signal->curr_target;
  762. if (t == NULL)
  763. /* restart balancing at this thread */
  764. t = signal->curr_target = p;
  765. while (!wants_signal(sig, t)) {
  766. t = next_thread(t);
  767. if (t == signal->curr_target)
  768. /*
  769. * No thread needs to be woken.
  770. * Any eligible threads will see
  771. * the signal in the queue soon.
  772. */
  773. return;
  774. }
  775. signal->curr_target = t;
  776. }
  777. /*
  778. * Found a killable thread. If the signal will be fatal,
  779. * then start taking the whole group down immediately.
  780. */
  781. if (sig_fatal(p, sig) && !(signal->flags & SIGNAL_GROUP_EXIT) &&
  782. !sigismember(&t->real_blocked, sig) &&
  783. (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
  784. /*
  785. * This signal will be fatal to the whole group.
  786. */
  787. if (!sig_kernel_coredump(sig)) {
  788. /*
  789. * Start a group exit and wake everybody up.
  790. * This way we don't have other threads
  791. * running and doing things after a slower
  792. * thread has the fatal signal pending.
  793. */
  794. signal->flags = SIGNAL_GROUP_EXIT;
  795. signal->group_exit_code = sig;
  796. signal->group_stop_count = 0;
  797. t = p;
  798. do {
  799. sigaddset(&t->pending.signal, SIGKILL);
  800. signal_wake_up(t, 1);
  801. } while_each_thread(p, t);
  802. return;
  803. }
  804. }
  805. /*
  806. * The signal is already in the shared-pending queue.
  807. * Tell the chosen thread to wake up and dequeue it.
  808. */
  809. signal_wake_up(t, sig == SIGKILL);
  810. return;
  811. }
  812. int
  813. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  814. {
  815. int ret;
  816. /*
  817. * Put this signal on the shared-pending queue, or fail with EAGAIN.
  818. * We always use the shared queue for process-wide signals,
  819. * to avoid several races.
  820. */
  821. ret = send_signal(sig, info, p, &p->signal->shared_pending);
  822. if (ret <= 0)
  823. return ret;
  824. __group_complete_signal(sig, p);
  825. return 0;
  826. }
  827. /*
  828. * Nuke all other threads in the group.
  829. */
  830. void zap_other_threads(struct task_struct *p)
  831. {
  832. struct task_struct *t;
  833. p->signal->group_stop_count = 0;
  834. for (t = next_thread(p); t != p; t = next_thread(t)) {
  835. /*
  836. * Don't bother with already dead threads
  837. */
  838. if (t->exit_state)
  839. continue;
  840. /* SIGKILL will be handled before any pending SIGSTOP */
  841. sigaddset(&t->pending.signal, SIGKILL);
  842. signal_wake_up(t, 1);
  843. }
  844. }
  845. int __fatal_signal_pending(struct task_struct *tsk)
  846. {
  847. return sigismember(&tsk->pending.signal, SIGKILL);
  848. }
  849. EXPORT_SYMBOL(__fatal_signal_pending);
  850. struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
  851. {
  852. struct sighand_struct *sighand;
  853. rcu_read_lock();
  854. for (;;) {
  855. sighand = rcu_dereference(tsk->sighand);
  856. if (unlikely(sighand == NULL))
  857. break;
  858. spin_lock_irqsave(&sighand->siglock, *flags);
  859. if (likely(sighand == tsk->sighand))
  860. break;
  861. spin_unlock_irqrestore(&sighand->siglock, *flags);
  862. }
  863. rcu_read_unlock();
  864. return sighand;
  865. }
  866. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  867. {
  868. unsigned long flags;
  869. int ret;
  870. ret = check_kill_permission(sig, info, p);
  871. if (!ret && sig) {
  872. ret = -ESRCH;
  873. if (lock_task_sighand(p, &flags)) {
  874. ret = __group_send_sig_info(sig, info, p);
  875. unlock_task_sighand(p, &flags);
  876. }
  877. }
  878. return ret;
  879. }
  880. /*
  881. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  882. * control characters do (^C, ^Z etc)
  883. */
  884. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  885. {
  886. struct task_struct *p = NULL;
  887. int retval, success;
  888. success = 0;
  889. retval = -ESRCH;
  890. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  891. int err = group_send_sig_info(sig, info, p);
  892. success |= !err;
  893. retval = err;
  894. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  895. return success ? 0 : retval;
  896. }
  897. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  898. {
  899. int error = -ESRCH;
  900. struct task_struct *p;
  901. rcu_read_lock();
  902. retry:
  903. p = pid_task(pid, PIDTYPE_PID);
  904. if (p) {
  905. error = group_send_sig_info(sig, info, p);
  906. if (unlikely(error == -ESRCH))
  907. /*
  908. * The task was unhashed in between, try again.
  909. * If it is dead, pid_task() will return NULL,
  910. * if we race with de_thread() it will find the
  911. * new leader.
  912. */
  913. goto retry;
  914. }
  915. rcu_read_unlock();
  916. return error;
  917. }
  918. int
  919. kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  920. {
  921. int error;
  922. rcu_read_lock();
  923. error = kill_pid_info(sig, info, find_vpid(pid));
  924. rcu_read_unlock();
  925. return error;
  926. }
  927. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  928. int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
  929. uid_t uid, uid_t euid, u32 secid)
  930. {
  931. int ret = -EINVAL;
  932. struct task_struct *p;
  933. if (!valid_signal(sig))
  934. return ret;
  935. read_lock(&tasklist_lock);
  936. p = pid_task(pid, PIDTYPE_PID);
  937. if (!p) {
  938. ret = -ESRCH;
  939. goto out_unlock;
  940. }
  941. if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
  942. && (euid != p->suid) && (euid != p->uid)
  943. && (uid != p->suid) && (uid != p->uid)) {
  944. ret = -EPERM;
  945. goto out_unlock;
  946. }
  947. ret = security_task_kill(p, info, sig, secid);
  948. if (ret)
  949. goto out_unlock;
  950. if (sig && p->sighand) {
  951. unsigned long flags;
  952. spin_lock_irqsave(&p->sighand->siglock, flags);
  953. ret = __group_send_sig_info(sig, info, p);
  954. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  955. }
  956. out_unlock:
  957. read_unlock(&tasklist_lock);
  958. return ret;
  959. }
  960. EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
  961. /*
  962. * kill_something_info() interprets pid in interesting ways just like kill(2).
  963. *
  964. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  965. * is probably wrong. Should make it like BSD or SYSV.
  966. */
  967. static int kill_something_info(int sig, struct siginfo *info, int pid)
  968. {
  969. int ret;
  970. if (pid > 0) {
  971. rcu_read_lock();
  972. ret = kill_pid_info(sig, info, find_vpid(pid));
  973. rcu_read_unlock();
  974. return ret;
  975. }
  976. read_lock(&tasklist_lock);
  977. if (pid != -1) {
  978. ret = __kill_pgrp_info(sig, info,
  979. pid ? find_vpid(-pid) : task_pgrp(current));
  980. } else {
  981. int retval = 0, count = 0;
  982. struct task_struct * p;
  983. for_each_process(p) {
  984. if (p->pid > 1 && !same_thread_group(p, current)) {
  985. int err = group_send_sig_info(sig, info, p);
  986. ++count;
  987. if (err != -EPERM)
  988. retval = err;
  989. }
  990. }
  991. ret = count ? retval : -ESRCH;
  992. }
  993. read_unlock(&tasklist_lock);
  994. return ret;
  995. }
  996. /*
  997. * These are for backward compatibility with the rest of the kernel source.
  998. */
  999. /*
  1000. * These two are the most common entry points. They send a signal
  1001. * just to the specific thread.
  1002. */
  1003. int
  1004. send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  1005. {
  1006. int ret;
  1007. unsigned long flags;
  1008. /*
  1009. * Make sure legacy kernel users don't send in bad values
  1010. * (normal paths check this in check_kill_permission).
  1011. */
  1012. if (!valid_signal(sig))
  1013. return -EINVAL;
  1014. /*
  1015. * We need the tasklist lock even for the specific
  1016. * thread case (when we don't need to follow the group
  1017. * lists) in order to avoid races with "p->sighand"
  1018. * going away or changing from under us.
  1019. */
  1020. read_lock(&tasklist_lock);
  1021. spin_lock_irqsave(&p->sighand->siglock, flags);
  1022. ret = specific_send_sig_info(sig, info, p);
  1023. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1024. read_unlock(&tasklist_lock);
  1025. return ret;
  1026. }
  1027. #define __si_special(priv) \
  1028. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1029. int
  1030. send_sig(int sig, struct task_struct *p, int priv)
  1031. {
  1032. return send_sig_info(sig, __si_special(priv), p);
  1033. }
  1034. void
  1035. force_sig(int sig, struct task_struct *p)
  1036. {
  1037. force_sig_info(sig, SEND_SIG_PRIV, p);
  1038. }
  1039. /*
  1040. * When things go south during signal handling, we
  1041. * will force a SIGSEGV. And if the signal that caused
  1042. * the problem was already a SIGSEGV, we'll want to
  1043. * make sure we don't even try to deliver the signal..
  1044. */
  1045. int
  1046. force_sigsegv(int sig, struct task_struct *p)
  1047. {
  1048. if (sig == SIGSEGV) {
  1049. unsigned long flags;
  1050. spin_lock_irqsave(&p->sighand->siglock, flags);
  1051. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1052. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1053. }
  1054. force_sig(SIGSEGV, p);
  1055. return 0;
  1056. }
  1057. int kill_pgrp(struct pid *pid, int sig, int priv)
  1058. {
  1059. int ret;
  1060. read_lock(&tasklist_lock);
  1061. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1062. read_unlock(&tasklist_lock);
  1063. return ret;
  1064. }
  1065. EXPORT_SYMBOL(kill_pgrp);
  1066. int kill_pid(struct pid *pid, int sig, int priv)
  1067. {
  1068. return kill_pid_info(sig, __si_special(priv), pid);
  1069. }
  1070. EXPORT_SYMBOL(kill_pid);
  1071. int
  1072. kill_proc(pid_t pid, int sig, int priv)
  1073. {
  1074. int ret;
  1075. rcu_read_lock();
  1076. ret = kill_pid_info(sig, __si_special(priv), find_pid(pid));
  1077. rcu_read_unlock();
  1078. return ret;
  1079. }
  1080. /*
  1081. * These functions support sending signals using preallocated sigqueue
  1082. * structures. This is needed "because realtime applications cannot
  1083. * afford to lose notifications of asynchronous events, like timer
  1084. * expirations or I/O completions". In the case of Posix Timers
  1085. * we allocate the sigqueue structure from the timer_create. If this
  1086. * allocation fails we are able to report the failure to the application
  1087. * with an EAGAIN error.
  1088. */
  1089. struct sigqueue *sigqueue_alloc(void)
  1090. {
  1091. struct sigqueue *q;
  1092. if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
  1093. q->flags |= SIGQUEUE_PREALLOC;
  1094. return(q);
  1095. }
  1096. void sigqueue_free(struct sigqueue *q)
  1097. {
  1098. unsigned long flags;
  1099. spinlock_t *lock = &current->sighand->siglock;
  1100. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1101. /*
  1102. * If the signal is still pending remove it from the
  1103. * pending queue. We must hold ->siglock while testing
  1104. * q->list to serialize with collect_signal().
  1105. */
  1106. spin_lock_irqsave(lock, flags);
  1107. if (!list_empty(&q->list))
  1108. list_del_init(&q->list);
  1109. spin_unlock_irqrestore(lock, flags);
  1110. q->flags &= ~SIGQUEUE_PREALLOC;
  1111. __sigqueue_free(q);
  1112. }
  1113. static int do_send_sigqueue(int sig, struct sigqueue *q, struct task_struct *t,
  1114. struct sigpending *pending)
  1115. {
  1116. handle_stop_signal(sig, t);
  1117. if (unlikely(!list_empty(&q->list))) {
  1118. /*
  1119. * If an SI_TIMER entry is already queue just increment
  1120. * the overrun count.
  1121. */
  1122. BUG_ON(q->info.si_code != SI_TIMER);
  1123. q->info.si_overrun++;
  1124. return 0;
  1125. }
  1126. if (sig_ignored(t, sig))
  1127. return 1;
  1128. signalfd_notify(t, sig);
  1129. list_add_tail(&q->list, &pending->list);
  1130. sigaddset(&pending->signal, sig);
  1131. return 0;
  1132. }
  1133. int send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
  1134. {
  1135. unsigned long flags;
  1136. int ret = -1;
  1137. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1138. /*
  1139. * The rcu based delayed sighand destroy makes it possible to
  1140. * run this without tasklist lock held. The task struct itself
  1141. * cannot go away as create_timer did get_task_struct().
  1142. *
  1143. * We return -1, when the task is marked exiting, so
  1144. * posix_timer_event can redirect it to the group leader
  1145. */
  1146. if (!likely(lock_task_sighand(p, &flags)))
  1147. goto out_err;
  1148. ret = do_send_sigqueue(sig, q, p, &p->pending);
  1149. if (!sigismember(&p->blocked, sig))
  1150. signal_wake_up(p, sig == SIGKILL);
  1151. unlock_task_sighand(p, &flags);
  1152. out_err:
  1153. return ret;
  1154. }
  1155. int
  1156. send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
  1157. {
  1158. unsigned long flags;
  1159. int ret;
  1160. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1161. /* Since it_lock is held, p->sighand cannot be NULL. */
  1162. spin_lock_irqsave(&p->sighand->siglock, flags);
  1163. ret = do_send_sigqueue(sig, q, p, &p->signal->shared_pending);
  1164. __group_complete_signal(sig, p);
  1165. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1166. return ret;
  1167. }
  1168. /*
  1169. * Wake up any threads in the parent blocked in wait* syscalls.
  1170. */
  1171. static inline void __wake_up_parent(struct task_struct *p,
  1172. struct task_struct *parent)
  1173. {
  1174. wake_up_interruptible_sync(&parent->signal->wait_chldexit);
  1175. }
  1176. /*
  1177. * Let a parent know about the death of a child.
  1178. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1179. */
  1180. void do_notify_parent(struct task_struct *tsk, int sig)
  1181. {
  1182. struct siginfo info;
  1183. unsigned long flags;
  1184. struct sighand_struct *psig;
  1185. BUG_ON(sig == -1);
  1186. /* do_notify_parent_cldstop should have been called instead. */
  1187. BUG_ON(task_is_stopped_or_traced(tsk));
  1188. BUG_ON(!tsk->ptrace &&
  1189. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1190. info.si_signo = sig;
  1191. info.si_errno = 0;
  1192. /*
  1193. * we are under tasklist_lock here so our parent is tied to
  1194. * us and cannot exit and release its namespace.
  1195. *
  1196. * the only it can is to switch its nsproxy with sys_unshare,
  1197. * bu uncharing pid namespaces is not allowed, so we'll always
  1198. * see relevant namespace
  1199. *
  1200. * write_lock() currently calls preempt_disable() which is the
  1201. * same as rcu_read_lock(), but according to Oleg, this is not
  1202. * correct to rely on this
  1203. */
  1204. rcu_read_lock();
  1205. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1206. rcu_read_unlock();
  1207. info.si_uid = tsk->uid;
  1208. /* FIXME: find out whether or not this is supposed to be c*time. */
  1209. info.si_utime = cputime_to_jiffies(cputime_add(tsk->utime,
  1210. tsk->signal->utime));
  1211. info.si_stime = cputime_to_jiffies(cputime_add(tsk->stime,
  1212. tsk->signal->stime));
  1213. info.si_status = tsk->exit_code & 0x7f;
  1214. if (tsk->exit_code & 0x80)
  1215. info.si_code = CLD_DUMPED;
  1216. else if (tsk->exit_code & 0x7f)
  1217. info.si_code = CLD_KILLED;
  1218. else {
  1219. info.si_code = CLD_EXITED;
  1220. info.si_status = tsk->exit_code >> 8;
  1221. }
  1222. psig = tsk->parent->sighand;
  1223. spin_lock_irqsave(&psig->siglock, flags);
  1224. if (!tsk->ptrace && sig == SIGCHLD &&
  1225. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1226. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1227. /*
  1228. * We are exiting and our parent doesn't care. POSIX.1
  1229. * defines special semantics for setting SIGCHLD to SIG_IGN
  1230. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1231. * automatically and not left for our parent's wait4 call.
  1232. * Rather than having the parent do it as a magic kind of
  1233. * signal handler, we just set this to tell do_exit that we
  1234. * can be cleaned up without becoming a zombie. Note that
  1235. * we still call __wake_up_parent in this case, because a
  1236. * blocked sys_wait4 might now return -ECHILD.
  1237. *
  1238. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1239. * is implementation-defined: we do (if you don't want
  1240. * it, just use SIG_IGN instead).
  1241. */
  1242. tsk->exit_signal = -1;
  1243. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1244. sig = 0;
  1245. }
  1246. if (valid_signal(sig) && sig > 0)
  1247. __group_send_sig_info(sig, &info, tsk->parent);
  1248. __wake_up_parent(tsk, tsk->parent);
  1249. spin_unlock_irqrestore(&psig->siglock, flags);
  1250. }
  1251. static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
  1252. {
  1253. struct siginfo info;
  1254. unsigned long flags;
  1255. struct task_struct *parent;
  1256. struct sighand_struct *sighand;
  1257. if (tsk->ptrace & PT_PTRACED)
  1258. parent = tsk->parent;
  1259. else {
  1260. tsk = tsk->group_leader;
  1261. parent = tsk->real_parent;
  1262. }
  1263. info.si_signo = SIGCHLD;
  1264. info.si_errno = 0;
  1265. /*
  1266. * see comment in do_notify_parent() abot the following 3 lines
  1267. */
  1268. rcu_read_lock();
  1269. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1270. rcu_read_unlock();
  1271. info.si_uid = tsk->uid;
  1272. /* FIXME: find out whether or not this is supposed to be c*time. */
  1273. info.si_utime = cputime_to_jiffies(tsk->utime);
  1274. info.si_stime = cputime_to_jiffies(tsk->stime);
  1275. info.si_code = why;
  1276. switch (why) {
  1277. case CLD_CONTINUED:
  1278. info.si_status = SIGCONT;
  1279. break;
  1280. case CLD_STOPPED:
  1281. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1282. break;
  1283. case CLD_TRAPPED:
  1284. info.si_status = tsk->exit_code & 0x7f;
  1285. break;
  1286. default:
  1287. BUG();
  1288. }
  1289. sighand = parent->sighand;
  1290. spin_lock_irqsave(&sighand->siglock, flags);
  1291. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1292. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1293. __group_send_sig_info(SIGCHLD, &info, parent);
  1294. /*
  1295. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1296. */
  1297. __wake_up_parent(tsk, parent);
  1298. spin_unlock_irqrestore(&sighand->siglock, flags);
  1299. }
  1300. static inline int may_ptrace_stop(void)
  1301. {
  1302. if (!likely(current->ptrace & PT_PTRACED))
  1303. return 0;
  1304. /*
  1305. * Are we in the middle of do_coredump?
  1306. * If so and our tracer is also part of the coredump stopping
  1307. * is a deadlock situation, and pointless because our tracer
  1308. * is dead so don't allow us to stop.
  1309. * If SIGKILL was already sent before the caller unlocked
  1310. * ->siglock we must see ->core_waiters != 0. Otherwise it
  1311. * is safe to enter schedule().
  1312. */
  1313. if (unlikely(current->mm->core_waiters) &&
  1314. unlikely(current->mm == current->parent->mm))
  1315. return 0;
  1316. return 1;
  1317. }
  1318. /*
  1319. * Return nonzero if there is a SIGKILL that should be waking us up.
  1320. * Called with the siglock held.
  1321. */
  1322. static int sigkill_pending(struct task_struct *tsk)
  1323. {
  1324. return ((sigismember(&tsk->pending.signal, SIGKILL) ||
  1325. sigismember(&tsk->signal->shared_pending.signal, SIGKILL)) &&
  1326. !unlikely(sigismember(&tsk->blocked, SIGKILL)));
  1327. }
  1328. /*
  1329. * This must be called with current->sighand->siglock held.
  1330. *
  1331. * This should be the path for all ptrace stops.
  1332. * We always set current->last_siginfo while stopped here.
  1333. * That makes it a way to test a stopped process for
  1334. * being ptrace-stopped vs being job-control-stopped.
  1335. *
  1336. * If we actually decide not to stop at all because the tracer
  1337. * is gone, we keep current->exit_code unless clear_code.
  1338. */
  1339. static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
  1340. {
  1341. int killed = 0;
  1342. if (arch_ptrace_stop_needed(exit_code, info)) {
  1343. /*
  1344. * The arch code has something special to do before a
  1345. * ptrace stop. This is allowed to block, e.g. for faults
  1346. * on user stack pages. We can't keep the siglock while
  1347. * calling arch_ptrace_stop, so we must release it now.
  1348. * To preserve proper semantics, we must do this before
  1349. * any signal bookkeeping like checking group_stop_count.
  1350. * Meanwhile, a SIGKILL could come in before we retake the
  1351. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1352. * So after regaining the lock, we must check for SIGKILL.
  1353. */
  1354. spin_unlock_irq(&current->sighand->siglock);
  1355. arch_ptrace_stop(exit_code, info);
  1356. spin_lock_irq(&current->sighand->siglock);
  1357. killed = sigkill_pending(current);
  1358. }
  1359. /*
  1360. * If there is a group stop in progress,
  1361. * we must participate in the bookkeeping.
  1362. */
  1363. if (current->signal->group_stop_count > 0)
  1364. --current->signal->group_stop_count;
  1365. current->last_siginfo = info;
  1366. current->exit_code = exit_code;
  1367. /* Let the debugger run. */
  1368. __set_current_state(TASK_TRACED);
  1369. spin_unlock_irq(&current->sighand->siglock);
  1370. read_lock(&tasklist_lock);
  1371. if (!unlikely(killed) && may_ptrace_stop()) {
  1372. do_notify_parent_cldstop(current, CLD_TRAPPED);
  1373. read_unlock(&tasklist_lock);
  1374. schedule();
  1375. } else {
  1376. /*
  1377. * By the time we got the lock, our tracer went away.
  1378. * Don't drop the lock yet, another tracer may come.
  1379. */
  1380. __set_current_state(TASK_RUNNING);
  1381. if (clear_code)
  1382. current->exit_code = 0;
  1383. read_unlock(&tasklist_lock);
  1384. }
  1385. /*
  1386. * While in TASK_TRACED, we were considered "frozen enough".
  1387. * Now that we woke up, it's crucial if we're supposed to be
  1388. * frozen that we freeze now before running anything substantial.
  1389. */
  1390. try_to_freeze();
  1391. /*
  1392. * We are back. Now reacquire the siglock before touching
  1393. * last_siginfo, so that we are sure to have synchronized with
  1394. * any signal-sending on another CPU that wants to examine it.
  1395. */
  1396. spin_lock_irq(&current->sighand->siglock);
  1397. current->last_siginfo = NULL;
  1398. /*
  1399. * Queued signals ignored us while we were stopped for tracing.
  1400. * So check for any that we should take before resuming user mode.
  1401. * This sets TIF_SIGPENDING, but never clears it.
  1402. */
  1403. recalc_sigpending_tsk(current);
  1404. }
  1405. void ptrace_notify(int exit_code)
  1406. {
  1407. siginfo_t info;
  1408. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1409. memset(&info, 0, sizeof info);
  1410. info.si_signo = SIGTRAP;
  1411. info.si_code = exit_code;
  1412. info.si_pid = task_pid_vnr(current);
  1413. info.si_uid = current->uid;
  1414. /* Let the debugger run. */
  1415. spin_lock_irq(&current->sighand->siglock);
  1416. ptrace_stop(exit_code, 1, &info);
  1417. spin_unlock_irq(&current->sighand->siglock);
  1418. }
  1419. static void
  1420. finish_stop(int stop_count)
  1421. {
  1422. /*
  1423. * If there are no other threads in the group, or if there is
  1424. * a group stop in progress and we are the last to stop,
  1425. * report to the parent. When ptraced, every thread reports itself.
  1426. */
  1427. if (stop_count == 0 || (current->ptrace & PT_PTRACED)) {
  1428. read_lock(&tasklist_lock);
  1429. do_notify_parent_cldstop(current, CLD_STOPPED);
  1430. read_unlock(&tasklist_lock);
  1431. }
  1432. do {
  1433. schedule();
  1434. } while (try_to_freeze());
  1435. /*
  1436. * Now we don't run again until continued.
  1437. */
  1438. current->exit_code = 0;
  1439. }
  1440. /*
  1441. * This performs the stopping for SIGSTOP and other stop signals.
  1442. * We have to stop all threads in the thread group.
  1443. * Returns nonzero if we've actually stopped and released the siglock.
  1444. * Returns zero if we didn't stop and still hold the siglock.
  1445. */
  1446. static int do_signal_stop(int signr)
  1447. {
  1448. struct signal_struct *sig = current->signal;
  1449. int stop_count;
  1450. if (sig->group_stop_count > 0) {
  1451. /*
  1452. * There is a group stop in progress. We don't need to
  1453. * start another one.
  1454. */
  1455. stop_count = --sig->group_stop_count;
  1456. } else {
  1457. struct task_struct *t;
  1458. if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
  1459. unlikely(signal_group_exit(sig)))
  1460. return 0;
  1461. /*
  1462. * There is no group stop already in progress.
  1463. * We must initiate one now.
  1464. */
  1465. sig->group_exit_code = signr;
  1466. stop_count = 0;
  1467. for (t = next_thread(current); t != current; t = next_thread(t))
  1468. /*
  1469. * Setting state to TASK_STOPPED for a group
  1470. * stop is always done with the siglock held,
  1471. * so this check has no races.
  1472. */
  1473. if (!(t->flags & PF_EXITING) &&
  1474. !task_is_stopped_or_traced(t)) {
  1475. stop_count++;
  1476. signal_wake_up(t, 0);
  1477. }
  1478. sig->group_stop_count = stop_count;
  1479. }
  1480. if (stop_count == 0)
  1481. sig->flags = SIGNAL_STOP_STOPPED;
  1482. current->exit_code = sig->group_exit_code;
  1483. __set_current_state(TASK_STOPPED);
  1484. spin_unlock_irq(&current->sighand->siglock);
  1485. finish_stop(stop_count);
  1486. return 1;
  1487. }
  1488. static int ptrace_signal(int signr, siginfo_t *info,
  1489. struct pt_regs *regs, void *cookie)
  1490. {
  1491. if (!(current->ptrace & PT_PTRACED))
  1492. return signr;
  1493. ptrace_signal_deliver(regs, cookie);
  1494. /* Let the debugger run. */
  1495. ptrace_stop(signr, 0, info);
  1496. /* We're back. Did the debugger cancel the sig? */
  1497. signr = current->exit_code;
  1498. if (signr == 0)
  1499. return signr;
  1500. current->exit_code = 0;
  1501. /* Update the siginfo structure if the signal has
  1502. changed. If the debugger wanted something
  1503. specific in the siginfo structure then it should
  1504. have updated *info via PTRACE_SETSIGINFO. */
  1505. if (signr != info->si_signo) {
  1506. info->si_signo = signr;
  1507. info->si_errno = 0;
  1508. info->si_code = SI_USER;
  1509. info->si_pid = task_pid_vnr(current->parent);
  1510. info->si_uid = current->parent->uid;
  1511. }
  1512. /* If the (new) signal is now blocked, requeue it. */
  1513. if (sigismember(&current->blocked, signr)) {
  1514. specific_send_sig_info(signr, info, current);
  1515. signr = 0;
  1516. }
  1517. return signr;
  1518. }
  1519. int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
  1520. struct pt_regs *regs, void *cookie)
  1521. {
  1522. struct sighand_struct *sighand = current->sighand;
  1523. struct signal_struct *signal = current->signal;
  1524. int signr;
  1525. relock:
  1526. /*
  1527. * We'll jump back here after any time we were stopped in TASK_STOPPED.
  1528. * While in TASK_STOPPED, we were considered "frozen enough".
  1529. * Now that we woke up, it's crucial if we're supposed to be
  1530. * frozen that we freeze now before running anything substantial.
  1531. */
  1532. try_to_freeze();
  1533. spin_lock_irq(&sighand->siglock);
  1534. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  1535. int why = (signal->flags & SIGNAL_STOP_CONTINUED)
  1536. ? CLD_CONTINUED : CLD_STOPPED;
  1537. signal->flags &= ~SIGNAL_CLD_MASK;
  1538. spin_unlock_irq(&sighand->siglock);
  1539. read_lock(&tasklist_lock);
  1540. do_notify_parent_cldstop(current->group_leader, why);
  1541. read_unlock(&tasklist_lock);
  1542. goto relock;
  1543. }
  1544. for (;;) {
  1545. struct k_sigaction *ka;
  1546. if (unlikely(signal->group_stop_count > 0) &&
  1547. do_signal_stop(0))
  1548. goto relock;
  1549. signr = dequeue_signal(current, &current->blocked, info);
  1550. if (!signr)
  1551. break; /* will return 0 */
  1552. if (signr != SIGKILL) {
  1553. signr = ptrace_signal(signr, info, regs, cookie);
  1554. if (!signr)
  1555. continue;
  1556. }
  1557. ka = &sighand->action[signr-1];
  1558. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1559. continue;
  1560. if (ka->sa.sa_handler != SIG_DFL) {
  1561. /* Run the handler. */
  1562. *return_ka = *ka;
  1563. if (ka->sa.sa_flags & SA_ONESHOT)
  1564. ka->sa.sa_handler = SIG_DFL;
  1565. break; /* will return non-zero "signr" value */
  1566. }
  1567. /*
  1568. * Now we are doing the default action for this signal.
  1569. */
  1570. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  1571. continue;
  1572. /*
  1573. * Global init gets no signals it doesn't want.
  1574. */
  1575. if (is_global_init(current))
  1576. continue;
  1577. if (sig_kernel_stop(signr)) {
  1578. /*
  1579. * The default action is to stop all threads in
  1580. * the thread group. The job control signals
  1581. * do nothing in an orphaned pgrp, but SIGSTOP
  1582. * always works. Note that siglock needs to be
  1583. * dropped during the call to is_orphaned_pgrp()
  1584. * because of lock ordering with tasklist_lock.
  1585. * This allows an intervening SIGCONT to be posted.
  1586. * We need to check for that and bail out if necessary.
  1587. */
  1588. if (signr != SIGSTOP) {
  1589. spin_unlock_irq(&sighand->siglock);
  1590. /* signals can be posted during this window */
  1591. if (is_current_pgrp_orphaned())
  1592. goto relock;
  1593. spin_lock_irq(&sighand->siglock);
  1594. }
  1595. if (likely(do_signal_stop(signr))) {
  1596. /* It released the siglock. */
  1597. goto relock;
  1598. }
  1599. /*
  1600. * We didn't actually stop, due to a race
  1601. * with SIGCONT or something like that.
  1602. */
  1603. continue;
  1604. }
  1605. spin_unlock_irq(&sighand->siglock);
  1606. /*
  1607. * Anything else is fatal, maybe with a core dump.
  1608. */
  1609. current->flags |= PF_SIGNALED;
  1610. if ((signr != SIGKILL) && print_fatal_signals)
  1611. print_fatal_signal(regs, signr);
  1612. if (sig_kernel_coredump(signr)) {
  1613. /*
  1614. * If it was able to dump core, this kills all
  1615. * other threads in the group and synchronizes with
  1616. * their demise. If we lost the race with another
  1617. * thread getting here, it set group_exit_code
  1618. * first and our do_group_exit call below will use
  1619. * that value and ignore the one we pass it.
  1620. */
  1621. do_coredump((long)signr, signr, regs);
  1622. }
  1623. /*
  1624. * Death signals, no core dump.
  1625. */
  1626. do_group_exit(signr);
  1627. /* NOTREACHED */
  1628. }
  1629. spin_unlock_irq(&sighand->siglock);
  1630. return signr;
  1631. }
  1632. void exit_signals(struct task_struct *tsk)
  1633. {
  1634. int group_stop = 0;
  1635. struct task_struct *t;
  1636. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  1637. tsk->flags |= PF_EXITING;
  1638. return;
  1639. }
  1640. spin_lock_irq(&tsk->sighand->siglock);
  1641. /*
  1642. * From now this task is not visible for group-wide signals,
  1643. * see wants_signal(), do_signal_stop().
  1644. */
  1645. tsk->flags |= PF_EXITING;
  1646. if (!signal_pending(tsk))
  1647. goto out;
  1648. /* It could be that __group_complete_signal() choose us to
  1649. * notify about group-wide signal. Another thread should be
  1650. * woken now to take the signal since we will not.
  1651. */
  1652. for (t = tsk; (t = next_thread(t)) != tsk; )
  1653. if (!signal_pending(t) && !(t->flags & PF_EXITING))
  1654. recalc_sigpending_and_wake(t);
  1655. if (unlikely(tsk->signal->group_stop_count) &&
  1656. !--tsk->signal->group_stop_count) {
  1657. tsk->signal->flags = SIGNAL_STOP_STOPPED;
  1658. group_stop = 1;
  1659. }
  1660. out:
  1661. spin_unlock_irq(&tsk->sighand->siglock);
  1662. if (unlikely(group_stop)) {
  1663. read_lock(&tasklist_lock);
  1664. do_notify_parent_cldstop(tsk, CLD_STOPPED);
  1665. read_unlock(&tasklist_lock);
  1666. }
  1667. }
  1668. EXPORT_SYMBOL(recalc_sigpending);
  1669. EXPORT_SYMBOL_GPL(dequeue_signal);
  1670. EXPORT_SYMBOL(flush_signals);
  1671. EXPORT_SYMBOL(force_sig);
  1672. EXPORT_SYMBOL(kill_proc);
  1673. EXPORT_SYMBOL(ptrace_notify);
  1674. EXPORT_SYMBOL(send_sig);
  1675. EXPORT_SYMBOL(send_sig_info);
  1676. EXPORT_SYMBOL(sigprocmask);
  1677. EXPORT_SYMBOL(block_all_signals);
  1678. EXPORT_SYMBOL(unblock_all_signals);
  1679. /*
  1680. * System call entry points.
  1681. */
  1682. asmlinkage long sys_restart_syscall(void)
  1683. {
  1684. struct restart_block *restart = &current_thread_info()->restart_block;
  1685. return restart->fn(restart);
  1686. }
  1687. long do_no_restart_syscall(struct restart_block *param)
  1688. {
  1689. return -EINTR;
  1690. }
  1691. /*
  1692. * We don't need to get the kernel lock - this is all local to this
  1693. * particular thread.. (and that's good, because this is _heavily_
  1694. * used by various programs)
  1695. */
  1696. /*
  1697. * This is also useful for kernel threads that want to temporarily
  1698. * (or permanently) block certain signals.
  1699. *
  1700. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  1701. * interface happily blocks "unblockable" signals like SIGKILL
  1702. * and friends.
  1703. */
  1704. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  1705. {
  1706. int error;
  1707. spin_lock_irq(&current->sighand->siglock);
  1708. if (oldset)
  1709. *oldset = current->blocked;
  1710. error = 0;
  1711. switch (how) {
  1712. case SIG_BLOCK:
  1713. sigorsets(&current->blocked, &current->blocked, set);
  1714. break;
  1715. case SIG_UNBLOCK:
  1716. signandsets(&current->blocked, &current->blocked, set);
  1717. break;
  1718. case SIG_SETMASK:
  1719. current->blocked = *set;
  1720. break;
  1721. default:
  1722. error = -EINVAL;
  1723. }
  1724. recalc_sigpending();
  1725. spin_unlock_irq(&current->sighand->siglock);
  1726. return error;
  1727. }
  1728. asmlinkage long
  1729. sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
  1730. {
  1731. int error = -EINVAL;
  1732. sigset_t old_set, new_set;
  1733. /* XXX: Don't preclude handling different sized sigset_t's. */
  1734. if (sigsetsize != sizeof(sigset_t))
  1735. goto out;
  1736. if (set) {
  1737. error = -EFAULT;
  1738. if (copy_from_user(&new_set, set, sizeof(*set)))
  1739. goto out;
  1740. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1741. error = sigprocmask(how, &new_set, &old_set);
  1742. if (error)
  1743. goto out;
  1744. if (oset)
  1745. goto set_old;
  1746. } else if (oset) {
  1747. spin_lock_irq(&current->sighand->siglock);
  1748. old_set = current->blocked;
  1749. spin_unlock_irq(&current->sighand->siglock);
  1750. set_old:
  1751. error = -EFAULT;
  1752. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  1753. goto out;
  1754. }
  1755. error = 0;
  1756. out:
  1757. return error;
  1758. }
  1759. long do_sigpending(void __user *set, unsigned long sigsetsize)
  1760. {
  1761. long error = -EINVAL;
  1762. sigset_t pending;
  1763. if (sigsetsize > sizeof(sigset_t))
  1764. goto out;
  1765. spin_lock_irq(&current->sighand->siglock);
  1766. sigorsets(&pending, &current->pending.signal,
  1767. &current->signal->shared_pending.signal);
  1768. spin_unlock_irq(&current->sighand->siglock);
  1769. /* Outside the lock because only this thread touches it. */
  1770. sigandsets(&pending, &current->blocked, &pending);
  1771. error = -EFAULT;
  1772. if (!copy_to_user(set, &pending, sigsetsize))
  1773. error = 0;
  1774. out:
  1775. return error;
  1776. }
  1777. asmlinkage long
  1778. sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
  1779. {
  1780. return do_sigpending(set, sigsetsize);
  1781. }
  1782. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  1783. int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
  1784. {
  1785. int err;
  1786. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  1787. return -EFAULT;
  1788. if (from->si_code < 0)
  1789. return __copy_to_user(to, from, sizeof(siginfo_t))
  1790. ? -EFAULT : 0;
  1791. /*
  1792. * If you change siginfo_t structure, please be sure
  1793. * this code is fixed accordingly.
  1794. * Please remember to update the signalfd_copyinfo() function
  1795. * inside fs/signalfd.c too, in case siginfo_t changes.
  1796. * It should never copy any pad contained in the structure
  1797. * to avoid security leaks, but must copy the generic
  1798. * 3 ints plus the relevant union member.
  1799. */
  1800. err = __put_user(from->si_signo, &to->si_signo);
  1801. err |= __put_user(from->si_errno, &to->si_errno);
  1802. err |= __put_user((short)from->si_code, &to->si_code);
  1803. switch (from->si_code & __SI_MASK) {
  1804. case __SI_KILL:
  1805. err |= __put_user(from->si_pid, &to->si_pid);
  1806. err |= __put_user(from->si_uid, &to->si_uid);
  1807. break;
  1808. case __SI_TIMER:
  1809. err |= __put_user(from->si_tid, &to->si_tid);
  1810. err |= __put_user(from->si_overrun, &to->si_overrun);
  1811. err |= __put_user(from->si_ptr, &to->si_ptr);
  1812. break;
  1813. case __SI_POLL:
  1814. err |= __put_user(from->si_band, &to->si_band);
  1815. err |= __put_user(from->si_fd, &to->si_fd);
  1816. break;
  1817. case __SI_FAULT:
  1818. err |= __put_user(from->si_addr, &to->si_addr);
  1819. #ifdef __ARCH_SI_TRAPNO
  1820. err |= __put_user(from->si_trapno, &to->si_trapno);
  1821. #endif
  1822. break;
  1823. case __SI_CHLD:
  1824. err |= __put_user(from->si_pid, &to->si_pid);
  1825. err |= __put_user(from->si_uid, &to->si_uid);
  1826. err |= __put_user(from->si_status, &to->si_status);
  1827. err |= __put_user(from->si_utime, &to->si_utime);
  1828. err |= __put_user(from->si_stime, &to->si_stime);
  1829. break;
  1830. case __SI_RT: /* This is not generated by the kernel as of now. */
  1831. case __SI_MESGQ: /* But this is */
  1832. err |= __put_user(from->si_pid, &to->si_pid);
  1833. err |= __put_user(from->si_uid, &to->si_uid);
  1834. err |= __put_user(from->si_ptr, &to->si_ptr);
  1835. break;
  1836. default: /* this is just in case for now ... */
  1837. err |= __put_user(from->si_pid, &to->si_pid);
  1838. err |= __put_user(from->si_uid, &to->si_uid);
  1839. break;
  1840. }
  1841. return err;
  1842. }
  1843. #endif
  1844. asmlinkage long
  1845. sys_rt_sigtimedwait(const sigset_t __user *uthese,
  1846. siginfo_t __user *uinfo,
  1847. const struct timespec __user *uts,
  1848. size_t sigsetsize)
  1849. {
  1850. int ret, sig;
  1851. sigset_t these;
  1852. struct timespec ts;
  1853. siginfo_t info;
  1854. long timeout = 0;
  1855. /* XXX: Don't preclude handling different sized sigset_t's. */
  1856. if (sigsetsize != sizeof(sigset_t))
  1857. return -EINVAL;
  1858. if (copy_from_user(&these, uthese, sizeof(these)))
  1859. return -EFAULT;
  1860. /*
  1861. * Invert the set of allowed signals to get those we
  1862. * want to block.
  1863. */
  1864. sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1865. signotset(&these);
  1866. if (uts) {
  1867. if (copy_from_user(&ts, uts, sizeof(ts)))
  1868. return -EFAULT;
  1869. if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
  1870. || ts.tv_sec < 0)
  1871. return -EINVAL;
  1872. }
  1873. spin_lock_irq(&current->sighand->siglock);
  1874. sig = dequeue_signal(current, &these, &info);
  1875. if (!sig) {
  1876. timeout = MAX_SCHEDULE_TIMEOUT;
  1877. if (uts)
  1878. timeout = (timespec_to_jiffies(&ts)
  1879. + (ts.tv_sec || ts.tv_nsec));
  1880. if (timeout) {
  1881. /* None ready -- temporarily unblock those we're
  1882. * interested while we are sleeping in so that we'll
  1883. * be awakened when they arrive. */
  1884. current->real_blocked = current->blocked;
  1885. sigandsets(&current->blocked, &current->blocked, &these);
  1886. recalc_sigpending();
  1887. spin_unlock_irq(&current->sighand->siglock);
  1888. timeout = schedule_timeout_interruptible(timeout);
  1889. spin_lock_irq(&current->sighand->siglock);
  1890. sig = dequeue_signal(current, &these, &info);
  1891. current->blocked = current->real_blocked;
  1892. siginitset(&current->real_blocked, 0);
  1893. recalc_sigpending();
  1894. }
  1895. }
  1896. spin_unlock_irq(&current->sighand->siglock);
  1897. if (sig) {
  1898. ret = sig;
  1899. if (uinfo) {
  1900. if (copy_siginfo_to_user(uinfo, &info))
  1901. ret = -EFAULT;
  1902. }
  1903. } else {
  1904. ret = -EAGAIN;
  1905. if (timeout)
  1906. ret = -EINTR;
  1907. }
  1908. return ret;
  1909. }
  1910. asmlinkage long
  1911. sys_kill(int pid, int sig)
  1912. {
  1913. struct siginfo info;
  1914. info.si_signo = sig;
  1915. info.si_errno = 0;
  1916. info.si_code = SI_USER;
  1917. info.si_pid = task_tgid_vnr(current);
  1918. info.si_uid = current->uid;
  1919. return kill_something_info(sig, &info, pid);
  1920. }
  1921. static int do_tkill(int tgid, int pid, int sig)
  1922. {
  1923. int error;
  1924. struct siginfo info;
  1925. struct task_struct *p;
  1926. error = -ESRCH;
  1927. info.si_signo = sig;
  1928. info.si_errno = 0;
  1929. info.si_code = SI_TKILL;
  1930. info.si_pid = task_tgid_vnr(current);
  1931. info.si_uid = current->uid;
  1932. read_lock(&tasklist_lock);
  1933. p = find_task_by_vpid(pid);
  1934. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  1935. error = check_kill_permission(sig, &info, p);
  1936. /*
  1937. * The null signal is a permissions and process existence
  1938. * probe. No signal is actually delivered.
  1939. */
  1940. if (!error && sig && p->sighand) {
  1941. spin_lock_irq(&p->sighand->siglock);
  1942. error = specific_send_sig_info(sig, &info, p);
  1943. spin_unlock_irq(&p->sighand->siglock);
  1944. }
  1945. }
  1946. read_unlock(&tasklist_lock);
  1947. return error;
  1948. }
  1949. /**
  1950. * sys_tgkill - send signal to one specific thread
  1951. * @tgid: the thread group ID of the thread
  1952. * @pid: the PID of the thread
  1953. * @sig: signal to be sent
  1954. *
  1955. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  1956. * exists but it's not belonging to the target process anymore. This
  1957. * method solves the problem of threads exiting and PIDs getting reused.
  1958. */
  1959. asmlinkage long sys_tgkill(int tgid, int pid, int sig)
  1960. {
  1961. /* This is only valid for single tasks */
  1962. if (pid <= 0 || tgid <= 0)
  1963. return -EINVAL;
  1964. return do_tkill(tgid, pid, sig);
  1965. }
  1966. /*
  1967. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  1968. */
  1969. asmlinkage long
  1970. sys_tkill(int pid, int sig)
  1971. {
  1972. /* This is only valid for single tasks */
  1973. if (pid <= 0)
  1974. return -EINVAL;
  1975. return do_tkill(0, pid, sig);
  1976. }
  1977. asmlinkage long
  1978. sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
  1979. {
  1980. siginfo_t info;
  1981. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  1982. return -EFAULT;
  1983. /* Not even root can pretend to send signals from the kernel.
  1984. Nor can they impersonate a kill(), which adds source info. */
  1985. if (info.si_code >= 0)
  1986. return -EPERM;
  1987. info.si_signo = sig;
  1988. /* POSIX.1b doesn't mention process groups. */
  1989. return kill_proc_info(sig, &info, pid);
  1990. }
  1991. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  1992. {
  1993. struct task_struct *t = current;
  1994. struct k_sigaction *k;
  1995. sigset_t mask;
  1996. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  1997. return -EINVAL;
  1998. k = &t->sighand->action[sig-1];
  1999. spin_lock_irq(&current->sighand->siglock);
  2000. if (oact)
  2001. *oact = *k;
  2002. if (act) {
  2003. sigdelsetmask(&act->sa.sa_mask,
  2004. sigmask(SIGKILL) | sigmask(SIGSTOP));
  2005. *k = *act;
  2006. /*
  2007. * POSIX 3.3.1.3:
  2008. * "Setting a signal action to SIG_IGN for a signal that is
  2009. * pending shall cause the pending signal to be discarded,
  2010. * whether or not it is blocked."
  2011. *
  2012. * "Setting a signal action to SIG_DFL for a signal that is
  2013. * pending and whose default action is to ignore the signal
  2014. * (for example, SIGCHLD), shall cause the pending signal to
  2015. * be discarded, whether or not it is blocked"
  2016. */
  2017. if (__sig_ignored(t, sig)) {
  2018. sigemptyset(&mask);
  2019. sigaddset(&mask, sig);
  2020. rm_from_queue_full(&mask, &t->signal->shared_pending);
  2021. do {
  2022. rm_from_queue_full(&mask, &t->pending);
  2023. t = next_thread(t);
  2024. } while (t != current);
  2025. }
  2026. }
  2027. spin_unlock_irq(&current->sighand->siglock);
  2028. return 0;
  2029. }
  2030. int
  2031. do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
  2032. {
  2033. stack_t oss;
  2034. int error;
  2035. if (uoss) {
  2036. oss.ss_sp = (void __user *) current->sas_ss_sp;
  2037. oss.ss_size = current->sas_ss_size;
  2038. oss.ss_flags = sas_ss_flags(sp);
  2039. }
  2040. if (uss) {
  2041. void __user *ss_sp;
  2042. size_t ss_size;
  2043. int ss_flags;
  2044. error = -EFAULT;
  2045. if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
  2046. || __get_user(ss_sp, &uss->ss_sp)
  2047. || __get_user(ss_flags, &uss->ss_flags)
  2048. || __get_user(ss_size, &uss->ss_size))
  2049. goto out;
  2050. error = -EPERM;
  2051. if (on_sig_stack(sp))
  2052. goto out;
  2053. error = -EINVAL;
  2054. /*
  2055. *
  2056. * Note - this code used to test ss_flags incorrectly
  2057. * old code may have been written using ss_flags==0
  2058. * to mean ss_flags==SS_ONSTACK (as this was the only
  2059. * way that worked) - this fix preserves that older
  2060. * mechanism
  2061. */
  2062. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  2063. goto out;
  2064. if (ss_flags == SS_DISABLE) {
  2065. ss_size = 0;
  2066. ss_sp = NULL;
  2067. } else {
  2068. error = -ENOMEM;
  2069. if (ss_size < MINSIGSTKSZ)
  2070. goto out;
  2071. }
  2072. current->sas_ss_sp = (unsigned long) ss_sp;
  2073. current->sas_ss_size = ss_size;
  2074. }
  2075. if (uoss) {
  2076. error = -EFAULT;
  2077. if (copy_to_user(uoss, &oss, sizeof(oss)))
  2078. goto out;
  2079. }
  2080. error = 0;
  2081. out:
  2082. return error;
  2083. }
  2084. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2085. asmlinkage long
  2086. sys_sigpending(old_sigset_t __user *set)
  2087. {
  2088. return do_sigpending(set, sizeof(*set));
  2089. }
  2090. #endif
  2091. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2092. /* Some platforms have their own version with special arguments others
  2093. support only sys_rt_sigprocmask. */
  2094. asmlinkage long
  2095. sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
  2096. {
  2097. int error;
  2098. old_sigset_t old_set, new_set;
  2099. if (set) {
  2100. error = -EFAULT;
  2101. if (copy_from_user(&new_set, set, sizeof(*set)))
  2102. goto out;
  2103. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  2104. spin_lock_irq(&current->sighand->siglock);
  2105. old_set = current->blocked.sig[0];
  2106. error = 0;
  2107. switch (how) {
  2108. default:
  2109. error = -EINVAL;
  2110. break;
  2111. case SIG_BLOCK:
  2112. sigaddsetmask(&current->blocked, new_set);
  2113. break;
  2114. case SIG_UNBLOCK:
  2115. sigdelsetmask(&current->blocked, new_set);
  2116. break;
  2117. case SIG_SETMASK:
  2118. current->blocked.sig[0] = new_set;
  2119. break;
  2120. }
  2121. recalc_sigpending();
  2122. spin_unlock_irq(&current->sighand->siglock);
  2123. if (error)
  2124. goto out;
  2125. if (oset)
  2126. goto set_old;
  2127. } else if (oset) {
  2128. old_set = current->blocked.sig[0];
  2129. set_old:
  2130. error = -EFAULT;
  2131. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2132. goto out;
  2133. }
  2134. error = 0;
  2135. out:
  2136. return error;
  2137. }
  2138. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2139. #ifdef __ARCH_WANT_SYS_RT_SIGACTION
  2140. asmlinkage long
  2141. sys_rt_sigaction(int sig,
  2142. const struct sigaction __user *act,
  2143. struct sigaction __user *oact,
  2144. size_t sigsetsize)
  2145. {
  2146. struct k_sigaction new_sa, old_sa;
  2147. int ret = -EINVAL;
  2148. /* XXX: Don't preclude handling different sized sigset_t's. */
  2149. if (sigsetsize != sizeof(sigset_t))
  2150. goto out;
  2151. if (act) {
  2152. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2153. return -EFAULT;
  2154. }
  2155. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2156. if (!ret && oact) {
  2157. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2158. return -EFAULT;
  2159. }
  2160. out:
  2161. return ret;
  2162. }
  2163. #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
  2164. #ifdef __ARCH_WANT_SYS_SGETMASK
  2165. /*
  2166. * For backwards compatibility. Functionality superseded by sigprocmask.
  2167. */
  2168. asmlinkage long
  2169. sys_sgetmask(void)
  2170. {
  2171. /* SMP safe */
  2172. return current->blocked.sig[0];
  2173. }
  2174. asmlinkage long
  2175. sys_ssetmask(int newmask)
  2176. {
  2177. int old;
  2178. spin_lock_irq(&current->sighand->siglock);
  2179. old = current->blocked.sig[0];
  2180. siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
  2181. sigmask(SIGSTOP)));
  2182. recalc_sigpending();
  2183. spin_unlock_irq(&current->sighand->siglock);
  2184. return old;
  2185. }
  2186. #endif /* __ARCH_WANT_SGETMASK */
  2187. #ifdef __ARCH_WANT_SYS_SIGNAL
  2188. /*
  2189. * For backwards compatibility. Functionality superseded by sigaction.
  2190. */
  2191. asmlinkage unsigned long
  2192. sys_signal(int sig, __sighandler_t handler)
  2193. {
  2194. struct k_sigaction new_sa, old_sa;
  2195. int ret;
  2196. new_sa.sa.sa_handler = handler;
  2197. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  2198. sigemptyset(&new_sa.sa.sa_mask);
  2199. ret = do_sigaction(sig, &new_sa, &old_sa);
  2200. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  2201. }
  2202. #endif /* __ARCH_WANT_SYS_SIGNAL */
  2203. #ifdef __ARCH_WANT_SYS_PAUSE
  2204. asmlinkage long
  2205. sys_pause(void)
  2206. {
  2207. current->state = TASK_INTERRUPTIBLE;
  2208. schedule();
  2209. return -ERESTARTNOHAND;
  2210. }
  2211. #endif
  2212. #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
  2213. asmlinkage long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
  2214. {
  2215. sigset_t newset;
  2216. /* XXX: Don't preclude handling different sized sigset_t's. */
  2217. if (sigsetsize != sizeof(sigset_t))
  2218. return -EINVAL;
  2219. if (copy_from_user(&newset, unewset, sizeof(newset)))
  2220. return -EFAULT;
  2221. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2222. spin_lock_irq(&current->sighand->siglock);
  2223. current->saved_sigmask = current->blocked;
  2224. current->blocked = newset;
  2225. recalc_sigpending();
  2226. spin_unlock_irq(&current->sighand->siglock);
  2227. current->state = TASK_INTERRUPTIBLE;
  2228. schedule();
  2229. set_thread_flag(TIF_RESTORE_SIGMASK);
  2230. return -ERESTARTNOHAND;
  2231. }
  2232. #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
  2233. __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
  2234. {
  2235. return NULL;
  2236. }
  2237. void __init signals_init(void)
  2238. {
  2239. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  2240. }