Annotation of prex-old/usr/server/proc/hash.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: * hash.c - pid/pgid mapping tables.
! 32: */
! 33:
! 34: #include <server/stdmsg.h>
! 35: #include <sys/list.h>
! 36: #include <unistd.h>
! 37:
! 38: #include "proc.h"
! 39:
! 40: /*
! 41: * Hash tables for ID mapping
! 42: */
! 43: static struct list pid_hash[ID_MAXBUCKETS]; /* mapping: pid -> proc */
! 44: static struct list task_hash[ID_MAXBUCKETS]; /* mapping: task -> proc */
! 45: static struct list pgid_hash[ID_MAXBUCKETS]; /* mapping: pgid -> pgrp */
! 46:
! 47: /*
! 48: * Find process by pid.
! 49: */
! 50: struct proc *
! 51: proc_find(pid_t pid)
! 52: {
! 53: list_t head, n;
! 54: struct proc *p = NULL;
! 55:
! 56: head = &pid_hash[IDHASH(pid)];
! 57: n = list_first(head);
! 58: while (n != head) {
! 59: p = list_entry(n, struct proc, pid_link);
! 60: if (p->pid == pid)
! 61: return p;
! 62: n = list_next(n);
! 63: }
! 64: return NULL;
! 65: }
! 66:
! 67: /*
! 68: * Find process group by pgid.
! 69: */
! 70: struct pgrp *
! 71: pgrp_find(pid_t pgid)
! 72: {
! 73: list_t head, n;
! 74: struct pgrp *g = NULL;
! 75:
! 76: head = &pgid_hash[IDHASH(pgid)];
! 77: n = list_first(head);
! 78: while (n != head) {
! 79: g = list_entry(n, struct pgrp, pgid_link);
! 80: if (g->pgid == pgid)
! 81: return g;
! 82: n = list_next(n);
! 83: }
! 84: return NULL;
! 85: }
! 86:
! 87: /*
! 88: * Find process by task ID.
! 89: */
! 90: struct proc *
! 91: task_to_proc(task_t task)
! 92: {
! 93: list_t head, n;
! 94: struct proc *p = NULL;
! 95:
! 96: head = &task_hash[IDHASH(task)];
! 97: n = list_first(head);
! 98: while (n != head) {
! 99: p = list_entry(n, struct proc, task_link);
! 100: if (p->task == task)
! 101: return p;
! 102: n = list_next(n);
! 103: }
! 104: return NULL;
! 105: }
! 106:
! 107: /*
! 108: * Add process to the pid table and the task table.
! 109: * This routine assumes pid and task data has been already initialized.
! 110: */
! 111: void
! 112: proc_add(struct proc *proc)
! 113: {
! 114:
! 115: list_insert(&pid_hash[IDHASH(proc->pid)], &proc->pid_link);
! 116: list_insert(&task_hash[IDHASH(proc->task)], &proc->task_link);
! 117: }
! 118:
! 119: /*
! 120: * Remove process from both of pid table and task table.
! 121: */
! 122: void
! 123: proc_remove(struct proc *proc)
! 124: {
! 125:
! 126: list_remove(&proc->pid_link);
! 127: list_remove(&proc->task_link);
! 128: }
! 129:
! 130: /*
! 131: * Add process group to table.
! 132: * This routine assumes pgid has been already initialized.
! 133: */
! 134: void
! 135: pgrp_add(struct pgrp *pgrp)
! 136: {
! 137:
! 138: list_insert(&pgid_hash[IDHASH(pgrp->pgid)], &pgrp->pgid_link);
! 139: }
! 140:
! 141: /*
! 142: * Remove process from pgid table.
! 143: */
! 144: void
! 145: pgrp_remove(struct pgrp *pgrp)
! 146: {
! 147:
! 148: list_remove(&pgrp->pgid_link);
! 149: }
! 150:
! 151: /*
! 152: * Initialize tables.
! 153: */
! 154: void
! 155: table_init(void)
! 156: {
! 157: int i;
! 158:
! 159: for (i = 0; i < ID_MAXBUCKETS; i++) {
! 160: list_init(&pid_hash[i]);
! 161: list_init(&task_hash[i]);
! 162: list_init(&pgid_hash[i]);
! 163: }
! 164: }
CVSweb