-
Notifications
You must be signed in to change notification settings - Fork 761
/
sig.c
2889 lines (2615 loc) · 72.6 KB
/
sig.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
* Copyright (c) 2014, Joyent, Inc. All rights reserved.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <sys/param.h>
#include <sys/types.h>
#include <sys/bitmap.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/poll_impl.h> /* only needed for kludge in sigwaiting_send() */
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/fault.h>
#include <sys/ucontext.h>
#include <sys/procfs.h>
#include <sys/wait.h>
#include <sys/class.h>
#include <sys/mman.h>
#include <sys/procset.h>
#include <sys/kmem.h>
#include <sys/cpuvar.h>
#include <sys/prsystm.h>
#include <sys/debug.h>
#include <vm/as.h>
#include <sys/bitmap.h>
#include <c2/audit.h>
#include <sys/core.h>
#include <sys/schedctl.h>
#include <sys/contract/process_impl.h>
#include <sys/cyclic.h>
#include <sys/dtrace.h>
#include <sys/sdt.h>
#include <sys/signalfd.h>
const k_sigset_t nullsmask = {0, 0, 0};
const k_sigset_t fillset = /* MUST be contiguous */
{FILLSET0, FILLSET1, FILLSET2};
const k_sigset_t cantmask =
{CANTMASK0, CANTMASK1, CANTMASK2};
const k_sigset_t cantreset =
{(sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGPWR)), 0, 0};
const k_sigset_t ignoredefault =
{(sigmask(SIGCONT)|sigmask(SIGCLD)|sigmask(SIGPWR)
|sigmask(SIGWINCH)|sigmask(SIGURG)|sigmask(SIGWAITING)),
(sigmask(SIGLWP)|sigmask(SIGCANCEL)|sigmask(SIGFREEZE)
|sigmask(SIGTHAW)|sigmask(SIGXRES)|sigmask(SIGJVM1)
|sigmask(SIGJVM2)|sigmask(SIGINFO)), 0};
const k_sigset_t stopdefault =
{(sigmask(SIGSTOP)|sigmask(SIGTSTP)|sigmask(SIGTTOU)|sigmask(SIGTTIN)),
0, 0};
const k_sigset_t coredefault =
{(sigmask(SIGQUIT)|sigmask(SIGILL)|sigmask(SIGTRAP)|sigmask(SIGIOT)
|sigmask(SIGEMT)|sigmask(SIGFPE)|sigmask(SIGBUS)|sigmask(SIGSEGV)
|sigmask(SIGSYS)|sigmask(SIGXCPU)|sigmask(SIGXFSZ)), 0, 0};
const k_sigset_t holdvfork =
{(sigmask(SIGTTOU)|sigmask(SIGTTIN)|sigmask(SIGTSTP)), 0, 0};
static int isjobstop(int);
static void post_sigcld(proc_t *, sigqueue_t *);
/*
* signalfd helper function which is set when the signalfd driver loads.
*/
void (*sigfd_exit_helper)();
/*
* Internal variables for counting number of user thread stop requests posted.
* They may not be accurate at some special situation such as that a virtually
* stopped thread starts to run.
*/
static int num_utstop;
/*
* Internal variables for broadcasting an event when all thread stop requests
* are processed.
*/
static kcondvar_t utstop_cv;
static kmutex_t thread_stop_lock;
void del_one_utstop(void);
/*
* Send the specified signal to the specified process.
*/
void
psignal(proc_t *p, int sig)
{
mutex_enter(&p->p_lock);
sigtoproc(p, NULL, sig);
mutex_exit(&p->p_lock);
}
/*
* Send the specified signal to the specified thread.
*/
void
tsignal(kthread_t *t, int sig)
{
proc_t *p = ttoproc(t);
mutex_enter(&p->p_lock);
sigtoproc(p, t, sig);
mutex_exit(&p->p_lock);
}
int
signal_is_blocked(kthread_t *t, int sig)
{
return (sigismember(&t->t_hold, sig) ||
(schedctl_sigblock(t) && !sigismember(&cantmask, sig)));
}
/*
* Return true if the signal can safely be discarded on generation.
* That is, if there is no need for the signal on the receiving end.
* The answer is true if the process is a zombie or
* if all of these conditions are true:
* the signal is being ignored
* the process is single-threaded
* the signal is not being traced by /proc
* the signal is not blocked by the process
* the signal is not being accepted via sigwait()
*/
static int
sig_discardable(proc_t *p, int sig)
{
kthread_t *t = p->p_tlist;
return (t == NULL || /* if zombie or ... */
(sigismember(&p->p_ignore, sig) && /* signal is ignored */
t->t_forw == t && /* and single-threaded */
!tracing(p, sig) && /* and no /proc tracing */
!signal_is_blocked(t, sig) && /* and signal not blocked */
!sigismember(&t->t_sigwait, sig))); /* and not being accepted */
}
/*
* Return true if this thread is going to eat this signal soon.
* Note that, if the signal is SIGKILL, we force stopped threads to be
* set running (to make SIGKILL be a sure kill), but only if the process
* is not currently locked by /proc (the P_PR_LOCK flag). Code in /proc
* relies on the fact that a process will not change shape while P_PR_LOCK
* is set (it drops and reacquires p->p_lock while leaving P_PR_LOCK set).
* We wish that we could simply call prbarrier() below, in sigtoproc(), to
* ensure that the process is not locked by /proc, but prbarrier() drops
* and reacquires p->p_lock and dropping p->p_lock here would be damaging.
*/
int
eat_signal(kthread_t *t, int sig)
{
int rval = 0;
ASSERT(THREAD_LOCK_HELD(t));
/*
* Do not do anything if the target thread has the signal blocked.
*/
if (!signal_is_blocked(t, sig)) {
t->t_sig_check = 1; /* have thread do an issig */
if (ISWAKEABLE(t) || ISWAITING(t)) {
setrun_locked(t);
rval = 1;
} else if (t->t_state == TS_STOPPED && sig == SIGKILL &&
!(ttoproc(t)->p_proc_flag & P_PR_LOCK)) {
ttoproc(t)->p_stopsig = 0;
t->t_dtrace_stop = 0;
t->t_schedflag |= TS_XSTART | TS_PSTART;
setrun_locked(t);
} else if (t != curthread && t->t_state == TS_ONPROC) {
aston(t); /* make it do issig promptly */
if (t->t_cpu != CPU)
poke_cpu(t->t_cpu->cpu_id);
rval = 1;
} else if (t->t_state == TS_RUN) {
rval = 1;
}
}
return (rval);
}
/*
* Post a signal.
* If a non-null thread pointer is passed, then post the signal
* to the thread/lwp, otherwise post the signal to the process.
*/
void
sigtoproc(proc_t *p, kthread_t *t, int sig)
{
kthread_t *tt;
int ext = !(curproc->p_flag & SSYS) &&
(curproc->p_ct_process != p->p_ct_process);
ASSERT(MUTEX_HELD(&p->p_lock));
/* System processes don't get signals */
if (sig <= 0 || sig >= NSIG || (p->p_flag & SSYS))
return;
/*
* Regardless of origin or directedness,
* SIGKILL kills all lwps in the process immediately
* and jobcontrol signals affect all lwps in the process.
*/
if (sig == SIGKILL) {
p->p_flag |= SKILLED | (ext ? SEXTKILLED : 0);
t = NULL;
} else if (sig == SIGCONT) {
/*
* The SSCONT flag will remain set until a stopping
* signal comes in (below). This is harmless.
*/
p->p_flag |= SSCONT;
sigdelq(p, NULL, SIGSTOP);
sigdelq(p, NULL, SIGTSTP);
sigdelq(p, NULL, SIGTTOU);
sigdelq(p, NULL, SIGTTIN);
sigdiffset(&p->p_sig, &stopdefault);
sigdiffset(&p->p_extsig, &stopdefault);
p->p_stopsig = 0;
if ((tt = p->p_tlist) != NULL) {
do {
sigdelq(p, tt, SIGSTOP);
sigdelq(p, tt, SIGTSTP);
sigdelq(p, tt, SIGTTOU);
sigdelq(p, tt, SIGTTIN);
sigdiffset(&tt->t_sig, &stopdefault);
sigdiffset(&tt->t_extsig, &stopdefault);
} while ((tt = tt->t_forw) != p->p_tlist);
}
if ((tt = p->p_tlist) != NULL) {
do {
thread_lock(tt);
if (tt->t_state == TS_STOPPED &&
tt->t_whystop == PR_JOBCONTROL) {
tt->t_schedflag |= TS_XSTART;
setrun_locked(tt);
}
thread_unlock(tt);
} while ((tt = tt->t_forw) != p->p_tlist);
}
} else if (sigismember(&stopdefault, sig)) {
/*
* This test has a race condition which we can't fix:
* By the time the stopping signal is received by
* the target process/thread, the signal handler
* and/or the detached state might have changed.
*/
if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
(sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
p->p_flag &= ~SSCONT;
sigdelq(p, NULL, SIGCONT);
sigdelset(&p->p_sig, SIGCONT);
sigdelset(&p->p_extsig, SIGCONT);
if ((tt = p->p_tlist) != NULL) {
do {
sigdelq(p, tt, SIGCONT);
sigdelset(&tt->t_sig, SIGCONT);
sigdelset(&tt->t_extsig, SIGCONT);
} while ((tt = tt->t_forw) != p->p_tlist);
}
}
if (sig_discardable(p, sig)) {
DTRACE_PROC3(signal__discard, kthread_t *, p->p_tlist,
proc_t *, p, int, sig);
return;
}
if (t != NULL) {
/*
* This is a directed signal, wake up the lwp.
*/
sigaddset(&t->t_sig, sig);
if (ext)
sigaddset(&t->t_extsig, sig);
thread_lock(t);
(void) eat_signal(t, sig);
thread_unlock(t);
DTRACE_PROC2(signal__send, kthread_t *, t, int, sig);
if (p->p_sigfd != NULL && ((sigfd_proc_state_t *)
(p->p_sigfd))->sigfd_pollwake_cb != NULL)
(*((sigfd_proc_state_t *)(p->p_sigfd))->
sigfd_pollwake_cb)(p, sig);
} else if ((tt = p->p_tlist) != NULL) {
/*
* Make sure that some lwp that already exists
* in the process fields the signal soon.
* Wake up an interruptibly sleeping lwp if necessary.
* For SIGKILL make all of the lwps see the signal;
* This is needed to guarantee a sure kill for processes
* with a mix of realtime and non-realtime threads.
*/
int su = 0;
sigaddset(&p->p_sig, sig);
if (ext)
sigaddset(&p->p_extsig, sig);
do {
thread_lock(tt);
if (eat_signal(tt, sig) && sig != SIGKILL) {
thread_unlock(tt);
break;
}
if (SUSPENDED(tt))
su++;
thread_unlock(tt);
} while ((tt = tt->t_forw) != p->p_tlist);
/*
* If the process is deadlocked, make somebody run and die.
*/
if (sig == SIGKILL && p->p_stat != SIDL &&
p->p_lwprcnt == 0 && p->p_lwpcnt == su &&
!(p->p_proc_flag & P_PR_LOCK)) {
thread_lock(tt);
p->p_lwprcnt++;
tt->t_schedflag |= TS_CSTART;
setrun_locked(tt);
thread_unlock(tt);
}
DTRACE_PROC2(signal__send, kthread_t *, tt, int, sig);
if (p->p_sigfd != NULL && ((sigfd_proc_state_t *)
(p->p_sigfd))->sigfd_pollwake_cb != NULL)
(*((sigfd_proc_state_t *)(p->p_sigfd))->
sigfd_pollwake_cb)(p, sig);
}
}
static int
isjobstop(int sig)
{
proc_t *p = ttoproc(curthread);
ASSERT(MUTEX_HELD(&p->p_lock));
if (PTOU(curproc)->u_signal[sig-1] == SIG_DFL &&
sigismember(&stopdefault, sig)) {
/*
* If SIGCONT has been posted since we promoted this signal
* from pending to current, then don't do a jobcontrol stop.
*/
if (!(p->p_flag & SSCONT) &&
(sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned) &&
curthread != p->p_agenttp) {
sigqueue_t *sqp;
stop(PR_JOBCONTROL, sig);
mutex_exit(&p->p_lock);
sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
mutex_enter(&pidlock);
/*
* Only the first lwp to continue notifies the parent.
*/
if (p->p_pidflag & CLDCONT)
siginfofree(sqp);
else {
p->p_pidflag |= CLDCONT;
p->p_wcode = CLD_CONTINUED;
p->p_wdata = SIGCONT;
sigcld(p, sqp);
}
mutex_exit(&pidlock);
mutex_enter(&p->p_lock);
}
return (1);
}
return (0);
}
/*
* Returns true if the current process has a signal to process, and
* the signal is not held. The signal to process is put in p_cursig.
* This is asked at least once each time a process enters the system
* (though this can usually be done without actually calling issig by
* checking the pending signal masks). A signal does not do anything
* directly to a process; it sets a flag that asks the process to do
* something to itself.
*
* The "why" argument indicates the allowable side-effects of the call:
*
* FORREAL: Extract the next pending signal from p_sig into p_cursig;
* stop the process if a stop has been requested or if a traced signal
* is pending.
*
* JUSTLOOKING: Don't stop the process, just indicate whether or not
* a signal might be pending (FORREAL is needed to tell for sure).
*
* XXX: Changes to the logic in these routines should be propagated
* to lm_sigispending(). See bug 1201594.
*/
static int issig_forreal(void);
static int issig_justlooking(void);
int
issig(int why)
{
ASSERT(why == FORREAL || why == JUSTLOOKING);
return ((why == FORREAL)? issig_forreal() : issig_justlooking());
}
static int
issig_justlooking(void)
{
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
k_sigset_t set;
/*
* This function answers the question:
* "Is there any reason to call issig_forreal()?"
*
* We have to answer the question w/o grabbing any locks
* because we are (most likely) being called after we
* put ourselves on the sleep queue.
*/
if (t->t_dtrace_stop | t->t_dtrace_sig)
return (1);
/*
* Another piece of complexity in this process. When single-stepping a
* process, we don't want an intervening signal or TP_PAUSE request to
* suspend the current thread. Otherwise, the controlling process will
* hang beacuse we will be stopped with TS_PSTART set in t_schedflag.
* We will trigger any remaining signals when we re-enter the kernel on
* the single step trap.
*/
if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP)
return (0);
if ((lwp->lwp_asleep && MUSTRETURN(p, t)) ||
(p->p_flag & (SEXITLWPS|SKILLED)) ||
(lwp->lwp_nostop == 0 &&
(p->p_stopsig | (p->p_flag & (SHOLDFORK1|SHOLDWATCH)) |
(t->t_proc_flag &
(TP_PRSTOP|TP_HOLDLWP|TP_CHKPT|TP_PAUSE)))) ||
lwp->lwp_cursig)
return (1);
if (p->p_flag & SVFWAIT)
return (0);
set = p->p_sig;
sigorset(&set, &t->t_sig);
if (schedctl_sigblock(t)) /* all blockable signals blocked */
sigandset(&set, &cantmask);
else
sigdiffset(&set, &t->t_hold);
if (p->p_flag & SVFORK)
sigdiffset(&set, &holdvfork);
if (!sigisempty(&set)) {
int sig;
for (sig = 1; sig < NSIG; sig++) {
if (sigismember(&set, sig) &&
(tracing(p, sig) ||
sigismember(&t->t_sigwait, sig) ||
!sigismember(&p->p_ignore, sig))) {
/*
* Don't promote a signal that will stop
* the process when lwp_nostop is set.
*/
if (!lwp->lwp_nostop ||
PTOU(p)->u_signal[sig-1] != SIG_DFL ||
!sigismember(&stopdefault, sig))
return (1);
}
}
}
return (0);
}
static int
issig_forreal(void)
{
int sig = 0, ext = 0;
kthread_t *t = curthread;
klwp_t *lwp = ttolwp(t);
proc_t *p = ttoproc(t);
int toproc = 0;
int sigcld_found = 0;
int nostop_break = 0;
ASSERT(t->t_state == TS_ONPROC);
mutex_enter(&p->p_lock);
schedctl_finish_sigblock(t);
if (t->t_dtrace_stop | t->t_dtrace_sig) {
if (t->t_dtrace_stop) {
/*
* If DTrace's "stop" action has been invoked on us,
* set TP_PRSTOP.
*/
t->t_proc_flag |= TP_PRSTOP;
}
if (t->t_dtrace_sig != 0) {
k_siginfo_t info;
/*
* Post the signal generated as the result of
* DTrace's "raise" action as a normal signal before
* the full-fledged signal checking begins.
*/
bzero(&info, sizeof (info));
info.si_signo = t->t_dtrace_sig;
info.si_code = SI_DTRACE;
sigaddq(p, NULL, &info, KM_NOSLEEP);
t->t_dtrace_sig = 0;
}
}
for (;;) {
if (p->p_flag & (SEXITLWPS|SKILLED)) {
lwp->lwp_cursig = sig = SIGKILL;
lwp->lwp_extsig = ext = (p->p_flag & SEXTKILLED) != 0;
t->t_sig_check = 1;
break;
}
/*
* Another piece of complexity in this process. When
* single-stepping a process, we don't want an intervening
* signal or TP_PAUSE request to suspend the current thread.
* Otherwise, the controlling process will hang beacuse we will
* be stopped with TS_PSTART set in t_schedflag. We will
* trigger any remaining signals when we re-enter the kernel on
* the single step trap.
*/
if (lwp->lwp_pcb.pcb_flags & NORMAL_STEP) {
sig = 0;
break;
}
/*
* Hold the lwp here for watchpoint manipulation.
*/
if ((t->t_proc_flag & TP_PAUSE) && !lwp->lwp_nostop) {
stop(PR_SUSPENDED, SUSPEND_PAUSE);
continue;
}
if (lwp->lwp_asleep && MUSTRETURN(p, t)) {
if ((sig = lwp->lwp_cursig) != 0) {
/*
* Make sure we call ISSIG() in post_syscall()
* to re-validate this current signal.
*/
t->t_sig_check = 1;
}
break;
}
/*
* If the request is PR_CHECKPOINT, ignore the rest of signals
* or requests. Honor other stop requests or signals later.
* Go back to top of loop here to check if an exit or hold
* event has occurred while stopped.
*/
if ((t->t_proc_flag & TP_CHKPT) && !lwp->lwp_nostop) {
stop(PR_CHECKPOINT, 0);
continue;
}
/*
* Honor SHOLDFORK1, SHOLDWATCH, and TP_HOLDLWP before dealing
* with signals or /proc. Another lwp is executing fork1(),
* or is undergoing watchpoint activity (remapping a page),
* or is executing lwp_suspend() on this lwp.
* Again, go back to top of loop to check if an exit
* or hold event has occurred while stopped.
*/
if (((p->p_flag & (SHOLDFORK1|SHOLDWATCH)) ||
(t->t_proc_flag & TP_HOLDLWP)) && !lwp->lwp_nostop) {
stop(PR_SUSPENDED, SUSPEND_NORMAL);
continue;
}
/*
* Honor requested stop before dealing with the
* current signal; a debugger may change it.
* Do not want to go back to loop here since this is a special
* stop that means: make incremental progress before the next
* stop. The danger is that returning to top of loop would most
* likely drop the thread right back here to stop soon after it
* was continued, violating the incremental progress request.
*/
if ((t->t_proc_flag & TP_PRSTOP) && !lwp->lwp_nostop)
stop(PR_REQUESTED, 0);
/*
* If a debugger wants us to take a signal it will have
* left it in lwp->lwp_cursig. If lwp_cursig has been cleared
* or if it's being ignored, we continue on looking for another
* signal. Otherwise we return the specified signal, provided
* it's not a signal that causes a job control stop.
*
* When stopped on PR_JOBCONTROL, there is no current
* signal; we cancel lwp->lwp_cursig temporarily before
* calling isjobstop(). The current signal may be reset
* by a debugger while we are stopped in isjobstop().
*
* If the current thread is accepting the signal
* (via sigwait(), sigwaitinfo(), or sigtimedwait()),
* we allow the signal to be accepted, even if it is
* being ignored, and without causing a job control stop.
*/
if ((sig = lwp->lwp_cursig) != 0) {
ext = lwp->lwp_extsig;
lwp->lwp_cursig = 0;
lwp->lwp_extsig = 0;
if (sigismember(&t->t_sigwait, sig) ||
(!sigismember(&p->p_ignore, sig) &&
!isjobstop(sig))) {
if (p->p_flag & (SEXITLWPS|SKILLED)) {
sig = SIGKILL;
ext = (p->p_flag & SEXTKILLED) != 0;
}
lwp->lwp_cursig = (uchar_t)sig;
lwp->lwp_extsig = (uchar_t)ext;
break;
}
/*
* The signal is being ignored or it caused a
* job-control stop. If another current signal
* has not been established, return the current
* siginfo, if any, to the memory manager.
*/
if (lwp->lwp_cursig == 0 && lwp->lwp_curinfo != NULL) {
siginfofree(lwp->lwp_curinfo);
lwp->lwp_curinfo = NULL;
}
/*
* Loop around again in case we were stopped
* on a job control signal and a /proc stop
* request was posted or another current signal
* was established while we were stopped.
*/
continue;
}
if (p->p_stopsig && !lwp->lwp_nostop &&
curthread != p->p_agenttp) {
/*
* Some lwp in the process has already stopped
* showing PR_JOBCONTROL. This is a stop in
* sympathy with the other lwp, even if this
* lwp is blocking the stopping signal.
*/
stop(PR_JOBCONTROL, p->p_stopsig);
continue;
}
/*
* Loop on the pending signals until we find a
* non-held signal that is traced or not ignored.
* First check the signals pending for the lwp,
* then the signals pending for the process as a whole.
*/
for (;;) {
if ((sig = fsig(&t->t_sig, t)) != 0) {
toproc = 0;
if (tracing(p, sig) ||
sigismember(&t->t_sigwait, sig) ||
!sigismember(&p->p_ignore, sig)) {
if (sigismember(&t->t_extsig, sig))
ext = 1;
break;
}
sigdelset(&t->t_sig, sig);
sigdelset(&t->t_extsig, sig);
sigdelq(p, t, sig);
} else if ((sig = fsig(&p->p_sig, t)) != 0) {
if (sig == SIGCLD)
sigcld_found = 1;
toproc = 1;
if (tracing(p, sig) ||
sigismember(&t->t_sigwait, sig) ||
!sigismember(&p->p_ignore, sig)) {
if (sigismember(&p->p_extsig, sig))
ext = 1;
break;
}
sigdelset(&p->p_sig, sig);
sigdelset(&p->p_extsig, sig);
sigdelq(p, NULL, sig);
} else {
/* no signal was found */
break;
}
}
if (sig == 0) { /* no signal was found */
if (p->p_flag & (SEXITLWPS|SKILLED)) {
lwp->lwp_cursig = SIGKILL;
sig = SIGKILL;
ext = (p->p_flag & SEXTKILLED) != 0;
}
break;
}
/*
* If we have been informed not to stop (i.e., we are being
* called from within a network operation), then don't promote
* the signal at this time, just return the signal number.
* We will call issig() again later when it is safe.
*
* fsig() does not return a jobcontrol stopping signal
* with a default action of stopping the process if
* lwp_nostop is set, so we won't be causing a bogus
* EINTR by this action. (Such a signal is eaten by
* isjobstop() when we loop around to do final checks.)
*/
if (lwp->lwp_nostop) {
nostop_break = 1;
break;
}
/*
* Promote the signal from pending to current.
*
* Note that sigdeq() will set lwp->lwp_curinfo to NULL
* if no siginfo_t exists for this signal.
*/
lwp->lwp_cursig = (uchar_t)sig;
lwp->lwp_extsig = (uchar_t)ext;
t->t_sig_check = 1; /* so post_syscall will see signal */
ASSERT(lwp->lwp_curinfo == NULL);
sigdeq(p, toproc ? NULL : t, sig, &lwp->lwp_curinfo);
if (tracing(p, sig))
stop(PR_SIGNALLED, sig);
/*
* Loop around to check for requested stop before
* performing the usual current-signal actions.
*/
}
mutex_exit(&p->p_lock);
/*
* If SIGCLD was dequeued from the process's signal queue,
* search for other pending SIGCLD's from the list of children.
*/
if (sigcld_found)
sigcld_repost();
if (sig != 0)
(void) undo_watch_step(NULL);
/*
* If we have been blocked since the p_lock was dropped off
* above, then this promoted signal might have been handled
* already when we were on the way back from sleep queue, so
* just ignore it.
* If we have been informed not to stop, just return the signal
* number. Also see comments above.
*/
if (!nostop_break) {
sig = lwp->lwp_cursig;
}
return (sig != 0);
}
/*
* Return true if the process is currently stopped showing PR_JOBCONTROL.
* This is true only if all of the process's lwp's are so stopped.
* If this is asked by one of the lwps in the process, exclude that lwp.
*/
int
jobstopped(proc_t *p)
{
kthread_t *t;
ASSERT(MUTEX_HELD(&p->p_lock));
if ((t = p->p_tlist) == NULL)
return (0);
do {
thread_lock(t);
/* ignore current, zombie and suspended lwps in the test */
if (!(t == curthread || t->t_state == TS_ZOMB ||
SUSPENDED(t)) &&
(t->t_state != TS_STOPPED ||
t->t_whystop != PR_JOBCONTROL)) {
thread_unlock(t);
return (0);
}
thread_unlock(t);
} while ((t = t->t_forw) != p->p_tlist);
return (1);
}
/*
* Put ourself (curthread) into the stopped state and notify tracers.
*/
void
stop(int why, int what)
{
kthread_t *t = curthread;
proc_t *p = ttoproc(t);
klwp_t *lwp = ttolwp(t);
kthread_t *tx;
lwpent_t *lep;
int procstop;
int flags = TS_ALLSTART;
hrtime_t stoptime;
/*
* Can't stop a system process.
*/
if (p == NULL || lwp == NULL || (p->p_flag & SSYS) || p->p_as == &kas)
return;
ASSERT(MUTEX_HELD(&p->p_lock));
if (why != PR_SUSPENDED && why != PR_CHECKPOINT) {
/*
* Don't stop an lwp with SIGKILL pending.
* Don't stop if the process or lwp is exiting.
*/
if (lwp->lwp_cursig == SIGKILL ||
sigismember(&t->t_sig, SIGKILL) ||
sigismember(&p->p_sig, SIGKILL) ||
(t->t_proc_flag & TP_LWPEXIT) ||
(p->p_flag & (SEXITLWPS|SKILLED))) {
p->p_stopsig = 0;
t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP);
return;
}
}
/*
* Make sure we don't deadlock on a recursive call to prstop().
* prstop() sets the lwp_nostop flag.
*/
if (lwp->lwp_nostop)
return;
/*
* Make sure the lwp is in an orderly state for inspection
* by a debugger through /proc or for dumping via core().
*/
schedctl_finish_sigblock(t);
t->t_proc_flag |= TP_STOPPING; /* must set before dropping p_lock */
mutex_exit(&p->p_lock);
stoptime = gethrtime();
prstop(why, what);
(void) undo_watch_step(NULL);
mutex_enter(&p->p_lock);
ASSERT(t->t_state == TS_ONPROC);
switch (why) {
case PR_CHECKPOINT:
/*
* The situation may have changed since we dropped
* and reacquired p->p_lock. Double-check now
* whether we should stop or not.
*/
if (!(t->t_proc_flag & TP_CHKPT)) {
t->t_proc_flag &= ~TP_STOPPING;
return;
}
t->t_proc_flag &= ~TP_CHKPT;
flags &= ~TS_RESUME;
break;
case PR_JOBCONTROL:
ASSERT(what == SIGSTOP || what == SIGTSTP ||
what == SIGTTIN || what == SIGTTOU);
flags &= ~TS_XSTART;
break;
case PR_SUSPENDED:
ASSERT(what == SUSPEND_NORMAL || what == SUSPEND_PAUSE);
/*
* The situation may have changed since we dropped
* and reacquired p->p_lock. Double-check now
* whether we should stop or not.
*/
if (what == SUSPEND_PAUSE) {
if (!(t->t_proc_flag & TP_PAUSE)) {
t->t_proc_flag &= ~TP_STOPPING;
return;
}
flags &= ~TS_UNPAUSE;
} else {
if (!((t->t_proc_flag & TP_HOLDLWP) ||
(p->p_flag & (SHOLDFORK|SHOLDFORK1|SHOLDWATCH)))) {
t->t_proc_flag &= ~TP_STOPPING;
return;
}
/*
* If SHOLDFORK is in effect and we are stopping
* while asleep (not at the top of the stack),
* we return now to allow the hold to take effect
* when we reach the top of the kernel stack.
*/
if (lwp->lwp_asleep && (p->p_flag & SHOLDFORK)) {
t->t_proc_flag &= ~TP_STOPPING;
return;
}
flags &= ~TS_CSTART;
}
break;
default: /* /proc stop */
flags &= ~TS_PSTART;
/*
* Do synchronous stop unless the async-stop flag is set.
* If why is PR_REQUESTED and t->t_dtrace_stop flag is set,
* then no debugger is present and we also do synchronous stop.
*/
if ((why != PR_REQUESTED || t->t_dtrace_stop) &&
!(p->p_proc_flag & P_PR_ASYNC)) {
int notify;
for (tx = t->t_forw; tx != t; tx = tx->t_forw) {
notify = 0;
thread_lock(tx);
if (ISTOPPED(tx) ||
(tx->t_proc_flag & TP_PRSTOP)) {
thread_unlock(tx);
continue;
}
tx->t_proc_flag |= TP_PRSTOP;
tx->t_sig_check = 1;
if (tx->t_state == TS_SLEEP &&
(tx->t_flag & T_WAKEABLE)) {
/*
* Don't actually wake it up if it's
* in one of the lwp_*() syscalls.
* Mark it virtually stopped and
* notify /proc waiters (below).
*/
if (tx->t_wchan0 == NULL)
setrun_locked(tx);
else {
tx->t_proc_flag |= TP_PRVSTOP;
tx->t_stoptime = stoptime;
notify = 1;
}
}
/* Move waiting thread to run queue */
if (ISWAITING(tx))
setrun_locked(tx);
/*
* force the thread into the kernel