I would like to expose the settings and statistics of my program in a 'everything is a file' manner - sort of how /pr开发者_如何学Gooc/ 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.
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.
In addition to FUSE, another solution is to export a 9p filesystem. wmii does this, for example.
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.
精彩评论