corrections on 03 and 08, 09 done
This commit is contained in:
17
03/main.c
17
03/main.c
@ -1,3 +1,4 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/delay.h>
|
||||
@ -7,21 +8,21 @@ MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("42");
|
||||
MODULE_DESCRIPTION("A module that does work");
|
||||
|
||||
|
||||
static int do_work(int *nbLoop)
|
||||
static int do_work(int *nb_loop)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < *nbLoop; ++x) {
|
||||
udelay(10);
|
||||
}
|
||||
if (*nbLoop > 10)
|
||||
pr_info("do_work(): slept a long time\n");
|
||||
return(x * *nbLoop);
|
||||
|
||||
for (x = 0; x < *nb_loop; ++x)
|
||||
usleep_range(10);
|
||||
if (*nb_loop > 10)
|
||||
pr_info("%s: slept a long time\n", __func__);
|
||||
return(x * *nb_loop);
|
||||
}
|
||||
|
||||
static int __init module_example_42_init(void)
|
||||
{
|
||||
int x = 10;
|
||||
|
||||
x = do_work(&x);
|
||||
return x;
|
||||
}
|
||||
|
22
08/main.c
22
08/main.c
@ -1,3 +1,4 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include <linux/module.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/init.h>
|
||||
@ -9,12 +10,12 @@ MODULE_LICENSE("GPL");
|
||||
MODULE_AUTHOR("Louis Solofrizzo <louis@ne02ptzero.me>");
|
||||
MODULE_DESCRIPTION("Useless module");
|
||||
|
||||
static ssize_t myfd_read (struct file *fp, char __user *user,
|
||||
size_t size, loff_t *offs);
|
||||
static ssize_t myfd_read(struct file *fp, char __user *user,
|
||||
size_t size, loff_t *offs);
|
||||
static ssize_t myfd_write(struct file *fp, const char __user *user,
|
||||
size_t size, loff_t *offs);
|
||||
size_t size, loff_t *offs);
|
||||
|
||||
static struct file_operations myfd_fops = {
|
||||
static const struct file_operations myfd_fops = {
|
||||
.owner = THIS_MODULE,
|
||||
.read = &myfd_read,
|
||||
.write = &myfd_write
|
||||
@ -27,7 +28,8 @@ static struct miscdevice myfd_device = {
|
||||
};
|
||||
|
||||
char str[PAGE_SIZE];
|
||||
DEFINE_MUTEX(rw_lock);
|
||||
|
||||
DEFINE_MUTEX(rw_lock); //lock read/write operations of the /dev/reverse file
|
||||
|
||||
static int __init myfd_init(void)
|
||||
{
|
||||
@ -45,25 +47,25 @@ ssize_t myfd_read(struct file *fp, char __user *user, size_t size, loff_t *offs)
|
||||
ssize_t retval;
|
||||
size_t len;
|
||||
char *tmp;
|
||||
|
||||
|
||||
mutex_lock(&rw_lock);
|
||||
len = strlen(str);
|
||||
tmp = kmalloc(sizeof(char) * PAGE_SIZE, GFP_KERNEL);
|
||||
for (size_t i = 0,j = len - 1; i < len; i++, j--) {
|
||||
for (size_t i = 0, j = len - 1; i < len; i++, j--)
|
||||
tmp[i] = str[j];
|
||||
}
|
||||
tmp[len] = 0;
|
||||
retval = simple_read_from_buffer(user, size, offs, tmp, len);
|
||||
mutex_unlock(&rw_lock);
|
||||
kfree(tmp);
|
||||
return retval;
|
||||
}
|
||||
|
||||
ssize_t myfd_write(struct file *fp, const char __user *user, size_t size,
|
||||
loff_t *offs)
|
||||
loff_t *offs)
|
||||
{
|
||||
ssize_t res;
|
||||
|
||||
if(size >= (PAGE_SIZE - 1))
|
||||
if (size >= (PAGE_SIZE - 1))
|
||||
return -EINVAL;
|
||||
mutex_lock(&rw_lock);
|
||||
res = simple_write_to_buffer(str, size, offs, user, size);
|
||||
|
9
09/Makefile
Normal file
9
09/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
obj-m += mounts.o
|
||||
mounts-y = main.o rw.o
|
||||
PWD := $(CURDIR)
|
||||
|
||||
all:
|
||||
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
|
||||
|
||||
clean:
|
||||
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
|
44
09/main.c
Normal file
44
09/main.c
Normal file
@ -0,0 +1,44 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/seq_file.h>
|
||||
|
||||
#include <linux/sched.h>
|
||||
#include <linux/nsproxy.h>
|
||||
#include <linux/mount.h>
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_DESCRIPTION("create /proc/mymounts file that lists mounts");
|
||||
|
||||
struct proc_dir_entry *proc_file;
|
||||
|
||||
int list_mounts(struct seq_file *m, void *v);
|
||||
|
||||
static int list_mounts_open(struct inode *inode, struct file *file)
|
||||
{
|
||||
return single_open(file, list_mounts, NULL);
|
||||
}
|
||||
|
||||
static const struct proc_ops proc_fops = {
|
||||
.proc_open = list_mounts_open,
|
||||
.proc_read = seq_read,
|
||||
.proc_lseek = seq_lseek,
|
||||
.proc_release = single_release,
|
||||
};
|
||||
|
||||
static int __init mounts_init(void)
|
||||
{
|
||||
proc_file = proc_create("mymounts", 0444, 0, &proc_fops);
|
||||
if(IS_ERR(proc_file)) return PTR_ERR(proc_file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void __exit mounts_exit(void)
|
||||
{
|
||||
proc_remove(proc_file);
|
||||
}
|
||||
|
||||
module_init(mounts_init);
|
||||
module_exit(mounts_exit);
|
||||
|
41
09/rw.c
Normal file
41
09/rw.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/kernel.h>
|
||||
|
||||
#include <linux/nsproxy.h>
|
||||
#include <linux/sched.h>
|
||||
#include <linux/fs.h>
|
||||
#include <../fs/mount.h>
|
||||
|
||||
int list_mounts(struct seq_file *m, void *v);
|
||||
|
||||
int list_mounts(struct seq_file *m, void *v)
|
||||
{
|
||||
struct rb_node *cur;
|
||||
struct mount *mnt;
|
||||
char *buf;
|
||||
char *path;
|
||||
|
||||
cur = current->nsproxy->mnt_ns->mnt_first_node;
|
||||
|
||||
buf = kmalloc(sizeof(char) * PATH_MAX, GFP_KERNEL);
|
||||
while(cur)
|
||||
{
|
||||
mnt = rb_entry(cur, struct mount, mnt_node);
|
||||
|
||||
struct path p = {
|
||||
.mnt = &mnt->mnt,
|
||||
.dentry = mnt->mnt.mnt_root,
|
||||
};
|
||||
|
||||
path = d_path(&p, buf, PATH_MAX);
|
||||
if(IS_ERR(path))
|
||||
path = 0;
|
||||
seq_printf(m, "%-10s %s\n", mnt->mnt_devname, path);
|
||||
cur = rb_next(cur);
|
||||
}
|
||||
kfree(buf);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user