c-如何根据用户请求删除proc条目



通常proc条目正在删除具有__exit属性的函数内部。如何根据用户请求删除它?我已经厌倦了在写回调中调用remove_proc_entry,但内核在那里崩溃了。我不能只是删除模块,因为它一次管理多个proc条目。我只想删除我要说的那个";删除";。

static int my_write(struct file *file, const char __user *userbuff,                                                                                                                                                                                                             
size_t len, loff_t *ppos)                                                                                                                                                                                                                                   
{                                                                                                                                                                                                                                                                               
struct  seq_file *seqf = file->private_data;                                                                                                                                                                                                                             
struct *my_data = seqf->private;                                                                                                                                                                                                                                        
char   *buff;                                                                                                                                                                                                                                                            
int     retval;                                                                                                                                                                                                                                                          
                                                                                                                                                                                                          
retval = -EINVAL;                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
if (!userbuff || len > PAGE_SIZE)                                                                                                                                                                                                                                       
return -EINVAL;                                                                                                                                                                                                                                                 
                                                                                                                                                                                                          
buff = kzalloc((len + 1), GFP_KERNEL);                                                                                                                                                                                                                                  
if (!buff)                                                                                                                                                                                                                                                              
return -ENOMEM;                                                                                                                                                                                                                                                 
                                                                                                                                                                                                          
if (copy_from_user(buff, userbuff, len))                                                                                                                                                                                                                                
goto out;                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
buff[len] = '';                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
if (!strncmp("delete", buff, 6)) {                                                                                                                                                                                                                                      
/* Cannot use remove_proc_entry inside that write callback. */                                                                                                                                                                                                  
retval = 0;                                                                                                                                                                                                                                                     
}                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
if (!retval)                                                                                                                                                                                                                                                            
retval = len;                                                                                                                                                                                                                                                   
else                                                                                                                                                                                                                                                                    
printk("Usage: echo delete > /proc/my_proc to delete that entry.n");                                                                                                                                                                                          
                                                                                                                                                                                                          
return retval;                                                                                                                                                                                                                                                          
}                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                          
static const struct file_operations my_proc_fops = {                                                                                                                                                                                                                            
.open    = my_proc_open,                                                                                                                                                                                                                                                
.read    = seq_read,                                                                                                                                                                                                                                                    
.write   = my_write,                                                                                                                                                                                                                                                    
.llseek  = seq_lseek,                                                                                                                                                                                                                                                   
.release = single_release,                                                                                                                                                                                                                                              
};  

谢谢!我已经通过schedule_work完成了。

struct my_data {
/* data */
struct work_struct remove_proc_work;
};
static void remove_proc_work_handler(struct work_struct *work)                                                                                                                                                                                                                  
{                                                                                                                                                                                                                                                                               
struct my_data *data;                                                                                                                                                                                                                                                  
                                                                                                                                                                                                          
data = container_of(work, struct my_data, remove_proc_work);                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
/* call remove_proc_entry */                                                                                                                                                                                                                                     
} 
static void init_data(struct my_data *data)
{
/* init data */
INIT_WORK(&data->remove_proc_work, remove_proc_work_handler);
}
static int my_write(struct file *file, const char __user *userbuff,                                                                                                                                                                                                             
size_t len, loff_t *ppos)                                                                                                                                                                                                                                   
{                                                                                                                                                                                                                                                                               
struct  seq_file *seqf = file->private_data;                                                                                                                                                                                                                             
struct my_data *data = seqf->private;                                                                                                                                                                                                                                        
char   *buff;                                                                                                                                                                                                                                                            
int     retval;                                                                                                                                                                                                                                                          
                                                                                                                                                                                                          
retval = -EINVAL;                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
if (!userbuff || len > PAGE_SIZE)                                                                                                                                                                                                                                       
return -EINVAL;                                                                                                                                                                                                                                                 
                                                                                                                                                                                                          
buff = kzalloc((len + 1), GFP_KERNEL);                                                                                                                                                                                                                                  
if (!buff)                                                                                                                                                                                                                                                              
return -ENOMEM;                                                                                                                                                                                                                                                 
                                                                                                                                                                                                          
if (copy_from_user(buff, userbuff, len))                                                                                                                                                                                                                                
goto out;                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
buff[len] = '';                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
if (!strncmp("delete", buff, 6)) {                                                                                                                                                                                                                                      
/* Cannot use remove_proc_entry inside that write callback. */                          
schedule_work(&data->remove_proc_work);                                                                                                                                                       
retval = 0;                                                                                                                                                                                                                                                     
}                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                          
if (!retval)                                                                                                                                                                                                                                                            
retval = len;                                                                                                                                                                                                                                                   
else                                                                                                                                                                                                                                                                    
printk("Usage: echo delete > /proc/my_proc to delete that entry.n");                                                                                                                                                                                          
                                                                                                                                                                                                          
return retval;                                                                                                                                                                                                                                                          
}                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                          
static const struct file_operations my_proc_fops = {                                                                                                                                                                                                                            
.open    = my_proc_open,                                                                                                                                                                                                                                                
.read    = seq_read,                                                                                                                                                                                                                                                    
.write   = my_write,                                                                                                                                                                                                                                                    
.llseek  = seq_lseek,                                                                                                                                                                                                                                                   
.release = single_release,                                                                                                                                                                                                                                              
};  

最新更新