kern_fork.c (112198) | kern_fork.c (112564) |
---|---|
1/* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 22 unchanged lines hidden (view full) --- 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 | 1/* 2 * Copyright (c) 1982, 1986, 1989, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 22 unchanged lines hidden (view full) --- 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)kern_fork.c 8.6 (Berkeley) 4/8/94 |
39 * $FreeBSD: head/sys/kern/kern_fork.c 112198 2003-03-13 18:24:22Z jhb $ | 39 * $FreeBSD: head/sys/kern/kern_fork.c 112564 2003-03-24 21:15:35Z jhb $ |
40 */ 41 42#include "opt_ktrace.h" 43#include "opt_mac.h" 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/sysproto.h> | 40 */ 41 42#include "opt_ktrace.h" 43#include "opt_mac.h" 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/sysproto.h> |
48#include <sys/eventhandler.h> |
|
48#include <sys/filedesc.h> 49#include <sys/kernel.h> 50#include <sys/sysctl.h> 51#include <sys/lock.h> 52#include <sys/malloc.h> 53#include <sys/mutex.h> 54#include <sys/proc.h> 55#include <sys/pioctl.h> --- 15 unchanged lines hidden (view full) --- 71#include <vm/vm_map.h> 72#include <vm/vm_extern.h> 73#include <vm/uma.h> 74 75#include <sys/vmmeter.h> 76#include <sys/user.h> 77#include <machine/critical.h> 78 | 49#include <sys/filedesc.h> 50#include <sys/kernel.h> 51#include <sys/sysctl.h> 52#include <sys/lock.h> 53#include <sys/malloc.h> 54#include <sys/mutex.h> 55#include <sys/proc.h> 56#include <sys/pioctl.h> --- 15 unchanged lines hidden (view full) --- 72#include <vm/vm_map.h> 73#include <vm/vm_extern.h> 74#include <vm/uma.h> 75 76#include <sys/vmmeter.h> 77#include <sys/user.h> 78#include <machine/critical.h> 79 |
79static MALLOC_DEFINE(M_ATFORK, "atfork", "atfork callback"); 80 81/* 82 * These are the stuctures used to create a callout list for things to do 83 * when forking a process 84 */ 85struct forklist { 86 forklist_fn function; 87 TAILQ_ENTRY(forklist) next; 88}; 89 90static struct sx fork_list_lock; 91 92TAILQ_HEAD(forklist_head, forklist); 93static struct forklist_head fork_list = TAILQ_HEAD_INITIALIZER(fork_list); 94 | |
95#ifndef _SYS_SYSPROTO_H_ 96struct fork_args { 97 int dummy; 98}; 99#endif 100 101int forksleep; /* Place for fork1() to sleep on. */ 102 | 80#ifndef _SYS_SYSPROTO_H_ 81struct fork_args { 82 int dummy; 83}; 84#endif 85 86int forksleep; /* Place for fork1() to sleep on. */ 87 |
103static void 104init_fork_list(void *data __unused) 105{ 106 107 sx_init(&fork_list_lock, "fork list"); 108} 109SYSINIT(fork_list, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_fork_list, NULL); 110 | |
111/* 112 * MPSAFE 113 */ 114/* ARGSUSED */ 115int 116fork(td, uap) 117 struct thread *td; 118 struct fork_args *uap; --- 113 unchanged lines hidden (view full) --- 232 struct proc **procp; /* child proc */ 233{ 234 struct proc *p2, *pptr; 235 uid_t uid; 236 struct proc *newproc; 237 int trypid; 238 int ok; 239 static int pidchecked = 0; | 88/* 89 * MPSAFE 90 */ 91/* ARGSUSED */ 92int 93fork(td, uap) 94 struct thread *td; 95 struct fork_args *uap; --- 113 unchanged lines hidden (view full) --- 209 struct proc **procp; /* child proc */ 210{ 211 struct proc *p2, *pptr; 212 uid_t uid; 213 struct proc *newproc; 214 int trypid; 215 int ok; 216 static int pidchecked = 0; |
240 struct forklist *ep; | |
241 struct filedesc *fd; 242 struct proc *p1 = td->td_proc; 243 struct thread *td2; 244 struct kse *ke2; 245 struct ksegrp *kg2; 246 struct sigacts *newsigacts; 247 struct procsig *newprocsig; 248 int error; --- 449 unchanged lines hidden (view full) --- 698 p2->p_vmspace->vm_ssize; 699 } 700 701 /* 702 * Both processes are set up, now check if any loadable modules want 703 * to adjust anything. 704 * What if they have an error? XXX 705 */ | 217 struct filedesc *fd; 218 struct proc *p1 = td->td_proc; 219 struct thread *td2; 220 struct kse *ke2; 221 struct ksegrp *kg2; 222 struct sigacts *newsigacts; 223 struct procsig *newprocsig; 224 int error; --- 449 unchanged lines hidden (view full) --- 674 p2->p_vmspace->vm_ssize; 675 } 676 677 /* 678 * Both processes are set up, now check if any loadable modules want 679 * to adjust anything. 680 * What if they have an error? XXX 681 */ |
706 sx_slock(&fork_list_lock); 707 TAILQ_FOREACH(ep, &fork_list, next) { 708 (*ep->function)(p1, p2, flags); 709 } 710 sx_sunlock(&fork_list_lock); | 682 EVENTHANDLER_INVOKE(process_fork, p1, p2, flags); |
711 712 /* 713 * If RFSTOPPED not requested, make child runnable and add to 714 * run queue. 715 */ 716 microtime(&(p2->p_stats->p_start)); 717 p2->p_acflag = AFORK; 718 if ((flags & RFSTOPPED) == 0) { --- 48 unchanged lines hidden (view full) --- 767 thread_single_end(); 768 PROC_UNLOCK(p1); 769 } 770 tsleep(&forksleep, PUSER, "fork", hz / 2); 771 return (error); 772} 773 774/* | 683 684 /* 685 * If RFSTOPPED not requested, make child runnable and add to 686 * run queue. 687 */ 688 microtime(&(p2->p_stats->p_start)); 689 p2->p_acflag = AFORK; 690 if ((flags & RFSTOPPED) == 0) { --- 48 unchanged lines hidden (view full) --- 739 thread_single_end(); 740 PROC_UNLOCK(p1); 741 } 742 tsleep(&forksleep, PUSER, "fork", hz / 2); 743 return (error); 744} 745 746/* |
775 * The next two functionms are general routines to handle adding/deleting 776 * items on the fork callout list. 777 * 778 * at_fork(): 779 * Take the arguments given and put them onto the fork callout list, 780 * However first make sure that it's not already there. 781 * Returns 0 on success or a standard error number. 782 */ 783 784int 785at_fork(function) 786 forklist_fn function; 787{ 788 struct forklist *ep; 789 790#ifdef INVARIANTS 791 /* let the programmer know if he's been stupid */ 792 if (rm_at_fork(function)) 793 printf("WARNING: fork callout entry (%p) already present\n", 794 function); 795#endif 796 ep = malloc(sizeof(*ep), M_ATFORK, M_NOWAIT); 797 if (ep == NULL) 798 return (ENOMEM); 799 ep->function = function; 800 sx_xlock(&fork_list_lock); 801 TAILQ_INSERT_TAIL(&fork_list, ep, next); 802 sx_xunlock(&fork_list_lock); 803 return (0); 804} 805 806/* 807 * Scan the exit callout list for the given item and remove it.. 808 * Returns the number of items removed (0 or 1) 809 */ 810 811int 812rm_at_fork(function) 813 forklist_fn function; 814{ 815 struct forklist *ep; 816 817 sx_xlock(&fork_list_lock); 818 TAILQ_FOREACH(ep, &fork_list, next) { 819 if (ep->function == function) { 820 TAILQ_REMOVE(&fork_list, ep, next); 821 sx_xunlock(&fork_list_lock); 822 free(ep, M_ATFORK); 823 return(1); 824 } 825 } 826 sx_xunlock(&fork_list_lock); 827 return (0); 828} 829 830/* | |
831 * Handle the return of a child process from fork1(). This function 832 * is called from the MD fork_trampoline() entry point. 833 */ 834void 835fork_exit(callout, arg, frame) 836 void (*callout)(void *, struct trapframe *); 837 void *arg; 838 struct trapframe *frame; --- 76 unchanged lines hidden --- | 747 * Handle the return of a child process from fork1(). This function 748 * is called from the MD fork_trampoline() entry point. 749 */ 750void 751fork_exit(callout, arg, frame) 752 void (*callout)(void *, struct trapframe *); 753 void *arg; 754 struct trapframe *frame; --- 76 unchanged lines hidden --- |