signal.c 69 KB

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