Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/server/proc/proc_hash.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. p_find
  2. pg_find
  3. task_to_proc
  4. p_add
  5. p_remove
  6. pg_add
  7. pg_remove
  8. table_init

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

/* [<][>][^][v][top][bottom][index][help] */