我正在编写一个linux内核模块,它创建一个列出进程列表的块设备。
我能够在内核日志文件中显示列表,但现在我正在尝试将输出显示到stdout。我的目标是在dev_open()中创建一个包含所有进程的大字符串,然后将该字符串复制到缓冲区,该缓冲区将转到dev_read()函数。
但是,我正在尝试以下代码,但我收到一条消息,告诉stdout说" Killed"。
这是什么意思?我该怎么解决这个问题呢。
代码::
#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>
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[1000];
int tem;
static struct file_operations fops =
{
.read = dev_read,
.open = dev_open,
.release = dev_rls,
};
int init_module(void)
{
int t = register_chrdev(81,"tempo",&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);
len=strlen(msg);
temp=len;
tem++;
}
printk("%s [%d]\n",task->comm , task->pid);
return 0;
}
void cleanup_module(void)
{
unregister_chrdev(81,"tempo");
}
static ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *offp)
{
printk("Hi\n");
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;
}