views:

388

answers:

4

Is it possible to make an executable look like a read-only file on Linux, such that opening the "file" for reading actually executes the file and makes its stdout available for reading as if it were data in the "file"? It should be openable by any program that knows how to open a file for reading, for example 'cat'.

+6  A: 

Look at popen. Basically what you are describing is a pipe.

P.S. If you need language specific help, edit the question and add the language/environment you're working in and I'll try to provide more specifics.

MarkusQ
Maybe even more like the /proc "filesystem"...
dmckee
Huh? He's wanting the output of the executable. How would '/proc "filesystem"' help?
MarkusQ
He says 'such that opening the "file" for reading actually executes the file and 'makes its stdout available for reading', which isn't quite a pipe and isn't quite like /proc (because that runs in kernel space), but...
dmckee
You're right, dmckee, I'm not looking for a pipe. I want something that can be opened by any arbitrary program that knows how to open a file for reading. I should have been more clear in my question.
kbluck
kbluck -- a named pipe can be opened from any arbitrary program that can knows how to open a file for reading.
MarkusQ
True enough, but I was responding to your original suggestion of popen. A named pipe still doesn't meet the other part of my requirement in that opening the read side of the pipe does not automatically execute the process and connect it to the write side of the pipe.
kbluck
+4  A: 

Use FUSE

vartec
Thanks, exactly the pointer I needed.
kbluck
I think the only way to start the program every time the file is opened is FUSE.
Doub
+2  A: 

On unix-like OS's you can send the output of a program to a named pipe that is opened by another program. Look at the mkfifo command to create the named pipe. The named pipe works a lot like a file, with some limitations. For example, it is not seekable.

karunski
A: 

Seems like you could pipe the output of the program into whatever you are using to "read". The problem is if you want to open the executable in say emacs or vim or whatever, it's not a matter of the executable so much as the editor doesn't know any other way to interpret it.

ras2124