在写驱动的过程中,通常内核驱动的日志会再系统的日志messages文件中,或者执行dmesg命令查看日志相关信息。
循环创建目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <linux/fs.h> #include <linux/dcache.h> #include <linux/namei.h> #include <linux/module.h> #include <linux/init.h> #include <linux/slab.h>
static int kernel_make_director(const char *path) { struct path dir_path; struct dentry *dentry; struct dentry *parent_dentry = NULL; char *token; char *paths; char *curr_path; int err = 0;
paths = kstrdup(path, GFP_KERNEL); if (!paths) { return -ENOMEM; }
curr_path = paths;
while ((token = strsep(&curr_path, "/")) != NULL) { if (*token == '\0') { continue; }
if (!parent_dentry) { err = kern_path("/", LOOKUP_FOLLOW, &dir_path); if (err) { pr_err("path lookup failed: %d\n", err); kfree(paths); return err; } } else { dir_path.dentry = parent_dentry; }
dentry = lookup_one_len(token, dir_path.dentry, strlen(token)); if (IS_ERR(dentry)) { pr_err("dentry lookup failed: %ld\n", PTR_ERR(dentry)); err = PTR_ERR(dentry); break; }
if (!dentry->d_inode) { err = vfs_mkdir(dir_path.dentry->d_inode, dentry, 0755); if (err) { pr_err("vfs_mkdir failed: %d\n", err); dput(dentry); break; } }
dput(dentry); parent_dentry = dentry; }
path_put(&dir_path); kfree(paths); return err; }
static int __init my_module_init(void) { return kernel_make_director("/path/to/parent/newdir"); }
static void __exit my_module_exit(void) { pr_info("Module exiting\n"); }
module_init(my_module_init); module_exit(my_module_exit);
MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A module to recursively create directories");
|