Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we block write function in linux?

Tags:

c

linux

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.

like image 850
Sharon Deborah Avatar asked Apr 10 '26 18:04

Sharon Deborah


1 Answers

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

like image 181
KamilCuk Avatar answered Apr 13 '26 11:04

KamilCuk