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

Annotation of prex-old/usr/server/proc/kill.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:  * kill.c - signal transfer.
        !            32:  */
        !            33:
        !            34: #include <prex/prex.h>
        !            35: #include <prex/capability.h>
        !            36: #include <server/proc.h>
        !            37: #include <sys/list.h>
        !            38:
        !            39: #include <unistd.h>
        !            40: #include <errno.h>
        !            41:
        !            42: #include "proc.h"
        !            43:
        !            44: /*
        !            45:  * Send a signal to the task.
        !            46:  */
        !            47: static int
        !            48: send_sig(struct proc *proc, int sig)
        !            49: {
        !            50:
        !            51:        if (proc->pid == 0 || proc->pid == 1)
        !            52:                return EPERM;
        !            53:        return exception_raise(proc->task, sig);
        !            54: }
        !            55:
        !            56: /*
        !            57:  * Send a signal to one process.
        !            58:  */
        !            59: static int
        !            60: kill_one(pid_t pid, int sig)
        !            61: {
        !            62:        struct proc *p;
        !            63:
        !            64:        dprintf("killone pid=%x sig=%d\n", pid, sig);
        !            65:        if ((p = proc_find(pid)) == NULL)
        !            66:                return ESRCH;
        !            67:        return send_sig(p, sig);
        !            68: }
        !            69:
        !            70: /*
        !            71:  * Send signal to all process in the process group.
        !            72:  */
        !            73: int
        !            74: kill_pg(pid_t pgid, int sig)
        !            75: {
        !            76:        struct proc *p;
        !            77:        struct pgrp *pgrp;
        !            78:        list_t head, n;
        !            79:        int err = 0;
        !            80:
        !            81:        dprintf("killpg pgid=%x sig=%d\n", pgid, sig);
        !            82:
        !            83:        if ((pgrp = pgrp_find(pgid)) == NULL)
        !            84:                return ESRCH;
        !            85:
        !            86:        head = &pgrp->members;
        !            87:        for (n = list_first(head); n != head; n = list_next(n)) {
        !            88:                p = list_entry(n, struct proc, pgrp_link);
        !            89:                if ((err = send_sig(p, sig)) != 0)
        !            90:                        break;
        !            91:        }
        !            92:        return err;
        !            93: }
        !            94:
        !            95: /*
        !            96:  * Send a signal.
        !            97:  *
        !            98:  * The behavior is different for the pid value.
        !            99:  *
        !           100:  *  if (pid > 0)
        !           101:  *    Send a signal to specific process.
        !           102:  *
        !           103:  *  if (pid == 0)
        !           104:  *    Send a signal to all processes in same process group.
        !           105:  *
        !           106:  *  if (pid == -1)
        !           107:  *    Send a signal to all processes except init.
        !           108:  *
        !           109:  *  if (pid < -1)
        !           110:  *     Send a signal to the process group.
        !           111:  *
        !           112:  * Note: Need CAP_KILL capability to send a signal to the different
        !           113:  * process/group.
        !           114:  */
        !           115: int
        !           116: proc_kill(struct msg *msg)
        !           117: {
        !           118:        pid_t pid;
        !           119:        struct proc *p;
        !           120:        list_t n;
        !           121:        int sig, capable = 0;
        !           122:        int err = 0;
        !           123:
        !           124:        pid = (pid_t)msg->data[0];
        !           125:        sig = msg->data[1];
        !           126:
        !           127:        dprintf("kill pid=%x sig=%d\n", pid, sig);
        !           128:
        !           129:        switch (sig) {
        !           130:        case SIGFPE:
        !           131:        case SIGILL:
        !           132:        case SIGSEGV:
        !           133:                return EINVAL;
        !           134:        }
        !           135:
        !           136:        if (curproc->cap & CAP_KILL)
        !           137:                capable = 1;
        !           138:
        !           139:        if (pid > 0) {
        !           140:                if (pid != curproc->pid && !capable)
        !           141:                        return EPERM;
        !           142:                err = kill_one(pid, sig);
        !           143:        }
        !           144:        else if (pid == -1) {
        !           145:                if (!capable)
        !           146:                        return EPERM;
        !           147:                for (n = list_first(&allproc); n != &allproc;
        !           148:                     n = list_next(n)) {
        !           149:                        p = list_entry(n, struct proc, link);
        !           150:                        if (p->pid != 0 && p->pid != 1) {
        !           151:                                err = kill_one(p->pid, sig);
        !           152:                                if (err != 0)
        !           153:                                        break;
        !           154:                        }
        !           155:                }
        !           156:        }
        !           157:        else if (pid == 0) {
        !           158:                if ((p = proc_find(pid)) == NULL)
        !           159:                        return ESRCH;
        !           160:                err = kill_pg(p->pgrp->pgid, sig);
        !           161:        }
        !           162:        else {
        !           163:                if (curproc->pgrp->pgid != -pid && !capable)
        !           164:                        return EPERM;
        !           165:                err = kill_pg(-pid, sig);
        !           166:        }
        !           167:        return err;
        !           168: }

CVSweb