summaryrefslogtreecommitdiff
path: root/proc/hash.c
blob: e4dc5ffdbce9353226f8ebe757ae00c2b3455e9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Hash table functions
   Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation

This file is part of the GNU Hurd.

The GNU Hurd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The GNU Hurd is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the GNU Hurd; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Written by Michael I. Bushnell.  */

#include <mach.h>
#include <stddef.h>
#include <sys/types.h>
#include <hurd/hurd_types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/resource.h>

#include "proc.h"
#include <hurd/ihash.h>

static struct hurd_ihash pghash
  = HURD_IHASH_INITIALIZER (offsetof (struct pgrp, pg_hashloc));
static struct hurd_ihash pidhash
  = HURD_IHASH_INITIALIZER (offsetof (struct proc, p_pidhashloc));
static struct hurd_ihash taskhash
  = HURD_IHASH_INITIALIZER (offsetof (struct proc, p_taskhashloc));
static struct hurd_ihash sidhash
  = HURD_IHASH_INITIALIZER (offsetof (struct session, s_hashloc));


/* Find the process corresponding to a given pid. */
struct proc *
pid_find (pid_t pid)
{
  struct proc *p;
  p = hurd_ihash_find (&pidhash, pid);
  return (!p || p->p_dead) ? 0 : p;
}

/* Find the process corresponding to a given pid.  Return it even if
   it's dead. */
struct proc *
pid_find_allow_zombie (pid_t pid)
{
  return hurd_ihash_find (&pidhash, pid);
}

/* Find the process corresponding to a given task. */
struct proc *
task_find (task_t task)
{
  struct proc *p;
  p = hurd_ihash_find (&taskhash, task) ? : add_tasks (task);
  return (!p || p->p_dead) ? 0 : p;
}

/* Find the process corresponding to a given task, but
   if we don't already know about it, just return 0. */
struct proc *
task_find_nocreate (task_t task)
{
  struct proc *p;
  p = hurd_ihash_find (&taskhash, task);
  return (!p || p->p_dead) ? 0 : p;
}

/* Find the process group corresponding to a given pgid. */
struct pgrp *
pgrp_find (pid_t pgid)
{
  return hurd_ihash_find (&pghash, pgid);
}

/* Find the session corresponding to a given sid. */
struct session *
session_find (pid_t sid)
{
  return hurd_ihash_find (&sidhash, sid);
}

/* Add a new process to the various hash tables. */
void
add_proc_to_hash (struct proc *p)
{
  hurd_ihash_add (&pidhash, p->p_pid, p);
  hurd_ihash_add (&taskhash, p->p_task, p);
}

/* Add a new process group to the various hash tables. */
void
add_pgrp_to_hash (struct pgrp *pg)
{
  hurd_ihash_add (&pghash, pg->pg_pgid, pg);
}

/* Add a new session to the various hash tables. */
void
add_session_to_hash (struct session *s)
{
  hurd_ihash_add (&sidhash, s->s_sid, s);
}

/* Remove a process group from the various hash tables. */
void
remove_pgrp_from_hash (struct pgrp *pg)
{
  hurd_ihash_locp_remove (&pghash, pg->pg_hashloc);
}

/* Remove a process from the various hash tables. */
void
remove_proc_from_hash (struct proc *p)
{
  hurd_ihash_locp_remove (&pidhash, p->p_pidhashloc);
  hurd_ihash_locp_remove (&taskhash, p->p_taskhashloc);
}

/* Remove a session from the various hash tables. */
void
remove_session_from_hash (struct session *s)
{
  hurd_ihash_locp_remove (&sidhash, s->s_hashloc);
}

/* Call function FUN of two args for each process.  FUN's first arg is
   the process, its second arg is ARG. */
void
prociterate (void (*fun) (struct proc *, void *), void *arg)
{
  HURD_IHASH_ITERATE (&pidhash, value)
    {
      struct proc *p = value;
      if (!p->p_dead)
	(*fun)(p, arg);
    }
}

/* Tell if a pid is available for use */
int
pidfree (pid_t pid)
{
  return (!pid_find_allow_zombie (pid)
	  && !pgrp_find (pid) && !session_find (pid));
}