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

How can I delete proc entry by user request?

通常 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] = '[=10=]';                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                
        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] = '[=10=]';                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                
        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,                                                                                                                                                                                                                                              
};