Skip to content
Snippets Groups Projects
Commit 284f480b authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Material und Beispiele 30.4.2021

parent a8c15674
Branches
Tags
No related merge requests found
Showing
with 749 additions and 0 deletions
Makefile-modules-2
\ No newline at end of file
hello-3: hello-3.c
gcc -Wall -O hello-3.c -o hello-3
TARGET = hello-3
CFLAGS = -Wall -O
$(TARGET): hello-3.c
gcc $(CFLAGS) hello-3.c -o hello-3
TARGET = hello-3
CFLAGS = -Wall -O
$(TARGET): $(TARGET).c
gcc $(CFLAGS) $< -o $(TARGET)
obj-m += hellomod-1.o
# obj-m += hellomod-2.o
# obj-m += chardev-1.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
obj-m = hellomod-1.o
obj-m += hellomod-2.o
obj-m += chardev-1.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
/*
* chardev.c: Creates a read-only char device that says how many times
* you've read from the dev file
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h> /* for put_user */
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/*
* This function is called when the module is loaded
*/
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
return Major;
}
printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major);
printk(KERN_INFO "the driver, create a dev file with\n");
printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n");
printk(KERN_INFO "the device file.\n");
printk(KERN_INFO "Remove the device file and module when done.\n");
return SUCCESS;
}
/*
* This function is called when the module is unloaded
*/
void cleanup_module(void)
{
/*
* Unregister the device
*/
unregister_chrdev(Major, DEVICE_NAME);
}
/*
* Methods
*/
/*
* Called when a process tries to open the device file, like
* "cat /dev/mycharfile"
*/
static int device_open(struct inode *inode, struct file *file)
{
static int counter = 0;
if (Device_Open)
return -EBUSY;
Device_Open++;
sprintf(msg, "I already told you %d times Hello world!\n", counter++);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
/*
* Called when a process closes the device file.
*/
static int device_release(struct inode *inode, struct file *file)
{
Device_Open--; /* We're now ready for our next caller */
/*
* Decrement the usage count, or else once you opened the file, you'll
* never get get rid of the module.
*/
module_put(THIS_MODULE);
return 0;
}
/*
* Called when a process, which already opened the dev file, attempts to
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
/*
* Number of bytes actually written to the buffer
*/
int bytes_read = 0;
/*
* If we're at the end of the message,
* return 0 signifying end of file
*/
if (*msg_Ptr == 0)
return 0;
/*
* Actually put the data into the buffer
*/
while (length && *msg_Ptr) {
/*
* The buffer is in the user data segment, not the kernel
* segment so "*" assignment won't work. We have to use
* put_user which copies data from the kernel data segment to
* the user data segment.
*/
put_user(*(msg_Ptr++), buffer++);
length--;
bytes_read++;
}
/*
* Most read functions return the number of bytes put into the buffer
*/
return bytes_read;
}
/*
* Called when a process writes to dev file: echo "hi" > /dev/hello
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;
}
/*
* chardev.c: Creates a read-only char device that says how many times
* you've read from the dev file
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <asm/uaccess.h> /* for put_user */
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
struct class *dev_Class;
struct device *chr_dev;
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/*
* This function is called when the module is loaded
*/
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
return Major;
}
// Create module class
printk(KERN_INFO "Creating device class LCD...\n");
dev_Class = class_create(THIS_MODULE,DEVICE_NAME);
if( dev_Class == NULL)
{
printk( KERN_ALERT "Error! Class couldn't be created!\n" );
return 1 ;
}
printk( KERN_INFO "Class created!\n" );
// Create device in /dev/...
printk(KERN_INFO "Creating device\n");
chr_dev = device_create(dev_Class, NULL, MKDEV(Major,0), NULL, DEVICE_NAME);
printk(KERN_INFO "I was assigned major number %d. To talk to\n", Major);
printk(KERN_INFO "the driver, create a dev file with\n");
printk(KERN_INFO "'mknod /dev/%s c %d 0'.\n", DEVICE_NAME, Major);
printk(KERN_INFO "Try various minor numbers. Try to cat and echo to\n");
printk(KERN_INFO "the device file.\n");
printk(KERN_INFO "Remove the device file and module when done.\n");
return SUCCESS;
}
/*
* This function is called when the module is unloaded
*/
void cleanup_module(void)
{
/*
* Unregister the device
*/
unregister_chrdev(Major, DEVICE_NAME);
}
/*
* Methods
*/
/*
* Called when a process tries to open the device file, like
* "cat /dev/mycharfile"
*/
static int device_open(struct inode *inode, struct file *file)
{
static int counter = 0;
if (Device_Open)
return -EBUSY;
Device_Open++;
sprintf(msg, "I already told you %d times Hello world!\n", counter++);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
/*
* Called when a process closes the device file.
*/
static int device_release(struct inode *inode, struct file *file)
{
Device_Open--; /* We're now ready for our next caller */
/*
* Decrement the usage count, or else once you opened the file, you'll
* never get get rid of the module.
*/
module_put(THIS_MODULE);
return 0;
}
/*
* Called when a process, which already opened the dev file, attempts to
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
/*
* Number of bytes actually written to the buffer
*/
int bytes_read = 0;
/*
* If we're at the end of the message,
* return 0 signifying end of file
*/
if (*msg_Ptr == 0)
return 0;
/*
* Actually put the data into the buffer
*/
while (length && *msg_Ptr) {
/*
* The buffer is in the user data segment, not the kernel
* segment so "*" assignment won't work. We have to use
* put_user which copies data from the kernel data segment to
* the user data segment.
*/
put_user(*(msg_Ptr++), buffer++);
length--;
bytes_read++;
}
/*
* Most read functions return the number of bytes put into the buffer
*/
return bytes_read;
}
/*
* Called when a process writes to dev file: echo "hi" > /dev/hello
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;
}
/*
* chardev.c: Creates a read-only char device that says how many times
* you've read from the dev file
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <asm/uaccess.h> /* for put_user */
MODULE_LICENSE("GPL");
/*
* Prototypes - this would normally go in a .h file
*/
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode *, struct file *);
static int device_release(struct inode *, struct file *);
static ssize_t device_read(struct file *, char *, size_t, loff_t *);
static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
struct class *dev_Class;
struct device *chr_dev;
#define SUCCESS 0
#define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
#define BUF_LEN 80 /* Max length of the message from the device */
/*
* Global variables are declared as static, so are global within the file.
*/
static int Major; /* Major number assigned to our device driver */
static int Device_Open = 0; /* Is device open?
* Used to prevent multiple access to device */
static char msg[BUF_LEN]; /* The msg the device will give when asked */
static char *msg_Ptr;
static struct file_operations fops = {
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release
};
/*
* This function is called when the module is loaded
*/
int init_module(void)
{
Major = register_chrdev(0, DEVICE_NAME, &fops);
if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
return Major;
}
// Create module class
printk (KERN_INFO "Creating device class \"chardev\" ...\n");
dev_Class = class_create (THIS_MODULE,DEVICE_NAME);
if( dev_Class == NULL)
{
printk (KERN_ALERT "Error! Class couldn't be created!\n");
return 1;
}
printk (KERN_INFO "Class created!\n");
// Create device in /dev/...
printk (KERN_INFO "Creating device\n");
chr_dev = device_create (dev_Class, NULL, MKDEV (Major,0), NULL, DEVICE_NAME);
if (chr_dev == NULL)
{
printk( KERN_ALERT "Error! Device couldn't be created!\n" );
return 1 ;
}
return SUCCESS;
}
/*
* This function is called when the module is unloaded
*/
void cleanup_module(void)
{
printk(KERN_INFO "module chardev-4 cleanup\n");
//Unregister the device
if (chr_dev)
{
printk(KERN_INFO "Unregister device ...\n");
device_unregister(chr_dev);
printk(KERN_INFO "OK\n");
}
if (dev_Class)
{
printk(KERN_INFO "Unregister class ...\n");
class_unregister(dev_Class);
printk(KERN_INFO "OK\n");
}
printk(KERN_INFO "Unregister Chardev ...\n");
unregister_chrdev(Major, DEVICE_NAME);
printk(KERN_INFO "Device %s unregistered!\n", DEVICE_NAME);
}
/*
* Methods
*/
/*
* Called when a process tries to open the device file, like
* "cat /dev/mycharfile"
*/
static int device_open(struct inode *inode, struct file *file)
{
static int counter = 0;
if (Device_Open)
return -EBUSY;
Device_Open++;
sprintf(msg, "I already told you %d times Hello world!\n", counter++);
msg_Ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
/*
* Called when a process closes the device file.
*/
static int device_release(struct inode *inode, struct file *file)
{
Device_Open--; /* We're now ready for our next caller */
/*
* Decrement the usage count, or else once you opened the file, you'll
* never get get rid of the module.
*/
module_put(THIS_MODULE);
return 0;
}
/*
* Called when a process, which already opened the dev file, attempts to
* read from it.
*/
static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
char *buffer, /* buffer to fill with data */
size_t length, /* length of the buffer */
loff_t * offset)
{
/*
* Number of bytes actually written to the buffer
*/
int bytes_read = 0;
/*
* If we're at the end of the message,
* return 0 signifying end of file
*/
if (*msg_Ptr == 0)
return 0;
/*
* Actually put the data into the buffer
*/
while (length && *msg_Ptr) {
/*
* The buffer is in the user data segment, not the kernel
* segment so "*" assignment won't work. We have to use
* put_user which copies data from the kernel data segment to
* the user data segment.
*/
put_user(*(msg_Ptr++), buffer++);
length--;
bytes_read++;
}
/*
* Most read functions return the number of bytes put into the buffer
*/
return bytes_read;
}
/*
* Called when a process writes to dev file: echo "hi" > /dev/hello
*/
static ssize_t
device_write(struct file *filp, const char *buff, size_t len, loff_t * off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported.\n");
return -EINVAL;
}
File added
#include <stdio.h>
int main (void)
{
printf ("");
printf ("H");
printf ("e");
printf ("l");
printf ("l");
printf ("o");
return 0;
}
#include <stdio.h>
int main (void)
{
printf ("\x1b[H\x1b[J");
printf ("\x1b[00m\x1b[01;35m");
printf ("Hello, world!\n");
return 0;
}
#include <stdio.h>
int main (void)
{
printf ("Hello, world!\n");
return 0;
}
/*
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
/*
* hello-2.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
MODULE_LICENSE("GPL");
int init_module(void)
{
printk(KERN_INFO "Hello world 2.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 2.\n");
}
cassini/home/peter/bo/2021ss/bs/20210430> cat Makefile
hello-3: hello-3.c
gcc -Wall -O hello-3.c -o hello-3
cassini/home/peter/bo/2021ss/bs/20210430> cat hello-3.c
#include <stdio.h>
int main (void)
{
printf ("Hello, world!\n");
return 0;
}
cassini/home/peter/bo/2021ss/bs/20210430> make
gcc -Wall -O hello-3.c -o hello-3
cassini/home/peter/bo/2021ss/bs/20210430> ./hello-3
Hello, world!
cassini/home/peter/bo/2021ss/bs/20210430> make
make: „hello-3“ ist bereits aktuell.
cassini/home/peter/bo/2021ss/bs/20210430> touch hello-3.c
cassini/home/peter/bo/2021ss/bs/20210430> ls -lrt
insgesamt 1120
-rw-r--r-- 1 peter peter 1048576 Okt 22 2013 freedos.hd
-rw-r--r-- 1 peter peter 4069 Apr 4 2014 chardev-1.c
-rw-r--r-- 1 peter peter 390 Apr 4 2014 hellomod-1.c
-rw-r--r-- 1 peter peter 4659 Mai 5 2017 chardev-2.c
-rw-r--r-- 1 peter peter 414 Apr 26 2019 hellomod-2.c
-rw-r--r-- 1 peter peter 4997 Apr 26 2019 chardev-3.c
-rw-r--r-- 1 peter peter 230 Apr 30 12:06 SCRATCH
-rw-r--r-- 1 peter peter 210 Apr 30 12:25 nix-hello.c
-rw-r--r-- 1 peter peter 185 Apr 30 12:33 hello-1.c
-rw-r--r-- 1 peter peter 143 Apr 30 12:36 hello-2.c
-rwxr-xr-x 1 peter peter 16608 Apr 30 12:40 a.out
-rw-r--r-- 1 peter peter 208 Apr 30 12:52 Makefile-1
-rw-r--r-- 1 peter peter 54 Apr 30 12:55 Makefile
-rwxr-xr-x 1 peter peter 16608 Apr 30 12:55 hello-3
-rw-r--r-- 1 peter peter 82 Apr 30 12:56 hello-3.c
cassini/home/peter/bo/2021ss/bs/20210430> make
gcc -Wall -O hello-3.c -o hello-3
cassini/home/peter/bo/2021ss/bs/20210430> ./hello-3
Hello, world!
cassini/home/peter/bo/2021ss/bs/20210430>
TARGET = hello-3
CFLAGS = -Wall -O
$(TARGET): hello-3.c
gcc $(CFLAGS) hello-3.c -o hello-3
cassini/home/peter/bo/2021ss/bs/20210430> make
make: „hello-3“ ist bereits aktuell.
cassini/home/peter/bo/2021ss/bs/20210430> touch hello-3.c
cassini/home/peter/bo/2021ss/bs/20210430> make
gcc -Wall -O hello-3.c -o hello-3
cassini/home/peter/bo/2021ss/bs/20210430>
TARGET = hello-3
CFLAGS = -Wall -O
$(TARGET): $(TARGET).c
gcc $(CFLAGS) $< -o $(TARGET)
cassini/home/peter/bo/2021ss/bs/20210430> touch hello-3.c
cassini/home/peter/bo/2021ss/bs/20210430> make
gcc -Wall -O hello-3.c -o hello-3
cassini/home/peter/bo/2021ss/bs/20210430>
cassini/home/peter/bo/2020ws/hp/20201217> cat philosophy.c
#include <stdio.h>
#include "answer.h"
int main (void)
{
printf ("The answer is %d.\n", answer ());
return 0;
}
cassini/home/peter/bo/2020ws/hp/20201217> cat answer.h
extern int answer (void);
cassini/home/peter/bo/2020ws/hp/20201217> gcc -Wall -O philosophy.c -o philosophy
/usr/bin/ld: /tmp/ccPlT8mv.o: in function `main':
philosophy.c:(.text+0x5): undefined reference to `answer'
collect2: error: ld returned 1 exit status
cassini/home/peter/bo/2020ws/hp/20201217> cat answer.c
#include "answer.h"
int answer (void)
{
return 23;
}
cassini/home/peter/bo/2020ws/hp/20201217> gcc -Wall -O philosophy.c answer.c -o philosophy
cassini/home/peter/bo/2020ws/hp/20201217> ./philosophy
The answer is 23.
cassini/home/peter/bo/2020ws/hp/20201217>
cassini/home/peter/bo/2020ws/hp/20201217> gcc -Wall -O philosophy.c -c
cassini/home/peter/bo/2020ws/hp/20201217> ls -l philosophy*
-rwxr-xr-x 1 peter peter 16680 Apr 30 14:18 philosophy
-rw-r--r-- 1 peter peter 117 Jan 4 14:29 philosophy.c
-rw-r--r-- 1 peter peter 1632 Apr 30 14:19 philosophy.o
cassini/home/peter/bo/2020ws/hp/20201217> gcc -Wall -O answer.c -c
cassini/home/peter/bo/2020ws/hp/20201217> ls -l answer.*
-rw-r--r-- 1 peter peter 56 Jan 4 14:29 answer.c
-rw-r--r-- 1 peter peter 26 Jan 4 14:29 answer.h
-rw-r--r-- 1 peter peter 1208 Apr 30 14:20 answer.o
cassini/home/peter/bo/2020ws/hp/20201217> gcc philosophy.o answer.o -o philosophy
cassini/home/peter/bo/2020ws/hp/20201217> ./philosophy
The answer is 23.
cassini/home/peter/bo/2020ws/hp/20201217>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment