signal.c 69 KB

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