]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/kern_core.c
xnu-517.9.5.tar.gz
[apple/xnu.git] / bsd / kern / kern_core.c
index 4ef5b24cec980eaa44b7020fe76b148c82008f73..fe156fcbff37b7bc492f83ee466ebf1702100b40 100644 (file)
@@ -1,24 +1,21 @@
 /*
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
- * Copyright (c) 1999-2003 Apple Computer, Inc.  All Rights Reserved.
+ * The contents of this file constitute Original Code as defined in and
+ * are subject to the Apple Public Source License Version 1.1 (the
+ * "License").  You may not use this file except in compliance with the
+ * License.  Please obtain a copy of the License at
+ * http://www.apple.com/publicsource and read it before using this file.
  * 
  * 
- * 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. 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
+ * This 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,
  * 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.
+ * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
+ * License for the specific language governing rights and limitations
+ * under the License.
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
  * 
  * @APPLE_LICENSE_HEADER_END@
  */
@@ -28,9 +25,6 @@
  *
  *     This file contains machine independent code for performing core dumps.
  *
  *
  *     This file contains machine independent code for performing core dumps.
  *
- * HISTORY
- * 16-Feb-91  Mike DeMoney (mike@next.com)
- *     Massaged into MI form from m68k/core.c.
  */
 
 #include <mach/vm_param.h>
  */
 
 #include <mach/vm_param.h>
@@ -98,6 +92,18 @@ typedef struct {
        int tstate_size;
 } tir_t;
 
        int tstate_size;
 } tir_t;
 
+/* XXX should be static */
+void collectth_state(thread_act_t th_act, tir_t *t);
+
+/* XXX not in a Mach header anywhere */
+kern_return_t thread_getstatus(register thread_act_t act, int flavor,
+       thread_state_t tstate, mach_msg_type_number_t *count);
+
+
+__private_extern__ do_coredump = 1;    /* default: dump cores */
+__private_extern__ sugid_coredump = 0; /* deafult: but not on SGUID binaries */
+
+void
 collectth_state(thread_act_t th_act, tir_t *t)
 {
        vm_offset_t     header;
 collectth_state(thread_act_t th_act, tir_t *t)
 {
        vm_offset_t     header;
@@ -126,20 +132,23 @@ collectth_state(thread_act_t th_act, tir_t *t)
                          flavors[i];
                        hoffset += sizeof(mythread_state_flavor_t);
                        thread_getstatus(th_act, flavors[i].flavor,
                          flavors[i];
                        hoffset += sizeof(mythread_state_flavor_t);
                        thread_getstatus(th_act, flavors[i].flavor,
-                                       (thread_state_t *)(header+hoffset),
+                                       (thread_state_t)(header+hoffset),
                                        &flavors[i].count);
                        hoffset += flavors[i].count*sizeof(int);
                }
 
                t->hoffset = hoffset;
 }
                                        &flavors[i].count);
                        hoffset += flavors[i].count*sizeof(int);
                }
 
                t->hoffset = hoffset;
 }
+
+extern boolean_t coredumpok(vm_map_t map, vm_offset_t va);  /* temp fix */
+extern task_t current_task(void);      /* XXX */
+
 /*
  * Create a core image on the file "core".
  */
 #define        MAX_TSTATE_FLAVORS      10
 int
 /*
  * Create a core image on the file "core".
  */
 #define        MAX_TSTATE_FLAVORS      10
 int
-coredump(p)
-       register struct proc *p;
+coredump(struct proc *p)
 {
        int error=0;
        register struct pcred *pcred = p->p_cred;
 {
        int error=0;
        register struct pcred *pcred = p->p_cred;
@@ -154,17 +163,16 @@ coredump(p)
        struct machine_slot     *ms;
        struct mach_header      *mh;
        struct segment_command  *sc;
        struct machine_slot     *ms;
        struct mach_header      *mh;
        struct segment_command  *sc;
-       struct thread_command   *tc;
        vm_size_t       size;
        vm_prot_t       prot;
        vm_prot_t       maxprot;
        vm_inherit_t    inherit;
        vm_size_t       size;
        vm_prot_t       prot;
        vm_prot_t       maxprot;
        vm_inherit_t    inherit;
-       vm_offset_t     offset;
        int             error1;
        task_t          task;
        char            core_name[MAXCOMLEN+6];
        int             error1;
        task_t          task;
        char            core_name[MAXCOMLEN+6];
+       char            *name;
        mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
        mythread_state_flavor_t flavors[MAX_TSTATE_FLAVORS];
-       vm_size_t       nflavors,mapsize;
+       vm_size_t       mapsize;
        int             i;
        int nesting_depth = 0;
        kern_return_t   kret;
        int             i;
        int nesting_depth = 0;
        kern_return_t   kret;
@@ -172,10 +180,14 @@ coredump(p)
        int vbrcount=0;
        tir_t tir1;
        struct vnode * vp;
        int vbrcount=0;
        tir_t tir1;
        struct vnode * vp;
-       extern boolean_t coredumpok(vm_map_t map, vm_offset_t va);  /* temp fix */
 
 
-       if (pcred->p_svuid != pcred->p_ruid || pcred->p_svgid != pcred->p_rgid)
+       if (do_coredump == 0 ||         /* Not dumping at all */
+           ( (sugid_coredump == 0) &&  /* Not dumping SUID/SGID binaries */
+             ( (pcred->p_svuid != pcred->p_ruid) ||
+               (pcred->p_svgid != pcred->p_rgid)))) {
+           
                return (EFAULT);
                return (EFAULT);
+       }
 
        task = current_task();
        map = current_map();
 
        task = current_task();
        map = current_map();
@@ -185,9 +197,17 @@ coredump(p)
                return (EFAULT);
        (void) task_suspend(task);
 
                return (EFAULT);
        (void) task_suspend(task);
 
+       /* create name according to sysctl'able format string */
+       name = proc_core_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
+
+       /* if name creation fails, fall back to historical behaviour... */
+       if (name == NULL) {
        sprintf(core_name, "/cores/core.%d", p->p_pid);
        sprintf(core_name, "/cores/core.%d", p->p_pid);
-       NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, core_name, p);
-       if(error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR ))
+               name = core_name;
+       }
+
+       NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
+       if((error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR )) != 0)
                return (error);
        vp = nd.ni_vp;
        
                return (error);
        vp = nd.ni_vp;
        
@@ -212,24 +232,9 @@ coredump(p)
 
        thread_count = get_task_numacts(task);
        segment_count = get_vmmap_entries(map); /* XXX */
 
        thread_count = get_task_numacts(task);
        segment_count = get_vmmap_entries(map); /* XXX */
-       /*
-        * nflavors here is really the number of ints in flavors
-        * to meet the thread_getstatus() calling convention
-        */
-#if 0
-       nflavors = sizeof(flavors)/sizeof(int);
-       if (thread_getstatus(current_thread(), THREAD_STATE_FLAVOR_LIST,
-                               (thread_state_t)(flavors),
-                                &nflavors) != KERN_SUCCESS)
-           panic("core flavor list");
-       /* now convert to number of flavors */
-       nflavors /= sizeof(mythread_state_flavor_t)/sizeof(int);
-#else
-       nflavors = mynum_flavors;
        bcopy(thread_flavor_array,flavors,sizeof(thread_flavor_array));
        bcopy(thread_flavor_array,flavors,sizeof(thread_flavor_array));
-#endif
        tstate_size = 0;
        tstate_size = 0;
-       for (i = 0; i < nflavors; i++)
+       for (i = 0; i < mynum_flavors; i++)
                tstate_size += sizeof(mythread_state_flavor_t) +
                  (flavors[i].count * sizeof(int));
 
                tstate_size += sizeof(mythread_state_flavor_t) +
                  (flavors[i].count * sizeof(int));
 
@@ -258,7 +263,8 @@ coredump(p)
        hoffset = sizeof(struct mach_header);   /* offset into header */
        foffset = round_page_32(header_size);   /* offset into file */
        vmoffset = VM_MIN_ADDRESS;              /* offset into VM */
        hoffset = sizeof(struct mach_header);   /* offset into header */
        foffset = round_page_32(header_size);   /* offset into file */
        vmoffset = VM_MIN_ADDRESS;              /* offset into VM */
-       /* We use to check for an error, here, now we try and get 
+       /*
+        * We use to check for an error, here, now we try and get 
         * as much as we can
         */
        while (segment_count > 0){
         * as much as we can
         */
        while (segment_count > 0){
@@ -315,7 +321,9 @@ coredump(p)
                 *      Note: if we can't read, then we end up with
                 *      a hole in the file.
                 */
                 *      Note: if we can't read, then we end up with
                 *      a hole in the file.
                 */
-               if ((maxprot & VM_PROT_READ) == VM_PROT_READ && vbr.user_tag != VM_MEMORY_IOKIT && coredumpok(map,vmoffset)) {
+               if ((maxprot & VM_PROT_READ) == VM_PROT_READ
+                       && vbr.user_tag != VM_MEMORY_IOKIT
+                       && coredumpok(map,vmoffset)) {
                        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)vmoffset, size, foffset,
                                UIO_USERSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
                }
                        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)vmoffset, size, foffset,
                                UIO_USERSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *) 0, p);
                }
@@ -326,44 +334,12 @@ coredump(p)
                segment_count--;
        }
 
                segment_count--;
        }
 
-#if 0 /* [ */
-       task_lock(task);
-       thread = (thread_t) queue_first(&task->thread_list);
-       while (thread_count > 0) {
-               /*
-                *      Fill in thread command structure.
-                */
-               tc = (struct thread_command *) (header + hoffset);
-               tc->cmd = LC_THREAD;
-               tc->cmdsize = sizeof(struct thread_command)
-                               + tstate_size;
-               hoffset += sizeof(struct thread_command);
-               /*
-                * Follow with a struct thread_state_flavor and
-                * the appropriate thread state struct for each
-                * thread state flavor.
-                */
-               for (i = 0; i < nflavors; i++) {
-                       *(mythread_state_flavor_t *)(header+hoffset) =
-                         flavors[i];
-                       hoffset += sizeof(mythread_state_flavor_t);
-                       thread_getstatus(thread, flavors[i].flavor,
-                                       (thread_state_t *)(header+hoffset),
-                                       &flavors[i].count);
-                       hoffset += flavors[i].count*sizeof(int);
-               }
-               thread = (thread_t) queue_next(&thread->thread_list);
-               thread_count--;
-       }
-       task_unlock(task);
-#else /* /* 0 ][ */
        tir1.header = header;
        tir1.hoffset = hoffset;
        tir1.flavors = flavors;
        tir1.tstate_size = tstate_size;
        task_act_iterate_wth_args(task, collectth_state,&tir1);
 
        tir1.header = header;
        tir1.hoffset = hoffset;
        tir1.flavors = flavors;
        tir1.tstate_size = tstate_size;
        task_act_iterate_wth_args(task, collectth_state,&tir1);
 
-#endif /* 0 ] */
        /*
         *      Write out the Mach header at the beginning of the
         *      file.
        /*
         *      Write out the Mach header at the beginning of the
         *      file.
@@ -376,4 +352,5 @@ out:
        error1 = vn_close(vp, FWRITE, cred, p);
        if (error == 0)
                error = error1;
        error1 = vn_close(vp, FWRITE, cred, p);
        if (error == 0)
                error = error1;
+       return (error);
 }
 }