More advanced character device with read, write, open and release

This commit is contained in:
Imbus 2025-05-10 12:51:32 +02:00
parent bb9bce27f6
commit 83db8ec77e
3 changed files with 177 additions and 0 deletions

23
chardev/Makefile Normal file
View file

@ -0,0 +1,23 @@
# This is a standard makefile for building a kernel module.
# It looks a bit backwards compared to a normal makefile, but that's because
# the kernel supplies its own sub-makefile that we use to build the module.
#
# The M= option tells the kernel's makefile where to find the module's source
# files, and the -C option sets the context directory for make.
#
# The obj-m variable tells the kernel's makefile what the module's object file
# should be called. In this case, it's demo_module.o. This is how the sub-makefile
# infers the name of the module's source file.
#
# This build is highly sensitive to compiler version, and requires the system gcc
# to be the same version as the gcc that built the current kernel.
#
# For more info, see: https://www.kernel.org/doc/html/latest/kbuild/modules.html
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

113
chardev/chardev.c Normal file
View file

@ -0,0 +1,113 @@
#include "linux/fs.h"
#include "linux/types.h"
#include <linux/gpio/consumer.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
/*
* Last compiled with Linux 6.14.5-300.fc42.x86_64
*/
/* Meta Info */
MODULE_DESCRIPTION("Chardev kernel module.");
MODULE_VERSION("0.0.1");
MODULE_AUTHOR("Imbus");
MODULE_LICENSE("GPL");
// Since were dynamically assigned a major device number, we will store it here
static int major;
static char text[64];
// Regular forward decls
static ssize_t my_read(struct file *f, char __user *u, size_t l, loff_t *o);
static ssize_t my_write(struct file *, const char __user *, size_t, loff_t *);
static int my_open(struct inode *, struct file *);
static int my_release(struct inode *, struct file *);
// See: https://elixir.bootlin.com/linux/v6.14.5/source/include/linux/fs.h#L2131
static struct file_operations fops = {
.read = my_read, .write = my_write, .open = my_open, .release = my_release};
static ssize_t my_write(struct file *filp, const char __user *user_buf,
size_t len, loff_t *off) {
printk(KERN_INFO "hello_cdev - Write is called by %s...\n", current->comm);
int not_copied, delta,
to_copy = (len + *off) < sizeof(text) ? len : (sizeof(text) - *off);
if (*off >= sizeof(text))
return 0;
not_copied = copy_from_user(text, user_buf, to_copy);
delta = to_copy - not_copied;
if (not_copied)
pr_warn("hello_cdev - Could only copy %d bytes\n", delta);
*off = delta;
return delta;
}
static ssize_t my_read(struct file *filp, char __user *user_buf, size_t len,
loff_t *off) {
printk(KERN_INFO "hello_cdev - Read is called\n");
int not_copied, delta,
to_copy = (len + *off) < sizeof(text) ? len : (sizeof(text) - *off);
if (*off >= sizeof(text))
return 0;
not_copied = copy_to_user(user_buf, text, to_copy);
delta = to_copy - not_copied;
if (not_copied)
pr_warn("hello_cdev - Could only copy %d bytes\n", delta);
*off = delta;
return delta;
}
static int my_open(struct inode *inode, struct file *filp) {
pr_info("hello-cdev - Major %d, Minor %d\n", imajor(inode), iminor(inode));
pr_info("hello-cdev - filp->f_pos: %lld\n", filp->f_pos);
pr_info("hello-cdev - filp->f_mode: 0x%x\n", filp->f_mode);
pr_info("hello-cdev - filp->f_flags: 0x%x\n", filp->f_flags);
return 0;
}
static int my_release(struct inode *inode, struct file *filp) {
pr_info("hello-cdev - Closed \n");
return 0;
}
/**
* @brief Called on load
*/
static int __init demo_mod_init(void) {
major = register_chrdev(0, "hello_cdev", &fops);
if (major < 0) {
printk(KERN_INFO "hello_cdev - Error registering chrdev\n");
return major;
}
printk(KERN_INFO "hello_cdev - Major nbr %d loaded successfully...", major);
return 0;
}
/**
* @brief Called on unload
*/
static void __exit demo_mod_exit(void) {
unregister_chrdev(major, "hello_cdev");
printk(KERN_INFO "hello_cdev - unloaded successfully...");
}
module_init(demo_mod_init);
module_exit(demo_mod_exit);

41
chardev/test.c Normal file
View file

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv) {
int fd;
if (argc < 2) {
printf("I need the file to open as an argument!\n");
return 0;
}
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
perror("open");
return fd;
}
close (fd);
fd = open(argv[1], O_RDWR | O_SYNC);
if (fd < 0) {
perror("open");
return fd;
}
close (fd);
fd = open(argv[1], O_WRONLY | O_NONBLOCK);
if (fd < 0) {
perror("open");
return fd;
}
close (fd);
return 0;
}