sysopen - die only if EBUSY?

  • Thread starter Tomasz Chmielewski
  • Start date
T

Tomasz Chmielewski

I want to check if a given block device is already used by the system
(i.e. device is mounted, used for swap, part of a RAID array etc.).

In short, it can be done with:


use Fcntl qw(O_RDONLY O_EXCL);

my $path = "/dev/sda";

sysopen(FH, $path, O_RDONLY | O_EXCL) or die $!;


If /dev/sda is really used, the script will die with
"Device or resource busy" error message (depending on locales).

Low level, it can be seen as:

open("/dev/sda", O_RDONLY|O_EXCL|O_LARGEFILE) = -1 EBUSY (Device or resource busy)


The script will also die if the file does not exist - looking
low level it would be:

open("/dev/blah", O_RDONLY|O_EXCL|O_LARGEFILE) = -1 ENOENT (No such file or directory)


I want the script to take a specified action depending on
the error (EBUSY, ENOENT, etc.).
How can I read these values?
 
P

Peter Makholm

Tomasz Chmielewski said:
I want the script to take a specified action depending on
the error (EBUSY, ENOENT, etc.).
How can I read these values?

The numeric value of $! would corrospond to the actual error code.

If you 'use Errno;' you will have a magic %! which can be easier to
use. If the error was EBUSY then $!{EBUSY} would be true, if the error
wwas ENOENT $!{[ENOENT} is true, and so on.

Read 'perldoc Errno'

//Makholm
 
T

Tomasz Chmielewski

Peter said:
Tomasz Chmielewski said:
I want the script to take a specified action depending on
the error (EBUSY, ENOENT, etc.).
How can I read these values?

The numeric value of $! would corrospond to the actual error code.

If you 'use Errno;' you will have a magic %! which can be easier to
use. If the error was EBUSY then $!{EBUSY} would be true, if the error
wwas ENOENT $!{[ENOENT} is true, and so on.

Read 'perldoc Errno'

Thanks!
 
B

Ben Morrow

Quoth Peter Makholm said:
Tomasz Chmielewski said:
I want the script to take a specified action depending on
the error (EBUSY, ENOENT, etc.).
How can I read these values?

The numeric value of $! would corrospond to the actual error code.

If you 'use Errno;' you will have a magic %! which can be easier to
use. If the error was EBUSY then $!{EBUSY} would be true, if the error
wwas ENOENT $!{[ENOENT} is true, and so on.

You can also export the E* constants from POSIX:

use POSIX qw/:errno_h/;

if ($! == EBUSY) {
...
}

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top