LinuxQuestions.org
Help answer threads with 0 replies.
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 04-02-2009, 10:37 AM   #1
strider82
LQ Newbie
 
Registered: Apr 2009
Location: USA
Distribution: Debian, Ubuntu
Posts: 7

Rep: Reputation: 0
Question Write data to proc


I have some pre-computed data which I need to write to /proc, so that another kernel module can access it. I have written a kernel module to create the file in /proc, but not sure how to write data into it. The data to be written is already hardcoded into a structure in the module.

Thanks,
 
Old 04-02-2009, 12:20 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by strider82 View Post
I have written a kernel module to create the file in /proc, but not sure how to write data into it.
You need to add a read handler to the "struct file_operations" that you assign to entry->proc_fops. When someone does something like "cat /proc/sys/your_entry", the read handler will be called. In your read handler, you will convert the data in the structure to human readable format using snprintf.

Look at pages 83-90 of "Linux Device Drivers, 3rd. Ed", Corbet et. al.

If you don't have this book, run, don't walk, to Amazon.com and buy it.
 
Old 04-02-2009, 01:38 PM   #3
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
This code does that. A command string is passed in (by a userspace app that writes to /proc ) with an arbitrary number of commands in the form var=val;var=val and so forth. This code parses the individual var=val strings using the semicolon as a separator then passes the result on to a subroutine (proc_cmd) that does appropriate things with the data.

This is code that I wrote from scratch; you may use those portions of it that help you.

Code:
#define CMD_BUFFER_MAX_LEN 40

int dsp_write_procmem(struct file *file, const char __user *buf,
			   unsigned long count, void *data)
{
  char *loc_buf,*stst,*sten,*cmdbuf,tstchar,delimit;
  int cmdlen,stdiff,stdiff2,cmdpassed;
  char usecmd[CMD_BUFFER_MAX_LEN];
  cmdlen = CMD_BUFFER_MAX_LEN; //maximum length of any given command string
  delimit = 0x3b; //semicolon as a command delimiter
  cmdbuf = &usecmd[0];
  loc_buf = kmalloc(count,GFP_KERNEL);
  if(copy_from_user(loc_buf,buf,count)) {return -EFAULT;}
  stst = loc_buf;
  sten = loc_buf;
  stdiff = 0;
  while(*stst == 0x20 && stdiff < count) { //strip any leading spaces
	stst++;
	sten++;
  	stdiff = stst - loc_buf;
  }
  stdiff = (int)(stst - loc_buf);
  stdiff2 = (int)(sten - loc_buf);
  while(stdiff <= count && stdiff2 <= count){  //parse the command line, separating commands on semicolons
	tstchar = *sten;
	if(tstchar == delimit || tstchar == 0x0) {
		if(sten-stst <= 1) {//two semicolons in a row; no data
			sten++;
			stst = sten;
		} else {
			cmdpassed = 0;
			memset(cmdbuf,0x0,CMD_BUFFER_MAX_LEN);
			while(stst < sten && cmdpassed < CMD_BUFFER_MAX_LEN) {
				if(*stst == 0x0a || *stst == 0x0d){stst++;} //strip newlines
				*cmdbuf++ = *stst++;
				cmdpassed++;
			}
			cmdbuf = &usecmd[0];
			if(cmdpassed == CMD_BUFFER_MAX_LEN) {
				printk("<1>tidsp:  Error; command buffer length overflow.  Command was %s\n",(char *)usecmd);
			}
			sten++;
			stst = sten;
			if(tstchar == 0x0) {
				stdiff = count;
			} else {
  				stdiff = (int)(stst - loc_buf);
			}
  			stdiff2 = (int)(sten - loc_buf);
			proc_cmd(file,cmdbuf);
		}
	} else {
		sten++;
  		stdiff = (int)(stst - loc_buf);
  		stdiff2 = (int)(sten - loc_buf);
	}
  }
  kfree(loc_buf);
return count;
}
 
  


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
setting up port forwarding, but write access to /proc/.../.../... denied wilsonsamm Linux - Networking 6 01-05-2010 04:16 PM
command to write into proc entry pravin.embedded Linux - Newbie 1 09-11-2008 08:03 AM
Anybody know how to write to proc??? greek Linux - Hardware 2 08-15-2004 01:46 PM
Getting data from /proc azrail1222 Linux - General 10 07-15-2004 03:05 AM
Can't write to the /proc/acpi/ directoryas root. Netromancer Linux - Newbie 4 05-15-2004 03:54 AM

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

All times are GMT -5. The time now is 11:20 AM.

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