signal.c 68 KB

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