0

My kernel module would create an entry under an existing proc entry, e.g /proc/sys,

So the ordinary call to proc_create fails.

Then I try to see if there's a function to obtain the right parent node, by checking proc_fs.h, but to no avail.

What should I do now?

daisy
  • 22,498
  • 29
  • 129
  • 265

1 Answers1

-3

Hope this code helps you to understand

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h> /*this is the file structure, file open read close */
#include<linux/cdev.h> /* this is for character device, makes cdev avilable*/
#include<linux/semaphore.h> /* this is for the semaphore*/
#include<linux/uaccess.h> /*this is for copy_user vice vers*/
#include<linux/proc_fs.h>

#define MAX_LEN 1024
int read_info(char *page, char **start, off_t off,  int count, int *eof, void *data);
int write_info(struct file *filp, const char __user *buffer, unsigned long length, void *data);
int proc_init(void);
void proc_clean(void);


static struct proc_dir_entry *proc_entry;
static char *info;
static int write_index;
static int read_index;

int write_info(struct file *filp, const char __user *buffer, unsigned long length, void *data) {
    int capacity = (MAX_LEN - write_index) +1;
    if(length > capacity) {
        printk(KERN_INFO "NO sapce to write into peoc file \n");
        return -1;
    }
    if(copy_from_user(&info[write_index], buffer, length)) 
        return -2;
    write_index += length;
    printk(KERN_INFO " megharaj proc writing succesful, %d write \n", length);
    return length;
}

int read_info(char *page, char **start, off_t off, int count, int *eof, void *data) {
    int len;
    len = sprintf(page, "%s\n", &info[read_index]);
    read_index += len;
    printk(KERN_INFO " megharaj proc reading succesful, %d read \n", len);
    return len;
}


int proc_init(void) 
{
    int ret = 0;
    info = (char *)vmalloc(MAX_LEN);
    memset(info, 0 , MAX_LEN);
/*truct proc_dir_entry *create_proc_entry(const char *name, mode_t mode,
                                         struct proc_dir_entry *parent);*/
    proc_entry = create_proc_entry("megharaj_proc", 0666, NULL);
    if(proc_entry == NULL) {
        vfree(info);
        printk(KERN_INFO " megharaj proc not created \n");
        ret = -ENOMEM;
    }
    else {
        write_index = 0;
        read_index = 0;
        proc_entry->read_proc = read_info;
        proc_entry->write_proc = write_info;
        printk(KERN_INFO " megharaj proc created \n");
    }
    return ret;
}

void proc_clean(void) 
{
    vfree(info);
    remove_proc_entry("megharaj_proc", NULL);

}

MODULE_LICENSE("GPL");   
module_init(proc_init);
module_exit(proc_clean);

make file

obj-m   := proc.o

KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)

all:
    $(MAKE) -C $(KERNELDIR) M=$(PWD)

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
Megharaj
  • 1,589
  • 2
  • 20
  • 32
  • You're creating an proc entry under `/proc`, not under an existing proc entry. So .. you didn't answer the question ;-P – daisy Sep 20 '13 at 00:48
  • @warl0ck create_proc_entry("megharaj_proc", 0666, NULL); megharaj_proc will be the file inside the proc. If NULL states that the entry is made in the /proc, if you want to create the proc entry in any directory of the /proc, instead of null just replace NULL with /proc/directory – Megharaj Sep 20 '13 at 05:14
  • @warl0ck struct proc_dir_entry *create_proc_entry(const char *name, mode_t mode, struct proc_dir_entry *parent); is the syntax. if you have any problem let me know. Let me know what exactly you need that would be very helpful :) – Megharaj Sep 20 '13 at 05:17
  • @warl0ck also do execute this code and see. to do the same, to build make, then insmod the module. Than you can find a a file inside /proc/megharaj_proc. you can read it by cat /proc/megharaj_proc, also you can write it by echo data to write > /proc/megharaj_proc. – Megharaj Sep 20 '13 at 05:21
  • @warl0ck one more thing megharaj is my name so i have given proc entry name that way. Please change accordingly. Also the best way to learn is to execute the code and see the output and re write once on your own. – Megharaj Sep 20 '13 at 05:21
  • 1
    I don't think you understood my question. Try create an entry under `/proc/sys` instead. See if you could do that .. – daisy Sep 20 '13 at 07:26