[BACK]Return to fork.c CVS log [TXT][DIR] Up to [local] / prex / usr / server / proc

Annotation of prex/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 *);
        !            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
        !            52:  * fork() 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_flag;
        !            62:
        !            63:        if (curproc == NULL)
        !            64:                return EINVAL;
        !            65:
        !            66:        child = (task_t)msg->data[0];
        !            67:        vfork_flag = msg->data[1];
        !            68:
        !            69:        DPRINTF(("fork: parent=%x child=%x vfork=%d\n", msg->hdr.task,
        !            70:                 child, vfork_flag));
        !            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->p_parent = curproc;
        !            82:        p->p_pgrp = curproc->p_pgrp;
        !            83:        p->p_stat = SRUN;
        !            84:        p->p_exitcode = 0;
        !            85:        p->p_pid = pid;
        !            86:        p->p_task = child;
        !            87:        list_init(&p->p_children);
        !            88:        proc_add(p);
        !            89:        list_insert(&curproc->p_children, &p->p_sibling);
        !            90:        pgrp = p->p_pgrp;
        !            91:        list_insert(&pgrp->pg_members, &p->p_pgrp_link);
        !            92:        list_insert(&allproc, &p->p_link);
        !            93:
        !            94:        if (vfork_flag)
        !            95:                vfork_start(curproc);
        !            96:
        !            97:        DPRINTF(("fork: new pid=%d\n", p->p_pid));
        !            98:        msg->data[0] = (int)p->p_pid;
        !            99:        return 0;
        !           100: }
        !           101:
        !           102: /*
        !           103:  * Clean up all resource created by fork().
        !           104:  */
        !           105: void
        !           106: proc_cleanup(struct proc *p)
        !           107: {
        !           108:        struct proc *pp;
        !           109:
        !           110:        pp = p->p_parent;
        !           111:        list_remove(&p->p_sibling);
        !           112:        list_remove(&p->p_pgrp_link);
        !           113:        proc_remove(p);
        !           114:        list_remove(&p->p_link);
        !           115:        free(p);
        !           116: }
        !           117:
        !           118: static int
        !           119: vfork_start(struct proc *p)
        !           120: {
        !           121:        void *stack;
        !           122:
        !           123:        /*
        !           124:         * Save parent's stack
        !           125:         */
        !           126:        if (vm_allocate(p->p_task, &stack, USTACK_SIZE, 1) != 0)
        !           127:                return ENOMEM;
        !           128:
        !           129:        memcpy(stack, p->p_stackbase, USTACK_SIZE);
        !           130:        p->p_stacksaved = stack;
        !           131:
        !           132:        p->p_vforked = 1;
        !           133:        DPRINTF(("vfork_start: saved=%x org=%x\n", stack, p->p_stackbase));
        !           134:        return 0;
        !           135: }
        !           136:
        !           137: void
        !           138: vfork_end(struct proc *p)
        !           139: {
        !           140:
        !           141:        DPRINTF(("vfork_end: org=%x saved=%x\n", p->p_stackbase,
        !           142:                 p->p_stacksaved));
        !           143:        /*
        !           144:         * Restore parent's stack
        !           145:         */
        !           146:        memcpy(p->p_stackbase, p->p_stacksaved, USTACK_SIZE);
        !           147:        vm_free(p->p_task, p->p_stacksaved);
        !           148:
        !           149:        /*
        !           150:         * Resume parent
        !           151:         */
        !           152:        p->p_vforked = 0;
        !           153:        task_resume(p->p_task);
        !           154: }

CVSweb