1837112 Members
2874 Online
110112 Solutions
New Discussion

fcntl newbie

 
SOLVED
Go to solution
mango_1
Frequent Advisor

fcntl newbie

hello all! I would like to ask about how fcntl works? I currently have a program that reads/writes to a (text) file and I don't want any other programs (i.e. vi) to be able to edit it if I have the file locked in the other program. How do I do this? I was trying to lock the file for reading in my program and disallow any writes to that file using vi. But when I tried it out. I was able to modify the file. :((

please help. any help is much appreciated. thanks!

6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: fcntl newbie

Fcntl is working EXACTLY as it is supposed to. By default, the file locks are advisory; meaning that all processes have to check for file locks and observe them but it's strictly an honor-code thing. If an application wasn't coded to test for the locks then it can't respect them. You can change the locking to mandatory by setting the setgid (02000) bit on a non-executable file.
If it ain't broke, I can fix that.
mango_1
Frequent Advisor

Re: fcntl newbie

thanks for the input. I was wondering if there is any way for me to prevent them from modifying the text file thru vi only when I have the file locked in the program? From what you are saying, I don't think it's possible since vi doesn't check for file locking? Can you please tell me how I could use the setgid(02000)? how come its 02000?

thanks again! :)
A. Clay Stephenson
Acclaimed Contributor

Re: fcntl newbie

chmod 2664 myfile would set the setgid bit (2000 octal), r+w (600 octal ), r+w (60 octal), r (4 octal) for myfile. Man chmod for details.
If it ain't broke, I can fix that.
mango_1
Frequent Advisor

Re: fcntl newbie

hello again! I don't think I could use mandatory locking since there are multiple instances of the program and setting it to mandatory will disallow any reads from those program.

I'm stuck. :( any help is much appreciated.
thanks!
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: fcntl newbie

I suspect you are using write locks (F_WRLCK) change those to read locks (F_RDLCK) and leave the setuid bit on. I suspect that this is just the behavior that you are looking for.
If it ain't broke, I can fix that.
mango_1
Frequent Advisor

Re: fcntl newbie

Hello again! I noticed some weird behavior with fcntl. If you have a file locked and I opened it under vi. It hangs. It doesn't allow me to read it. I think vi might be waiting indefinitely for the program to unlock the file. But if the program performed a read lock to the file, the vi can open it but cannot write to it. Is it possible to be able to write lock the file and for vi to open the file for reading or if not say "text busy" or something?

thanks again! :D