Re: Reading from a pty after slave exited
Re: Reading from a pty after slave exited
- Subject: Re: Reading from a pty after slave exited
- From: Terry Lambert <email@hidden>
- Date: Fri, 23 Apr 2010 23:41:45 -0700
Thanks for the update, Julian!
I think I can safely say the operations will fail in the future if you
lose the race, so code not written to do ioctl the right end of the
pty will get race failures, but it will only harm the code doing the
ioctl on the wrong fd, and then not always.
-- Terry
On Apr 23, 2010, at 11:14 PM, Julian Scheid wrote:
On Mon, Apr 19, 2010 at 2:55 PM, Julian Scheid <email@hidden
> wrote:
Is it not possible to implement something akin to Screen to work
reliably on both Linux and OS X using POSIX interfaces, without
resorting to hacks such as those found in [3]?
It seems I have found a very simple solution: use openpty+fork instead
of forkpty and keep the slave fd open on the master side until after
EOF is hit. Why this would work makes sense after reading your
explanation.
Below is a modified version of the program from my original post. This
works fine on Darwin, I haven't tested it on Linux yet but I don't see
why it wouldn't work.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifdef __APPLE__
#include <util.h>
#else
#include <pty.h>
#endif
#include <string.h>
int main() {
const char* test_string = "foo";
int pid, master_fd, slave_fd;
openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
pid = fork();
assert(pid >= 0);
if (pid == 0) {
/* slave */
dup2(slave_fd, fileno(stdout));
int nwritten = write(fileno(stdout), test_string, sizeof
test_string);
assert(nwritten == sizeof test_string);
exit(0);
}
else {
/* master */
waitpid(pid, NULL, 0);
char buf[32];
int nread = read(master_fd, buf, sizeof buf);
assert(nread > 0);
assert(0 == strcmp(buf, test_string));
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden