Find the answer to your Linux question:
Results 1 to 3 of 3
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. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2014
    Posts
    1

    Getting killed message when doing cat /dev/myModule


    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;
    }

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    362
    I'm no kernel guru, but...

    You say you need to create a *block* device, but aren't you actually creating a *character* device?

    Also I'm not so sure about that for_each_process(task) bit. It seems a bit grotty and strcat is dangerous. Use strncat and check the lengths for sanity. You could be getting some unexpected stuff. msg[] only holds 3000 bytes. That's not very big. It's only 37.5 lines of a 80 column display.

    You never check to see if task is NULL. Dereferencing a NULL pointer goes straight to segfault with no stops on the way.

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,512
    Miven is correct. Normally you can't just cat from /dev/blockdevid unless your kernel driver code has some ability to deal with such requests. Without your full driver code, there isn't much we can tell you.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •