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

Hi

I wanted to port a script that is running perfectly fine on my own Debian GNU/Linux boxes to one of the Solaris boxes at work. Somehow though this is a no go :-(

The problem is with file locking. Google and the monastery search didn't help me, but maybe someone else can explain to me what's going on here.

This is the code that's giving me trouble:

#Lock the file and move it to the processing file
if (-f $file) {
  print "Moving the file from $file to $processfile...\n" if $verbose;
  open(INFILE, '<', $file) or die "Cannot open the file: $!\n";
  unless (flock(INFILE, LOCK_EX | LOCK_NB)) {
    if ($verbose) {
      local $| = 1; #Flush buffer so the message is shown immediately
      print "Waiting for a lock on $file...\n";
    }
    flock(INFILE, LOCK_EX) or die "Cannot lock $file: $!\n";
  }
  move($file, $processfile) or die "Cannot move $file: $!\n";
  close INFILE;
}
else {
  die "No file found at $file\n";
}

On Solaris this results in the following error message:
Waiting for a lock on /home/flr/gvtest...
Cannot lock file: Bad file number

It seems open is successful, but locking is not.

As far as I know this is normally the result of $file not being a file, but the -f test should exclude that possibility.

Can anyone tell me where to look or maybe even how to solve this?

Help would be appreciated as this is the last hurdle to take. If this is remedied I have a fully functional Antivirus Monitor that looks up local infections in our network and processes them (reporting, warnings, statistics, ticketing...)

Thanks for your time

Replies are listed 'Best First'.
Re: Trouble with locking under Solaris
by Abigail-II (Bishop) on Sep 05, 2003 at 12:53 UTC
    Not all OSses allow you to have an exclusive lock on a file that you have open for reading. Furthermore, although flock takes a file handle as argument, the file itself is locked. But since you are locking, I presume other processes can do the same - including moving the file away. Which means there's no file left to lock.

    Check the system manuals what they say about 'flock'. I don't have access to a Solaris machine at the moment, so I cannot check.

    Abigail

      flock man page from solaris 8:

      SunOS/BSD Compatibility Library Functions flock(3UCB) NAME flock - apply or remove an advisory lock on an open file SYNOPSIS /usr/ucb/cc[ flag ... ] file ... #include <sys/file.h> int flock( fd, operation); int fd, operation; DESCRIPTION flock() applies or removes an advisory lock on the file associated with the file descriptor fd. The compatibility version of flock() has been implemented on top of fcntl(2) locking. It does not provide complete binary compatibility. Advisory locks allow cooperating processes to perform con- sistent operations on files, but do not guarantee exclusive access (that is, processes may still access files without using advisory locks, possibly resulting in inconsisten- cies). The locking mechanism allows two types of locks: shared locks and exclusive locks. More than one process may hold a shared lock for a file at any given time, but multiple exclusive, or both shared and exclusive, locks may not exist simultaneously on a file. A lock is applied by specifying an operation parameter LOCK_SH for a shared lock or LOCK_EX for an exclusive lock. The operation paramerer may be ORed with LOCK_NB to make the operation non-blocking. To unlock an existing lock, the operation should be LOCK_UN. Read permission is required on a file to obtain a shared lock, and write permission is required to obtain an exclusive lock. Locking a segment that is already locked by the calling process causes the old lock type to be removed and the new lock type to take effect. Requesting a lock on an object that is already locked nor- mally causes the caller to block until the lock may be acquired. If LOCK_NB is included in operation, then this will not happen; instead, the call will fail and the error EWOULDBLOCK will be returned. RETURN VALUES flock() returns: 0 on success. SunOS 5.8 Last change: 19 Jul 1994 1 SunOS/BSD Compatibility Library Functions flock(3UCB) -1 on failure and sets errno to indicate the error. ERRORS EBADF The argument fd is an invalid descriptor. EINVAL operation is not a valid argument. EOPNOTSUPP The argument fd refers to an object other than a file. EWOULDBLOCK The file is locked and the LOCK_NB option was speci- fied. SEE ALSO lockd(1M), chmod(2), close(2), dup(2), exec(2), fcntl(2), fork(2), open(2), lockf(3C) NOTES Use of these interfaces should be restricted to only appli- cations written on BSD platforms. Use of these interfaces with any of the system libraries or in multi-thread applica- tions is unsupported. Locks are on files, not file descriptors. That is, file descriptors duplicated through dup(2) or fork(2) do not result in multiple instances of a lock, but rather multiple references to a single lock. If a process holding a lock on a file forks and the child explicitly unlocks the file, the parent will lose its lock. Locks are not inherited by a child process. Processes blocked awaiting a lock may be awakened by sig- nals. Mandatory locking may occur, depending on the mode bits of the file. See chmod(2). Locks obtained through the flock() mechanism under SunOS 4.1 were known only within the system on which they were placed. This is no longer true.

      --
      "I just read perlman:perlboot," said Tom, objectively.
      naChoZ

      Abigail-II is, as usual, right.

      I've had similar problems on Solaris in the past myself.
      On Solaris, you must open a file for writing if you require an exclusive lock.
      Likewise you must open a file for reading if all you need is a shared lock.

      Cheers,

      BazB


      If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
      That way everyone learns.

Re: Trouble with locking under Solaris
by graff (Chancellor) on Sep 06, 2003 at 03:29 UTC
    I think what you may want to do is use a semaphore file -- that is, a separate file (which can remain empty) to which you apply flock. Competing processes use the same semaphore file to serialize the use of any shared resource. Each process tries to get a lock on the semaphore, and once it has the lock, it does whatever it needs to do with the shared resource (e.g. move or alter some other data file), then releases the semaphore.

    I happen to have posted a semaphore-file module here -- I picked it up and adapted it from a Perl Journal article by Sean Burke a couple years back. Works great on Solaris -- and even on ms-windows (tested on win98/2k).