This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

1.3.3 - fcntl locking + rename bug


Hi all,

I've been fighting LPRng for about a month now trying to get it working on
cygwin, and have finally succeeded. Hooray!

However, I think I have discovered a bug in the following situation, which
occurs frequently in LPRng:

Open a status file (named lpq in LPRng's case) for WR, and get a lock on
it with fcntl (call it lockfd).

Create a tempfile (tempfd), stuff it with info, and close tempfd.

Rename tempfile to status file - this causes error "Permission Denied".

Close lockfd.

To get LPRng to work sucessfully, I had to move the "close lockfd"
*before* attempting to rename the tempfile.  I have written a small test
program (composed mainly of code straight from LPRng) to verify the above
behavior (locktest.c) and it does reproduce the problem as it occurs in
LPRng on cygwin.  The test program (as well as LPRng) both work fine on my
OpenBSD machine.

Attached is the output of cygcheck -s -v -r as well as the test program.

Thanks!  If people are interested in LPRng for Cygwin, please let me know
- I'm now going to get the setuid stuff working.

Luke Bakken
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>


int Do_lock( int fd, int block );


int main(void)
{
	int tempfd, lockfd;
	int err;
	struct stat statb;
	char pathname[1024];
	char *string = "THIS IS THE TEST STRING\n";


	/* first, create tempfile with mkstemp, write
	   the string to it, and close the file */

	strcpy(pathname, "tempdXXXXXX");
	tempfd = mkstemp( pathname );
	fprintf(stderr, "mkstemp created file %s\n", pathname);
	if( tempfd == -1 ){
		fprintf(stderr, "Make_temp_fd_in_dir: cannot create tempfile '%s'\n", pathname);
	}
	if( fchmod(tempfd,0600 ) == -1 ){
		fprintf(stderr, "Make_temp_fd_in_dir: chmod '%s' to 0%o failed\n",
			pathname, 0600);
	}
	if( stat(pathname,&statb) == -1 ){
		fprintf(stderr, "Make_temp_fd_in_dir: stat '%s' failed\n", pathname );
	}
	printf("Make_temp_fd_in_dir: fd %d, name '%s', perms 0%o, owner %d, group %d\n", tempfd, pathname, statb.st_mode, statb.st_uid, statb.st_gid );

	if(!write(tempfd, string, strlen(string)))
	{
		fprintf(stderr, "write to %s failed\n", pathname);
	}
	if(close(tempfd))
	{
		fprintf(stderr, "close of %s failed\n", pathname);
	}



	/* now, open and lock the file to which we are renaming */
	lockfd = open("lpq", O_RDWR | O_CREAT, 0600);
	if(lockfd < 0)
	{
		fprintf(stderr, "error opening lpq for write\n");
	}
	Do_lock(lockfd, 0);
	err = rename(pathname, "lpq");
	printf("Rename returned %d\n", err);
	close(lockfd);
	unlink(pathname);
	return( 0 );
}

int Do_lock( int fd, int block )
{
    int code = -2;

	fprintf(stderr, "Do_lock: fd %d, block '%d'\n", fd, block );

	if( code == -2 ){
		struct flock file_lock;
		int err;
		int how;
		fprintf(stderr, "Do_lock: using fcntl with SEEK_SET, block %d\n", block );

		how = F_SETLK;
		if( block ) how = F_SETLKW;

		memset( &file_lock, 0, sizeof( file_lock ) );
		file_lock.l_type = F_WRLCK;
		file_lock.l_whence = SEEK_SET;
		code = fcntl( fd, how, &file_lock);
		if( code < 0 ){
			code = -1;
		} else {
			code = 0;
		}
		fprintf(stderr, "devlock_fcntl: status %d\n", code );
	}

	fprintf(stderr, "Do_lock: status %d\n", code);
	return( code);
}

Cygnus Win95/NT Configuration Diagnostics
Current System Time: Wed Oct 10 07:53:30 2001

WinNT Ver 4.0 build 1381 Service Pack 6

Path:	/usr/local/bin
	/usr/bin
	/bin
	/mnt/c/global/bin
	/mnt/c/bcc/bin
	/mnt/c/Perl/bin/
	/mnt/c/vim/vim58
	/mnt/c/PKZIP
	/mnt/c/RES
	/mnt/c/RES/POSIX
	/mnt/c/ghostgum/gsview
	/mnt/c/WINNT/system32
	/mnt/c/WINNT
	/usr/bin
	/usr/bin
	/usr/local/bin
	/mnt/c/usr/local/bin
	/mnt/c/scripts

SysDir: C:\WINNT\System32
WinDir: C:\WINNT

PWD = `/home/lukeb/locks'
CYGWIN = `binmode tty ntsec ntea'
USER = `lukeb'
MAKE_MODE = `unix'
HOME = `/home/lukeb'

NUMBER_OF_PROCESSORS = `1'
LOGONSERVER = `\\CGSERVER'
OS2LIBPATH = `C:\WINNT\system32\os2\dll;'
COLORFGBG = `15;default;0'
COMSPEC = `C:\WINNT\system32\cmd.exe'
WINDOWID = `167837848'
PAGER = `less'
SYSTEMDRIVE = `C:'
HOSTNAME = `CBINFRALXB'
PROCESSOR_REVISION = `0502'
PATHEXT = `.BAT;.COM;.EXE;.CMD;.PLX;.VBS;.VBE;.JS;.JSE;.WSF;.WSH'
HTTP_proxy = `http://129.189.10.254:80'
COMPUTERNAME = `CBINFRALXB'
WINDIR = `C:\WINNT'
USERPROFILE = `C:\WINNT\Profiles\lukeb'
HTTP_PROXY = `http://129.189.10.254:80'
PS1 = `\[\033]0;\w\007
\033[32m\]\u@\h \[\033[33m\w\033[0m\]
$ '
MOADIR = `c:\ppnt'
CDSDDMODE = `development'
MACHTYPE = `i686-pc-cygwin'
PROCESSOR_IDENTIFIER = `x86 Family 6 Model 5 Stepping 2, GenuineIntel'
OS = `Windows_NT'
OLDPWD = `/home/lukeb'
PROCESSOR_ARCHITECTURE = `x86'
TEMP = `/mnt/c/TEMP'
NTRESKIT = `C:\RES'
PROCESSOR_LEVEL = `6'
SYSTEMROOT = `C:\WINNT'
COLORTERM = `rxvt-xpm'
TMP = `/mnt/c/TEMP'
HOMEDRIVE = `C:'
DISPLAY = `:0'
SSDIR = `\\CBSOURCE\CITIGROUP'
SHLVL = `1'
PPLUS_PW = `vu1can'
HOMEPATH = `\'
USERDOMAIN = `CBINFRA'
USERNAME = `lukeb'
SHELL = `/bin/bash'
HOSTTYPE = `i686'
OSTYPE = `cygwin'
TERM = `xterm'
ROOT_PW = `deathgr1p'
PPLUSDIR = `c:\ppnt'
SSUSER = `lukeb'
_ = `/usr/bin/cygcheck'
TZ = `PST8PDT7,M4.1.0/2,M10.5.0/2'

HKEY_CURRENT_USER\Software\Cygnus Solutions
HKEY_CURRENT_USER\Software\Cygnus Solutions\Cygwin
HKEY_CURRENT_USER\Software\Cygnus Solutions\Cygwin\mounts v2
  (default) = `/mnt'
  cygdrive flags = 0x00000022
HKEY_CURRENT_USER\Software\Cygnus Solutions\Cygwin\Program Options
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/
  (default) = `C:/cyg'
  flags = 0x0000000a
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/bin
  (default) = `C:/cyg/bin'
  flags = 0x0000000a
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\mounts v2\/usr/lib
  (default) = `C:/cyg/lib'
  flags = 0x0000000a
HKEY_LOCAL_MACHINE\SOFTWARE\Cygnus Solutions\Cygwin\Program Options

a:  fd           N/A    N/A                    
c:  hd  NTFS    9640Mb  38% CP CS UN PA FC     
d:  fd           N/A    N/A                    
e:  cd           N/A    N/A                    
x:  net NTFS    2047Mb  99% CP CS UN PA FC     cdrive

C:\cyg\bin  /usr/bin  system  binmode
C:\cyg\lib  /usr/lib  system  binmode
C:\cyg  /        system  binmode
c:    /mnt/c   user    binmode,noumount
x:    /mnt/x   user    binmode,noumount

Found: C:\cyg\bin\bash.exe
Found: C:\cyg\bin\cat.exe
Found: c:\RES\cat.exe
Warning: C:\cyg\bin\cat.exe hides c:\RES\cat.exe
Found: c:\RES\POSIX\cat.exe
Warning: C:\cyg\bin\cat.exe hides c:\RES\POSIX\cat.exe
Found: c:\usr\local\bin\cat.exe
Warning: C:\cyg\bin\cat.exe hides c:\usr\local\bin\cat.exe
Found: C:\cyg\bin\cpp.exe
Found: C:\cyg\bin\find.exe
Found: c:\RES\POSIX\find.exe
Warning: C:\cyg\bin\find.exe hides c:\RES\POSIX\find.exe
Found: c:\usr\local\bin\find.exe
Warning: C:\cyg\bin\find.exe hides c:\usr\local\bin\find.exe
Found: C:\cyg\bin\gcc.exe
Found: c:\usr\local\bin\gcc.exe
Warning: C:\cyg\bin\gcc.exe hides c:\usr\local\bin\gcc.exe
Found: C:\cyg\bin\gdb.exe
Found: C:\cyg\bin\ld.exe
Found: c:\usr\local\bin\ld.exe
Warning: C:\cyg\bin\ld.exe hides c:\usr\local\bin\ld.exe
Found: C:\cyg\bin\ls.exe
Found: c:\RES\ls.exe
Warning: C:\cyg\bin\ls.exe hides c:\RES\ls.exe
Found: c:\RES\POSIX\ls.exe
Warning: C:\cyg\bin\ls.exe hides c:\RES\POSIX\ls.exe
Found: c:\usr\local\bin\ls.exe
Warning: C:\cyg\bin\ls.exe hides c:\usr\local\bin\ls.exe
Found: C:\cyg\bin\make.exe
Found: c:\bcc\bin\make.exe
Warning: C:\cyg\bin\make.exe hides c:\bcc\bin\make.exe
Found: c:\usr\local\bin\make.exe
Warning: C:\cyg\bin\make.exe hides c:\usr\local\bin\make.exe
Found: C:\cyg\bin\sh.exe
Found: c:\RES\POSIX\sh.exe
Warning: C:\cyg\bin\sh.exe hides c:\RES\POSIX\sh.exe
Found: c:\usr\local\bin\sh.exe
Warning: C:\cyg\bin\sh.exe hides c:\usr\local\bin\sh.exe

   56k 2000/12/03 C:\cyg\bin\cygbz21.0.dll - os=4.0 img=1.0 sys=4.0
                  "cygbz21.0.dll" v0.0 ts=2000/11/20 15:53
   45k 2001/04/25 C:\cyg\bin\cygform5.dll - os=4.0 img=1.0 sys=4.0
                  "cygform5.dll" v0.0 ts=2001/4/24 22:28
   34k 2001/09/30 C:\cyg\bin\cygform6.dll - os=4.0 img=1.0 sys=4.0
                  "cygform6.dll" v0.0 ts=2001/9/29 19:43
   18k 2000/10/23 C:\cyg\bin\cyggdbm.dll - os=4.0 img=1.0 sys=4.0
                  "cyggdbm.dll" v0.0 ts=2000/10/22 19:26
   17k 2001/06/28 C:\cyg\bin\cyghistory4.dll - os=4.0 img=1.0 sys=4.0
                  "cyghistory4.dll" v0.0 ts=2001/1/6 20:34
   21k 2001/06/28 C:\cyg\bin\cyghistory5.dll - os=4.0 img=1.0 sys=4.0
                  "cyghistory5.dll" v0.0 ts=2001/6/27 19:27
   21k 2001/06/20 C:\cyg\bin\cygintl.dll - os=4.0 img=1.0 sys=4.0
                  "cygintl.dll" v0.0 ts=2001/6/20 10:09
   81k 2000/12/05 C:\cyg\bin\cygitcl30.dll - os=4.0 img=1.0 sys=4.0
                  "cygitcl30.dll" v0.0 ts=2000/11/25 17:43
   35k 2000/12/05 C:\cyg\bin\cygitk30.dll - os=4.0 img=1.0 sys=4.0
                  "cygitk30.dll" v0.0 ts=2000/11/25 17:43
   45k 2001/07/04 C:\cyg\bin\cygjbig1.dll - os=4.0 img=1.0 sys=4.0
                  "cygjbig1.dll" v0.0 ts=2001/7/3 20:25
  119k 2001/06/06 C:\cyg\bin\cygjpeg6b.dll - os=4.0 img=1.0 sys=4.0
                  "cygjpeg6b.dll" v0.0 ts=2001/6/5 21:27
   26k 2001/04/25 C:\cyg\bin\cygmenu5.dll - os=4.0 img=1.0 sys=4.0
                  "cygmenu5.dll" v0.0 ts=2001/4/24 22:27
   19k 2001/09/30 C:\cyg\bin\cygmenu6.dll - os=4.0 img=1.0 sys=4.0
                  "cygmenu6.dll" v0.0 ts=2001/9/29 19:43
  156k 2001/04/25 C:\cyg\bin\cygncurses++5.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses++5.dll" v0.0 ts=2001/4/24 22:29
  175k 2001/09/30 C:\cyg\bin\cygncurses++6.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses++6.dll" v0.0 ts=2001/9/29 19:45
  226k 2001/04/25 C:\cyg\bin\cygncurses5.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses5.dll" v0.0 ts=2001/4/24 22:17
  201k 2001/09/30 C:\cyg\bin\cygncurses6.dll - os=4.0 img=1.0 sys=4.0
                  "cygncurses6.dll" v0.0 ts=2001/9/29 19:42
   15k 2001/04/25 C:\cyg\bin\cygpanel5.dll - os=4.0 img=1.0 sys=4.0
                  "cygpanel5.dll" v0.0 ts=2001/4/24 22:27
   12k 2001/09/30 C:\cyg\bin\cygpanel6.dll - os=4.0 img=1.0 sys=4.0
                  "cygpanel6.dll" v0.0 ts=2001/9/29 19:43
  163k 2001/05/06 C:\cyg\bin\cygpng2.dll - os=4.0 img=1.0 sys=4.0
                  "cygpng2.dll" v0.0 ts=2001/5/5 21:05
  108k 2001/06/28 C:\cyg\bin\cygreadline4.dll - os=4.0 img=1.0 sys=4.0
                  "cygreadline4.dll" v0.0 ts=2001/1/6 20:34
  135k 2001/06/28 C:\cyg\bin\cygreadline5.dll - os=4.0 img=1.0 sys=4.0
                  "cygreadline5.dll" v0.0 ts=2001/6/27 19:27
  390k 2000/12/05 C:\cyg\bin\cygtcl80.dll - os=4.0 img=1.0 sys=4.0
                  "cygtcl80.dll" v0.0 ts=2000/11/25 17:39
    5k 2000/12/05 C:\cyg\bin\cygtclpip80.dll - os=4.0 img=1.0 sys=4.0
   10k 2000/12/05 C:\cyg\bin\cygtclreg80.dll - os=4.0 img=1.0 sys=4.0
                  "cygtclreg80.dll" v0.0 ts=2000/11/25 17:39
  245k 2001/06/12 C:\cyg\bin\cygtiff3.dll - os=4.0 img=1.0 sys=4.0
                  "cygtiff3.dll" v0.0 ts=2001/6/12 10:25
  623k 2000/12/05 C:\cyg\bin\cygtk80.dll - os=4.0 img=1.0 sys=4.0
                  "cygtk80.dll" v0.0 ts=2000/11/25 17:43
   41k 2001/07/04 C:\cyg\bin\cygXpm-noX4.dll - os=4.0 img=1.0 sys=4.0
                  "cygXpm-noX4.dll" v0.0 ts=2001/7/3 18:21
   45k 2001/07/04 C:\cyg\bin\cygXpm-X4.dll - os=4.0 img=1.0 sys=4.0
                  "cygXpm-X4.dll" v0.0 ts=2000/11/19 18:45
   49k 2001/02/03 C:\cyg\bin\cygz.dll - os=4.0 img=1.0 sys=4.0
                  "cygz.dll" v0.0 ts=2001/2/3 12:35
  701k 2001/09/13 C:\cyg\bin\cygwin1.dll - os=4.0 img=1.0 sys=4.0
                  "cygwin1.dll" v0.0 ts=2001/9/12 20:54
    Cygwin DLL version info:
        dll major: 1003
        dll minor: 3
        dll epoch: 19
        dll bad signal mask: 19005
        dll old termios: 5
        dll malloc env: 28
        api major: 0
        api minor: 46
        shared data: 3
        dll identifier: cygwin1
        mount registry: 2
        cygnus registry name: Cygnus Solutions
        cygwin registry name: Cygwin
        program options name: Program Options
        cygwin mount registry name: mounts v2
        cygdrive flags: cygdrive flags
        cygdrive prefix: cygdrive prefix
        cygdrive default prefix: 
        build date: Wed Sep 12 23:54:31 EDT 2001
        shared id: cygwin1S3

Use -h to see help about each section
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]