character device failure TODO, mknoddev use awk

This commit is contained in:
Ciro Santilli
2017-08-06 12:41:05 +01:00
parent dab04d1852
commit f67d99ff24
2 changed files with 5 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
/* /*
Automatically create the device under /dev on insmod, and remove on rmmod. Automatically create the device under /dev on insmod, and remove on rmmod.
TODO: by itself works, but if I try to cat character_device.ko after removing this, that OOPS!
https://stackoverflow.com/questions/5970595/create-a-device-node-in-code/ https://stackoverflow.com/questions/5970595/create-a-device-node-in-code/
https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/18594761#18594761 https://stackoverflow.com/questions/5970595/how-to-create-a-device-node-from-the-init-module-code-of-a-linux-kernel-module/18594761#18594761
*/ */
@@ -39,7 +41,8 @@ static const struct file_operations fops = {
static int myinit(void) static int myinit(void)
{ {
/* cat /proc/devices */ /* cat /proc/devices */
alloc_chrdev_region(&major, 0, 1, NAME "_proc"); if (alloc_chrdev_region(&major, 0, 1, NAME "_proc")) {
};
/* ls /sys/class */ /* ls /sys/class */
myclass = class_create(THIS_MODULE, NAME "_sys"); myclass = class_create(THIS_MODULE, NAME "_sys");
/* ls /dev/ */ /* ls /dev/ */

View File

@@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
dev="$1" dev="$1"
major="$(grep "$dev" /proc/devices | cut -d ' ' -f 1)" major="$(awk -F ' ' '$2 == "'"$dev"'" { print $1 }' /proc/devices)"
mknod "/dev/$dev" c "$major" 0 mknod "/dev/$dev" c "$major" 0