[comp.sys.mips] flock

bin@primate.wisc.edu (Brain in Neutral) (07/24/89)

Synopsis:  flock does not return EWOULDBLOCK as advertised.

Description:  flock is supposed to return EWOULDBLOCK when trying to acquire
an exclusive lock on an open file descriptor and the "don't block" flag is
specified.  It doesn't, it returns EACCES "Permission denied" instead.  This
can be seen by compiling the code below and running two instances of it
simultaneously.

# include	<sys/file.h>
# include	<errno.h>


main ()
{
extern int	errno;
int		f;

	f = open ("junk", O_WRONLY|O_CREAT, 0644);
	if (f < 0)
	{
		perror ("f open");
		exit (1);
	}
	printf ("f opened\n");
	if (flock (f, LOCK_EX|LOCK_NB) < 0)
	{
		if (errno == EWOULDBLOCK)
		{
			perror ("f flock1");
			exit (1);
		}
		perror ("f flock2");
		exit(1);
	}
	printf ("f locked\n");
	sleep (10);
}

The first instance prints:
	f opened
	f locked

The second instance prints:
	f opened
	f flock2: Permission denied

This is not only wrong, but EACCES is not one of the possible error returns
mentioned in the man page.

Package/Version:

OS: RISC/os 4.0
Configuration:  MIPS M/120, 16MB, 2 328MB disks, cartridge tape, etc.

Suggested workaround:

Do

	if (errno == EWOULDBLOCK || errno == EACCES)

instead of

	if (errno == EWOULDBLOCK)

Of course this is ugly and shouldn't have to be done.

rogerk@mips.COM (Roger B.A. Klorese) (07/25/89)

In article <448@indri.primate.wisc.edu> bin@primate.wisc.edu (Brain in Neutral) writes:
>Synopsis:  flock does not return EWOULDBLOCK as advertised.

Thank you for your report.  This has been entered as bug #4809.  I will
advise you (and this group) as to a fix when it is scheduled.

Please continue to report problems, and to send general questions, to
hotline@mips.com.
-- 
ROGER B.A. KLORESE      MIPS Computer Systems, Inc.      phone: +1 408 720-2939
928 E. Arques Ave.  Sunnyvale, CA  94086                        rogerk@mips.COM
{ames,decwrl,pyramid}!mips!rogerk
"I want to live where it's always Saturday."  -- Guadalcanal Diary

reggers@uwovax.uwo.ca (Reg Quinton) (09/08/90)

I've been trying to get mh running on a Mips/120 here at Western. I'm
building things in a BSD environment, my search path is such that I
hit the bsd43/cc rather than the SYSV version.

Anyways, I've got stuck on the flock call used for file locking. Here's
a very simple program that demonstrates the problem.

----- cut here for test.c -----
#include <stdio.h>
#include <sys/file.h>

#define	FILNAM	".cshrc"
#define	MODE	O_RDONLY
#define	NOTOK	(-1)

main(argc,argv)
int	argc;
char	*argv[];

{
	int	fd;

	if ((fd=open(FILNAM,MODE,0777)) == NOTOK)
	{	perror(FILNAM);	exit(1);	}

	if (flock(fd,LOCK_EX | LOCK_NB) == NOTOK)
	{	perror("lock");	exit(2);	}

	printf("She works!\n");
}
----- cut here for test.c -----

If I "make test" and then "./test" in my home directory (where I have a
.cshrc file) I always get a perror message of this sort:

	lock: Bad file number

Any ideas what I'm doing wrong? Or how I can get this to work?

P.S. the above code works fine on our Suns and Next machines..

-- 
Telephone:	(519) 661 2151 x6026 (a real person and not a machine)
Canada:		reggers@uwo.ca (used to be UWO.CDN)
BITNET:		reggers@uwovax.BITNET (for the ethnocentric)
UUCP:		reggers@ria.UUCP (...uunet!utai!ria!reggers)

bin@primate.wisc.edu (Brain in Neutral) (09/10/90)

Are you running 4.5?
The bug you describe sounds like one I reported for RISC/os 4.0/4.01
(June or July '89) and if it's not yet fixed in 4.5 I'm going to be
quite disgusted.

Paul DuBois
dubois@primate.wisc.edu