当前位置: 技术问答>linux和unix
如何编写和运行模块?
来源: 互联网 发布时间:2015-12-24
本文导语: 用VI写了一个hello.world的C程序.用gcc 编译OK后生成 hello.o 用root身份.终端下输入insmod hello.o 按照书上说的应该输出程序的结果. 但是系统提示 bash: insmod : command not found. 真是纳闷. 我是按照书上做的.与书上的结果...
用VI写了一个hello.world的C程序.用gcc 编译OK后生成 hello.o
用root身份.终端下输入insmod hello.o
按照书上说的应该输出程序的结果. 但是系统提示 bash: insmod : command not found.
真是纳闷. 我是按照书上做的.与书上的结果不一样.为什么?
用root身份.终端下输入insmod hello.o
按照书上说的应该输出程序的结果. 但是系统提示 bash: insmod : command not found.
真是纳闷. 我是按照书上做的.与书上的结果不一样.为什么?
|
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
struct timer_list my_timer;
struct tty_driver *my_driver;
struct cdev *char_dev;
unsigned long led_seq = 1;
#define BLINK_DELAY HZ/5
#define LED_FIRST 0x02
#define LED_SECOND 0x04
#define LED_THIRD 0x01
#define LED_ALL 0x07
#define RESTORE_LEDS 0xFF
#define LED_LIGHT 0x01
#define LED_DARK 0x02
static void my_timer_func(unsigned long ptr)
{
if(ptr == 1){
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_FIRST);
led_seq = 2;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
}
if(ptr == 2){
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_SECOND);
led_seq = 3;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
}
if(ptr == 3)
{
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_SECOND);
led_seq = 1;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
}
}
static int device_open(struct inode *inode,struct file *file)
{
int i;
for(i=0; ivc_tty->driver;
init_timer(&my_timer);
my_timer.fucction = my_timer_func;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
return 0;
}
static int device_release(struct inode *inode,struct file *file)
{
del_timer(&my_timer);
return 0;
}
static ssize_t device_read(struct file *file,char * buf,size_t count,loff_t *ppos)
{
return 0;
}
static ssize_t device_write(struct file *file,char * buf,size_t count,loff_t *ppos)
{
return 0;
}
static int device_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd){
case LED_LIGHT:
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_ALL);
break;
case LED_DARK:
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
break;
default:break;
}
return 0;
}
struct file_operations device_ops = {
.owner = THIS_MODULE,
.llseek = NULL,
.read = device_read,
.write = device_write,
.readdir = NULL,
.poll = NULL,
.ioctl = device_ioctl,
.mmap = NULL,
.open = device_open,
.flush = NULL,
.release = device_release,
.fsync = NULL,
.fasync = NULL,
.lock = NULL,
.readv = NULL,
.writev = NULL,
}
static int __init kbleds_init(void)
{
int i,ret;
int chardev_major;
dev_t dev = 0;
ret = alloc_chrdev_region(&dev,0,1,"char_dev");
chardev_major = MAJOR(dev);
if(ret driver;
return 0;
}
static void __exit kbleds_cleanup(void)
{
cdev_del(char_dev);
unregister_chardev_region(dev,1);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
}
module_init(kbleds_init);
module_exit(kbleds_cleanup);
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
struct timer_list my_timer;
struct tty_driver *my_driver;
struct cdev *char_dev;
unsigned long led_seq = 1;
#define BLINK_DELAY HZ/5
#define LED_FIRST 0x02
#define LED_SECOND 0x04
#define LED_THIRD 0x01
#define LED_ALL 0x07
#define RESTORE_LEDS 0xFF
#define LED_LIGHT 0x01
#define LED_DARK 0x02
static void my_timer_func(unsigned long ptr)
{
if(ptr == 1){
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_FIRST);
led_seq = 2;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
}
if(ptr == 2){
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_SECOND);
led_seq = 3;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
}
if(ptr == 3)
{
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_SECOND);
led_seq = 1;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
}
}
static int device_open(struct inode *inode,struct file *file)
{
int i;
for(i=0; ivc_tty->driver;
init_timer(&my_timer);
my_timer.fucction = my_timer_func;
my_timer.data = led_seq;
my_timer.expires = jiffies + BLINK_DELAY;
add_timer(&my_timer);
return 0;
}
static int device_release(struct inode *inode,struct file *file)
{
del_timer(&my_timer);
return 0;
}
static ssize_t device_read(struct file *file,char * buf,size_t count,loff_t *ppos)
{
return 0;
}
static ssize_t device_write(struct file *file,char * buf,size_t count,loff_t *ppos)
{
return 0;
}
static int device_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
switch(cmd){
case LED_LIGHT:
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,LED_ALL);
break;
case LED_DARK:
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
break;
default:break;
}
return 0;
}
struct file_operations device_ops = {
.owner = THIS_MODULE,
.llseek = NULL,
.read = device_read,
.write = device_write,
.readdir = NULL,
.poll = NULL,
.ioctl = device_ioctl,
.mmap = NULL,
.open = device_open,
.flush = NULL,
.release = device_release,
.fsync = NULL,
.fasync = NULL,
.lock = NULL,
.readv = NULL,
.writev = NULL,
}
static int __init kbleds_init(void)
{
int i,ret;
int chardev_major;
dev_t dev = 0;
ret = alloc_chrdev_region(&dev,0,1,"char_dev");
chardev_major = MAJOR(dev);
if(ret driver;
return 0;
}
static void __exit kbleds_cleanup(void)
{
cdev_del(char_dev);
unregister_chardev_region(dev,1);
(my_driver->ioctl)(vc_cons[fg_console].d->vc_tty,NULL,KDSETLED,RESTORE_LEDS);
}
module_init(kbleds_init);
module_exit(kbleds_cleanup);
|
bash: insmod : command not found:
可能是权限不够,su到root.
可能是权限不够,su到root.
|
"bash: insmod : command not found"
这时候的问题肯定是和你的INSMOD命令有关,可以换一台真正安装了
LINUX的机器试试.
这时候的问题肯定是和你的INSMOD命令有关,可以换一台真正安装了
LINUX的机器试试.
|
你的系统当前不支持insmod命令,处去权限问题
应该是module工具的问题
你升级下module-init工具就可以了
应该是module工具的问题
你升级下module-init工具就可以了
|
路径的问题吧,
locate insmod 看看这个命令在哪里,
比如
/sbin/insmod xxx.o
locate insmod 看看这个命令在哪里,
比如
/sbin/insmod xxx.o