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