Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This might just be a dumb question cuz I'm using procfs for the 1st time. So need some hlp.
I needed to gather some statistics from Ext3fs. So I tried to use the procfs_example.c from Documentation/DocBook/, modified this function which is used to read jiffies from the kernel-
//original
static int proc_read_jiffies(char *page, char **start,off_t off, int count,int *eof, void *data)
{
int len;
len = sprintf(page, "jiffies = %ld\n", jiffies);
return len;
}
Based on the proc_read_jiffies(), I introduced a new header file in Ext3 - 'myheader.h'. This has a single data element (int mydata), which get initialized and updated in the Ext3fs code. And I then try to sprint that along with the jiffies.
//modified
static int proc_read_jiffies(char *page, char **start,off_t off, int count,int *eof, void *data)
{
int len;
len = sprintf(page, "jiffies = %ld \t mydata = %d\n", jiffies,mydata);
return len;
}
But mydata is always 0 whn I run -
$cat /proc/procfs_example/jiffies
I made sure the mydata is valid thru printk.
Please let me know if there's something I am missing here, or the approach itself is wrong.
Hi,
i presume that you have exported mydata from the ext3 code so it can be seen from other kernel moduls.
How do you declare mydata in the procfs_example.c?
Maybe you have to declare it volatile so the value is checked on each access.
I declared mydata as extern. I don't think I need to export it in that case (?). Anyways I opted for a different approach, I now open and write a file to userspace from within the ext3 code. And have ioctls to open and close the file. It works but I would still like to know how can it be done thru procfs.
Or is there any other way to spit out information from inside a kernel module??
Hi,
dont know if it helps you but here is a small module which creates a proc entry.
procmod.c:
Code:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/string.h>
/* define some module specific things */
#define MODNAME "procmod"
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Proc filesystem test module");
/* print debug info if compiled with "-DMODDBG" */
#if defined MODDBG
#define DBGPRINT(x...) printk((KERN_DEBUG x))
#else
#define DBGPRINT(x...)
#endif
/* macro for kernel infos */
#define PINFO(x...) printk(KERN_INFO x)
/* macro for kernel errors */
#define PERR(x...) printk(KERN_ERR x)
#define HELLOSTR "Hello from procfs!\n"
/* globals */
static struct proc_dir_entry* procentry = NULL;
static int timesread=0;
/* the read function for the proc entry */
int procmod_readproc(char *buf, char **start, off_t offset,
int count, int *eof, void *data)
{
int len=0;
DBGPRINT("%s::%s:\n\tbuf= %s\n\tstart= %s\n\tbufoffs= %d\n\t"
"bufsize= %d\n\t*eof= %d\n\tdata= 0x%8p\n",
MODNAME,__FUNCTION__,buf,*start,offset,count,*eof,data);
*start =buf;
// if not written to buf before
if (!offset) {
// inc read counter
timesread++;
// print text to buffer
len += sprintf(buf,"%s You read this file %d times.\n",
HELLOSTR,timesread);
*eof = 1;
}
return len;
}
/* the module init function */
int __init procmod_init(void)
{
DBGPRINT("%s is loaded into the kernel\n",MODNAME);
// create the procfs entry
PINFO("%s: creating \"/proc/%s\" for read access\n",MODNAME,MODNAME);
procentry = create_proc_read_entry(MODNAME, 0, NULL, procmod_readproc, NULL);
DBGPRINT(("%s: procentrysi wrotetruct @ 0x%8p\n",MODNAME,procentry));
if (!procentry) {
PERR("%s ERR: could not create procfs entry!\n",MODNAME);
return -ENODEV;
}
return 0;
}
/* the module exit function */
void procmod_exit(void)
{
PINFO("%s: removing \"/proc/%s\"\n",MODNAME,MODNAME);
remove_proc_entry(MODNAME,NULL);
DBGPRINT("%s is removed from the kernel\n",MODNAME);
}
/* register init and exit functions */
module_init(procmod_init);
module_exit(procmod_exit);
For the internal, static-link-only parts of the kernel, regular linking rules apply. Symbols used in one part of the code must be external.
Typically, however, we want to allow the bit to be placed in a dynamically loadable module, in which case all of the symbols in that module which another module might need to refer to must be declared such that they will appear in the symbol-map. Callers "find" the symbols by looking them up in that map.
Thanks for the code. I did try the same way and it works. But this approach works good for small amount of data. How about large volume of data, getting generated very fast. For example the block allocation in Ext3. Say if you write a 10mb file, it would generate 2560 lies of output (2560, 4k blocks). I am not sure how this can be handles thru profs. Any ideas?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.