indent and checker

This commit is contained in:
rick 2024-01-11 19:55:48 +01:00
parent 1441afda58
commit 3d9046f06d
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA

View file

@ -4,13 +4,13 @@
#include <asm/page.h> #include <asm/page.h>
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/rwsem.h> #include <linux/rwsem.h>
#ifdef DEBUG #ifdef DEBUG
#include <linux/kernel.h> #include <linux/delay.h>
#endif #endif
#define BUFFER_LENGTH 10 #define BUFFER_LENGTH 10
@ -23,18 +23,19 @@ static char *id_msg;
static char *foo_msg; static char *foo_msg;
static struct rw_semaphore foo_sem; static struct rw_semaphore foo_sem;
static ssize_t id_read(struct file *, char __user *buffer, size_t length, loff_t *offset) static ssize_t id_read(struct file *, char __user *buffer, size_t length,
loff_t *offset)
{ {
int bytes = 0; int bytes = 0;
const char *msg_tmp = id_msg; const char *msg_tmp = id_msg;
/* /*
* TODO * TODO
if (!*(msg_tmp + *offset)) { if (!*(msg_tmp + *offset)) {
*offset = 0; *offset = 0;
return 0; return 0;
} }
*/ */
msg_tmp += *offset; msg_tmp += *offset;
@ -82,46 +83,51 @@ static const struct file_operations id_fops = {
.write = id_write, .write = id_write,
}; };
static ssize_t foo_read(struct file *, char __user *buffer, size_t count, loff_t *offset) static ssize_t foo_read(struct file *, char __user *buffer, size_t count,
loff_t *offset)
{ {
int bytes = -EINVAL; int bytes = -EINVAL;
int tmp_len = strlen(foo_msg); int tmp_len = strlen(foo_msg);
if (*offset > tmp_len) bytes = -EFAULT; if (*offset > tmp_len) {
else if (down_read_trylock(&foo_sem)) bytes = -EFAULT;
{ } else if (down_read_trylock(&foo_sem)) {
char *tmp_str = foo_msg + *offset; char *tmp_str = foo_msg + *offset;
tmp_len = strlen(tmp_str);
bytes = tmp_len > count ? count : tmp_len; tmp_len = strlen(tmp_str);
if (copy_to_user(buffer, tmp_str, bytes)) bytes = -EFAULT; bytes = tmp_len > count ? count : tmp_len;
else *offset += bytes;
if (copy_to_user(buffer, tmp_str, bytes))
bytes = -EFAULT;
else
*offset += bytes;
#ifdef DEBUG #ifdef DEBUG
msleep(5000); msleep(5000);
#endif #endif
up_read(&foo_sem); up_read(&foo_sem);
} }
return bytes; return bytes;
} }
static ssize_t foo_write(struct file *, const char __user *buffer, static ssize_t foo_write(struct file *, const char __user *buffer,
size_t count, loff_t * offset) size_t count, loff_t *offset)
{ {
int bytes = -EINVAL; int bytes = -EINVAL;
if ((*offset + count) > PAGE_SIZE) bytes = -EFAULT; if ((*offset + count) > PAGE_SIZE) {
else if (down_write_trylock(&foo_sem)) bytes = -EFAULT;
{ } else if (down_write_trylock(&foo_sem)) {
bytes = count; bytes = count;
if (copy_from_user(foo_msg, buffer, bytes)) bytes = -EINVAL; if (copy_from_user(foo_msg, buffer, bytes))
bytes = -EINVAL;
#ifdef DEBUG #ifdef DEBUG
msleep(5000); msleep(5000);
#endif #endif
up_write(&foo_sem); up_write(&foo_sem);
} }
return bytes; return bytes;
} }
static const struct file_operations foo_fops = { static const struct file_operations foo_fops = {
@ -132,25 +138,25 @@ static const struct file_operations foo_fops = {
static int __init my_init(void) static int __init my_init(void)
{ {
init_rwsem(&foo_sem); init_rwsem(&foo_sem);
id_msg = kmalloc(12, GFP_KERNEL); id_msg = kmalloc(12, GFP_KERNEL);
foo_msg = kmalloc(PAGE_SIZE, GFP_KERNEL); foo_msg = kmalloc(PAGE_SIZE, GFP_KERNEL);
strcpy(id_msg, "pouetpouet\n"); strscpy(id_msg, "pouetpouet\n", 12);
pr_info("Coucou le gens !!!!\n"); pr_info("Coucou le gens !!!!\n");
folder = debugfs_create_dir("eudyptula", NULL); folder = debugfs_create_dir("eudyptula", NULL);
id_file = debugfs_create_file("id", 0666, folder, NULL, &id_fops); id_file = debugfs_create_file("id", 0666, folder, NULL, &id_fops);
debugfs_create_u64("jiffies", 0444, folder, &jiffies_64); debugfs_create_u64("jiffies", 0444, folder, &jiffies_64);
foo_file = debugfs_create_file("foo", 0664, folder, NULL, &foo_fops); foo_file = debugfs_create_file("foo", 0664, folder, NULL, &foo_fops);
return 0; return 0;
} }
static void __exit my_exit(void) static void __exit my_exit(void)
{ {
debugfs_remove_recursive(folder); debugfs_remove_recursive(folder);
kfree(id_msg); kfree(id_msg);
kfree(foo_msg); kfree(foo_msg);
pr_info("Tschuss !!!\n"); pr_info("Tschuss !!!\n");
} }