signal.c 68 KB

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