signal.c 71 KB

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