Thread (12 messages) 12 messages, 6 authors, 2011-09-28

Prevent a process from opening a file more than once

From: Venkatram Tummala <hidden>
Date: 2011-09-28 04:41:28

On Tue, Sep 27, 2011 at 9:19 PM, rohan puri [off-list ref] wrote:

On Wed, Sep 28, 2011 at 6:17 AM, Venkatram Tummala <venkatram867@gmail.com
quoted
wrote:
quoted
On Tue, Sep 27, 2011 at 5:40 PM, Jeff Haran [off-list ref]wrote:
quoted

From: kernelnewbies-bounces at kernelnewbies.org
[mailto:kernelnewbies-bounces at kernelnewbies.org] On Behalf Of Venkatram
Tummala
Sent: Tuesday, September 27, 2011 5:31 PM
To: Mulyadi Santosa
Cc: kernelnewbies
Subject: Re: Prevent a process from opening a file more than once

On Tue, Sep 27, 2011 at 5:22 PM, Mulyadi Santosa
[off-list ref] wrote:
Hi :)

On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala
[off-list ref] wrote:
quoted
Hi All,
I have a simple device driver which creates a /dev/XYZ file. I need to
prevent a process from opening the file more than once. However,
multiple
quoted
processes can open the file simultaneously. Is there any any elegant
way to
quoted
do this other than checking all opened files in the process ?
Uhm, keep a reference count and increment it on every file open in
your module? How does that sound?
Well, which refcount should i use? I can't use the refcount in the file
object as the file objects passed to me are different each time the file
is opened in the process.

When you say "I need to prevent a process from opening the file more
than once.", do you mean a single process opening the file, closing it
and then opening it again would be disallowed?
No. If the file is already opened in the process, the process shouldn't
 be allowed to open the file again. It is fine if the process opens, closes
& then opens the file again.
quoted
Or do you mean that a
single process opening the file, keeping it open and then opening it
again under another fd would be disallowed?
Yes, this is what i am looking for.
quoted
How about multiple threads within the same process? Are they treated as
the same process by these rules?
Yes. Threads are treated as the same process. So, if one thread has the
file already opened, another thread in the same process shouldn't be able to
open it.

Venkat


_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Hi Venkatram,
I agree with Mulyadi, you maintain a static global variable (int),

in device_open() ->

if(var)
      return -EBUSY
var++

&

in device_release() ->

var--


I think this should do the job.
This will prevent other processes to open the file until a process releases
it. This is not what i need. Only the threads in a process shouldn't be able
to open the file if it is already opened in the process. Other processes
should be able to open it.

Venkat
Regards,
Rohan Puri
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110927/6739564c/attachment.html 
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help