LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2005, 05:37 PM   #1
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Rep: Reputation: 15
Strange that i'm not getting an output


I am building a kernel module to run and compile on my system. The way the program works is that it obtains process information from the system and outputs the information to a proc file that the program creates which can be view by cat /proc/myprocess.

My problem is that the proc file does not seem to contain any information. It must have something to do with the way i'm dealing with the buffers but I just cannot figure out what it is exactly. Below is my full code.


Code:
#include <linux/init.h>
#include <linux/kernel.h>	/* Necessary because we are working with kernel */
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>	/* Necessary because we use the proc fs */
#include <linux/file.h>		/* Necessary to use the files_struct struct */
#include <linux/sched.h>

#define procfs_name "myprocess"
//..........................
struct proc_dir_entry *myprocfile; //Holds procfile information

int
myprocess_load(char buffer[32024], char **buffer_location, off_t offset, int buffer_length, int *eof, void *data)
{
	int ret;
	int add;
	int i = 0;
	struct task_struct *task; //Holds process information
	struct file *fd;
	struct dentry *nameinfo;// Describes a name of the specific file
	struct files_struct *pidfile; // Used to get specific file information
	
	printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);
	
	if (offset > 0) {
		
		/* we have finished to read, return 0 */
		ret  = 0;
		
	} else {
		
		for_each_process(task){
			add = sprintf(buffer, "Process: %s  \t Process PID %d \n", task->comm, task->pid); //Print to proc
			buffer = buffer + add; //Add additional info to buffer

			pidfile = task->files; //Files for the process
			
			if (pidfile)
			{
				for( i = 0; i <= (pidfile->next_fd-1); i++)
				{
					if(pidfile->fd[i])
					{
						fd = pidfile->fd[i];
						nameinfo = fd->f_dentry;
						if(nameinfo)			
						{
							add = sprintf(buffer, "\tOpen Descriptor name: %s Iname: %8p\n",nameinfo->d_iname,fd);// Print to proc
							buffer = buffer + add; //Add additional info to buffer
							
						}// If nameinfo
					}// If pidfile->fd
				}// For loop
			} // If pidfile
			///..........
		}// For each process
	}// Else
	
	return ret;
}// Myprocess function

int init_module()
{
	myprocfile = create_proc_entry(procfs_name, 0644, NULL);
	
	if (myprocfile == NULL) {
		remove_proc_entry(procfs_name, &proc_root);
		printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
			procfs_name);
		return -ENOMEM;
	}
	
	myprocfile->read_proc = myprocess_load;
	myprocfile->owner 	 = THIS_MODULE;
	myprocfile->mode 	 = S_IFREG | S_IRUGO;
	myprocfile->uid 	 = 0;
	myprocfile->gid 	 = 0;
	myprocfile->size 	 = 37;
	
	printk(KERN_INFO "/proc/%s created\n", procfs_name);	
	return 0;	/* everything is ok */
}

static void myprocess_unload(void)
{
	printk("\nFinished Process lookup\n");
	remove_proc_entry(procfs_name, &proc_root);
}

module_exit(myprocess_unload);

Last edited by alltime; 10-24-2005 at 06:34 PM.
 
Old 10-24-2005, 06:35 PM   #2
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
After the second instance of the following code:

buffer = buffer + add;

The sytem will actually stall on me and then I have to reboot.
 
Old 10-24-2005, 07:25 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by alltime
After the second instance of the following code:

buffer = buffer + add;

The sytem will actually stall on me and then I have to reboot.
buffer is a char* and add is an int
did you intend to move the buffer? maybe this is why it fails you may be an overflow espically is its a signed int.

Last edited by dmail; 10-24-2005 at 07:36 PM.
 
Old 10-24-2005, 07:36 PM   #4
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
Well this is really my problem.

I am trying to fill the buffer with information. When for_each_process runs once, it fill s the buffer with:

process name PID
INIT 1
Open Descriptor name: Init Iname: 0x985


The next time the loop runs, i'm trying to add the data to the end of the buffer possibly (not sure if I explained that correctly) so that the buffer would read

Process Nam PID
INIT 1
Open Descriptor name: Init Iname: 0x985
Some procsess Some PID
Open Descriptor name: something Iname: some number

and so on.
 
Old 10-24-2005, 07:39 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
from what i see this is just C(am i write)
Buffer = Buffer + add;
will move the position of the buffer(char*) by add*sizeof(char)

edit--
ok i see now
what you want to use is the char** buffer_location which i cant see that you use.
set it to point to the start of the buffer and then you could derefence it and use it. incrementing it as you go.


Last edited by dmail; 10-24-2005 at 07:50 PM.
 
Old 10-24-2005, 08:58 PM   #6
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
I understand what you mean but i'm a little confused on how to do this.
 
Old 10-24-2005, 09:49 PM   #7
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
Would it be as easy as just setting

*buffer_location = buffer;
 
Old 10-25-2005, 04:19 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally posted by alltime
Would it be as easy as just setting

*buffer_location = buffer;
yep that just points the location to the start of the buffer.
 
Old 10-26-2005, 08:04 PM   #9
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
Ahh, I did get the code to work properly. The only problem that I have now and i'm not sure why but some systems stall after about 20 seconds after the code executes and i'm not quite sure why. Here is my final code.

Code:
/*
*       Designed and Coded by alltime
*       Akande@gmail.com
*/
#include <linux/init.h>
#include <linux/kernel.h>	/* Necessary because we are working with kernel */
#include <linux/module.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>	/* Necessary because we use the proc fs */
#include <linux/file.h>		/* Necessary to use the files_struct struct */
#include <linux/sched.h>

#define procfs_name "myprocess"

struct proc_dir_entry *myprocfile; //Holds procfile information

int
myprocess_load(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data)
{
	int ret;
	int add;
	int i = 0;
	struct task_struct *task; // Process information
	struct file *fd;
	struct dentry *nameinfo;// Describes a name of the specific file
	struct files_struct *pidfile; // Used to get specific file information
	
	printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);
	
	if (offset > 0) {

		/* we have finished, return 0 */
		ret  = 0;
				
	} else {

		add = sprintf(buffer, "Process\tPID\n");
		
		for_each_process(task){
			add += sprintf(buffer + add, "%s\t%d\n", task->comm, task->pid); //Print to proc
                        *buffer_location = buffer; //Add additional info to buffer
                        printk("buffer is: %s\n", buffer);
			pidfile = task->files; //Files for the process
			
			if (pidfile) // Gathering open files for processes
			{
				for( i = 0; i <= (pidfile->next_fd-1); i++)
				{
					if(pidfile->fd[i])
					{
						fd = pidfile->fd[i];
						nameinfo = fd->f_dentry;
						if(nameinfo)			
						{
                        				add += sprintf(buffer + add, "\tFile name: %s[%8p] \n",nameinfo->d_iname,fd);// Print to proc
                                                        //*buffer_location = buffer; //Add additional info to buffer
                                                        //printk("with filename and info buffer is: %s\n", buffer);
							
						}// If nameinfo
					}// If pidfile->fd
				}// For loop
			} // If pidfile
			
			if (offset >= add) {
			     *eof = 1;
			     return 0;
			}//if offset
			
			// Section below increments the buffer so that we are not continously writing over the same buffer portion
			*buffer_location = buffer + offset;
			if (buffer_length >= add - offset) {
			     *eof = 1;
			}		
		}// For each process
		return (min(buffer_length, add - (int)offset)); // Return buffer value so output to proc
	}// Else
	
}// Myprocess function

int init_module()
{
	myprocfile = create_proc_entry(procfs_name, 0644, NULL);
	
	if (myprocfile == NULL) {
		remove_proc_entry(procfs_name, &proc_root);
		printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
			procfs_name);
		return -ENOMEM;
	}
	
	myprocfile->read_proc = myprocess_load;
	myprocfile->owner 	 = THIS_MODULE;
	myprocfile->mode 	 = S_IFREG | S_IRUGO;
	myprocfile->uid 	 = 0;
	myprocfile->gid 	 = 0;
	myprocfile->size 	 = 37;
	
	printk(KERN_INFO "/proc/%s created\n", procfs_name);	
	return 0;	/* everything is ok */
}
/* Unload module */
static void myprocess_unload(void)
{
	printk("\nFinished Process lookup\n");
	remove_proc_entry(procfs_name, &proc_root);
}

module_exit(myprocess_unload);

MODULE_AUTHOR("alltime");
MODULE_LICENSE("GPL");

Last edited by alltime; 10-26-2005 at 08:06 PM.
 
  


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
No output through SPDIf, but analog works. How do I get output through SPDIF? seanwnz Linux - General 11 11-08-2007 11:24 AM
tv output Shibby Linux - Software 1 06-07-2005 11:35 AM
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM
Output of ls -l Crashed_Again Linux - Newbie 4 12-11-2003 06:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03: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