rsp rsp - 3 months ago 27
Linux Question

insmod: ERROR: could not insert module charDriv.ko: Operation not permitted

I have Ubuntu 14.04(4.7.1). I am trying to load a kernel module via

insmod
command and I get the following error for typing
insmod charDriv.ko
command:

insmod: ERROR: could not insert module charDriv.ko: Operation not permitted


Has anyone any idea?

Here my charDriv.c file code

#include <linux/types.h>
#include <linux/kdev_t.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>

static dev_t first; // Global variable for the first device number

static int __init driver_init(void) // constructor
{
printk(KERN_INFO "Namaskar : driver registered");
if (alloc_chrdev_region(&first, 0, 3, "MSC") < 0);
{
return -1;
}
printk(KERN_INFO "<Major, Minor>: <%d, %d>\n", MAJOR(first), MINOR(first));
return 0;
}

static void __exit driver_exit(void) //destructor
{
unregister_chrdev_region(first, 3);
printk(KERN_INFO "Alvida: driver unregistered");
}

module_init(driver_init);
module_exit(driver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("MS Chaudhari <mschaudhari9@gmail.com>");
MODULE_DESCRIPTION("My First Character Driver");


Makefile code,

obj-m += charDriv.o

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean


Then, using
sudo su
, enter into root user and using
make
command compile my module.

root@radix-VirtualBox:/home/radix/programing/DD# make
make -C /lib/modules/4.7.1/build M=/home/radix/programing/DD modules
make[1]: Entering directory `/usr/src/linux-4.7.1'
Building modules, stage 2.
MODPOST 6 modules
make[1]: Leaving directory `/usr/src/linux-4.7.1'
root@radix-VirtualBox:/home/radix/programing/DD#


After that, I trying to load a kernel module via
insmod
command, getting above error.

Thank in advance.

PS. I tried it as root.

Answer

You can't use semicolon(;) end of the if condition. like,

if (alloc_chrdev_region(&first, 0, 3, "MSC") < 0)

Then compile your module again.