在 Perl 中,如何安全地创建临时 FIFO? [英] In Perl, how do I safely create a temporary FIFO?
问题描述
与diff
相邻记录进行比较从一个文件中,我创建了两个 FIFO,分叉儿童提供他们的写入结束,并捕获了
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
标量包含 FIFO 路径的地方——类似于 bash
进程替换 有效.
where the scalars contain the FIFOs’ paths—kind of how bash
process substitution works.
这不是一个需要防弹的程序,但如果是,我将如何创建临时 FIFO 以避免 竞争条件 和其他漏洞?想象一下 File::Temp
有一个 File::Temp::FIFO
表弟:后者的实现是什么?
This is not a program that needs to be bullet-proof, but if it were, how would I create temporary 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?
推荐答案
如何创建一个临时目录(a la mkdtemp())以避免竞争条件,然后将您的 FIFO 放在那里?
How about creating a temporary directory (a la mkdtemp()) to avoid race conditions, and then put your FIFOs in there?
例如:
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";
这篇关于在 Perl 中,如何安全地创建临时 FIFO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!