Skip to main content

How to add /proc file system entry for the module, illustrated here

here is simple example to demonstrate adding /proc file system entry for the module, as follows


shiv@ubuntu:~/ds/linx$ cat heloproc.c
/*
 * Kernel program demo to illustrate how to create/add entry to
 * /proc file system
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>

/* Module license */
MODULE_LICENSE("Dual BSD/GPL");

/*
 * This is the service routine for added /proc entry
 * when user try to look into /proc/<entry added> this routine will be executed
 *
 * char *buf - below is page allocated by kernel and passed to service routine
 * eof and start would be used only when using /proc for
 * getting more than one page (usually page is 4KB)
 * for complete info you could refer linux docs
 */

int demo_read_fs(char *buf, char **start, off_t offset, int count, int *eof, void *data)
{
  int len=0;

 len = sprintf(buf, "This is Demo proc fs creation\n\n");

 return len;

}

/*
 * create_proc_read_entry() used for adding /proc fs entry in linux
 * name of the entry will be "Demoprocfs"
 * and it will be seen as /proc/Demoprocfs
 */

static void add_proc_fs_entry()
{
  create_proc_read_entry("Demoprocfs", 0, NULL, demo_read_fs, NULL);
}

/* module init */

static int proc_fs_init(void)
{
   printk(KERN_ALERT "loading demo proc fs file module\n");

   /* add /proc fs entry */
   add_proc_fs_entry(); 
   return 0;
}

/* module unloaded when we remove it */
static void proc_fs_exit(void)
{
    printk(KERN_ALERT "unloaded demo proc fs file module");
    /*
     * when we unload module, it will remove /proc entry added
     * when loading the module
     */
    remove_proc_entry("Demoprocfs", NULL);
}

module_init(proc_fs_init);
module_exit(proc_fs_exit);


/* end of the program */

Testing of sample code written above




reference:
Linux device drivers by Jonathan Corbert, Alessandro, Greg Kroah-Hartman, Oreilly publication
Linux source : http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=tree;hb=HEAD


Thanks,
-Shiv

Comments

Popular posts from this blog

C Program to Simulate Linux Cat command

/*  * C program to Simulate Implementation of  Linux *cat* command  * This program will not handle redirection  * you can try to improve this program to handle redirection of file contents  */ #include<stdio.h> #include<string.h> #define MAX_FILE_NAME_CHARS 255 int main(int argc, char *argv[]) {  FILE *fp;  char file_name[MAX_FILE_NAME_CHARS], ch;  int i;  /*   * after creating a.out, rename it as mycat for our own cat command   * and it usage is same as standard cat command   */  if(argc<1){     printf("Usage mycat <filename> \n");     return 0;  }  /*   * cat handles more than one file   * so we need this loop to handle all the files provided   * on command line   */  for(i=1; i<=argc;i++){     /* no need of copy but for understanding purpose, i have created      * string for each file name      */     strncpy(file_name, argv[i], MAX_FILE_NAME_CHARS);     fp=fopen(file_name, "r");     if(fp == NULL) {   

simple program to create Orphan process

shiv@ubuntu:~/ds/unix$ cat  orp.c /*  * Program to create orphan process @ Linux  * getpid() gives process PID and   * getppid() gives process's parent ID   * here main() process ID is parent id is current shells PID  * once process becomes orphan it is adopted by init process(it's PID is 1)  */   #include<stdio.h> #include<unistd.h> int main() {  pid_t p; /* create child process */  p=fork();  if(p==0) {     /* fork() returns Zero to child */     sleep(10);  }  printf("The child process pid is %d parent pid %d\n", getpid(), getppid()); /*parent/child waits for 20 secs and exits*/  sleep(20);  printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());  return 0; } O/p shiv@ubuntu:~/ds/unix$ ./a.out The child process pid is 2575 parent pid 1922 The child process pid is 2576 parent pid 2575 Process 2575 is done its Parent pid 1922... shiv@ubuntu:~/ds/unix$ Process 2576 is done its Parent pid 1...

Interactive C program to implement stack using doubly linked list

shiv@ubuntu:~/ds/stack$ cat stack.c /*  * Copy right by Shiv Yaragatti  * you are free to use, modify  and redistribute code on own risk  * Interactive C Program to implement STACK using Doubly linked list  */ #include "header.h" struct stack { int data; struct stack *next; struct stack *prev; }; typedef struct stack *S; /*Push elements in to stack */ void push(S *stack) {   int e;   S temp;   temp=malloc(sizeof(struct stack));   if(!temp){      printf("Can't allocate memory\n");      return;  }  printf("Enter element to push\n");  scanf("%d", &e);  temp->data=e;  if(*stack == NULL){     temp->next = temp->prev = NULL;     *stack=temp;  } else {    temp->next = *stack;    (*stack)->prev = temp;    temp->prev = NULL;    *stack=temp;  } } /* Display elements in the stack */ void display(S s){    S temp;    temp=s;    printf("Elements are ...\n");    while(temp) {     printf("%d\n", temp->data