Newer
Older
advsyssoft / lkmpg / hello_proc.c
/**
 * Original site
 *
 * http://pointer-overloading.blogspot.com/2013/09/linux-creating-entry-in-proc-file.html
 *
 * modified for kernel version >= 5.6
 * https://stackoverflow.com/questions/64931555/how-to-fix-error-passing-argument-4-of-proc-create-from-incompatible-pointer
 *  
 */

#include <linux/module.h> 
#include <linux/proc_fs.h> 
#include <linux/seq_file.h> 
#include <linux/kernel.h>
#include <linux/version.h>

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0)
#define HAVE_PROCOPS
#endif

static int hello_proc_show(struct seq_file *m, void *v) { 
    seq_printf(m, "Hello proc!\n"); 
    return 0; 
} 

static int hello_proc_open(struct inode *inode, struct file *file) { 
    return single_open(file, hello_proc_show, NULL); 
} 

#ifdef HAVE_PROCOPS
static const struct proc_ops hello_proc_fops = { 
    .proc_open = hello_proc_open, 
    .proc_read = seq_read, 
    .proc_lseek = seq_lseek, 
    .proc_release = single_release, 
}; 
#else
static const struct file_operations hello_proc_fops = {
    .owner = THIS_MODULE,
    .open = hello_proc_open,
    .read = seq_read,
    .llseek = seq_lseek,
    .release = single_release,
};
#endif

static int __init hello_proc_init(void) { 
    proc_create("hello_proc", 0, NULL, &hello_proc_fops); 
    return 0; 
} 

static void __exit hello_proc_exit(void) { 
    remove_proc_entry("hello_proc", NULL); 
} 

MODULE_LICENSE("GPL"); 
module_init(hello_proc_init); 
module_exit(hello_proc_exit);