Работает отлично. Суёшь в него какие-нибудь буквы, а он возвращает чистейший
метан. Статистика работы биореактора находится в /proc/bioreactor. Там указано
количество произведённого метана и количество метана, находящегося в
резервуаре.
Makefile:
_______________________________________
obj-m += bioreactor.o
all:
make -C /lib/modules/`uname -r`/build M=${PWD} modules
clean:
make -C /lib/modules/`uname -r`/build M=${PWD} clean
_______________________________________
bioreactor.c:
_______________________________________
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
static int major;
static int opened;
static int ch4, total_ch4;
struct proc_dir_entry *proc_bioreactor;
static int bioreactor_open(struct inode *inode, struct file *file);
static int bioreactor_close(struct inode *inode, struct file *file);
static ssize_t bioreactor_read(struct file *, char *, size_t, loff_t *);
static ssize_t bioreactor_write(struct file *, char *, size_t, loff_t *);
int proc_bioreactor_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data);
static struct file_operations fops = {
.read = bioreactor_read,
.write = bioreactor_write,
.open = bioreactor_open,
.release = bioreactor_close
};
static int bioreactor_open(struct inode *inode, struct file *file)
{
if (opened) return -EBUSY;
opened++;
printk(KERN_INFO "Door opened. Give me some shit!\n");
return 0;
}
static int bioreactor_close(struct inode *inode, struct file *file)
{
opened--;
printk(KERN_INFO "Door closed.\n");
return 0;
}
static ssize_t bioreactor_read(struct file *f, char *buffer, size_t length, loff_t *offset)
{
int l, i;
char metan = 'm';
if (length > ch4)
{
l = ch4;
ch4 = 0;
} else
{
l = length;
ch4 -= length;
}
for(i = 0; i < l; i++) put_user('m', buffer++);
return l;
}
static ssize_t bioreactor_write(struct file *f, char *buffer, size_t length, loff_t *offset)
{
ch4 += length;
total_ch4 += length;
return length;
}
int proc_bioreactor_read(char *buffer, char **buffer_location, off_t offset, int buffer_length, int *eof, void *data)
{
return sprintf(buffer, "Total CH4: %10d Kg\nCH4 in the tank: %10d Kg\n", total_ch4, ch4);
}
static int __init bioreactor_init(void)
{
printk(KERN_INFO "Turning bioreactor on...\n");
major = register_chrdev(0, "bioreactor", &fops);
if (major < 0)
{
printk(KERN_ALERT "Failed to register device\n");
return major;
}
printk(KERN_INFO "Registered device %d\n", major);
proc_bioreactor = create_proc_entry("bioreactor", 644, NULL);
proc_bioreactor->read_proc = proc_bioreactor_read;
proc_bioreactor->owner = THIS_MODULE;
proc_bioreactor->mode = S_IFREG | S_IRUGO;
proc_bioreactor->uid = 0;
proc_bioreactor->gid = 0;
proc_bioreactor->size = 20;
printk(KERN_INFO "/proc/bioreactor has been created\n");
return 0;
}
static void __exit bioreactor_exit(void)
{
int r = unregister_chrdev(major, "bioreactor");
if (r < 0) printk(KERN_ALERT "Error in unregister_chrdev %d\n", r);
remove_proc_entry("bioreactor", &proc_root);
printk(KERN_INFO "Turning bioreactor off...\n");
}
MODULE_AUTHOR("unnamed");
MODULE_DESCRIPTION("Bioreactor");
module_init(bioreactor_init);
module_exit(bioreactor_exit);
_______________________________________