2021-07-22 11:25:32 +08:00
|
|
|
/*
|
2021-08-08 01:24:59 +08:00
|
|
|
* procfs4.c - create a "file" in /proc
|
|
|
|
* This program uses the seq_file library to manage the /proc file.
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
|
|
|
|
2021-09-02 15:15:07 +08:00
|
|
|
#include <linux/kernel.h> /* We are doing kernel work */
|
|
|
|
#include <linux/module.h> /* Specifically, a module */
|
|
|
|
#include <linux/proc_fs.h> /* Necessary because we use proc fs */
|
2021-07-22 06:58:13 +08:00
|
|
|
#include <linux/seq_file.h> /* for seq_file */
|
2021-07-22 07:17:31 +08:00
|
|
|
#include <linux/version.h>
|
|
|
|
|
|
|
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
|
|
|
|
#define HAVE_PROC_OPS
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-07-22 06:58:13 +08:00
|
|
|
#define PROC_NAME "iter"
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* This function is called at the beginning of a sequence.
|
2021-07-22 06:35:24 +08:00
|
|
|
* ie, when:
|
2021-08-07 10:54:19 +08:00
|
|
|
* - the /proc file is read (first time)
|
|
|
|
* - after the function stop (end of sequence)
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
|
|
|
static void *my_seq_start(struct seq_file *s, loff_t *pos)
|
|
|
|
{
|
|
|
|
static unsigned long counter = 0;
|
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* beginning a new sequence? */
|
2021-07-22 06:58:13 +08:00
|
|
|
if (*pos == 0) {
|
2021-07-22 06:35:24 +08:00
|
|
|
/* yes => return a non null value to begin the sequence */
|
|
|
|
return &counter;
|
|
|
|
}
|
2021-09-02 15:15:07 +08:00
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* no => it is the end of the sequence, return end to stop reading */
|
|
|
|
*pos = 0;
|
|
|
|
return NULL;
|
2021-07-22 06:35:24 +08:00
|
|
|
}
|
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* This function is called after the beginning of a sequence.
|
2023-10-08 17:14:09 +08:00
|
|
|
* It is called until the return is NULL (this ends the sequence).
|
2021-07-22 06:35:24 +08:00
|
|
|
*/
|
|
|
|
static void *my_seq_next(struct seq_file *s, void *v, loff_t *pos)
|
|
|
|
{
|
2021-09-02 15:15:07 +08:00
|
|
|
unsigned long *tmp_v = (unsigned long *)v;
|
2021-07-22 06:35:24 +08:00
|
|
|
(*tmp_v)++;
|
|
|
|
(*pos)++;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* This function is called at the end of a sequence. */
|
2021-07-22 06:35:24 +08:00
|
|
|
static void my_seq_stop(struct seq_file *s, void *v)
|
|
|
|
{
|
|
|
|
/* nothing to do, we use a static value in start() */
|
|
|
|
}
|
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* This function is called for each "step" of a sequence. */
|
2021-07-22 06:35:24 +08:00
|
|
|
static int my_seq_show(struct seq_file *s, void *v)
|
|
|
|
{
|
2021-09-02 15:15:07 +08:00
|
|
|
loff_t *spos = (loff_t *)v;
|
2021-07-22 06:35:24 +08:00
|
|
|
|
|
|
|
seq_printf(s, "%Ld\n", *spos);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* This structure gather "function" to manage the sequence */
|
|
|
|
static struct seq_operations my_seq_ops = {
|
|
|
|
.start = my_seq_start,
|
|
|
|
.next = my_seq_next,
|
|
|
|
.stop = my_seq_stop,
|
|
|
|
.show = my_seq_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This function is called when the /proc file is open. */
|
2021-07-22 06:35:24 +08:00
|
|
|
static int my_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return seq_open(file, &my_seq_ops);
|
|
|
|
};
|
|
|
|
|
2021-08-07 10:54:19 +08:00
|
|
|
/* This structure gather "function" that manage the /proc file */
|
2021-07-22 07:17:31 +08:00
|
|
|
#ifdef HAVE_PROC_OPS
|
|
|
|
static const struct proc_ops my_file_ops = {
|
|
|
|
.proc_open = my_open,
|
|
|
|
.proc_read = seq_read,
|
|
|
|
.proc_lseek = seq_lseek,
|
|
|
|
.proc_release = seq_release,
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
static const struct file_operations my_file_ops = {
|
|
|
|
.open = my_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release,
|
|
|
|
};
|
|
|
|
#endif
|
2021-07-22 06:35:24 +08:00
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
static int __init procfs4_init(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
struct proc_dir_entry *entry;
|
|
|
|
|
|
|
|
entry = proc_create(PROC_NAME, 0, NULL, &my_file_ops);
|
2021-07-22 06:58:13 +08:00
|
|
|
if (entry == NULL) {
|
2021-07-22 06:35:24 +08:00
|
|
|
pr_debug("Error: Could not initialize /proc/%s\n", PROC_NAME);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:29:24 +08:00
|
|
|
static void __exit procfs4_exit(void)
|
2021-07-22 06:35:24 +08:00
|
|
|
{
|
|
|
|
remove_proc_entry(PROC_NAME, NULL);
|
|
|
|
pr_debug("/proc/%s removed\n", PROC_NAME);
|
|
|
|
}
|
2021-08-08 00:29:24 +08:00
|
|
|
|
|
|
|
module_init(procfs4_init);
|
|
|
|
module_exit(procfs4_exit);
|
|
|
|
|
2021-09-08 22:23:02 +08:00
|
|
|
MODULE_LICENSE("GPL");
|