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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/* Hash table functions
Copyright (C) 1993, 1994 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 <sys/types.h>
#include <hurd/hurd_types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/resource.h>
#include "proc.h"
#define HASH(pg) (id % ht->size)
#define REHASH(pg, h) ((id + (h * h)) % ht->size)
struct htable
{
void **tab;
int *ids;
int size;
};
#define HASH_DEL ((void *) -1)
static struct htable pghash, pidhash, taskhash, porthash, sidhash, collhash;
static struct htable pgcollhash, sesscollhash;
/* Add ITEM to the hash table HT. LOCP is the address of a pointer located
in ITEM; *LOCP will be set to the address of ITEM's hash table pointer.
The lookup key of ITEM is ID. */
static void
addhash (struct htable *ht,
void *item,
void ***locp,
int id)
{
int h, firsth = -1;
void **oldlist;
int *oldids;
int oldsize;
int i;
for (h = HASH (id);
(ht->tab[h] != 0 && ht->tab[h] != HASH_DEL && h != firsth);
firsth = (firsth == -1) ? h : firsth, h = REHASH (id, firsth))
;
if (ht->tab[h] == 0
|| ht->tab[h] == HASH_DEL)
{
ht->tab[h] = item;
ht->ids[h] = id;
*locp = &ht->tab[h];
return;
}
/* We have to rehash this again? */
oldlist = ht->tab;
oldsize = ht->size;
oldids = ht->ids;
ht->size = nextprime (2 * ht->size);
ht->tab = malloc (ht->size * sizeof (void *));
ht->ids = malloc (ht->size * sizeof (int));
bzero (ht->tab, (ht->size * sizeof (void *)));
if (oldlist)
for (i = 0; i < oldsize; i++)
if (oldlist[i] != 0
&& oldlist[i] != HASH_DEL)
addhash (ht, oldlist[i], locp, oldids[i]);
addhash (ht, item, locp, id);
if (oldlist)
{
free (oldlist);
free (oldids);
}
}
/* Find and return the item in hash table HT with key ID. */
static void *
findhash (struct htable *ht,
int id)
{
int h, firsth = -1;
void *ret;
for (h = HASH (id);
(ht->tab[h] != 0 && ht->ids[h] != id && h != firsth);
firsth = (firsth == -1) ? h : firsth, h = REHASH (id, firsth))
;
if (ht->ids[h] == id)
ret = ht->tab[h];
else
ret = 0;
return ret;
}
/* Find the process corresponding to a given pid. */
struct proc *
pid_find (pid_t pid)
{
return findhash (&pidhash, pid);
}
/* Find the process corresponding to a given task. */
struct proc *
task_find (task_t task)
{
return findhash (&taskhash, task) ? : add_tasks (task);
}
/* 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)
{
return findhash (&taskhash, task);
}
/* Find the process corresponding to a given request port. */
struct proc *
reqport_find (mach_port_t reqport)
{
return findhash (&porthash, reqport);
}
/* Find the process group corresponding to a given pgid. */
struct pgrp *
pgrp_find (pid_t pgid)
{
return findhash (&pghash, pgid);
}
/* Find the session corresponding to a given sid. */
struct session *
session_find (pid_t sid)
{
return findhash (&sidhash, sid);
}
/* Find the process collection corresponding to a gived refport. */
struct coll *
coll_find (mach_port_t refport)
{
return findhash (&collhash, refport);
}
/* Add a new process to the various hash tables. */
void
add_proc_to_hash (struct proc *p)
{
addhash (&pidhash, p, &p->p_pidhashloc, p->p_pid);
addhash (&taskhash, p, &p->p_taskhashloc, p->p_task);
addhash (&porthash, p, &p->p_porthashloc, p->p_reqport);
}
/* Add a new process group to the various hash tables. */
void
add_pgrp_to_hash (struct pgrp *pg)
{
addhash (&pghash, pg, &pg->pg_hashloc, pg->pg_pgid);
if (pg->pg_fakecoll)
addhash (&pgcollhash, pg, &pg->pg_fakecollhashloc, pg->pg_fakecoll);
else
pg->pg_fakecollhashloc = 0;
}
/* A process group's fake collection port has changed; update
the hash tables apporpriately. */
void
add_pgrp_fakecoll_to_hash (struct pgrp *pg)
{
if (pg->pg_fakecollhashloc)
*pg->pg_fakecollhashloc = HASH_DEL;
addhash (&pgcollhash, pg, &pg->pg_fakecollhashloc, pg->pg_fakecoll);
}
/* Add a new process collection to the various hash tables. */
void
add_coll_to_hash (struct coll *c)
{
addhash (&collhash, c, &c->c_hashloc, c->c_refport);
}
/* Add a new session to the various hash tables. */
void
add_session_to_hash (struct session *s)
{
addhash (&sidhash, s, &s->s_hashloc, s->s_sid);
if (s->s_fakecoll)
addhash (&sesscollhash, s, &s->s_fakecollhashloc, s->s_fakecoll);
else
s->s_fakecollhashloc = 0;
}
/* A session's fake collection port has changes; update the hash tables
appropriately. */
void
add_session_fakecoll_to_hash (struct session *s)
{
if (s->s_fakecollhashloc)
*s->s_fakecollhashloc = HASH_DEL;
addhash (&sesscollhash, s, &s->s_fakecollhashloc, s->s_fakecoll);
}
/* Remove a process group from the various hash tables. */
void
remove_pgrp_from_hash (struct pgrp *pg)
{
*pg->pg_hashloc = HASH_DEL;
if (pg->pg_fakecollhashloc)
*pg->pg_fakecollhashloc = HASH_DEL;
}
/* Remove a process from the various hash tables. */
void
remove_proc_from_hash (struct proc *p)
{
*p->p_pidhashloc = HASH_DEL;
*p->p_taskhashloc = HASH_DEL;
*p->p_porthashloc = HASH_DEL;
}
/* Remove a process collection from the various hash tables. */
void
remove_coll_from_hash (struct coll *c)
{
*c->c_hashloc = HASH_DEL;
}
/* Remove a session from the various hash tables. */
void
remove_session_from_hash (struct session *s)
{
*s->s_hashloc = HASH_DEL;
if (s->s_fakecollhashloc)
*s->s_fakecollhashloc = HASH_DEL;
}
/* 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)
{
int i;
for (i = 0; i < pidhash.size; i++)
if (pidhash.tab[i] && pidhash.tab[i] != HASH_DEL)
(*fun)(pidhash.tab[i], arg);
}
/* Call function FUN of two args for each process collection. FUN's first
arg is the collection, its second arg is ARG. */
void
colliterate (void (*fun) (struct coll *, void *), void *arg)
{
int i;
for (i = 0; i < collhash.size; i++)
if (collhash.tab[i] && collhash.tab[i] != HASH_DEL)
(*fun)(collhash.tab[i], arg);
}
/* Tell if a pid is available for use */
int
pidfree (pid_t pid)
{
return (!pid_find (pid) && !pgrp_find (pid)
&& !session_find (pid) && !zombie_check_pid (pid));
}
|