# Linux Kernel Module Cheat
The perfect emulation setup to study and develop the <<linux-kernel>> v5.9.2, kernel modules, <<qemu-buildroot-setup,QEMU>>, <<gem5-buildroot-setup,gem5>> and x86_64, ARMv7 and ARMv8 <<userland-assembly,userland>> and <<baremetal-setup,baremetal>> assembly, <<c,ANSI C>>, <<cpp,C++>> and <<posix,POSIX>>. <<gdb>> and <<kgdb>> just work. Powered by <<about-the-qemu-buildroot-setup,Buildroot>> and <<about-the-baremetal-setup,crosstool-NG>>. Highly automated. Thoroughly documented. Automated <<test-this-repo,tests>>.

#### 17.8.2. seq_file

TODO these examples broke in kernel 5.0 and we didn’t notice because they just added a flimsy warning and nothing else:

- https://stackoverflow.com/questions/25399112/how-to-use-a-seq-file-in-linux-kernel-modules/45100058

- https://stackoverflow.com/questions/60570242/seq-file-not-working-properly-after-next-returns-null

Fix with ifdef for kernel 4/5. The error message is:

    seq_file: buggy .next function next [module-name] did not update position index

Writing trivial read File operations is repetitive and error prone. The `seq_file` API makes the process much easier for those trivial cases:

    ./seq_file.sh
    echo $?

Outcome: the test passes:

    0

Sources:

- kernel_modules/seq_file.c

- rootfs_overlay/lkmc/seq_file.sh

In this example we create a debugfs file that behaves just like a file that contains:

    0
    1
    2

However, we only store a single integer in memory and calculate the file on the fly in an iterator fashion.

`seq_file` does not provide `write`: https://stackoverflow.com/questions/30710517/how-to-implement-a-writable-proc-file-by-using-seq-file-in-a-driver-module

Bibliography:

- Documentation/filesystems/seq_file.txt

- https://stackoverflow.com/questions/25399112/how-to-use-a-seq-file-in-linux-modules

- seq_file single_open