Annotation of prex-old/usr/server/proc/fork.c, Revision 1.1
1.1 ! nbrk 1: /*
! 2: * Copyright (c) 2005-2006, Kohsuke Ohtani
! 3: * All rights reserved.
! 4: *
! 5: * Redistribution and use in source and binary forms, with or without
! 6: * modification, are permitted provided that the following conditions
! 7: * are met:
! 8: * 1. Redistributions of source code must retain the above copyright
! 9: * notice, this list of conditions and the following disclaimer.
! 10: * 2. Redistributions in binary form must reproduce the above copyright
! 11: * notice, this list of conditions and the following disclaimer in the
! 12: * documentation and/or other materials provided with the distribution.
! 13: * 3. Neither the name of the author nor the names of any co-contributors
! 14: * may be used to endorse or promote products derived from this software
! 15: * without specific prior written permission.
! 16: *
! 17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
! 18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
! 21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 27: * SUCH DAMAGE.
! 28: */
! 29:
! 30: /*
! 31: * fork.c - fork() support
! 32: */
! 33:
! 34: #include <prex/prex.h>
! 35: #include <server/proc.h>
! 36: #include <sys/list.h>
! 37:
! 38: #include <unistd.h>
! 39: #include <errno.h>
! 40: #include <stdlib.h>
! 41: #include <string.h>
! 42:
! 43: #include "proc.h"
! 44:
! 45: static int vfork_start(struct proc *proc);
! 46:
! 47: /*
! 48: * fork() support.
! 49: *
! 50: * It creates new process data and update all process relations.
! 51: * The task creation and the thread creation are done by the fork()
! 52: * library stub.
! 53: */
! 54: int
! 55: proc_fork(struct msg *msg)
! 56: {
! 57: struct proc *p;
! 58: struct pgrp *pgrp;
! 59: task_t child;
! 60: pid_t pid;
! 61: int vfork;
! 62:
! 63: if (curproc == NULL)
! 64: return EINVAL;
! 65:
! 66: child = (task_t)msg->data[0];
! 67: vfork = msg->data[1];
! 68:
! 69: dprintf("fork: parent=%x child=%x vfork=%d\n",
! 70: msg->hdr.task, child, vfork);
! 71:
! 72: if (task_to_proc(child) != NULL)
! 73: return EINVAL; /* Process already exists */
! 74:
! 75: if ((pid = pid_assign()) == 0)
! 76: return EAGAIN; /* Too many processes */
! 77:
! 78: if ((p = malloc(sizeof(struct proc))) == NULL)
! 79: return ENOMEM;
! 80:
! 81: p->parent = curproc;
! 82: p->pgrp = curproc->pgrp;
! 83: p->stat = SRUN;
! 84: p->exit_code = 0;
! 85: p->pid = pid;
! 86: p->task = child;
! 87: list_init(&p->children);
! 88: proc_add(p);
! 89:
! 90: list_insert(&curproc->children, &p->sibling);
! 91:
! 92: pgrp = p->pgrp;
! 93: list_insert(&pgrp->members, &p->pgrp_link);
! 94:
! 95: list_insert(&allproc, &p->link);
! 96:
! 97: if (vfork)
! 98: vfork_start(curproc);
! 99:
! 100: dprintf("fork: new pid=%d\n", p->pid);
! 101: msg->data[0] = (int)p->pid;
! 102: return 0;
! 103: }
! 104:
! 105: /*
! 106: * Clean up all resource created by fork().
! 107: */
! 108: void
! 109: proc_cleanup(struct proc *proc)
! 110: {
! 111: struct proc *pp;
! 112:
! 113: pp = proc->parent;
! 114: list_remove(&proc->sibling);
! 115: list_remove(&proc->pgrp_link);
! 116: proc_remove(proc);
! 117: list_remove(&proc->link);
! 118: free(proc);
! 119: }
! 120:
! 121: static int
! 122: vfork_start(struct proc *proc)
! 123: {
! 124: void *stack;
! 125:
! 126: /*
! 127: * Save parent's stack
! 128: */
! 129: if (vm_allocate(proc->task, &stack, USTACK_SIZE, 1) != 0)
! 130: return ENOMEM;
! 131:
! 132: memcpy(stack, proc->stack_base, USTACK_SIZE);
! 133: proc->stack_saved = stack;
! 134:
! 135: proc->wait_vfork = 1;
! 136: dprintf("vfork_start: saved=%x org=%x\n", stack, proc->stack_base);
! 137:
! 138: return 0;
! 139: }
! 140:
! 141: void
! 142: vfork_end(struct proc *proc)
! 143: {
! 144:
! 145: dprintf("vfork_end: org=%x saved=%x\n", proc->stack_base,
! 146: proc->stack_saved);
! 147: /*
! 148: * Restore parent's stack
! 149: */
! 150: memcpy(proc->stack_base, proc->stack_saved, USTACK_SIZE);
! 151: vm_free(proc->task, proc->stack_saved);
! 152:
! 153: /*
! 154: * Resume parent
! 155: */
! 156: proc->wait_vfork = 0;
! 157: task_resume(proc->task);
! 158: }
CVSweb