开发者

In Perl, how do I safely create a temporary FIFO?

开发者 https://www.devze.com 2023-01-17 19:49 出处:网络
To compare with diff adjacent records from a file, I created two FIFOs, forked children to supply their write ends, and captured the output of

To compare with diff adjacent records from a file, I created two FIFOs, forked children to supply their write ends, and captured the output of

diff -ub $previous $current

where the scalars contain the FIFOs’ paths—kind of how bash process substitution works.

This is not a program that needs to be bullet-proof, but if it were, how would I create t开发者_JS百科emporary FIFOs so as to avoid race conditions and other vulnerabilities? Imagine File::Temp has a File::Temp::FIFO cousin: what would be the latter's implementation?


How about creating a temporary directory (a la mkdtemp()) to avoid race conditions, and then put your FIFOs in there?

For example:

use File::Temp qw(tempdir);
use File::Spec::Functions qw(catfile);
use POSIX qw(mkfifo);

my $dir = tempdir(CLEANUP=>1);
my $fifo0 = catfile($dir, "fifo0");
mkfifo($fifo0, 0700) or die "mkfifo($fifo0) failed: $!";
my $fifo1 = catfile($dir, "fifo1");
mkfifo($fifo1, 0700) or die "mkfifo($fifo1) failed: $!";

print "FIFO #0: $fifo0\n";
print "FIFO #1: $fifo1\n";


Assuming all the process ends are connected to the FIFOs you created, can't you just remove them from the filesystem? The opened filehandles will keep the FIFO from being deleted, but no new handles could be attached to it, and once the existing filehandles are closed, the FIFO itself will disappear.

0

精彩评论

暂无评论...
验证码 换一张
取 消