]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_core.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1991 NeXT Computer, Inc. All rights reserved.
29 *
30 * File: bsd/kern/kern_core.c
31 *
32 * This file contains machine independent code for performing core dumps.
33 *
1c79356b 34 */
39037602 35#if CONFIG_COREDUMP
1c79356b
A
36
37#include <mach/vm_param.h>
38#include <mach/thread_status.h>
5ba3f43e 39#include <sys/content_protection.h>
1c79356b
A
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/signalvar.h>
43#include <sys/resourcevar.h>
44#include <sys/namei.h>
91447636
A
45#include <sys/vnode_internal.h>
46#include <sys/proc_internal.h>
47#include <sys/kauth.h>
1c79356b
A
48#include <sys/timeb.h>
49#include <sys/times.h>
1c79356b 50#include <sys/acct.h>
91447636 51#include <sys/file_internal.h>
1c79356b
A
52#include <sys/uio.h>
53#include <sys/kernel.h>
54#include <sys/stat.h>
55
56#include <mach-o/loader.h>
57#include <mach/vm_region.h>
765c9de3 58#include <mach/vm_statistics.h>
1c79356b
A
59
60#include <vm/vm_kern.h>
91447636 61#include <vm/vm_protos.h> /* last */
0a7de745
A
62#include <vm/vm_map.h> /* current_map() */
63#include <mach/mach_vm.h> /* mach_vm_region_recurse() */
64#include <mach/task.h> /* task_suspend() */
65#include <kern/task.h> /* get_task_numacts() */
1c79356b 66
b0d623f7
A
67#include <security/audit/audit.h>
68
c6bf4f31
A
69#if CONFIG_MACF
70#include <security/mac_framework.h>
71#endif /* CONFIG_MACF */
72
1c79356b 73typedef struct {
0a7de745
A
74 int flavor; /* the number for this flavor */
75 mach_msg_type_number_t count; /* count of ints in this flavor */
1c79356b
A
76} mythread_state_flavor_t;
77
6d2010ae 78#if defined (__i386__) || defined (__x86_64__)
0a7de745
A
79mythread_state_flavor_t thread_flavor_array[] = {
80 {x86_THREAD_STATE, x86_THREAD_STATE_COUNT},
81 {x86_FLOAT_STATE, x86_FLOAT_STATE_COUNT},
82 {x86_EXCEPTION_STATE, x86_EXCEPTION_STATE_COUNT},
83};
84int mynum_flavors = 3;
5ba3f43e 85#elif defined (__arm__)
0a7de745
A
86mythread_state_flavor_t thread_flavor_array[] = {
87 {ARM_THREAD_STATE, ARM_THREAD_STATE_COUNT},
88 {ARM_VFP_STATE, ARM_VFP_STATE_COUNT},
89 {ARM_EXCEPTION_STATE, ARM_EXCEPTION_STATE_COUNT}
90};
91int mynum_flavors = 3;
5ba3f43e
A
92
93#elif defined (__arm64__)
0a7de745
A
94mythread_state_flavor_t thread_flavor_array[] = {
95 {ARM_THREAD_STATE64, ARM_THREAD_STATE64_COUNT},
96 /* ARM64_TODO: VFP */
97 {ARM_EXCEPTION_STATE64, ARM_EXCEPTION_STATE64_COUNT}
98};
99int mynum_flavors = 2;
1c79356b
A
100#else
101#error architecture not supported
102#endif
103
104
105typedef struct {
0a7de745 106 vm_offset_t header;
f427ee49 107 size_t hoffset;
1c79356b 108 mythread_state_flavor_t *flavors;
f427ee49
A
109 size_t tstate_size;
110 size_t flavor_count;
1c79356b
A
111} tir_t;
112
39236c6e
A
113extern int freespace_mb(vnode_t vp);
114
e5568f75 115/* XXX not in a Mach header anywhere */
39037602 116kern_return_t thread_getstatus(thread_t act, int flavor,
0a7de745
A
117 thread_state_t tstate, mach_msg_type_number_t *count);
118void task_act_iterate_wth_args(task_t, void (*)(thread_t, void *), void *);
e5568f75 119
593a1d5f 120#ifdef SECURE_KERNEL
0a7de745 121__XNU_PRIVATE_EXTERN int do_coredump = 0; /* default: don't dump cores */
593a1d5f 122#else
0a7de745 123__XNU_PRIVATE_EXTERN int do_coredump = 1; /* default: dump cores */
593a1d5f 124#endif
39236c6e 125__XNU_PRIVATE_EXTERN int sugid_coredump = 0; /* default: but not SGUID binaries */
e5568f75 126
b0d623f7
A
127
128/* cpu_type returns only the most generic indication of the current CPU. */
129/* in a core we want to know the kind of process. */
130
131static cpu_type_t
132process_cpu_type(proc_t core_proc)
133{
134 cpu_type_t what_we_think;
135#if defined (__i386__) || defined (__x86_64__)
d9a64523 136 if (IS_64BIT_PROCESS(core_proc)) {
b0d623f7
A
137 what_we_think = CPU_TYPE_X86_64;
138 } else {
139 what_we_think = CPU_TYPE_I386;
140 }
5ba3f43e
A
141#elif defined (__arm__) || defined(__arm64__)
142 if (IS_64BIT_PROCESS(core_proc)) {
143 what_we_think = CPU_TYPE_ARM64;
144 } else {
145 what_we_think = CPU_TYPE_ARM;
146 }
b0d623f7 147#endif
d9a64523 148
b0d623f7
A
149 return what_we_think;
150}
151
152static cpu_type_t
153process_cpu_subtype(proc_t core_proc)
154{
155 cpu_type_t what_we_think;
156#if defined (__i386__) || defined (__x86_64__)
d9a64523 157 if (IS_64BIT_PROCESS(core_proc)) {
b0d623f7
A
158 what_we_think = CPU_SUBTYPE_X86_64_ALL;
159 } else {
160 what_we_think = CPU_SUBTYPE_I386_ALL;
161 }
5ba3f43e 162#elif defined (__arm__) || defined(__arm64__)
d9a64523 163 if (IS_64BIT_PROCESS(core_proc)) {
5ba3f43e
A
164 what_we_think = CPU_SUBTYPE_ARM64_ALL;
165 } else {
166 what_we_think = CPU_SUBTYPE_ARM_ALL;
167 }
b0d623f7
A
168#endif
169 return what_we_think;
170}
171
39037602 172static void
91447636 173collectth_state(thread_t th_act, void *tirp)
1c79356b 174{
0a7de745 175 vm_offset_t header;
f427ee49 176 size_t hoffset, i;
1c79356b 177 mythread_state_flavor_t *flavors;
0a7de745 178 struct thread_command *tc;
91447636
A
179 tir_t *t = (tir_t *)tirp;
180
0a7de745
A
181 /*
182 * Fill in thread command structure.
183 */
184 header = t->header;
185 hoffset = t->hoffset;
186 flavors = t->flavors;
187
188 tc = (struct thread_command *) (header + hoffset);
189 tc->cmd = LC_THREAD;
f427ee49
A
190 tc->cmdsize = (uint32_t)(sizeof(struct thread_command)
191 + t->tstate_size);
0a7de745
A
192 hoffset += sizeof(struct thread_command);
193 /*
194 * Follow with a struct thread_state_flavor and
195 * the appropriate thread state struct for each
196 * thread state flavor.
197 */
198 for (i = 0; i < t->flavor_count; i++) {
199 *(mythread_state_flavor_t *)(header + hoffset) =
200 flavors[i];
201 hoffset += sizeof(mythread_state_flavor_t);
202 thread_getstatus(th_act, flavors[i].flavor,
203 (thread_state_t)(header + hoffset),
204 &flavors[i].count);
205 hoffset += flavors[i].count * sizeof(int);
206 }
1c79356b 207
0a7de745 208 t->hoffset = hoffset;
1c79356b 209}
e5568f75 210
1c79356b 211/*
2d21ac55
A
212 * coredump
213 *
214 * Description: Create a core image on the file "core" for the process
215 * indicated
216 *
217 * Parameters: core_proc Process to dump core [*]
39236c6e
A
218 * reserve_mb If non-zero, leave filesystem with
219 * at least this much free space.
3e170ce0 220 * coredump_flags Extra options (ignore rlimit, run fsync)
2d21ac55
A
221 *
222 * Returns: 0 Success
c6bf4f31 223 * !0 Failure errno
2d21ac55
A
224 *
225 * IMPORTANT: This function can only be called on the current process, due
226 * to assumptions below; see variable declaration section for
227 * details.
1c79356b 228 */
0a7de745 229#define MAX_TSTATE_FLAVORS 10
1c79356b 230int
3e170ce0 231coredump(proc_t core_proc, uint32_t reserve_mb, int coredump_flags)
1c79356b 232{
2d21ac55
A
233/* Begin assumptions that limit us to only the current process */
234 vfs_context_t ctx = vfs_context_current();
0a7de745
A
235 vm_map_t map = current_map();
236 task_t task = current_task();
2d21ac55
A
237/* End assumptions */
238 kauth_cred_t cred = vfs_context_ucred(ctx);
239 int error = 0;
91447636 240 struct vnode_attr va;
f427ee49
A
241 size_t thread_count, segment_count;
242 size_t command_size, header_size, tstate_size;
243 size_t hoffset;
0a7de745 244 off_t foffset;
316670eb 245 mach_vm_offset_t vmoffset;
0a7de745
A
246 vm_offset_t header;
247 mach_vm_size_t vmsize;
248 vm_prot_t prot;
249 vm_prot_t maxprot;
250 vm_inherit_t inherit;
251 int error1 = 0;
252 char stack_name[MAXCOMLEN + 6];
253 char *alloced_name = NULL;
c6bf4f31 254 char *name = NULL;
1c79356b 255 mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
0a7de745 256 vm_size_t mapsize;
f427ee49 257 size_t i;
2d21ac55 258 uint32_t nesting_depth = 0;
0a7de745 259 kern_return_t kret;
1c79356b 260 struct vm_region_submap_info_64 vbr;
2d21ac55 261 mach_msg_type_number_t vbrcount = 0;
1c79356b
A
262 tir_t tir1;
263 struct vnode * vp;
0a7de745
A
264 struct mach_header *mh = NULL; /* protected by is_64 */
265 struct mach_header_64 *mh64 = NULL; /* protected by is_64 */
266 int is_64 = 0;
267 size_t mach_header_sz = sizeof(struct mach_header);
268 size_t segment_command_sz = sizeof(struct segment_command);
269
fe8ab488
A
270 if (current_proc() != core_proc) {
271 panic("coredump() called against proc that is not current_proc: %p", core_proc);
272 }
1c79356b 273
0a7de745
A
274 if (do_coredump == 0 || /* Not dumping at all */
275 ((sugid_coredump == 0) && /* Not dumping SUID/SGID binaries */
276 ((kauth_cred_getsvuid(cred) != kauth_cred_getruid(cred)) ||
277 (kauth_cred_getsvgid(cred) != kauth_cred_getrgid(cred))))) {
c6bf4f31
A
278 error = EFAULT;
279 goto out2;
280 }
281
282#if CONFIG_MACF
283 error = mac_proc_check_dump_core(core_proc);
284 if (error != 0) {
285 goto out2;
e5568f75 286 }
c6bf4f31 287#endif
1c79356b 288
2d21ac55 289 if (IS_64BIT_PROCESS(core_proc)) {
91447636
A
290 is_64 = 1;
291 mach_header_sz = sizeof(struct mach_header_64);
292 segment_command_sz = sizeof(struct segment_command_64);
293 }
294
1c79356b
A
295 mapsize = get_vmmap_size(map);
296
3e170ce0 297 if (((coredump_flags & COREDUMP_IGNORE_ULIMIT) == 0) &&
f427ee49 298 (mapsize >= proc_limitgetcur(core_proc, RLIMIT_CORE, FALSE))) {
c6bf4f31
A
299 error = EFAULT;
300 goto out2;
0a7de745 301 }
3e170ce0 302
39236c6e 303 (void) task_suspend_internal(task);
1c79356b 304
c3c9b80d 305 alloced_name = zalloc_flags(ZV_NAMEI, Z_NOWAIT | Z_ZERO);
e5568f75 306
2d21ac55 307 /* create name according to sysctl'able format string */
e5568f75 308 /* if name creation fails, fall back to historical behaviour... */
b0d623f7
A
309 if (alloced_name == NULL ||
310 proc_core_name(core_proc->p_comm, kauth_cred_getuid(cred),
0a7de745 311 core_proc->p_pid, alloced_name, MAXPATHLEN)) {
2d21ac55 312 snprintf(stack_name, sizeof(stack_name),
0a7de745 313 "/cores/core.%d", core_proc->p_pid);
2d21ac55 314 name = stack_name;
0a7de745 315 } else {
2d21ac55 316 name = alloced_name;
0a7de745 317 }
e5568f75 318
0a7de745 319 if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, ctx))) {
2d21ac55 320 goto out2;
0a7de745 321 }
91447636
A
322
323 VATTR_INIT(&va);
324 VATTR_WANTED(&va, va_nlink);
1c79356b
A
325 /* Don't dump to non-regular files or files with links. */
326 if (vp->v_type != VREG ||
2d21ac55 327 vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) {
1c79356b
A
328 error = EFAULT;
329 goto out;
330 }
331
0a7de745 332 VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_setsize */
91447636 333 VATTR_SET(&va, va_data_size, 0);
5ba3f43e
A
334 if (core_proc == initproc) {
335 VATTR_SET(&va, va_dataprotect_class, PROTECTION_CLASS_D);
336 }
2d21ac55
A
337 vnode_setattr(vp, &va, ctx);
338 core_proc->p_acflag |= ACORE;
1c79356b 339
39236c6e
A
340 if ((reserve_mb > 0) &&
341 ((freespace_mb(vp) - (mapsize >> 20)) < reserve_mb)) {
342 error = ENOSPC;
343 goto out;
344 }
345
1c79356b
A
346 /*
347 * If the task is modified while dumping the file
348 * (e.g., changes in threads or VM, the resulting
349 * file will not necessarily be correct.
350 */
351
352 thread_count = get_task_numacts(task);
0a7de745
A
353 segment_count = get_vmmap_entries(map); /* XXX */
354 tir1.flavor_count = sizeof(thread_flavor_array) / sizeof(mythread_state_flavor_t);
355 bcopy(thread_flavor_array, flavors, sizeof(thread_flavor_array));
1c79356b 356 tstate_size = 0;
0a7de745 357 for (i = 0; i < tir1.flavor_count; i++) {
1c79356b 358 tstate_size += sizeof(mythread_state_flavor_t) +
0a7de745
A
359 (flavors[i].count * sizeof(int));
360 }
1c79356b 361
f427ee49
A
362 {
363 size_t lhs;
364 size_t rhs;
365
366 /* lhs = segment_count * segment_command_sz */
367 if (os_mul_overflow(segment_count, segment_command_sz, &lhs)) {
368 error = ENOMEM;
369 goto out;
370 }
371
372 /* rhs = (tstate_size + sizeof(struct thread_command)) * thread_count */
373 if (os_add_and_mul_overflow(tstate_size, sizeof(struct thread_command), thread_count, &rhs)) {
374 error = ENOMEM;
375 goto out;
376 }
377
378 /* command_size = lhs + rhs */
379 if (os_add_overflow(lhs, rhs, &command_size)) {
380 error = ENOMEM;
381 goto out;
382 }
383 }
384
385 if (os_add_overflow(command_size, mach_header_sz, &header_size)) {
386 error = ENOMEM;
387 goto out;
388 }
1c79356b 389
3e170ce0 390 if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) {
b0d623f7
A
391 error = ENOMEM;
392 goto out;
393 }
1c79356b
A
394
395 /*
396 * Set up Mach-O header.
397 */
91447636
A
398 if (is_64) {
399 mh64 = (struct mach_header_64 *)header;
400 mh64->magic = MH_MAGIC_64;
b0d623f7
A
401 mh64->cputype = process_cpu_type(core_proc);
402 mh64->cpusubtype = process_cpu_subtype(core_proc);
91447636 403 mh64->filetype = MH_CORE;
f427ee49
A
404 mh64->ncmds = (uint32_t)(segment_count + thread_count);
405 mh64->sizeofcmds = (uint32_t)command_size;
0a7de745 406 mh64->reserved = 0; /* 8 byte alignment */
91447636
A
407 } else {
408 mh = (struct mach_header *)header;
409 mh->magic = MH_MAGIC;
b0d623f7
A
410 mh->cputype = process_cpu_type(core_proc);
411 mh->cpusubtype = process_cpu_subtype(core_proc);
91447636 412 mh->filetype = MH_CORE;
f427ee49
A
413 mh->ncmds = (uint32_t)(segment_count + thread_count);
414 mh->sizeofcmds = (uint32_t)command_size;
91447636
A
415 }
416
0a7de745
A
417 hoffset = mach_header_sz; /* offset into header */
418 foffset = round_page(header_size); /* offset into file */
419 vmoffset = MACH_VM_MIN_ADDRESS; /* offset into VM */
91447636 420
55e303ae 421 /*
0a7de745 422 * We use to check for an error, here, now we try and get
1c79356b
A
423 * as much as we can
424 */
91447636 425 while (segment_count > 0) {
0a7de745
A
426 struct segment_command *sc;
427 struct segment_command_64 *sc64;
91447636 428
1c79356b
A
429 /*
430 * Get region information for next region.
431 */
0a7de745 432
1c79356b
A
433 while (1) {
434 vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64;
0a7de745
A
435 if ((kret = mach_vm_region_recurse(map,
436 &vmoffset, &vmsize, &nesting_depth,
437 (vm_region_recurse_info_t)&vbr,
438 &vbrcount)) != KERN_SUCCESS) {
91447636
A
439 break;
440 }
441 /*
442 * If we get a valid mapping back, but we're dumping
443 * a 32 bit process, and it's over the allowable
444 * address space of a 32 bit process, it's the same
445 * as if mach_vm_region_recurse() failed.
446 */
447 if (!(is_64) &&
448 (vmoffset + vmsize > VM_MAX_ADDRESS)) {
0a7de745 449 kret = KERN_INVALID_ADDRESS;
1c79356b
A
450 break;
451 }
0a7de745 452 if (vbr.is_submap) {
1c79356b
A
453 nesting_depth++;
454 continue;
455 } else {
456 break;
457 }
458 }
0a7de745 459 if (kret != KERN_SUCCESS) {
1c79356b 460 break;
0a7de745 461 }
1c79356b
A
462
463 prot = vbr.protection;
464 maxprot = vbr.max_protection;
465 inherit = vbr.inheritance;
466 /*
467 * Fill in segment command structure.
468 */
91447636
A
469 if (is_64) {
470 sc64 = (struct segment_command_64 *)(header + hoffset);
471 sc64->cmd = LC_SEGMENT_64;
472 sc64->cmdsize = sizeof(struct segment_command_64);
473 /* segment name is zeroed by kmem_alloc */
474 sc64->segname[0] = 0;
475 sc64->vmaddr = vmoffset;
476 sc64->vmsize = vmsize;
477 sc64->fileoff = foffset;
478 sc64->filesize = vmsize;
479 sc64->maxprot = maxprot;
480 sc64->initprot = prot;
481 sc64->nsects = 0;
3e170ce0 482 sc64->flags = 0;
0a7de745 483 } else {
91447636
A
484 sc = (struct segment_command *) (header + hoffset);
485 sc->cmd = LC_SEGMENT;
486 sc->cmdsize = sizeof(struct segment_command);
487 /* segment name is zeroed by kmem_alloc */
488 sc->segname[0] = 0;
f427ee49
A
489 sc->vmaddr = CAST_DOWN_EXPLICIT(uint32_t, vmoffset);
490 sc->vmsize = CAST_DOWN_EXPLICIT(uint32_t, vmsize);
0a7de745
A
491 sc->fileoff = CAST_DOWN_EXPLICIT(uint32_t, foffset); /* will never truncate */
492 sc->filesize = CAST_DOWN_EXPLICIT(uint32_t, vmsize); /* will never truncate */
91447636
A
493 sc->maxprot = maxprot;
494 sc->initprot = prot;
495 sc->nsects = 0;
3e170ce0 496 sc->flags = 0;
91447636 497 }
1c79356b
A
498
499 /*
500 * Write segment out. Try as hard as possible to
501 * get read access to the data.
502 */
503 if ((prot & VM_PROT_READ) == 0) {
91447636 504 mach_vm_protect(map, vmoffset, vmsize, FALSE,
0a7de745 505 prot | VM_PROT_READ);
1c79356b
A
506 }
507 /*
508 * Only actually perform write if we can read.
509 * Note: if we can't read, then we end up with
510 * a hole in the file.
511 */
55e303ae 512 if ((maxprot & VM_PROT_READ) == VM_PROT_READ
0a7de745
A
513 && vbr.user_tag != VM_MEMORY_IOKIT
514 && coredumpok(map, vmoffset)) {
b0d623f7 515 error = vn_rdwr_64(UIO_WRITE, vp, vmoffset, vmsize, foffset,
0a7de745
A
516 (IS_64BIT_PROCESS(core_proc) ? UIO_USERSPACE64 : UIO_USERSPACE32),
517 IO_NOCACHE | IO_NODELOCKED | IO_UNIT, cred, (int64_t *) 0, core_proc);
1c79356b
A
518 }
519
91447636
A
520 hoffset += segment_command_sz;
521 foffset += vmsize;
522 vmoffset += vmsize;
1c79356b
A
523 segment_count--;
524 }
525
91447636
A
526 /*
527 * If there are remaining segments which have not been written
528 * out because break in the loop above, then they were not counted
529 * because they exceed the real address space of the executable
530 * type: remove them from the header's count. This is OK, since
531 * we are allowed to have a sparse area following the segments.
532 */
533 if (is_64) {
534 mh64->ncmds -= segment_count;
2d21ac55 535 mh64->sizeofcmds -= segment_count * segment_command_sz;
91447636
A
536 } else {
537 mh->ncmds -= segment_count;
2d21ac55 538 mh->sizeofcmds -= segment_count * segment_command_sz;
91447636
A
539 }
540
1c79356b
A
541 tir1.header = header;
542 tir1.hoffset = hoffset;
543 tir1.flavors = flavors;
544 tir1.tstate_size = tstate_size;
0a7de745 545 task_act_iterate_wth_args(task, collectth_state, &tir1);
1c79356b 546
1c79356b
A
547 /*
548 * Write out the Mach header at the beginning of the
91447636 549 * file. OK to use a 32 bit write for this.
1c79356b 550 */
f427ee49 551 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, (int)MIN(header_size, INT_MAX), (off_t)0,
0a7de745 552 UIO_SYSSPACE, IO_NOCACHE | IO_NODELOCKED | IO_UNIT, cred, (int *) 0, core_proc);
1c79356b 553 kmem_free(kernel_map, header, header_size);
3e170ce0 554
0a7de745 555 if ((coredump_flags & COREDUMP_FULLFSYNC) && error == 0) {
3e170ce0 556 error = VNOP_IOCTL(vp, F_FULLFSYNC, (caddr_t)NULL, 0, ctx);
0a7de745 557 }
1c79356b 558out:
2d21ac55
A
559 error1 = vnode_close(vp, FWRITE, ctx);
560out2:
b0d623f7
A
561#if CONFIG_AUDIT
562 audit_proc_coredump(core_proc, name, error);
563#endif
0a7de745 564 if (alloced_name != NULL) {
c3c9b80d 565 zfree(ZV_NAMEI, alloced_name);
0a7de745
A
566 }
567 if (error == 0) {
1c79356b 568 error = error1;
0a7de745 569 }
91447636 570
0a7de745 571 return error;
1c79356b 572}
39037602
A
573
574#else /* CONFIG_COREDUMP */
575
576/* When core dumps aren't needed, no need to compile this file at all */
577
578#error assertion failed: this section is not compiled
579
580#endif /* CONFIG_COREDUMP */