summaryrefslogtreecommitdiff
path: root/proc/msg.c
blob: 63be1e120bb9ea76042c132988e1639e2aefe057 (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
/* Message port manipulations
   Copyright (C) 1994 Free Software Foundation

   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 <mach.h>
#include <hurd.h>
#include "proc.h"
#include "process_reply.h"
#include <assert.h>
#include <stdlib.h>

/* Check to see if process P is blocked trying to get the message
   port of process AVAILP; if so, return its call.  */
void
check_message_return (struct proc *p, void *availpaddr)
{
  struct proc *availp = availpaddr;
  struct getmsgport_c *c = &p->p_continuation.getmsgport_c;
  
  if (p->p_msgportwait && c->msgp == availp)
    {
      proc_getmsgport_reply (c->reply_port, c->reply_port_type,
			     0, availp->p_msgport);
      c->msgp = 0;
      p->p_msgportwait = 0;
    }
}

error_t
S_proc_setmsgport (struct proc *p,
		 mach_port_t msgport,
		 mach_port_t *oldmsgport)
{
  *oldmsgport = p->p_msgport;
  p->p_msgport = msgport;
  p->p_deadmsg = 0;
  if (p->p_checkmsghangs)
    prociterate (check_message_return, p);
  p->p_checkmsghangs = 0;

  if (p == startup_proc)
    startup_essential_task (msgport, mach_task_self (), MACH_PORT_NULL,
			    "proc", master_host_port);
      
  return 0;
}

/* Check to see if process P is blocked trying to get the message port of 
   process DYINGP; if so, return its call with ESRCH. */
void
check_message_dying (struct proc *p, struct proc *dyingp)
{
  struct getmsgport_c *c = &p->p_continuation.getmsgport_c;
  
  if (p->p_msgportwait && c->msgp == dyingp)
    {
      proc_getmsgport_reply (c->reply_port, c->reply_port_type, ESRCH,
			     MACH_PORT_NULL);
      c->msgp = 0;
      p->p_msgportwait = 0;
    }
}

/* Cause a pending proc_getmsgport operation to immediately return */
void
abort_getmsgport (struct proc *p)
{
  struct getmsgport_c *c = &p->p_continuation.getmsgport_c;
  
  proc_getmsgport_reply (c->reply_port, c->reply_port_type, EINTR,
			 MACH_PORT_NULL);
  c->msgp = 0;
  p->p_msgportwait = 0;
}

error_t
S_proc_getmsgport (struct proc *callerp,
		   mach_port_t reply_port,
		   mach_msg_type_name_t reply_port_type,
		   pid_t pid,
		   mach_port_t *msgport)
{
  struct proc *p = pid_find (pid);

  if (!p)
    return ESRCH;
  
  if (p->p_deadmsg)
    {
      struct getmsgport_c *c = &callerp->p_continuation.getmsgport_c;
      if (callerp->p_msgportwait)
	return EBUSY;
      c->reply_port = reply_port;
      c->reply_port_type = reply_port_type;
      c->msgp = p;
      p->p_checkmsghangs = 1;
      callerp->p_msgportwait = 1;
      return MIG_NO_REPLY;
    }
  
  *msgport = p->p_msgport;
  return 0;
}

void
message_port_dead (struct proc *p)
{
  mach_port_deallocate (mach_task_self (), p->p_msgport);
  p->p_msgport = MACH_PORT_NULL;
  p->p_deadmsg = 1;
}