Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 01_git/scissors/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CC=gcc
CFLAGS=-I.
CRYPTOLAGS=-lcrypto
OUT_NAME=rock-paper-scissors

all: $(OUT_NAME)

$(OUT_NAME): rock_paper_scissors.o main.o
$(CC) rock_paper_scissors.o main.o -o $(OUT_NAME) $(CFLAGS) $(CRYPTOLAGS)

main.o: main.c
$(CC) -c main.c $(CFLAGS)

rock_paper_scissors.o: rock_paper_scissors.c
$(CC) -c rock_paper_scissors.c $(CFLAGS)

clean:
/bin/rm -rf *.o a.out $(OUT_NAME)
7 changes: 7 additions & 0 deletions 01_git/scissors/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <rock_paper_scissors.h>

int main()
{
RPS_run_game();
return 0;
}
161 changes: 161 additions & 0 deletions 01_git/scissors/rock_paper_scissors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <openssl/md5.h>

#include "rock_paper_scissors.h"

static RPS_CHOICES_E get_human_choice(void);
static RPS_CHOICES_E get_random_choice(void);
static void RPS_show_game_result(RPS_CHOICES_E comp_choice, RPS_CHOICES_E human_choice);
static void RPS_print_md5(char value);

static RPS_CHOICE_ENTRY_S choices_arr[] = {{RPS_CHOICE_ROCK, "rock", 'r'},
{RPS_CHOICE_PAPER, "paper", 'p'},
{RPS_CHOICE_SCISSORS, "scissors", 's'},
{RPS_CHOICE_UNKNOWN, "unknown", 'u'}};

static RPS_CHOICES_E RPS_get_random_choice(void)
{
unsigned char buff;
RPS_CHOICES_E choice = RPS_CHOICE_UNKNOWN;

int fd = open("/dev/urandom", O_RDONLY);

if (!fd)
{
printf("Error opening /dev/urandom\n");
exit(1);
}

read(fd, &buff, sizeof buff);
close(fd);
choice = buff % RPS_N_VALUABLE_CHOICES;

return choice;
}


static RPS_CHOICES_E RPS_get_human_choice(void)
{
RPS_CHOICES_E choice = RPS_CHOICE_UNKNOWN;
char read_value = '?';

printf("Please choose: rock (r) - paper (p) - scissors (s)\n");
scanf(" %c", &read_value);

switch (read_value)
{
case 'r':
{
choice = RPS_CHOICE_ROCK;
break;
}

case 'p':
{
choice = RPS_CHOICE_PAPER;
break;
}
case 's':
{
choice = RPS_CHOICE_SCISSORS;
break;
}
default:
{
choice = RPS_CHOICE_UNKNOWN;
break;
}
}

return choice;
}


static void RPS_print_md5(char value)
{
MD5_CTX ctx;
unsigned char md5digest[MD5_DIGEST_LENGTH];

MD5_Init(&ctx);
MD5_Update(&ctx, (const void *)&value, sizeof value);
MD5_Final(md5digest, &ctx);

printf("Random choice: %c md5: ", value);

for ( int i=0;i < MD5_DIGEST_LENGTH; i++ ) {
printf("%02x", md5digest[i]);
};
printf("\n");
}


static void RPS_show_game_result(RPS_CHOICES_E comp_choice, RPS_CHOICES_E human_choice)
{
RPS_CHOICE_ENTRY_S *p_human_entry;
RPS_CHOICE_ENTRY_S *p_comp_entry;
int game_result;

p_comp_entry = &choices_arr[comp_choice];
p_human_entry = &choices_arr[human_choice];

printf("You choose %s, I choose %s\n", p_human_entry->long_name, p_comp_entry->long_name);

game_result = comp_choice - human_choice;

switch (game_result)
{
case 0:
{
printf("Draw: %s and %s\n", p_human_entry->long_name, p_comp_entry->long_name);
break;
}

case 1:
{
printf("I win: %s beats %s\n", p_comp_entry->long_name, p_human_entry->long_name);
break;
}
case 2:
{
printf("You win: %s beats %s\n", p_human_entry->long_name, p_comp_entry->long_name);
break;
}
case -1:
{
printf("You win: %s beats %s\n", p_human_entry->long_name, p_comp_entry->long_name);
break;
}
case -2:
{
printf("I win: %s beats %s\n", p_comp_entry->long_name, p_human_entry->long_name);
break;
}
default:
{
printf("Error game result: %i\n", game_result);
exit(1);
break;
}
}
}


void RPS_run_game(void)
{
RPS_CHOICES_E human_choice = RPS_CHOICE_UNKNOWN;
RPS_CHOICES_E comp_choice = RPS_CHOICE_UNKNOWN;

comp_choice = RPS_get_random_choice();
// Optional task: add printing md5 checksum of computer's choice before human's move.
RPS_print_md5(choices_arr[comp_choice].short_name);

for ( ; human_choice == RPS_CHOICE_UNKNOWN; )
{
human_choice = RPS_get_human_choice();
}

RPS_show_game_result(comp_choice, human_choice);
}
23 changes: 23 additions & 0 deletions 01_git/scissors/rock_paper_scissors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __ROCK_PAPER_SCISSORS_H_
#define __ROCK_PAPER_SCISSORS_H_

#define RPS_N_VALUABLE_CHOICES 3

typedef enum RPS_CHOICES {
RPS_CHOICE_ROCK,
RPS_CHOICE_PAPER,
RPS_CHOICE_SCISSORS,
RPS_CHOICE_UNKNOWN
} RPS_CHOICES_E;


typedef struct RPS_CHOICE_ENTRY {
RPS_CHOICES_E id;
char long_name[16];
char short_name;
} RPS_CHOICE_ENTRY_S;


void RPS_run_game(void);

#endif // __ROCK_PAPER_SCISSORS_H_
19 changes: 19 additions & 0 deletions 02_bash/hwdetect/hwdetect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

FOLDER=${1:-/dev}
echo "Polling folder: $FOLDER"

PREV=$(stat --terse --format="%n %z" ${FOLDER}/*|sort -n)

while true
do
NEW=$(stat --terse --format="%n %z" ${FOLDER}/*|sort -n)
DELTA=$(diff <(echo "$PREV") <(echo "$NEW") | grep '^>')

if [[ "${#DELTA}" != 0 ]]
then
echo "$DELTA"
fi
PREV=$NEW
sleep 1
done
9 changes: 9 additions & 0 deletions 03_module/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
KERNELDIR ?= ../output/build/linux-5.10.7

obj-m := hello.o

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
15 changes: 15 additions & 0 deletions 03_module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## module home work: create a simple loadable module with parameters

Create a loadadle kernel module which should accept two integer parameters and provide:
- A sum of parameters upon driver load
- A substration of parameters upon driver unload

Info about module parameters can be found at: https://devarea.com/linux-kernel-development-kernel-module-parameters/#.YZfWcpFByV4

Task should be performed using buildroot+qemu approach

The task results should contain:
- The module code
- The Makefile
- Dump of the kernel logs from the target system

11 changes: 11 additions & 0 deletions 03_module/dump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# insmod /tmp/hello.ko n1=42 n2=-10
hello add n1: 42 and n2: -10 result: 32
# cd /sys/module/hello/parameters/
# ls
n1 n2
# cat n1
42
# cat n2
-10
# rmmod hello.ko
Bye substract n1: 42 and n2: -10 result: 52
26 changes: 26 additions & 0 deletions 03_module/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>


static int n1=0;
module_param(n1, int, 0660);

static int n2=0;
module_param(n2, int, 0660);

int init_module(void)
{
printk(KERN_INFO "hello add n1: %d and n2: %d result: %d\n", n1, n2, n1+n2);
return 0;
}

void cleanup_module(void)
{
printk(KERN_INFO "Bye substract n1: %d and n2: %d result: %d\n", n1, n2, n1-n2);
}

MODULE_AUTHOR("Ivan Stepanenko <istepanenko@gmail.com>");
MODULE_DESCRIPTION("Hello world module");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
9 changes: 9 additions & 0 deletions 04_basic_struct/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
KERNELDIR ?= ../output/build/linux-5.10.7

obj-m := wimbledon.o

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
7 changes: 7 additions & 0 deletions 04_basic_struct/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Basic structure homework
Implement object with name “MyObject” which is parent of kernel_kobj.
Object should include linked_list structure.
This object should contain sysfs attribute with name “list”.
On read form attribute “list” it should show content of the objects linked list.
On write to attribute “list” it should add new string to the objects linked list.
!! Do not forget properly free all the resources during rmmod.
37 changes: 37 additions & 0 deletions 04_basic_struct/dump.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# insmod /tmp/wimbledon.ko
wimbledon: loading out-of-tree module taints kernel.
# echo "1" > /sys/kernel/wimbledon/list
wimbledon: adding msg size: 2 to the list: '1
'
# echo -n "2" > /sys/kernel/wimbledon/list
wimbledon: adding msg size: 1 to the list: '2'
# echo -n "33" > /sys/kernel/wimbledon/list
wimbledon: adding msg size: 2 to the list: '33'
# echo "44" > /sys/kernel/wimbledon/list
wimbledon: adding msg size: 3 to the list: '44
'
# echo -n "55" > /sys/kernel/wimbledon/list
wimbledon: adding msg size: 2 to the list: '55'
# cat /sys/kernel/wimbledon/list
wimbledon: msg #0 size: 2 '1
'
wimbledon: msg #1 size: 1 '2'
wimbledon: msg #2 size: 2 '33'
wimbledon: msg #3 size: 3 '44
'
wimbledon: msg #4 size: 2 '55'
1
23344
55#

# rmmod wimbledon.ko
wimbledon: freeing msg size: 2 from the list: '55'
wimbledon: freeing msg size: 3 from the list: '44
'
wimbledon: freeing msg size: 2 from the list: '33'
wimbledon: freeing msg size: 1 from the list: '2'
wimbledon: freeing msg size: 2 from the list: '1
'
wimbledon: list is empty: 1
wimbledon: Module exited

Loading