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

Annotation of prex-old/usr/server/proc/exit.c, Revision 1.1.1.1.2.1

1.1       nbrk        1: /*
1.1.1.1.2.1! nbrk        2:  * Copyright (c) 2005-2008, Kohsuke Ohtani
1.1       nbrk        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:  * exit.c - process exit and wait
                     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 <signal.h>
                     41:
                     42: #include "proc.h"
                     43:
                     44: /*
                     45:  * Exit process.
                     46:  *
1.1.1.1.2.1! nbrk       47:  * process_exit() sets the process state to zombie state, and it
        !            48:  * saves the exit code for waiting process.
1.1       nbrk       49:  */
                     50: int
                     51: proc_exit(struct msg *msg)
                     52: {
1.1.1.1.2.1! nbrk       53:        int exitcode;
1.1       nbrk       54:        struct proc *child, *parent;
                     55:        list_t head, n;
                     56:
                     57:        if (curproc == NULL)
                     58:                return EINVAL;
                     59:
1.1.1.1.2.1! nbrk       60:        exitcode = msg->data[0];
        !            61:        DPRINTF(("exit pid=%d task=%x code=%x\n", curproc->p_pid,
        !            62:                 msg->hdr.task, exitcode));
1.1       nbrk       63:
1.1.1.1.2.1! nbrk       64:        if (curproc->p_stat == SZOMB)
1.1       nbrk       65:                return EBUSY;
                     66:
1.1.1.1.2.1! nbrk       67:        curproc->p_stat = SZOMB;
        !            68:        curproc->p_exitcode = exitcode;
1.1       nbrk       69:
                     70:        /*
                     71:         * Set the parent pid of all child processes to 1 (init).
                     72:         */
1.1.1.1.2.1! nbrk       73:        head = &curproc->p_children;
        !            74:        n = list_first(head);
        !            75:        while (n != head) {
        !            76:                child = list_entry(n, struct proc, p_sibling);
        !            77:                n = list_next(n);
        !            78:
        !            79:                child->p_parent = &initproc;
        !            80:                list_remove(&child->p_sibling);
        !            81:                list_insert(&initproc.p_children, &child->p_sibling);
1.1       nbrk       82:        }
1.1.1.1.2.1! nbrk       83:
1.1       nbrk       84:        /*
                     85:         * Resume parent process which is wating in vfork.
                     86:         */
1.1.1.1.2.1! nbrk       87:        parent = curproc->p_parent;
        !            88:        if (parent != NULL && parent->p_vforked) {
1.1       nbrk       89:                vfork_end(parent);
                     90:
                     91:                /*
                     92:                 * The child task loses its stack data.
                     93:                 * So, it can not run anymore.
                     94:                 */
1.1.1.1.2.1! nbrk       95:                task_terminate(curproc->p_task);
1.1       nbrk       96:        }
                     97:
                     98:        /* Send a signal to the parent process. */
1.1.1.1.2.1! nbrk       99:        exception_raise(curproc->p_parent->p_task, SIGCHLD);
1.1       nbrk      100:        return 0;
                    101: }
                    102:
                    103: /*
                    104:  * Stop process.
                    105:  *
                    106:  * This is similar with exit(), but it does not update the parent
                    107:  * pid of any child processes.
                    108:  */
                    109: int
                    110: proc_stop(struct msg *msg)
                    111: {
                    112:        int code;
                    113:
                    114:        if (curproc == NULL)
                    115:                return EINVAL;
                    116:
                    117:        code = msg->data[0];
1.1.1.1.2.1! nbrk      118:        DPRINTF(("stop task=%x code=%x\n", msg->hdr.task, code));
1.1       nbrk      119:
1.1.1.1.2.1! nbrk      120:        if (curproc->p_stat == SZOMB)
1.1       nbrk      121:                return EBUSY;
                    122:
1.1.1.1.2.1! nbrk      123:        curproc->p_stat = SSTOP;
        !           124:        curproc->p_exitcode = code;
1.1       nbrk      125:
                    126:        /* Send a signal to the parent process. */
1.1.1.1.2.1! nbrk      127:        exception_raise(curproc->p_parent->p_task, SIGCHLD);
1.1       nbrk      128:        return 0;
                    129: }
                    130:
                    131: /*
1.1.1.1.2.1! nbrk      132:  * Find the zombie process in the child processes. It just
        !           133:  * returns the pid and exit code if it find at least one zombie
        !           134:  * process.
1.1       nbrk      135:  *
                    136:  * The library stub for waitpid() will wait the SIGCHLD signal in
                    137:  * the stub code if there is no zombie process in child process.
                    138:  * This signal is sent by proc_exit() or proc_stop() routines in
                    139:  * the process server.
                    140:  */
                    141: int
                    142: proc_waitpid(struct msg *msg)
                    143: {
                    144:        pid_t pid, pid_child;
1.1.1.1.2.1! nbrk      145:        int options, code, match;
1.1       nbrk      146:        struct proc *p;
                    147:        list_t head, n;
                    148:
                    149:        if (curproc == NULL)
                    150:                return EINVAL;
                    151:
                    152:        pid = (pid_t)msg->data[0];
                    153:        options = msg->data[1];
1.1.1.1.2.1! nbrk      154:        DPRINTF(("wait task=%x pid=%d options=%x\n", msg->hdr.task,
        !           155:                 pid, options));
1.1       nbrk      156:
1.1.1.1.2.1! nbrk      157:        if (list_empty(&curproc->p_children))
1.1       nbrk      158:                return ECHILD;  /* No child process */
                    159:
                    160:        /* Set the default pid and exit code */
                    161:        pid_child = 0;
                    162:        code = 0;
                    163:
                    164:        /*
                    165:         * Check all processes.
                    166:         */
                    167:        p = NULL;
1.1.1.1.2.1! nbrk      168:        head = &curproc->p_children;
1.1       nbrk      169:        for (n = list_first(head); n != head; n = list_next(n)) {
1.1.1.1.2.1! nbrk      170:                p = list_entry(n, struct proc, p_sibling);
1.1       nbrk      171:
                    172:                /*
                    173:                 * Check if pid matches.
                    174:                 */
1.1.1.1.2.1! nbrk      175:                match = 0;
1.1       nbrk      176:                if (pid > 0) {
1.1.1.1.2.1! nbrk      177:                        /*
        !           178:                         * Wait a specific child process.
        !           179:                         */
        !           180:                        if (p->p_pid == pid)
        !           181:                                match = 1;
1.1       nbrk      182:                } else if (pid == 0) {
1.1.1.1.2.1! nbrk      183:                        /*
        !           184:                         * Wait a process who has same pgid.
        !           185:                         */
        !           186:                        if (p->p_pgrp->pg_pgid == curproc->p_pgrp->pg_pgid)
        !           187:                                match = 1;
1.1       nbrk      188:                } else if (pid != -1) {
1.1.1.1.2.1! nbrk      189:                        /*
        !           190:                         * Wait a specific pgid.
        !           191:                         */
        !           192:                        if (p->p_pgrp->pg_pgid == -pid)
        !           193:                                match = 1;
        !           194:                } else {
        !           195:                        /*
        !           196:                         * pid = -1 means wait any child process.
        !           197:                         */
        !           198:                        match = 1;
1.1       nbrk      199:                }
1.1.1.1.2.1! nbrk      200:                if (match) {
        !           201:                        /*
        !           202:                         * Get the exit code.
        !           203:                         */
        !           204:                        if (p->p_stat == SSTOP) {
        !           205:                                pid_child = p->p_pid;
        !           206:                                code = p->p_exitcode;
        !           207:                                break;
        !           208:                        } else if (p->p_stat == SZOMB) {
        !           209:                                pid_child = p->p_pid;
        !           210:                                code = p->p_exitcode;
        !           211:                                proc_cleanup(p);
        !           212:                                break;
        !           213:                        }
1.1       nbrk      214:                }
                    215:        }
                    216:        msg->data[0] = pid_child;
                    217:        msg->data[1] = code;
                    218:        return 0;
                    219: }

CVSweb