[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: elf: openpty () usage question



> Plenty of examples in my two packages dnprogs and latd (in unstable
> or from linux-decnet.sourceforge.net/download.html). The easiest to
> follow is dnetd/task_server.c in dnprogs, ignore the code that
> replaces openpty for broken RedHat 5 systems.

I've done some research, read the glibc code as well as your
task_server.c file.  ITOT your code does exactly the same steps as the
glibc code for forkpty.

Still, I'm not finding anything wrong with my simple implementation.
Is there something dumb that I'm missing here?

I'm expecting that the "hello" would be received on the slave pty.
Instead, it is received on the master pty.  Any thoughts?


/* main.cc
     $Id$

   written by Marc Singer
   28 May 2001

   Copyright (C) 2001 The Buici Company

   -----------
   DESCRIPTION
   -----------

*/

//#define LOOPBACK		// Enable loopback mode, for testing
#define g_msTimeout	5000

#include <fcntl.h>
#include <stdio.h>
#include <pty.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <termios.h>

pid_t pid;

void watch (int c, int* rgfh) 
{
  fd_set rfds;                  // Read fh's
  struct timeval tv;            // Timeout

  //  printf ("watching %d\n", rgfh[0]);

  while (1) {
    FD_ZERO (&rfds);

    bzero (&tv, sizeof (tv));
    tv.tv_usec = (g_msTimeout%1000)*1000;
    tv.tv_sec = g_msTimeout/1000; 

                // -- Prepare fh sets
    for (int i = 0; i < c; ++i)
      FD_SET (rgfh[i], &rfds);

    int result = select (10, &rfds, NULL, NULL, &tv);
  
    if (result <= 0) {              // Timeout or error
      continue;
    }

    for (int i = 0; i < c; ++i) {
      if (FD_ISSET (rgfh[i], &rfds)) {
	char rgb[512];
	int cch = read (rgfh[i], rgb, sizeof (rgb));
	if (cch == -1) {
	  printf ("%s: errno %d (%s)\n", ttyname (0), 
		  errno, strerror (errno));
	  return ;
	}
	if (cch)
	  printf ("* %s: received on %d: (%*.*s)\n", ttyname (0), 
		   rgfh[i], cch, cch, rgb);

#if defined (LOOPBACK)
	if (i == 0)
	  write (rgfh[i], rgb, cch);
#endif
      }
    }
  }
}

int main (int argc, char** argv)
{
  int rgfh[2];
  char sz[80];

  *sz = 0;

  pid = forkpty (&rgfh[0], sz, NULL, NULL);
  if (pid == -1) {
    printf ("error\n");
    exit (0);
  }

  if (pid) {			// Parent
    printf ("%s: master %d (%s) sz %s\n", 
	    ttyname (0), rgfh[0], ttyname(rgfh[0]), sz);
    char* szTty = ttyname (0);
    write (rgfh[0], "hello", 5);
    watch (1, rgfh);
  }
  else {
    int fh = 0;			// STDIN
    watch (1, &fh);
  }

  return 0;
}



Reply to: