How can I make a special file that executes code when read from

Most files on a Linux system are normal files, i.e. they are saved on disk and reading from them just reads from a specified chunk of memory on the disk. How can I make something that behaves like a file in terms of being able to read from it as one would a normal file, but is actually returning programmatically generated data instead? As a concrete example, a file that downloads the current google.com and returns it, such that cat ~/myspecialfile would output the contents of google.com to stdout?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

As the other answers have indicated, you can do part of what you’ve asked for using named pipes. For a complete solution, you’d have to develop some kind of virtual filesystem that took the desired actions when a path on the virtual filesystem was accessed. There are a few approaches to doing this:

  • Write a kernel-mode filesystem driver, like the procfs driver.
  • Write a user-mode filesystem implementation, using FUSE for example.
  • Write a program which provides an NFS server interface or another network filesystem protocol.
  • Maybe a program that pretends to be a USB file-storage device or another piece of hardware.

Method 2

You can conceivably do something like this with a FIFO/named pipe:

$ mkfifo ~/myspecialfile
$ wget -q -O ~/myspecialfile google.com &
[2] 26186
$

mkfifo creates a named pipe called ~/myspecialfile. wget then directs its output to that named pipe. You can read (once) from that named pipe as if it were a regular file. e.g. to get wc counts for that file:

$ wc ~/myspecialfile
      7     430   17738 /home/ubuntu/myspecialfile
[2]+  Done                    wget -q -O ~/myspecialfile google.com
$

Note that typically the writer (wget in this instance) will open() the pipe with the O_WRONLY flag. This open() call will typically block until the reader (wc in this instance) opens the other end of the pipe with the O_RDONLY flag. So here the wget process will be started immediately, but it will be doing nothing in the background until the reader (opens and) starts to read. I have verified this behaviour with wireshark on Ubuntu 14.04.

Method 3

You can make myspecialfile a named pipe. Then you can have a script running in the background that’s in a loop writing the output of the program to the pipe:

#!/bin/bash
mkfifo ~/myspecialfile
while :; do
    run program here > ~/myspecialfile
done

There’s a limitation to this. If two processes both open the file at the same time, they might each get only parts of the output.

A more elaborate way to do something that doesn’t have this limitation would be to implement a FUSE filesystem. ~/myfyspecialfile would be the mount point of the filesystem.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x