SLG150 has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys.

In using, File::lockf, the lock() function from that package is returning a status of "9". I can't find the corresponding error in any of my system documentation(IE: "man lockf" and the POD for File::lockf). Please advise as to the nature of this error. Thank you in advance, I appreciate any and all help!

Regards,
Steve
  • Comment on File::lockf and the lock() return value

Replies are listed 'Best First'.
Re: File::lockf and the lock() return value
by count0 (Friar) on Jan 12, 2002 at 00:43 UTC
    First off, i'd highly suggest using flock() if at all possible.

    The big limitation here is that File::lockf is a Perl binding to the SYSV lockf(3) call. Since lockf(3) will never return a 9, my guess is that the Perl binding (File::lockf) is returning the value that errno is set to.

    Now the next hurdle; how do you figure out what an errno value corresponds to? ... with the strerror(3) (POSIX/SYSV/BSD/ISO 9899, etc) call. OOOoooh loook, it conforms to POSIX! =) That means that we're in luck (*this* time), since Perl has a fairly complete POSIX module.

    The short answer (this is barebones, no strict or anything... and *just* a snippet):
    use POSIX; use File::lockf; $status = File::lockf::lock ($args); # replace $args as appropriate. if ($status) { # Uh oh, non zero value returned. # Lets use POSIX's errstr() to find out what happened $error_string = strerror ($status); die "Uh oh, lockf returned '$error_string'"; }

    The real answer: flock

    =)

    Update: I forgot to mention, in this case, with a status of '9', the error is "Bad file descriptor". This means that something was wrong with your filehandle.
      Three points and clarifications.

      The first is that it is often impossible to use flock due to OS or filesystem limitations. For instance in Linux you cannot use it over NFS.

      The second is that if you want to turn errno into a string message, the simplest way to do it is to assign it to $! and then use $! as a string. You can demonstrate this with:

      sub errno_to_str { $! = shift; return "$!"; }
      The third point is that what this conversion will give for a particular number is highly OS dependent. While 9 gives you "Bad file descriptor", it would be highly unwise to assume that it does the same on someone else's machines.
        ++, thanks tilly! =)

        The $! = $returned_status is a great trick. I've never used $! like that.

        And thanks for clearing up those other two points.