This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
kern_proc.c
3431 lines (2909 loc) · 72.4 KB
/
kern_proc.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
/*
* Copyright (c) 2000-2007 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*
* Copyright (c) 1982, 1986, 1989, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kern_proc.c 8.4 (Berkeley) 1/4/94
*/
/*
* NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
* support for mandatory and extensible security protections. This notice
* is included in support of clause 2.2 (b) of the Apple Public License,
* Version 2.0.
*/
/* HISTORY
* 04-Aug-97 Umesh Vaishampayan (umeshv@apple.com)
* Added current_proc_EXTERNAL() function for the use of kernel
* lodable modules.
*
* 05-Jun-95 Mac Gillon (mgillon) at NeXT
* New version based on 3.3NS and 4.4
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc_internal.h>
#include <sys/acct.h>
#include <sys/wait.h>
#include <sys/file_internal.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/mbuf.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/signalvar.h>
#include <sys/syslog.h>
#include <sys/sysctl.h>
#include <sys/sysproto.h>
#include <sys/kauth.h>
#include <sys/codesign.h>
#include <sys/kernel_types.h>
#include <sys/ubc.h>
#include <kern/kalloc.h>
#include <kern/task.h>
#include <kern/coalition.h>
#include <sys/coalition.h>
#include <kern/assert.h>
#include <vm/vm_protos.h>
#include <vm/vm_map.h> /* vm_map_switch_protect() */
#include <vm/vm_pageout.h>
#include <mach/task.h>
#include <mach/message.h>
#include <sys/priv.h>
#include <sys/proc_info.h>
#include <sys/bsdtask_info.h>
#include <sys/persona.h>
#if CONFIG_CSR
#include <sys/csr.h>
#endif
#if CONFIG_MEMORYSTATUS
#include <sys/kern_memorystatus.h>
#endif
#if CONFIG_MACF
#include <security/mac_framework.h>
#endif
#include <libkern/crypto/sha1.h>
/*
* Structure associated with user cacheing.
*/
struct uidinfo {
LIST_ENTRY(uidinfo) ui_hash;
uid_t ui_uid;
long ui_proccnt;
};
#define UIHASH(uid) (&uihashtbl[(uid) & uihash])
LIST_HEAD(uihashhead, uidinfo) *uihashtbl;
u_long uihash; /* size of hash table - 1 */
/*
* Other process lists
*/
struct pidhashhead *pidhashtbl;
u_long pidhash;
struct pgrphashhead *pgrphashtbl;
u_long pgrphash;
struct sesshashhead *sesshashtbl;
u_long sesshash;
struct proclist allproc;
struct proclist zombproc;
extern struct tty cons;
extern int cs_debug;
#if DEVELOPMENT || DEBUG
extern int cs_enforcement_enable;
#endif
#if DEBUG
#define __PROC_INTERNAL_DEBUG 1
#endif
#if CONFIG_COREDUMP
/* Name to give to core files */
#if CONFIG_EMBEDDED
__XNU_PRIVATE_EXTERN char corefilename[MAXPATHLEN+1] = {"/private/var/cores/%N.core"};
#else
__XNU_PRIVATE_EXTERN char corefilename[MAXPATHLEN+1] = {"/cores/core.%P"};
#endif
#endif
#if PROC_REF_DEBUG
#include <kern/backtrace.h>
#endif
static void orphanpg(struct pgrp * pg);
void proc_name_kdp(task_t t, char * buf, int size);
void * proc_get_uthread_uu_threadlist(void * uthread_v);
int proc_threadname_kdp(void * uth, char * buf, size_t size);
void proc_starttime_kdp(void * p, uint64_t * tv_sec, uint64_t * tv_usec, uint64_t * abstime);
char * proc_name_address(void * p);
/* TODO: make a header that's exported and usable in osfmk */
char* proc_best_name(proc_t p);
static void pgrp_add(struct pgrp * pgrp, proc_t parent, proc_t child);
static void pgrp_remove(proc_t p);
static void pgrp_replace(proc_t p, struct pgrp *pgrp);
static void pgdelete_dropref(struct pgrp *pgrp);
extern void pg_rele_dropref(struct pgrp * pgrp);
static int csops_internal(pid_t pid, int ops, user_addr_t uaddr, user_size_t usersize, user_addr_t uaddittoken);
static boolean_t proc_parent_is_currentproc(proc_t p);
struct fixjob_iterargs {
struct pgrp * pg;
struct session * mysession;
int entering;
};
int fixjob_callback(proc_t, void *);
uint64_t get_current_unique_pid(void);
uint64_t
get_current_unique_pid(void)
{
proc_t p = current_proc();
if (p)
return p->p_uniqueid;
else
return 0;
}
/*
* Initialize global process hashing structures.
*/
void
procinit(void)
{
LIST_INIT(&allproc);
LIST_INIT(&zombproc);
pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash);
pgrphashtbl = hashinit(maxproc / 4, M_PROC, &pgrphash);
sesshashtbl = hashinit(maxproc / 4, M_PROC, &sesshash);
uihashtbl = hashinit(maxproc / 16, M_PROC, &uihash);
#if CONFIG_PERSONAS
personas_bootstrap();
#endif
}
/*
* Change the count associated with number of processes
* a given user is using. This routine protects the uihash
* with the list lock
*/
int
chgproccnt(uid_t uid, int diff)
{
struct uidinfo *uip;
struct uidinfo *newuip = NULL;
struct uihashhead *uipp;
int retval;
again:
proc_list_lock();
uipp = UIHASH(uid);
for (uip = uipp->lh_first; uip != 0; uip = uip->ui_hash.le_next)
if (uip->ui_uid == uid)
break;
if (uip) {
uip->ui_proccnt += diff;
if (uip->ui_proccnt > 0) {
retval = uip->ui_proccnt;
proc_list_unlock();
goto out;
}
if (uip->ui_proccnt < 0)
panic("chgproccnt: procs < 0");
LIST_REMOVE(uip, ui_hash);
retval = 0;
proc_list_unlock();
FREE_ZONE(uip, sizeof(*uip), M_PROC);
goto out;
}
if (diff <= 0) {
if (diff == 0) {
retval = 0;
proc_list_unlock();
goto out;
}
panic("chgproccnt: lost user");
}
if (newuip != NULL) {
uip = newuip;
newuip = NULL;
LIST_INSERT_HEAD(uipp, uip, ui_hash);
uip->ui_uid = uid;
uip->ui_proccnt = diff;
retval = diff;
proc_list_unlock();
goto out;
}
proc_list_unlock();
MALLOC_ZONE(newuip, struct uidinfo *, sizeof(*uip), M_PROC, M_WAITOK);
if (newuip == NULL)
panic("chgproccnt: M_PROC zone depleted");
goto again;
out:
if (newuip != NULL)
FREE_ZONE(newuip, sizeof(*uip), M_PROC);
return(retval);
}
/*
* Is p an inferior of the current process?
*/
int
inferior(proc_t p)
{
int retval = 0;
proc_list_lock();
for (; p != current_proc(); p = p->p_pptr)
if (p->p_pid == 0)
goto out;
retval = 1;
out:
proc_list_unlock();
return(retval);
}
/*
* Is p an inferior of t ?
*/
int
isinferior(proc_t p, proc_t t)
{
int retval = 0;
int nchecked = 0;
proc_t start = p;
/* if p==t they are not inferior */
if (p == t)
return(0);
proc_list_lock();
for (; p != t; p = p->p_pptr) {
nchecked++;
/* Detect here if we're in a cycle */
if ((p->p_pid == 0) || (p->p_pptr == start) || (nchecked >= nprocs))
goto out;
}
retval = 1;
out:
proc_list_unlock();
return(retval);
}
int
proc_isinferior(int pid1, int pid2)
{
proc_t p = PROC_NULL;
proc_t t = PROC_NULL;
int retval = 0;
if (((p = proc_find(pid1)) != (proc_t)0 ) && ((t = proc_find(pid2)) != (proc_t)0))
retval = isinferior(p, t);
if (p != PROC_NULL)
proc_rele(p);
if (t != PROC_NULL)
proc_rele(t);
return(retval);
}
proc_t
proc_find(int pid)
{
return(proc_findinternal(pid, 0));
}
proc_t
proc_findinternal(int pid, int locked)
{
proc_t p = PROC_NULL;
if (locked == 0) {
proc_list_lock();
}
p = pfind_locked(pid);
if ((p == PROC_NULL) || (p != proc_ref_locked(p)))
p = PROC_NULL;
if (locked == 0) {
proc_list_unlock();
}
return(p);
}
proc_t
proc_findthread(thread_t thread)
{
proc_t p = PROC_NULL;
struct uthread *uth;
proc_list_lock();
uth = get_bsdthread_info(thread);
if (uth && (uth->uu_flag & UT_VFORK))
p = uth->uu_proc;
else
p = (proc_t)(get_bsdthreadtask_info(thread));
p = proc_ref_locked(p);
proc_list_unlock();
return(p);
}
void
uthread_reset_proc_refcount(void *uthread) {
uthread_t uth;
uth = (uthread_t) uthread;
uth->uu_proc_refcount = 0;
#if PROC_REF_DEBUG
if (proc_ref_tracking_disabled) {
return;
}
uth->uu_pindex = 0;
#endif
}
#if PROC_REF_DEBUG
int
uthread_get_proc_refcount(void *uthread) {
uthread_t uth;
if (proc_ref_tracking_disabled) {
return 0;
}
uth = (uthread_t) uthread;
return uth->uu_proc_refcount;
}
#endif
static void
record_procref(proc_t p __unused, int count) {
uthread_t uth;
uth = current_uthread();
uth->uu_proc_refcount += count;
#if PROC_REF_DEBUG
if (proc_ref_tracking_disabled) {
return;
}
if (count == 1) {
if (uth->uu_pindex < NUM_PROC_REFS_TO_TRACK) {
backtrace((uintptr_t *) &uth->uu_proc_pcs[uth->uu_pindex], PROC_REF_STACK_DEPTH);
uth->uu_proc_ps[uth->uu_pindex] = p;
uth->uu_pindex++;
}
}
#endif
}
static boolean_t
uthread_needs_to_wait_in_proc_refwait(void) {
uthread_t uth = current_uthread();
/*
* Allow threads holding no proc refs to wait
* in proc_refwait, allowing threads holding
* proc refs to wait in proc_refwait causes
* deadlocks and makes proc_find non-reentrant.
*/
if (uth->uu_proc_refcount == 0)
return TRUE;
return FALSE;
}
int
proc_rele(proc_t p)
{
proc_list_lock();
proc_rele_locked(p);
proc_list_unlock();
return(0);
}
proc_t
proc_self(void)
{
struct proc * p;
p = current_proc();
proc_list_lock();
if (p != proc_ref_locked(p))
p = PROC_NULL;
proc_list_unlock();
return(p);
}
proc_t
proc_ref_locked(proc_t p)
{
proc_t p1 = p;
int pid = proc_pid(p);
retry:
/*
* if process still in creation or proc got recycled
* during msleep then return failure.
*/
if ((p == PROC_NULL) || (p1 != p) || ((p->p_listflag & P_LIST_INCREATE) != 0))
return (PROC_NULL);
/*
* Do not return process marked for termination
* or proc_refdrain called without ref wait.
* Wait for proc_refdrain_with_refwait to complete if
* process in refdrain and refwait flag is set, unless
* the current thread is holding to a proc_ref
* for any proc.
*/
if ((p->p_stat != SZOMB) &&
((p->p_listflag & P_LIST_EXITED) == 0) &&
((p->p_listflag & P_LIST_DEAD) == 0) &&
(((p->p_listflag & (P_LIST_DRAIN | P_LIST_DRAINWAIT)) == 0) ||
((p->p_listflag & P_LIST_REFWAIT) != 0))) {
if ((p->p_listflag & P_LIST_REFWAIT) != 0 && uthread_needs_to_wait_in_proc_refwait()) {
msleep(&p->p_listflag, proc_list_mlock, 0, "proc_refwait", 0) ;
/*
* the proc might have been recycled since we dropped
* the proc list lock, get the proc again.
*/
p = pfind_locked(pid);
goto retry;
}
p->p_refcount++;
record_procref(p, 1);
}
else
p1 = PROC_NULL;
return(p1);
}
void
proc_rele_locked(proc_t p)
{
if (p->p_refcount > 0) {
p->p_refcount--;
record_procref(p, -1);
if ((p->p_refcount == 0) && ((p->p_listflag & P_LIST_DRAINWAIT) == P_LIST_DRAINWAIT)) {
p->p_listflag &= ~P_LIST_DRAINWAIT;
wakeup(&p->p_refcount);
}
} else
panic("proc_rele_locked -ve ref\n");
}
proc_t
proc_find_zombref(int pid)
{
proc_t p;
proc_list_lock();
again:
p = pfind_locked(pid);
/* should we bail? */
if ((p == PROC_NULL) /* not found */
|| ((p->p_listflag & P_LIST_INCREATE) != 0) /* not created yet */
|| ((p->p_listflag & P_LIST_EXITED) == 0)) { /* not started exit */
proc_list_unlock();
return (PROC_NULL);
}
/* If someone else is controlling the (unreaped) zombie - wait */
if ((p->p_listflag & P_LIST_WAITING) != 0) {
(void)msleep(&p->p_stat, proc_list_mlock, PWAIT, "waitcoll", 0);
goto again;
}
p->p_listflag |= P_LIST_WAITING;
proc_list_unlock();
return(p);
}
void
proc_drop_zombref(proc_t p)
{
proc_list_lock();
if ((p->p_listflag & P_LIST_WAITING) == P_LIST_WAITING) {
p->p_listflag &= ~P_LIST_WAITING;
wakeup(&p->p_stat);
}
proc_list_unlock();
}
void
proc_refdrain(proc_t p)
{
proc_refdrain_with_refwait(p, FALSE);
}
proc_t
proc_refdrain_with_refwait(proc_t p, boolean_t get_ref_and_allow_wait)
{
boolean_t initexec = FALSE;
proc_list_lock();
p->p_listflag |= P_LIST_DRAIN;
if (get_ref_and_allow_wait) {
/*
* All the calls to proc_ref_locked will wait
* for the flag to get cleared before returning a ref,
* unless the current thread is holding to a proc ref
* for any proc.
*/
p->p_listflag |= P_LIST_REFWAIT;
if (p == initproc) {
initexec = TRUE;
}
}
/* Do not wait in ref drain for launchd exec */
while (p->p_refcount && !initexec) {
p->p_listflag |= P_LIST_DRAINWAIT;
msleep(&p->p_refcount, proc_list_mlock, 0, "proc_refdrain", 0) ;
}
p->p_listflag &= ~P_LIST_DRAIN;
if (!get_ref_and_allow_wait) {
p->p_listflag |= P_LIST_DEAD;
} else {
/* Return a ref to the caller */
p->p_refcount++;
record_procref(p, 1);
}
proc_list_unlock();
if (get_ref_and_allow_wait) {
return (p);
}
return NULL;
}
void
proc_refwake(proc_t p)
{
proc_list_lock();
p->p_listflag &= ~P_LIST_REFWAIT;
wakeup(&p->p_listflag);
proc_list_unlock();
}
proc_t
proc_parentholdref(proc_t p)
{
proc_t parent = PROC_NULL;
proc_t pp;
int loopcnt = 0;
proc_list_lock();
loop:
pp = p->p_pptr;
if ((pp == PROC_NULL) || (pp->p_stat == SZOMB) || ((pp->p_listflag & (P_LIST_CHILDDRSTART | P_LIST_CHILDDRAINED)) == (P_LIST_CHILDDRSTART | P_LIST_CHILDDRAINED))) {
parent = PROC_NULL;
goto out;
}
if ((pp->p_listflag & (P_LIST_CHILDDRSTART | P_LIST_CHILDDRAINED)) == P_LIST_CHILDDRSTART) {
pp->p_listflag |= P_LIST_CHILDDRWAIT;
msleep(&pp->p_childrencnt, proc_list_mlock, 0, "proc_parent", 0);
loopcnt++;
if (loopcnt == 5) {
parent = PROC_NULL;
goto out;
}
goto loop;
}
if ((pp->p_listflag & (P_LIST_CHILDDRSTART | P_LIST_CHILDDRAINED)) == 0) {
pp->p_parentref++;
parent = pp;
goto out;
}
out:
proc_list_unlock();
return(parent);
}
int
proc_parentdropref(proc_t p, int listlocked)
{
if (listlocked == 0)
proc_list_lock();
if (p->p_parentref > 0) {
p->p_parentref--;
if ((p->p_parentref == 0) && ((p->p_listflag & P_LIST_PARENTREFWAIT) == P_LIST_PARENTREFWAIT)) {
p->p_listflag &= ~P_LIST_PARENTREFWAIT;
wakeup(&p->p_parentref);
}
} else
panic("proc_parentdropref -ve ref\n");
if (listlocked == 0)
proc_list_unlock();
return(0);
}
void
proc_childdrainstart(proc_t p)
{
#if __PROC_INTERNAL_DEBUG
if ((p->p_listflag & P_LIST_CHILDDRSTART) == P_LIST_CHILDDRSTART)
panic("proc_childdrainstart: childdrain already started\n");
#endif
p->p_listflag |= P_LIST_CHILDDRSTART;
/* wait for all that hold parentrefs to drop */
while (p->p_parentref > 0) {
p->p_listflag |= P_LIST_PARENTREFWAIT;
msleep(&p->p_parentref, proc_list_mlock, 0, "proc_childdrainstart", 0) ;
}
}
void
proc_childdrainend(proc_t p)
{
#if __PROC_INTERNAL_DEBUG
if (p->p_childrencnt > 0)
panic("exiting: children stil hanging around\n");
#endif
p->p_listflag |= P_LIST_CHILDDRAINED;
if ((p->p_listflag & (P_LIST_CHILDLKWAIT |P_LIST_CHILDDRWAIT)) != 0) {
p->p_listflag &= ~(P_LIST_CHILDLKWAIT |P_LIST_CHILDDRWAIT);
wakeup(&p->p_childrencnt);
}
}
void
proc_checkdeadrefs(__unused proc_t p)
{
#if __PROC_INTERNAL_DEBUG
if ((p->p_listflag & P_LIST_INHASH) != 0)
panic("proc being freed and still in hash %p: %u\n", p, p->p_listflag);
if (p->p_childrencnt != 0)
panic("proc being freed and pending children cnt %p:%d\n", p, p->p_childrencnt);
if (p->p_refcount != 0)
panic("proc being freed and pending refcount %p:%d\n", p, p->p_refcount);
if (p->p_parentref != 0)
panic("proc being freed and pending parentrefs %p:%d\n", p, p->p_parentref);
#endif
}
int
proc_pid(proc_t p)
{
if (p != NULL)
return (p->p_pid);
return -1;
}
int
proc_ppid(proc_t p)
{
if (p != NULL)
return (p->p_ppid);
return -1;
}
int
proc_selfpid(void)
{
return (current_proc()->p_pid);
}
int
proc_selfppid(void)
{
return (current_proc()->p_ppid);
}
int
proc_selfcsflags(void)
{
return (current_proc()->p_csflags);
}
#if CONFIG_DTRACE
static proc_t
dtrace_current_proc_vforking(void)
{
thread_t th = current_thread();
struct uthread *ut = get_bsdthread_info(th);
if (ut &&
((ut->uu_flag & (UT_VFORK|UT_VFORKING)) == (UT_VFORK|UT_VFORKING))) {
/*
* Handle the narrow window where we're in the vfork syscall,
* but we're not quite ready to claim (in particular, to DTrace)
* that we're running as the child.
*/
return (get_bsdtask_info(get_threadtask(th)));
}
return (current_proc());
}
int
dtrace_proc_selfpid(void)
{
return (dtrace_current_proc_vforking()->p_pid);
}
int
dtrace_proc_selfppid(void)
{
return (dtrace_current_proc_vforking()->p_ppid);
}
uid_t
dtrace_proc_selfruid(void)
{
return (dtrace_current_proc_vforking()->p_ruid);
}
#endif /* CONFIG_DTRACE */
proc_t
proc_parent(proc_t p)
{
proc_t parent;
proc_t pp;
proc_list_lock();
loop:
pp = p->p_pptr;
parent = proc_ref_locked(pp);
if ((parent == PROC_NULL) && (pp != PROC_NULL) && (pp->p_stat != SZOMB) && ((pp->p_listflag & P_LIST_EXITED) != 0) && ((pp->p_listflag & P_LIST_CHILDDRAINED)== 0)){
pp->p_listflag |= P_LIST_CHILDLKWAIT;
msleep(&pp->p_childrencnt, proc_list_mlock, 0, "proc_parent", 0);
goto loop;
}
proc_list_unlock();
return(parent);
}
static boolean_t
proc_parent_is_currentproc(proc_t p)
{
boolean_t ret = FALSE;
proc_list_lock();
if (p->p_pptr == current_proc())
ret = TRUE;
proc_list_unlock();
return ret;
}
void
proc_name(int pid, char * buf, int size)
{
proc_t p;
if ((p = proc_find(pid)) != PROC_NULL) {
strlcpy(buf, &p->p_comm[0], size);
proc_rele(p);
}
}
void
proc_name_kdp(task_t t, char * buf, int size)
{
proc_t p = get_bsdtask_info(t);
if (p == PROC_NULL)
return;
if ((size_t)size > sizeof(p->p_comm))
strlcpy(buf, &p->p_name[0], MIN((int)sizeof(p->p_name), size));
else
strlcpy(buf, &p->p_comm[0], MIN((int)sizeof(p->p_comm), size));
}
int
proc_threadname_kdp(void * uth, char * buf, size_t size)
{
if (size < MAXTHREADNAMESIZE) {
/* this is really just a protective measure for the future in
* case the thread name size in stackshot gets out of sync with
* the BSD max thread name size. Note that bsd_getthreadname
* doesn't take input buffer size into account. */
return -1;
}
if (uth != NULL) {
bsd_getthreadname(uth, buf);
}
return 0;
}
/* note that this function is generally going to be called from stackshot,
* and the arguments will be coming from a struct which is declared packed
* thus the input arguments will in general be unaligned. We have to handle
* that here. */
void
proc_starttime_kdp(void *p, uint64_t *tv_sec, uint64_t *tv_usec, uint64_t *abstime)
{
proc_t pp = (proc_t)p;
struct uint64p {
uint64_t val;
} __attribute__((packed));
if (pp != PROC_NULL) {
if (tv_sec != NULL)
((struct uint64p *)tv_sec)->val = pp->p_start.tv_sec;
if (tv_usec != NULL)
((struct uint64p *)tv_usec)->val = pp->p_start.tv_usec;
if (abstime != NULL) {
if (pp->p_stats != NULL)
*abstime = pp->p_stats->ps_start;
else
*abstime = 0;
}
}
}
char *
proc_name_address(void *p)
{
return &((proc_t)p)->p_comm[0];
}
char *
proc_best_name(proc_t p)
{
if (p->p_name[0] != 0)
return (&p->p_name[0]);
return (&p->p_comm[0]);
}
void
proc_selfname(char * buf, int size)
{
proc_t p;
if ((p = current_proc())!= (proc_t)0) {
strlcpy(buf, &p->p_comm[0], size);
}
}
void
proc_signal(int pid, int signum)
{
proc_t p;
if ((p = proc_find(pid)) != PROC_NULL) {
psignal(p, signum);
proc_rele(p);
}
}
int
proc_issignal(int pid, sigset_t mask)
{
proc_t p;
int error=0;
if ((p = proc_find(pid)) != PROC_NULL) {
error = proc_pendingsignals(p, mask);
proc_rele(p);
}
return(error);
}
int
proc_noremotehang(proc_t p)
{
int retval = 0;
if (p)
retval = p->p_flag & P_NOREMOTEHANG;
return(retval? 1: 0);
}
int
proc_exiting(proc_t p)
{
int retval = 0;
if (p)
retval = p->p_lflag & P_LEXIT;