Logo Search packages:      
Sourcecode: condor version File versions  Download package

int DaemonCore::Create_Pipe ( int *  pipe_ends,
bool  can_register_read = false,
bool  can_register_write = false,
bool  nonblocking_read = false,
bool  nonblocking_write = false,
unsigned int  psize = 4096 
)

Create an anonymous pipe.

Definition at line 1483 of file daemon_core.cpp.

References MyString::sprintf(), and MyString::Value().

Referenced by Create_Process().

{
      dprintf(D_DAEMONCORE,"Entering Create_Pipe()\n");

      PipeHandle read_handle, write_handle;

#ifdef WIN32
      DWORD overlapped_read_flag = 0, overlapped_write_flag = 0;
      if (can_register_read) {
            overlapped_read_flag = FILE_FLAG_OVERLAPPED;
      }
      if (can_register_write || nonblocking_write) {
            overlapped_write_flag = FILE_FLAG_OVERLAPPED;
      }

      static unsigned pipe_counter = 0;
      MyString pipe_name;
      pipe_name.sprintf("\\\\.\\pipe\\condor_pipe_%u_%u", GetCurrentProcessId(), pipe_counter++);
      HANDLE w =
            CreateNamedPipe(pipe_name.Value(),  // the name
                        PIPE_ACCESS_OUTBOUND |      // "server" to "client" only
                        overlapped_write_flag,      // overlapped mode
                        0,                          // byte-mode, blocking
                        1,                          // only one instance
                        psize,                      // outgoing buffer size
                        0,                          // incoming buffer size (not used)
                        0,                          // default wait timeout (not used)
                        NULL);                      // we mark handles inheritable in Create_Process
      if (w == INVALID_HANDLE_VALUE) {
            dprintf(D_ALWAYS, "CreateNamedPipe(%s) error: %d\n", 
                  pipe_name.Value (), GetLastError());
            return FALSE;
      }
      HANDLE r =
            CreateFile(pipe_name.Value(),   // the named pipe
                     GENERIC_READ,            // desired access
                     0,                       // no sharing
                     NULL,                    // we mark handles inheritable in Create_Process
                     OPEN_EXISTING,           // existing named pipe
                     overlapped_read_flag,    // disable overlapped i/o on read end
                     NULL);                   // no template file
      if (r == INVALID_HANDLE_VALUE) {
            CloseHandle(w);
            dprintf(D_ALWAYS, "CreateFile(%s) error on named pipe: %d\n", 
                  pipe_name.Value(), GetLastError());
            return FALSE;
      }
      read_handle = new ReadPipeEnd(r, overlapped_read_flag, nonblocking_read, psize);
      write_handle = new WritePipeEnd(w, overlapped_write_flag, nonblocking_write, psize);
#else
      // Unix

      // Shut the compiler up
      // These parameters are needed on Windows
      can_register_read = can_register_read;
      can_register_write = can_register_write;
      psize = psize;

      bool failed = false;
      int filedes[2];
      if ( pipe(filedes) == -1 ) {
            dprintf(D_ALWAYS,"Create_Pipe(): call to pipe() failed\n");
            return FALSE;
      }

      if ( nonblocking_read ) {
            int fcntl_flags;
            if ( (fcntl_flags=fcntl(filedes[0], F_GETFL)) < 0 ) {
                  failed = true;
            } else {
                  fcntl_flags |= O_NONBLOCK;    // set nonblocking mode
                  if ( fcntl(filedes[0],F_SETFL,fcntl_flags) == -1 ) {
                        failed = true;
                  }
            }
      }
      if ( nonblocking_write ) {
            int fcntl_flags;
            if ( (fcntl_flags=fcntl(filedes[1], F_GETFL)) < 0 ) {
                  failed = true;
            } else {
                  fcntl_flags |= O_NONBLOCK;    // set nonblocking mode
                  if ( fcntl(filedes[1],F_SETFL,fcntl_flags) == -1 ) {
                        failed = true;
                  }
            }
      }
      if ( failed == true ) {
            close(filedes[0]);
            filedes[0] = -1;
            close(filedes[1]);
            filedes[1] = -1;
            dprintf(D_ALWAYS,"Create_Pipe() failed to set non-blocking mode\n");
            return FALSE;
      }

      read_handle = filedes[0];
      write_handle = filedes[1];
#endif

      // add PipeHandles to pipeHandleTable
      pipe_ends[0] = pipeHandleTableInsert(read_handle) + PIPE_INDEX_OFFSET;
      pipe_ends[1] = pipeHandleTableInsert(write_handle) + PIPE_INDEX_OFFSET;

      dprintf(D_DAEMONCORE,"Create_Pipe() success read_handle=%d write_handle=%d\n",
              pipe_ends[0],pipe_ends[1]);
      return TRUE;
}


Generated by  Doxygen 1.6.0   Back to index