Solved

flock() not working in my Perl script.. Why??

Posted on 2007-03-19
4
758 Views
Last Modified: 2013-11-17
I'm trying to test if a file is opened by another process but am not getting any results for the flock command in Perl. I have a very simple test script:

#!/usr/bin/perl
use Fcntl qw(:DEFAULT :flock);
use strict;
open(FH, "<test.txt");
print flock(FH,LOCK_EX|LOCK_NB);
close(FH);

No mater if the file is opened or not, it returns 0 all the time, which means that it is locked. What am I doing wrong? It is running on Unix AIX. Is there a system command I should use or is flock pretty accurate at the OS level? I'm asking because not only my script is accessing this file, but a completely different software program is access the file. So I want to be able to detect is the file is open at all but I'm not getting any results. Please help! Thanks in advance.
0
Comment
Question by:bemara57
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 18749181
flock doesn't check whether a file is opened, it checks whether another process has a flock on the file
I notice you do not check whether the open succeded or not.
If the open failed, then so will the flock.
0
 

Author Comment

by:bemara57
ID: 18749771
So if another process is using the file but doesn't use any flock, how can I determine if the file is used by that other process? Any alternatives to flock? All i want to do is detect if the file is in use so I can modify it, otherwise I want it stuck in a loop until it is freed up (maybe 10 minute loop-kill).
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 18751823
All locking mechanisms work on the same method, ie:  *all* processes that write to a file have to use the *exact* same locking mechanism, otherwise it's useless.

If you have no control over the other processes that can write to the file, then probably your best bet is to use the output of 'lsof' to see if the file is already opened.
0
 
LVL 84

Expert Comment

by:ozo
ID: 20402062
using lsof can suffer from a race condition if another process opens a file in between the time you do the lsof and the time you modify the file.
If  the other process won't cooperate in using locking mechanism, and it is only doing reads,
and you are the only one doing writes, you might be able to do something like make a copy of the file, do your modifications on the copy, then rename the copy to the original.
Then any other process that opens the file will either get the original or the modification, and not some intermediate state.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now