Re: Reading from a pty after slave exited
site_archiver@lists.apple.com Delivered-To: darwin-kernel@lists.apple.com Thanks for the update, Julian! -- Terry 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); _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-kernel mailing list (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/site_archiver%40lists.a... 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. On Apr 23, 2010, at 11:14 PM, Julian Scheid wrote: On Mon, Apr 19, 2010 at 2:55 PM, Julian Scheid <julians37@googlemail.com
wrote:
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)); } } This email sent to site_archiver@lists.apple.com
participants (1)
-
Terry Lambert