signal.c 62 KB

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