Prevent a process from opening a file more than once
From: rohan puri <hidden>
Date: 2011-09-28 14:00:46
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:quoted
On Tue, Sep 27, 2011 at 9:19 PM, rohan puri [off-list ref]wrote: ...quoted
quoted
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 processreleasesquoted
it. This is not what i need. Only the threads in a process shouldn't beablequoted
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
-------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20110928/c4c207a9/attachment.html