Esempio n. 1
0
/*
 *  Fixup file descriptors so that all stdin and stdout of the application
 *  is piped to alt_stdout (for stdout) and alt_stdin (for stdin).
 *  Now we can watch and modify the datastream from and to the application.
 *  That allows us to insert keep-alive characters to keep socat happy!
 */
int comm_init( void)
{
  int result;
  int fds[ 2];

  // keep a copy of the program's original fds for stdin and stdout
  fd_stdin  = fcntl( 0, F_DUPFD);
  fd_stdout = fcntl( 1, F_DUPFD);
  // Note on debug output: Because we're messing with stdout,
  // all debug output for 'comm_init' is sent to stderr instead of stdout !
  if (DEBUG_COMM && (debug_flags & DEBUG_COMM)) {
    fprintf( stderr, "fd_stdin = %d, fd_stdout = %d\n", fd_stdin, fd_stdout);
  }

  // create the output pipe with all new fds
  // replace application's stdout descriptor (1) by write side of output pipe
  result = pipe2( fds, O_NONBLOCK);
  if (result < 0) {
    perror( "output pipe creation failed");
    return -1;
  }
  close( 1);
  result = fcntl( fds[ 1], F_DUPFD);
  close( fds[ 1]);
  fds[ 1] = result;
  if (DEBUG_COMM && (debug_flags & DEBUG_COMM)) {
    fprintf( stderr, "output pipe: output/read side fd = %d, input/write side fd = %d\n", fds[ 0], fds[ 1]);
  }
  alt_stdout = fds[ 0];

  // create the input pipe with all new fds
  // replace application's stdin descriptor (0) by read side of input pipe
  close( 0);
  result = pipe2( fds, 0);
  if (result < 0) {
    perror( "input pipe creation failed");
    return -1;
  }
  if (DEBUG_COMM && (debug_flags & DEBUG_COMM)) {
    fprintf( stderr, "input  pipe: output/read side fd = %d, input/write side fd = %d\n", fds[ 0], fds[ 1]);
  }
  alt_stdin = fds[ 1];

  if (DEBUG_COMM && (debug_flags & DEBUG_COMM)) {
    fprintf( stderr, "alt_stdin = %d, alt_stdout = %d\n", alt_stdin, alt_stdout);
  }

  if (mendel_thread_create( "comm", &worker, NULL, &comm_thread, NULL) != 0) {
    return -1;
  }
  struct sched_param param = {
    .sched_priority = COMM_PRIO
  };
  pthread_setschedparam( worker, COMM_SCHED, &param);

  return 0;
}
Esempio n. 2
0
int limsw_init( void)
{
  fdset = calloc( nr_limits, sizeof( struct pollfd));

  if (mendel_thread_create( "limit_switches", &worker, NULL, &limsw_watcher, NULL) != 0) {
    return -1;
  }
  struct sched_param param = {
    .sched_priority = LIMSW_PRIO
  };
  pthread_setschedparam( worker, LIMSW_SCHED, &param);

  return 0;
}