简体   繁体   中英

Read Write issues with Pseudo Terminal in Linux

I am writing a C++ program that would interact with an external process. The external process is written in C# and runs on mono. Note that I cannot modify the C# code as it is not a program written by me.

In this regard, I first set out by using pipes, which of course as I later realized is fully buffered and hence I faced a lot of sync issues. Essentially the external process had to flush its output after every write and this was not possible.

The next thing that I was about to try out was files, but however I found out that using pseudo-terminals would be more apt in my case. Here is some sample code that I have written:

int main()
{
    int fdm, fds, rc, pid;
    bool rValue;
    /* Setup Master pty*/
    rValue = rValue && (fdm = posix_openpt(O_RDWR)) >= 0 &&
             (rc = grantpt(fdm)) == 0 && (rc = unlockpt(fdm) == 0);
    if (rValue) {
        /* Open Slave pty */
        fds = open(ptsname(fdm), O_RDWR);
        pid = fork();
        if(pid < 0)
            perror("fork failed");
        else if(pid == 0) //child
        {
            close(fdm); //close master
            struct termios slave_orig_term_settings;
            struct termios new_term_settings;
            tcgetattr(slaveTTY, &slave_orig_term_settings);
            new_term_settings = slave_orig_term_settings;
            cfmakeraw(&new_term_settings);
            tcsetattr(slaveTTY, TCSANOW, &new_term_settings);

            //redirect I/O of this process
            close(0);
            close(1);
            close(2);
            dup(slaveTTY);
            dup(slaveTTY);
            dup(slaveTTY);

            close(slaveTTY);

            setsid();
            ioctl(0, TIOCSCTTY, 1);

            //launch the external process and replace its image in this process
            execve(argv[0],...);
        }
        else
        {
            close(fds); //close slave
            //Perform some interaction
            write(something using fdm);
            //Assume fdsets declared and set somewhere here
            select(fdm +1,&fdset,NULL,NULL,NULL);
            int readBytes = read(someting using fds);
        }
    }
    return EXIT_SUCCESS;
}

Assume that the fdset and fdclr for select are being taken care of.

The following issues are being observed in the parent process:

  1. Sometimes read returns with readBytes > 0 but there is nothing present in the buffer
  2. Sometimes whatever has been written to the terminal is read back
  3. Some garbage values such as ^]]49]1R are being dumped on the terminal (this is the actual terminal ie my output window)

PS: When the external process is written in C/C++, this issue is not occuring. Only when I run a C# program in mono.

I think pexpect in python is a good choice if you don't have to do that in C++ , it will save you a lot of time. And also you can use python freeze tools like pyinstaller to convert your python script to standalone binary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM