]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
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. | |
8f6c56a5 | 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> | |
39 | ||
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 A |
61 | #include <vm/vm_protos.h> /* last */ |
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 | ||
39037602 A |
69 | #if CONFIG_CSR |
70 | #include <sys/codesign.h> | |
71 | #include <sys/csr.h> | |
72 | #endif | |
73 | ||
1c79356b A |
74 | typedef struct { |
75 | int flavor; /* the number for this flavor */ | |
2d21ac55 | 76 | mach_msg_type_number_t count; /* count of ints in this flavor */ |
1c79356b A |
77 | } mythread_state_flavor_t; |
78 | ||
6d2010ae | 79 | #if defined (__i386__) || defined (__x86_64__) |
1c79356b | 80 | mythread_state_flavor_t thread_flavor_array [] = { |
0c530ab8 A |
81 | {x86_THREAD_STATE, x86_THREAD_STATE_COUNT}, |
82 | {x86_FLOAT_STATE, x86_FLOAT_STATE_COUNT}, | |
83 | {x86_EXCEPTION_STATE, x86_EXCEPTION_STATE_COUNT}, | |
1c79356b | 84 | }; |
0c530ab8 | 85 | int mynum_flavors=3; |
1c79356b A |
86 | #else |
87 | #error architecture not supported | |
88 | #endif | |
89 | ||
90 | ||
91 | typedef struct { | |
92 | vm_offset_t header; | |
93 | int hoffset; | |
94 | mythread_state_flavor_t *flavors; | |
95 | int tstate_size; | |
2d21ac55 | 96 | int flavor_count; |
1c79356b A |
97 | } tir_t; |
98 | ||
39236c6e A |
99 | extern int freespace_mb(vnode_t vp); |
100 | ||
e5568f75 | 101 | /* XXX not in a Mach header anywhere */ |
39037602 | 102 | kern_return_t thread_getstatus(thread_t act, int flavor, |
e5568f75 | 103 | thread_state_t tstate, mach_msg_type_number_t *count); |
91447636 | 104 | void task_act_iterate_wth_args(task_t, void(*)(thread_t, void *), void *); |
e5568f75 | 105 | |
593a1d5f | 106 | #ifdef SECURE_KERNEL |
39236c6e | 107 | __XNU_PRIVATE_EXTERN int do_coredump = 0; /* default: don't dump cores */ |
593a1d5f | 108 | #else |
39236c6e | 109 | __XNU_PRIVATE_EXTERN int do_coredump = 1; /* default: dump cores */ |
593a1d5f | 110 | #endif |
39236c6e | 111 | __XNU_PRIVATE_EXTERN int sugid_coredump = 0; /* default: but not SGUID binaries */ |
e5568f75 | 112 | |
b0d623f7 A |
113 | |
114 | /* cpu_type returns only the most generic indication of the current CPU. */ | |
115 | /* in a core we want to know the kind of process. */ | |
116 | ||
117 | static cpu_type_t | |
118 | process_cpu_type(proc_t core_proc) | |
119 | { | |
120 | cpu_type_t what_we_think; | |
121 | #if defined (__i386__) || defined (__x86_64__) | |
122 | if (IS_64BIT_PROCESS(core_proc)) { | |
123 | what_we_think = CPU_TYPE_X86_64; | |
124 | } else { | |
125 | what_we_think = CPU_TYPE_I386; | |
126 | } | |
b0d623f7 A |
127 | #endif |
128 | return what_we_think; | |
129 | } | |
130 | ||
131 | static cpu_type_t | |
132 | process_cpu_subtype(proc_t core_proc) | |
133 | { | |
134 | cpu_type_t what_we_think; | |
135 | #if defined (__i386__) || defined (__x86_64__) | |
136 | if (IS_64BIT_PROCESS(core_proc)) { | |
137 | what_we_think = CPU_SUBTYPE_X86_64_ALL; | |
138 | } else { | |
139 | what_we_think = CPU_SUBTYPE_I386_ALL; | |
140 | } | |
b0d623f7 A |
141 | #endif |
142 | return what_we_think; | |
143 | } | |
144 | ||
39037602 | 145 | static void |
91447636 | 146 | collectth_state(thread_t th_act, void *tirp) |
1c79356b A |
147 | { |
148 | vm_offset_t header; | |
149 | int hoffset, i ; | |
150 | mythread_state_flavor_t *flavors; | |
151 | struct thread_command *tc; | |
91447636 A |
152 | tir_t *t = (tir_t *)tirp; |
153 | ||
1c79356b A |
154 | /* |
155 | * Fill in thread command structure. | |
156 | */ | |
157 | header = t->header; | |
158 | hoffset = t->hoffset; | |
159 | flavors = t->flavors; | |
160 | ||
161 | tc = (struct thread_command *) (header + hoffset); | |
162 | tc->cmd = LC_THREAD; | |
163 | tc->cmdsize = sizeof(struct thread_command) | |
164 | + t->tstate_size; | |
165 | hoffset += sizeof(struct thread_command); | |
166 | /* | |
167 | * Follow with a struct thread_state_flavor and | |
168 | * the appropriate thread state struct for each | |
169 | * thread state flavor. | |
170 | */ | |
2d21ac55 | 171 | for (i = 0; i < t->flavor_count; i++) { |
1c79356b A |
172 | *(mythread_state_flavor_t *)(header+hoffset) = |
173 | flavors[i]; | |
174 | hoffset += sizeof(mythread_state_flavor_t); | |
175 | thread_getstatus(th_act, flavors[i].flavor, | |
e5568f75 | 176 | (thread_state_t)(header+hoffset), |
1c79356b A |
177 | &flavors[i].count); |
178 | hoffset += flavors[i].count*sizeof(int); | |
179 | } | |
180 | ||
181 | t->hoffset = hoffset; | |
182 | } | |
e5568f75 | 183 | |
1c79356b | 184 | /* |
2d21ac55 A |
185 | * coredump |
186 | * | |
187 | * Description: Create a core image on the file "core" for the process | |
188 | * indicated | |
189 | * | |
190 | * Parameters: core_proc Process to dump core [*] | |
39236c6e A |
191 | * reserve_mb If non-zero, leave filesystem with |
192 | * at least this much free space. | |
3e170ce0 | 193 | * coredump_flags Extra options (ignore rlimit, run fsync) |
2d21ac55 A |
194 | * |
195 | * Returns: 0 Success | |
196 | * EFAULT Failed | |
197 | * | |
198 | * IMPORTANT: This function can only be called on the current process, due | |
199 | * to assumptions below; see variable declaration section for | |
200 | * details. | |
1c79356b A |
201 | */ |
202 | #define MAX_TSTATE_FLAVORS 10 | |
203 | int | |
3e170ce0 | 204 | coredump(proc_t core_proc, uint32_t reserve_mb, int coredump_flags) |
1c79356b | 205 | { |
2d21ac55 A |
206 | /* Begin assumptions that limit us to only the current process */ |
207 | vfs_context_t ctx = vfs_context_current(); | |
208 | vm_map_t map = current_map(); | |
209 | task_t task = current_task(); | |
210 | /* End assumptions */ | |
211 | kauth_cred_t cred = vfs_context_ucred(ctx); | |
212 | int error = 0; | |
91447636 | 213 | struct vnode_attr va; |
1c79356b A |
214 | int thread_count, segment_count; |
215 | int command_size, header_size, tstate_size; | |
91447636 A |
216 | int hoffset; |
217 | off_t foffset; | |
316670eb | 218 | mach_vm_offset_t vmoffset; |
1c79356b | 219 | vm_offset_t header; |
316670eb | 220 | mach_vm_size_t vmsize; |
1c79356b A |
221 | vm_prot_t prot; |
222 | vm_prot_t maxprot; | |
223 | vm_inherit_t inherit; | |
2d21ac55 A |
224 | int error1 = 0; |
225 | char stack_name[MAXCOMLEN+6]; | |
226 | char *alloced_name = NULL; | |
e5568f75 | 227 | char *name; |
1c79356b | 228 | mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS]; |
e5568f75 | 229 | vm_size_t mapsize; |
1c79356b | 230 | int i; |
2d21ac55 | 231 | uint32_t nesting_depth = 0; |
1c79356b A |
232 | kern_return_t kret; |
233 | struct vm_region_submap_info_64 vbr; | |
2d21ac55 | 234 | mach_msg_type_number_t vbrcount = 0; |
1c79356b A |
235 | tir_t tir1; |
236 | struct vnode * vp; | |
2d21ac55 A |
237 | struct mach_header *mh = NULL; /* protected by is_64 */ |
238 | struct mach_header_64 *mh64 = NULL; /* protected by is_64 */ | |
91447636 A |
239 | int is_64 = 0; |
240 | size_t mach_header_sz = sizeof(struct mach_header); | |
241 | size_t segment_command_sz = sizeof(struct segment_command); | |
fe8ab488 A |
242 | |
243 | if (current_proc() != core_proc) { | |
244 | panic("coredump() called against proc that is not current_proc: %p", core_proc); | |
245 | } | |
1c79356b | 246 | |
e5568f75 A |
247 | if (do_coredump == 0 || /* Not dumping at all */ |
248 | ( (sugid_coredump == 0) && /* Not dumping SUID/SGID binaries */ | |
6d2010ae A |
249 | ( (kauth_cred_getsvuid(cred) != kauth_cred_getruid(cred)) || |
250 | (kauth_cred_getsvgid(cred) != kauth_cred_getrgid(cred))))) { | |
b0d623f7 A |
251 | |
252 | #if CONFIG_AUDIT | |
253 | audit_proc_coredump(core_proc, NULL, EFAULT); | |
254 | #endif | |
1c79356b | 255 | return (EFAULT); |
e5568f75 | 256 | } |
1c79356b | 257 | |
39037602 A |
258 | #if CONFIG_CSR |
259 | /* If the process is restricted, CSR isn't configured to allow | |
260 | * restricted processes to be debugged, and CSR isn't configured in | |
261 | * AppleInternal mode, then don't dump core. */ | |
262 | if (cs_restricted(core_proc) && | |
263 | csr_check(CSR_ALLOW_TASK_FOR_PID) && | |
264 | csr_check(CSR_ALLOW_APPLE_INTERNAL)) { | |
265 | #if CONFIG_AUDIT | |
266 | audit_proc_coredump(core_proc, NULL, EFAULT); | |
267 | #endif | |
268 | return (EFAULT); | |
269 | } | |
270 | #endif | |
271 | ||
2d21ac55 | 272 | if (IS_64BIT_PROCESS(core_proc)) { |
91447636 A |
273 | is_64 = 1; |
274 | mach_header_sz = sizeof(struct mach_header_64); | |
275 | segment_command_sz = sizeof(struct segment_command_64); | |
276 | } | |
277 | ||
1c79356b A |
278 | mapsize = get_vmmap_size(map); |
279 | ||
3e170ce0 A |
280 | if (((coredump_flags & COREDUMP_IGNORE_ULIMIT) == 0) && |
281 | (mapsize >= core_proc->p_rlimit[RLIMIT_CORE].rlim_cur)) | |
1c79356b | 282 | return (EFAULT); |
3e170ce0 | 283 | |
39236c6e | 284 | (void) task_suspend_internal(task); |
1c79356b | 285 | |
2d21ac55 | 286 | MALLOC(alloced_name, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO); |
e5568f75 | 287 | |
2d21ac55 | 288 | /* create name according to sysctl'able format string */ |
e5568f75 | 289 | /* if name creation fails, fall back to historical behaviour... */ |
b0d623f7 A |
290 | if (alloced_name == NULL || |
291 | proc_core_name(core_proc->p_comm, kauth_cred_getuid(cred), | |
2d21ac55 A |
292 | core_proc->p_pid, alloced_name, MAXPATHLEN)) { |
293 | snprintf(stack_name, sizeof(stack_name), | |
294 | "/cores/core.%d", core_proc->p_pid); | |
295 | name = stack_name; | |
296 | } else | |
297 | name = alloced_name; | |
e5568f75 | 298 | |
2d21ac55 A |
299 | if ((error = vnode_open(name, (O_CREAT | FWRITE | O_NOFOLLOW), S_IRUSR, VNODE_LOOKUP_NOFOLLOW, &vp, ctx))) |
300 | goto out2; | |
91447636 A |
301 | |
302 | VATTR_INIT(&va); | |
303 | VATTR_WANTED(&va, va_nlink); | |
1c79356b A |
304 | /* Don't dump to non-regular files or files with links. */ |
305 | if (vp->v_type != VREG || | |
2d21ac55 | 306 | vnode_getattr(vp, &va, ctx) || va.va_nlink != 1) { |
1c79356b A |
307 | error = EFAULT; |
308 | goto out; | |
309 | } | |
310 | ||
91447636 A |
311 | VATTR_INIT(&va); /* better to do it here than waste more stack in vnode_setsize */ |
312 | VATTR_SET(&va, va_data_size, 0); | |
2d21ac55 A |
313 | vnode_setattr(vp, &va, ctx); |
314 | core_proc->p_acflag |= ACORE; | |
1c79356b | 315 | |
39236c6e A |
316 | if ((reserve_mb > 0) && |
317 | ((freespace_mb(vp) - (mapsize >> 20)) < reserve_mb)) { | |
318 | error = ENOSPC; | |
319 | goto out; | |
320 | } | |
321 | ||
1c79356b A |
322 | /* |
323 | * If the task is modified while dumping the file | |
324 | * (e.g., changes in threads or VM, the resulting | |
325 | * file will not necessarily be correct. | |
326 | */ | |
327 | ||
328 | thread_count = get_task_numacts(task); | |
329 | segment_count = get_vmmap_entries(map); /* XXX */ | |
6d2010ae A |
330 | tir1.flavor_count = sizeof(thread_flavor_array)/sizeof(mythread_state_flavor_t); |
331 | bcopy(thread_flavor_array, flavors,sizeof(thread_flavor_array)); | |
1c79356b | 332 | tstate_size = 0; |
2d21ac55 | 333 | for (i = 0; i < tir1.flavor_count; i++) |
1c79356b A |
334 | tstate_size += sizeof(mythread_state_flavor_t) + |
335 | (flavors[i].count * sizeof(int)); | |
91447636 | 336 | command_size = segment_count * segment_command_sz + |
1c79356b A |
337 | thread_count*sizeof(struct thread_command) + |
338 | tstate_size*thread_count; | |
339 | ||
91447636 | 340 | header_size = command_size + mach_header_sz; |
1c79356b | 341 | |
3e170ce0 | 342 | if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) { |
b0d623f7 A |
343 | error = ENOMEM; |
344 | goto out; | |
345 | } | |
1c79356b A |
346 | |
347 | /* | |
348 | * Set up Mach-O header. | |
349 | */ | |
91447636 A |
350 | if (is_64) { |
351 | mh64 = (struct mach_header_64 *)header; | |
352 | mh64->magic = MH_MAGIC_64; | |
b0d623f7 A |
353 | mh64->cputype = process_cpu_type(core_proc); |
354 | mh64->cpusubtype = process_cpu_subtype(core_proc); | |
91447636 A |
355 | mh64->filetype = MH_CORE; |
356 | mh64->ncmds = segment_count + thread_count; | |
357 | mh64->sizeofcmds = command_size; | |
358 | mh64->reserved = 0; /* 8 byte alignment */ | |
359 | } else { | |
360 | mh = (struct mach_header *)header; | |
361 | mh->magic = MH_MAGIC; | |
b0d623f7 A |
362 | mh->cputype = process_cpu_type(core_proc); |
363 | mh->cpusubtype = process_cpu_subtype(core_proc); | |
91447636 A |
364 | mh->filetype = MH_CORE; |
365 | mh->ncmds = segment_count + thread_count; | |
366 | mh->sizeofcmds = command_size; | |
367 | } | |
368 | ||
369 | hoffset = mach_header_sz; /* offset into header */ | |
370 | foffset = round_page(header_size); /* offset into file */ | |
371 | vmoffset = MACH_VM_MIN_ADDRESS; /* offset into VM */ | |
372 | ||
55e303ae A |
373 | /* |
374 | * We use to check for an error, here, now we try and get | |
1c79356b A |
375 | * as much as we can |
376 | */ | |
91447636 A |
377 | while (segment_count > 0) { |
378 | struct segment_command *sc; | |
379 | struct segment_command_64 *sc64; | |
380 | ||
1c79356b A |
381 | /* |
382 | * Get region information for next region. | |
383 | */ | |
384 | ||
385 | while (1) { | |
386 | vbrcount = VM_REGION_SUBMAP_INFO_COUNT_64; | |
91447636 A |
387 | if((kret = mach_vm_region_recurse(map, |
388 | &vmoffset, &vmsize, &nesting_depth, | |
389 | (vm_region_recurse_info_t)&vbr, | |
390 | &vbrcount)) != KERN_SUCCESS) { | |
391 | break; | |
392 | } | |
393 | /* | |
394 | * If we get a valid mapping back, but we're dumping | |
395 | * a 32 bit process, and it's over the allowable | |
396 | * address space of a 32 bit process, it's the same | |
397 | * as if mach_vm_region_recurse() failed. | |
398 | */ | |
399 | if (!(is_64) && | |
400 | (vmoffset + vmsize > VM_MAX_ADDRESS)) { | |
401 | kret = KERN_INVALID_ADDRESS; | |
1c79356b A |
402 | break; |
403 | } | |
404 | if(vbr.is_submap) { | |
405 | nesting_depth++; | |
406 | continue; | |
407 | } else { | |
408 | break; | |
409 | } | |
410 | } | |
411 | if(kret != KERN_SUCCESS) | |
412 | break; | |
413 | ||
414 | prot = vbr.protection; | |
415 | maxprot = vbr.max_protection; | |
416 | inherit = vbr.inheritance; | |
417 | /* | |
418 | * Fill in segment command structure. | |
419 | */ | |
91447636 A |
420 | if (is_64) { |
421 | sc64 = (struct segment_command_64 *)(header + hoffset); | |
422 | sc64->cmd = LC_SEGMENT_64; | |
423 | sc64->cmdsize = sizeof(struct segment_command_64); | |
424 | /* segment name is zeroed by kmem_alloc */ | |
425 | sc64->segname[0] = 0; | |
426 | sc64->vmaddr = vmoffset; | |
427 | sc64->vmsize = vmsize; | |
428 | sc64->fileoff = foffset; | |
429 | sc64->filesize = vmsize; | |
430 | sc64->maxprot = maxprot; | |
431 | sc64->initprot = prot; | |
432 | sc64->nsects = 0; | |
3e170ce0 | 433 | sc64->flags = 0; |
91447636 A |
434 | } else { |
435 | sc = (struct segment_command *) (header + hoffset); | |
436 | sc->cmd = LC_SEGMENT; | |
437 | sc->cmdsize = sizeof(struct segment_command); | |
438 | /* segment name is zeroed by kmem_alloc */ | |
439 | sc->segname[0] = 0; | |
b0d623f7 A |
440 | sc->vmaddr = CAST_DOWN_EXPLICIT(vm_offset_t,vmoffset); |
441 | sc->vmsize = CAST_DOWN_EXPLICIT(vm_size_t,vmsize); | |
442 | sc->fileoff = CAST_DOWN_EXPLICIT(uint32_t,foffset); /* will never truncate */ | |
443 | sc->filesize = CAST_DOWN_EXPLICIT(uint32_t,vmsize); /* will never truncate */ | |
91447636 A |
444 | sc->maxprot = maxprot; |
445 | sc->initprot = prot; | |
446 | sc->nsects = 0; | |
3e170ce0 | 447 | sc->flags = 0; |
91447636 | 448 | } |
1c79356b A |
449 | |
450 | /* | |
451 | * Write segment out. Try as hard as possible to | |
452 | * get read access to the data. | |
453 | */ | |
454 | if ((prot & VM_PROT_READ) == 0) { | |
91447636 A |
455 | mach_vm_protect(map, vmoffset, vmsize, FALSE, |
456 | prot|VM_PROT_READ); | |
1c79356b A |
457 | } |
458 | /* | |
459 | * Only actually perform write if we can read. | |
460 | * Note: if we can't read, then we end up with | |
461 | * a hole in the file. | |
462 | */ | |
55e303ae A |
463 | if ((maxprot & VM_PROT_READ) == VM_PROT_READ |
464 | && vbr.user_tag != VM_MEMORY_IOKIT | |
465 | && coredumpok(map,vmoffset)) { | |
b0d623f7 A |
466 | |
467 | error = vn_rdwr_64(UIO_WRITE, vp, vmoffset, vmsize, foffset, | |
2d21ac55 | 468 | (IS_64BIT_PROCESS(core_proc) ? UIO_USERSPACE64 : UIO_USERSPACE32), |
b0d623f7 A |
469 | IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int64_t *) 0, core_proc); |
470 | ||
1c79356b A |
471 | } |
472 | ||
91447636 A |
473 | hoffset += segment_command_sz; |
474 | foffset += vmsize; | |
475 | vmoffset += vmsize; | |
1c79356b A |
476 | segment_count--; |
477 | } | |
478 | ||
91447636 A |
479 | /* |
480 | * If there are remaining segments which have not been written | |
481 | * out because break in the loop above, then they were not counted | |
482 | * because they exceed the real address space of the executable | |
483 | * type: remove them from the header's count. This is OK, since | |
484 | * we are allowed to have a sparse area following the segments. | |
485 | */ | |
486 | if (is_64) { | |
487 | mh64->ncmds -= segment_count; | |
2d21ac55 | 488 | mh64->sizeofcmds -= segment_count * segment_command_sz; |
91447636 A |
489 | } else { |
490 | mh->ncmds -= segment_count; | |
2d21ac55 | 491 | mh->sizeofcmds -= segment_count * segment_command_sz; |
91447636 A |
492 | } |
493 | ||
1c79356b A |
494 | tir1.header = header; |
495 | tir1.hoffset = hoffset; | |
496 | tir1.flavors = flavors; | |
497 | tir1.tstate_size = tstate_size; | |
498 | task_act_iterate_wth_args(task, collectth_state,&tir1); | |
499 | ||
1c79356b A |
500 | /* |
501 | * Write out the Mach header at the beginning of the | |
91447636 | 502 | * file. OK to use a 32 bit write for this. |
1c79356b A |
503 | */ |
504 | error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0, | |
b0d623f7 | 505 | UIO_SYSSPACE, IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc); |
1c79356b | 506 | kmem_free(kernel_map, header, header_size); |
3e170ce0 A |
507 | |
508 | if ((coredump_flags & COREDUMP_FULLFSYNC) && error == 0) | |
509 | error = VNOP_IOCTL(vp, F_FULLFSYNC, (caddr_t)NULL, 0, ctx); | |
1c79356b | 510 | out: |
2d21ac55 A |
511 | error1 = vnode_close(vp, FWRITE, ctx); |
512 | out2: | |
b0d623f7 A |
513 | #if CONFIG_AUDIT |
514 | audit_proc_coredump(core_proc, name, error); | |
515 | #endif | |
2d21ac55 A |
516 | if (alloced_name != NULL) |
517 | FREE(alloced_name, M_TEMP); | |
1c79356b A |
518 | if (error == 0) |
519 | error = error1; | |
91447636 | 520 | |
55e303ae | 521 | return (error); |
1c79356b | 522 | } |
39037602 A |
523 | |
524 | #else /* CONFIG_COREDUMP */ | |
525 | ||
526 | /* When core dumps aren't needed, no need to compile this file at all */ | |
527 | ||
528 | #error assertion failed: this section is not compiled | |
529 | ||
530 | #endif /* CONFIG_COREDUMP */ |