
#include "V.h"
/*
* Name
*   pipe - create an interprocess channel
*
* Syntax
*   #include <limits.h>	-- On Ultrix.
*      pipe(fildes)
*      int fildes[2];
*
* Arguments
*
*   fildes     Passing an address as an array of two integers into the
*              pipe system call.
*
* Description
*   The pipe system call creates an I/O mechanism called a pipe.   The
*   file   descriptors   returned  can  be  used  in  read  and  write
*   operations.  Their integer values will be the two lowest available
*   at  the  time  of  the  pipe  function  call.   The O_NONBLOCK and
*   FD_CLOEXEC flags will be clear on both file descriptors.
*
*   When the pipe is written using the  descriptor  fildes[1],  up  to
*   PIPE_MAX  bytes of data are buffered before the writing process is
*   suspended.  A read using the descriptor  fildes[0]  picks  up  the
*   data.
*
*   It  is  assumed that after the pipe has been set up, two (or more)
*   cooperating processes (created by subsequent fork calls) pass data
*   through the pipe with read and write calls.
*
*   The  shell  has  a  syntax  to  set up a linear array of processes
*   connected by pipes.
*
*   For further information on how read and write  calls  behave  with
*   pipes, see the read(2) and write(2) reference pages.
*
*   A  signal  is  generated if a write on a pipe with only one end is
*   attempted.
*
* Restrictions
*   Should more than 4096 bytes be necessary in any pipe among a  loop
*   of processes, deadlock may occur.
*
*   The underlying implementation of pipes is  socket  based  on  some
*   (BSD)  versions  of  Unix, and not on others (SYS/V, Ultrix).  Any
*   application that needs socket functionality from pipes should  use
*   the socketpair system call, to be on the safe side.
*
* Return Values
*   The function value zero is returned if the pipe was created; -1 if
*   an error occurred.
*
* Diagnostics
*   The pipe call fails if:
*
*   [EMFILE]  Too many descriptors are active.
*
*   [ENFILE]  Too many open files in system.
*
*   [ENFILE]  The system file table is full.
*
*   [EFAULT]  The fildes buffer is in an invalid area of the process's
*             address space.
*
* See Also
*   sh(1), fork(2), read(2), socketpair(2), write(2)
*/
d_pipe(fd,msg)
	int  fd[2];
	char*msg;	/* Why does caller want the pipe? */
{	int p;
	Fname("d_pipe");
	V6 "pipe(%08X)",fd D;
	errno = 0;
	p = pipe(fd);
	V6 "pipe()=%d read=%d write=%d [Err %d=%s=%s]",p,fd[0],fd[1],Errinfo D;
	if (p == 0) {	/* Success */
		d_newfile(File_LOCAL|File_PIPE,fd[0],"pipe-r",msg);
		d_newfile(File_LOCAL|File_PIPE,fd[1],"pipe-w",msg);
	} else {
		P2 m_access,pname,"create",msg,"pipe",Errinfo D;
	}
	Return(p);
}
