How to execute a program from a file descriptor?

I need to execute a file when I only know the handle. It is also possible that there are no file links, so specifying a name is somehow not an option. All functions execve (), execvp (), etc. Accept the file name. dlopen () also takes a name.

Horrible decisions (like reading a file and calling some function pointer) are fine.

+6
c file-descriptor exec
source share
2 answers

Use fexecve .

PS: reading a file and calling some function pointer is definitely not in order. :)

+7
source share

Interesting. I think that it is best to use FD to write a temporary file and then execute it using a regular exec call.

You can use mkstemp to create a guaranteed unique file name. Then read the contents from the file descriptor and upload it to the temp file. Then use the name given to you by mkstemp in the exec call.

If for some reason you do not want to write a new file, I think that your only other option is to manually analyze the image of the exe file, load it into memory, and then call its main () function. This duplicates the many features that already exist in the OS, and I don’t think you want to do this. It will be difficult to do it right and it seems not worth the effort.

+1
source share

All Articles