File and device input/output

Selecting advisory or mandatory locking

The use of mandatory locking is not recommended for reasons that will be made clear in a subsequent section. Whether or not locks are enforced by the I/O system calls is determined at the time the calls are made by the permissions on the file (see chmod(2)). For locks to be under mandatory enforcement, the file must be a regular file with the set-group-ID bit on and the group execute permission off. If either condition fails, all record locks are advisory. Mandatory enforcement can be assured by the following code:

   #include <sys/types.h>
   #include <sys/stat.h>

int mode; struct stat buf; . . . if (stat(filename, &buf) < 0) { perror("program"); exit (2); } /* get currently set mode */ mode = buf.st_mode; /* remove group execute permission from mode */ mode &= ~(S_IEXEC>>3); /* set 'set group id bit' in mode */ mode |= S_ISGID; if (chmod(filename, mode) < 0) { perror("program"); exit(2); } . . .

Files that are to be record locked should never have any type of execute permission set on them. This is because the operating system does not obey the record locking protocol when executing a file.

The chmod(1) command can also be easily used to set a file to have mandatory locking. This can be done with the command:

   chmod +l file

The ls(1) command shows this setting when you ask for the long listing format:

   ls -l file

causes the following to be printed:

-rw---l--- 1 user group size mod_time file

Advisory versus mandatory locking

Note the following points when choosing whether to implement advisory or mandatory locking:

Next topic: Record locking and future releases of the UNIX system
Previous topic: Deadlock handling

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004