/* * This function is called at module load time, and creates the * directory in debugfs and the two files. */ static int __init nullderef_init(void) { /* Create the directory our files will live in. */ nullderef_root = debugfs_create_dir("nullderef", NULL); if (!nullderef_root) { printk(KERN_ERR "nullderef: creating root dir failed\n"); return -ENODEV; } /* * Create the null_read and null_call files. Use the fops * structs defined above so that the kernel knows how to * handle writes to them, and set the permissions to be * writable by anyone. */ read_de = debugfs_create_file("null_read", 0222, nullderef_root, NULL, &null_read_fops); call_de = debugfs_create_file("null_call", 0222, nullderef_root, NULL, &null_call_fops); if (!read_de || !call_de) goto out_err; return 0; out_err: cleanup_debugfs(); return -ENODEV; }
/* module loading callback */ static int kernwrite_init(void) { /* initialize the data pointer to `ops' */ ops_ptr = &ops; /* create the kernwrite directory in debugfs */ kernwrite_root = debugfs_create_dir("kernwrite", NULL); /* failed */ if (kernwrite_root == NULL) { /* verbose */ printk(KERN_ERR "kernwrite: creating root dir failed\n"); return -ENODEV; } /* create the files with the appropriate `fops' struct and perms */ over_func_ptr = debugfs_create_file("over_func_ptr", 0222, kernwrite_root, NULL, &over_func_fops); over_data_ptr = debugfs_create_file("over_data_ptr", 0222, kernwrite_root, NULL, &over_data_fops); invoke_func_ptr = debugfs_create_file("invoke_func", 0222, kernwrite_root, NULL, &invoke_func_fops); /* error handling */ if (over_func_ptr == NULL || over_data_ptr == NULL || invoke_func_ptr == NULL) goto out_err; /* return with success */ return 0; out_err: /* cleanup */ printk(KERN_ERR "kernwrite: creating files in root dir failed\n"); cleanup_debugfs(); /* return with failure */ return -ENODEV; }
/* * This function is called on module unload, and cleans up our files. */ static void __exit nullderef_exit(void) { cleanup_debugfs(); }
/* module unloading callback */ static void kernwrite_exit(void) { cleanup_debugfs(); }