Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 25 Oct 1995 15:02:53 -0400 (EDT)
From:      "Ron G. Minnich" <rminnich@Sarnoff.COM>
To:        Theo de Raadt <deraadt@theos.com>, hackers@freebsd.org
Subject:   Re: anatomy of rfork, part 2: fork code
Message-ID:  <Pine.SUN.3.91.951025145433.28796A-100000@terra>
In-Reply-To: <Pine.SUN.3.91.951025143802.28686A-100000@terra>

next in thread | previous in thread | raw e-mail | index | archive | help
This one is really easy. Basically you have to mod the fork code to take
an option that indicates whether you dup the open file table for the
process or simply bump the use count and use it for the child. The segment
inheritance management has been done at this point: it gets done in user
mode via the minherit() i showed in the previous note. I delete the middle
parts that don't change ... it's about 10 lines of difference from a
regular fork. 

Points to note: parameter from user mode, which if has bit 0x80 set, 
means 'dup the file table'. SO i set the dupfd variable at the beginning. 
At the end, code decides to either dupfd() or just bump counters. Note in 
include sys/vnode.h, and to make it work correctly, i have to under 
KERNEL before and redefine it after the include. Ah well ... i think this 
oughtta get fixed somehow. 

note the implication of the option: fork is a special case of rfork. 

/*
 * Copyright (c) 1982, 1986, 1989, 1991, 1993
 *	The Regents of the University of California.  All rights reserved.
 * (c) UNIX System Laboratories, Inc.
 * All or some portions of this file are derived from material licensed
 * to the University of California by American Telephone and Telegraph
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
 * the permission of UNIX System Laboratories, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)kern_fork.c	8.6 (Berkeley) 4/8/94
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/filedesc.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/file.h>
#include <sys/acct.h>
#include <sys/ktrace.h>

/* oh, yuck */
/* this is due to include of vnode_if.h, which is automatically
 * generated. ouch.
 */
#undef KERNEL 
#include <sys/vnode.h>
#define KERNEL
#define VREF(vp)        (vp)->v_usecount++      /* increase reference */

struct rfa { int opts; };
/* ARGSUSED */
rfork(p1, uap, retval)
      struct proc *p1;
      struct rfa  *uap;
      int retval[];
{     
	int dupfd = 0; /* added for rfork() */

	register struct proc *p2;
	register uid_t uid;
	struct proc *newproc;
	struct proc **hash;
	int count;
	static int nextpid, pidchecked = 0;

	if (uap->opts&0x80)
		dupfd = 1;


	/* DUPLICATE FORK CODE DELETED HERE ... */
	.
	.
	.
	/* END DELETED FORK CODE */
	/* bump references to the text vnode (for procfs) */
	p2->p_textvp = p1->p_textvp;
	if (p2->p_textvp)
		VREF(p2->p_textvp);

	/* BEGIN CHANGED CODE FOR RFORK FOR DUPFD () */
       if (dupfd)
               p2->p_fd = fdcopy(p1);
       else
               {
                       /* make this a function at some point */
                       /* danger!!! no locks!!! */
                       p2->p_fd = p1->p_fd;
                       p2->p_fd->fd_refcnt++;
               }
	/* END CHANGED CODE FOR RFORK() */
	/* MORE DELETED UNCHANGED CODE */
	/* END DELETED CODE */
	/*
	 * Return child pid to parent process,
	 * marking us as parent via retval[1].
	 */
	retval[0] = p2->p_pid;
	retval[1] = 0;
	return (0);
}





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.SUN.3.91.951025145433.28796A-100000>