Warm tip: This article is reproduced from serverfault.com, please click

using a shared library : custom myfopen(), myfrwrite()

发布于 2020-11-28 08:03:34

Hello I have created a shared library named logger.so. This library is my custom fopen() and fwrite(). It collects some data and produce a file with these data.

Now I am writing a bash script and I want to use this library as well. Producing a txt file I would be able to see the extra file that produce my custom fopen(). So when I use the command fopen() in a c file this extra file is produced.

My Question is which commands are using fopen() and fwrite() functions in bash?

I have already preloaded my shared library but It doesn't work. Maybe these commands don't use fopen(),fwrite()

export LD_PRELOAD=./logger.so 

read -p 'Enter the number of your files to create: [ENTER]: ' file_number


for ((i=1; i<=file_number; i++))
do
     echo file_"$i" > "file_${i}"
done
Questioner
Ioannis Michalakis
Viewed
0
eewanco 2020-11-29 01:05:57

This may require some trial and error. I see two ways to do this. One is to run potential commands that deal with files in bash and see if when traced they call fopen:

strace bash -c "read a < /dev/null"`

or

strace bash -c "read a < /dev/null"` 2&>1 | fgrep fopen

This shows that that read uses open, not fopen.

Another way is to grep through the source code of bash as @oguz suggested. When I did this I found several places where fopen is called, but I did not investigate further:

curl https://mirrors.tripadvisor.com/gnu/bash/bash-5.1-rc3.tar.gz|tar -z -x --to-stdout --wildcards \*.c | fgrep fopen

You'll want to unarchive the whole package and search through the .c files one by one, e.g.:

curl https://mirrors.tripadvisor.com/gnu/bash/bash-5.1-rc3.tar.gz|tar -z -v -x --wildcards \*.c

You can also FTP the file or save it via a browser and do the tar standalone if you don't have curl (wget will also work).

Hopefully you can trace the relevant commands but it might not be easy.