sysopen - die only if EBUSY?

Discussion in 'Perl Misc' started by Tomasz Chmielewski, Oct 2, 2008.

  1. 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?


    --
    Tomasz Chmielewski
    http://wpkg.org
     
    Tomasz Chmielewski, Oct 2, 2008
    #1
    1. Advertising

  2. Tomasz Chmielewski <> writes:

    > 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
     
    Peter Makholm, Oct 2, 2008
    #2
    1. Advertising

  3. Peter Makholm schrieb:
    > Tomasz Chmielewski <> writes:
    >
    >> 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!


    --
    Tomasz Chmielewski
    http://wpkg.org
     
    Tomasz Chmielewski, Oct 2, 2008
    #3
  4. Tomasz Chmielewski

    Ben Morrow Guest

    Quoth Peter Makholm <>:
    > Tomasz Chmielewski <> writes:
    >
    > > 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

    --
    You poor take courage, you rich take care:
    The Earth was made a common treasury for everyone to share
    All things in common, all people one.
    'We come in peace'---the order came to cut them down. []
     
    Ben Morrow, Oct 2, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Sheen

    Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    446
    Natty Gur
    Jan 26, 2004
  2. Lloyd Sheen

    More Die IDE Die

    Lloyd Sheen, Jan 24, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    460
    Alvin Bruney
    Jan 25, 2004
  3. kyung seop kim
    Replies:
    2
    Views:
    1,069
    Asfand Yar Qazi
    Nov 11, 2004
  4. Hon Guin Lee - Web Producer - SMI Marketing

    sysopen problem

    Hon Guin Lee - Web Producer - SMI Marketing, Oct 7, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    119
  5. Michele Dondi

    problem with sysopen() on nfs

    Michele Dondi, Sep 18, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    112
    Michele Dondi
    Sep 19, 2004
Loading...

Share This Page