I'm trying to block write() function in linux in my code. What can we done to block the write() function in such a way it is blocked and returns -1?
I'm trying to fail write() function, to hit an error to check the code, I've written.
Should I block or unblock I/O to fail the write() function in linux? write() function that handles file descriptor.
What can we done to block the write() function in such a way it is blocked and returns -1?
Options:
Just write your own write() function that returns -1.
ssize_t write(int fd, const void *buf, size_t s) { return -1; }
A bit of trivia: glibc and musl export weak symbols for most C library functions, and your code will be linked first before C standard library. This all makes the linker prefer the function that you provide over C standard library implementation.
Use -Wl,--wrap=write when linking and provide __wrap_write implementation.
ptrace the program and modify return value of write
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With