signal.c 69 KB

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