[Samba] [SMB 3.0.10] File Locking Mechanism Windows <-> Unix

Jeremy Allison jra at samba.org
Tue Mar 15 18:02:31 GMT 2005


On Tue, Mar 15, 2005 at 07:54:38PM +0530, Asif, M wrote:
> Hello Jeremy,
> 
> Thanks for your response.
> 
> I tried the following
> 
> On Windows side, an MFC application with the code given below access the
> file using the samba share.
> 
> 
> 			CString strFile =
> _T("\\\\180.144.1.11\\netmbase\\tmp\\test.txt");
> 		CStdioFile fValueFile;
> 			if (!fValueFile.Open( strFile ,CFile::modeWrite |
> CFile::shareExclusive    ) )
> 			{
> 				wcout<<_T("Open failed")<<endl;
> 			}
> 			else
> 			{
> 				//wcout<<_T("Open Success")<<endl;
> 			}
> 
> 		fValueFile.WriteString( strt );
> 		getchar()
> 
> 		fValueFile.Close();
> 
> On UNIX side, code is something similar to this
> 
> 		int fid =
> open("/export/home/scsd13/kiran/NetMBase/tmp/test.txt", O_RDWR|O_EXCL ) ;
> 		if( 0 > fid )
> 		{
> 			perror("open"),exit(1);
> 		}
> 
> 		struct flock lockDetails ;
> 		lockDetails.l_start = 0 ;
> 		lockDetails.l_whence = SEEK_SET;
> 		lockDetails.l_len = 0 ;
> 		lockDetails.l_type = F_WRLCK ;
> 		int nErr = fcntl( fid, F_SETLK, &lockDetails);
> 		if( 0 > nErr )
> 		{
> 			perror("lock"),exit(1);
> 		}
> 		getchar();
> 		close(fid);
> 	
> Now,
> 
> 1. Two instances of the executable runs on Windows side, open fails with
> sharing violation if the first one is not released ( blocked at getchar()
> call) , no problem.
> 2. Same is the case with two instances of UNIX processes.
> 3.The unix process locks the file and does not release it (blocked at
> getchar() ), and then the windows client is executed, WriteString throws an
> exceptoin giving error 33(locked by other process). No problem 4. Now if the
> Windows client opens the file first (blocked at getchar() ) and then the
> unix client is invoked, the fcntl call succeeds... 
> 
> I tried lockf() instead of fcntl(), turned on mandatory locking etc, but
> with no success.
> 
> Am I missing out some thing? Is there an easier way for getting this work?
> Basically, I want to ensure that there is no read happening when any one
> (from unix or windows) is writing into the file an vice versa.

What OS are you running on ? This code will work on Linux with kernel
oplocks, and if you turn off oplocks should work on other UNIXes.

Jeremy.


More information about the samba mailing list