#include #include #include #include #include #include MODULE_LICENSE("GPL"); MODULE_AUTHOR("Developer"); MODULE_DESCRIPTION("A Hello World Module with debugfs functionality"); MODULE_VERSION("1.0"); static struct dentry *dir_ret = NULL; static struct dentry *file_ret = NULL; // Triggered when a userspace process reads the debugfs file static ssize_t hello_write(struct file *fp, const char __user *user_buffer, size_t count, loff_t *position) { char buf[60] = "UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"; int rc; pr_info("pos=%lld count=%lu\n", *position, count); rc = simple_write_to_buffer(buf, sizeof(buf), position, user_buffer, count); if (rc < 0) return rc; buf[rc] = '\0'; pr_info("rc=%d buf %s\n", rc, buf); return count; } // Map the custom read logic to file operations structure static const struct file_operations hello_fops = { .owner = THIS_MODULE, .write = hello_write, }; // Module Initialization static int __init hello_init(void) { pr_info("hello_debugfs: Module loaded successfully\n"); // 1. Create top-level parent directory in debugfs root dir_ret = debugfs_create_dir("hello_dir", NULL); if (IS_ERR(dir_ret)) { pr_err("hello_debugfs: Failed to create parent directory\n"); return PTR_ERR(dir_ret); } // 2. Create target communication file inside the parent directory (Read-only for all: 0444) file_ret = debugfs_create_file("hello_file", 0444, dir_ret, NULL, &hello_fops); if (IS_ERR(file_ret)) { pr_err("hello_debugfs: Failed to create communication file\n"); debugfs_remove_recursive(dir_ret); return PTR_ERR(file_ret); } return 0; } // Module Cleanup static void __exit hello_exit(void) { // Recursively destroys the created files and directory nodes debugfs_remove_recursive(dir_ret); pr_info("hello_debugfs: Module unloaded successfully\n"); } module_init(hello_init); module_exit(hello_exit);