faulty.c

来自「LINUX设备驱动程序第二版配套源码 第一版的内容」· C语言 代码 · 共 82 行

C
82
字号
/* * faulty.c -- a module which generates an oops when read (v2.1) * */#ifndef __KERNEL__#  define __KERNEL__#endif#ifndef MODULE#  define MODULE#endif#define __NO_VERSION__ /* don't define kernel_verion in module.h */#include <linux/module.h>#include <linux/version.h>char kernel_version [] = UTS_RELEASE;#include <linux/kernel.h> /* printk() */#include <linux/fs.h>     /* everything... */#include <linux/types.h>  /* size_t */#include <asm/segment.h>#include "sysdep-2.1.h"int faulty_major=0;char faulty_buf[1024];read_write_t faulty_read (struct inode *inode, struct file *filp,                char *buf, count_t count){  printk(KERN_DEBUG "read: inode %p, file %p, buf %p, count %li\n",         inode, filp, buf, (long)count);  copy_to_user(buf,faulty_buf,count);  return count;}struct pio {int a; char b; long c;};read_write_t faulty_write (struct inode *inode, struct file *filp,               const char *buf, count_t count){    /* put_user(0,(struct pio *)buf);*/    return 0;}struct file_operations faulty_fops = {    NULL,          /* lseek */    faulty_read,    faulty_write,                   /* nothing more, fill with NULLs */};int init_module(void){    int result;    /*     * Register your major, and accept a dynamic number     */    result = register_chrdev(faulty_major, "faulty", &faulty_fops);    if (result < 0) return result;    if (faulty_major == 0) faulty_major = result; /* dynamic */    /*      * allocate the devices -- we can't have them static, as the number     * can be specified at load time     */    return 0;}void cleanup_module(void){    unregister_chrdev(faulty_major, "faulty");}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?