signal.c 69 KB

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