Take the 2-minute tour ×

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

Ugly solutions (like reading the file and calling some function pointer) are OK.

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Use fexecve.

PS: reading the file and calling some function pointer is definitely not OK. :)

share|improve this answer
I meant it's OK if there are no simple solutions. I won't do that if I can just call fexecve(). Thank you. –  stribika May 23 '10 at 16:42
If fexecve weren't supported you could do some horrible stuff with mmap and libelf -- sort of making a fdlopen. That would suck, though. I'm glad to lear about fexecve, though. I've wondered about it, but never seen it before, and never HAD to have it. –  nategoose May 23 '10 at 20:21
add comment

Interesting. I think your best bet is going to be to use the FD that you have to write a temporary file and then exec it using a normal exec call.

You can use mkstemp to make a guaranteed unique file name. Then read the content from your file descriptor and dump it to the temp file. Then use the name given to you by mkstemp in an exec call.

If you don't for some reason want to write a new file then I think your only other option will be to manually parse the exe file image, load it properly in memory, and then call it's main() function. That's duplicating a lot of functionality that already exists in the OS, and I don't think you want to do it. It will be hard to get right, and does not seem to be worth the effort.

share|improve this answer
add comment

Your Answer


