summaryrefslogtreecommitdiff
path: root/proc/proc.h
blob: 0e83574de570ae2977d9114852e30e6cbbdefc37 (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
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
/* Process server definitions
   Copyright (C) 1992,93,94,95,96,99,2000,01 Free Software Foundation, Inc.

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.  */

#ifndef PROC_H_INCLUDED
#define PROC_H_INCLUDED

#include <sys/resource.h>
#include <sys/mman.h>
#include <hurd/ports.h>
#include <cthreads.h>

struct proc
{
  struct port_info p_pi;

  /* List of members of a process group */
  struct proc *p_gnext, **p_gprevp; /* process group */

  /* Hash table pointers that point here */
  void **p_pidhashloc;		/* by pid */
  void **p_taskhashloc;		/* by task port */

  /* Identification of this process */
  task_t p_task;
  pid_t p_pid;
  struct login *p_login;
  uid_t p_owner;
  struct ids *p_id;

  /* Process hierarchy */
  /* Every process is in the process hierarchy except processes
     0 and 1.  Processes which have not had proc_child called
     on their behalf are parented by 1. */
  struct proc *p_parent;	/* parent process */
  struct proc *p_ochild;	/* youngest child */
  struct proc *p_sib, **p_prevsib; /* next youngest sibling */

  /* Process group structure */
  struct pgrp *p_pgrp;

  /* Communication */
  mach_port_t p_msgport;	/* send right */

  struct condition p_wakeup;

  /* Miscellaneous information */
  vm_address_t p_argv, p_envp;
  int p_status;			/* to return via wait */
  int p_sigcode;
  struct rusage p_rusage;	/* my usage if I'm dead, to return via wait */

  struct rusage p_child_rusage;	/* accumulates p_rusage of all dead children */

  unsigned int p_exec:1;	/* has called proc_mark_exec */
  unsigned int p_stopped:1;	/* has called proc_mark_stop */
  unsigned int p_waited:1;	/* stop has been reported to parent */
  unsigned int p_exiting:1;	/* has called proc_mark_exit */
  unsigned int p_waiting:1;	/* blocked in wait */
  unsigned int p_traced:1;	/* has called proc_mark_traced */
  unsigned int p_nostopcld:1;	/* has called proc_mark_nostopchild */
  unsigned int p_parentset:1;	/* has had a parent set with proc_child */
  unsigned int p_deadmsg:1;	/* hang on requests for a message port */
  unsigned int p_checkmsghangs:1; /* someone is currently hanging on us */
  unsigned int p_msgportwait:1;	/* blocked in getmsgport */
  unsigned int p_noowner:1;	/* has no owner known */
  unsigned int p_loginleader:1;	/* leader of login collection */
  unsigned int p_dead:1;	/* process is dead */
};

typedef struct proc *pstruct_t;

struct pgrp
{
  void **pg_hashloc;
  struct proc *pg_plist;	/* member processes */
  struct pgrp *pg_next, **pg_prevp; /* list of pgrps in session */
  pid_t pg_pgid;
  struct session *pg_session;
  int pg_orphcnt;		/* number of non-orphaned processes */
};

struct session
{
  void **s_hashloc;
  pid_t s_sid;
  struct pgrp *s_pgrps;		/* list of member pgrps */
  mach_port_t s_sessionid;	/* receive right */
};

struct login
{
  int l_refcnt;
  char l_name[0];
};

struct ids
{
  int i_refcnt;
  int i_nuids;
  uid_t i_uids[0];
};

/* Structure for an exception port we are listening on.  */
struct exc
{
  struct port_info pi;
  mach_port_t forwardport;	/* Send right to forward msg to.  */
  int flavor;			/* State to restore faulting thread to.  */
  mach_msg_type_number_t statecnt;
  natural_t thread_state[0];
};

struct zombie *zombie_list;

mach_port_t authserver;
struct proc *self_proc;		/* process 0 (us) */
struct proc *startup_proc;	/* process 1 (init) */

struct port_bucket *proc_bucket;
struct port_class *proc_class;
struct port_class *generic_port_class;
struct port_class *exc_class;

mach_port_t master_host_port;
mach_port_t master_device_port;

mach_port_t generic_port;	/* messages not related to a specific proc */

struct mutex global_lock;

extern inline void
process_drop (struct proc *p)
{
  if (p)
    ports_port_deref (p);
}

/* Forward declarations */
void complete_wait (struct proc *, int);
int check_uid (struct proc *, uid_t);
void addalltasks (void);
void prociterate (void (*)(struct proc *, void *), void *);
void count_up (void *);
void store_pid (void *);
void free_process (struct proc *);
void panic (char *);
int valid_task (task_t);
int genpid ();
void abort_getmsgport (struct proc *);
int zombie_check_pid (pid_t);
void check_message_dying (struct proc *, struct proc *);
int check_msgport_death (struct proc *);
void check_dead_execdata_notify (mach_port_t);

void add_proc_to_hash (struct proc *);
void add_exc_to_hash (struct exc *);
void remove_proc_from_hash (struct proc *);
void add_pgrp_to_hash (struct pgrp *);
void add_session_to_hash (struct session *);
void remove_session_from_hash (struct session *);
void remove_pgrp_from_hash (struct pgrp *);
void remove_exc_from_hash (struct exc *);
struct exc *exc_find (mach_port_t);
struct proc *pid_find (int);
struct proc *pid_find_allow_zombie (int);
struct proc *task_find (task_t);
struct proc *task_find_nocreate (task_t);
struct pgrp *pgrp_find (int);
struct proc *reqport_find (mach_port_t);
struct session *session_find (pid_t);

void exc_clean (void *);

struct proc *add_tasks (task_t);
int pidfree (pid_t);

struct proc *create_startup_proc (void);
struct proc *allocate_proc (task_t);
void proc_death_notify (struct proc *);
void complete_proc (struct proc *, pid_t);

void leave_pgrp (struct proc *);
void join_pgrp (struct proc *);
void boot_setsid (struct proc *);

void process_has_exited (struct proc *);
void alert_parent (struct proc *);
void reparent_zombies (struct proc *);
void complete_exit (struct proc *);

void initialize_version_info (void);

void send_signal (mach_port_t, int, mach_port_t);

/* Returns true if PROC1 has `owner' privileges over PROC2 (and can thus get
   its task port &c).  If PROC2 has an owner, then PROC1 must have that uid;
   otherwise, both must be in the same login collection.  */
extern inline int
check_owner (struct proc *proc1, struct proc *proc2)
{
  return
    proc2->p_noowner
      ? check_uid (proc1, 0) || proc1->p_login == proc2->p_login
      : check_uid (proc1, proc2->p_owner);
}

#endif