signal.c 64 KB

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