--- SerialImp.c 2006-06-03 21:45:50.962728000 -0400 +++ SerialImp.c.new 2006-06-03 22:08:48.735159000 -0400 @@ -656,6 +656,24 @@ fd=OPEN (filename, O_RDWR | O_NOCTTY | O_NONBLOCK ); } while (fd < 0 && errno==EINTR); +#ifdef OPEN_EXCL + // Note that open() follows POSIX semantics: multiple open() calls to + // the same file will succeed unless the TIOCEXCL ioctl is issued. + // This will prevent additional opens except by root-owned processes. + // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details. + + if (fd >= 0 && (ioctl(fd, TIOCEXCL) == -1)) + { + sprintf( message, "open: exclusive access denied for %s\n", + filename ); + report( message ); + report_error( message ); + + close(fd); + goto fail; + } +#endif /* OPEN_EXCL */ + if( configure_port( fd ) ) goto fail; (*env)->ReleaseStringUTFChars( env, jstr, filename ); sprintf( message, "open: fd returned is %i\n", fd ); --- SerialImp.h 2006-06-03 20:21:08.927474000 -0400 +++ SerialImp.h.new 2006-06-03 22:09:38.840254000 -0400 @@ -136,7 +136,8 @@ /*# define LOCKDIR "/var/spool/uucp"*/ # define LOCKDIR "/var/lock" # define LOCKFILEPREFIX "LK." -# define UUCP +/*# define UUCP*/ +# define OPEN_EXCL #endif /* __APPLE__ */ #if defined(__NetBSD__) # define DEVICEDIR "/dev/"