signal.c 68 KB

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