signal.c 67 KB

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