Vortrag:Howto Linux-Kernel

Aus k4cg.org
Version vom 17. Mai 2015, 08:10 Uhr von Gpunktschmitz (Diskussion | Beiträge) (Gpunktschmitz verschob die Seite Vorträge:Howto Linux-Kernel nach Vortrag:Howto Linux-Kernel)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

HOWTO Linux Kernel

Meta

Datum: 20.11.08 20:00 Uhr Referent: Georg / gnu

Code

Makefile

obj-m += minDev.o
obj-m += halloKernel.o
obj-m += bad.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

bad.c

#include <linux/module.h>
#include <linux/mm.h>

int init_module ( void ) 
{ 
        int* badarray, *funcptr;
        int i;
        printk ("Bad Kernel Module\n");
        badarray = (int*) kmalloc(100, GFP_KERNEL);
        if(!badarray) {
                printk(KERN_WARNING "no more memory :(\n");
                return 0;
        }

        funcptr = (int*) &do_gettimeofday; 
        printk("0x%08x do_gettimeofday\n", (int)funcptr);

        /* 
        badarray = funcptr
        for(i=0; i<500; ++i) badarray[i] = 0xfea1dead;
        */
        kfree(badarray);
        return 0;
}

void cleanup_module( void )
{ 
        printk ("Goodbye Kernel.\n");
}

MODULE_AUTHOR("gnu");
MODULE_LICENSE("GPL");

halloKernel.c

#include <linux/module.h>

int init_module ( void ) 
{ 
        printk ("Hello Kernel!\n");
        return 0;
}

void cleanup_module( void )
{ 
        printk ("Goodbye Kernel.\n");
}

MODULE_AUTHOR("gun");
MODULE_LICENSE("GPL");

minDev.c

#include <linux/module.h>
#include <linux/fs.h>

static int device_open(struct inode *inp, struct file *filp)
{
        return 0;
}

static ssize_t device_read(struct file *filp, char *buf, size_t length, loff_t *f_pos)
{
        return 0;
}

static ssize_t device_write(struct file *filp, const char *buf, size_t length, loff_t *f_pos)
{
        printk( "received: %c%c\n",buf[0], buf[1]);
        return 2; 
}

static int device_release(struct inode *inp, struct file *filp)
{
        return 0;
}

static struct file_operations fops = {
        open: device_open,
        read: device_read,
        write: device_write,
        release: device_release
};

//////////////////////////////////////////////////////////////
static int MajorNumber = 0;

int init_module ( void ) 
{ 
        printk ("mindev loaded!\n");

        MajorNumber = register_chrdev(0, "mydevice", &fops);
        if( MajorNumber < 0 )
        {
                printk ("Registering mydevice failed.\n");
                return MajorNumber;
        }
        else
        {
                printk ("create a device: 'mknod /dev/mydevice c %d 0'.\n",MajorNumber);
        }
        return 0;
}

void cleanup_module( void )
{ 
        unregister_chrdev(MajorNumber, "mydevice");
        printk ("mindev unloaded.\n");
}

MODULE_AUTHOR("gnu");
MODULE_LICENSE("GPL");

Links zum Workshop