Skip to content

Commit 8b02809

Browse files
committed
Import from internal tree
0 parents  commit 8b02809

File tree

7 files changed

+630
-0
lines changed

7 files changed

+630
-0
lines changed

.clang-format

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
BasedOnStyle: Chromium
2+
Language: Cpp
3+
MaxEmptyLinesToKeep: 3
4+
IndentCaseLabels: false
5+
AllowShortIfStatementsOnASingleLine: false
6+
AllowShortCaseLabelsOnASingleLine: false
7+
AllowShortLoopsOnASingleLine: false
8+
DerivePointerAlignment: false
9+
PointerAlignment: Right
10+
SpaceAfterCStyleCast: true
11+
TabWidth: 4
12+
UseTab: Never
13+
IndentWidth: 4
14+
BreakBeforeBraces: Linux
15+
AccessModifierOffset: -4
16+
ForEachMacros:
17+
- foreach
18+
- Q_FOREACH
19+
- BOOST_FOREACH
20+
- list_for_each
21+
- list_for_each_safe
22+
- list_for_each_entry
23+
- list_for_each_entry_safe
24+
- hlist_for_each_entry
25+
- rb_list_foreach
26+
- rb_list_foreach_safe
27+
- vec_foreach

.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Object files
5+
*.o
6+
*.ko
7+
*.elf
8+
9+
# Linker output
10+
*.map
11+
12+
# Precompiled Headers
13+
*.gch
14+
*.pch
15+
16+
# Libraries
17+
*.a
18+
19+
# Kernel Module Compile Results
20+
*.mod*
21+
*.cmd
22+
.tmp_versions/
23+
modules.order
24+
Module.symvers
25+
Mkfile.old
26+
dkms.conf
27+
28+
# test-oriented
29+
client
30+
out

Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
CONFIG_MODULE_SIG = n
2+
TARGET_MODULE := fibdrv
3+
4+
obj-m := $(TARGET_MODULE).o
5+
ccflags-y := -std=gnu99 -Wno-declaration-after-statement
6+
7+
KDIR := /lib/modules/$(shell uname -r)/build
8+
PWD := $(shell pwd)
9+
10+
all: client
11+
$(MAKE) -C $(KDIR) M=$(PWD) modules
12+
13+
clean:
14+
$(MAKE) -C $(KDIR) M=$(PWD) clean
15+
$(RM) client out
16+
load:
17+
sudo insmod $(TARGET_MODULE).ko
18+
unload:
19+
sudo rmmod $(TARGET_MODULE) || true >/dev/null
20+
21+
client: client.c
22+
$(CC) -o $@ $^
23+
24+
PRINTF = env printf
25+
PASS_COLOR = \e[32;01m
26+
NO_COLOR = \e[0m
27+
pass = $(PRINTF) "$(PASS_COLOR)$1 Passed [-]$(NO_COLOR)\n"
28+
29+
check: all
30+
$(MAKE) unload
31+
$(MAKE) load
32+
sudo ./client > out
33+
$(MAKE) unload
34+
@diff -u out expected.txt && $(call pass)

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# fibdrv
2+
3+
Linux kernel module that creates device /dev/fibonacci. Writing to this device
4+
should have no effect, however reading at offset k should return the kth
5+
fibonacci number.
6+
7+
## References
8+
9+
* [Writing a simple device driver](https://www.apriorit.com/dev-blog/195-simple-driver-for-linux-os)
10+
* [Character device drivers](https://linux-kernel-labs.github.io/master/labs/device_drivers.html#open-and-release)
11+
* [cdev interface](https://lwn.net/Articles/195805/)
12+
* [Character device files](https://sysplay.in/blog/linux-device-drivers/2013/06/character-device-files-creation-operations/)

client.c

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <sys/types.h>
6+
#include <unistd.h>
7+
8+
#define FIB_DEV "/dev/fibonacci"
9+
10+
int main()
11+
{
12+
int fd;
13+
long long sz;
14+
15+
char buf[1];
16+
char write_buf[] = "testing writing";
17+
int offset = 100; // TODO: test something bigger than the limit
18+
int i = 0;
19+
20+
fd = open(FIB_DEV, O_RDWR);
21+
22+
if (fd < 0) {
23+
perror("Failed to open character device");
24+
exit(1);
25+
}
26+
27+
for (i = 0; i <= offset; i++) {
28+
sz = write(fd, write_buf, strlen(write_buf));
29+
printf("Writing to " FIB_DEV ", returned the sequence %lld\n", sz);
30+
}
31+
32+
for (i = 0; i <= offset; i++) {
33+
lseek(fd, i, SEEK_SET);
34+
sz = read(fd, buf, 1);
35+
printf("Reading from " FIB_DEV
36+
" at offset %d, returned the sequence "
37+
"%lld.\n",
38+
i, sz);
39+
}
40+
41+
for (i = offset; i >= 0; i--) {
42+
lseek(fd, i, SEEK_SET);
43+
sz = read(fd, buf, 1);
44+
printf("Reading from " FIB_DEV
45+
" at offset %d, returned the sequence "
46+
"%lld.\n",
47+
i, sz);
48+
}
49+
50+
close(fd);
51+
return 0;
52+
}

0 commit comments

Comments
 (0)