Last active
August 3, 2023 19:51
-
-
Save brenns10/65d1ee6bb8419f96d2ae693eb7a66cc0 to your computer and use it in GitHub Desktop.
Linux Character Device Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* chardev.c: Creates a read-only char device that says how many times you've | |
* read from the dev file. | |
* | |
* You can have some fun with this by removing the module_get/put calls, | |
* allowing the module to be removed while the file is still open. | |
* | |
* Compile with `make`. Load with `sudo insmod chardev.ko`. Check `dmesg | tail` | |
* output to see the assigned device number and command to create a device file. | |
* | |
* From TLDP.org's LKMPG book. | |
*/ | |
#include <linux/kernel.h> | |
#include <linux/module.h> | |
#include <linux/fs.h> | |
#include <asm/uaccess.h> /* for put_user */ | |
/* | |
* Prototypes - this would normally go in a .h file | |
*/ | |
int init_module(void); | |
void cleanup_module(void); | |
static int device_open(struct inode *, struct file *); | |
static int device_release(struct inode *, struct file *); | |
static ssize_t device_read(struct file *, char *, size_t, loff_t *); | |
static ssize_t device_write(struct file *, const char *, size_t, loff_t *); | |
#define SUCCESS 0 | |
#define DEVICE_NAME "chardev" | |
#define BUF_LEN 80 | |
/* | |
* Global variables are declared as static, so are global within the file. | |
*/ | |
static int Major; | |
static int Device_Open = 0; | |
static char msg[BUF_LEN]; | |
static char *msg_Ptr; | |
static struct file_operations fops = { | |
.read = device_read, | |
.write = device_write, | |
.open = device_open, | |
.release = device_release | |
}; | |
/* | |
* This function is called when the module is loaded | |
*/ | |
int init_module(void) | |
{ | |
Major = register_chrdev(0, DEVICE_NAME, &fops); | |
if (Major < 0) { | |
printk(KERN_ALERT "Registering char device failed with %d\n", Major); | |
return Major; | |
} | |
printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major); | |
printk(KERN_INFO "the driver, create a dev file with\n"); | |
printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major); | |
printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n"); | |
printk(KERN_INFO "the device file.\n"); | |
printk(KERN_INFO "Remove the device file and module when done.\n"); | |
return SUCCESS; | |
} | |
/* | |
* This function is called when the module is unloaded | |
*/ | |
void cleanup_module(void) | |
{ | |
/* | |
* Unregister the device | |
*/ | |
unregister_chrdev(Major, DEVICE_NAME); | |
} | |
/* | |
* Methods | |
*/ | |
/* | |
* Called when a process tries to open the device file, like | |
* "cat /dev/mycharfile" | |
*/ | |
static int device_open(struct inode *inode, struct file *filp) | |
{ | |
static int counter = 0; | |
if (Device_Open) | |
return -EBUSY; | |
Device_Open++; | |
sprintf(msg, "I already told you %d times Hello world!\n", counter++); | |
msg_Ptr = msg; | |
/* | |
* TODO: comment out the line below to have some fun! | |
*/ | |
try_module_get(THIS_MODULE); | |
return SUCCESS; | |
} | |
/* | |
* Called when a process closes the device file. | |
*/ | |
static int device_release(struct inode *inode, struct file *filp) | |
{ | |
Device_Open--; | |
/* | |
* Decrement the usage count, or else once you opened the file, you'll never | |
* get rid of the module. | |
* | |
* TODO: comment out the line below to have some fun! | |
*/ | |
module_put(THIS_MODULE); | |
return SUCCESS; | |
} | |
/* | |
* Called when a process, which already opened the dev file, attempts to read | |
* from it. | |
*/ | |
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */ | |
char *buffer, /* buffer to fill with data */ | |
size_t length, /* length of the buffer */ | |
loff_t *offset) | |
{ | |
/* | |
* Number of bytes actually written to the buffer | |
*/ | |
int bytes_read = 0; | |
/* | |
* If we're at the end of the message, return 0 signifying end of file. | |
*/ | |
if (*msg_Ptr == 0) | |
return 0; | |
/* | |
* Actually put the data into the buffer | |
*/ | |
while (length && *msg_Ptr) { | |
/* | |
* The buffer is in the user data segment, not the kernel segment so "*" | |
* assignment won't work. We have to use put_user which copies data from the | |
* kernel data segment to the user data segment. | |
*/ | |
put_user(*(msg_Ptr++), buffer++); | |
length--; | |
bytes_read++; | |
} | |
/* | |
* Most read functions return the number of bytes put into the buffer | |
*/ | |
return bytes_read; | |
} | |
/* | |
* Called when a process writes to dev file: echo "hi" > /dev/hello | |
*/ | |
static ssize_t | |
device_write(struct file *filp, const char *buf, size_t len, loff_t *off) | |
{ | |
printk(KERN_ALERT "Sorry, this operation isn't supported.\n"); | |
return -EINVAL; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj-m += chardev.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 |
had an issue when including <asm/uaccess.h>
make would print out:
./arch/x86/include/asm/uaccess.h: In function ‘set_fs’: ./arch/x86/include/asm/uaccess.h:32:9: error: dereferencing pointer to incomplete type ‘struct task_struct’ current->thread.addr_limit = fs; ^~
Fixed it by replacing it with:
#include <linux/uaccess.h>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minimal example with runnable QEMU + Buildroot boilerplate: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/ca8298aca99691dc4e13068ddeb3d5184ceceaf1/kernel_module/character_device.c