Perl: creating zombies via open () without closing ()

Here is the problem: I have a daemon that receives requests from the client, performs a function (from some module) because of the request, and returns a response to the client. After fork () I close STDIN, STDOUT and STDERR. One function is to check dmesg. To do this, I get dmesg output through open (DMESG, "/ bin / dmesg |"). I do not close this fh after reading, because I thought that it automatically closes after the function finishes. But this does not happen, and I get zombies for every dmesg call.

Q How can I reinitialize STDIN / STDOUT / STDERR Perl? I found: "The problem with closing STDOUT instead of reopening is that if you open other files, they can get fd 0.1 or 2 - prevent the reopening of STDOUT in the future." by jmanning2k And I think this has something to do with it, but I really don't understand. I hope someone can explain this to me.

I know that I can avoid the problem, for example. calling dmesg via qx (); or just closing fh, but I want to understand where the zombies come from.

+3
source share
3 answers

The form

open DMESG, "/bin/dmesg|";

DMESG. "" Perl, , local.

open my $dmesg, "/bin/dmesg|";

$dmesg , , (.. ).

+7

open(DMESG, "/bin/dmesg |") fh , , .

, , .

open my $dmesg, …
+6

The problem is how Perl is implemented. Here's a snippet of code from a function Perl_do_opennin a file doio.c:

fd = PerlIO_fileno(IoIFP(io));
if (IoTYPE(io) == IoTYPE_STD) {
    /* This is a clone of one of STD* handles */
    result = 0;
}
else if (fd >= 0 && fd <= PL_maxsysfd) {
    /* This is one of the original STD* handles */
    saveifp  = IoIFP(io);
    saveofp  = IoOFP(io);
    savetype = IoTYPE(io);
    savefd   = fd;
    result   = 0;
}

If you open an existing file descriptor, the file descriptor will be closed and reopened open(). This does not happen with descriptors STD*, as you can see from the code above. Therefore, Perl takes the next free handle to open, while the older one remains open.

+1
source

Source: https://habr.com/ru/post/1780452/


All Articles