LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-31-2006, 10:07 AM   #1
android_online
LQ Newbie
 
Registered: May 2006
Location: Lexington, KY
Distribution: slackware
Posts: 7

Rep: Reputation: 0
using profs_example.c to read data from kernel


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.

thx.
 
Old 06-01-2006, 06:39 AM   #2
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
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.

exman
 
Old 06-02-2006, 10:05 AM   #3
android_online
LQ Newbie
 
Registered: May 2006
Location: Lexington, KY
Distribution: slackware
Posts: 7

Original Poster
Rep: Reputation: 0
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??

thx
 
Old 06-06-2006, 03:56 AM   #4
exman
LQ Newbie
 
Registered: May 2006
Location: Germany, BS
Distribution: Debian Kernel 2.6.15, Kubuntu Dapper
Posts: 24

Rep: Reputation: 15
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);
Makefile:
Code:
INC_DIR = /usr/src/linux/include

# build flags for irq kernel module
MODCCDEFS = $(CCDEFS) -D__KERNEL__ -DMODULE 
MODCCDEFS += -DMODDBG
MODCCFLAGS = $(CCFLAGS) -O2 -Wall $(MODCCDEFS) -I $(INC_DIR)
LINUX_VERSION = $(shell uname -r)

PWD = $(shell pwd)

obj-m += procmod.o

all: clean
	make -C /usr/src/linux/ M=$(PWD) modules

clean:
	-rm -f *.o *.ko *.cmd
	-rm -rf ./.tmp_versions

distclean: clean

install:
	mv procmod.ko  /lib/modules/$(LINUX_VERSION)/misc/
 
Old 06-06-2006, 08:53 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 06-06-2006, 09:10 AM   #6
android_online
LQ Newbie
 
Registered: May 2006
Location: Lexington, KY
Distribution: slackware
Posts: 7

Original Poster
Rep: Reputation: 0
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read data from a socket allomeen Linux - Networking 1 05-08-2006 03:19 AM
Burn Data DVD... Read Data in Linux and Windows SaintStrive Linux - Newbie 3 09-18-2004 05:04 PM
read from a data DVD on FC1 Vid Linux - Software 3 05-31-2004 01:35 PM
cannot read data cds ac404 Linux - Hardware 1 03-15-2004 12:59 PM
cannot read data in CD-RW tony yu Mandriva 4 12-26-2003 03:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 12:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration