B
baljit92
I'm a newbie in linux kernel programming and needed some help.
I need to write a Linux kernel module that creates a block device /dev/theprocs that "contains" the process list.
So I have written some code which gives me the list of the current processes. The only problem I am running into is displaying the list in stdout(the terminal) instead of the kernel log file.
When I cat /dev/theprocs first time, its gives me the message "Killed". When I remove and install the module again; it works. So it works every alternate time.
I need help in solving this issue.
Code:
#include <linux/module.h>
#include<linux/sched.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Device Driver Demo");
MODULE_AUTHOR("Joey");
static int dev_open(struct inode *, struct file *);
static int dev_rls(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
int len,temp;
char msg[3000];
int totalLen;
static struct file_operations fops =
{
.read = dev_read,
.open = dev_open,
.release = dev_rls,
};
int init_module(void)
{
int t = register_chrdev(101,"theprocs",&fops);
if(t<0) printk(KERN_ALERT "Device failed to register!");
else printk(KERN_ALERT "Registered device...\n");
return t;
}
static int dev_open(struct inode *inod, struct file *fil)
{
struct task_struct *task;
for_each_process(task)
{
printk("%s [%d]\n",task->comm, task->pid);
strcat(&msg[0],task->comm);
strcat(&msg[0],"\n");
totalLen = totalLen + strlen(task->comm);
}
strcat(&msg[0],"\0");
len=strlen(msg);
temp=len;
printk("%s [%d]\n",task->comm , totalLen);
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(101,"theprocs");
}
static ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offp)
{
if(count>temp)
{
count=temp;
}
temp=temp-count;
copy_to_user(buf,msg, count);
if(count==0)
temp=len;
return count;
//return 0;
}
static int dev_rls(struct inode *inod, struct file *fil)
{
printk(KERN_ALERT"Done with device\n");
return 0;
}
Continue reading...
I need to write a Linux kernel module that creates a block device /dev/theprocs that "contains" the process list.
So I have written some code which gives me the list of the current processes. The only problem I am running into is displaying the list in stdout(the terminal) instead of the kernel log file.
When I cat /dev/theprocs first time, its gives me the message "Killed". When I remove and install the module again; it works. So it works every alternate time.
I need help in solving this issue.
Code:
#include <linux/module.h>
#include<linux/sched.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Device Driver Demo");
MODULE_AUTHOR("Joey");
static int dev_open(struct inode *, struct file *);
static int dev_rls(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
int len,temp;
char msg[3000];
int totalLen;
static struct file_operations fops =
{
.read = dev_read,
.open = dev_open,
.release = dev_rls,
};
int init_module(void)
{
int t = register_chrdev(101,"theprocs",&fops);
if(t<0) printk(KERN_ALERT "Device failed to register!");
else printk(KERN_ALERT "Registered device...\n");
return t;
}
static int dev_open(struct inode *inod, struct file *fil)
{
struct task_struct *task;
for_each_process(task)
{
printk("%s [%d]\n",task->comm, task->pid);
strcat(&msg[0],task->comm);
strcat(&msg[0],"\n");
totalLen = totalLen + strlen(task->comm);
}
strcat(&msg[0],"\0");
len=strlen(msg);
temp=len;
printk("%s [%d]\n",task->comm , totalLen);
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(101,"theprocs");
}
static ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offp)
{
if(count>temp)
{
count=temp;
}
temp=temp-count;
copy_to_user(buf,msg, count);
if(count==0)
temp=len;
return count;
//return 0;
}
static int dev_rls(struct inode *inod, struct file *fil)
{
printk(KERN_ALERT"Done with device\n");
return 0;
}
Continue reading...