Subject: help with program that uses forkpty
To: None <netbsd-users@netbsd.org>
From: Felix Zaslavskiy <felix@students.poly.edu>
List: netbsd-users
Date: 03/25/2003 13:38:25
hello , i am not sure if this is best list for this but i posted to
unix.porograming and i got some suggestions but still not much help
anyway this progam i been working on seems to not behave how it expected.

why is it that the read from child does not return the modifiend string ?

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<util.h>


int main()
{
         int pty_pid;
         int ptyfd;
         int errfd;
         FILE * errfile;

         errfile = fopen("error.log" , "a");

         //create a new process on a new pty
         pty_pid = forkpty(&ptyfd, NULL, NULL, NULL);

         if(pty_pid == -1){
                 printf("error forking pty");
                 exit(0);
         }
         else if(pty_pid == 0)
         {
                 //i am the new process
                 char buf[64];

                 //bake off
                 sleep(2);

                 if(read(0,buf ,63 ) == -1)
                         fprintf(errfile, "read from parent : %s \n" ,
strerror(errno));
                 else {
                         buf[33] = 'x';
                         buf[63] = '\0';
                         fprintf(errfile, "read from parent: %s \n" , buf);
                 }

                 if(write(1, buf, 64) == -1)
                         fprintf(errfile, "write to parent : %s \n" ,
strerror(errno));

                 exit(0);
         }
         else
         {
                 char buf[64];
                 int i = 0;
                 for(; i < 63; ++i)
                 {
                         buf[i] = '0' + i%10;
                 }
                 buf[63]='\n';

                 if(fork() == 0)
                 {
                         if(write(ptyfd, buf , 64 ) == -1)
                         {
                                 fprintf(errfile , "write to child : %s
\n" , strerror(errno));
                         }
                 }
                 else
                 {
                         //bake off
                         sleep(4);

                         if(read(ptyfd, buf , 63) != -1)
                         {
                                 fprintf(errfile, "read from child:
%s\n", buf);
                         }
                         else
                         {
                                 fprintf(errfile, "read from child: %s
\n", strerror(errno));
                         }
                         sleep(1);
                         close(ptyfd);

                         if(wait() == pty_pid){
                                 fprintf(errfile, "Child exited");
                         }
                 }
         }

         return 0;
}

I added the back offs to make sure sequence of calls if correct and it
is as this trace i collected, here is piece of it:
720 is original process, 721 is the process that is on pty  and 722 is
process that writes to the child process.
    428     720 a.out    CALL  open(0x48065308,0x2,0)
    429     720 a.out    NAMI  "/dev/ttyp3"
    430     720 a.out    RET   open 5
    431     720 a.out    CALL  fork
    432     721 a.out    EMUL  "netbsd"
    433     720 a.out    RET   fork 721/0x2d1
    434     720 a.out    CALL  close(0x5)
    435     720 a.out    RET   close 0
    436     720 a.out    CALL  fork
    437     722 a.out    EMUL  "netbsd"
    438     720 a.out    RET   fork 722/0x2d2
    439     720 a.out    CALL  nanosleep(0xbfbfd5d4,0xbfbfd5cc)
    440     721 a.out    RET   fork 0
    441     721 a.out    CALL  close(0x4)
    442     721 a.out    RET   close 0
    443     721 a.out    CALL  setsid
    444     721 a.out    RET   setsid 721/0x2d1
    445     721 a.out    CALL  ioctl(0x5,TIOCSCTTY,0)
    446     721 a.out    RET   ioctl 0
    447     721 a.out    CALL  dup2(0x5,0)
    448     721 a.out    RET   dup2 0
    449     721 a.out    CALL  dup2(0x5,0x1)
    450     721 a.out    RET   dup2 1
    451     721 a.out    CALL  dup2(0x5,0x2)
    452     721 a.out    RET   dup2 2
    453     721 a.out    CALL  close(0x5)
    454     721 a.out    RET   close 0
    455     721 a.out    CALL  nanosleep(0xbfbfd5d4,0xbfbfd5cc)
    456     722 a.out    RET   fork 0
    457     722 a.out    CALL  write(0x4,0xbfbfd61c,0x40)
    458     722 a.out    GIO   fd 4 wrote 64 bytes
    459
"012345678901234567890123456789012345678901234567890123456789012
    460         "
    461     722 a.out    RET   write 64/0x40
    462     722 a.out    CALL  exit(0)
    463     721 a.out    RET   nanosleep 0
    464     721 a.out    CALL  read(0,0xbfbfd65c,0x3f)
    465     721 a.out    GIO   fd 0 read 63 bytes
    466
"012345678901234567890123456789012345678901234567890123456789012"
    467     721 a.out    RET   read 63/0x3f
    468     721 a.out    CALL  __fstat13(0x3,0xbfbfd20c)
    469     721 a.out    RET   __fstat13 0
    470     721 a.out    CALL  write(0x1,0xbfbfd65c,0x40)
    471     721 a.out    GIO   fd 1 wrote 64 bytes
    472
"012345678901234567890123456789012x45678901234567890123456789012\0"
    473     721 a.out    RET   write 64/0x40
    474     721 a.out    CALL  write(0x3,0x8050000,0x53)
    475     721 a.out    GIO   fd 3 wrote 83 bytes
    476         "read from parent:
012345678901234567890123456789012x45678901234567890123456789012
    477         "
    478     721 a.out    RET   write 83/0x53
    479     721 a.out    CALL  exit(0)
    480     720 a.out    RET   nanosleep 0
    481     720 a.out    CALL  read(0x4,0xbfbfd61c,0x3f)
    482     720 a.out    GIO   fd 4 read 63 bytes
    483
"012345678901234567890123456789012345678901234567890123456789012"
    484     720 a.out    RET   read 63/0x3f
    485     720 a.out    CALL  __fstat13(0x3,0xbfbfd20c)
    486     720 a.out    RET   __fstat13 0
    487     720 a.out    CALL  nanosleep(0xbfbfd5d4,0xbfbfd5cc)
    488     720 a.out    RET   nanosleep 0
    489     720 a.out    CALL  close(0x4)
    490     720 a.out    RET   close 0
    491     720 a.out    CALL  wait4(0xffffffff,0x804a07c,0,0)
    492     720 a.out    RET   wait4 722/0x2d2
    493     720 a.out    CALL  write(0x3,0x8050000,0x5f)
    494     720 a.out    GIO   fd 3 wrote 95 bytes
    495         "read from child:
012345678901234567890123456789012345678901234567890123456789012
    496          \M-H\M-V\M-?\M-?D\M-O\^DH|\240\^D\bp
    497         "
    498     720 a.out    RET   write 95/0x5f
    499     720 a.out    CALL  exit(0)

The error.log looks something like this:

read from parent:
012345678901234567890123456789012x45678901234567890123456789012
read from child:
012345678901234567890123456789012345678901234567890123456789012
ÈÖ¿¿DÏcH| p