use open() for TMPFILE generation
authorHeiko Schlittermann <hs@schlittermann.de>
Wed, 22 Jun 2016 13:49:16 +0200
changeset 38 20874d23e7ed
parent 37 05ef007278eb
child 39 0f95ea2ef883
use open() for TMPFILE generation
lib/Nagios/Check/DNS/check_tlsa_record.pm
--- a/lib/Nagios/Check/DNS/check_tlsa_record.pm	Tue Jun 21 16:34:49 2016 +0200
+++ b/lib/Nagios/Check/DNS/check_tlsa_record.pm	Wed Jun 22 13:49:16 2016 +0200
@@ -2,26 +2,31 @@
 
 use strict;
 use warnings;
-use feature qw(say switch);
+use feature qw(say switch state);
 use Carp;
 use Data::Dumper;
 use if $ENV{DEBUG} => 'Smart::Comments';
 
 #use if $^V >= v5.0.20 => (experimental => gw(smartmatch));
 use experimental qw(smartmatch);
-use File::Temp;
 use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC);
 
 our $VERSION = '0.1';
 
 my $dane_pattern = qr'(?i)^(?<record>(?<tlsa_usage>\d+)\s+(?<tlsa_selector>\d+)\s+(?<tlsa_match_type>\d+)\s+(?<tlsa_hash>[0-9a-f ]+))$';
 
-# keep in in 'our' to avoid garbage collection and destruction
-# of the File::Temp object
-our $tmpfile = File::Temp->new();
+# Alternativly my $tmpfile = File::Temp->new();
+#              unlink($tmpfile);
+# if we need more # control over the directory, the name, and such.
+# The sub _tmpfile_ has the sole purpose to use the $tmpfile in a
+# *global* context. This prevents our garbage collector from closing the
+# file!
+open(my $tmpfile, '+>', undef)
+    or die "Can't open anonymous file: $!\n";
+fcntl($tmpfile, F_SETFD, fcntl($tmpfile, F_GETFD, 0) & ~FD_CLOEXEC) or die "clear FD_CLOEXEC on $tmpfile: $!\n";
+sub __tmpfile { $tmpfile }
 my $fdname = '/dev/fd/' . fileno $tmpfile;
-fcntl($tmpfile, F_SETFD, fcntl($tmpfile, F_GETFD, 0) & ~FD_CLOEXEC)
-    or die "clear FD_CLOEXEC on $tmpfile: $!\n";
+
 
 sub main {
     my $domain   = shift;