[comp.os.mach] Catching process abort/exit in Mach

kwk@ddsdx2.jhuapl.edu (Ken Koontz x6328) (06/08/91)

Craig Hughes writes:

>I'd like to be able to catch, via a user process, when any process
>running under Mach terminates either normally or abnormally. I believe this
>can be done with exception port manipulation, but I'm not sure how
>(I'm new to mach programming).

     I'm new at this Mach thing too but I've been playing around with an
experiment to do the same thing.  According to the "Mach Kernel
Interface Manual" (pg 28), the notify port (task_notify_ or task_notify()) is
used to send kernel events to a task.  One such event is the destruction of
a port to which the task has send rights.  I have a 2 tasks, A and B.  A has
send rights to B's input_port (obtained several ways, the easiest being through
the Network Name Server).  A has a port set containing some other application
input ports plus task_notify().  A can be sitting on a msg_receive and I can
kill off B (possibly running on another node) by hitting Ctrl-C in the terminal
port used to start B running.  A gets notified of B's death on task_notify()!
If B decides on its own to deallocate the port, A also gets notified.

     Sounds like this could be extended to a user process monitoring the death
of more than one (possibly many) other processes.  Call the monitor process
"the Coroner" (the Reaper already appears in too many Unix places).  Call the
processes to monitor "the Victims".  The Coroner would first allocate a port
from_victims (rights=SRO) and make it known to all Victims through the Network
Name Server (using netname_check_in).  A Victim would look for the Coroner
using netname_look_up to get send rights to the Coroner.  The Victim then
allocates a port my_pulse (rights=SRO).  Now the trick is to give the Coroner
send rights to the Victim's my_pulse port.  There are at least two ways to do
this:

     - the easiest way is to create a message in the Victim with
       .h.msg_local_port = my_pulse, .h.msg_remote_port = from_victims and
       send it to the Coroner.  Since .h.msg_local_port is not NULL, the kernel
       will give send rights to this port for an RPC.  But you don't need to
       send a return.  Here, we're just using the fact that the Coroner will
       now have send rights to a port on each Victim.

     - another possible way is for the Victim to construct a message with
       .h.msg_local_port = NULL, .h.msg_remote_port = from_victims, but place
       the my_pulse port in the message data (.msg_type_name = MSG_TYPE_PORT).
       Sending this message to the Coroner should transfer send, receive, and
       ownership rights to this port to the Coroner but the Victim should
       continue to have send rights.  I haven't checked this out but I think
       it should work.

     Now, the Coroner should make a port set containing from_victims and
task_notify() to simplify things (or you can use multiple threads, one on each
port and some sync between threads).  Any messages received on from_victims
should give the Coroner send rights which should also enable any notifying of
deallocation of the Victim's my_pulse port on task_notify.  The Victim's port
can also be saved in an array or linked list to allow the Coroner to determine
which Victim died.

     My current program has lots of other things in it besides just this stuff;
maybe I should spend some time to remove everything except the Coroner and
Victim and post it to the net (I'll be on travel next week, maybe after that).
This might help clarify the mud.

     There's one question I have for Mach CMU types.  I haven't looked at the
kernel code yet but I was wondering: what is the format of EVERY type of message
that is sent over task_notify?  I'm working on the Mt. Xinu Mach 2.6 release,
not 3.0 (it's probably different).  I haven't been able to find a document
containing this info but I haven't read every one cover-to-cover either.
What I'm interested in knowing is if and where does the message received tell
me which port was deallocated?  Otherwise, my Coroner just knows that a 
murderer is about but can't tell which of the Victims has died.

     As for normal exits, I'm not sure but if the Victim exits without first
deallocating the port, the Coroner will be informed.  Also, if the Victim is
well behaved and tells the Coroner that it's going to die by deallocating a
port intensionally, the Coroner will get informed.  I have to check this out
sometime.

     Sounds like there's a Mach Murder Mystery here somewhere!!!

Ken Koontz
The Johns Hopkins University
  Applied Physics Laboratory
Laurel, MD 20723
email: koontz@capsrv.jhuapl.edu