From: Dirk Gouders <dirk@gouders.net>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jslaby@suse.com>,
linux-kernel@vger.kernel.org, linux-newbie@vger.kernel.org
Subject: Re: pty: childs don't always react on close(2)
Date: Fri, 05 Dec 2025 23:20:03 +0100 [thread overview]
Message-ID: <ghh5u4zi3w.fsf@gouders.net> (raw)
In-Reply-To: <20251205214852.GL1712166@ZenIV> (Al Viro's message of "Fri, 5 Dec 2025 21:48:52 +0000")
[-- Attachment #1: Type: text/plain, Size: 850 bytes --]
Al Viro <viro@zeniv.linux.org.uk> writes:
> On Fri, Dec 05, 2025 at 10:37:44PM +0100, Dirk Gouders wrote:
>
>> child_pid1 = forkpty(&pty_fd1, NULL, NULL, NULL);
>
> You do realize that it will inherit all your opened descriptors,
> including pty_fd, right?
>
> ...
>> close(pty_fd);
>
> ... which doesn't do anything to the second child's descriptor
> table, including the descriptor that refers to the same opened file.
> IOW, the IO channel (== opened file) is very much opened after
> that close() - descriptors refering to it still exist.
Oh yes, thank you very much!
I use FD_CLOEXEC nearly everywhere in the mentioned program but exactly
not for the pty file descriptors. Oh well...
For completenes, I'll attach the modified test-case.
Would be interesting to hear if there are other possible fixes.
Thank you very much, again,
Dirk
[-- Attachment #2: PTY test program --]
[-- Type: text/plain, Size: 2138 bytes --]
/*
* Test closing file descriptors opened via forkpty() when not all data has been
* read. A following waitpid() blocks, when we opened two childs and try to
* close the file descriptor and then waitpid() for that child...
*/
#include <stdlib.h>
#include <stdio.h>
#include <pty.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdint.h>
#include <fcntl.h>
#define READ_SIZE 4096
/*
* Set PAGER variables and start a man(1) process.
*/
void do_child(void);
void do_child()
{
char *e_argv[3] = {"man", "groff_ms", NULL};
putenv("PAGER=cat");
putenv("MANPAGER=cat");
execvp("man", e_argv);
}
int main()
{
unsigned char read_buffer[READ_SIZE];
int pty_fd;
int pty_fd1;
int wstatus;
pid_t child_pid;
pid_t child_pid1;
pid_t ret_pid;
ssize_t ret;
/*
* Start a child to send us a manual page.
*/
child_pid = forkpty(&pty_fd, NULL, NULL, NULL);
if (child_pid == -1) {
fprintf(stderr, "forkpty(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
ret = fcntl(pty_fd, F_SETFD, FD_CLOEXEC);
if (child_pid == 0)
do_child();
printf("child_pid = %jd\n", (intmax_t) child_pid);
memset(read_buffer, '\0', READ_SIZE);
ret = read(pty_fd, read_buffer, READ_SIZE);
if (ret == -1) {
fprintf(stderr, "read(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("%s\n", read_buffer);
printf("%ld bytes read.\n", ret);
/*
* Start another child to send us a manual page.
*/
child_pid1 = forkpty(&pty_fd1, NULL, NULL, NULL);
if (child_pid1 == -1) {
fprintf(stderr, "forkpty(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
ret = fcntl(pty_fd1, F_SETFD, FD_CLOEXEC);
if (child_pid1 == 0)
do_child();
printf("child_pid1 = %jd\n", (intmax_t) child_pid1);
memset(read_buffer, '\0', READ_SIZE);
ret = read(pty_fd1, read_buffer, READ_SIZE);
if (ret == -1) {
fprintf(stderr, "read(): %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("%s\n", read_buffer);
printf("%ld bytes read.\n", ret);
close(pty_fd);
ret_pid = waitpid(child_pid, &wstatus, 0);
printf("ret_pid = %jd\n", (intmax_t) ret_pid);
exit(EXIT_SUCCESS);
}
prev parent reply other threads:[~2025-12-05 22:20 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-05 21:37 pty: childs don't always react on close(2) Dirk Gouders
2025-12-05 21:48 ` Al Viro
2025-12-05 22:20 ` Dirk Gouders [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ghh5u4zi3w.fsf@gouders.net \
--to=dirk@gouders.net \
--cc=gregkh@linuxfoundation.org \
--cc=jslaby@suse.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-newbie@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.