1

Is it possible to create a virtual file in unix, whose contents are determined programmatically when the file is accessed, a bit like the files in /proc?

For example, I have a program that retrieves a particular setting by reading/catting a file. However, rather than store that setting directly in a plain text file, I want to be able to retrieve that setting from a database in the background and then pass that information to the program when it reads this virtual file. Is it possible to do so?

Amr Bekhit
  • 643
  • 2
  • 10
  • 17
  • I it is possible. you should have another application that will provide this functionality for you by creating a pipe (or named pipe). That application should be able to query data from database and send it to that pipe where on the other side your application will be able to read that data. – Scantlight Apr 12 '16 at 15:18
  • Or, the `LD_PRELOAD` trick, but try the other methods first! – thrig Apr 12 '16 at 16:06
  • See also: [File contents created when opened](http://unix.stackexchange.com/q/222665) – Stéphane Chazelas Apr 12 '16 at 16:36
  • And: [Have a 'file' generate its content on demand?](http://unix.stackexchange.com/q/273812) – Stéphane Chazelas Apr 12 '16 at 16:36

1 Answers1

3

You could look at Named Pipes.

man fifo for a starting point.

Essentially you create a named pipe, one process (or more) reads from it and another can write to it.

EightBitTony
  • 20,963
  • 4
  • 61
  • 62
  • In addition to named pipes, you could also consider unix domain sockets. – Andy Dalton Apr 12 '16 at 14:57
  • Named pipes are problematic when you have multiple simultaneous readers; you'll frequently get a reader that gets part of the data. Sockets are a problem when you have no control over the reader; you can't open a socket the same way you can a file or pipe. – Chris Cogdon Dec 05 '17 at 17:26