echo 'main(){}' | gcc -xc - -o /dev/stdout | ???
Is there a way to run the output binary on a unix-like system?
EDIT: I needed it to run the output of g++ in a sandboxed environment where I can't write any file (nothing malicious, I promise).
Is there a way to run the output binary on a unix-like system? EDIT: I needed it to run the output of g++ in a sandboxed environment where I can't write any file (nothing malicious, I promise). |
||||
|
I don't believe this is possible. The exec(2) system call always requires a filename or absolute path (the filename is always a The closest you could do is pipe the output into a named pipe and try executing from the pipe. That may work, although the shell may refuse to execute any file that does not have the Another approach would be to write something that reads standard input, writes a file out to a temporay area, sets the --x bits on it, forks and execs then deletes the file. The inode and contents will remain until the program finishes executing but it won't be accessible through the file system. When the process terminates the inode will be released and storage will be returned to the free list. EDIT: As Mat points out, the first approach won't work as the loader will attempt to demand-page in the executable, which will generate random seek traffic on the file, and this isn't possible on a pipe. This leaves some sort of approach like the second. |
|||||||||||||
|
|
This will automatically run the compilation of your code, but creates a file (temparaily) on the filesystem in order to do it.
(I'm currently testing this now, but I'm pretty sure this, or something close to it will work for you) EDIT: If the goal of your piping is to cut physical disks out of the equation for speed, consider creating a ram disk to hold the intermediate file. |
|||||||
|
|
You could try tcc, which will compile and execute a program in one step, without writing any intermediate files. It's not gcc, which may be a problem for you, but it is spectacularly fast, so it may even bet better than gcc for your purposes. |
|||
|
|
csh. – rozcietrzewiacz Oct 7 '11 at 15:02