tags:

views:

159

answers:

3

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.

+10  A: 

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.

jldupont
thank you, looks good.
disown
+3  A: 

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

ephemient
That is a great idea. Reading up on Plan9 was really the reason why I got this idea started anyway.
disown
If you want to go the 9p route, have a look at http://kyuba.org/libduat - a library for implementing 9p filesystems.
camh
Thanks. I also just found out that debian has a 9mount package for mounting v9fs.
disown
+1  A: 

Perhaps the way to do this is just use "real" files and use a change-notification library (inotify preferred) to detect when they change and update your behaviour accordingly.

The /proc and /sys are for kernel-user communication and not really intended for userspace programs' IPC - you're expected to use named pipes, sockets, shared memory etc for that.

(ab)using FUSE is not really a nice idea in this case, I think.

MarkR
I expect a lot of interaction with other programs, and in this case it fits the bill well. I'm also curious and would like to learn how to do this.
disown
And another reason why I'd like to do this is because I'm fed up with having so many different monitoring interfaces for different programs, and I've started to take the stance that this really belongs in the FS.
disown