示例#1
0
/**
 ** Analogue: fputs().
 ** Sends two commands to the child: first the "real command", then the
 ** "tag (trivial) command".
 ** Uses a "lazy fork" concept; we don't actually start the child
 ** at the time of the child_open() call but instead when the first cmd
 ** is sent. This means a program which employs a dispatcher, for instance,
 ** can do a child_open() in the main function, pass the handle off to
 ** all the routines it dispatches to, and child_close() before exiting
 ** without worrying about which of those functions will actually need
 ** the coprocess.  If some of them don't need it, they won't send it
 ** any commands and will incur no fork/exec overhead.
 **/
int
child_puts(char *s, CHILD *handle, AV* perl_out_array, AV* perl_err_array)
{
   int n;

   if (handle == NULL)
      handle = mru_handle;
   if ((mru_handle = handle) == NULL)
      return 0;
   if (handle->cph_pid == 0) {
      if (_cp_start_child(handle) != 0) {
	 fprintf(stderr, "can't start child %s\n",  handle->cph_cmd);
	 exit(1);
      }
      /* Add file descriptors to poll vector. */
      poll_add_fd( fileno(handle->cph_back),
                   NPOLL_TXT, bck_read, NULL, handle );
      poll_add_fd( fileno(handle->cph_err),
                   NPOLL_TXT, err_read, NULL, handle );
   }

   /* Save Perl array references for access from callbacks. */
   handle->cph_out_array = perl_out_array;
   handle->cph_err_array = perl_err_array;

   _dbg(F,L,1, "-->> %s", s);

   /** set error count to 0 for new command **/
   handle->cph_errs = 0;

   /** send the cmd down the pipe to the child **/
   if ((n = fputs(s, handle->cph_down)) == EOF)
      return EOF;
   /** be helpful and add a newline if there isn't one **/
   if (strrchr(s, '\n') != endof(s) - 1)
      if (fputc('\n', handle->cph_down) == EOF)
	 return EOF;
   /** send the tag cmd **/
   _dbg(F,L,4, "-->> [TAG]");
   if (fputs(handle->cph_tag, handle->cph_down) == EOF)
      return EOF;
   handle->cph_pending = TRUE;
   _dbg(F,L,4, "pending ...");

   /** poll **/
   poll_rcv( NPOLL_NO_TIMEOUT ); 

   return n;
}
示例#2
0
文件: poll_t.c 项目: dasima/WD
void poll_accept(Poll_t *pol)
{
    if(pol->clients_[0].revents & POLLIN)
    {
        int peerfd = accept(pol->listenfd_, NULL, NULL);
        if(peerfd == -1)
            ERR_EXIT("accept");
        poll_add_fd(pol, peerfd);
    }
}
示例#3
0
void poll_handle_accept(poll_t *_poll)
{
    if (_poll->clients_[0].revents & POLLIN)
    {
        int peerfd = accept(_poll->listenfd_, NULL, NULL);
        if (peerfd == -1)
        {
            ERR_EXIT("accept");
        }
        poll_add_fd(_poll, peerfd);
    }
}