From: PerlFAQ Server on
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

5.35: How do I close a file descriptor by number?

If, for some reason, you have a file descriptor instead of a filehandle
(perhaps you used "POSIX::open"), you can use the "close()" function
from the "POSIX" module:

use POSIX ();

POSIX::close( $fd );

This should rarely be necessary, as the Perl "close()" function is to be
used for things that Perl opened itself, even if it was a dup of a
numeric descriptor as with "MHCONTEXT" above. But if you really have to,
you may be able to do this:

require 'sys/syscall.ph';
my $rc = syscall(&SYS_close, $fd + 0); # must force numeric
die "can't sysclose $fd: $!" unless $rc == -1;

Or, just use the fdopen(3S) feature of "open()":

{
open my( $fh ), "<&=$fd" or die "Cannot reopen fd=$fd: $!";
close $fh;
}



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.
From: Vilmos Soti on
PerlFAQ Server <brian(a)theperlreview.com> writes:

> 5.35: How do I close a file descriptor by number?
>
> If, for some reason, you have a file descriptor instead of a filehandle
> (perhaps you used "POSIX::open"), you can use the "close()" function
> from the "POSIX" module:

I have a related question:

How do I find all the open file descriptors so I can close them?

I have a program which basically does this (runs on Unix):

$ ./perl-program
sub bye () {
umount /mnt/cdrom;
eject cd;
}

$SIG{INT} = bye;

close cd tray;
mount /mnt/cdrom;
cp -r /mnt/cdrom/ /destdir/;
bye;
$

The problem is that if the user hits Ctrl-C in the middle of the copy,
the open file descriptors on /mnt/cdrom will prevent umounting and
ejecting.

Thanks, Vilmos
From: J�rgen Exner on
Vilmos Soti <vilmos(a)soti.ca> wrote:
>How do I find all the open file descriptors so I can close them?
>
>I have a program which basically does this (runs on Unix):
>
>$ ./perl-program
> sub bye () {
> umount /mnt/cdrom;
> eject cd;
> }
>
> $SIG{INT} = bye;
>
> close cd tray;
> mount /mnt/cdrom;
> cp -r /mnt/cdrom/ /destdir/;
> bye;
>$

You must have a different Perl than I. My Perl knows neither cp nor
mount or umount and the syntax for close looks rather strange, too.

>The problem is that if the user hits Ctrl-C in the middle of the copy,
>the open file descriptors on /mnt/cdrom will prevent umounting and
>ejecting.

AFAIR file descriptors are closed automatically when they go out of
scope. So scope them correctly and you shouldn't have any problems.

jue
From: Vilmos Soti on
J�rgen Exner <jurgenex(a)hotmail.com> writes:

>> How do I find all the open file descriptors so I can close them?
>>
>> I have a program which basically does this (runs on Unix):
>>
>> $ ./perl-program
>> sub bye () {
>> umount /mnt/cdrom;
>> eject cd;
>> }
>>
>> $SIG{INT} = bye;
>>
>> close cd tray;
>> mount /mnt/cdrom;
>> cp -r /mnt/cdrom/ /destdir/;
>> bye;
>> $
>
> You must have a different Perl than I. My Perl knows neither cp nor
> mount or umount and the syntax for close looks rather strange, too.

Fair enough. :-) I just wanted to give a quick pseudo code (from the
top of my head) which describes the problem.

>> The problem is that if the user hits Ctrl-C in the middle of the copy,
>> the open file descriptors on /mnt/cdrom will prevent umounting and
>> ejecting.
>
> AFAIR file descriptors are closed automatically when they go out of
> scope. So scope them correctly and you shouldn't have any problems.

How do I do this?

Here is an actual (and running) perl program which demonstrates this problem:

---------------------- code starts ---------------------
#!/usr/bin/perl

use File::Copy;
use File::Find;

my $find_code = sub {
copy ($File::Find::name, "/dev/null") if -f $File::Find::name;
};

sub bye () {
system ("/bin/umount /mnt/cdrom");
system ("/usr/bin/eject /mnt/cdrom");
exit;
}

$SIG{INT} = \&bye;

system ("/usr/bin/eject -t /mnt/cdrom"); # close the cd tray
system ("/bin/mount /mnt/cdrom");
print "copy starts\n";
find ( { wanted => $find_code, follow => 0, no_chdir => 1 }, "/mnt/cdrom");
print "copy ends\n";
bye;
---------------------- code ends ---------------------

And here are two runs: The second one is interrupted. The output
is formatted for easy readability.

$ ./s.pl
copy starts
copy ends
$ # at this point, the cd is ejected.
$ ./s.pl
copy starts
umount: /mnt/cdrom: device is busy
umount: /mnt/cdrom: device is busy
umount: /mnt/cdrom: device is busy
umount: /mnt/cdrom: device is busy
/usr/bin/eject: unmount of `/dev/scd0' failed
$

Now I manually have to unmount and eject the cd.

Basically I would like something like this in the bye function:

foreach (@OPEN_FILE_DESCRIPTORS) {
next if stdin or stdout or stderr;
close $_;
}

The question is ... how can I get a list of open file descriptors.

Thanks for your time, Vilmos
From: Alan Curry on
In article <lqsk6pc0vd.fsf(a)glia.msmri.medicine.ubc.ca>,
Vilmos Soti <vilmos(a)soti.ca> wrote:
|
|my $find_code = sub {
| copy ($File::Find::name, "/dev/null") if -f $File::Find::name;
|};
|
|sub bye () {
| system ("/bin/umount /mnt/cdrom");
| system ("/usr/bin/eject /mnt/cdrom");
| exit;
|}
|
|$SIG{INT} = \&bye;
|
|system ("/usr/bin/eject -t /mnt/cdrom"); # close the cd tray
|system ("/bin/mount /mnt/cdrom");
|print "copy starts\n";
|find ( { wanted => $find_code, follow => 0, no_chdir => 1 }, "/mnt/cdrom");
|print "copy ends\n";
|bye;

Put the find() in a subprocess, ignore SIGINT in the parent, do the unmount
after the subprocess is finished.

--
Alan Curry