Skip to content
/ leftpadm Public

/dev/leftpad - Linux kernel module for left-padding strings. In Rust.

License

Notifications You must be signed in to change notification settings

boldx/leftpadm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

/dev/leftpad

Linux kernel module for left-padding strings. In Rust.

Motivation

I've been contemplating making something in Rust for a while, and now that Rust in The Kernel is all the rage FOMO kicked in I guess?
So this is my Rust hello world.

Build & load

On Ubuntu 20.04:

sudo apt install build-essential llvm clang-11 linux-headers-"$(uname -r)" curl
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup default nightly
rustup component add --toolchain=nightly rust-src rustfmt

cd leftpad
make CC=clang-11
make load

Usage

Write the target len, the padding char and the string-to-be-padded, separated by spaces into the leftpad file.
Read the padded string back from the file.

echo 3 0 7  > /dev/leftpad
cat /dev/leftpad

Mind the newline...

echo -n 3 0 7  > /dev/leftpad
cat /dev/leftpad; echo

Akchually, the padding char can be a string.

echo 16 na ' batman!' > /dev/leftpad
cat /dev/leftpad

It supports UTF-8 at a certain extent: the target length is measured in Unicode Scalar Value units, so it might not does exactly what you expect, but it works if you dont push it too hard.

echo 16 💩 psicle > /dev/leftpad
cat /dev/leftpad

The general intent was to mimic the operation of JavaScrip padStart()

FQA

Q: But https://github.com/Rust-for-Linux/linux?
A: Yes!

Q: But the write ignores the offset!
A: Yes!

Q: But it's racy!
A: Yes!

Q: You should have used sysctl!
A: LOL!

References

https://lkml.org/lkml/2016/3/31/1176
https://github.com/fishinabarrel/linux-kernel-module-rust
jbaublitz/knock-out#9
jbaublitz/knock-out#11
lizhuohua/linux-kernel-module-rust#1

About

/dev/leftpad - Linux kernel module for left-padding strings. In Rust.

Topics

Resources

License

Stars

Watchers

Forks