Results 1 to 1 of 1
Hi,
I just wrote a very basic character device that implements the open(), read(), write() and close() syscalls. I compile it as a kernel module, then bind it with insmod, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-20-2008 #1Just Joined!
- Join Date
- Mar 2008
- Posts
- 1
Character device causing segfault when open()'ed
Hi,
I just wrote a very basic character device that implements the open(), read(), write() and close() syscalls. I compile it as a kernel module, then bind it with insmod, and create a node to the corresponding device in /dev/nvram. All that works fine.
The problem is, when I call open() to the device from a userspace program it gives a nasty segmentation fault. My implementation of open() is empty and just returns 123 so I wonder what would be causing that?
Here is the code of the module:
The userspace program just calls open() and gives dozens of messages in syslogd for violating memory:Code:#include <linux/module.h> #include <linux/errno.h> #include <linux/fs.h> #include <linux/mm.h> #include <linux/interrupt.h> #include <linux/sched.h> #include <asm/uaccess.h> #include <asm/io.h> #define NVRAM_MINOR 0 #define NVRAM_MAJOR 32 #define NVRAM_DEVICE_NAME "nvram" #define NVRAM_BUFFER_SIZE 4096 #define NVRAM_MAX_TRANSACTIONS 100 static int nvram_open (struct inode *, struct file *); static ssize_t nvram_write (struct file *, const char *, size_t, loff_t *); static ssize_t nvram_read (struct file *, char *, size_t, loff_t *); static int nvram_release (struct inode *, struct file *); static const struct file_operations nvram_fops = { .owner = THIS_MODULE, .llseek = NULL, .read = nvram_read, .write = nvram_write, .readdir = NULL, .poll = NULL, .ioctl = NULL, .mmap = NULL, .open = nvram_open, .flush = NULL, .release = nvram_release, .fsync = NULL, .fasync = NULL, .lock = NULL, .readv = NULL, .writev = NULL, }; static int __init nvram_init_module (void) { printk(KERN_ALERT "Initializing NVRAM driver.\n" ); if (register_chrdev(NVRAM_MAJOR, NVRAM_DEVICE_NAME, &nvram_fops) != 0) { return -EIO; } return 0; } static void __exit nvram_cleanup_module (void) { unregister_chrdev(NVRAM_MAJOR, NVRAM_DEVICE_NAME); } static int nvram_open (struct inode *inode, struct file *file) { return 123; } static ssize_t nvram_read (struct file *file, char *buf, size_t count, loff_t *offset) { return 345; } static ssize_t nvram_write (struct file *file, const char *buf, size_t count, loff_t *offset) { return 567; } static int nvram_release (struct inode *inode, struct file *file) { return 0; } module_init(nvram_init_module); module_exit(nvram_cleanup_module);
Any help would be greatly appreciated!Code:#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/ioctl.h> #include <unistd.h> #include <stdio.h> int main (void) { int fd; fd = open("/dev/nvram", O_RDONLY); return 0; }
P.S.
Issue resolved!


Reply With Quote
