Prevent a process from opening a file more than once
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 processes can open the file simultaneously. Is there any any elegant way to do this other than checking all opened files in the process ?
Hi :) On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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? -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
On Tue, Sep 27, 2011 at 5:22 PM, Mulyadi Santosa <mulyadi.santosa@gmail.com>wrote:
Hi :)
On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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.
-- regards,
Mulyadi Santosa Freelance Linux trainer and consultant
blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
On Wed, Sep 28, 2011 at 07:30, Venkatram Tummala <venkatram867@gmail.com> wrote:
On Tue, Sep 27, 2011 at 5:22 PM, Mulyadi Santosa <mulyadi.santosa@gmail.com> wrote:
Hi :)
On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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.
Sorry, I mean something that traps everytime that file is opened and increase a count. Thinking a while, inotify might do the job perfectly IMHO.... -- regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
From: kernelnewbies-bounces@kernelnewbies.org [mailto:kernelnewbies-bounces@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 <mulyadi.santosa@gmail.com> wrote: Hi :) On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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? 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? How about multiple threads within the same process? Are they treated as the same process by these rules?
On Tue, Sep 27, 2011 at 5:40 PM, Jeff Haran <jharan@bytemobile.com> wrote:
From: kernelnewbies-bounces@kernelnewbies.org [mailto:kernelnewbies-bounces@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 <mulyadi.santosa@gmail.com> wrote: Hi :)
On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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.
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.
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
On Wed, Sep 28, 2011 at 6:17 AM, Venkatram Tummala <venkatram867@gmail.com>wrote:
On Tue, Sep 27, 2011 at 5:40 PM, Jeff Haran <jharan@bytemobile.com> wrote:
From: kernelnewbies-bounces@kernelnewbies.org [mailto:kernelnewbies-bounces@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 <mulyadi.santosa@gmail.com> wrote: Hi :)
On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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.
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.
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@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. Regards, Rohan Puri
On Tue, Sep 27, 2011 at 9:19 PM, rohan puri <rohan.puri15@gmail.com> wrote:
On Wed, Sep 28, 2011 at 6:17 AM, Venkatram Tummala <venkatram867@gmail.com
wrote:
On Tue, Sep 27, 2011 at 5:40 PM, Jeff Haran <jharan@bytemobile.com>wrote:
From: kernelnewbies-bounces@kernelnewbies.org [mailto:kernelnewbies-bounces@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 <mulyadi.santosa@gmail.com> wrote: Hi :)
On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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.
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.
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@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
On Wed, Sep 28, 2011 at 10:11 AM, Venkatram Tummala <venkatram867@gmail.com>wrote:
On Tue, Sep 27, 2011 at 9:19 PM, rohan puri <rohan.puri15@gmail.com>wrote:
On Wed, Sep 28, 2011 at 6:17 AM, Venkatram Tummala < venkatram867@gmail.com> wrote:
On Tue, Sep 27, 2011 at 5:40 PM, Jeff Haran <jharan@bytemobile.com>wrote:
From: kernelnewbies-bounces@kernelnewbies.org [mailto:kernelnewbies-bounces@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 <mulyadi.santosa@gmail.com> wrote: Hi :)
On Wed, Sep 28, 2011 at 06:56, Venkatram Tummala <venkatram867@gmail.com> wrote:
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 processes can open the file simultaneously. Is there any any elegant way to 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.
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.
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@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
Hi Venkat,
If you want only threads in a process should not be able to open the file, heres an untidy solution :- 1. in open store the tgid in a hash table ( or any other data structure), if (storing sucessful) -> do the opening. else if(storing op return -EEXISTS) -> return -EBUSY 2. in release remove the tgid entry from the hash. Regards, Rohan Puri
Hi! On 21:41 Tue 27 Sep , Venkatram Tummala wrote:
On Tue, Sep 27, 2011 at 9:19 PM, rohan puri <rohan.puri15@gmail.com> wrote: ...
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.
You could create something like this: DEFINE_MUTEX(pidlist_lock); LIST_HEAD(pidlist); struct pidlist_node{ struct list_head lh; pid_t pid; } static struct pidlist_node *get_pin(void) { struct list_head *curr = pidlist.next; while (curr != pidlist) { struct struct pidlist_node *pin = container_of(curr, struct pidlist_node, lh); if (pin->pid == current->pid) { return pin; } } return 0; } int open(void) { struct pidlist_node *pin; mutex_lock(&pidlist_lock); pin = get_pin(); if (pin != 0) { mutex_unlock(&pidlist_lock); return -EBUSY; } pin = kmalloc(sizeof(struct pidlist_node), GFP_KERNEL); if (pin == 0) { mutex_unlock(&pidlist_lock); return -ENOMEM; } pin->pid = current->pid; list_add(&(pin->lh), &pidlist); mutex_unlock(&pidlist_lock); } int close(void) { struct pidlist_node *pin; mutex_lock(&pidlist_lock); pin = get_pin(); if (pin != 0) { list_del(&(pin->lh)); kfree(pin); } mutex_unlock(&pidlist_lock); } -Michi -- programing a layer 3+4 network protocol for mesh networks see http://michaelblizek.twilightparadox.com
Hi! First, no need to send HTML parts. On Mit, 2011-09-28 at 07:41 +0200, Michael Blizek wrote: [...]
You could create something like this: [...]
Yes, that's the solution. But keep in mind that userspace also have the dup() and dup2() sys-call which duplicate the file descriptor (within the same process) and there is AFAIK no way to even detect that - let alone disallow it. And after a fork(), it's more or less the same (except that the open file descriptors belong to different processes ....). Generally, I wonder why that is even necessary. Bernd -- Bernd Petrovitsch Email : bernd@petrovitsch.priv.at LUGA : http://www.luga.at
On Wed, Sep 28, 2011 at 11:11 AM, Michael Blizek < michi1@michaelblizek.twilightparadox.com> wrote:
Hi!
On 21:41 Tue 27 Sep , Venkatram Tummala wrote:
On Tue, Sep 27, 2011 at 9:19 PM, rohan puri <rohan.puri15@gmail.com> wrote: ...
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.
You could create something like this:
DEFINE_MUTEX(pidlist_lock); LIST_HEAD(pidlist);
struct pidlist_node{ struct list_head lh; pid_t pid; }
static struct pidlist_node *get_pin(void) { struct list_head *curr = pidlist.next; while (curr != pidlist) { struct struct pidlist_node *pin = container_of(curr, struct pidlist_node, lh); if (pin->pid == current->pid) {
Instead of pid check, AFAIK here tgid comparison should be done. Threads in a single process may have different pid but same tgid and we want to restrict access to only one thread in a single process. Right?
return pin; } } return 0; }
int open(void) { struct pidlist_node *pin;
mutex_lock(&pidlist_lock);
pin = get_pin(); if (pin != 0) { mutex_unlock(&pidlist_lock); return -EBUSY; }
pin = kmalloc(sizeof(struct pidlist_node), GFP_KERNEL); if (pin == 0) { mutex_unlock(&pidlist_lock); return -ENOMEM; }
pin->pid = current->pid; list_add(&(pin->lh), &pidlist);
mutex_unlock(&pidlist_lock); }
int close(void) { struct pidlist_node *pin;
mutex_lock(&pidlist_lock);
pin = get_pin(); if (pin != 0) { list_del(&(pin->lh)); kfree(pin); }
mutex_unlock(&pidlist_lock); }
-Michi -- programing a layer 3+4 network protocol for mesh networks see http://michaelblizek.twilightparadox.com
participants (6)
-
Bernd Petrovitsch -
Jeff Haran -
Michael Blizek -
Mulyadi Santosa -
rohan puri -
Venkatram Tummala