Find the answer to your Linux question:
Results 1 to 3 of 3
#include <linux/module.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/kernel.h> #include <linux/malloc.h> #include <asm/uaccess.h> #include <linux/errno.h> unsigned int fs_major = 0; static char *data; //static dev_OPEN=0; static struct file_operations chr_fops = { ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    9

    What is wrong of this module program?

    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/fs.h>
    #include <linux/kernel.h>
    #include <linux/malloc.h>
    #include <asm/uaccess.h>
    #include <linux/errno.h>
    unsigned int fs_major = 0;
    static char *data;
    //static dev_OPEN=0;
    static struct file_operations chr_fops = {
    open: test_open,
    read: test_read,
    write: test_write,
    release: test_release,
    };
    static ssize_t test_read(struct file *file,char *buf,size_t count,loff_t *f_pos);
    static ssize_t test_write(struct file *file,const char *buffer,size_t count,loff_t *f_pos);
    static int test_open(struct inode *inode,struct file *file);
    static int test_release(struct inode *inode,struct file *file);
    int init_module(void);
    void cleanup_module(void);

    static ssize_t test_read(struct file *file,char *buf,size_t count,loff_t *f_pos)
    { int len;
    if(count<0)
    return -EINVAL;
    len = strlen(data);
    if(len<count)
    count = len;
    copy_to_user(buf,data,count+1);
    return count;
    }

    static ssize_t test_write(struct file *file,const char *buffer,size_t count,loff_t *f_pos)
    {
    if(count < 0)
    return -EINVAL;
    kfree(data);
    data = (char *)kmalloc(sizeof(char)*(count+1),GFP_KERNEL);
    if(!data)
    return -ENOMEM;
    copy_from_user(data,buffer,count+1);
    return count;
    }

    static int test_open(struct inode *inode,struct file *file)
    {
    MOD_INC_USE_COUNT;
    // dev_OPEN++;
    // try_module_get(fs_major);
    printk("This is open\n");
    return 0;
    }

    static int test_release(struct inode *inode,struct file *file)
    {
    MOD_DEC_USE_COUNT;
    // dev_OPEN--;
    // module_put(fs_major);
    printk("this is released\n");
    return 0;
    }

    int init_module(void)
    {
    int res;
    res=register_chrdev(0,"fs",&chr_fops);
    if(res<0)
    {
    printk("can't get major name!\n");
    return res;
    }
    if(fs_major = 0)
    fs_major = res;
    return 0;
    }
    void cleanup_module(void)
    {
    unregister_chrdev(fs_major,"fs");
    }
    __________________________________________________ _______
    makefile file as below:

    CC=gcc
    MODCFLAGS:=-Wall -DMODULE -D__KERNEL__ -DLINUX -I/usr/src/kernels/2.4.18-8.el5-i686/include/
    test.o:test.c
    $(CC) $(MODCFLAGS) -c $<
    __________________________________________________ ______

    and the error as :
    test.c:12: `test_open' undeclared here (not in a function)
    test.c:12: initializer element is not constant
    test.c:12: (near initialization for `chr_fops.open')
    test.c:13: `test_read' undeclared here (not in a function)
    test.c:13: initializer element is not constant
    test.c:13: (near initialization for `chr_fops.read')
    test.c:14: `test_write' undeclared here (not in a function)
    test.c:14: initializer element is not constant
    test.c:14: (near initialization for `chr_fops.write')
    test.c:15: `test_release' undeclared here (not in a function)
    test.c:15: initializer element is not constant
    test.c:15: (near initialization for `chr_fops.release')
    test.c:16: initializer element is not constant
    test.c:16: (near initialization for `chr_fops')
    test.c:16: initializer element is not constant
    test.c:16: (near initialization for `chr_fops')
    test.c:16: initializer element is not constant
    test.c:16: (near initialization for `chr_fops')
    test.c:16: initializer element is not constant
    test.c:16: (near initialization for `chr_fops')
    test.c: In function `init_module':
    test.c:74: warning: suggest parentheses around assignment used as truth value
    test.c: At top level:
    test.c:25: warning: `test_read' defined but not used
    test.c:36: warning: `test_write' defined but not used
    test.c:48: warning: `test_open' defined but not used
    test.c:57: warning: `test_release' defined but not used
    __________________________________________________
    what is going on? who can help me?
    thanks!
    __________________________________________________ ______

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Well, I haven't look at the whole code of your module (it would be easier to read if you place it in CODE blocks !) but in this line of the file_operations struct:

    Code:
    release: test_release,
    you must remove the comma at the end.

    These functions:

    Code:
    static ssize_t test_read(struct file *file,char *buf,size_t count,loff_t *f_pos);
    static ssize_t test_write(struct file *file,const char *buffer,size_t count,loff_t *f_pos);
    must be defined before the defining of the file_operations struct.

    Try this makefile (place real tabs before the make commands!):

    Code:
    obj-m	+= test.o
    
    all:
    	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
    
    clean:
    	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
    And please enclose your code in code brackets, that will increase the number of people reading your posting.


    Regards

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    9
    Thank you for your respond!
    I am sorry, I am new for here,I will do as you said next time!

Posting Permissions

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