Skip to content

Commit

Permalink
Added new module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arna-Maity committed Jun 5, 2020
1 parent fe979e5 commit 9fb45c9
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ obj-m += hello.o \
hello-5.o \
chardev.o \
procfs.o \
procfs2.o
procfs2.o \
procfs3.o

MAKE = /usr/bin/make

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Reference: [The LKM programming Guide](https://www.tldp.org/LDP/lkmpg/2.6/lkmpg.
5. Simple Character Device Driver.
6. /proc File System Module.
7. /proc using Standard File System.
8. Managing procfs with seq_file.

### Some useful StackOverflow Threads:
1. [proc_root](https://stackoverflow.com/questions/2531730/linux-kernel-module-creating-proc-file-proc-root-undeclared-error)
Expand Down
90 changes: 90 additions & 0 deletions procfs3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

#define PROC_NAME "iter"

MODULE_AUTHOR("Arna Maity <[email protected]>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Module illustrating the management of procfs using seq_file.");

// Prototypes for Module Entry & Exit functions.
static int __init procfs3_init(void);
static void __exit procfs3_exit(void);

struct proc_dir_entry* entry;

static void* my_seq_start(struct seq_file* s, loff_t* pos)
{
static unsigned long counter = 0;
if(*pos == 0)
return &counter;

else{
*pos = 0;
return NULL;
}
}

static void* my_seq_next(struct seq_file* s, void* v, loff_t* pos)
{
unsigned long *tmp_v = (unsigned long*)v;
(*tmp_v)++;
(*pos)++;
return NULL;
}

static void my_seq_stop(struct seq_file* s, void* v)
{

}

static int my_seq_show(struct seq_file* s, void* v)
{
loff_t* spos = (loff_t*)v;
seq_printf(s,"%Ld\n",*spos);

return 0;
}

/* This Structure gathers "functions" 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 opened.
*/

static int my_open(struct inode* inode, struct file* file)
{
return seq_open(file, &my_seq_ops);
}

/* This structure gathers all the functions to manage the /proc file.
*/
static struct file_operations my_file_ops = {
.owner = THIS_MODULE,
.open = my_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release
};

static int __init procfs3_init(void)
{
entry = proc_create(PROC_NAME,0,NULL,&my_file_ops);
return 0;
}

static void __exit procfs3_exit(void)
{
proc_remove(entry);
}

module_init(procfs3_init);
module_exit(procfs3_exit);

0 comments on commit 9fb45c9

Please sign in to comment.