V
Vilmos Soti
Hello,
I am trying to find all open filehandles in my program and close
them before exiting.
Among many things, my program mounts a disk (runs under Linux),
finds all files with File::Find, then copies the files with
File::Copy. I use filenames as parameters to copy in order to
keep my program simple, and also because the manpage for File::Copy
recommends using filenames wherever possible.
I have a signal handler which tries to unmount the disk in
the case of a sigint, but it will fail if copy from File::Copy
has an open filehandle on the mounted disk.
Here is a short program which fully illustrates my problem:
(I excluded the checking of return values for the sake of keeping
this short program, well, short)
#################### program starts ####################
#!/usr/bin/perl -w
use strict;
use File::Copy;
sub bye () {
print "in function bye\n";
system ("umount /tmp/mountpoint");
exit;
}
$SIG{INT} = \&bye;
system ("mount -o loop /tmp/filesystem.img /tmp/mountpoint");
system ("ls -l /tmp/mountpoint");
print "copy starts\n";
copy ("/tmp/mountpoint/devzero", "/tmp/mountpoint/devnull");
#################### program ends ####################
And here are the results after running the program:
#################### results start ####################
[root@cord pts/6 tmp]# ./a.pl
total 12
crw-rw-rw- 1 root root 1, 3 Dec 31 1969 devnull
crw-rw-rw- 1 root root 1, 5 Dec 31 1969 devzero
drwxr-xr-x 2 root root 12288 Apr 21 11:19 lost+found
copy starts
in function bye
umount: /tmp/mountpoint: device is busy
[root@cord pts/6 tmp]#
#################### results end ####################
Is there any way to find all open files and close them?
Or a way to close all open files apart from stdin, stdout, and stderr?
I (possibly naively) tried to iterate through %main:: and find
all filehandles, descend into additional structures if I need to,
but I didn't succeed. I just tried and tried and tried, and
failed and failed and failed.
Here is the code snipplet I had in the "bye" function.
#################### Code starts ####################
foreach my $key (sort keys %main: {
my $x = \$main::{$key};
print $x;
print "\n";
}
#################### Code ends ####################
However, it returned only GLOBs. I am out of ideas.
Of course, I could write my own copy function, but I would
prefer to use the already existing functions, and also to
keep my code small and simple.
Also, I could close the FROM and TO filehandles in bye,
but that, besides being an ugly hack is an understatement,
is not a general solution, and has no learning value.
I hope I provided enough information about my misery and
my efforts of how I tried to solve it.
Thanks, Vilmos
I am trying to find all open filehandles in my program and close
them before exiting.
Among many things, my program mounts a disk (runs under Linux),
finds all files with File::Find, then copies the files with
File::Copy. I use filenames as parameters to copy in order to
keep my program simple, and also because the manpage for File::Copy
recommends using filenames wherever possible.
I have a signal handler which tries to unmount the disk in
the case of a sigint, but it will fail if copy from File::Copy
has an open filehandle on the mounted disk.
Here is a short program which fully illustrates my problem:
(I excluded the checking of return values for the sake of keeping
this short program, well, short)
#################### program starts ####################
#!/usr/bin/perl -w
use strict;
use File::Copy;
sub bye () {
print "in function bye\n";
system ("umount /tmp/mountpoint");
exit;
}
$SIG{INT} = \&bye;
system ("mount -o loop /tmp/filesystem.img /tmp/mountpoint");
system ("ls -l /tmp/mountpoint");
print "copy starts\n";
copy ("/tmp/mountpoint/devzero", "/tmp/mountpoint/devnull");
#################### program ends ####################
And here are the results after running the program:
#################### results start ####################
[root@cord pts/6 tmp]# ./a.pl
total 12
crw-rw-rw- 1 root root 1, 3 Dec 31 1969 devnull
crw-rw-rw- 1 root root 1, 5 Dec 31 1969 devzero
drwxr-xr-x 2 root root 12288 Apr 21 11:19 lost+found
copy starts
in function bye
umount: /tmp/mountpoint: device is busy
[root@cord pts/6 tmp]#
#################### results end ####################
Is there any way to find all open files and close them?
Or a way to close all open files apart from stdin, stdout, and stderr?
I (possibly naively) tried to iterate through %main:: and find
all filehandles, descend into additional structures if I need to,
but I didn't succeed. I just tried and tried and tried, and
failed and failed and failed.
Here is the code snipplet I had in the "bye" function.
#################### Code starts ####################
foreach my $key (sort keys %main: {
my $x = \$main::{$key};
print $x;
print "\n";
}
#################### Code ends ####################
However, it returned only GLOBs. I am out of ideas.
Of course, I could write my own copy function, but I would
prefer to use the already existing functions, and also to
keep my code small and simple.
Also, I could close the FROM and TO filehandles in bye,
but that, besides being an ugly hack is an understatement,
is not a general solution, and has no learning value.
I hope I provided enough information about my misery and
my efforts of how I tried to solve it.
Thanks, Vilmos