signal.c 65 KB

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