Fix reuse of pty slave devices - #2449
Conversation
| lock(&tty->lock); | ||
| if (tty->hung_up || pty_is_half_closed_master(tty)) { | ||
| unlock(&pids_lock); | ||
| err = -1; |
| // hang_up on the pty master. But the session leader may have a | ||
| // reference, and the pty master always has a reference. | ||
| if (tty->refcount - 1 == (tty->session ? 2 : 1)) { | ||
| tty->pty.other->hung_up = false; |
| // If userland's reference count on the pty slave is now 1, clear | ||
| // hang_up on the pty master. But the session leader may have a | ||
| // reference, and the pty master always has a reference. | ||
| if (tty->refcount - 1 == (tty->session ? 2 : 1)) { |
There was a problem hiding this comment.
I found this check a bit unsettling in the last PR but figured it could be fixed later. Seeing it again, I'm thinking much the same thing... Ideally we would define explicitly which references count for this and which ones don't, and with that knowledge the likely solution is to create a second refcount field for tracking just those.
|
Independently reproduced and confirmed — this fixes a real divergence from Linux, and the patch still applies cleanly to current master (7864dd6). Test: open a pty master, open its slave, write and read a line, close the slave fully, then reopen the same slave and write another line. int m = posix_openpt(O_RDWR|O_NOCTTY); grantpt(m); unlockpt(m);
ptsname_r(m, name, sizeof name);
int s = open(name, O_RDWR|O_NOCTTY); write(s, "one\n", 4); read(m, ...);
close(s);
int s2 = open(name, O_RDWR|O_NOCTTY); write(s2, "two\n", 4); read(m, ...);Same static i386 binary, four ways:
The failure mode on master is a silent EOF rather than an error, which is the kind of thing that makes a reusing program look like it just stopped receiving output. Two notes, neither a objection:
@cgull — for what it is worth, our fork reached equivalent behaviour independently, so the semantics you are restoring here match what we settled on too. |
This should fix the problem brought up by @francoisvignon in my original work on #2387. At the time I wrote that I hadn't even thought about pty masters allowing serial reuse of the pty slave, and so it worked more like physical serial devices, where you don't want them to work after the modem hangs up.
This is definitely one of those less well documented and supported bits of functionality in ptys (there are many). But it seems like a good idea to support this because Linux does.