For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > September 2006 > how to tell if a file is open









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to tell if a file is open
shilpa.saraogi@gmail.com

2006-09-21, 7:03 pm


Heya all,

How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.

My program needs to open files and work on it, and I want it to make
sure that the file is not being written to (e.g. by scp, cp, mv etc)
before I attampt to open it. Is there any function in C to see the
status of the file ? Using/implementing locking mechanism is out of the
question. If not provided by the language C, is there anything provided
by the Unix operatins system ?

Shilpa

Pascal Bourguignon

2006-09-21, 7:03 pm

"shilpa.saraogi@gmail.com" <shilpa.saraogi@gmail.com> writes:
> How do I tell, prgrammatically, if a file is opened for read/write by
> some other process? In unix .. of course.
>
> My program needs to open files and work on it, and I want it to make
> sure that the file is not being written to (e.g. by scp, cp, mv etc)
> before I attampt to open it. Is there any function in C to see the
> status of the file ? Using/implementing locking mechanism is out of the
> question. If not provided by the language C, is there anything provided
> by the Unix operatins system ?


There is nothing that will prevent another process to open and modify
the file at the same time.

That's why the wall(1) command was invented.

You'd go:

% wall <<EOF
Hey people! I'm going to work on file /data/some-file
Please don't touch it until further notice!
EOF
% mytool /data/some-file



If you want to be definitely sure that you are the only one to have a
handle to the file you'd need to:

1- open it (this creates one more handle to the file). open(2)

2- delete all the links there are on this file from the file system. unlink(2)

3- make sure that no other process still has an open handle on it.
This is the tricky part. On linux you could use lsof(1), possibly
before step 2, but there is always the possibility that another process
opens the file too before you delete all its names.

4- work on the file. Since all the names for this file are deleted from
the file system, nobody else can get a handle on it. When you're done
your only option is to close the file and have it being deleted.



Another way would be to keep the only link to the file in a directory
that is only accessible by responsible users (perhaps root).


Perhaps you're wanting to use some other kind of operating system?
eros? plan9?

--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
David Schwartz

2006-09-21, 7:03 pm


shilpa.saraogi@gmail.com wrote:

> How do I tell, prgrammatically, if a file is opened for read/write by
> some other process? In unix .. of course.
>
> My program needs to open files and work on it, and I want it to make
> sure that the file is not being written to (e.g. by scp, cp, mv etc)
> before I attampt to open it. Is there any function in C to see the
> status of the file ? Using/implementing locking mechanism is out of the
> question. If not provided by the language C, is there anything provided
> by the Unix operatins system ?


That just isn't the UNIX way, and if you try to make UNIX work the way
you think it should, rather than the way it always has, you will suffer
a lot of pain.

Processes are either cooperating or they are not. If they are
cooperating, then any sensible method will work. If they are not
cooperating, then there is nothing you can do. You really can't
distinguish between a process that is modifying a file and a process
that is about to modify a file.

Consider 'scp' as an example. One could code 'scp' to open a file,
write to it as it received data, and then close the file. But one could
equally well code 'scp' to open a file, write to it, and close it as it
got each block of data. One can make a variety of arguments about the
superiority or inferiority of each method.

One heuristic that might be helpful is to check on the file
periodically, looking each time at its last modified time. If you see
it being modified every second, then ten seconds with no modification
might be enough to convince you that it's "stable enough". It really
depends upon exactly what you want to do to the file.

DS

William Ahern

2006-09-21, 7:03 pm

On Thu, 21 Sep 2006 22:51:38 +0200, Pascal Bourguignon wrote:
<snip>
> There is nothing that will prevent another process to open and modify
> the file at the same time.


Wouldn't SysV Mandatory Locks suffice? Assuming it was supported?


William Ahern

2006-09-21, 7:03 pm

On Thu, 21 Sep 2006 12:25:33 -0700, shilpa.saraogi@gmail.com wrote:
>
> Heya all,
>
> How do I tell, prgrammatically, if a file is opened for read/write by some
> other process? In unix .. of course.
>
> My program needs to open files and work on it, and I want it to make sure
> that the file is not being written to (e.g. by scp, cp, mv etc) before I
> attampt to open it. Is there any function in C to see the status of the
> file ? Using/implementing locking mechanism is out of the question.


So, it's okay if you check a file, everything seems to be okay, you
open that file, you being your work, and while you're doing your work
something else starts to modify it?

Gordon Burditt

2006-09-21, 7:03 pm

>How do I tell, prgrammatically, if a file is opened for read/write by
>some other process? In unix .. of course.


That information will be out of date by the time you attempt to use
it, unless you use file locking to PREVENT someone from opening or
closing the file between the time you check it and the time you open it.

>My program needs to open files and work on it, and I want it to make
>sure that the file is not being written to (e.g. by scp, cp, mv etc)
>before I attampt to open it. Is there any function in C to see the
>status of the file ?


The Standard C language has no concept of "other process", therefore
there is nothing else to have the file open.

"mv" does not write to files. It renames them. The file you CHECK
under one name is not necessarily the same file as the one you OPEN
later under the same name.

>Using/implementing locking mechanism is out of the
>question. If not provided by the language C, is there anything provided
>by the Unix operatins system ?


No matter how much you check, something will eventually jump in
between checks and mess it up (maliciously or otherwise: timing
attacks have produced real exploits). What *ATOMIC* operation do
you need? Open file but fail if it's already open?

Gordon L. Burditt
Bivek Agrawal

2006-09-21, 7:03 pm


William Ahern wrote:
> On Thu, 21 Sep 2006 12:25:33 -0700, shilpa.saraogi@gmail.com wrote:
>
> So, it's okay if you check a file, everything seems to be okay, you
> open that file, you being your work, and while you're doing your work
> something else starts to modify it?


No. It is not okay. (and that makes it harder). Probably the best thing
to do would be to call system(cp original_file new_copy) and then work
on the new_copy.

- ss

Pascal Bourguignon

2006-09-21, 7:03 pm

"Bivek Agrawal" <bivek.agrawal@gmail.com> writes:

> William Ahern wrote:
>
> No. It is not okay. (and that makes it harder). Probably the best thing
> to do would be to call system(cp original_file new_copy) and then work
> on the new_copy.


And what if some other process starts to modify the file during the cp?

Unix doesn't provide any solution because there's no solution. The
only thing you can do is to push the problem a little more farther,
but it'll come back to bite you one way or another. For example, in
the way of dead locks between processes trying to access files locked
by other processes.

--
__Pascal Bourguignon__ http://www.informatimago.com/
Grace personified,
I leap into the window.
I meant to do that.
Nils O. Selåsdal

2006-09-22, 4:01 am

shilpa.saraogi@gmail.com wrote:
> Heya all,
>
> How do I tell, prgrammatically, if a file is opened for read/write by
> some other process? In unix .. of course.
>
> My program needs to open files and work on it, and I want it to make
> sure that the file is not being written to (e.g. by scp, cp, mv etc)
> before I attampt to open it. Is there any function in C to see the
> status of the file ? Using/implementing locking mechanism is out of the
> question. If not provided by the language C, is there anything provided
> by the Unix operatins system ?

What if someone opened it right after you checked if anyone had it open
?

You're better off implementing a locking scheme(which might be as simple
as "I assume noone else ever messes with the file") and blame the
one that doesn't follow that.
Michael Kerrisk

2006-09-22, 8:00 am

On Thu, 21 Sep 2006 15:09:13 -0700, William Ahern
<william@25thandClement.com> wrote:

>On Thu, 21 Sep 2006 22:51:38 +0200, Pascal Bourguignon wrote:
><snip>
>
>Wouldn't SysV Mandatory Locks suffice? Assuming it was supported?


That's only going to work if (the system supports mandatory locking,
and) the file in question has mandatory locking enabled ("chmod
g+s,g-x"). Some systems also require special mount options for the
file system (e.g., Linux needs "mount -o mand"). So it can't be a
general solution.

Cheers,

Michael
Chuck Dillon

2006-09-22, 8:00 am

shilpa.saraogi@gmail.com wrote:
> Heya all,
>
> How do I tell, prgrammatically, if a file is opened for read/write by
> some other process? In unix .. of course.
>
> My program needs to ...


To summarize the responses you need to stop thinking about your program
and focus on designing a *system* that to solve your problem.

-- ced


--
Chuck Dillon
Manager of Software Development, Bioinformatics
NimbleGen Systems Inc.
Rainer Weikusat

2006-09-22, 7:01 pm

Chuck Dillon <spamless@nimblejen.com> writes:
> shilpa.saraogi@gmail.com wrote:
>
> To summarize the responses you need to stop thinking about your
> program and focus on designing a *system* that to solve your
> problem.


It's probably easier to just document that X must not be modified
while Z is doing Y and leave the descision to do or avoid doing so to
a sufficiently priviledged individual (which is what you'll get in any
case, no matter how thick a smokescreen might try to shield you from
realizing that).
shilpa.saraogi@gmail.com

2006-09-22, 7:01 pm


Nils O. Sel=E5sdal wrote:
> shilpa.saraogi@gmail.com wrote:
> What if someone opened it right after you checked if anyone had it open
> ?
>
> You're better off implementing a locking scheme(which might be as simple
> as "I assume noone else ever messes with the file") and blame the
> one that doesn't follow that.


Thank you all for the replies and suggestions. I was pointed to the
source of 'fuser' command to draw some inspiration from. (fuser-
identify processes using files or sockets). But then, as pointed out
already by many, even if I am able to somehow figure out who else has
that file open, I cannot prevent anyone from writing to it after
checking...

vasudevram

2006-09-27, 7:02 pm


shilpa.saraogi@gmail.com wrote:
> Nils O. Sel=E5sdal wrote:
he[color=darkred]
ed[color=darkred]
>
> Thank you all for the replies and suggestions. I was pointed to the
> source of 'fuser' command to draw some inspiration from. (fuser-
> identify processes using files or sockets). But then, as pointed out
> already by many, even if I am able to somehow figure out who else has
> that file open, I cannot prevent anyone from writing to it after
> checking...


Adding my 2c, though it seems you've got the idea ...

A lot of good replies by others ...

As one of the other posters replied, it pays to think in systems terms,
not just programming terms, even if you're not a system architect.
Programmers do need to consider the systems they're designing, or in
which their programs are going to work, not just the program's own
design and code - i.e. in what environment it is going to exist, what
other processes and users will be doing etc. e.g. if on your own box,
and you're the only user, no problem exists. If its in a corporate or
other organizational environment, talk to the system administrator for
their advice on setting up the right environment - using UNIX
appropriate permissions on files and directories, adding you to the
appropriate group, etc. This will prevent at least some case of other
users modifying the file, but not all. The superuser (root) can anyway
modify your file.

If in a hosted UNIX environment (e.g. for web apps), the proper
permissions will likely already have been set so that others will not
easily be able to modify your files.

lsof and fuser can be useful ... , so might be commands variously
called truss, trace, or strace on different UNIX flavours - the last
three trace the system calls that a process is invoking as it runs.

Vasudev Ram
http://www.dancingbison.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com