ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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);
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.
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
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.
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");
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.