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
|
/*
* 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 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI" /* from SVr4.0 1.25 */
#include <sys/types.h>
#include <sys/sysmacros.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/vfs.h>
#include <sys/cred.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/kmem.h>
#include <sys/user.h>
#include <sys/buf.h>
#include <sys/var.h>
#include <sys/conf.h>
#include <sys/debug.h>
#include <sys/proc.h>
#include <sys/signal.h>
#include <sys/siginfo.h>
#include <sys/acct.h>
#include <sys/procset.h>
#include <sys/cmn_err.h>
#include <sys/fault.h>
#include <sys/syscall.h>
#include <sys/ucontext.h>
#include <sys/procfs.h>
#include <sys/session.h>
#include <sys/task.h>
#include <sys/project.h>
#include <sys/pool.h>
#include <sys/zone.h>
#include <sys/contract/process_impl.h>
id_t getmyid(idtype_t);
int checkprocset(procset_t *);
static kthread_t *getlwpptr(id_t);
int procinset(proc_t *, procset_t *);
static int lwpinset(proc_t *, procset_t *, kthread_t *, int *);
/*
* The dotoprocs function locates the process(es) specified
* by the procset structure pointed to by psp. If funcp
* is non-NULL then it points to a function which dotoprocs
* will call for each process in the specified set. The
* arguments to this function will be a pointer to the
* current process from the set and arg.
* If the called function returns -1, it means that processing of the
* procset should stop and a normal (non-error) return should be made
* to the caller of dotoprocs.
* If the called function returns any other non-zero value the search
* is terminated and the function's return value is returned to
* the caller of dotoprocs. This will normally be an error code.
* Otherwise, dotoprocs will return zero after processing the entire
* process set unless no processes were found in which case ESRCH will
* be returned.
*/
int
dotoprocs(procset_t *psp, int (*funcp)(), char *arg)
{
proc_t *prp; /* A process from the set */
int error;
int nfound; /* Nbr of processes found. */
proc_t *lastprp; /* Last proc found. */
/*
* Check that the procset_t is valid.
*/
error = checkprocset(psp);
if (error) {
return (error);
}
/*
* Check for the special value P_MYID in either operand
* and replace it with the correct value. We don't check
* for an error return from getmyid() because the idtypes
* have been validated by the checkprocset() call above.
*/
mutex_enter(&pidlock);
if (psp->p_lid == P_MYID) {
psp->p_lid = getmyid(psp->p_lidtype);
}
if (psp->p_rid == P_MYID) {
psp->p_rid = getmyid(psp->p_ridtype);
}
/*
* If psp only acts on a single proc, we can reduce pidlock hold time
* by avoiding a needless scan of the entire proc list. Although
* there are many procset_t combinations which might boil down to a
* single proc, the most common case is an AND operation where one
* side is a specific pid, and the other side is P_ALL, so that is
* the case for which we will provide a fast path. Other cases could
* be added in a similar fashion if they were to become significant
* pidlock bottlenecks.
*
* Perform the check symmetrically: either the left or right side may
* specify a pid, with the opposite side being 'all'.
*/
if (psp->p_op == POP_AND) {
if (((psp->p_lidtype == P_PID) && (psp->p_ridtype == P_ALL)) ||
((psp->p_ridtype == P_PID) && (psp->p_lidtype == P_ALL))) {
id_t pid;
pid = (psp->p_lidtype == P_PID) ?
psp->p_lid : psp->p_rid;
if ((prp = prfind((pid_t)pid)) == NULL) {
/* specified proc doesn't exist */
mutex_exit(&pidlock);
return (ESRCH);
}
/* operate only on the specified proc */
error = (*funcp)(prp, arg);
mutex_exit(&pidlock);
if (error == -1)
error = 0;
return (error);
}
}
nfound = 0;
error = 0;
for (prp = practive; prp != NULL; prp = prp->p_next) {
/*
* If caller is in a non-global zone, skip processes
* in other zones.
*/
if (!HASZONEACCESS(curproc, prp->p_zone->zone_id))
continue;
if (prp->p_stat == SIDL || prp->p_stat == SZOMB ||
prp->p_tlist == NULL || prp->p_flag & SSYS)
continue;
if (procinset(prp, psp)) {
nfound++;
lastprp = prp;
if (funcp != NULL && prp != proc_init) {
error = (*funcp)(prp, arg);
if (error == -1) {
mutex_exit(&pidlock);
return (0);
} else if (error) {
mutex_exit(&pidlock);
return (error);
}
}
}
}
if (nfound == 0) {
mutex_exit(&pidlock);
return (ESRCH);
}
if (nfound == 1 && lastprp == proc_init && funcp != NULL)
error = (*funcp)(lastprp, arg);
if (error == -1)
error = 0;
mutex_exit(&pidlock);
return (error);
}
/*
* Check if a procset_t is valid. Return zero or an errno.
*/
int
checkprocset(procset_t *psp)
{
switch (psp->p_lidtype) {
case P_LWPID:
case P_PID:
case P_PPID:
case P_PGID:
case P_SID:
case P_TASKID:
case P_CID:
case P_UID:
case P_GID:
case P_PROJID:
case P_POOLID:
case P_ZONEID:
case P_CTID:
case P_ALL:
break;
default:
return (EINVAL);
}
switch (psp->p_ridtype) {
case P_LWPID:
case P_PID:
case P_PPID:
case P_PGID:
case P_SID:
case P_TASKID:
case P_CID:
case P_UID:
case P_GID:
case P_PROJID:
case P_POOLID:
case P_ZONEID:
case P_CTID:
case P_ALL:
break;
default:
return (EINVAL);
}
switch (psp->p_op) {
case POP_DIFF:
case POP_AND:
case POP_OR:
case POP_XOR:
break;
default:
return (EINVAL);
}
return (0);
}
/*
* procinset returns 1 if the process pointed to
* by pp is in the process set specified by psp, otherwise 0 is returned.
* The caller should check that the process is not exiting and is not
* in the SYS scheduling class.
*
* This function expects to be called with a valid procset_t.
* The set should be checked using checkprocset() before calling
* this function.
*/
int
procinset(proc_t *pp, procset_t *psp)
{
int loperand = 0;
int roperand = 0;
int lwplinproc = 0;
int lwprinproc = 0;
kthread_t *tp = proctot(pp);
switch (psp->p_lidtype) {
case P_LWPID:
if (pp == ttoproc(curthread))
if (getlwpptr(psp->p_lid) != NULL)
lwplinproc++;
break;
case P_PID:
if (pp->p_pid == psp->p_lid)
loperand++;
break;
case P_PPID:
if (pp->p_ppid == psp->p_lid)
loperand++;
break;
case P_PGID:
if (pp->p_pgrp == psp->p_lid)
loperand++;
break;
case P_SID:
mutex_enter(&pp->p_splock);
if (pp->p_sessp->s_sid == psp->p_lid)
loperand++;
mutex_exit(&pp->p_splock);
break;
case P_CID:
ASSERT(tp != NULL);
/* This case is broken for now. Need to be fixed XXX */
if (tp->t_cid == psp->p_lid)
/*
* if (checkcid(psp->p_lid))
*/
loperand++;
break;
case P_TASKID:
if (pp->p_task->tk_tkid == psp->p_lid)
loperand++;
break;
case P_UID:
mutex_enter(&pp->p_crlock);
if (crgetuid(pp->p_cred) == psp->p_lid)
loperand++;
mutex_exit(&pp->p_crlock);
break;
case P_GID:
mutex_enter(&pp->p_crlock);
if (crgetgid(pp->p_cred) == psp->p_lid)
loperand++;
mutex_exit(&pp->p_crlock);
break;
case P_PROJID:
if (pp->p_task->tk_proj->kpj_id == psp->p_lid)
loperand++;
break;
case P_POOLID:
if (pp->p_pool->pool_id == psp->p_lid)
loperand++;
break;
case P_ZONEID:
if (pp->p_zone->zone_id == psp->p_lid)
loperand++;
break;
case P_CTID:
if (PRCTID(pp) == psp->p_lid)
loperand++;
break;
case P_ALL:
loperand++;
break;
default:
#ifdef DEBUG
cmn_err(CE_WARN, "procinset called with bad set");
return (0);
#else
return (0);
#endif
}
switch (psp->p_ridtype) {
case P_LWPID:
if (pp == ttoproc(curthread))
if (getlwpptr(psp->p_rid) != NULL)
lwprinproc++;
break;
case P_PID:
if (pp->p_pid == psp->p_rid)
roperand++;
break;
case P_PPID:
if (pp->p_ppid == psp->p_rid)
roperand++;
break;
case P_PGID:
if (pp->p_pgrp == psp->p_rid)
roperand++;
break;
case P_SID:
mutex_enter(&pp->p_splock);
if (pp->p_sessp->s_sid == psp->p_rid)
roperand++;
mutex_exit(&pp->p_splock);
break;
case P_TASKID:
if (pp->p_task->tk_tkid == psp->p_rid)
roperand++;
break;
case P_CID:
ASSERT(tp != NULL);
/* This case is broken for now. Need to be fixed XXX */
if (tp->t_cid == psp->p_rid)
/*
* if (checkcid(psp->p_rid))
*/
roperand++;
break;
case P_UID:
mutex_enter(&pp->p_crlock);
if (crgetuid(pp->p_cred) == psp->p_rid)
roperand++;
mutex_exit(&pp->p_crlock);
break;
case P_GID:
mutex_enter(&pp->p_crlock);
if (crgetgid(pp->p_cred) == psp->p_rid)
roperand++;
mutex_exit(&pp->p_crlock);
break;
case P_PROJID:
if (pp->p_task->tk_proj->kpj_id == psp->p_rid)
roperand++;
break;
case P_POOLID:
if (pp->p_pool->pool_id == psp->p_rid)
roperand++;
break;
case P_ZONEID:
if (pp->p_zone->zone_id == psp->p_rid)
roperand++;
break;
case P_CTID:
if (PRCTID(pp) == psp->p_rid)
roperand++;
break;
case P_ALL:
roperand++;
break;
default:
#ifdef DEBUG
cmn_err(CE_WARN, "procinset called with bad set");
return (0);
#else
return (0);
#endif
}
switch (psp->p_op) {
case POP_DIFF:
if (loperand && !lwprinproc && !roperand)
return (1);
else
return (0);
case POP_AND:
if (loperand && roperand)
return (1);
else
return (0);
case POP_OR:
if (loperand || roperand)
return (1);
else
return (0);
case POP_XOR:
if ((loperand && !lwprinproc && !roperand) ||
(roperand && !lwplinproc && !loperand))
return (1);
else
return (0);
default:
#ifdef DEBUG
cmn_err(CE_WARN, "procinset called with bad set");
return (0);
#else
return (0);
#endif
}
/* NOTREACHED */
}
/*
* lwpinset returns 1 if the thread pointed to
* by tp is in the process set specified by psp and is not in
* the sys scheduling class - otherwise 0 is returned.
*
* This function expects to be called with a valid procset_t.
* The set should be checked using checkprocset() before calling
* this function.
*/
int
lwpinset(proc_t *pp, procset_t *psp, kthread_t *tp, int *done)
{
int loperand = 0;
int roperand = 0;
int lwplinset = 0;
int lwprinset = 0;
ASSERT(ttoproc(tp) == pp);
/*
* If process is in the sys class return (0).
*/
if (proctot(pp)->t_cid == 0) {
return (0);
}
switch (psp->p_lidtype) {
case P_LWPID:
if (tp->t_tid == psp->p_lid)
lwplinset ++;
break;
case P_PID:
if (pp->p_pid == psp->p_lid)
loperand++;
break;
case P_PPID:
if (pp->p_ppid == psp->p_lid)
loperand++;
break;
case P_PGID:
if (pp->p_pgrp == psp->p_lid)
loperand++;
break;
case P_SID:
mutex_enter(&pp->p_splock);
if (pp->p_sessp->s_sid == psp->p_lid)
loperand++;
mutex_exit(&pp->p_splock);
break;
case P_TASKID:
if (pp->p_task->tk_tkid == psp->p_lid)
loperand++;
break;
case P_CID:
if (tp->t_cid == psp->p_lid)
loperand++;
break;
case P_UID:
mutex_enter(&pp->p_crlock);
if (crgetuid(pp->p_cred) == psp->p_lid)
loperand++;
mutex_exit(&pp->p_crlock);
break;
case P_GID:
mutex_enter(&pp->p_crlock);
if (crgetgid(pp->p_cred) == psp->p_lid)
loperand++;
mutex_exit(&pp->p_crlock);
break;
case P_PROJID:
if (pp->p_task->tk_proj->kpj_id == psp->p_lid)
loperand++;
break;
case P_POOLID:
if (pp->p_pool->pool_id == psp->p_lid)
loperand++;
break;
case P_ZONEID:
if (pp->p_zone->zone_id == psp->p_lid)
loperand++;
break;
case P_CTID:
if (PRCTID(pp) == psp->p_lid)
loperand++;
break;
case P_ALL:
loperand++;
break;
default:
#ifdef DEBUG
cmn_err(CE_WARN, "lwpinset called with bad set");
return (0);
#else
return (0);
#endif
}
switch (psp->p_ridtype) {
case P_LWPID:
if (tp->t_tid == psp->p_rid)
lwprinset ++;
break;
case P_PID:
if (pp->p_pid == psp->p_rid)
roperand++;
break;
case P_PPID:
if (pp->p_ppid == psp->p_rid)
roperand++;
break;
case P_PGID:
if (pp->p_pgrp == psp->p_rid)
roperand++;
break;
case P_SID:
mutex_enter(&pp->p_splock);
if (pp->p_sessp->s_sid == psp->p_rid)
roperand++;
mutex_exit(&pp->p_splock);
break;
case P_TASKID:
if (pp->p_task->tk_tkid == psp->p_rid)
roperand++;
break;
case P_CID:
if (tp->t_cid == psp->p_rid)
roperand++;
break;
case P_UID:
mutex_enter(&pp->p_crlock);
if (crgetuid(pp->p_cred) == psp->p_rid)
roperand++;
mutex_exit(&pp->p_crlock);
break;
case P_GID:
mutex_enter(&pp->p_crlock);
if (crgetgid(pp->p_cred) == psp->p_rid)
roperand++;
mutex_exit(&pp->p_crlock);
break;
case P_PROJID:
if (pp->p_task->tk_proj->kpj_id == psp->p_rid)
roperand++;
break;
case P_POOLID:
if (pp->p_pool->pool_id == psp->p_rid)
roperand++;
break;
case P_ZONEID:
if (pp->p_zone->zone_id == psp->p_rid)
roperand++;
break;
case P_CTID:
if (PRCTID(pp) == psp->p_rid)
roperand++;
break;
case P_ALL:
roperand++;
break;
default:
#ifdef DEBUG
cmn_err(CE_WARN, "lwpinset called with bad set");
return (0);
#else
return (0);
#endif
}
if (lwplinset && lwprinset)
*done = 1;
switch (psp->p_op) {
case POP_DIFF:
if ((loperand || lwplinset) && !(lwprinset || roperand))
return (1);
else
return (0);
case POP_AND:
if ((loperand || lwplinset) && (roperand || lwprinset))
return (1);
else
return (0);
case POP_OR:
if (loperand || roperand || lwplinset || lwprinset)
return (1);
else
return (0);
case POP_XOR:
if (((loperand || lwplinset) &&
!(lwprinset || roperand)) ||
((roperand || lwprinset) &&
!(lwplinset || loperand)))
return (1);
else
return (0);
default:
#ifdef DEBUG
cmn_err(CE_WARN, "lwpinset called with bad set");
return (0);
#else
return (0);
#endif
}
/* NOTREACHED */
}
/*
* Check for common cases of procsets which specify only the
* current process. cur_inset_only() returns B_TRUE when
* the current process is the only one in the set. B_FALSE
* is returned to indicate that this may not be the case.
*/
boolean_t
cur_inset_only(procset_t *psp)
{
if (((psp->p_lidtype == P_PID &&
(psp->p_lid == P_MYID ||
psp->p_lid == ttoproc(curthread)->p_pid)) ||
((psp->p_lidtype == P_LWPID) &&
(psp->p_lid == P_MYID ||
psp->p_lid == curthread->t_tid))) &&
psp->p_op == POP_AND && psp->p_ridtype == P_ALL)
return (B_TRUE);
if (((psp->p_ridtype == P_PID &&
(psp->p_rid == P_MYID ||
psp->p_rid == ttoproc(curthread)->p_pid)) ||
((psp->p_ridtype == P_LWPID) &&
(psp->p_rid == P_MYID ||
psp->p_rid == curthread->t_tid))) &&
psp->p_op == POP_AND && psp->p_lidtype == P_ALL)
return (B_TRUE);
return (B_FALSE);
}
id_t
getmyid(idtype_t idtype)
{
proc_t *pp;
uid_t uid;
gid_t gid;
pid_t sid;
pp = ttoproc(curthread);
switch (idtype) {
case P_LWPID:
return (curthread->t_tid);
case P_PID:
return (pp->p_pid);
case P_PPID:
return (pp->p_ppid);
case P_PGID:
return (pp->p_pgrp);
case P_SID:
mutex_enter(&pp->p_splock);
sid = pp->p_sessp->s_sid;
mutex_exit(&pp->p_splock);
return (sid);
case P_TASKID:
return (pp->p_task->tk_tkid);
case P_CID:
return (curthread->t_cid);
case P_UID:
mutex_enter(&pp->p_crlock);
uid = crgetuid(pp->p_cred);
mutex_exit(&pp->p_crlock);
return (uid);
case P_GID:
mutex_enter(&pp->p_crlock);
gid = crgetgid(pp->p_cred);
mutex_exit(&pp->p_crlock);
return (gid);
case P_PROJID:
return (pp->p_task->tk_proj->kpj_id);
case P_POOLID:
return (pp->p_pool->pool_id);
case P_ZONEID:
return (pp->p_zone->zone_id);
case P_CTID:
return (PRCTID(pp));
case P_ALL:
/*
* The value doesn't matter for P_ALL.
*/
return (0);
default:
return (-1);
}
}
static kthread_t *
getlwpptr(id_t id)
{
proc_t *p;
kthread_t *t;
if (id == P_MYID)
t = curthread;
else {
p = ttoproc(curthread);
mutex_enter(&p->p_lock);
t = idtot(p, id);
mutex_exit(&p->p_lock);
}
return (t);
}
/*
* The dotolwp function locates the LWP(s) specified by the procset structure
* pointed to by psp. If funcp is non-NULL then it points to a function
* which dotolwp will call for each LWP in the specified set.
* LWPIDs specified in the procset structure always refer to lwps in curproc.
* The arguments for this function must be "char *arg", and "kthread_t *tp",
* where tp is a pointer to the current thread from the set.
* Note that these arguments are passed to the function in reversed order
* than the order of arguments passed by dotoprocs() to its callback function.
* Also note that there are two separate cases where this routine returns zero.
* In the first case no mutex is grabbed, in the second the p_lock mutex
* is NOT RELEASED. The priocntl code is expecting this behaviour.
*/
int
dotolwp(procset_t *psp, int (*funcp)(), char *arg)
{
int error = 0;
int nfound = 0;
kthread_t *tp;
proc_t *pp;
int done = 0;
/*
* Check that the procset_t is valid.
*/
error = checkprocset(psp);
if (error) {
return (error);
}
mutex_enter(&pidlock);
/*
* Check for the special value P_MYID in either operand
* and replace it with the correct value. We don't check
* for an error return from getmyid() because the idtypes
* have been validated by the checkprocset() call above.
*/
if (psp->p_lid == P_MYID) {
psp->p_lid = getmyid(psp->p_lidtype);
}
if (psp->p_rid == P_MYID) {
psp->p_rid = getmyid(psp->p_ridtype);
}
pp = ttoproc(curthread);
if (procinset(pp, psp)) {
mutex_exit(&pidlock);
return (0);
}
mutex_enter(&pp->p_lock);
if ((tp = pp->p_tlist) == NULL) {
mutex_exit(&pp->p_lock);
mutex_exit(&pidlock);
return (0);
}
do {
if (lwpinset(pp, psp, tp, &done)) {
nfound ++;
error = (*funcp)(arg, tp);
if (error) {
mutex_exit(&pp->p_lock);
mutex_exit(&pidlock);
return (error);
}
}
} while (((tp = tp->t_forw) != pp->p_tlist) && !done);
if (nfound == 0) {
mutex_exit(&pp->p_lock);
mutex_exit(&pidlock);
return (ESRCH);
}
mutex_exit(&pidlock);
return (error);
}
|