Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write my own 'everything is a file' interface

Tags:

linux

unix

posix

I would like to expose the settings and statistics of my program in a 'everything is a file' manner - sort of how /proc/ and /sys/ works.

As an example, imagine for a moment that apache2 had this type of interface. You would then be able to do something like this (hypothetical):

cd /apache2/virtual_hosts
mkdir 172.20.30.50
cd 172.20.30.50
echo '/www/example1' > DocumentRoot
echo 'www.example1.com' > ServerName
echo 1 > control/enabled
cat control/status
   enabled true
   uptime 4080
   hits 0

Now, are there any tutorials or similar on how to do this? I'm mainly looking for techniques for 'pretending to be a file or dir'. I'm on linux, POSIX or other more portable method would be preferable, but not required.

like image 236
Alexander Torstling Avatar asked Jan 14 '10 21:01

Alexander Torstling


People also ask

What does Everything is a file mean in Linux?

Everything is a file describes one of the defining features of Unix, and its derivatives—that a wide range of input/output resources such as documents, directories, hard-drives, modems, keyboards, printers and even some inter-process and network communications are simple streams of bytes exposed through the filesystem ...

What is file system and file type?

In a computer, a file system -- sometimes written filesystem -- is the way in which files are named and where they are placed logically for storage and retrieval. Without a file system, stored information wouldn't be isolated into individual files and would be difficult to identify and retrieve.

What are the 3 standard file descriptors?

Stdin, stdout, and stderr On a Unix-like operating system, the first three file descriptors, by default, are STDIN (standard input), STDOUT (standard output), and STDERR (standard error).

What is a file in operating system?

Files are used for all input and output (I/O) of information in the operating system, to standardize access to both software and hardware. Input occurs when the contents of a file is modified or written to. Output occurs when the contents of one file is read or transferred to another file.


2 Answers

On Linux, have a look at Fuse: implement a fully functional filesystem in a userspace program.

  • Simple library API
  • Simple installation (no need to patch or recompile the kernel)
  • Secure implementation
  • Userspace - kernel interface is very efficient
  • Usable by non privileged users
  • Runs on Linux kernels 2.4.X and 2.6.X
  • Has proven very stable over time

Look at compatible platforms here. In terms of tutorial, one of good ones I've came across is here.

like image 158
jldupont Avatar answered Nov 04 '22 15:11

jldupont


In addition to FUSE, another solution is to export a 9p filesystem. wmii does this, for example.

like image 43
ephemient Avatar answered Nov 04 '22 14:11

ephemient