signal.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552
  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. /*
  576. * Test if P wants to take SIG. After we've checked all threads with this,
  577. * it's equivalent to finding no threads not blocking SIG. Any threads not
  578. * blocking SIG were ruled out because they are not running and already
  579. * have pending signals. Such threads will dequeue from the shared queue
  580. * as soon as they're available, so putting the signal on the shared queue
  581. * will be equivalent to sending it to one such thread.
  582. */
  583. static inline int wants_signal(int sig, struct task_struct *p)
  584. {
  585. if (sigismember(&p->blocked, sig))
  586. return 0;
  587. if (p->flags & PF_EXITING)
  588. return 0;
  589. if (sig == SIGKILL)
  590. return 1;
  591. if (task_is_stopped_or_traced(p))
  592. return 0;
  593. return task_curr(p) || !signal_pending(p);
  594. }
  595. static void complete_signal(int sig, struct task_struct *p, int group)
  596. {
  597. struct signal_struct *signal = p->signal;
  598. struct task_struct *t;
  599. /*
  600. * Now find a thread we can wake up to take the signal off the queue.
  601. *
  602. * If the main thread wants the signal, it gets first crack.
  603. * Probably the least surprising to the average bear.
  604. */
  605. if (wants_signal(sig, p))
  606. t = p;
  607. else if (!group || thread_group_empty(p))
  608. /*
  609. * There is just one thread and it does not need to be woken.
  610. * It will dequeue unblocked signals before it runs again.
  611. */
  612. return;
  613. else {
  614. /*
  615. * Otherwise try to find a suitable thread.
  616. */
  617. t = signal->curr_target;
  618. while (!wants_signal(sig, t)) {
  619. t = next_thread(t);
  620. if (t == signal->curr_target)
  621. /*
  622. * No thread needs to be woken.
  623. * Any eligible threads will see
  624. * the signal in the queue soon.
  625. */
  626. return;
  627. }
  628. signal->curr_target = t;
  629. }
  630. /*
  631. * Found a killable thread. If the signal will be fatal,
  632. * then start taking the whole group down immediately.
  633. */
  634. if (sig_fatal(p, sig) && !(signal->flags & SIGNAL_GROUP_EXIT) &&
  635. !sigismember(&t->real_blocked, sig) &&
  636. (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
  637. /*
  638. * This signal will be fatal to the whole group.
  639. */
  640. if (!sig_kernel_coredump(sig)) {
  641. /*
  642. * Start a group exit and wake everybody up.
  643. * This way we don't have other threads
  644. * running and doing things after a slower
  645. * thread has the fatal signal pending.
  646. */
  647. signal->flags = SIGNAL_GROUP_EXIT;
  648. signal->group_exit_code = sig;
  649. signal->group_stop_count = 0;
  650. t = p;
  651. do {
  652. sigaddset(&t->pending.signal, SIGKILL);
  653. signal_wake_up(t, 1);
  654. } while_each_thread(p, t);
  655. return;
  656. }
  657. }
  658. /*
  659. * The signal is already in the shared-pending queue.
  660. * Tell the chosen thread to wake up and dequeue it.
  661. */
  662. signal_wake_up(t, sig == SIGKILL);
  663. return;
  664. }
  665. static inline int legacy_queue(struct sigpending *signals, int sig)
  666. {
  667. return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
  668. }
  669. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
  670. int group)
  671. {
  672. struct sigpending *pending;
  673. struct sigqueue *q;
  674. assert_spin_locked(&t->sighand->siglock);
  675. handle_stop_signal(sig, t);
  676. pending = group ? &t->signal->shared_pending : &t->pending;
  677. /*
  678. * Short-circuit ignored signals and support queuing
  679. * exactly one non-rt signal, so that we can get more
  680. * detailed information about the cause of the signal.
  681. */
  682. if (sig_ignored(t, sig) || legacy_queue(pending, sig))
  683. return 0;
  684. /*
  685. * Deliver the signal to listening signalfds. This must be called
  686. * with the sighand lock held.
  687. */
  688. signalfd_notify(t, sig);
  689. /*
  690. * fast-pathed signals for kernel-internal things like SIGSTOP
  691. * or SIGKILL.
  692. */
  693. if (info == SEND_SIG_FORCED)
  694. goto out_set;
  695. /* Real-time signals must be queued if sent by sigqueue, or
  696. some other real-time mechanism. It is implementation
  697. defined whether kill() does so. We attempt to do so, on
  698. the principle of least surprise, but since kill is not
  699. allowed to fail with EAGAIN when low on memory we just
  700. make sure at least one signal gets delivered and don't
  701. pass on the info struct. */
  702. q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
  703. (is_si_special(info) ||
  704. info->si_code >= 0)));
  705. if (q) {
  706. list_add_tail(&q->list, &pending->list);
  707. switch ((unsigned long) info) {
  708. case (unsigned long) SEND_SIG_NOINFO:
  709. q->info.si_signo = sig;
  710. q->info.si_errno = 0;
  711. q->info.si_code = SI_USER;
  712. q->info.si_pid = task_pid_vnr(current);
  713. q->info.si_uid = current->uid;
  714. break;
  715. case (unsigned long) SEND_SIG_PRIV:
  716. q->info.si_signo = sig;
  717. q->info.si_errno = 0;
  718. q->info.si_code = SI_KERNEL;
  719. q->info.si_pid = 0;
  720. q->info.si_uid = 0;
  721. break;
  722. default:
  723. copy_siginfo(&q->info, info);
  724. break;
  725. }
  726. } else if (!is_si_special(info)) {
  727. if (sig >= SIGRTMIN && info->si_code != SI_USER)
  728. /*
  729. * Queue overflow, abort. We may abort if the signal was rt
  730. * and sent by user using something other than kill().
  731. */
  732. return -EAGAIN;
  733. }
  734. out_set:
  735. sigaddset(&pending->signal, sig);
  736. complete_signal(sig, t, group);
  737. return 0;
  738. }
  739. int print_fatal_signals;
  740. static void print_fatal_signal(struct pt_regs *regs, int signr)
  741. {
  742. printk("%s/%d: potentially unexpected fatal signal %d.\n",
  743. current->comm, task_pid_nr(current), signr);
  744. #if defined(__i386__) && !defined(__arch_um__)
  745. printk("code at %08lx: ", regs->ip);
  746. {
  747. int i;
  748. for (i = 0; i < 16; i++) {
  749. unsigned char insn;
  750. __get_user(insn, (unsigned char *)(regs->ip + i));
  751. printk("%02x ", insn);
  752. }
  753. }
  754. #endif
  755. printk("\n");
  756. show_regs(regs);
  757. }
  758. static int __init setup_print_fatal_signals(char *str)
  759. {
  760. get_option (&str, &print_fatal_signals);
  761. return 1;
  762. }
  763. __setup("print-fatal-signals=", setup_print_fatal_signals);
  764. int
  765. __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  766. {
  767. return send_signal(sig, info, p, 1);
  768. }
  769. static int
  770. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  771. {
  772. return send_signal(sig, info, t, 0);
  773. }
  774. /*
  775. * Force a signal that the process can't ignore: if necessary
  776. * we unblock the signal and change any SIG_IGN to SIG_DFL.
  777. *
  778. * Note: If we unblock the signal, we always reset it to SIG_DFL,
  779. * since we do not want to have a signal handler that was blocked
  780. * be invoked when user space had explicitly blocked it.
  781. *
  782. * We don't want to have recursive SIGSEGV's etc, for example.
  783. */
  784. int
  785. force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
  786. {
  787. unsigned long int flags;
  788. int ret, blocked, ignored;
  789. struct k_sigaction *action;
  790. spin_lock_irqsave(&t->sighand->siglock, flags);
  791. action = &t->sighand->action[sig-1];
  792. ignored = action->sa.sa_handler == SIG_IGN;
  793. blocked = sigismember(&t->blocked, sig);
  794. if (blocked || ignored) {
  795. action->sa.sa_handler = SIG_DFL;
  796. if (blocked) {
  797. sigdelset(&t->blocked, sig);
  798. recalc_sigpending_and_wake(t);
  799. }
  800. }
  801. ret = specific_send_sig_info(sig, info, t);
  802. spin_unlock_irqrestore(&t->sighand->siglock, flags);
  803. return ret;
  804. }
  805. void
  806. force_sig_specific(int sig, struct task_struct *t)
  807. {
  808. force_sig_info(sig, SEND_SIG_FORCED, t);
  809. }
  810. /*
  811. * Nuke all other threads in the group.
  812. */
  813. void zap_other_threads(struct task_struct *p)
  814. {
  815. struct task_struct *t;
  816. p->signal->group_stop_count = 0;
  817. for (t = next_thread(p); t != p; t = next_thread(t)) {
  818. /*
  819. * Don't bother with already dead threads
  820. */
  821. if (t->exit_state)
  822. continue;
  823. /* SIGKILL will be handled before any pending SIGSTOP */
  824. sigaddset(&t->pending.signal, SIGKILL);
  825. signal_wake_up(t, 1);
  826. }
  827. }
  828. int __fatal_signal_pending(struct task_struct *tsk)
  829. {
  830. return sigismember(&tsk->pending.signal, SIGKILL);
  831. }
  832. EXPORT_SYMBOL(__fatal_signal_pending);
  833. struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
  834. {
  835. struct sighand_struct *sighand;
  836. rcu_read_lock();
  837. for (;;) {
  838. sighand = rcu_dereference(tsk->sighand);
  839. if (unlikely(sighand == NULL))
  840. break;
  841. spin_lock_irqsave(&sighand->siglock, *flags);
  842. if (likely(sighand == tsk->sighand))
  843. break;
  844. spin_unlock_irqrestore(&sighand->siglock, *flags);
  845. }
  846. rcu_read_unlock();
  847. return sighand;
  848. }
  849. int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  850. {
  851. unsigned long flags;
  852. int ret;
  853. ret = check_kill_permission(sig, info, p);
  854. if (!ret && sig) {
  855. ret = -ESRCH;
  856. if (lock_task_sighand(p, &flags)) {
  857. ret = __group_send_sig_info(sig, info, p);
  858. unlock_task_sighand(p, &flags);
  859. }
  860. }
  861. return ret;
  862. }
  863. /*
  864. * __kill_pgrp_info() sends a signal to a process group: this is what the tty
  865. * control characters do (^C, ^Z etc)
  866. */
  867. int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
  868. {
  869. struct task_struct *p = NULL;
  870. int retval, success;
  871. success = 0;
  872. retval = -ESRCH;
  873. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  874. int err = group_send_sig_info(sig, info, p);
  875. success |= !err;
  876. retval = err;
  877. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  878. return success ? 0 : retval;
  879. }
  880. int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
  881. {
  882. int error = -ESRCH;
  883. struct task_struct *p;
  884. rcu_read_lock();
  885. retry:
  886. p = pid_task(pid, PIDTYPE_PID);
  887. if (p) {
  888. error = group_send_sig_info(sig, info, p);
  889. if (unlikely(error == -ESRCH))
  890. /*
  891. * The task was unhashed in between, try again.
  892. * If it is dead, pid_task() will return NULL,
  893. * if we race with de_thread() it will find the
  894. * new leader.
  895. */
  896. goto retry;
  897. }
  898. rcu_read_unlock();
  899. return error;
  900. }
  901. int
  902. kill_proc_info(int sig, struct siginfo *info, pid_t pid)
  903. {
  904. int error;
  905. rcu_read_lock();
  906. error = kill_pid_info(sig, info, find_vpid(pid));
  907. rcu_read_unlock();
  908. return error;
  909. }
  910. /* like kill_pid_info(), but doesn't use uid/euid of "current" */
  911. int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
  912. uid_t uid, uid_t euid, u32 secid)
  913. {
  914. int ret = -EINVAL;
  915. struct task_struct *p;
  916. if (!valid_signal(sig))
  917. return ret;
  918. read_lock(&tasklist_lock);
  919. p = pid_task(pid, PIDTYPE_PID);
  920. if (!p) {
  921. ret = -ESRCH;
  922. goto out_unlock;
  923. }
  924. if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
  925. && (euid != p->suid) && (euid != p->uid)
  926. && (uid != p->suid) && (uid != p->uid)) {
  927. ret = -EPERM;
  928. goto out_unlock;
  929. }
  930. ret = security_task_kill(p, info, sig, secid);
  931. if (ret)
  932. goto out_unlock;
  933. if (sig && p->sighand) {
  934. unsigned long flags;
  935. spin_lock_irqsave(&p->sighand->siglock, flags);
  936. ret = __group_send_sig_info(sig, info, p);
  937. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  938. }
  939. out_unlock:
  940. read_unlock(&tasklist_lock);
  941. return ret;
  942. }
  943. EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
  944. /*
  945. * kill_something_info() interprets pid in interesting ways just like kill(2).
  946. *
  947. * POSIX specifies that kill(-1,sig) is unspecified, but what we have
  948. * is probably wrong. Should make it like BSD or SYSV.
  949. */
  950. static int kill_something_info(int sig, struct siginfo *info, int pid)
  951. {
  952. int ret;
  953. if (pid > 0) {
  954. rcu_read_lock();
  955. ret = kill_pid_info(sig, info, find_vpid(pid));
  956. rcu_read_unlock();
  957. return ret;
  958. }
  959. read_lock(&tasklist_lock);
  960. if (pid != -1) {
  961. ret = __kill_pgrp_info(sig, info,
  962. pid ? find_vpid(-pid) : task_pgrp(current));
  963. } else {
  964. int retval = 0, count = 0;
  965. struct task_struct * p;
  966. for_each_process(p) {
  967. if (p->pid > 1 && !same_thread_group(p, current)) {
  968. int err = group_send_sig_info(sig, info, p);
  969. ++count;
  970. if (err != -EPERM)
  971. retval = err;
  972. }
  973. }
  974. ret = count ? retval : -ESRCH;
  975. }
  976. read_unlock(&tasklist_lock);
  977. return ret;
  978. }
  979. /*
  980. * These are for backward compatibility with the rest of the kernel source.
  981. */
  982. /*
  983. * The caller must ensure the task can't exit.
  984. */
  985. int
  986. send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
  987. {
  988. int ret;
  989. unsigned long flags;
  990. /*
  991. * Make sure legacy kernel users don't send in bad values
  992. * (normal paths check this in check_kill_permission).
  993. */
  994. if (!valid_signal(sig))
  995. return -EINVAL;
  996. spin_lock_irqsave(&p->sighand->siglock, flags);
  997. ret = specific_send_sig_info(sig, info, p);
  998. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  999. return ret;
  1000. }
  1001. #define __si_special(priv) \
  1002. ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
  1003. int
  1004. send_sig(int sig, struct task_struct *p, int priv)
  1005. {
  1006. return send_sig_info(sig, __si_special(priv), p);
  1007. }
  1008. void
  1009. force_sig(int sig, struct task_struct *p)
  1010. {
  1011. force_sig_info(sig, SEND_SIG_PRIV, p);
  1012. }
  1013. /*
  1014. * When things go south during signal handling, we
  1015. * will force a SIGSEGV. And if the signal that caused
  1016. * the problem was already a SIGSEGV, we'll want to
  1017. * make sure we don't even try to deliver the signal..
  1018. */
  1019. int
  1020. force_sigsegv(int sig, struct task_struct *p)
  1021. {
  1022. if (sig == SIGSEGV) {
  1023. unsigned long flags;
  1024. spin_lock_irqsave(&p->sighand->siglock, flags);
  1025. p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
  1026. spin_unlock_irqrestore(&p->sighand->siglock, flags);
  1027. }
  1028. force_sig(SIGSEGV, p);
  1029. return 0;
  1030. }
  1031. int kill_pgrp(struct pid *pid, int sig, int priv)
  1032. {
  1033. int ret;
  1034. read_lock(&tasklist_lock);
  1035. ret = __kill_pgrp_info(sig, __si_special(priv), pid);
  1036. read_unlock(&tasklist_lock);
  1037. return ret;
  1038. }
  1039. EXPORT_SYMBOL(kill_pgrp);
  1040. int kill_pid(struct pid *pid, int sig, int priv)
  1041. {
  1042. return kill_pid_info(sig, __si_special(priv), pid);
  1043. }
  1044. EXPORT_SYMBOL(kill_pid);
  1045. int
  1046. kill_proc(pid_t pid, int sig, int priv)
  1047. {
  1048. int ret;
  1049. rcu_read_lock();
  1050. ret = kill_pid_info(sig, __si_special(priv), find_pid(pid));
  1051. rcu_read_unlock();
  1052. return ret;
  1053. }
  1054. /*
  1055. * These functions support sending signals using preallocated sigqueue
  1056. * structures. This is needed "because realtime applications cannot
  1057. * afford to lose notifications of asynchronous events, like timer
  1058. * expirations or I/O completions". In the case of Posix Timers
  1059. * we allocate the sigqueue structure from the timer_create. If this
  1060. * allocation fails we are able to report the failure to the application
  1061. * with an EAGAIN error.
  1062. */
  1063. struct sigqueue *sigqueue_alloc(void)
  1064. {
  1065. struct sigqueue *q;
  1066. if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
  1067. q->flags |= SIGQUEUE_PREALLOC;
  1068. return(q);
  1069. }
  1070. void sigqueue_free(struct sigqueue *q)
  1071. {
  1072. unsigned long flags;
  1073. spinlock_t *lock = &current->sighand->siglock;
  1074. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1075. /*
  1076. * If the signal is still pending remove it from the
  1077. * pending queue. We must hold ->siglock while testing
  1078. * q->list to serialize with collect_signal().
  1079. */
  1080. spin_lock_irqsave(lock, flags);
  1081. if (!list_empty(&q->list))
  1082. list_del_init(&q->list);
  1083. spin_unlock_irqrestore(lock, flags);
  1084. q->flags &= ~SIGQUEUE_PREALLOC;
  1085. __sigqueue_free(q);
  1086. }
  1087. static int do_send_sigqueue(struct sigqueue *q, struct task_struct *t,
  1088. int group)
  1089. {
  1090. int sig = q->info.si_signo;
  1091. struct sigpending *pending;
  1092. unsigned long flags;
  1093. int ret;
  1094. BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
  1095. ret = -1;
  1096. if (!likely(lock_task_sighand(t, &flags)))
  1097. goto ret;
  1098. handle_stop_signal(sig, t);
  1099. ret = 1;
  1100. if (sig_ignored(t, sig))
  1101. goto out;
  1102. ret = 0;
  1103. if (unlikely(!list_empty(&q->list))) {
  1104. /*
  1105. * If an SI_TIMER entry is already queue just increment
  1106. * the overrun count.
  1107. */
  1108. BUG_ON(q->info.si_code != SI_TIMER);
  1109. q->info.si_overrun++;
  1110. goto out;
  1111. }
  1112. signalfd_notify(t, sig);
  1113. pending = group ? &t->signal->shared_pending : &t->pending;
  1114. list_add_tail(&q->list, &pending->list);
  1115. sigaddset(&pending->signal, sig);
  1116. complete_signal(sig, t, group);
  1117. out:
  1118. unlock_task_sighand(t, &flags);
  1119. ret:
  1120. return ret;
  1121. }
  1122. int send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
  1123. {
  1124. return do_send_sigqueue(q, p, 0);
  1125. }
  1126. int
  1127. send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
  1128. {
  1129. return do_send_sigqueue(q, p, 1);
  1130. }
  1131. /*
  1132. * Wake up any threads in the parent blocked in wait* syscalls.
  1133. */
  1134. static inline void __wake_up_parent(struct task_struct *p,
  1135. struct task_struct *parent)
  1136. {
  1137. wake_up_interruptible_sync(&parent->signal->wait_chldexit);
  1138. }
  1139. /*
  1140. * Let a parent know about the death of a child.
  1141. * For a stopped/continued status change, use do_notify_parent_cldstop instead.
  1142. */
  1143. void do_notify_parent(struct task_struct *tsk, int sig)
  1144. {
  1145. struct siginfo info;
  1146. unsigned long flags;
  1147. struct sighand_struct *psig;
  1148. BUG_ON(sig == -1);
  1149. /* do_notify_parent_cldstop should have been called instead. */
  1150. BUG_ON(task_is_stopped_or_traced(tsk));
  1151. BUG_ON(!tsk->ptrace &&
  1152. (tsk->group_leader != tsk || !thread_group_empty(tsk)));
  1153. info.si_signo = sig;
  1154. info.si_errno = 0;
  1155. /*
  1156. * we are under tasklist_lock here so our parent is tied to
  1157. * us and cannot exit and release its namespace.
  1158. *
  1159. * the only it can is to switch its nsproxy with sys_unshare,
  1160. * bu uncharing pid namespaces is not allowed, so we'll always
  1161. * see relevant namespace
  1162. *
  1163. * write_lock() currently calls preempt_disable() which is the
  1164. * same as rcu_read_lock(), but according to Oleg, this is not
  1165. * correct to rely on this
  1166. */
  1167. rcu_read_lock();
  1168. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1169. rcu_read_unlock();
  1170. info.si_uid = tsk->uid;
  1171. /* FIXME: find out whether or not this is supposed to be c*time. */
  1172. info.si_utime = cputime_to_jiffies(cputime_add(tsk->utime,
  1173. tsk->signal->utime));
  1174. info.si_stime = cputime_to_jiffies(cputime_add(tsk->stime,
  1175. tsk->signal->stime));
  1176. info.si_status = tsk->exit_code & 0x7f;
  1177. if (tsk->exit_code & 0x80)
  1178. info.si_code = CLD_DUMPED;
  1179. else if (tsk->exit_code & 0x7f)
  1180. info.si_code = CLD_KILLED;
  1181. else {
  1182. info.si_code = CLD_EXITED;
  1183. info.si_status = tsk->exit_code >> 8;
  1184. }
  1185. psig = tsk->parent->sighand;
  1186. spin_lock_irqsave(&psig->siglock, flags);
  1187. if (!tsk->ptrace && sig == SIGCHLD &&
  1188. (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
  1189. (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
  1190. /*
  1191. * We are exiting and our parent doesn't care. POSIX.1
  1192. * defines special semantics for setting SIGCHLD to SIG_IGN
  1193. * or setting the SA_NOCLDWAIT flag: we should be reaped
  1194. * automatically and not left for our parent's wait4 call.
  1195. * Rather than having the parent do it as a magic kind of
  1196. * signal handler, we just set this to tell do_exit that we
  1197. * can be cleaned up without becoming a zombie. Note that
  1198. * we still call __wake_up_parent in this case, because a
  1199. * blocked sys_wait4 might now return -ECHILD.
  1200. *
  1201. * Whether we send SIGCHLD or not for SA_NOCLDWAIT
  1202. * is implementation-defined: we do (if you don't want
  1203. * it, just use SIG_IGN instead).
  1204. */
  1205. tsk->exit_signal = -1;
  1206. if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
  1207. sig = 0;
  1208. }
  1209. if (valid_signal(sig) && sig > 0)
  1210. __group_send_sig_info(sig, &info, tsk->parent);
  1211. __wake_up_parent(tsk, tsk->parent);
  1212. spin_unlock_irqrestore(&psig->siglock, flags);
  1213. }
  1214. static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
  1215. {
  1216. struct siginfo info;
  1217. unsigned long flags;
  1218. struct task_struct *parent;
  1219. struct sighand_struct *sighand;
  1220. if (tsk->ptrace & PT_PTRACED)
  1221. parent = tsk->parent;
  1222. else {
  1223. tsk = tsk->group_leader;
  1224. parent = tsk->real_parent;
  1225. }
  1226. info.si_signo = SIGCHLD;
  1227. info.si_errno = 0;
  1228. /*
  1229. * see comment in do_notify_parent() abot the following 3 lines
  1230. */
  1231. rcu_read_lock();
  1232. info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
  1233. rcu_read_unlock();
  1234. info.si_uid = tsk->uid;
  1235. /* FIXME: find out whether or not this is supposed to be c*time. */
  1236. info.si_utime = cputime_to_jiffies(tsk->utime);
  1237. info.si_stime = cputime_to_jiffies(tsk->stime);
  1238. info.si_code = why;
  1239. switch (why) {
  1240. case CLD_CONTINUED:
  1241. info.si_status = SIGCONT;
  1242. break;
  1243. case CLD_STOPPED:
  1244. info.si_status = tsk->signal->group_exit_code & 0x7f;
  1245. break;
  1246. case CLD_TRAPPED:
  1247. info.si_status = tsk->exit_code & 0x7f;
  1248. break;
  1249. default:
  1250. BUG();
  1251. }
  1252. sighand = parent->sighand;
  1253. spin_lock_irqsave(&sighand->siglock, flags);
  1254. if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
  1255. !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
  1256. __group_send_sig_info(SIGCHLD, &info, parent);
  1257. /*
  1258. * Even if SIGCHLD is not generated, we must wake up wait4 calls.
  1259. */
  1260. __wake_up_parent(tsk, parent);
  1261. spin_unlock_irqrestore(&sighand->siglock, flags);
  1262. }
  1263. static inline int may_ptrace_stop(void)
  1264. {
  1265. if (!likely(current->ptrace & PT_PTRACED))
  1266. return 0;
  1267. /*
  1268. * Are we in the middle of do_coredump?
  1269. * If so and our tracer is also part of the coredump stopping
  1270. * is a deadlock situation, and pointless because our tracer
  1271. * is dead so don't allow us to stop.
  1272. * If SIGKILL was already sent before the caller unlocked
  1273. * ->siglock we must see ->core_waiters != 0. Otherwise it
  1274. * is safe to enter schedule().
  1275. */
  1276. if (unlikely(current->mm->core_waiters) &&
  1277. unlikely(current->mm == current->parent->mm))
  1278. return 0;
  1279. return 1;
  1280. }
  1281. /*
  1282. * Return nonzero if there is a SIGKILL that should be waking us up.
  1283. * Called with the siglock held.
  1284. */
  1285. static int sigkill_pending(struct task_struct *tsk)
  1286. {
  1287. return ((sigismember(&tsk->pending.signal, SIGKILL) ||
  1288. sigismember(&tsk->signal->shared_pending.signal, SIGKILL)) &&
  1289. !unlikely(sigismember(&tsk->blocked, SIGKILL)));
  1290. }
  1291. /*
  1292. * This must be called with current->sighand->siglock held.
  1293. *
  1294. * This should be the path for all ptrace stops.
  1295. * We always set current->last_siginfo while stopped here.
  1296. * That makes it a way to test a stopped process for
  1297. * being ptrace-stopped vs being job-control-stopped.
  1298. *
  1299. * If we actually decide not to stop at all because the tracer
  1300. * is gone, we keep current->exit_code unless clear_code.
  1301. */
  1302. static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
  1303. {
  1304. int killed = 0;
  1305. if (arch_ptrace_stop_needed(exit_code, info)) {
  1306. /*
  1307. * The arch code has something special to do before a
  1308. * ptrace stop. This is allowed to block, e.g. for faults
  1309. * on user stack pages. We can't keep the siglock while
  1310. * calling arch_ptrace_stop, so we must release it now.
  1311. * To preserve proper semantics, we must do this before
  1312. * any signal bookkeeping like checking group_stop_count.
  1313. * Meanwhile, a SIGKILL could come in before we retake the
  1314. * siglock. That must prevent us from sleeping in TASK_TRACED.
  1315. * So after regaining the lock, we must check for SIGKILL.
  1316. */
  1317. spin_unlock_irq(&current->sighand->siglock);
  1318. arch_ptrace_stop(exit_code, info);
  1319. spin_lock_irq(&current->sighand->siglock);
  1320. killed = sigkill_pending(current);
  1321. }
  1322. /*
  1323. * If there is a group stop in progress,
  1324. * we must participate in the bookkeeping.
  1325. */
  1326. if (current->signal->group_stop_count > 0)
  1327. --current->signal->group_stop_count;
  1328. current->last_siginfo = info;
  1329. current->exit_code = exit_code;
  1330. /* Let the debugger run. */
  1331. __set_current_state(TASK_TRACED);
  1332. spin_unlock_irq(&current->sighand->siglock);
  1333. read_lock(&tasklist_lock);
  1334. if (!unlikely(killed) && may_ptrace_stop()) {
  1335. do_notify_parent_cldstop(current, CLD_TRAPPED);
  1336. read_unlock(&tasklist_lock);
  1337. schedule();
  1338. } else {
  1339. /*
  1340. * By the time we got the lock, our tracer went away.
  1341. * Don't drop the lock yet, another tracer may come.
  1342. */
  1343. __set_current_state(TASK_RUNNING);
  1344. if (clear_code)
  1345. current->exit_code = 0;
  1346. read_unlock(&tasklist_lock);
  1347. }
  1348. /*
  1349. * While in TASK_TRACED, we were considered "frozen enough".
  1350. * Now that we woke up, it's crucial if we're supposed to be
  1351. * frozen that we freeze now before running anything substantial.
  1352. */
  1353. try_to_freeze();
  1354. /*
  1355. * We are back. Now reacquire the siglock before touching
  1356. * last_siginfo, so that we are sure to have synchronized with
  1357. * any signal-sending on another CPU that wants to examine it.
  1358. */
  1359. spin_lock_irq(&current->sighand->siglock);
  1360. current->last_siginfo = NULL;
  1361. /*
  1362. * Queued signals ignored us while we were stopped for tracing.
  1363. * So check for any that we should take before resuming user mode.
  1364. * This sets TIF_SIGPENDING, but never clears it.
  1365. */
  1366. recalc_sigpending_tsk(current);
  1367. }
  1368. void ptrace_notify(int exit_code)
  1369. {
  1370. siginfo_t info;
  1371. BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
  1372. memset(&info, 0, sizeof info);
  1373. info.si_signo = SIGTRAP;
  1374. info.si_code = exit_code;
  1375. info.si_pid = task_pid_vnr(current);
  1376. info.si_uid = current->uid;
  1377. /* Let the debugger run. */
  1378. spin_lock_irq(&current->sighand->siglock);
  1379. ptrace_stop(exit_code, 1, &info);
  1380. spin_unlock_irq(&current->sighand->siglock);
  1381. }
  1382. static void
  1383. finish_stop(int stop_count)
  1384. {
  1385. /*
  1386. * If there are no other threads in the group, or if there is
  1387. * a group stop in progress and we are the last to stop,
  1388. * report to the parent. When ptraced, every thread reports itself.
  1389. */
  1390. if (stop_count == 0 || (current->ptrace & PT_PTRACED)) {
  1391. read_lock(&tasklist_lock);
  1392. do_notify_parent_cldstop(current, CLD_STOPPED);
  1393. read_unlock(&tasklist_lock);
  1394. }
  1395. do {
  1396. schedule();
  1397. } while (try_to_freeze());
  1398. /*
  1399. * Now we don't run again until continued.
  1400. */
  1401. current->exit_code = 0;
  1402. }
  1403. /*
  1404. * This performs the stopping for SIGSTOP and other stop signals.
  1405. * We have to stop all threads in the thread group.
  1406. * Returns nonzero if we've actually stopped and released the siglock.
  1407. * Returns zero if we didn't stop and still hold the siglock.
  1408. */
  1409. static int do_signal_stop(int signr)
  1410. {
  1411. struct signal_struct *sig = current->signal;
  1412. int stop_count;
  1413. if (sig->group_stop_count > 0) {
  1414. /*
  1415. * There is a group stop in progress. We don't need to
  1416. * start another one.
  1417. */
  1418. stop_count = --sig->group_stop_count;
  1419. } else {
  1420. struct task_struct *t;
  1421. if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
  1422. unlikely(signal_group_exit(sig)))
  1423. return 0;
  1424. /*
  1425. * There is no group stop already in progress.
  1426. * We must initiate one now.
  1427. */
  1428. sig->group_exit_code = signr;
  1429. stop_count = 0;
  1430. for (t = next_thread(current); t != current; t = next_thread(t))
  1431. /*
  1432. * Setting state to TASK_STOPPED for a group
  1433. * stop is always done with the siglock held,
  1434. * so this check has no races.
  1435. */
  1436. if (!(t->flags & PF_EXITING) &&
  1437. !task_is_stopped_or_traced(t)) {
  1438. stop_count++;
  1439. signal_wake_up(t, 0);
  1440. }
  1441. sig->group_stop_count = stop_count;
  1442. }
  1443. if (stop_count == 0)
  1444. sig->flags = SIGNAL_STOP_STOPPED;
  1445. current->exit_code = sig->group_exit_code;
  1446. __set_current_state(TASK_STOPPED);
  1447. spin_unlock_irq(&current->sighand->siglock);
  1448. finish_stop(stop_count);
  1449. return 1;
  1450. }
  1451. static int ptrace_signal(int signr, siginfo_t *info,
  1452. struct pt_regs *regs, void *cookie)
  1453. {
  1454. if (!(current->ptrace & PT_PTRACED))
  1455. return signr;
  1456. ptrace_signal_deliver(regs, cookie);
  1457. /* Let the debugger run. */
  1458. ptrace_stop(signr, 0, info);
  1459. /* We're back. Did the debugger cancel the sig? */
  1460. signr = current->exit_code;
  1461. if (signr == 0)
  1462. return signr;
  1463. current->exit_code = 0;
  1464. /* Update the siginfo structure if the signal has
  1465. changed. If the debugger wanted something
  1466. specific in the siginfo structure then it should
  1467. have updated *info via PTRACE_SETSIGINFO. */
  1468. if (signr != info->si_signo) {
  1469. info->si_signo = signr;
  1470. info->si_errno = 0;
  1471. info->si_code = SI_USER;
  1472. info->si_pid = task_pid_vnr(current->parent);
  1473. info->si_uid = current->parent->uid;
  1474. }
  1475. /* If the (new) signal is now blocked, requeue it. */
  1476. if (sigismember(&current->blocked, signr)) {
  1477. specific_send_sig_info(signr, info, current);
  1478. signr = 0;
  1479. }
  1480. return signr;
  1481. }
  1482. int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
  1483. struct pt_regs *regs, void *cookie)
  1484. {
  1485. struct sighand_struct *sighand = current->sighand;
  1486. struct signal_struct *signal = current->signal;
  1487. int signr;
  1488. relock:
  1489. /*
  1490. * We'll jump back here after any time we were stopped in TASK_STOPPED.
  1491. * While in TASK_STOPPED, we were considered "frozen enough".
  1492. * Now that we woke up, it's crucial if we're supposed to be
  1493. * frozen that we freeze now before running anything substantial.
  1494. */
  1495. try_to_freeze();
  1496. spin_lock_irq(&sighand->siglock);
  1497. if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
  1498. int why = (signal->flags & SIGNAL_STOP_CONTINUED)
  1499. ? CLD_CONTINUED : CLD_STOPPED;
  1500. signal->flags &= ~SIGNAL_CLD_MASK;
  1501. spin_unlock_irq(&sighand->siglock);
  1502. read_lock(&tasklist_lock);
  1503. do_notify_parent_cldstop(current->group_leader, why);
  1504. read_unlock(&tasklist_lock);
  1505. goto relock;
  1506. }
  1507. for (;;) {
  1508. struct k_sigaction *ka;
  1509. if (unlikely(signal->group_stop_count > 0) &&
  1510. do_signal_stop(0))
  1511. goto relock;
  1512. signr = dequeue_signal(current, &current->blocked, info);
  1513. if (!signr)
  1514. break; /* will return 0 */
  1515. if (signr != SIGKILL) {
  1516. signr = ptrace_signal(signr, info, regs, cookie);
  1517. if (!signr)
  1518. continue;
  1519. }
  1520. ka = &sighand->action[signr-1];
  1521. if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
  1522. continue;
  1523. if (ka->sa.sa_handler != SIG_DFL) {
  1524. /* Run the handler. */
  1525. *return_ka = *ka;
  1526. if (ka->sa.sa_flags & SA_ONESHOT)
  1527. ka->sa.sa_handler = SIG_DFL;
  1528. break; /* will return non-zero "signr" value */
  1529. }
  1530. /*
  1531. * Now we are doing the default action for this signal.
  1532. */
  1533. if (sig_kernel_ignore(signr)) /* Default is nothing. */
  1534. continue;
  1535. /*
  1536. * Global init gets no signals it doesn't want.
  1537. */
  1538. if (is_global_init(current))
  1539. continue;
  1540. if (sig_kernel_stop(signr)) {
  1541. /*
  1542. * The default action is to stop all threads in
  1543. * the thread group. The job control signals
  1544. * do nothing in an orphaned pgrp, but SIGSTOP
  1545. * always works. Note that siglock needs to be
  1546. * dropped during the call to is_orphaned_pgrp()
  1547. * because of lock ordering with tasklist_lock.
  1548. * This allows an intervening SIGCONT to be posted.
  1549. * We need to check for that and bail out if necessary.
  1550. */
  1551. if (signr != SIGSTOP) {
  1552. spin_unlock_irq(&sighand->siglock);
  1553. /* signals can be posted during this window */
  1554. if (is_current_pgrp_orphaned())
  1555. goto relock;
  1556. spin_lock_irq(&sighand->siglock);
  1557. }
  1558. if (likely(do_signal_stop(signr))) {
  1559. /* It released the siglock. */
  1560. goto relock;
  1561. }
  1562. /*
  1563. * We didn't actually stop, due to a race
  1564. * with SIGCONT or something like that.
  1565. */
  1566. continue;
  1567. }
  1568. spin_unlock_irq(&sighand->siglock);
  1569. /*
  1570. * Anything else is fatal, maybe with a core dump.
  1571. */
  1572. current->flags |= PF_SIGNALED;
  1573. if ((signr != SIGKILL) && print_fatal_signals)
  1574. print_fatal_signal(regs, signr);
  1575. if (sig_kernel_coredump(signr)) {
  1576. /*
  1577. * If it was able to dump core, this kills all
  1578. * other threads in the group and synchronizes with
  1579. * their demise. If we lost the race with another
  1580. * thread getting here, it set group_exit_code
  1581. * first and our do_group_exit call below will use
  1582. * that value and ignore the one we pass it.
  1583. */
  1584. do_coredump((long)signr, signr, regs);
  1585. }
  1586. /*
  1587. * Death signals, no core dump.
  1588. */
  1589. do_group_exit(signr);
  1590. /* NOTREACHED */
  1591. }
  1592. spin_unlock_irq(&sighand->siglock);
  1593. return signr;
  1594. }
  1595. void exit_signals(struct task_struct *tsk)
  1596. {
  1597. int group_stop = 0;
  1598. struct task_struct *t;
  1599. if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
  1600. tsk->flags |= PF_EXITING;
  1601. return;
  1602. }
  1603. spin_lock_irq(&tsk->sighand->siglock);
  1604. /*
  1605. * From now this task is not visible for group-wide signals,
  1606. * see wants_signal(), do_signal_stop().
  1607. */
  1608. tsk->flags |= PF_EXITING;
  1609. if (!signal_pending(tsk))
  1610. goto out;
  1611. /* It could be that __group_complete_signal() choose us to
  1612. * notify about group-wide signal. Another thread should be
  1613. * woken now to take the signal since we will not.
  1614. */
  1615. for (t = tsk; (t = next_thread(t)) != tsk; )
  1616. if (!signal_pending(t) && !(t->flags & PF_EXITING))
  1617. recalc_sigpending_and_wake(t);
  1618. if (unlikely(tsk->signal->group_stop_count) &&
  1619. !--tsk->signal->group_stop_count) {
  1620. tsk->signal->flags = SIGNAL_STOP_STOPPED;
  1621. group_stop = 1;
  1622. }
  1623. out:
  1624. spin_unlock_irq(&tsk->sighand->siglock);
  1625. if (unlikely(group_stop)) {
  1626. read_lock(&tasklist_lock);
  1627. do_notify_parent_cldstop(tsk, CLD_STOPPED);
  1628. read_unlock(&tasklist_lock);
  1629. }
  1630. }
  1631. EXPORT_SYMBOL(recalc_sigpending);
  1632. EXPORT_SYMBOL_GPL(dequeue_signal);
  1633. EXPORT_SYMBOL(flush_signals);
  1634. EXPORT_SYMBOL(force_sig);
  1635. EXPORT_SYMBOL(kill_proc);
  1636. EXPORT_SYMBOL(ptrace_notify);
  1637. EXPORT_SYMBOL(send_sig);
  1638. EXPORT_SYMBOL(send_sig_info);
  1639. EXPORT_SYMBOL(sigprocmask);
  1640. EXPORT_SYMBOL(block_all_signals);
  1641. EXPORT_SYMBOL(unblock_all_signals);
  1642. /*
  1643. * System call entry points.
  1644. */
  1645. asmlinkage long sys_restart_syscall(void)
  1646. {
  1647. struct restart_block *restart = &current_thread_info()->restart_block;
  1648. return restart->fn(restart);
  1649. }
  1650. long do_no_restart_syscall(struct restart_block *param)
  1651. {
  1652. return -EINTR;
  1653. }
  1654. /*
  1655. * We don't need to get the kernel lock - this is all local to this
  1656. * particular thread.. (and that's good, because this is _heavily_
  1657. * used by various programs)
  1658. */
  1659. /*
  1660. * This is also useful for kernel threads that want to temporarily
  1661. * (or permanently) block certain signals.
  1662. *
  1663. * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
  1664. * interface happily blocks "unblockable" signals like SIGKILL
  1665. * and friends.
  1666. */
  1667. int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
  1668. {
  1669. int error;
  1670. spin_lock_irq(&current->sighand->siglock);
  1671. if (oldset)
  1672. *oldset = current->blocked;
  1673. error = 0;
  1674. switch (how) {
  1675. case SIG_BLOCK:
  1676. sigorsets(&current->blocked, &current->blocked, set);
  1677. break;
  1678. case SIG_UNBLOCK:
  1679. signandsets(&current->blocked, &current->blocked, set);
  1680. break;
  1681. case SIG_SETMASK:
  1682. current->blocked = *set;
  1683. break;
  1684. default:
  1685. error = -EINVAL;
  1686. }
  1687. recalc_sigpending();
  1688. spin_unlock_irq(&current->sighand->siglock);
  1689. return error;
  1690. }
  1691. asmlinkage long
  1692. sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
  1693. {
  1694. int error = -EINVAL;
  1695. sigset_t old_set, new_set;
  1696. /* XXX: Don't preclude handling different sized sigset_t's. */
  1697. if (sigsetsize != sizeof(sigset_t))
  1698. goto out;
  1699. if (set) {
  1700. error = -EFAULT;
  1701. if (copy_from_user(&new_set, set, sizeof(*set)))
  1702. goto out;
  1703. sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1704. error = sigprocmask(how, &new_set, &old_set);
  1705. if (error)
  1706. goto out;
  1707. if (oset)
  1708. goto set_old;
  1709. } else if (oset) {
  1710. spin_lock_irq(&current->sighand->siglock);
  1711. old_set = current->blocked;
  1712. spin_unlock_irq(&current->sighand->siglock);
  1713. set_old:
  1714. error = -EFAULT;
  1715. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  1716. goto out;
  1717. }
  1718. error = 0;
  1719. out:
  1720. return error;
  1721. }
  1722. long do_sigpending(void __user *set, unsigned long sigsetsize)
  1723. {
  1724. long error = -EINVAL;
  1725. sigset_t pending;
  1726. if (sigsetsize > sizeof(sigset_t))
  1727. goto out;
  1728. spin_lock_irq(&current->sighand->siglock);
  1729. sigorsets(&pending, &current->pending.signal,
  1730. &current->signal->shared_pending.signal);
  1731. spin_unlock_irq(&current->sighand->siglock);
  1732. /* Outside the lock because only this thread touches it. */
  1733. sigandsets(&pending, &current->blocked, &pending);
  1734. error = -EFAULT;
  1735. if (!copy_to_user(set, &pending, sigsetsize))
  1736. error = 0;
  1737. out:
  1738. return error;
  1739. }
  1740. asmlinkage long
  1741. sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
  1742. {
  1743. return do_sigpending(set, sigsetsize);
  1744. }
  1745. #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
  1746. int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
  1747. {
  1748. int err;
  1749. if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
  1750. return -EFAULT;
  1751. if (from->si_code < 0)
  1752. return __copy_to_user(to, from, sizeof(siginfo_t))
  1753. ? -EFAULT : 0;
  1754. /*
  1755. * If you change siginfo_t structure, please be sure
  1756. * this code is fixed accordingly.
  1757. * Please remember to update the signalfd_copyinfo() function
  1758. * inside fs/signalfd.c too, in case siginfo_t changes.
  1759. * It should never copy any pad contained in the structure
  1760. * to avoid security leaks, but must copy the generic
  1761. * 3 ints plus the relevant union member.
  1762. */
  1763. err = __put_user(from->si_signo, &to->si_signo);
  1764. err |= __put_user(from->si_errno, &to->si_errno);
  1765. err |= __put_user((short)from->si_code, &to->si_code);
  1766. switch (from->si_code & __SI_MASK) {
  1767. case __SI_KILL:
  1768. err |= __put_user(from->si_pid, &to->si_pid);
  1769. err |= __put_user(from->si_uid, &to->si_uid);
  1770. break;
  1771. case __SI_TIMER:
  1772. err |= __put_user(from->si_tid, &to->si_tid);
  1773. err |= __put_user(from->si_overrun, &to->si_overrun);
  1774. err |= __put_user(from->si_ptr, &to->si_ptr);
  1775. break;
  1776. case __SI_POLL:
  1777. err |= __put_user(from->si_band, &to->si_band);
  1778. err |= __put_user(from->si_fd, &to->si_fd);
  1779. break;
  1780. case __SI_FAULT:
  1781. err |= __put_user(from->si_addr, &to->si_addr);
  1782. #ifdef __ARCH_SI_TRAPNO
  1783. err |= __put_user(from->si_trapno, &to->si_trapno);
  1784. #endif
  1785. break;
  1786. case __SI_CHLD:
  1787. err |= __put_user(from->si_pid, &to->si_pid);
  1788. err |= __put_user(from->si_uid, &to->si_uid);
  1789. err |= __put_user(from->si_status, &to->si_status);
  1790. err |= __put_user(from->si_utime, &to->si_utime);
  1791. err |= __put_user(from->si_stime, &to->si_stime);
  1792. break;
  1793. case __SI_RT: /* This is not generated by the kernel as of now. */
  1794. case __SI_MESGQ: /* But this is */
  1795. err |= __put_user(from->si_pid, &to->si_pid);
  1796. err |= __put_user(from->si_uid, &to->si_uid);
  1797. err |= __put_user(from->si_ptr, &to->si_ptr);
  1798. break;
  1799. default: /* this is just in case for now ... */
  1800. err |= __put_user(from->si_pid, &to->si_pid);
  1801. err |= __put_user(from->si_uid, &to->si_uid);
  1802. break;
  1803. }
  1804. return err;
  1805. }
  1806. #endif
  1807. asmlinkage long
  1808. sys_rt_sigtimedwait(const sigset_t __user *uthese,
  1809. siginfo_t __user *uinfo,
  1810. const struct timespec __user *uts,
  1811. size_t sigsetsize)
  1812. {
  1813. int ret, sig;
  1814. sigset_t these;
  1815. struct timespec ts;
  1816. siginfo_t info;
  1817. long timeout = 0;
  1818. /* XXX: Don't preclude handling different sized sigset_t's. */
  1819. if (sigsetsize != sizeof(sigset_t))
  1820. return -EINVAL;
  1821. if (copy_from_user(&these, uthese, sizeof(these)))
  1822. return -EFAULT;
  1823. /*
  1824. * Invert the set of allowed signals to get those we
  1825. * want to block.
  1826. */
  1827. sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1828. signotset(&these);
  1829. if (uts) {
  1830. if (copy_from_user(&ts, uts, sizeof(ts)))
  1831. return -EFAULT;
  1832. if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
  1833. || ts.tv_sec < 0)
  1834. return -EINVAL;
  1835. }
  1836. spin_lock_irq(&current->sighand->siglock);
  1837. sig = dequeue_signal(current, &these, &info);
  1838. if (!sig) {
  1839. timeout = MAX_SCHEDULE_TIMEOUT;
  1840. if (uts)
  1841. timeout = (timespec_to_jiffies(&ts)
  1842. + (ts.tv_sec || ts.tv_nsec));
  1843. if (timeout) {
  1844. /* None ready -- temporarily unblock those we're
  1845. * interested while we are sleeping in so that we'll
  1846. * be awakened when they arrive. */
  1847. current->real_blocked = current->blocked;
  1848. sigandsets(&current->blocked, &current->blocked, &these);
  1849. recalc_sigpending();
  1850. spin_unlock_irq(&current->sighand->siglock);
  1851. timeout = schedule_timeout_interruptible(timeout);
  1852. spin_lock_irq(&current->sighand->siglock);
  1853. sig = dequeue_signal(current, &these, &info);
  1854. current->blocked = current->real_blocked;
  1855. siginitset(&current->real_blocked, 0);
  1856. recalc_sigpending();
  1857. }
  1858. }
  1859. spin_unlock_irq(&current->sighand->siglock);
  1860. if (sig) {
  1861. ret = sig;
  1862. if (uinfo) {
  1863. if (copy_siginfo_to_user(uinfo, &info))
  1864. ret = -EFAULT;
  1865. }
  1866. } else {
  1867. ret = -EAGAIN;
  1868. if (timeout)
  1869. ret = -EINTR;
  1870. }
  1871. return ret;
  1872. }
  1873. asmlinkage long
  1874. sys_kill(int pid, int sig)
  1875. {
  1876. struct siginfo info;
  1877. info.si_signo = sig;
  1878. info.si_errno = 0;
  1879. info.si_code = SI_USER;
  1880. info.si_pid = task_tgid_vnr(current);
  1881. info.si_uid = current->uid;
  1882. return kill_something_info(sig, &info, pid);
  1883. }
  1884. static int do_tkill(int tgid, int pid, int sig)
  1885. {
  1886. int error;
  1887. struct siginfo info;
  1888. struct task_struct *p;
  1889. unsigned long flags;
  1890. error = -ESRCH;
  1891. info.si_signo = sig;
  1892. info.si_errno = 0;
  1893. info.si_code = SI_TKILL;
  1894. info.si_pid = task_tgid_vnr(current);
  1895. info.si_uid = current->uid;
  1896. rcu_read_lock();
  1897. p = find_task_by_vpid(pid);
  1898. if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
  1899. error = check_kill_permission(sig, &info, p);
  1900. /*
  1901. * The null signal is a permissions and process existence
  1902. * probe. No signal is actually delivered.
  1903. *
  1904. * If lock_task_sighand() fails we pretend the task dies
  1905. * after receiving the signal. The window is tiny, and the
  1906. * signal is private anyway.
  1907. */
  1908. if (!error && sig && lock_task_sighand(p, &flags)) {
  1909. error = specific_send_sig_info(sig, &info, p);
  1910. unlock_task_sighand(p, &flags);
  1911. }
  1912. }
  1913. rcu_read_unlock();
  1914. return error;
  1915. }
  1916. /**
  1917. * sys_tgkill - send signal to one specific thread
  1918. * @tgid: the thread group ID of the thread
  1919. * @pid: the PID of the thread
  1920. * @sig: signal to be sent
  1921. *
  1922. * This syscall also checks the @tgid and returns -ESRCH even if the PID
  1923. * exists but it's not belonging to the target process anymore. This
  1924. * method solves the problem of threads exiting and PIDs getting reused.
  1925. */
  1926. asmlinkage long sys_tgkill(int tgid, int pid, int sig)
  1927. {
  1928. /* This is only valid for single tasks */
  1929. if (pid <= 0 || tgid <= 0)
  1930. return -EINVAL;
  1931. return do_tkill(tgid, pid, sig);
  1932. }
  1933. /*
  1934. * Send a signal to only one task, even if it's a CLONE_THREAD task.
  1935. */
  1936. asmlinkage long
  1937. sys_tkill(int pid, int sig)
  1938. {
  1939. /* This is only valid for single tasks */
  1940. if (pid <= 0)
  1941. return -EINVAL;
  1942. return do_tkill(0, pid, sig);
  1943. }
  1944. asmlinkage long
  1945. sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
  1946. {
  1947. siginfo_t info;
  1948. if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
  1949. return -EFAULT;
  1950. /* Not even root can pretend to send signals from the kernel.
  1951. Nor can they impersonate a kill(), which adds source info. */
  1952. if (info.si_code >= 0)
  1953. return -EPERM;
  1954. info.si_signo = sig;
  1955. /* POSIX.1b doesn't mention process groups. */
  1956. return kill_proc_info(sig, &info, pid);
  1957. }
  1958. int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
  1959. {
  1960. struct task_struct *t = current;
  1961. struct k_sigaction *k;
  1962. sigset_t mask;
  1963. if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
  1964. return -EINVAL;
  1965. k = &t->sighand->action[sig-1];
  1966. spin_lock_irq(&current->sighand->siglock);
  1967. if (oact)
  1968. *oact = *k;
  1969. if (act) {
  1970. sigdelsetmask(&act->sa.sa_mask,
  1971. sigmask(SIGKILL) | sigmask(SIGSTOP));
  1972. *k = *act;
  1973. /*
  1974. * POSIX 3.3.1.3:
  1975. * "Setting a signal action to SIG_IGN for a signal that is
  1976. * pending shall cause the pending signal to be discarded,
  1977. * whether or not it is blocked."
  1978. *
  1979. * "Setting a signal action to SIG_DFL for a signal that is
  1980. * pending and whose default action is to ignore the signal
  1981. * (for example, SIGCHLD), shall cause the pending signal to
  1982. * be discarded, whether or not it is blocked"
  1983. */
  1984. if (__sig_ignored(t, sig)) {
  1985. sigemptyset(&mask);
  1986. sigaddset(&mask, sig);
  1987. rm_from_queue_full(&mask, &t->signal->shared_pending);
  1988. do {
  1989. rm_from_queue_full(&mask, &t->pending);
  1990. t = next_thread(t);
  1991. } while (t != current);
  1992. }
  1993. }
  1994. spin_unlock_irq(&current->sighand->siglock);
  1995. return 0;
  1996. }
  1997. int
  1998. do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
  1999. {
  2000. stack_t oss;
  2001. int error;
  2002. if (uoss) {
  2003. oss.ss_sp = (void __user *) current->sas_ss_sp;
  2004. oss.ss_size = current->sas_ss_size;
  2005. oss.ss_flags = sas_ss_flags(sp);
  2006. }
  2007. if (uss) {
  2008. void __user *ss_sp;
  2009. size_t ss_size;
  2010. int ss_flags;
  2011. error = -EFAULT;
  2012. if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
  2013. || __get_user(ss_sp, &uss->ss_sp)
  2014. || __get_user(ss_flags, &uss->ss_flags)
  2015. || __get_user(ss_size, &uss->ss_size))
  2016. goto out;
  2017. error = -EPERM;
  2018. if (on_sig_stack(sp))
  2019. goto out;
  2020. error = -EINVAL;
  2021. /*
  2022. *
  2023. * Note - this code used to test ss_flags incorrectly
  2024. * old code may have been written using ss_flags==0
  2025. * to mean ss_flags==SS_ONSTACK (as this was the only
  2026. * way that worked) - this fix preserves that older
  2027. * mechanism
  2028. */
  2029. if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
  2030. goto out;
  2031. if (ss_flags == SS_DISABLE) {
  2032. ss_size = 0;
  2033. ss_sp = NULL;
  2034. } else {
  2035. error = -ENOMEM;
  2036. if (ss_size < MINSIGSTKSZ)
  2037. goto out;
  2038. }
  2039. current->sas_ss_sp = (unsigned long) ss_sp;
  2040. current->sas_ss_size = ss_size;
  2041. }
  2042. if (uoss) {
  2043. error = -EFAULT;
  2044. if (copy_to_user(uoss, &oss, sizeof(oss)))
  2045. goto out;
  2046. }
  2047. error = 0;
  2048. out:
  2049. return error;
  2050. }
  2051. #ifdef __ARCH_WANT_SYS_SIGPENDING
  2052. asmlinkage long
  2053. sys_sigpending(old_sigset_t __user *set)
  2054. {
  2055. return do_sigpending(set, sizeof(*set));
  2056. }
  2057. #endif
  2058. #ifdef __ARCH_WANT_SYS_SIGPROCMASK
  2059. /* Some platforms have their own version with special arguments others
  2060. support only sys_rt_sigprocmask. */
  2061. asmlinkage long
  2062. sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
  2063. {
  2064. int error;
  2065. old_sigset_t old_set, new_set;
  2066. if (set) {
  2067. error = -EFAULT;
  2068. if (copy_from_user(&new_set, set, sizeof(*set)))
  2069. goto out;
  2070. new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
  2071. spin_lock_irq(&current->sighand->siglock);
  2072. old_set = current->blocked.sig[0];
  2073. error = 0;
  2074. switch (how) {
  2075. default:
  2076. error = -EINVAL;
  2077. break;
  2078. case SIG_BLOCK:
  2079. sigaddsetmask(&current->blocked, new_set);
  2080. break;
  2081. case SIG_UNBLOCK:
  2082. sigdelsetmask(&current->blocked, new_set);
  2083. break;
  2084. case SIG_SETMASK:
  2085. current->blocked.sig[0] = new_set;
  2086. break;
  2087. }
  2088. recalc_sigpending();
  2089. spin_unlock_irq(&current->sighand->siglock);
  2090. if (error)
  2091. goto out;
  2092. if (oset)
  2093. goto set_old;
  2094. } else if (oset) {
  2095. old_set = current->blocked.sig[0];
  2096. set_old:
  2097. error = -EFAULT;
  2098. if (copy_to_user(oset, &old_set, sizeof(*oset)))
  2099. goto out;
  2100. }
  2101. error = 0;
  2102. out:
  2103. return error;
  2104. }
  2105. #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
  2106. #ifdef __ARCH_WANT_SYS_RT_SIGACTION
  2107. asmlinkage long
  2108. sys_rt_sigaction(int sig,
  2109. const struct sigaction __user *act,
  2110. struct sigaction __user *oact,
  2111. size_t sigsetsize)
  2112. {
  2113. struct k_sigaction new_sa, old_sa;
  2114. int ret = -EINVAL;
  2115. /* XXX: Don't preclude handling different sized sigset_t's. */
  2116. if (sigsetsize != sizeof(sigset_t))
  2117. goto out;
  2118. if (act) {
  2119. if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
  2120. return -EFAULT;
  2121. }
  2122. ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
  2123. if (!ret && oact) {
  2124. if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
  2125. return -EFAULT;
  2126. }
  2127. out:
  2128. return ret;
  2129. }
  2130. #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
  2131. #ifdef __ARCH_WANT_SYS_SGETMASK
  2132. /*
  2133. * For backwards compatibility. Functionality superseded by sigprocmask.
  2134. */
  2135. asmlinkage long
  2136. sys_sgetmask(void)
  2137. {
  2138. /* SMP safe */
  2139. return current->blocked.sig[0];
  2140. }
  2141. asmlinkage long
  2142. sys_ssetmask(int newmask)
  2143. {
  2144. int old;
  2145. spin_lock_irq(&current->sighand->siglock);
  2146. old = current->blocked.sig[0];
  2147. siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
  2148. sigmask(SIGSTOP)));
  2149. recalc_sigpending();
  2150. spin_unlock_irq(&current->sighand->siglock);
  2151. return old;
  2152. }
  2153. #endif /* __ARCH_WANT_SGETMASK */
  2154. #ifdef __ARCH_WANT_SYS_SIGNAL
  2155. /*
  2156. * For backwards compatibility. Functionality superseded by sigaction.
  2157. */
  2158. asmlinkage unsigned long
  2159. sys_signal(int sig, __sighandler_t handler)
  2160. {
  2161. struct k_sigaction new_sa, old_sa;
  2162. int ret;
  2163. new_sa.sa.sa_handler = handler;
  2164. new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
  2165. sigemptyset(&new_sa.sa.sa_mask);
  2166. ret = do_sigaction(sig, &new_sa, &old_sa);
  2167. return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
  2168. }
  2169. #endif /* __ARCH_WANT_SYS_SIGNAL */
  2170. #ifdef __ARCH_WANT_SYS_PAUSE
  2171. asmlinkage long
  2172. sys_pause(void)
  2173. {
  2174. current->state = TASK_INTERRUPTIBLE;
  2175. schedule();
  2176. return -ERESTARTNOHAND;
  2177. }
  2178. #endif
  2179. #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
  2180. asmlinkage long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
  2181. {
  2182. sigset_t newset;
  2183. /* XXX: Don't preclude handling different sized sigset_t's. */
  2184. if (sigsetsize != sizeof(sigset_t))
  2185. return -EINVAL;
  2186. if (copy_from_user(&newset, unewset, sizeof(newset)))
  2187. return -EFAULT;
  2188. sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
  2189. spin_lock_irq(&current->sighand->siglock);
  2190. current->saved_sigmask = current->blocked;
  2191. current->blocked = newset;
  2192. recalc_sigpending();
  2193. spin_unlock_irq(&current->sighand->siglock);
  2194. current->state = TASK_INTERRUPTIBLE;
  2195. schedule();
  2196. set_thread_flag(TIF_RESTORE_SIGMASK);
  2197. return -ERESTARTNOHAND;
  2198. }
  2199. #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
  2200. __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
  2201. {
  2202. return NULL;
  2203. }
  2204. void __init signals_init(void)
  2205. {
  2206. sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
  2207. }