forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkern_exit.c
2955 lines (2568 loc) · 89.4 KB
/
kern_exit.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-2016 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, 1997 Apple Computer, Inc. All Rights Reserved */
/*
* Copyright (c) 1982, 1986, 1989, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* 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_exit.c 8.7 (Berkeley) 2/12/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.
*/
#include <machine/reg.h>
#include <machine/psl.h>
#include <stdatomic.h>
#include "compat_43.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ioctl.h>
#include <sys/proc_internal.h>
#include <sys/proc.h>
#include <sys/kauth.h>
#include <sys/tty.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/kernel.h>
#include <sys/wait.h>
#include <sys/file_internal.h>
#include <sys/vnode_internal.h>
#include <sys/syslog.h>
#include <sys/malloc.h>
#include <sys/resourcevar.h>
#include <sys/ptrace.h>
#include <sys/proc_info.h>
#include <sys/reason.h>
#include <sys/_types/_timeval64.h>
#include <sys/user.h>
#include <sys/aio_kern.h>
#include <sys/sysproto.h>
#include <sys/signalvar.h>
#include <sys/kdebug.h>
#include <sys/filedesc.h> /* fdfree */
#include <sys/acct.h> /* acct_process */
#include <sys/codesign.h>
#include <sys/event.h> /* kevent_proc_copy_uptrs */
#include <sys/sdt.h>
#include <security/audit/audit.h>
#include <bsm/audit_kevents.h>
#include <mach/mach_types.h>
#include <mach/task.h>
#include <mach/thread_act.h>
#include <kern/exc_resource.h>
#include <kern/kern_types.h>
#include <kern/kalloc.h>
#include <kern/task.h>
#include <corpses/task_corpse.h>
#include <kern/thread.h>
#include <kern/thread_call.h>
#include <kern/sched_prim.h>
#include <kern/assert.h>
#include <kern/policy_internal.h>
#include <kern/exc_guard.h>
#include <kern/backtrace.h>
#include <vm/vm_protos.h>
#include <os/log.h>
#include <pexpert/pexpert.h>
#if SYSV_SHM
#include <sys/shm_internal.h> /* shmexit */
#endif /* SYSV_SHM */
#if CONFIG_PERSONAS
#include <sys/persona.h>
#endif /* CONFIG_PERSONAS */
#if CONFIG_MEMORYSTATUS
#include <sys/kern_memorystatus.h>
#endif /* CONFIG_MEMORYSTATUS */
#if CONFIG_DTRACE
/* Do not include dtrace.h, it redefines kmem_[alloc/free] */
void dtrace_proc_exit(proc_t p);
#include <sys/dtrace_ptss.h>
#endif /* CONFIG_DTRACE */
#if CONFIG_MACF
#include <security/mac_framework.h>
#include <security/mac_mach_internal.h>
#include <sys/syscall.h>
#endif /* CONFIG_MACF */
#if CONFIG_MEMORYSTATUS
static void proc_memorystatus_remove(proc_t p);
#endif /* CONFIG_MEMORYSTATUS */
void proc_prepareexit(proc_t p, int rv, boolean_t perf_notify);
void gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
mach_exception_data_type_t code, mach_exception_data_type_t subcode,
uint64_t *udata_buffer, int num_udata, void *reason);
mach_exception_data_type_t proc_encode_exit_exception_code(proc_t p);
void vfork_exit(proc_t p, int rv);
__private_extern__ void munge_user64_rusage(struct rusage *a_rusage_p, struct user64_rusage *a_user_rusage_p);
__private_extern__ void munge_user32_rusage(struct rusage *a_rusage_p, struct user32_rusage *a_user_rusage_p);
static int reap_child_locked(proc_t parent, proc_t child, int deadparent, int reparentedtoinit, int locked, int droplock);
static void populate_corpse_crashinfo(proc_t p, task_t corpse_task,
struct rusage_superset *rup, mach_exception_data_type_t code,
mach_exception_data_type_t subcode, uint64_t *udata_buffer,
int num_udata, os_reason_t reason);
static void proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode);
extern int proc_pidpathinfo_internal(proc_t p, uint64_t arg, char *buffer, uint32_t buffersize, int32_t *retval);
static __attribute__((noinline)) void launchd_crashed_panic(proc_t p, int rv);
extern void proc_piduniqidentifierinfo(proc_t p, struct proc_uniqidentifierinfo *p_uniqidinfo);
extern void task_coalition_ids(task_t task, uint64_t ids[COALITION_NUM_TYPES]);
extern uint64_t get_task_phys_footprint_limit(task_t);
int proc_list_uptrs(void *p, uint64_t *udata_buffer, int size);
extern uint64_t task_corpse_get_crashed_thread_id(task_t corpse_task);
ZONE_DECLARE(zombie_zone, "zombie",
sizeof(struct rusage_superset), ZC_NOENCRYPT);
/*
* Things which should have prototypes in headers, but don't
*/
void proc_exit(proc_t p);
int wait1continue(int result);
int waitidcontinue(int result);
kern_return_t sys_perf_notify(thread_t thread, int pid);
kern_return_t task_exception_notify(exception_type_t exception,
mach_exception_data_type_t code, mach_exception_data_type_t subcode);
kern_return_t task_violated_guard(mach_exception_code_t, mach_exception_subcode_t, void *);
void delay(int);
void gather_rusage_info(proc_t p, rusage_info_current *ru, int flavor);
#if __has_feature(ptrauth_calls)
int exit_with_pac_exception(proc_t p, exception_type_t exception, mach_exception_code_t code,
mach_exception_subcode_t subcode);
#endif /* __has_feature(ptrauth_calls) */
/*
* NOTE: Source and target may *NOT* overlap!
* XXX Should share code with bsd/dev/ppc/unix_signal.c
*/
void
siginfo_user_to_user32(user_siginfo_t *in, user32_siginfo_t *out)
{
out->si_signo = in->si_signo;
out->si_errno = in->si_errno;
out->si_code = in->si_code;
out->si_pid = in->si_pid;
out->si_uid = in->si_uid;
out->si_status = in->si_status;
out->si_addr = CAST_DOWN_EXPLICIT(user32_addr_t, in->si_addr);
/* following cast works for sival_int because of padding */
out->si_value.sival_ptr = CAST_DOWN_EXPLICIT(user32_addr_t, in->si_value.sival_ptr);
out->si_band = (user32_long_t)in->si_band; /* range reduction */
}
void
siginfo_user_to_user64(user_siginfo_t *in, user64_siginfo_t *out)
{
out->si_signo = in->si_signo;
out->si_errno = in->si_errno;
out->si_code = in->si_code;
out->si_pid = in->si_pid;
out->si_uid = in->si_uid;
out->si_status = in->si_status;
out->si_addr = in->si_addr;
/* following cast works for sival_int because of padding */
out->si_value.sival_ptr = in->si_value.sival_ptr;
out->si_band = in->si_band; /* range reduction */
}
static int
copyoutsiginfo(user_siginfo_t *native, boolean_t is64, user_addr_t uaddr)
{
if (is64) {
user64_siginfo_t sinfo64;
bzero(&sinfo64, sizeof(sinfo64));
siginfo_user_to_user64(native, &sinfo64);
return copyout(&sinfo64, uaddr, sizeof(sinfo64));
} else {
user32_siginfo_t sinfo32;
bzero(&sinfo32, sizeof(sinfo32));
siginfo_user_to_user32(native, &sinfo32);
return copyout(&sinfo32, uaddr, sizeof(sinfo32));
}
}
void
gather_populate_corpse_crashinfo(proc_t p, task_t corpse_task,
mach_exception_data_type_t code, mach_exception_data_type_t subcode,
uint64_t *udata_buffer, int num_udata, void *reason)
{
struct rusage_superset rup;
gather_rusage_info(p, &rup.ri, RUSAGE_INFO_CURRENT);
rup.ri.ri_phys_footprint = 0;
populate_corpse_crashinfo(p, corpse_task, &rup, code, subcode,
udata_buffer, num_udata, reason);
}
static void
proc_update_corpse_exception_codes(proc_t p, mach_exception_data_type_t *code, mach_exception_data_type_t *subcode)
{
mach_exception_data_type_t code_update = *code;
mach_exception_data_type_t subcode_update = *subcode;
if (p->p_exit_reason == OS_REASON_NULL) {
return;
}
switch (p->p_exit_reason->osr_namespace) {
case OS_REASON_JETSAM:
if (p->p_exit_reason->osr_code == JETSAM_REASON_MEMORY_PERPROCESSLIMIT) {
/* Update the code with EXC_RESOURCE code for high memory watermark */
EXC_RESOURCE_ENCODE_TYPE(code_update, RESOURCE_TYPE_MEMORY);
EXC_RESOURCE_ENCODE_FLAVOR(code_update, FLAVOR_HIGH_WATERMARK);
EXC_RESOURCE_HWM_ENCODE_LIMIT(code_update, ((get_task_phys_footprint_limit(p->task)) >> 20));
subcode_update = 0;
break;
}
break;
default:
break;
}
*code = code_update;
*subcode = subcode_update;
return;
}
mach_exception_data_type_t
proc_encode_exit_exception_code(proc_t p)
{
uint64_t subcode = 0;
if (p->p_exit_reason == OS_REASON_NULL) {
return 0;
}
/* Embed first 32 bits of osr_namespace and osr_code in exception code */
ENCODE_OSR_NAMESPACE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_namespace);
ENCODE_OSR_CODE_TO_MACH_EXCEPTION_CODE(subcode, p->p_exit_reason->osr_code);
return (mach_exception_data_type_t)subcode;
}
static void
populate_corpse_crashinfo(proc_t p, task_t corpse_task, struct rusage_superset *rup,
mach_exception_data_type_t code, mach_exception_data_type_t subcode,
uint64_t *udata_buffer, int num_udata, os_reason_t reason)
{
mach_vm_address_t uaddr = 0;
mach_exception_data_type_t exc_codes[EXCEPTION_CODE_MAX];
exc_codes[0] = code;
exc_codes[1] = subcode;
cpu_type_t cputype;
struct proc_uniqidentifierinfo p_uniqidinfo;
struct proc_workqueueinfo pwqinfo;
int retval = 0;
uint64_t crashed_threadid = task_corpse_get_crashed_thread_id(corpse_task);
unsigned int pflags = 0;
uint64_t max_footprint_mb;
uint64_t max_footprint;
uint64_t ledger_internal;
uint64_t ledger_internal_compressed;
uint64_t ledger_iokit_mapped;
uint64_t ledger_alternate_accounting;
uint64_t ledger_alternate_accounting_compressed;
uint64_t ledger_purgeable_nonvolatile;
uint64_t ledger_purgeable_nonvolatile_compressed;
uint64_t ledger_page_table;
uint64_t ledger_phys_footprint;
uint64_t ledger_phys_footprint_lifetime_max;
uint64_t ledger_network_nonvolatile;
uint64_t ledger_network_nonvolatile_compressed;
uint64_t ledger_wired_mem;
uint64_t ledger_tagged_footprint;
uint64_t ledger_tagged_footprint_compressed;
uint64_t ledger_media_footprint;
uint64_t ledger_media_footprint_compressed;
uint64_t ledger_graphics_footprint;
uint64_t ledger_graphics_footprint_compressed;
uint64_t ledger_neural_footprint;
uint64_t ledger_neural_footprint_compressed;
void *crash_info_ptr = task_get_corpseinfo(corpse_task);
#if CONFIG_MEMORYSTATUS
int memstat_dirty_flags = 0;
#endif
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_EXCEPTION_CODES, sizeof(exc_codes), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, exc_codes, sizeof(exc_codes));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PID, sizeof(p->p_pid), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_pid, sizeof(p->p_pid));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PPID, sizeof(p->p_ppid), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_ppid, sizeof(p->p_ppid));
}
/* Don't include the crashed thread ID if there's an exit reason that indicates it's irrelevant */
if ((p->p_exit_reason == OS_REASON_NULL) || !(p->p_exit_reason->osr_flags & OS_REASON_FLAG_NO_CRASHED_TID)) {
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CRASHED_THREADID, sizeof(uint64_t), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &crashed_threadid, sizeof(uint64_t));
}
}
static_assert(sizeof(struct proc_uniqidentifierinfo) == sizeof(struct crashinfo_proc_uniqidentifierinfo));
if (KERN_SUCCESS ==
kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_BSDINFOWITHUNIQID, sizeof(struct proc_uniqidentifierinfo), &uaddr)) {
proc_piduniqidentifierinfo(p, &p_uniqidinfo);
kcdata_memcpy(crash_info_ptr, uaddr, &p_uniqidinfo, sizeof(struct proc_uniqidentifierinfo));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RUSAGE_INFO, sizeof(rusage_info_current), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &rup->ri, sizeof(rusage_info_current));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_CSFLAGS, sizeof(p->p_csflags), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_csflags, sizeof(p->p_csflags));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_NAME, sizeof(p->p_comm), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_comm, sizeof(p->p_comm));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_STARTTIME, sizeof(p->p_start), &uaddr)) {
struct timeval64 t64;
t64.tv_sec = (int64_t)p->p_start.tv_sec;
t64.tv_usec = (int64_t)p->p_start.tv_usec;
kcdata_memcpy(crash_info_ptr, uaddr, &t64, sizeof(t64));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_USERSTACK, sizeof(p->user_stack), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->user_stack, sizeof(p->user_stack));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_ARGSLEN, sizeof(p->p_argslen), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argslen, sizeof(p->p_argslen));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_ARGC, sizeof(p->p_argc), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_argc, sizeof(p->p_argc));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PATH, MAXPATHLEN, &uaddr)) {
char *buf = zalloc_flags(ZV_NAMEI, Z_WAITOK | Z_ZERO);
proc_pidpathinfo_internal(p, 0, buf, MAXPATHLEN, &retval);
kcdata_memcpy(crash_info_ptr, uaddr, buf, MAXPATHLEN);
zfree(ZV_NAMEI, buf);
}
pflags = p->p_flag & (P_LP64 | P_SUGID | P_TRANSLATED);
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_FLAGS, sizeof(pflags), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &pflags, sizeof(pflags));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_UID, sizeof(p->p_uid), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_uid, sizeof(p->p_uid));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_GID, sizeof(p->p_gid), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_gid, sizeof(p->p_gid));
}
cputype = cpu_type() & ~CPU_ARCH_MASK;
if (IS_64BIT_PROCESS(p)) {
cputype |= CPU_ARCH_ABI64;
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_CPUTYPE, sizeof(cpu_type_t), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &cputype, sizeof(cpu_type_t));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT, sizeof(max_footprint_mb), &uaddr)) {
max_footprint = get_task_phys_footprint_limit(p->task);
max_footprint_mb = max_footprint >> 20;
kcdata_memcpy(crash_info_ptr, uaddr, &max_footprint_mb, sizeof(max_footprint_mb));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT_LIFETIME_MAX, sizeof(ledger_phys_footprint_lifetime_max), &uaddr)) {
ledger_phys_footprint_lifetime_max = get_task_phys_footprint_lifetime_max(p->task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_phys_footprint_lifetime_max, sizeof(ledger_phys_footprint_lifetime_max));
}
// In the forking case, the current ledger info is copied into the corpse while the original task is suspended for consistency
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_INTERNAL, sizeof(ledger_internal), &uaddr)) {
ledger_internal = get_task_internal(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_internal, sizeof(ledger_internal));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_INTERNAL_COMPRESSED, sizeof(ledger_internal_compressed), &uaddr)) {
ledger_internal_compressed = get_task_internal_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_internal_compressed, sizeof(ledger_internal_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_IOKIT_MAPPED, sizeof(ledger_iokit_mapped), &uaddr)) {
ledger_iokit_mapped = get_task_iokit_mapped(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_iokit_mapped, sizeof(ledger_iokit_mapped));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING, sizeof(ledger_alternate_accounting), &uaddr)) {
ledger_alternate_accounting = get_task_alternate_accounting(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_alternate_accounting, sizeof(ledger_alternate_accounting));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_ALTERNATE_ACCOUNTING_COMPRESSED, sizeof(ledger_alternate_accounting_compressed), &uaddr)) {
ledger_alternate_accounting_compressed = get_task_alternate_accounting_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_alternate_accounting_compressed, sizeof(ledger_alternate_accounting_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE, sizeof(ledger_purgeable_nonvolatile), &uaddr)) {
ledger_purgeable_nonvolatile = get_task_purgeable_nonvolatile(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_purgeable_nonvolatile, sizeof(ledger_purgeable_nonvolatile));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PURGEABLE_NONVOLATILE_COMPRESSED, sizeof(ledger_purgeable_nonvolatile_compressed), &uaddr)) {
ledger_purgeable_nonvolatile_compressed = get_task_purgeable_nonvolatile_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_purgeable_nonvolatile_compressed, sizeof(ledger_purgeable_nonvolatile_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PAGE_TABLE, sizeof(ledger_page_table), &uaddr)) {
ledger_page_table = get_task_page_table(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_page_table, sizeof(ledger_page_table));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_PHYS_FOOTPRINT, sizeof(ledger_phys_footprint), &uaddr)) {
ledger_phys_footprint = get_task_phys_footprint(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_phys_footprint, sizeof(ledger_phys_footprint));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE, sizeof(ledger_network_nonvolatile), &uaddr)) {
ledger_network_nonvolatile = get_task_network_nonvolatile(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_network_nonvolatile, sizeof(ledger_network_nonvolatile));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NETWORK_NONVOLATILE_COMPRESSED, sizeof(ledger_network_nonvolatile_compressed), &uaddr)) {
ledger_network_nonvolatile_compressed = get_task_network_nonvolatile_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_network_nonvolatile_compressed, sizeof(ledger_network_nonvolatile_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_WIRED_MEM, sizeof(ledger_wired_mem), &uaddr)) {
ledger_wired_mem = get_task_wired_mem(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_wired_mem, sizeof(ledger_wired_mem));
}
bzero(&pwqinfo, sizeof(struct proc_workqueueinfo));
retval = fill_procworkqueue(p, &pwqinfo);
if (retval == 0) {
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_WORKQUEUEINFO, sizeof(struct proc_workqueueinfo), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &pwqinfo, sizeof(struct proc_workqueueinfo));
}
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_RESPONSIBLE_PID, sizeof(p->p_responsible_pid), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_responsible_pid, sizeof(p->p_responsible_pid));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_PROC_PERSONA_ID, sizeof(uid_t), &uaddr)) {
uid_t persona_id = proc_persona_id(p);
kcdata_memcpy(crash_info_ptr, uaddr, &persona_id, sizeof(persona_id));
}
#if CONFIG_COALITIONS
if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_COALITION_ID, sizeof(uint64_t), COALITION_NUM_TYPES, &uaddr)) {
uint64_t coalition_ids[COALITION_NUM_TYPES];
task_coalition_ids(p->task, coalition_ids);
kcdata_memcpy(crash_info_ptr, uaddr, coalition_ids, sizeof(coalition_ids));
}
#endif /* CONFIG_COALITIONS */
#if CONFIG_MEMORYSTATUS
memstat_dirty_flags = memorystatus_dirty_get(p, FALSE);
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_DIRTY_FLAGS, sizeof(memstat_dirty_flags), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &memstat_dirty_flags, sizeof(memstat_dirty_flags));
}
#endif
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORY_LIMIT_INCREASE, sizeof(p->p_memlimit_increase), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_memlimit_increase, sizeof(p->p_memlimit_increase));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT, sizeof(ledger_tagged_footprint), &uaddr)) {
ledger_tagged_footprint = get_task_tagged_footprint(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_tagged_footprint, sizeof(ledger_tagged_footprint));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_TAGGED_FOOTPRINT_COMPRESSED, sizeof(ledger_tagged_footprint_compressed), &uaddr)) {
ledger_tagged_footprint_compressed = get_task_tagged_footprint_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_tagged_footprint_compressed, sizeof(ledger_tagged_footprint_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT, sizeof(ledger_media_footprint), &uaddr)) {
ledger_media_footprint = get_task_media_footprint(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_media_footprint, sizeof(ledger_media_footprint));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_MEDIA_FOOTPRINT_COMPRESSED, sizeof(ledger_media_footprint_compressed), &uaddr)) {
ledger_media_footprint_compressed = get_task_media_footprint_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_media_footprint_compressed, sizeof(ledger_media_footprint_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT, sizeof(ledger_graphics_footprint), &uaddr)) {
ledger_graphics_footprint = get_task_graphics_footprint(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_graphics_footprint, sizeof(ledger_graphics_footprint));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_GRAPHICS_FOOTPRINT_COMPRESSED, sizeof(ledger_graphics_footprint_compressed), &uaddr)) {
ledger_graphics_footprint_compressed = get_task_graphics_footprint_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_graphics_footprint_compressed, sizeof(ledger_graphics_footprint_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT, sizeof(ledger_neural_footprint), &uaddr)) {
ledger_neural_footprint = get_task_neural_footprint(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_neural_footprint, sizeof(ledger_neural_footprint));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_LEDGER_NEURAL_FOOTPRINT_COMPRESSED, sizeof(ledger_neural_footprint_compressed), &uaddr)) {
ledger_neural_footprint_compressed = get_task_neural_footprint_compressed(corpse_task);
kcdata_memcpy(crash_info_ptr, uaddr, &ledger_neural_footprint_compressed, sizeof(ledger_neural_footprint_compressed));
}
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, TASK_CRASHINFO_MEMORYSTATUS_EFFECTIVE_PRIORITY, sizeof(p->p_memstat_effectivepriority), &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, &p->p_memstat_effectivepriority, sizeof(p->p_memstat_effectivepriority));
}
if (p->p_exit_reason != OS_REASON_NULL && reason == OS_REASON_NULL) {
reason = p->p_exit_reason;
}
if (reason != OS_REASON_NULL) {
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, EXIT_REASON_SNAPSHOT, sizeof(struct exit_reason_snapshot), &uaddr)) {
struct exit_reason_snapshot ers = {
.ers_namespace = reason->osr_namespace,
.ers_code = reason->osr_code,
.ers_flags = reason->osr_flags
};
kcdata_memcpy(crash_info_ptr, uaddr, &ers, sizeof(ers));
}
if (reason->osr_kcd_buf != 0) {
uint32_t reason_buf_size = (uint32_t)kcdata_memory_get_used_bytes(&reason->osr_kcd_descriptor);
assert(reason_buf_size != 0);
if (KERN_SUCCESS == kcdata_get_memory_addr(crash_info_ptr, KCDATA_TYPE_NESTED_KCDATA, reason_buf_size, &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, reason->osr_kcd_buf, reason_buf_size);
}
}
}
if (num_udata > 0) {
if (KERN_SUCCESS == kcdata_get_memory_addr_for_array(crash_info_ptr, TASK_CRASHINFO_UDATA_PTRS,
sizeof(uint64_t), num_udata, &uaddr)) {
kcdata_memcpy(crash_info_ptr, uaddr, udata_buffer, sizeof(uint64_t) * num_udata);
}
}
}
/*
* We only parse exit reason kcdata blobs for launchd when it dies
* and we're going to panic.
*
* Meant to be called immediately before panicking.
*/
char *
launchd_exit_reason_get_string_desc(os_reason_t exit_reason)
{
kcdata_iter_t iter;
if (exit_reason == OS_REASON_NULL || exit_reason->osr_kcd_buf == NULL ||
exit_reason->osr_bufsize == 0) {
return NULL;
}
iter = kcdata_iter(exit_reason->osr_kcd_buf, exit_reason->osr_bufsize);
if (!kcdata_iter_valid(iter)) {
#if DEBUG || DEVELOPMENT
printf("launchd exit reason has invalid exit reason buffer\n");
#endif
return NULL;
}
if (kcdata_iter_type(iter) != KCDATA_BUFFER_BEGIN_OS_REASON) {
#if DEBUG || DEVELOPMENT
printf("launchd exit reason buffer type mismatch, expected %d got %d\n",
KCDATA_BUFFER_BEGIN_OS_REASON, kcdata_iter_type(iter));
#endif
return NULL;
}
iter = kcdata_iter_find_type(iter, EXIT_REASON_USER_DESC);
if (!kcdata_iter_valid(iter)) {
return NULL;
}
return (char *)kcdata_iter_payload(iter);
}
__abortlike
static void
launchd_crashed_panic(proc_t p, int rv)
{
char *launchd_exit_reason_desc = launchd_exit_reason_get_string_desc(p->p_exit_reason);
if (p->p_exit_reason == OS_REASON_NULL) {
printf("pid 1 exited -- no exit reason available -- (signal %d, exit %d)\n",
WTERMSIG(rv), WEXITSTATUS(rv));
} else {
printf("pid 1 exited -- exit reason namespace %d subcode 0x%llx, description %s\n",
p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code, launchd_exit_reason_desc ?
launchd_exit_reason_desc : "none");
}
const char *launchd_crashed_prefix_str;
if (strnstr(p->p_name, "preinit", sizeof(p->p_name))) {
launchd_crashed_prefix_str = "LTE preinit process exited";
} else {
launchd_crashed_prefix_str = "initproc exited";
}
#if (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP
/*
* For debugging purposes, generate a core file of initproc before
* panicking. Leave at least 300 MB free on the root volume, and ignore
* the process's corefile ulimit. fsync() the file to ensure it lands on disk
* before the panic hits.
*/
int err;
uint64_t coredump_start = mach_absolute_time();
uint64_t coredump_end;
clock_sec_t tv_sec;
clock_usec_t tv_usec;
uint32_t tv_msec;
err = coredump(p, 300, COREDUMP_IGNORE_ULIMIT | COREDUMP_FULLFSYNC);
coredump_end = mach_absolute_time();
absolutetime_to_microtime(coredump_end - coredump_start, &tv_sec, &tv_usec);
tv_msec = tv_usec / 1000;
if (err != 0) {
printf("Failed to generate initproc core file: error %d, took %d.%03d seconds\n",
err, (uint32_t)tv_sec, tv_msec);
} else {
printf("Generated initproc core file in %d.%03d seconds\n",
(uint32_t)tv_sec, tv_msec);
}
#endif /* (DEVELOPMENT || DEBUG) && CONFIG_COREDUMP */
sync(p, (void *)NULL, (int *)NULL);
if (p->p_exit_reason == OS_REASON_NULL) {
panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s -- no exit reason available -- (signal %d, exit status %d %s)",
launchd_crashed_prefix_str, WTERMSIG(rv), WEXITSTATUS(rv), ((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""));
} else {
panic_with_options(0, NULL, DEBUGGER_OPTION_INITPROC_PANIC, "%s %s -- exit reason namespace %d subcode 0x%llx description: %." LAUNCHD_PANIC_REASON_STRING_MAXLEN "s",
((p->p_csflags & CS_KILLED) ? "CS_KILLED" : ""),
launchd_crashed_prefix_str, p->p_exit_reason->osr_namespace, p->p_exit_reason->osr_code,
launchd_exit_reason_desc ? launchd_exit_reason_desc : "none");
}
}
#define OS_REASON_IFLAG_USER_FAULT 0x1
#define OS_REASON_TOTAL_USER_FAULTS_PER_PROC 5
static int
abort_with_payload_internal(proc_t p,
uint32_t reason_namespace, uint64_t reason_code,
user_addr_t payload, uint32_t payload_size,
user_addr_t reason_string, uint64_t reason_flags,
uint32_t internal_flags)
{
os_reason_t exit_reason = OS_REASON_NULL;
kern_return_t kr = KERN_SUCCESS;
if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
uint32_t old_value = atomic_load_explicit(&p->p_user_faults,
memory_order_relaxed);
for (;;) {
if (old_value >= OS_REASON_TOTAL_USER_FAULTS_PER_PROC) {
return EQFULL;
}
// this reloads the value in old_value
if (atomic_compare_exchange_strong_explicit(&p->p_user_faults,
&old_value, old_value + 1, memory_order_relaxed,
memory_order_relaxed)) {
break;
}
}
}
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE,
p->p_pid, reason_namespace,
reason_code, 0, 0);
exit_reason = build_userspace_exit_reason(reason_namespace, reason_code,
payload, payload_size, reason_string, reason_flags | OS_REASON_FLAG_ABORT);
if (internal_flags & OS_REASON_IFLAG_USER_FAULT) {
mach_exception_code_t code = 0;
EXC_GUARD_ENCODE_TYPE(code, GUARD_TYPE_USER); /* simulated EXC_GUARD */
EXC_GUARD_ENCODE_FLAVOR(code, 0);
EXC_GUARD_ENCODE_TARGET(code, reason_namespace);
if (exit_reason == OS_REASON_NULL) {
kr = KERN_RESOURCE_SHORTAGE;
} else {
kr = task_violated_guard(code, reason_code, exit_reason);
}
os_reason_free(exit_reason);
} else {
/*
* We use SIGABRT (rather than calling exit directly from here) so that
* the debugger can catch abort_with_{reason,payload} calls.
*/
psignal_try_thread_with_reason(p, current_thread(), SIGABRT, exit_reason);
}
switch (kr) {
case KERN_SUCCESS:
return 0;
case KERN_NOT_SUPPORTED:
return ENOTSUP;
case KERN_INVALID_ARGUMENT:
return EINVAL;
case KERN_RESOURCE_SHORTAGE:
default:
return EBUSY;
}
}
int
abort_with_payload(struct proc *cur_proc, struct abort_with_payload_args *args,
__unused void *retval)
{
abort_with_payload_internal(cur_proc, args->reason_namespace,
args->reason_code, args->payload, args->payload_size,
args->reason_string, args->reason_flags, 0);
return 0;
}
int
os_fault_with_payload(struct proc *cur_proc,
struct os_fault_with_payload_args *args, __unused int *retval)
{
return abort_with_payload_internal(cur_proc, args->reason_namespace,
args->reason_code, args->payload, args->payload_size,
args->reason_string, args->reason_flags, OS_REASON_IFLAG_USER_FAULT);
}
/*
* exit --
* Death of process.
*/
__attribute__((noreturn))
void
exit(proc_t p, struct exit_args *uap, int *retval)
{
p->p_xhighbits = ((uint32_t)(uap->rval) & 0xFF000000) >> 24;
exit1(p, W_EXITCODE((uint32_t)uap->rval, 0), retval);
thread_exception_return();
/* NOTREACHED */
while (TRUE) {
thread_block(THREAD_CONTINUE_NULL);
}
/* NOTREACHED */
}
/*
* Exit: deallocate address space and other resources, change proc state
* to zombie, and unlink proc from allproc and parent's lists. Save exit
* status and rusage for wait(). Check for child processes and orphan them.
*/
int
exit1(proc_t p, int rv, int *retval)
{
return exit1_internal(p, rv, retval, TRUE, TRUE, 0);
}
int
exit1_internal(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
int jetsam_flags)
{
return exit_with_reason(p, rv, retval, thread_can_terminate, perf_notify, jetsam_flags, OS_REASON_NULL);
}
/*
* NOTE: exit_with_reason drops a reference on the passed exit_reason
*/
int
exit_with_reason(proc_t p, int rv, int *retval, boolean_t thread_can_terminate, boolean_t perf_notify,
int jetsam_flags, struct os_reason *exit_reason)
{
thread_t self = current_thread();
struct task *task = p->task;
struct uthread *ut;
int error = 0;
/*
* If a thread in this task has already
* called exit(), then halt any others
* right here.
*/
ut = get_bsdthread_info(self);
if ((p == current_proc()) &&
(ut->uu_flag & UT_VFORK)) {
os_reason_free(exit_reason);
if (!thread_can_terminate) {
return EINVAL;
}
vfork_exit(p, rv);
vfork_return(p, retval, p->p_pid);
unix_syscall_return(0);
/* NOT REACHED */
}
/*
* The parameter list of audit_syscall_exit() was augmented to
* take the Darwin syscall number as the first parameter,
* which is currently required by mac_audit_postselect().
*/
/*
* The BSM token contains two components: an exit status as passed
* to exit(), and a return value to indicate what sort of exit it
* was. The exit status is WEXITSTATUS(rv), but it's not clear
* what the return value is.
*/
AUDIT_ARG(exit, WEXITSTATUS(rv), 0);
/*
* TODO: what to audit here when jetsam calls exit and the uthread,
* 'ut' does not belong to the proc, 'p'.
*/
AUDIT_SYSCALL_EXIT(SYS_exit, p, ut, 0); /* Exit is always successfull */
DTRACE_PROC1(exit, int, CLD_EXITED);
/* mark process is going to exit and pull out of DBG/disk throttle */
/* TODO: This should be done after becoming exit thread */
proc_set_task_policy(p->task, TASK_POLICY_ATTRIBUTE,
TASK_POLICY_TERMINATED, TASK_POLICY_ENABLE);
proc_lock(p);
error = proc_transstart(p, 1, (jetsam_flags ? 1 : 0));
if (error == EDEADLK) {
/*
* If proc_transstart() returns EDEADLK, then another thread
* is either exec'ing or exiting. Return an error and allow
* the other thread to continue.
*/
proc_unlock(p);
os_reason_free(exit_reason);
if (current_proc() == p) {
if (p->exit_thread == self) {
panic("exit_thread failed to exit");
}
if (thread_can_terminate) {
thread_exception_return();
}
}
return error;
}
while (p->exit_thread != self) {
if (sig_try_locked(p) <= 0) {
proc_transend(p, 1);
os_reason_free(exit_reason);
if (get_threadtask(self) != task) {
proc_unlock(p);
return 0;
}
proc_unlock(p);
thread_terminate(self);
if (!thread_can_terminate) {
return 0;
}
thread_exception_return();
/* NOTREACHED */
}
sig_lock_to_exit(p);
}
if (exit_reason != OS_REASON_NULL) {
KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_COMMIT) | DBG_FUNC_NONE,
p->p_pid, exit_reason->osr_namespace,
exit_reason->osr_code, 0, 0);
}
assert(p->p_exit_reason == OS_REASON_NULL);
p->p_exit_reason = exit_reason;
p->p_lflag |= P_LEXIT;
p->p_xstat = rv;
p->p_lflag |= jetsam_flags;
proc_transend(p, 1);
proc_unlock(p);
proc_prepareexit(p, rv, perf_notify);
/* Last thread to terminate will call proc_exit() */
task_terminate_internal(task);
return 0;
}
#if CONFIG_MEMORYSTATUS
/*
* Remove this process from jetsam bands for freezing or exiting. Note this will block, if the process
* is currently being frozen.
* The proc_list_lock is held by the caller.