Technical article 100856

Trying to lock unnamed pipe on OpenServer 5.0 results in "Invalid Argument" (EINVAL).

OpenServer 5.0.0OpenServer 5.0.2

Written · Last revised

Problem

The following example program attempts to determine whether a write lock has been applied to an unnamed pipe.

NOTE: This code is for demonstration purposes and is not intended to perform meaningful actions.

/*
 * cc my_pipe_tst.c -o my_pipe_tst
 */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
main(void)
{
        int         ret;
        struct      flock f_lock;
        int         my_pipe[2];

if (pipe(my_pipe) < 0) { perror("Failed to Open Pipe\n\r"); exit(-1); }

f_lock.l_type   = F_WRLCK;
f_lock.l_whence = 0;
f_lock.l_start  = 0L;
f_lock.l_len    = 0;
ret = fcntl(my_pipe[1], F_GETLK, &f_lock);

if (ret == -1) printf("Return Value from fcntl() is %d\n", ret);

if (f_lock.l_type != F_UNLCK) printf("Errno Value from fcntl() is %d\n", errno);

printf("Exiting\n");
        exit(0);
}

When building and running this program on SCO OpenServer 5.0, the program fails with fcntl(S) returning -1 and errno(S) set to EINVAL (22), for example:

OSr5# ./my_pipe_tst Return Value from fcntl() is -1 Errno Value from fcntl() is 22 Exiting OSr5#

If the program is built on SCO Open Server 3.0 and run on SCO OpenServer 5.0, this program will fail in the same manner.

However, when the same program is built and run on SCO OpenServer 3.0, these same operations complete successfully, for example:

OSr3# ./my_pipe_tst
Exiting
OSr3#

Cause

In SCO OpenServer 5.0, pipes are now implemented by default as part of the High Performance Pipe System (HPPS) (see HPPS(ADM)). In SCO OpenServer 3.0 and earlier releases, pipes were implemented as part of the UNIX System V (S5) pipe filesystem.

The HPPS kernel driver does not support application locks via the
fcntl(S) system call and hence rejects such operations with errno(S)
set to EINVAL ("Invalid argument"). This is true for the fcntl(S)
lock operations F_GETLK, F_SETLK, and F_SETLKW.

Solution

This code works properly in release 5.0.4.

As an alternative to changing the program source to implement its pipe locking via a different mechanism, such as semaphores, it is instead possible to revert back to using the S5-based pipes. In this way, the above example program will perform successfully as it did on SCO OpenServer 3.0, for example:

OSr5# pipe -l       (list the contents of the pipe fs stack)
HPPS
/dev/root

OSr5# ./my_pipe_tst Return Value from fcntl() is -1 Errno Value from fcntl() is 22 Exiting

OSr5# pipe -p       (pop the top-most pipe fs, e.g. HPPS,
                                                from stack)
OSr5# pipe -l
/dev/root
OSr5# ./my_pipe_tst
Exiting
OSr5# pipe -s HPPS  (pushes HPPS back onto the pipe fs stack
                                                (optional))
OSr5# pipe -l
HPPS
/dev/root
NOTE:     Changing the pipe filesystem to S5 will apply system-wide.
          You may want to consider reinstating HPPS once more when your
          program has completed, as outlined in the above example command
          sequence.

From the Xinuos technical article database, record 100856. Reproduced with permission. The original is at www.sco.com/ta/100856, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.