]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_core.c
xnu-3248.50.21.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
index 1c3093e4df072a51e3f70272ee8c7861fb0c2701..9477378efef55ebf546573a516b513eafc58661d 100644 (file)
@@ -93,6 +93,8 @@ typedef struct {
 /* XXX should be static */
 void collectth_state(thread_t th_act, void *tirp);
 
 /* XXX should be static */
 void collectth_state(thread_t th_act, void *tirp);
 
+extern int freespace_mb(vnode_t vp);
+
 /* XXX not in a Mach header anywhere */
 kern_return_t thread_getstatus(register thread_t act, int flavor,
        thread_state_t tstate, mach_msg_type_number_t *count);
 /* XXX not in a Mach header anywhere */
 kern_return_t thread_getstatus(register thread_t act, int flavor,
        thread_state_t tstate, mach_msg_type_number_t *count);
@@ -102,11 +104,11 @@ static cpu_type_t process_cpu_type(proc_t proc);
 static cpu_type_t process_cpu_subtype(proc_t proc);
 
 #ifdef SECURE_KERNEL
 static cpu_type_t process_cpu_subtype(proc_t proc);
 
 #ifdef SECURE_KERNEL
-__private_extern__ int do_coredump = 0;        /* default: don't dump cores */
+__XNU_PRIVATE_EXTERN int do_coredump = 0;      /* default: don't dump cores */
 #else
 #else
-__private_extern__ int do_coredump = 1;        /* default: dump cores */
+__XNU_PRIVATE_EXTERN int do_coredump = 1;      /* default: dump cores */
 #endif
 #endif
-__private_extern__ int sugid_coredump = 0; /* default: but not SGUID binaries */
+__XNU_PRIVATE_EXTERN int sugid_coredump = 0; /* default: but not SGUID binaries */
 
 
 /* cpu_type returns only the most generic indication of the current CPU. */
 
 
 /* cpu_type returns only the most generic indication of the current CPU. */
@@ -187,6 +189,9 @@ collectth_state(thread_t th_act, void *tirp)
  *             indicated
  *
  * Parameters: core_proc                       Process to dump core [*]
  *             indicated
  *
  * Parameters: core_proc                       Process to dump core [*]
+ *                             reserve_mb                      If non-zero, leave filesystem with
+ *                                                                     at least this much free space.
+ *                             coredump_flags  Extra options (ignore rlimit, run fsync)
  *
  * Returns:    0                               Success
  *             EFAULT                          Failed
  *
  * Returns:    0                               Success
  *             EFAULT                          Failed
@@ -197,7 +202,7 @@ collectth_state(thread_t th_act, void *tirp)
  */
 #define        MAX_TSTATE_FLAVORS      10
 int
  */
 #define        MAX_TSTATE_FLAVORS      10
 int
-coredump(proc_t core_proc)
+coredump(proc_t core_proc, uint32_t reserve_mb, int coredump_flags)
 {
 /* Begin assumptions that limit us to only the current process */
        vfs_context_t ctx = vfs_context_current();
 {
 /* Begin assumptions that limit us to only the current process */
        vfs_context_t ctx = vfs_context_current();
@@ -235,6 +240,10 @@ coredump(proc_t core_proc)
        int             is_64 = 0;
        size_t          mach_header_sz = sizeof(struct mach_header);
        size_t          segment_command_sz = sizeof(struct segment_command);
        int             is_64 = 0;
        size_t          mach_header_sz = sizeof(struct mach_header);
        size_t          segment_command_sz = sizeof(struct segment_command);
+       
+       if (current_proc() != core_proc) {
+               panic("coredump() called against proc that is not current_proc: %p", core_proc);
+       }
 
        if (do_coredump == 0 ||         /* Not dumping at all */
            ( (sugid_coredump == 0) &&  /* Not dumping SUID/SGID binaries */
 
        if (do_coredump == 0 ||         /* Not dumping at all */
            ( (sugid_coredump == 0) &&  /* Not dumping SUID/SGID binaries */
@@ -255,9 +264,11 @@ coredump(proc_t core_proc)
 
        mapsize = get_vmmap_size(map);
 
 
        mapsize = get_vmmap_size(map);
 
-       if (mapsize >=  core_proc->p_rlimit[RLIMIT_CORE].rlim_cur)
+       if (((coredump_flags & COREDUMP_IGNORE_ULIMIT) == 0) &&
+           (mapsize >=  core_proc->p_rlimit[RLIMIT_CORE].rlim_cur))
                return (EFAULT);
                return (EFAULT);
-       (void) task_suspend(task);
+
+       (void) task_suspend_internal(task);
 
        MALLOC(alloced_name, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
 
 
        MALLOC(alloced_name, char *, MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
 
@@ -289,6 +300,12 @@ coredump(proc_t core_proc)
        vnode_setattr(vp, &va, ctx);
        core_proc->p_acflag |= ACORE;
 
        vnode_setattr(vp, &va, ctx);
        core_proc->p_acflag |= ACORE;
 
+       if ((reserve_mb > 0) &&
+           ((freespace_mb(vp) - (mapsize >> 20)) < reserve_mb)) {
+               error = ENOSPC;
+               goto out;
+       }
+
        /*
         *      If the task is modified while dumping the file
         *      (e.g., changes in threads or VM, the resulting
        /*
         *      If the task is modified while dumping the file
         *      (e.g., changes in threads or VM, the resulting
@@ -309,7 +326,7 @@ coredump(proc_t core_proc)
 
        header_size = command_size + mach_header_sz;
 
 
        header_size = command_size + mach_header_sz;
 
-       if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size) != KERN_SUCCESS) {
+       if (kmem_alloc(kernel_map, &header, (vm_size_t)header_size, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) {
                error = ENOMEM;
                goto out;
        }
                error = ENOMEM;
                goto out;
        }
@@ -400,6 +417,7 @@ coredump(proc_t core_proc)
                        sc64->maxprot = maxprot;
                        sc64->initprot = prot;
                        sc64->nsects = 0;
                        sc64->maxprot = maxprot;
                        sc64->initprot = prot;
                        sc64->nsects = 0;
+                       sc64->flags = 0;
                } else  {
                        sc = (struct segment_command *) (header + hoffset);
                        sc->cmd = LC_SEGMENT;
                } else  {
                        sc = (struct segment_command *) (header + hoffset);
                        sc->cmd = LC_SEGMENT;
@@ -413,6 +431,7 @@ coredump(proc_t core_proc)
                        sc->maxprot = maxprot;
                        sc->initprot = prot;
                        sc->nsects = 0;
                        sc->maxprot = maxprot;
                        sc->initprot = prot;
                        sc->nsects = 0;
+                       sc->flags = 0;
                }
 
                /*
                }
 
                /*
@@ -472,6 +491,9 @@ coredump(proc_t core_proc)
        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
                        UIO_SYSSPACE, IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
        kmem_free(kernel_map, header, header_size);
        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)header, header_size, (off_t)0,
                        UIO_SYSSPACE, IO_NOCACHE|IO_NODELOCKED|IO_UNIT, cred, (int *) 0, core_proc);
        kmem_free(kernel_map, header, header_size);
+
+       if ((coredump_flags & COREDUMP_FULLFSYNC) && error == 0)
+               error = VNOP_IOCTL(vp, F_FULLFSYNC, (caddr_t)NULL, 0, ctx);
 out:
        error1 = vnode_close(vp, FWRITE, ctx);
 out2:
 out:
        error1 = vnode_close(vp, FWRITE, ctx);
 out2: