signal.c 68 KB

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