summaryrefslogtreecommitdiff
path: root/libps/context.c
blob: a9cab6e7b51891674630f1fcd8dec394242abfb3 (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
157
158
159
160
161
162
163
164
165
/* The ps_context type, for per-procserver and somewhat global state.

   Copyright (C) 1995,96,99,2000 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.org>

   This program 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.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <hurd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <hurd/term.h>

#include "ps.h"
#include "common.h"

/* ---------------------------------------------------------------- */

/* Returns in PC a new ps_context for the proc server SERVER.  If a memory
   allocation error occurs, ENOMEM is returned, otherwise 0.  */
error_t
ps_context_create (process_t server, struct ps_context **pc)
{
  error_t err_procs, err_ttys, err_ttys_by_cttyid, err_users;

  *pc = NEW (struct ps_context);
  if (*pc == NULL)
    return ENOMEM;

  (*pc)->server = server;
  (*pc)->user_hooks = 0;
  err_procs = ihash_create (&(*pc)->procs);
  err_ttys = ihash_create (&(*pc)->ttys);
  err_ttys_by_cttyid = ihash_create (&(*pc)->ttys_by_cttyid);
  err_users = ihash_create (&(*pc)->users);

  if (err_procs || err_ttys || err_ttys_by_cttyid)
    /* Some allocation error occurred, backout any successful ones and fail. */
    {
      if (!err_procs) ihash_free ((*pc)->procs);
      if (!err_users) ihash_free ((*pc)->users);
      if (!err_ttys)  ihash_free ((*pc)->ttys);
      if (!err_ttys_by_cttyid) ihash_free ((*pc)->ttys_by_cttyid);
      free (*pc);
      return ENOMEM;
    }

  ihash_set_cleanup ((*pc)->procs,
		    (void (*)(void *, void *arg))_proc_stat_free,
		    NULL);
  ihash_set_cleanup ((*pc)->ttys,
		    (void (*)(void *, void *arg))ps_tty_free,
		    NULL);
  ihash_set_cleanup ((*pc)->users,
		    (void (*)(void *, void *arg))ps_user_free,
		    NULL);

  return 0;
}

/* Frees PC and any resources it consumes.  */
void
ps_context_free (struct ps_context *pc)
{
  ihash_free (pc->procs);
  ihash_free (pc->ttys);
  ihash_free (pc->ttys_by_cttyid);
  ihash_free (pc->users);
  free (pc);
}

/* ---------------------------------------------------------------- */

/* Return the value in HT indexed by the key ID.  If it doesn't exist create
   it by calling CREATE with ID and a return location pointer as arguments
   (CREATE should return either an error-code or 0 if no error occurs), and
   cache it in HT.  */
static error_t
lookup (int id, ihash_t ht, error_t (*create)(int id, void **), void **value)
{
  *value = ihash_find (ht, id);
  if (*value == NULL)
    {
      error_t err = create (id, value);
      if (err)
	return err;
      ihash_add (ht, id, *value, NULL);
    }
  return 0;
}

/* Find a proc_stat for the process referred to by PID, and return it in
   PS.  If an error occurs, it is returned, otherwise 0.  */
error_t
ps_context_find_proc_stat (struct ps_context *pc, pid_t pid, struct proc_stat **ps)
{
  error_t create (int pid, void **value)
    {
      return _proc_stat_create (pid, pc, (struct proc_stat **)value);
    }
  return lookup (pid, pc->procs, create, (void **)ps);
}

/* Find a ps_tty for the terminal referred to by the port TTY_PORT, and
   return it in TTY.  If an error occurs, it is returned, otherwise 0.  */
error_t
ps_context_find_tty (struct ps_context *pc, mach_port_t tty_port,
		     struct ps_tty **tty)
{
  return lookup (tty_port,
		pc->ttys,
		(error_t (*)(int id, void **result))ps_tty_create,
		(void **)tty);
}

/* Find a ps_tty for the terminal referred to by the ctty id port
   CTTYID_PORT, and return it in TTY.  If an error occurs, it is returned,
   otherwise 0.  */
error_t
ps_context_find_tty_by_cttyid (struct ps_context *pc, mach_port_t cttyid_port,
			       struct ps_tty **tty)
{
  error_t create (int cttyid_port, void **value)
    {
      if (cttyid_port == MACH_PORT_NULL)
	{
	  *value = 0;
	  return 0;
	}
      else
	{
	  int tty_port;
	  error_t err = termctty_open_terminal (cttyid_port, 0, &tty_port);
	  if (err)
	    return err;
	  else
	    return ps_context_find_tty (pc, tty_port, (struct ps_tty **)value);
	}
    }

  return lookup (cttyid_port, pc->ttys_by_cttyid, create, (void **)tty);
}

/* Find a ps_user for the user referred to by UID, and return it in U.  */
error_t
ps_context_find_user (struct ps_context *pc, uid_t uid, struct ps_user **u)
{
  return lookup (uid,
		pc->users,
		(error_t (*)(int id, void **result))ps_user_create,
		(void **)u);
}