Markus Rechberger / Kernel Spielereien
 
StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern

Meine ersten Schritte im Linux Code, hierbei wurde erstmals mehr auf Quantität als auf Qualität geachtet, das ganze wurde für Linux2.6 "entwickelt"

Ziele:

 * eine device in /dev erstellen
 * proc filesystem
 * read/write/(ioctl) auf diese device durchführen

Motivation:
 * wie immer eigenes interesse :)

Inspired by:
 * Sourcestruktur des Creative SB Extigy USB Kernelmodules
 * cvsfs

#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/smp_lock.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/usb.h>
#include <linux/version.h>
#include <linux/vmalloc.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>


MODULE_AUTHOR("Markus Rechberger");
MODULE_DESCRIPTION("testmodule");
MODULE_LICENSE("GPL");

static int rev_fill_super(struct super_block *sb, void *options, int silent){
        printk(KERN_INFO"trying to mount this thing!\n");
        printk(KERN_INFO"%s\n",sb->s_id);
        printk(KERN_INFO"mounting options: %s\n",(char*)options);
        return(-EINVAL);
}

struct super_block *revfs_get_sb (struct file_system_type *fs_type, int flags, const char *dev_name, void *data){
        return get_sb_bdev(fs_type,flags,dev_name,data,rev_fill_super);
}

static struct file_system_type revfs_fs_type={
        .owner = THIS_MODULE,
        .name = "revfs",
        .get_sb = revfs_get_sb,
        .kill_sb = kill_block_super,
//      .fs_flags = FS_REQUIRES_DEV,
};


static int foo_open(struct inode *inode, struct file *filp);
static ssize_t foo_read(struct file *filp, char *buf, size_t count, loff_t *f_pos);
static int foo_release(struct inode *inode, struct file *file);
static ssize_t foo_write(struct file *file, const char *buffer, size_t count, loff_t *ppos);

static struct proc_dir_entry *revfs_root = NULL;
const char *revfs_procfs_root = "revfs";
const char *revfs_procfs_mounts         = "mounts";

static int aufgabe_major;

static struct file_operations foo_fops = {
       owner: THIS_MODULE,
       open:  foo_open,
       read:  foo_read,
       write: foo_write,
       release: foo_release,
};

static int foo_open(struct inode *inode, struct file *filp){
        printk("<1>hOhoooo .. someone want's me!!\n");
        return(0);
}

static ssize_t foo_write(struct file *file, const char *buffer, size_t count, loff_t *ppos)
{
        printk("<1>YYYEEP : someone told me :%s\n",buffer);
        return(count);
}

static ssize_t foo_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
        char name[100];
        printk("<1>someone's reading here! %d bytes\n",count);
        strcpy(name,"Markus Rechberger!");
        copy_to_user(buf,name,strlen(name)+1);

        return(strlen(name)+1);
}

static int foo_release(struct inode *inode, struct file *file)
{
        return(0);
}
static int proc_revfs_read_mounts (char *buffer, char **start, off_t offset, int size, int *eof, void *data){

        printk(KERN_INFO"procfs_read_mounts huhu\n");
        return(0);
}


int init_module2(void){
        printk("<1>Hello world\n");
        revfs_root = proc_mkdir(revfs_procfs_root,NULL);
        if(revfs_root!=NULL){
                struct proc_dir_entry *entry;
                revfs_root->owner = THIS_MODULE;
                entry = create_proc_entry(revfs_procfs_mounts,S_IFREG|S_IRUGO,revfs_root);
                if(entry!=NULL){
                        entry->read_proc = proc_revfs_read_mounts;
                        entry->owner = THIS_MODULE;
                } else {
                        remove_proc_entry(revfs_procfs_root,NULL);
                }
        }
        aufgabe_major=register_chrdev(0,"foo",&foo_fops);
        register_filesystem (&revfs_fs_type);
        printk("<1>registered: %d\n",aufgabe_major);
        return 0;
}

void xcleanup_module(void){
        remove_proc_entry (revfs_procfs_mounts, revfs_root);
        remove_proc_entry (revfs_procfs_root, NULL);

        if(unregister_chrdev(aufgabe_major,"foo")){
                printk("error unregistering foo");
        }
        unregister_filesystem (&revfs_fs_type);
        printk("<1>bybye!\n");
}


module_init(init_module2);
module_exit(xcleanup_module);

Zugehörige Makefile
ifneq ($(KERNELRELEASE),)
obj-m := aufgabe.o
#aufgabe-objs := aufgabe.o

else

KERNEL_SRC = /lib/modules/$(shell uname -r)/build
CC = gcc
CFLAGS = -Wall

all: modules

modules:
        $(MAKE) -C $(KERNEL_SRC) SUBDIRS=$(shell pwd) modules

endif

Testfiles:
#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

int main(){
        FILE *filehandle;
        char buffer[200];
        int buffersize;
        filehandle=fopen("/dev/foo","r");
        buffersize=fread(buffer,100,1,filehandle);
        printf("read: %d\n",buffersize);
        buffer[buffersize]=0;
//      printf("kernel said: %s\n", buffer);    
        fclose(filehandle);
        return(0);
}

#include <stdio.h>
#include <string.h>

int main(){
        FILE *filehandle;
        int length;
        char message[]="hello kernel!\n";
        filehandle=fopen("/dev/foo","w");
        length=fwrite(message,1, strlen(message),filehandle);
        printf("written: %d\n",length);
        fclose(filehandle);
        return(0);
}


StartSeite | MarkusRechberger/ | Neues | TestSeite | ForumSeite | Teilnehmer | Kategorien | Index | Hilfe | Einstellungen | Ändern
Text dieser Seite ändern (zuletzt geändert: 11. Oktober 2004 14:11 (diff))
Suchbegriff: gesucht wird
im Titel
im Text