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

Beispiele und Screenshots 4.4.2022

parent cedd34ee
No related branches found
No related tags found
No related merge requests found
Showing
with 451 additions and 0 deletions
../common/Operating_system_placement-de.pdf
\ No newline at end of file
Makefile-modules-1
\ No newline at end of file
philosophy: philosophy.o answer.o
gcc philosophy.o answer.o -o philosophy
answer.o: answer.c answer.h
gcc -Wall -O answer.c -c
philosophy.o: philosophy.c answer.h
gcc -Wall -O philosophy.c -c
TARGET = philosophy
OBJECTS = philosophy.o answer.o
HEADERS = answer.h
CFLAGS = -Wall -O
$(TARGET): $(OBJECTS)
gcc $(OBJECTS) -o $(TARGET)
answer.o: answer.c $(HEADERS)
gcc $(CFLAGS) answer.c -c
philosophy.o: philosophy.c $(HEADERS)
gcc $(CFLAGS) philosophy.c -c
clean:
rm -f $(OBJECTS) $(TARGET)
TARGET = philosophy
OBJECTS = philosophy.o answer.o
HEADERS = answer.h
CFLAGS = -Wall -O
$(TARGET): $(OBJECTS)
gcc $(OBJECTS) -o $(TARGET)
%.o: %.c $(HEADERS)
gcc $(CFLAGS) $< -c
clean:
rm -f $(OBJECTS) $(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
#include "answer.h"
int answer (void)
{
return ANSWER;
}
extern int answer (void);
#define ANSWER 42
/*
* 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;
}
File added
/*
* 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/2022ss/bs/20220404> 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/2022ss/bs/20220404> cat answer.h
extern int answer (void);
#define ANSWER 42
cassini/home/peter/bo/2022ss/bs/20220404> cat answer.c
#include "answer.h"
int answer (void)
{
return ANSWER;
}
cassini/home/peter/bo/2022ss/bs/20220404> gcc -Wall -O philosophy.c -o philosophy
/usr/bin/ld: /tmp/ccjJ8ZhE.o: in function `main':
philosophy.c:(.text+0x5): undefined reference to `answer'
collect2: error: ld returned 1 exit status
cassini/home/peter/bo/2022ss/bs/20220404> gcc -Wall -O philosophy.c answer.c -o philosophy
Sie haben neue Post in /var/mail/peter.
cassini/home/peter/bo/2022ss/bs/20220404> ./philosophy
The answer is 42.
cassini/home/peter/bo/2022ss/bs/20220404> gcc -Wall -O philosophy.c -c
cassini/home/peter/bo/2022ss/bs/20220404> gcc -Wall -O answer.c -c
cassini/home/peter/bo/2022ss/bs/20220404> ls -l philosophy.[co] answer.[co]
-rw-r--r-- 1 peter peter 60 Dez 13 13:46 answer.c
-rw-r--r-- 1 peter peter 1208 Apr 4 13:57 answer.o
-rw-r--r-- 1 peter peter 117 Jan 4 2021 philosophy.c
-rw-r--r-- 1 peter peter 1632 Apr 4 13:57 philosophy.o
cassini/home/peter/bo/2022ss/bs/20220404> gcc philosophy.o answer.o -o philosophy
cassini/home/peter/bo/2022ss/bs/20220404> ./philosophy
The answer is 42.
cassini/home/peter/bo/2022ss/bs/20220404> gcc philosophy.o -o philosophy
/usr/bin/ld: philosophy.o: in function `main':
philosophy.c:(.text+0x5): undefined reference to `answer'
collect2: error: ld returned 1 exit status
cassini/home/peter/bo/2022ss/bs/20220404> cat Makefile-1
philosophy: philosophy.o answer.o
gcc philosophy.o answer.o -o philosophy
answer.o: answer.c answer.h
gcc -Wall -O answer.c -c
philosophy.o: philosophy.c answer.h
gcc -Wall -O philosophy.c -c
cassini/home/peter/bo/2022ss/bs/20220404> make
make: *** Es wurden keine Ziele angegeben und keine „make“-Steuerdatei gefunden. Schluss.
cassini/home/peter/bo/2022ss/bs/20220404> make -f Makefile-1
gcc philosophy.o answer.o -o philosophy
cassini/home/peter/bo/2022ss/bs/20220404> rm *.o
cassini/home/peter/bo/2022ss/bs/20220404> make -f Makefile-1
gcc -Wall -O philosophy.c -c
gcc -Wall -O answer.c -c
gcc philosophy.o answer.o -o philosophy
/*
* 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");
}
cassini/home/peter/bo/2022ss/bs/20220404> cat Makefile-modules-1
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
cassini/home/peter/bo/2022ss/bs/20220404> ln -sf Makefile-modules-1 Makefile
cassini/home/peter/bo/2022ss/bs/20220404> echo /lib/modules/$(shell uname -r)/build
bash: shell: Kommando nicht gefunden.
/lib/modules//build
cassini/home/peter/bo/2022ss/bs/20220404> echo /lib/modules/$(uname -r)/build
/lib/modules/4.19.0-12-amd64/build
cassini/home/peter/bo/2022ss/bs/20220404> ls /lib/modules/4.19.0-12-amd64/build
arch include Makefile Module.symvers scripts tools
cassini/home/peter/bo/2022ss/bs/20220404> uname
Linux
cassini/home/peter/bo/2022ss/bs/20220404> uname -r
4.19.0-12-amd64
cassini/home/peter/bo/2022ss/bs/20220404> pwd
/home/peter/bo/2022ss/bs/20220404
-rw-r--r-- 1 peter peter 270648 Apr 4 14:19 hellomod-1.ko
cassini/home/peter/bo/2022ss/bs/20220404> ./hellomod-1.ko
bash: ./hellomod-1.ko: Keine Berechtigung
cassini/home/peter/bo/2022ss/bs/20220404> chmod +x hellomod-1.ko
cassini/home/peter/bo/2022ss/bs/20220404> ./hellomod-1.ko
bash: ./hellomod-1.ko: Kann die Binärdatei nicht ausführen: Fehler im Format der Programmdatei
cassini/home/peter/bo/2022ss/bs/20220404> chmod -x hellomod-1.ko
cassini/home/peter/bo/2022ss/bs/20220404> insmod hellomod-1.ko
bash: insmod: Kommando nicht gefunden.
cassini/home/peter/bo/2022ss/bs/20220404> sudo insmod hellomod-1.ko
[sudo] Passwort für peter:
cassini/home/peter/bo/2022ss/bs/20220404> sudo rmmod hellomod-1.ko
cassini/home/peter/bo/2022ss/bs/20220404> cat hellomod-1.c
/*
* 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");
}
cassini/home/peter/bo/2022ss/bs/20220404> sudo dmesg
[...]
[1583119.050593] hellomod_1: loading out-of-tree module taints kernel.
[1583119.050600] hellomod_1: module license 'unspecified' taints kernel.
[1583119.050601] Disabling lock debugging due to kernel taint
[1583119.050666] hellomod_1: module verification failed: signature and/or required key missing - tainting kernel
[1583119.051455] Hello world 1.
[1583136.476867] Goodbye world 1.
cassini/home/peter/bo/2022ss/bs/20220404> sudo dmesg
[...]
[1583551.529893] Hello world 2.
[1583557.018894] Goodbye world 2.
[1584035.731938] I was assigned major number 241. To talk to
[1584035.731939] the driver, create a dev file with
[1584035.731940] 'mknod /dev/chardev c 241 0'.
[1584035.731940] Try various minor numbers. Try to cat and echo to
[1584035.731941] the device file.
[1584035.731941] Remove the device file and module when done.
cassini/home/peter/bo/2022ss/bs/20220404> mkdir dev
cassini/home/peter/bo/2022ss/bs/20220404> mknod dev/chardev c 241 0
mknod: dev/chardev: Die Operation ist nicht erlaubt
cassini/home/peter/bo/2022ss/bs/20220404> sudo mknod dev/chardev c 241 0
cassini/home/peter/bo/2022ss/bs/20220404> ls -l dev/
insgesamt 0
crw-r--r-- 1 root root 241, 0 Apr 4 14:39 chardev
cassini/home/peter/bo/2022ss/bs/20220404> ls -l /dev/tty
crw-rw-rw- 1 root tty 5, 0 Apr 4 14:22 /dev/tty
cassini/home/peter/bo/2022ss/bs/20220404> cat dev/chardev
I already told you 0 times Hello world!
cassini/home/peter/bo/2022ss/bs/20220404> cat dev/chardev
I already told you 1 times Hello world!
cassini/home/peter/bo/2022ss/bs/20220404> cat dev/chardev
I already told you 2 times Hello world!
int main (void)
{
char *p = (char *) 0xb8000000;
*p++ = 'H';
*p++ = 0x04;
*p++ = 'e';
*p++ = 0x02;
*p++ = 'l';
*p++ = 0x01;
*p++ = 'l';
*p++ = 0x06;
*p++ = 'o';
*p++ = 0x03;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment