home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 14 Text / 14-Text.zip / machdocs.zip / manual1.doc < prev    next >
Internet Message Format  |  1993-02-26  |  65KB

  1. From Pa.dec.com!nobody Thu Feb 25 20:15:32 1993
  2. Return-Path: <Pa.dec.com!nobody>
  3. Received: by ukelele.GCR.COM (Smail3.1.28.1 #1)
  4.     id m0nRtfn-0001S1a; Thu, 25 Feb 93 20:15 EST
  5. Received: from inet-gw-2.pa.dec.com by uu5.psi.com (5.65b/4.0.071791-PSI/PSINet) via SMTP;
  6.         id AA01551 for spj@ukelele.GCR.COM; Thu, 25 Feb 93 19:08:59 -0500
  7. Received: by inet-gw-2.pa.dec.com; id AA28393; Thu, 25 Feb 93 16:09:02 -0800
  8. Date: Thu, 25 Feb 93 16:09:02 -0800
  9. Message-Id: <9302260009.AA28393@inet-gw-2.pa.dec.com>
  10. From: "ftpmail service on inet-gw-2.pa.dec.com" <nobody@Pa.dec.com>
  11. To: spj@ukelele.GCR.COM
  12. Subject: part 001 of /.0/Mach/doc/manual.doc (@gatekeeper.dec.com) [Mach Dox] (ascii)
  13. X-Complaints-To: ftpmail-admin@inet-gw-2.pa.dec.com
  14. X-Service-Address: ftpmail@inet-gw-2.pa.dec.com
  15. X-Job-Number: 730680718.24209
  16. Precedence: bulk
  17. Reply-To: <nobody@inet-gw-2.pa.dec.com>
  18. Content-Type: text
  19. Content-Length: 64046
  20. Status: O
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.                          MACH KERNEL INTERFACE MANUAL
  37.  
  38.                                 Robert V. Baron
  39.                                   David Black
  40.                                 William Bolosky
  41.                                  Jonathan Chew
  42.                                Richard P. Draves
  43.                                 David B. Golub
  44.                                Richard F. Rashid
  45.                              Avadis Tevanian, Jr.
  46.                               Michael Wayne Young
  47.  
  48.                         Department of Computer Science
  49.                           Carnegie-Mellon University
  50.                              Pittsburgh, PA 15213
  51.                                   Version of:
  52.                                 9 January 1990
  53.  
  54.  
  55.  
  56.  
  57.  
  58.                                    ABSTRACT
  59.  
  60. MACH  is  an  operating  system  kernel  under  development  at Carnegie-Mellon
  61. University to support distributed and parallel computation.  MACH  is  designed
  62. to  support  computing environments consisting of networks of uniprocessors and
  63. multiprocessors.  This manual describes the interface to  the  MACH  kernel  in
  64. detail.    The MACH system currently runs on a wide variety of uniprocessor and
  65. multiprocessor architectures.
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.   This research was sponsored by the Defense Advanced Research Projects  Agency
  74. (DOD),  ARPA  Order  No. 4864, monitored by the Space and Naval Warfare Systems
  75. Command under contract N00039-84-C-0467.
  76.  
  77.   The views and conclusions contained in this document are those of the authors
  78. and  should  not  be  interpreted  as  representing  official  policies, either
  79. expressed or implied, of the Defense Advanced Research Projects  Agency  or  of
  80. the U.S.  Government.
  81. 1. Introduction
  82.   MACH is a communication-oriented operating system kernel providing:
  83.  
  84.    - multiple tasks, each with a large, paged virtual memory space,
  85.  
  86.    - multiple  threads  of  execution  within  each  task, with a flexible
  87.      scheduling facility,
  88.  
  89.    - flexible sharing of memory between tasks,
  90.  
  91.    - message-based interprocess communication,
  92.  
  93.    - transparent network extensibility, and
  94.  
  95.    - a flexible capability-based approach to security and protection.
  96.  
  97.   MACH supports multiprocessor scheduling and  is  currently  in  use  on  both
  98. general  purpose  multiprocessor  and  uniprocessor systems.  MACH is currently
  99. supported at CMU on the DEC VAX 8650, 8600, 11/785, 11/780, 11/750 and MicroVAX
  100. II,  the  IBM  RT/PC  and  the  SUN  3.    It  also will run as a shared memory
  101. multiprocessor system on the four processor VAX 11/784 and  two  processor  VAX
  102. 11/782,  the two processor VAX 8300, the VAX 8200 with one or more CPUs, the 20
  103. processor Encore MultiMax and the 30 processor Sequent Balance 21000.  Ports of
  104. MACH to other computers are in progress.
  105.  
  106.  
  107.  
  108. 1.1. Overall system organization
  109.   As  a  working  environment  for developing application programs, MACH can be
  110. viewed as being split into two components:
  111.  
  112.    - a small, extensible system kernel which provides scheduling,  virtual
  113.      memory and interprocess communications and
  114.  
  115.    - several,  possibly  parallel,  operating  system support environments
  116.      which provide the following two items: 1) distributed file access and
  117.      remote  execution  2)  emulation  for  established  operating  system
  118.      environments such as UNIX.
  119.  
  120.   The extensibility of the basic MACH kernel facilitates the  incorporation  of
  121. new  operating system functions; user-state programs can simply be added to the
  122. existing kernel without the need to modify the underlying  kernel  base.    The
  123. basic  kernel  abstractions  have been designed in such a way as to provide for
  124. completely transparent network extensibility of all kernel functions.
  125.  
  126.   MACH is 4.3bsd UNIX binary compatible  on  VAX  architecture  machines.    In
  127. addition,   the   MACH   environment  includes  an  internal  kernel  debugger,
  128. transparent network interprocess communication, remote execution facilities,  a
  129. transparent remote UNIX file system and support for graphics workstations.
  130.  
  131.  
  132.  
  133. 1.2. Basic kernel functionality
  134.   The MACH kernel supports the following basic abstractions:
  135.  
  136.    - A  task is an execution environment and is the basic unit of resource
  137.      allocation.  A  task  includes  a   paged   virtual   address   space
  138.      (potentially  sparse)  and protected access to system resources (such
  139.      as processors, port capabilities and virtual memory).
  140.  
  141.    - A thread is the  basic  unit  of  execution.    It  consists  of  all
  142.      processor  state  (e.g.hardware  registers) necessary for independent
  143.      execution.  A thread executes in the virtual memory and  port  rights
  144.      context  of  a single task.  The conventional notion of a process is,
  145.      in MACH, represented by a task with a single thread of control.
  146.  
  147.    - A port is a simplex communication channel -- implemented as a message
  148.      queue  managed and protected by the kernel.  A port is also the basic
  149.      object reference mechanism in MACH.   Ports  are  used  to  refer  to
  150.      objects;  operations  on objects are requested by sending messages to
  151.      the ports which represent them.
  152.  
  153.    - A port set is a group of ports, implemented as a queue combining  the
  154.      message queues of the constituent ports.  A thread may use a port set
  155.      to receive a message sent to any of several ports.
  156.  
  157.    - A message is a typed collection of data objects used in communication
  158.      between  threads.  Messages may be of any size and may contain inline
  159.      data, pointers to data, and capabilities for ports.
  160.  
  161.    - A memory object is a secondary storage object that is mapped  into  a
  162.      task's virtual memory. Memory objects are commonly files managed by a
  163.      file server, but as far as the MACH kernel  is  concerned,  a  memory
  164.      object  may  be implemented by any object (i.e. port) that can handle
  165.      requests to read and write data.
  166.  
  167.   Message-passing is the primary means of communication both among  tasks,  and
  168. between  tasks  and  the  operating  system  kernel itself.  The only functions
  169. implemented  by  system  traps  are  those  directly  concerned  with   message
  170. communication; all the rest are implemented by messages to a task's task_port.
  171.  
  172.   The MACH kernel functions can be divided into the following catagories:
  173.  
  174.    - basic message primitives and support facilities,
  175.  
  176.    - port and port set management facilities,
  177.  
  178.    - task and thread creation and management facilities,
  179.  
  180.    - virtual memory management functions,
  181.  
  182.    - operations on memory objects.
  183.  
  184.   MACH and other server interfaces are defined in a high-level remote procedure
  185. call language called MIG; from that definition, interfaces for C are generated.
  186. In the future, MIG may generate interfaces in other languages.  In this manual,
  187. calls are shown in the C language.
  188.  
  189.   All MACH kernel procedures return a value indicating the  success  or  reason
  190. for  failure  of that request. The errors unique to each function are described
  191. with those functions; however, since all  requests  involve  primitive  message
  192. operations, errors described in that section may also apply.
  193.  
  194.  
  195.  
  196. 1.3. User operating system environments
  197.   In  addition  to  the  facilities  provided directly by the kernel, MACH also
  198. provides for complete emulation of all 4.3bsd functions  as  described  in  the
  199. 4.3bsd  manual.   This emulation is completely transparent to user programs and
  200. requires no special libraries or other utilities.  On all VAX hardware MACH  is
  201. binary compatible with 4.3bsd.
  202.  
  203.   This  manual  does  not  reproduce  descriptions  of  the  UNIX system calls.
  204. Programmers wishing to use the functions  provided  within  these  environments
  205. should consult the relevant UNIX system manuals.
  206. 2. Message primitives
  207.  
  208.  
  209.  
  210. 2.1. Basic terms
  211.   MACH message primitives manipulate three distinct objects:
  212.  
  213.    1. ports  -  protected kernel objects to which messages may be sent and
  214.       logically queued until reception,
  215.  
  216.    2. port sets - protected kernel objects  which  combine  multiple  port
  217.       queues and from which messages may be dequeued, and
  218.  
  219.    3. messages  -  ordered collections of typed data consisting of a fixed
  220.       size message header and a variable size message body.
  221.  
  222.  
  223.  
  224. 2.2. Ports
  225.   Access rights to a port consist of the ability to send to, receive  from,  or
  226. own  that port.  A task may hold just send rights or any combination of receive
  227. and ownership rights plus send rights.  Threads within a task may only refer to
  228. ports  to  which  that  task has been given access.  When a new port is created
  229. within a task, that task is given all three access rights to that port.
  230.  
  231.   The port access rights are operationally defined as follows:
  232.  
  233.   Send access     to a port implies that a message can be sent  to  that  port.
  234.                   Should  the port be destroyed during the time a task has send
  235.                   access, a message will be sent to that  task  by  the  kernel
  236.                   indicating that the port has disappeared.
  237.  
  238.   Receive access  to  a  port  allows  a message to be dequeued from that port.
  239.                   Only one task may have receive access for a given port  at  a
  240.                   time;  however,  more  than  one  thread within that task may
  241.                   concurrently attempt to receive messages from a  given  port.
  242.                   Receive access implies send rights.
  243.  
  244.   Ownership       of  a  port implies that, should the task with receive access
  245.                   to that port  relinquish  its  receive  access,  the  receive
  246.                   access to the port will be sent to the owner task.  Likewise,
  247.                   should ownership be relinquished, the  ownership  rights  are
  248.                   sent  by the kernel to the receiving task. The name ownership
  249.                   is somewhat misleading as all it really  means  is  that  the
  250.                   task  is  a  backup reciever if the current receiver gives up
  251.                   its rights.  As with receive access, only one task  may  hold
  252.                   ownership  access  to any given port.  Ownership implies send
  253.                   rights.    NOTE:  the  ownership  abstraction  is  considered
  254.                   obsolete and has been replaced with the use of a backup port.
  255.                   This is a port associated with a primary port, to  which  the
  256.                   receive  rights of the primary port will be sent in the event
  257.                   of an attempted destruction  of  the  primary  port.  Current
  258.                   versions of MACH implement both mechanisms, but the ownership
  259.                   rights may disappear in future releases.
  260.  
  261.   Port access rights can be passed in messages.  They are  interpreted  by  the
  262. kernel  and transferred from the sender to the kernel upon message transmission
  263. and to the receiver upon  message  reception.  Send  rights  are  kept  by  the
  264. original  task  as  well as being transmitted to the receiver task, but receive
  265. rights and ownership rights are removed from the orignal task at  the  time  of
  266. the  send,  and  appear  in the user task when the receive is done.  During the
  267. time between a send and receive, the kernel holds the rights and  any  messages
  268. sent  to the port will be queued awaiting a new task to receive on the port. If
  269. the task that was intended to receive  the  rights  dies  before  it  does  the
  270. receive,  the rights are handled as though the receive had been done before the
  271. task died; that is receive rights are transferred to the owner or ownership  is
  272. transferred to the receiver.  If the receiver and owner are both dead, the port
  273. is destroyed.
  274.  
  275.   The message queue associated with a port is of finite  length  and  thus  may
  276. become  full.    Threads  may exercise several options for handling the case of
  277. message transmission to a full queue (see msg_send below).  Unless  a  specific
  278. option is set, msg_send will block until the message can be queued.
  279.  
  280.  
  281.  
  282. 2.3. Port sets
  283.   Conceptually,  a  port  set  is a bag holding zero or more receive rights.  A
  284. port set allows a thread to block waiting for a message sent to any of  several
  285. ports.  A port may be a member of at most one port set at any time.
  286.  
  287.   A  task's  port  set  right, created by port_set_allocate, allows the task to
  288. receive a message from the port set with msg_receive and  manipulate  the  port
  289. set     with     port_set_add,     port_set_remove,     port_set_status,    and
  290. port_set_deallocate.  Unlike port rights, a port set right may not be passed in
  291. messages.
  292.  
  293.  
  294.  
  295. 2.4. Port names
  296.   Every  task  has  its  own port name space, used for port and port set names.
  297. For example, one task with receive and ownership rights for a port may know the
  298. port  by the name 13, while another task with send rights for the same port may
  299. know it by the name 17.  A task only has one name for a port, so  if  the  task
  300. with send rights named 17 receives another message carrying send rights for the
  301. same port, the arriving rights will also be named 17.
  302.  
  303.   Typically  these  names  are  small  integers,  but  that  is  implementation
  304. dependent.   When a task receives a message carrying rights for a new port, the
  305. MACH kernel is free to choose any unused name.  The  port_rename  call  can  be
  306. used to change a task's name for a port.
  307.  
  308.  
  309.  
  310. 2.5. Port types
  311.   There  are  several type defintions for ports used in this manual and defined
  312. in <mach/port.h>. The type port_name_t is used to refer to a port to which  the
  313. task may have no rights. When this type is used in a message definition no port
  314. rights are sent in the message and the kernel does no  mapping  of  ports.  The
  315. type  port_set_name_t  is  used  to  refer to a port set and does not imply any
  316. rights to the set. Only port set names can be passed in messages. In  order  to
  317. pass  the  rights  to a port set, a task must pass each port separately and the
  318. receiving port must then define a new port set with consisting of those  ports.
  319. The  types  port_t, port_rcv_t and port_all_t are used to imply a port to which
  320. the task has the specified rights. Typically port_t is used for a port with any
  321. rights.  One  of  these  types  must be used in the message definition if ports
  322. rights are to be sent in the message.  All of these types are defined to be the
  323. same  basic  C  types,  so  that  they  can be used interchangeably in calls to
  324. primitives.
  325.  
  326.   Most of the MACH calls take a task or thread as their  first  argument  where
  327. this  agrument is said to be the target task/thread.  In most cases the task or
  328. thread is the one doing the call. In  those  cases  any  port_name_t  arguments
  329. represent ports to which the task has or receives rights. But in the case where
  330. task is not the caller, then the target task gets the rights but  doesn't  know
  331. the  name,  and  the  caller  gets the name but does not have any rights to the
  332. port.
  333.  
  334.  
  335.  
  336. 2.6. Messages
  337.   A message consists of a fixed header, followed by a variable amount of  data.
  338. The C type definition for the message header is as follows:
  339.  
  340.     typedef struct {
  341.                     int             :24,
  342.                                     msg_simple : 8;
  343.                     int             msg_size;
  344.                     int             msg_type;
  345.                     port_t          msg_local_port;
  346.                     port_t          msg_remote_port;
  347.                     int             msg_id;
  348.     } msg_header_t;
  349.  
  350. The  msg_local_port  and  msg_remote_port  fields are used to name the ports on
  351. which a message is to be received or sent. In the case of msg_receive this  may
  352. be  either  a  port  or a port set.  The msg_size field is used to describe the
  353. size of the message to be sent, or the maximum size of the message which can be
  354. received.   The size includes the header and inline data and is given in bytes.
  355. The msg_simple field is used to indicate that no ports or out-of-line data  are
  356. contained  in  the  body.    The  msg_id  field may be used by user programs to
  357. identify the meaning of this message to the intended recipient.
  358.  
  359.   The variable data part of a message consists of an array of  descriptors  and
  360. data. Each data descriptor is of the form:
  361.  
  362.     typedef struct  {
  363.             unsigned int    msg_type_name : 8,
  364.                                     /* What kind of data */
  365.                             msg_type_size : 8,
  366.                                     /* How many bits is each item */
  367.                             msg_type_number : 12,
  368.                                     /* How many items are there */
  369.                             msg_type_inline : 1,
  370.                                     /* If true, actual data follows;
  371.                                      *  else a pointer to the data */
  372.                             msg_type_longform : 1,
  373.                                     /* Name, size, number follow */
  374.                             msg_type_deallocate : 1;
  375.                                     /* Deallocate port rights or memory */
  376.     } msg_type_t;
  377.  
  378. msg_type_name     describes  the  basic  type  of  data comprising this object.
  379.                 There are several system-defined data types, including:
  380.  
  381.                    - Ports, including combinations of send,  receive,  and
  382.                      ownership rights,
  383.  
  384.                    - Port  and  port set names.  This is the same language
  385.                      data type  as  port  rights,  but  the  message  only
  386.                      carries  a  task's  name for a port and doesn't cause
  387.                      any transferal of rights.
  388.  
  389.                    - Simple data types, such as integers, characters,  and
  390.                      floating point values.
  391.  
  392. msg_type_size     indicates  the  size in bits of the basic object named in the
  393.                 msg_type_name field.
  394.  
  395. msg_type_number  indicates the number of items of the basic data  type  present
  396.                 after the type descriptor.
  397.  
  398. msg_type_inline   indicates  that  the  actual  data is included after the type
  399.                 descriptor; otherwise, the word following the descriptor  is  a
  400.                 pointer to the data to be sent.
  401.  
  402. msg_type_deallocate
  403.                 indicates that the port rights and/or data pointed to  in  this
  404.                 object  are  to  be  deallocated  after  the  queueing  of this
  405.                 message.  Receive and ownership rights may not  be  deallocated
  406.                 with msg_type_deallocate.
  407.  
  408. msg_type_longform
  409.                 indicates that the name, size, and number fields were too  long
  410.                 to  fit  in  the  structure described above.  Instead, the data
  411.                 type descriptor is described by the following structure:
  412.  
  413.                     typedef struct  {
  414.                             msg_type_t      msg_type_header;
  415.                             short           msg_type_long_name;
  416.                             short           msg_type_long_size;
  417.                             int             msg_type_long_number;
  418.                     } msg_type_long_t;
  419.  
  420.   A data item or a pointer to data follows each data descriptor.
  421.  
  422.   All the C types and constants needed to use the message functions are defined
  423. in <mach/message.h>. The declarations in this section are taken from this file.
  424. msg_send
  425.  
  426. #include <mach/message.h>
  427. msg_return_t msg_send(header, option, timeout)
  428.         msg_header_t    *header;
  429.         msg_option_t    option;
  430.         msg_timeout_t   timeout;
  431.  
  432.  
  433. Arguments
  434.  
  435.   header          The  address of the message to be sent. A message consists of
  436.                   a fixed sized header followed by a variable  number  of  data
  437.                   descriptors  and  data  items.  See  <mach/message.h>  for  a
  438.                   definition of the message structure.
  439.  
  440.   timeout         In the event that  the  destination  port  is  full  and  the
  441.                   SEND_TIMEOUT  option has been specified, this value specifies
  442.                   the maximum wait time (in milliseconds).
  443.  
  444.   option          The failure conditions under which msg_send should terminate;
  445.                   the  value  of  this parameter is an or'ed combination of the
  446.                   following two options.   Unless  one  of  the  two  following
  447.                   values  for  the  option  parameter  is explicitly specified,
  448.                   msg_send does not return until the  message  is  successfully
  449.                   queued for the intended receiver.
  450.  
  451.   SEND_TIMEOUT    specifies  that  the  msg_send request should terminate after
  452.                   the timeout period has elapsed, even if the kernel  has  been
  453.                   unable to queue the message.
  454.  
  455.   SEND_NOTIFY     allows  the  sender  to  give  exactly  one  message  to  the
  456.                   operating  system  without   being   suspended   should   the
  457.                   destination  port  be full.  When that message can in fact be
  458.                   posted to  the  receiving  port's  queue,  the  sending  task
  459.                   receives a message which notifies it that another message can
  460.                   be sent.  A second attempt to send a message with the  notify
  461.                   option  before  the  first notification arrives results in an
  462.                   error.  If SEND_TIMEOUT is also specified, msg_send will wait
  463.                   until  the  specified timeout has elapsed before invoking the
  464.                   SEND_NOTIFY option.
  465.  
  466.   SEND_INTERRUPT  Specifies that msg_send should return if a software interrupt
  467.                   occurs in this thread.
  468.  
  469.   MSG_OPTION_NONE A  constant defined as zero which may be used to specify that
  470.                   neither of the previous options are wanted.
  471.  
  472. Description
  473.  
  474.   msg_send transmits a message  from  the  current  task  to  the  remote  port
  475. specified  in the message header field (msg_remote_port).  The message consists
  476. of its header, followed by a variable  number  of  data  descriptors  and  data
  477. items.    (See  the  introduction  to  this  section  for  details  on  message
  478. formatting.)
  479.  
  480.   If the msg_local_port field is not set to PORT_NULL, send rights to that port
  481. will  be passed to the receiver of this message. The receiver task may use that
  482. port to send a reply to this message.
  483.  
  484.   If the SEND_NOTIFY option is used and this call  returns  a  SEND_WILL_NOTIFY
  485. code,  then  the  user  can expect to receive a notify message from the kernel.
  486. This message will either be  a  NOTIFY_MSG_ACCEPTED  or  a  NOTIFY_PORT_DELETED
  487. message  depending  on  what happened to the queued message. The first and only
  488. data item in these messages is the port to which the original message was sent.
  489. The ids and formats for these messages are defined in <mach/notify.h>.
  490.  
  491. Returns
  492.  
  493.   SEND_SUCCESS    The message has been queued for the destination port.
  494.  
  495.   SEND_INVALID_MEMORY
  496.                   The message header or body was not readable  by  the  calling
  497.                   task,  or  the  message body specified out-of-line data which
  498.                   was not readable.
  499.  
  500.   SEND_INVALID_PORT
  501.                   The  message refers to a name for which the current task does
  502.                   not have access, or to which access  was  explicitly  removed
  503.                   from the current task (see port_deallocate) while waiting for
  504.                   the message to be posted, or a  msg_type_name  field  in  the
  505.                   message  specifies rights that the name doesn't denote in the
  506.                   task (eg, specifying MSG_TYPE_SEND and supplying a port set's
  507.                   name).
  508.  
  509.   SEND_TIMED_OUT  The message was not sent since the destination port was still
  510.                   full after timeout milliseconds.
  511.  
  512.   SEND_WILL_NOTIFY
  513.                   The  destination port was full but the SEND_NOTIFY option was
  514.                   specified.  A notification message  will  be  sent  when  the
  515.                   message can be posted.
  516.  
  517.   SEND_NOTIFY_IN_PROGRESS
  518.                   The SEND_NOTIFY  option  was  specified  but  a  notification
  519.                   request  is  already  outstanding  for  this thread and given
  520.                   destination port.
  521.  
  522. See Also
  523.  
  524.   msg_receive, msg_rpc
  525. msg_receive
  526.  
  527. #include <mach/message.h>
  528. #include <mach/port.h>
  529. msg_return_t msg_receive(header, option, timeout)
  530.         msg_header_t    *header;        /* in/out */
  531.         msg_option_t    option;
  532.         msg_timeout_t   timeout;
  533.  
  534.  
  535. Arguments
  536.  
  537.   header          The address of a  buffer  in  which  the  message  is  to  be
  538.                   received.    Two  fields  of  the  message header must be set
  539.                   before the call is made:  msg_local_port is set to  the  name
  540.                   of  the  port  or  port  set  from which the message is to be
  541.                   received and msg_size must be set to the maximum size of  the
  542.                   message  that  may be received. It must be less than or equal
  543.                   to the size of the buffer.
  544.  
  545.   timeout         If RCV_TIMEOUT is specified this value is the maximum time in
  546.                   milliseconds to wait for a message before giving up.
  547.  
  548.   option          The   failure   conditions  under  which  msg_receive  should
  549.                   terminate;  the  value  of  this  parameter  is  a  bit  or'd
  550.                   combination the following two options.  Unless one of the two
  551.                   following values  for  the  option  parameter  is  explicitly
  552.                   specified,  msg_receive  does  not return until a message has
  553.                   been received.
  554.  
  555.   RCV_TIMEOUT     Specifies that msg_receive should return when  the  specified
  556.                   timeout  elapses,  if a message has not arrived by that time;
  557.                   if  not  specified,  the  timeout  will  be   ignored   (i.e.
  558.                   infinite).
  559.  
  560.   RCV_NO_SENDERS  Specifies  that msg_receive should return if the receiver and
  561.                   owner tasks have the only access rights to the port specified
  562.                   in the message header. (Not implemented yet)
  563.  
  564.   RCV_INTERRUPT   Specifies  that  msg_receive  should  return  when a software
  565.                   interrupt has occurred in this thread.
  566.  
  567.   MSG_OPTION_NONE Specifies that none of the above options are desired.
  568.  
  569. Description
  570.  
  571.   msg_receive retrieves the next message from a port or port set  specified  in
  572. the  msg_local_port  field  of  the  specified  message  header.   If a port is
  573. specified, the port may not be a member of a  port  set.    The  msg_local_port
  574. field will be set to the specific port on which the message was found.
  575.  
  576.   If  a  port  set is specified, the msg_receive will retrieve messages sent to
  577. any of the set's member ports.  It is not an error for the port set to have  no
  578. members,  or  for  members  to  be  added  and  removed from a port set while a
  579. msg_receive on the port set is in progress.
  580.  
  581.   The message consists of its header, followed by a variable  amount  of  data;
  582. the message header supplied to msg_receive must specify the maximum size of the
  583. message which can be received into the buffer provided.  (See the  introduction
  584. to this section for details on message formatting).
  585.  
  586.   If  no messages are present on the port(s) in question, msg_receive will wait
  587. until a message arrives, or until one of the specified  termination  conditions
  588. is met (see above for discussion of the option parameter).
  589.  
  590.   If  the  received  messages  contains  out-of-line  data  (i.e. for which the
  591. msg_type_inline attribute was specified as FALSE), the data will be returned in
  592. a  newly-allocated region of memory; the message body will contain a pointer to
  593. that new region.  (See vm_allocate call for  a  description  of  the  state  of
  594. newly-allocated  memory.)  The  user may wish to deallocte this memory when the
  595. data is no longer needed.
  596.  
  597. Returns
  598.  
  599.   RCV_SUCCESS     The message has been received.
  600.  
  601.   RCV_INVALID_MEMORY
  602.                   The message specified was not writable by the calling task.
  603.  
  604.   RCV_INVALID_PORT
  605.                   An attempt was made to receive on a port to which the calling
  606.                   task   does   not  have  the  proper  access,  or  which  was
  607.                   deallocated  (see  port_deallocate)  while  waiting   for   a
  608.                   message.
  609.  
  610.   RCV_TOO_LARGE   The message header and body combined are larger than the size
  611.                   specified by msg_size.
  612.  
  613.   RCV_NOT_ENOUGH_MEMORY
  614.                   The  message  to  be  received contains more out-of-line data
  615.                   than can be allocated in the receiving task.
  616.  
  617.   RCV_TIMED_OUT   The message was not received after timeout milliseconds.
  618.  
  619.   RCV_ONLY_SENDER An attempt was made to receive on a port to  which  only  the
  620.                   receive  and/or  owner  have  access,  and the RCV_NO_SENDERS
  621.                   option was specified.
  622.  
  623.   RCV_INTERRUPTED A software interrupt occurred.
  624.  
  625.   RCV_PORT_CHANGE The port specified was moved  into  a  port  set  during  the
  626.                   duration of the msg_receive call.
  627.  
  628. See Also
  629.  
  630.   msg_rpc, msg_send
  631. msg_rpc
  632.  
  633. #include <mach/message.h>
  634. #include <mach/port.h>
  635. msg_return_t msg_rpc(header, option, rcv_size,
  636.                         send_timeout, rcv_timeout)
  637.         msg_header_t    *header;        /* in/out */
  638.         msg_option_t    option;
  639.         msg_size_t      rcv_size;
  640.         msg_timeout_t   send_timeout;
  641.         msg_timeout_t   rcv_timeout;
  642.  
  643.  
  644. Arguments
  645.  
  646.   header          Address  of  a  message  buffer  which  will be used for both
  647.                   msg_send and msg_receive.  This  buffer  contains  a  message
  648.                   header  followed by the data for the message to be sent.  The
  649.                   msg_remote_port field specifies the port to which the message
  650.                   is  to  be sent.  The msg_local_port field specifies the port
  651.                   on which a message is then to be received; if  this  port  is
  652.                   the  special  value  PORT_DEFAULT, it will be replaced by the
  653.                   value PORT_NULL for the purposes of the msg_send operation.
  654.  
  655.   option          A  union  of  the  option  parameters   for   the   component
  656.                   operations.  (see msg_send and msg_receive)
  657.  
  658.   rcv_size        The  maximum size allowed for the received message; this must
  659.                   be less than or equal to the size of the message buffer.  The
  660.                   msg_size  field  in  the  header  specifies  the  size of the
  661.                   message to be sent.
  662.  
  663.   send_timeout;rcv_timeout
  664.                   The timeout values to be applied to the component operations.
  665.                   These are  only  used  if  the  options  SEND_TIMEOUT  and/or
  666.                   RCV_TIMEOUT are specified.
  667.  
  668. Description
  669.  
  670.   msg_rpc is a hybrid call which performs a msg_send followed by a msg_receive,
  671. using the same message buffer.
  672.  
  673. Returns
  674.  
  675.   RPC_SUCCESS     message was successfully sent and a reply was recived.
  676.  
  677.   FAILURES        are the same as those for msg_send and msg_receive; any error
  678.                   during the msg_send portion will terminate the call.
  679.  
  680. See Also
  681.  
  682.   msg_receive, msg_send
  683. 3. Port and port set primitives
  684. port_names
  685.  
  686. #include <mach.h>
  687.  
  688. kern_return_t port_names(task,
  689.                          port_names, port_namesCnt,
  690.                          port_types, port_typesCnt)
  691.         task_t task;
  692.         port_name_array_t *port_names;  /* out array */
  693.         unsigned int *port_namesCnt;    /* out */
  694.         port_type_array_t *port_types;  /* out array */
  695.         unsigned int *port_typesCnt;    /* out */
  696.  
  697.  
  698. Arguments
  699.  
  700.   task            The task whose port name space is queried.
  701.  
  702.   port_names      The  names of the ports and port sets in the task's port name
  703.                   space, in no particular order.
  704.  
  705.   port_namesCnt   The number of names returned.
  706.  
  707.   port_types      The type of each corresponding name.  Indicates what kind  of
  708.                   right the task holds for the port or port set.
  709.  
  710.   port_typesCnt   Should be the same as port_namesCnt.
  711.  
  712. Description
  713.  
  714.   port_names returns the currently valid ports and port set names of task.  For
  715. each name, it also returns what type  of  rights  task  holds.  port_names  and
  716. port_types  are  arrays that are automatically allocated when the reply message
  717. is received. The user may wish to vm_deallocate them when the data is no longer
  718. needed.
  719.  
  720. Returns
  721.  
  722.   KERN_SUCCESS    The call succeeded.
  723.  
  724.   KERN_INVALID_ARGUMENT
  725.                   task was invalid.
  726.  
  727. See Also
  728.  
  729.   port_type, port_status, port_set_status
  730. port_type
  731.  
  732. #include <mach.h>
  733.  
  734. kern_return_t port_type(task, port_name, port_type)
  735.         task_t task;
  736.         port_name_t port_name;
  737.         port_type_t *port_type;         /* out */
  738.  
  739.  
  740. Arguments
  741.  
  742.   task            The task whose port name space is queried.
  743.  
  744.   port_name       The name being queried.
  745.  
  746.   port_type       The type of the name.  Indicates what kind of right the  task
  747.                   holds for the port or port set.
  748.  
  749. Description
  750.  
  751.   port_type  returns information about task's rights for a specific name in its
  752. port name space.
  753.  
  754. Returns
  755.  
  756.   KERN_SUCCESS    The call succeeded.
  757.  
  758.   KERN_INVALID_ARGUMENT
  759.                   task  was  invalid  or  task  did  not  have any rights named
  760.                   port_name.
  761.  
  762. See Also
  763.  
  764.   port_names, port_status, port_set_status
  765. port_rename
  766.  
  767. #include <mach.h>
  768.  
  769. kern_return_t port_rename(task, old_name, new_name)
  770.         task_t task;
  771.         port_name_t old_name;
  772.         port_name_t new_name;
  773.  
  774.  
  775. Arguments
  776.  
  777.   task            The task whose port name space is changed.
  778.  
  779.   old_name        The name being changed.
  780.  
  781.   new_name        The new value for old_name.
  782.  
  783. Description
  784.  
  785.   port_rename changes the name by which a port or port set is  known  to  task.
  786. new_name must not already be in use, and it can't be a distinguished value like
  787. PORT_NULL.
  788.  
  789. Returns
  790.  
  791.   KERN_SUCCESS    The call succeeded.
  792.  
  793.   KERN_NAME_EXISTS
  794.                   task already has a right named new_name.
  795.  
  796.   KERN_INVALID_ARGUMENT
  797.                   task was invalid or  task  did  not  have  any  rights  named
  798.                   old_name or new_name was an invalid name.
  799.  
  800. See Also
  801.  
  802.   port_names
  803. port_allocate
  804.  
  805. #include <mach.h>
  806.  
  807. kern_return_t port_allocate(task, port_name)
  808.         task_t task;
  809.         port_name_t *port_name;         /* out */
  810.  
  811.  
  812. Arguments
  813.  
  814.   task            The task in which the new port is created.
  815.  
  816.   port_name       The task's name for the new port.
  817.  
  818. Description
  819.  
  820.   port_allocate  causes  a  port  to  be  created  for  the specified task; the
  821. resulting port's name is returned in port_name.  The target task initially  has
  822. all  three  access rights to the port.  If the caller is not the task specified
  823. by task, then it does not have any rights to the port.  The new port is  not  a
  824. member of any port set.
  825.  
  826. Returns
  827.  
  828.   KERN_SUCCESS    The call succeeded.
  829.  
  830.   KERN_INVALID_ARGUMENT
  831.                   task was invalid.
  832.  
  833.   KERN_RESOURCE_SHORTAGE
  834.                   The kernel ran out of memory.
  835.  
  836. See Also
  837.  
  838.   port_deallocate
  839. port_deallocate
  840.  
  841. #include <mach.h>
  842.  
  843. kern_return_t port_deallocate(task, port_name)
  844.         task_t task;
  845.         port_name_t port_name;
  846.  
  847.  
  848. Arguments
  849.  
  850.   task            The task from which to remove the port rights.
  851.  
  852.   port_name       task's name for the rights to be removed.
  853.  
  854. Description
  855.  
  856.   port_deallocate requests that the target task's rights for a port be removed.
  857.  
  858.   If  task  has receive rights for the port, and the port is a member of a port
  859. set, the port is removed from the port set.
  860.  
  861.   If the target task is both the receiver and owner for the port, then the port
  862. is  destroyed  and  all other tasks with send access are notified of the port's
  863. destruction. If the task is only the receiver for the port, receive rights  are
  864. sent  to the owner. If the task is only the owner of the port, ownership rights
  865. are sent to the receiver.
  866.  
  867. Returns
  868.  
  869.   KERN_SUCCESS    The call succeeded.
  870.  
  871.   KERN_INVALID_ARGUMENT
  872.                   task was invalid or port_name does not name a valid port.
  873.  
  874. See Also
  875.  
  876.   port_allocate
  877. port_status
  878.  
  879. #include <mach.h>
  880.  
  881. kern_return_t port_status(task, port_name, enabled,
  882.                           num_msgs, backlog, owner, receiver)
  883.         task_t task;
  884.         port_name_t port_name;
  885.         port_set_name_t *enabled;       /* out */
  886.         int *num_msgs;                  /* out */
  887.         int *backlog;                   /* out */
  888.         boolean_t *owner;               /* out */
  889.         boolean_t *receiver;            /* out */
  890.  
  891.  
  892. Arguments
  893.  
  894.   task            The task owning the port right in question.
  895.  
  896.   port_name       task's name for the port right.
  897.  
  898.   enabled         Returns  task's  name  for  the port set which the named port
  899.                   belongs to, or PORT_NULL if it isn't in a set.
  900.  
  901.   num_msgs        The number of messages queued on this port.
  902.  
  903.   backlog         The number of messages which  may  be  queued  to  this  port
  904.                   without causing the sender to block.
  905.  
  906.   owner           Returned as true iff the task is the owner of the port.
  907.  
  908.   receiver        Returned as true iff the task is the receive of the port.
  909.  
  910. Description
  911.  
  912.   port_status  returns  the  current  status  associated with task's port right
  913. named port_name.  If receiver  isn't  true,  then  the  enabled,  num_msg,  and
  914. backlog arguments don't return anything meaningful.
  915.  
  916. Returns
  917.  
  918.   KERN_SUCCESS    The call succeeded.
  919.  
  920.   KERN_INVALID_ARGUMENT
  921.                   task was invalid or port_name does not name a valid port.
  922.  
  923. See Also
  924.  
  925.   port_set_backlog, port_set_status
  926. port_set_backlog
  927.  
  928. #include <mach.h>
  929.  
  930. kern_return_t port_set_backlog(task, port_name, backlog)
  931.         task_t task;
  932.         port_name_t port_name;
  933.         int backlog;
  934.  
  935.  
  936. Arguments
  937.  
  938.   task            The task owning the named port right.
  939.  
  940.   port_name       task's name for the port right.
  941.  
  942.   backlog         The new backlog to be set.
  943.  
  944. Description
  945.  
  946.   The port's backlog value is  the  number  of  unreceived  messages  that  are
  947. allowed  in  its message queue before the kernel will refuse to accept any more
  948. sends to that  port.    port_set_backlog  changes  the  backlog  value  on  the
  949. specified port.
  950.  
  951.   task must have receive rights for the named port.
  952.  
  953.   The  file  <mach/mach_param.h>  exports the system default value for a port's
  954. backlog as the constant PORT_BACKLOG_DEFAULT and the maximum backlog  value  as
  955. the constant PORT_BACKLOG_MAX.
  956.  
  957. Returns
  958.  
  959.   KERN_SUCCESS    The call succeeded.
  960.  
  961.   KERN_NOT_RECEIVER
  962.                   port_name doesn't name receive rights in task.
  963.  
  964.   KERN_INVALID_ARGUMENT
  965.                   task  was  invalid or port_name does not name a valid port or
  966.                   the desired backlog was non-positive or the  desired  backlog
  967.                   was greater than PORT_BACKLOG_MAX.
  968.  
  969. See Also
  970.  
  971.   msg_send, port_status
  972. port_set_backup
  973.  
  974. #include <mach.h>
  975.  
  976. kern_return_t port_set_backup(task, primary, backup, previous)
  977.         task_t task;
  978.         port_name_t primary;
  979.         port_t backup;
  980.         port_t *previous;       /* out */
  981.  
  982.  
  983. Arguments
  984.  
  985.   task            The task owning the named port right.
  986.  
  987.   primary         task's name for the primary port.
  988.  
  989.   backup          The new backup port to be set.
  990.  
  991.   previous        The previous backup port.
  992.  
  993. Description
  994.  
  995.   A  backup port provides a automatic mechanism to transfer port receive rights
  996. to another task or thread in the event of a primary port's attempted death.  To
  997. be  more  precise,  if  a primary port has a backup port, and the primary would
  998. have been destroyed by the deallocation of its receive rights, then instead the
  999. receive   right   for   the   primary   port   is  sent  in  a  notify  message
  1000. (NOTIFY_PORT_DESTROYED) to the backup port.
  1001.  
  1002.   A newly allocated port does not have a backup port. The port_set_backup  call
  1003. changes  the  backup  of  the  primary  port. The target task must hold receive
  1004. rights for the primary port. The caller supplies send rights for the new backup
  1005. port  to  which  notification will be sent. The caller receives send rights for
  1006. the previous backup port or PORT_NULL if the target  did  not  have  a  backup.
  1007. port_set_backup  works  atomically, so that if one backup port is exchanged for
  1008. another, the primary port is never left without a backup.
  1009.  
  1010.   When the primary port is sent in a notify message to  the  backup  port,  the
  1011. primary  port  is  left  without  a  backup  port.   When the task receives the
  1012. notification and the receive rights to the primary port, it  may  wish  to  use
  1013. port_set_backup  to  reestablish  the  same  or a different backup port. If the
  1014. backup port is destroyed before the primary, then  the  primary  port  is  left
  1015. without a backup. (A subsequent port_set_backup call would return PORT_NULL).
  1016.  
  1017. Returns
  1018.  
  1019.   KERN_SUCCESS    The call succeeded.
  1020.  
  1021.   KERN_NOT_RECEIVER
  1022.                   primary doesn't name receive rights in task.
  1023.  
  1024.   KERN_INVALID_ARGUMENT
  1025.                   task  was  invalid  or  primary or backup do not name a valid
  1026.                   port.
  1027.  
  1028. See Also
  1029.  
  1030.   port_deallocate
  1031. port_set_allocate
  1032.  
  1033. #include <mach.h>
  1034.  
  1035. kern_return_t port_set_allocate(task, set_name)
  1036.         task_t task;
  1037.         port_set_name_t *set_name;      /* out */
  1038.  
  1039.  
  1040. Arguments
  1041.  
  1042.   task            The task in which the new port set is created.
  1043.  
  1044.   set_name        The task's name for the new port set.
  1045.  
  1046. Description
  1047.  
  1048.   port_set_allocate causes a port set to be created for the specified task; the
  1049. resulting set's name is returned in set_name.  The new port set is empty.
  1050.  
  1051. Returns
  1052.  
  1053.   KERN_SUCCESS    The call succeeded.
  1054.  
  1055.   KERN_INVALID_ARGUMENT
  1056.                   task was invalid.
  1057.  
  1058.   KERN_RESOURCE_SHORTAGE
  1059.                   The kernel ran out of memory.
  1060.  
  1061. See Also
  1062.  
  1063.   port_set_deallocate
  1064. port_set_deallocate
  1065.  
  1066. #include <mach.h>
  1067.  
  1068. kern_return_t port_set_deallocate(task, set_name)
  1069.         task_t task;
  1070.         port_set_name_t set_name;
  1071.  
  1072.  
  1073. Arguments
  1074.  
  1075.   task            The task owning the port set to be destroyed.
  1076.  
  1077.   set_name        task's name for the doomed port set.
  1078.  
  1079. Description
  1080.  
  1081.   port_set_deallocate requests that the target task's port set be destroyed.
  1082.  
  1083.   If the port set is non-empty, any members are first removed.
  1084.  
  1085. Returns
  1086.  
  1087.   KERN_SUCCESS    The call succeeded.
  1088.  
  1089.   KERN_FAILURE    set_name  is  task's  port  set  used  for  implementing  the
  1090.                   obsolete port_enable and port_disable calls.
  1091.  
  1092.   KERN_INVALID_ARGUMENT
  1093.                   task was invalid or set_name does not name a valid port set.
  1094.  
  1095. See Also
  1096.  
  1097.   port_set_allocate
  1098. port_set_add
  1099.  
  1100. #include <mach.h>
  1101.  
  1102. kern_return_t port_set_add(task, set_name, port_name)
  1103.         task_t task;
  1104.         port_set_name_t set_name;
  1105.         port_name_t port_name;
  1106.  
  1107.  
  1108. Arguments
  1109.  
  1110.   task            The task owning the port set and port right.
  1111.  
  1112.   set_name        task's name for the port set.
  1113.  
  1114.   port_name       task's name for the port.
  1115.  
  1116. Description
  1117.  
  1118.   port_set_add  moves  the  named port into the named port set.  task must have
  1119. receive rights for the port.
  1120.  
  1121.   If the port is already a member of another port set, it is removed from  that
  1122. set first.
  1123.  
  1124. Returns
  1125.  
  1126.   KERN_SUCCESS    The call succeeded.
  1127.  
  1128.   KERN_NOT_RECEIVER
  1129.                   port_name doesn't name receive rights in task.
  1130.  
  1131.   KERN_INVALID_ARGUMENT
  1132.                   task  was  invalid or set_name does not name a valid port set
  1133.                   or port_name does not name a valid port.
  1134.  
  1135. See Also
  1136.  
  1137.   port_set_remove
  1138. port_set_remove
  1139.  
  1140. #include <mach.h>
  1141.  
  1142. kern_return_t port_set_remove(task, port_name)
  1143.         task_t task;
  1144.         port_name_t port_name;
  1145.  
  1146.  
  1147. Arguments
  1148.  
  1149.   task            The task owning the receive rights and port set.
  1150.  
  1151.   port_name       task's name for the receive rights to be removed.
  1152.  
  1153. Description
  1154.  
  1155.   port_set_remove removes the named port from a  port  set.    task  must  have
  1156. receive rights for the port, and the port must be a member of a port set.
  1157.  
  1158. Returns
  1159.  
  1160.   KERN_SUCCESS    The call succeeded.
  1161.  
  1162.   KERN_NOT_RECEIVER
  1163.                   port_name doesn't name receive rights in task.
  1164.  
  1165.   KERN_NOT_IN_SET The port isn't a member of a set.
  1166.  
  1167.   KERN_INVALID_ARGUMENT
  1168.                   task was invalid or port_name does not name a valid port.
  1169.  
  1170. See Also
  1171.  
  1172.   port_set_add
  1173. port_set_status
  1174.  
  1175. #include <mach.h>
  1176.  
  1177. kern_return_t port_set_status(task, set_name, members, membersCnt)
  1178.         task_t task;
  1179.         port_set_name_t set_name;
  1180.         port_name_array_t *members;     /* out array */
  1181.         unsigned int *membersCnt;       /* out */
  1182.  
  1183.  
  1184. Arguments
  1185.  
  1186.   task            The task whose port set is queried.
  1187.  
  1188.   set_name        task's name for the port set.
  1189.  
  1190.   members         task's names for the port set's members.
  1191.  
  1192.   membersCnt      The number of port names returned.
  1193.  
  1194. Description
  1195.  
  1196.   port_set_status  returns the members of a port set.  members is an array that
  1197. is automatically allocated when the reply message is  received.  The  user  may
  1198. wish to vm_deallocate it when the data is no longer needed.
  1199.  
  1200. Returns
  1201.  
  1202.   KERN_SUCCESS    The call succeeded.
  1203.  
  1204.   KERN_INVALID_ARGUMENT
  1205.                   task was invalid or set_name does not name a valid port set.
  1206.  
  1207. See Also
  1208.  
  1209.   port_status
  1210. port_insert
  1211.  
  1212. #include <mach.h>
  1213.  
  1214. kern_return_t port_insert_send(task, my_port, his_name)
  1215.         task_t task;
  1216.         port_t my_port;
  1217.         port_name_t his_name;
  1218.  
  1219. kern_return_t port_insert_receive(task, my_port, his_name)
  1220.         task_t task;
  1221.         port_t my_port;
  1222.         port_name_t his_name;
  1223.  
  1224.  
  1225. Arguments
  1226.  
  1227.   task            The task getting the new rights.
  1228.  
  1229.   my_port         Rights supplied by the caller.
  1230.  
  1231.   his_name        The name by which task will know the new rights.
  1232.  
  1233. Description
  1234.  
  1235.   port_insert_send and port_insert_receive give a task rights with  a  specific
  1236. name.    If  task already has rights named his_name, or has some other name for
  1237. my_port, then the operation will fail.  his_name can't be a distinguished value
  1238. like PORT_NULL.
  1239.  
  1240.   port_insert_send inserts send rights, and port_insert_receive inserts receive
  1241. and ownership rights.
  1242.  
  1243. Returns
  1244.  
  1245.   KERN_SUCCESS    The call succeeded.
  1246.  
  1247.   KERN_NAME_EXISTS
  1248.                   task already has a right named his_name.
  1249.  
  1250.   KERN_FAILURE    task already has rights to my_port.
  1251.  
  1252.   KERN_INVALID_ARGUMENT
  1253.                   task was invalid or new_name was an invalid name.
  1254.  
  1255. Notes
  1256.  
  1257.   There is no way to insert just receive rights or just ownership rights.
  1258.  
  1259. See Also
  1260.  
  1261.   port_extract_send, port_extract_receive
  1262. port_extract
  1263.  
  1264. #include <mach.h>
  1265.  
  1266. kern_return_t port_extract_send(task, his_name, his_port)
  1267.         task_t task;
  1268.         port_name_t his_name;
  1269.         port_t *his_port;               /* out */
  1270.  
  1271. kern_return_t port_extract_receive(task, his_name, his_port)
  1272.         task_t task;
  1273.         port_name_t his_name;
  1274.         port_t *his_port;               /* out */
  1275.  
  1276.  
  1277. Arguments
  1278.  
  1279.   task            The task whose rights the caller takes.
  1280.  
  1281.   his_name        The name by which task knows the rights.
  1282.  
  1283.   his_port        Rights returned to the caller.
  1284.  
  1285. Description
  1286.  
  1287.   port_extract_send and port_extract_receive remove task's rights  for  a  port
  1288. and return the rights to the caller.  task is left with no rights for the port.
  1289.  
  1290.   port_extract_send  extracts send rights; task can't have receive or ownership
  1291. rights for the named port.    port_extract_receive  extracts  receive/ownership
  1292. rights, both of which task must hold.
  1293.  
  1294. Returns
  1295.  
  1296.   KERN_SUCCESS    The call succeeded.
  1297.  
  1298.   KERN_INVALID_ARGUMENT
  1299.                   task was invalid or his_name does not name a port  for  which
  1300.                   task has the required rights.
  1301.  
  1302. Notes
  1303.  
  1304.   There is no way to extract just receive rights or just ownership rights.
  1305.  
  1306. See Also
  1307.  
  1308.   port_insert_send, port_insert_receive
  1309. 4. Task and thread primitives
  1310.  
  1311.  
  1312.  
  1313. 4.1. Basic terms
  1314.   The  MACH  system  separates  the  traditional  notion  of a process into two
  1315. subconcepts:
  1316.  
  1317.    - Tasks contain the capabilities,  namely  the  port  rights,  resource
  1318.      limits,  and  address  space  of  a running entity.  Tasks perform no
  1319.      computation; they are a framework for running threads.
  1320.  
  1321.    - Threads contain  the  minimal  processing  state  associated  with  a
  1322.      computation,  e.g.  a  program counter, a stack pointer, and a set of
  1323.      registers.  A thread exists within exactly  one  task;  however,  one
  1324.      task may contain many threads.
  1325.  
  1326.   Tasks  are  the  basic  unit  of  protection.  All threads within a task have
  1327. access to all of that task's capabilities, and are thus not protected from each
  1328. other.
  1329.  
  1330.   Threads are the basic unit of scheduling.  On a multiprocessor host, multiple
  1331. threads from one task may be executing simultaneously (within  the  task's  one
  1332. address space).  A thread may be in a suspended state (prevented from running),
  1333. or in a runnable state (may be running or be scheduled to run).    There  is  a
  1334. non-negative  suspend  count associated with each thread.  The suspend count is
  1335. zero for runnable threads and positive for suspended threads.
  1336.  
  1337.   Tasks may be suspended or resumed as a whole.  A thread may only execute when
  1338. both  it  and  its  task  are  runnable.    Resuming  a task does not cause all
  1339. component threads to begin executing, but only  those  threads  which  are  not
  1340. suspended.
  1341.  
  1342.   Both  tasks  and threads are represented by ports. These ports are called the
  1343. task kernel port and the thread kernel port. These are  the  handles  that  are
  1344. used  in  the task and thread kernel calls to identify to the kernel which task
  1345. or thread is to be affected by the call. The  two  primitives  task_self()  and
  1346. thread_self()  return  the  task  and  thread  ports of the currently executing
  1347. thread. Tasks may have access to the task and thread ports of other  tasks  and
  1348. threads. For example, a task that creates another task or thread gets access to
  1349. the new task or thread port.  Also any thread may pass access to these ports in
  1350. a  message to another thread in the same or different task.  Having access to a
  1351. task or thread port enables the possessor to perform kernel calls on behalf  of
  1352. that  task or thread.  Access to a task's kernel port indirectly permits access
  1353. to all threads within that task via the task_threads call; however, access to a
  1354. thread's kernel port does not currently imply access to its task port.
  1355.  
  1356.   In addition to their kernel ports, tasks and threads have a number of special
  1357. ports associated with them. In general these are ports  that  the  kernel  must
  1358. know  about  in  order  to  communicate with the task or thread in a structured
  1359. manner.
  1360.  
  1361.   There are three ports associated with a task in addition to its kernel port:
  1362.  
  1363.    - The notify  port,  on  which  the  task  should  attempt  to  receive
  1364.      notification  of  such  kernel events as the destruction of a port to
  1365.      which it has send rights.  The task has receive rights to  this  port
  1366.      and can get its value from the primitive task_notify().
  1367.  
  1368.    - The  exception  port,  to  which  the  kernel  sends messages when an
  1369.      exception occurs.  Exceptions are  synchronous  interuptions  to  the
  1370.      normal  flow  of  program  control caused by the program itself. They
  1371.      include illegal memory accesses,  protection  violations,  arithmetic
  1372.      exceptions,  and hardware instructions intended to support emulation,
  1373.      debugging and/or  error  detection.  Some  of  these  exceptions  are
  1374.      handled  transparently  by  the  operating  system  but  some must be
  1375.      reported to the user program.  A default exception port is  inherited
  1376.      from  the  parent  at task creation time. This port can be changed by
  1377.      the task or any one of its threads in order to take an active role in
  1378.      handling exceptions.
  1379.  
  1380.    - The  bootstrap port, to which a new task can send a message that will
  1381.      return any other system  service  ports  that  the  task  needs,  for
  1382.      example  a port to the Network Nameserver or the Environment Manager.
  1383.      Send rights to this port  are  inherited  from  the  parent  at  task
  1384.      creation.    This  is  the one port that the kernel does not actually
  1385.      use, it just makes it available to a new task.
  1386.  
  1387.   There are two ports associated with a thread in addition to its kernel port:
  1388.  
  1389.    - The thread reply port, which may be used for initial messages from  a
  1390.      parent  or  for  early  remote  procedure  calls.  The thread_reply()
  1391.      primitive returns receive rights to this port.
  1392.  
  1393.    - The thread exception port, to which kernel sends exceptions  occuring
  1394.      in  this thread. This port is set to PORT_NULL at thread creation and
  1395.      can be set subsequently by the call  thread_set_exception_port.    As
  1396.      long  as  the  thread  exception port is PORT_NULL the task exception
  1397.      port will be used instead.
  1398.  
  1399.  
  1400.  
  1401. 4.2. Access to Tasks: Terminology
  1402.   In this and following sections, calls are described which may manipulate  the
  1403. state  of  a  task.    Although  some of the descriptions may refer to tasks as
  1404. performing these calls, it is in fact some thread within a task which makes any
  1405. call.
  1406.  
  1407.   Furthermore,  any  thread  within  any task which holds access rights to that
  1408. task (i.e. task kernel port)  may  perform  calls  which  take  a  task  as  an
  1409. argument.   Customarily, only threads within a task will manipulate that task's
  1410. state, but this custom is not enforced by the MACH kernel. Debugger tasks are a
  1411. notable exception to this rule.  Similarly, access to a thread is controlled by
  1412. access to its thread kernel port.
  1413. task_create
  1414.  
  1415. #include <mach.h>
  1416.  
  1417. kern_return_t task_create(parent_task, inherit_memory,
  1418.                                  child_task)
  1419.         task_t          parent_task
  1420.         boolean_t       inherit_memory;
  1421.         task_t          *child_task;    /* out */
  1422.  
  1423.  
  1424. Arguments
  1425.  
  1426.   target_task     The task from which the child's capabilities are drawn.
  1427.  
  1428.   inherit_memory  If set, the child task's address  space  is  built  from  the
  1429.                   parent  task  according  to  its  memory  inheritance values;
  1430.                   otherwise, the child task is given an empty address space.
  1431.  
  1432.   child_task      The new task.
  1433.  
  1434. Description
  1435.  
  1436.   task_create  creates  a  new  task  from  parent_task;  the  resulting   task
  1437. (child_task) acquires shared or copied parts of the parent's address space (see
  1438. vm_inherit).  The child task initially contains no threads.
  1439.  
  1440.   The child task gets the four special ports created or copied for it  at  task
  1441. creation.  The  task_kernel_port is created and send rights for it are given to
  1442. the child and returned to the  caller.  The  task_notify_port  is  created  and
  1443. receive,  ownership  and  send rights for it are given to the child. The caller
  1444. has no access to it.  The task_bootstrap_port and the  task_exception_port  are
  1445. inherited  from  the  parent  task.   The new task can get send rights to these
  1446. ports with the call task_get_special_port.
  1447.  
  1448. Returns
  1449.  
  1450.   KERN_SUCCESS    A new task has been created.
  1451.  
  1452.   KERN_INVALID_ARGUMENT
  1453.                   parent_task is not a valid task port.
  1454.  
  1455.   KERN_RESOURCE_SHORTAGE
  1456.                   Some critical kernel resource is unavailable.
  1457.  
  1458. See Also
  1459.  
  1460.   task_terminate, task_suspend, task_resume, task_special_ports,  task_threads,
  1461. thread_create, thread_resume, vm_inherit
  1462.  
  1463. Notes
  1464.  
  1465.   Not implemented yet. Use fork.
  1466. task_terminate
  1467.  
  1468. #include <mach.h>
  1469.  
  1470. kern_return_t task_terminate(target_task)
  1471.         task_t          target_task;
  1472.  
  1473.  
  1474. Arguments
  1475.  
  1476.   target_task     The task to be destroyed.
  1477.  
  1478. Description
  1479.  
  1480.   task_terminate  destroys  the  task  specified  by  target_task  and  all its
  1481. threads. All resources that are used only by this task are freed. Any  port  to
  1482. which this task has receive and ownership rights is destroyed.
  1483.  
  1484. Returns
  1485.  
  1486.   KERN_SUCCESS    The task has been killed.
  1487.  
  1488.   KERN_INVALID_ARGUMENT
  1489.                   target_task is not a task.
  1490.  
  1491. See Also
  1492.  
  1493.   task_create, task_suspend, task_resume, thread_terminate, thread_suspend
  1494.  
  1495. Notes
  1496.  
  1497.   Not implemented yet.
  1498. task_suspend
  1499.  
  1500. #include <mach.h>
  1501.  
  1502. kern_return_t task_suspend(target_task)
  1503.         task_t          target_task;
  1504.  
  1505.  
  1506. Arguments
  1507.  
  1508.   target_task     The task to be suspended.
  1509.  
  1510. Description
  1511.  
  1512.   Increments the task's suspend count and stops all threads  in  the  task.  As
  1513. long as the suspend count is positive newly created threads will not run.  This
  1514. call does not return until all threads are suspended.
  1515.  
  1516.   The count may become greater than one, with the effect that it will take more
  1517. than one resume call to restart the task.
  1518.  
  1519. Returns
  1520.  
  1521.   KERN_SUCCESS    The task has been suspended.
  1522.  
  1523.   KERN_INVALID_ARGUMENT
  1524.                   target_task is not a task.
  1525.  
  1526. See Also
  1527.  
  1528.   task_create, task_terminate, task_resume, task_info,  thread_suspend
  1529. task_resume
  1530.  
  1531. #include <mach.h>
  1532.  
  1533. kern_return_t task_resume(target_task)
  1534.         task_t          target_task;
  1535.  
  1536.  
  1537. Description
  1538.  
  1539.   Decrements the task's suspend count. If it becomes  zero,  all  threads  with
  1540. zero suspend counts in the task are resumed. The count may not become negative.
  1541.  
  1542. Arguments
  1543.  
  1544.   target_task     The task to be resumed.
  1545.  
  1546. Returns
  1547.  
  1548.   KERN_SUCCESS    The task has been resumed.
  1549.  
  1550.   KERN_FAILURE    The suspend count is already at zero.
  1551.  
  1552.   KERN_INVALID_ARGUMENT
  1553.                   target_task is not a task.
  1554.  
  1555. See Also
  1556.  
  1557.   task_create,   task_terminate,   task_suspend,   task_info,   thread_suspend,
  1558. thread_resume, thread_info
  1559. task_special_ports
  1560.  
  1561. #include <mach.h>
  1562.  
  1563. kern_return_t task_get_special_port(task, which_port, special_port)
  1564.         task_t          task;
  1565.         int             which_port;
  1566.         port_t          *special_port;  /* out */
  1567.  
  1568. kern_return_t task_set_special_port(task, which_port, special_port)
  1569.         task_t          task;
  1570.         int             which_port;
  1571.         port_t          special_port;
  1572.  
  1573.  
  1574. task_t task_self()
  1575.  
  1576. port_t task_notify()
  1577.  
  1578.  
  1579.  
  1580. Arguments
  1581.  
  1582.   task            The task for which to get the port
  1583.  
  1584.   which_port      the  port  that  is  requested.  Is  one of TASK_NOTIFY_PORT,
  1585.                   TASK_BOOTSTRAP_PORT, TASK_EXCEPTION_PORT.
  1586.  
  1587.   special_port    the value of the port that is being requested or being set.
  1588.  
  1589. Description
  1590.  
  1591.   get_special_port returns send rights to one of a set of special ports for the
  1592. task  specified  by  task.  In the case of the task's own task_notify_port, the
  1593. task also gets receive and ownership rights.
  1594.  
  1595.   set_special_port sets one of a set of special ports for the task specified by
  1596. task.
  1597.  
  1598.   task_self  returns the port to which kernel calls for the currently executing
  1599. thread should be directed.  Currently, task_self returns the task  kernel  port
  1600. which  is  a  port for which the kernel has receive rights and which it uses to
  1601. identify a task. In the future it may be possible for one task to  interpose  a
  1602. port as another's task's kernel port. At that time, task_self will still return
  1603. the port to which the executing thread should direct kernel calls, but  it  may
  1604. no longer be a port on which the kernel has receive rights.
  1605.  
  1606.   If  one  task,  the controller, has send access to the kernel port of another
  1607. task, the subject task, then the controller task can perform kernel  operations
  1608. for  the  subject task. Normally only the task itself and the task that created
  1609. it will have access to the task kernel port, but any task may  pass  rights  to
  1610. its kernel port to any other task.
  1611.  
  1612.   task_notify  returns  receive,  ownership  and send rights to the notify port
  1613. associated with the task to which the executing thread belongs. The notify port
  1614. is  a  port on which the task should receive notification of such kernel events
  1615. of the destruction of a port to which it has send rights.
  1616.  
  1617.   The other special ports associated with a task are the bootstrap port and the
  1618. exception  port.  The  bootstrap  port  is  a port to which a thread may send a
  1619. message requesting other system service ports. This port is  not  used  by  the
  1620. kernel. The task's exception port is the port to which messages are sent by the
  1621. kernel when an exception occurs and the thread causing  the  exception  has  no
  1622. exception port of its own.
  1623.  
  1624.   Within the C environment, task_self and task_notify are implemented as macros
  1625. which execute the system traps the first time and thereafter  return  a  cached
  1626. value  for  the  ports.  Thus it is unnecessary for a programmer to cache these
  1627. variables himself and such caching may interfere with the future implementation
  1628. of port interposition.
  1629.  
  1630.   The  following  macros  to call task_set/get_special_port for a specific port
  1631. are    defined    in     <mach/task_special_ports.h>:     task_get_notify_port,
  1632. task_set_notify_port,     task_get_exception_port,     task_set_exception_port,
  1633. task_get_bootstrap_port and task_set_bootstrap_port.
  1634.  
  1635. Returns
  1636.  
  1637.   KERN_SUCCESS    The port was returned or set.
  1638.  
  1639.   KERN_INVALID_ARGUMENT
  1640.                   task is not a task or which_port is an invalid port selector.
  1641.  
  1642. See Also
  1643.  
  1644.   thread_special_ports,mach_init,task_create
  1645.  
  1646. Notes
  1647.  
  1648.   The  call  on  the  bootstrap  port  to get system service ports has not been
  1649. implemented yet.
  1650.  
  1651.   TASK_KERNEL_PORT may be added to the set of ports that  task_set_special_port
  1652. accepts.
  1653. task_info
  1654.  
  1655. #include <mach.h>
  1656.  
  1657. /* the definition of task_info_t from mach.h - mach/task_info.h is */
  1658.  
  1659. typedef int     *task_info_t;           /* variable length array of int */
  1660.  
  1661. /* currently the only interpretation of info is */
  1662.  
  1663.    struct task_basic_info {
  1664.         int             suspend_count;  /* suspend count for task */
  1665.         vm_size_t       virtual_size;   /* number of virtual pages */
  1666.         vm_size_t       resident_size;  /* number of resident pages */
  1667.         time_value_t    user_time;      /* total user run time for
  1668.                                            terminated threads */
  1669.         time_value_t    system_time;    /* total system run time for
  1670.                                            terminated threads */
  1671.    };
  1672. typedef struct task_basic_info          *task_basic_info_t;
  1673.  
  1674. kern_return_t task_info(target_task, flavor, task_info, task_infoCnt)
  1675.         task_t          target_task;
  1676.         int             flavor;
  1677.         task_info_t     task_info;      /* in and out */
  1678.         unsigned int    *task_infoCnt;  /* in and out */
  1679.  
  1680.  
  1681. Arguments
  1682.  
  1683.   target_task     The task to be affected.
  1684.  
  1685.   flavor          The  type  of  statistics  that  are  wanted.  Currently only
  1686.                   TASK_BASIC_INFO is implemented.
  1687.  
  1688.   task_info       Statistics about the task specified by target_task.
  1689.  
  1690.   task_infoCnt    Size    of    the    info    structure.    Currently     only
  1691.                   TASK_BASIC_INFO_COUNT is implemented.
  1692.  
  1693. Description
  1694.  
  1695.   Returns  the  selected  information array for a task, as specified by flavor.
  1696. task_info is an array of integers that is supplied by the  caller,  and  filled
  1697. with  specified  information. task_infoCnt is supplied as the maximum number of
  1698. integers in task_info. On return, it contains the actual number of integers  in
  1699. task_info.
  1700.  
  1701.   Currently  there  is  only  one  flavor  of  information  which is defined by
  1702. TASK_BASIC_INFO. Its size is defined by TASK_BASIC_INFO_COUNT.
  1703.  
  1704. Returns
  1705.  
  1706.   KERN_SUCCESS    The call succeeded.
  1707.  
  1708.   KERN_INVALID_ARGUMENT
  1709.                   target_task is not a task or flavor is not recognized.
  1710.  
  1711.   MIG_ARRAY_TOO_LARGE
  1712.                   Returned info array is too large for task_info. task_info  is
  1713.                   filled  as  much  as  possible.    task_infoCnt is set to the
  1714.                   number of elements that  would  be  returned  if  there  were
  1715.                   enough room.
  1716.  
  1717. See Also
  1718.  
  1719.   task_special_ports, task_threads, thread_info, thread_state
  1720. task_threads
  1721.  
  1722. #include <mach.h>
  1723.  
  1724. kern_return_t task_threads(target_task, thread_list, thread_count)
  1725.         task_t          target_task;
  1726.         thread_array_t  *thread_list;   /* out, ptr to array */
  1727.         int             *thread_count;  /* out */
  1728.  
  1729.  
  1730. Arguments
  1731.  
  1732.   target_task     The task to be affected.
  1733.  
  1734.   thread_list     The   set   of   threads  contained  within  target_task;  no
  1735.                   particular ordering is guaranteed.
  1736.  
  1737.   thread_count    The number of threads in the thread_list.
  1738.  
  1739. Description
  1740.  
  1741.   task_threads gets send rights to the kernel port for each thread contained in
  1742. target_task.  thread_list is an array that is created as a result of this call.
  1743. The caller may wish to vm_deallocate this array when  the  data  is  no  longer
  1744. needed.
  1745.  
  1746. Returns
  1747.  
  1748.   KERN_SUCCESS    The call succeeded.
  1749.  
  1750.   KERN_INVALID_ARGUMENT
  1751.                   target_task is not a task.
  1752.  
  1753. See Also
  1754.  
  1755.   thread_create, thread_terminate, thread_suspend
  1756. thread_create
  1757.  
  1758. #include <mach.h>
  1759.  
  1760. kern_return_t thread_create(parent_task, child_thread)
  1761.         task_t          parent_task;
  1762.         thread_t        *child_thread;  /* out */
  1763.  
  1764.  
  1765. Description
  1766.  
  1767.   thread_create creates a new thread within the task specified by  parent_task.
  1768. The  new thread has no processor state, and has a suspend count of 1.  To get a
  1769.  
  1770.