[comp.bugs.sys5] O_EXCL flag on open of character tty device.

fsg@holos0.UUCP (Frank Glass) (11/08/88)

	My understanding from the manuals is that the O_EXCL flag can
be used to exclusively open a device.  The following code works
as I expect on an AT&T 6386 in that the first open fails if a process
(like getty, for example) has tty00 open, otherwise the second open
fails.  Contrary to my expectation, _both_ opens succeed on the 3b2,
the NCR Tower, and a variety of other System V things.  Do I not
understand the documentation?

--------------------cut here----------------------------
#include <termio.h>
#include <fcntl.h>
#include <stdio.h>
main(argc,argv)
{
	int ii;
	char *tty_id = "/dev/tty00"; /* arbitrary tty choice */

	ii = open(tty_id,O_WRONLY|O_NDELAY|O_EXCL);
	if(ii<0) {
		fprintf(stderr,"First open attempt, ");
		perror(tty_id);
		exit(1);
	}
	ii = open(tty_id,O_WRONLY|O_NDELAY|O_EXCL);
	if(ii<0) {
		fprintf(stderr,"Second open attempt, ");
		perror(tty_id);
		exit(1);
	}
	fprintf(stderr,"Opened %s with no errors, O_EXCL a mystery.\n",tty_id);
}
-- 
Frank Glass
Holos Software, Inc.
Voice: (404) 496-1358
UUCP: ...!gatech!holos0!fsg

guy@auspex.UUCP (Guy Harris) (11/09/88)

>	My understanding from the manuals is that the O_EXCL flag can
>be used to exclusively open a device.

That's not what its original intent was; it was an "exclusive create"
flag, not an "exclusive-use" open flag - it said that if

	1) O_CREAT was also set

and

	2) the file already existed

the "open" should fail, returning -1 and setting "errno" to EEXIST.

Some vendors may have made some of their device drivers interpret it as
an exclusive-use "open", but this is not guaranteed to work everywhere.

mercer@ncrcce.StPaul.NCR.COM (Dan Mercer) (11/09/88)

In article <1464@holos0.UUCP> fsg@holos0.UUCP (Frank Glass) writes:
:
:	My understanding from the manuals is that the O_EXCL flag can
:be used to exclusively open a device.  The following code works
:as I expect on an AT&T 6386 in that the first open fails if a process
:(like getty, for example) has tty00 open, otherwise the second open
:fails.  Contrary to my expectation, _both_ opens succeed on the 3b2,
:the NCR Tower, and a variety of other System V things.  Do I not
:understand the documentation?
:
:--------------------cut here----------------------------
:#include <termio.h>
:#include <fcntl.h>
:#include <stdio.h>
:main(argc,argv)
:{
:	int ii;
:	char *tty_id = "/dev/tty00"; /* arbitrary tty choice */
:
:	ii = open(tty_id,O_WRONLY|O_NDELAY|O_EXCL);
:	if(ii<0) {
:		fprintf(stderr,"First open attempt, ");
:		perror(tty_id);
:		exit(1);
:	}
:	ii = open(tty_id,O_WRONLY|O_NDELAY|O_EXCL);
:	if(ii<0) {
:		fprintf(stderr,"Second open attempt, ");
:		perror(tty_id);
:		exit(1);
:	}
:	fprintf(stderr,"Opened %s with no errors, O_EXCL a mystery.\n",tty_id);
:}
:-- 
:Frank Glass
:Holos Software, Inc.
:Voice: (404) 496-1358
:UUCP: ...!gatech!holos0!fsg
Don't be fooled by the term exclusive.  The only documentation I found
indicates that the flag must be used with O_CREAT,  and failure depends
not on whether the file is open,  but whether it already exists.

Dan Mercer
NCR Comten

wescott@sauron.Columbia.NCR.COM (Mike Wescott) (11/09/88)

In article <1464@holos0.UUCP> fsg@holos0.UUCP (Frank Glass) writes:
> 
> 	My understanding from the manuals is that the O_EXCL flag can
> be used to exclusively open a device.

Nope. The man page says:

	O_EXCL	If O_EXCL and O_CREAT are set, the function open will
		fail if the file exists.

It does give you a chance to create a new file and know that you didn't clobber
anything.
-- 
	-Mike Wescott
	 mike.wescott@ncrcae.Columbia.NCR.COM

allbery@ncoast.UUCP (Brandon S. Allbery) (11/15/88)

As quoted from <1464@holos0.UUCP> by fsg@holos0.UUCP (Frank Glass):
+---------------
| 	My understanding from the manuals is that the O_EXCL flag can
| be used to exclusively open a device.  The following code works
| as I expect on an AT&T 6386 in that the first open fails if a process
| (like getty, for example) has tty00 open, otherwise the second open
| fails.  Contrary to my expectation, _both_ opens succeed on the 3b2,
| the NCR Tower, and a variety of other System V things.  Do I not
| understand the documentation?
+---------------

Unless O_EXCL has undergone considerable revision since System V Release 2,
you've got a misunderstanding.

(1) O_EXCL only works with O_CREAT; it is ignored otherwise.
(2) Its function is to insure that a file does NOT exist before its creation.
    This is used to allow for atomic lockfile creation:  there is no window
    of vulnerability between the check for the file's existence and its
    creation, so no race condition can result in two or more processes
    creating the file at the same time.

It is possible that System V.3.2 has decided to overload O_EXCL to mean the
same thing as BSD's TIOCEXCL, i.e. exclusive use of a tty device.  But I'm
almost certain that the 3b2 and NCR Tower have the original semantics.

++Brandon
-- 
Brandon S. Allbery, comp.sources.misc moderator and one admin of ncoast PA UN*X
uunet!hal.cwru.edu!ncoast!allbery  <PREFERRED!>	    ncoast!allbery@hal.cwru.edu
allberyb@skybridge.sdi.cwru.edu	      <ALSO>		   allbery@uunet.uu.net
comp.sources.misc is moving off ncoast -- please do NOT send submissions direct
      Send comp.sources.misc submissions to comp-sources-misc@<backbone>.