1

Kernel space device drivers usually implement directories and file that show through /sys or /proc. Can the long running user space programs do this as well?

I have a daemon or long running program that needs to be able to be queried for some data and have some data set by external programs while it runs.
I could do a full blown sockets interface, but that's a lot of overhead for the program and the external requestors.
As the linux kernel developers found, using the "everything is a file" model was useful for tweaking kernel setting. I'd like to do the same.

Some may think the /sys directory is the sacred space of the kernel, but I don't see an important line between what is what is the "system" and some other services/servers/applications.

Using FUSE... I've decided to use FUSE, the 'File system in USErspace' package libfuse3.so. (After writing a wrapper for it...) I can define an array of structs, one per access variable/file:

struct fileObj files[] = {
  {"mode", mode, getFunc, putFunc},
  {"numbProcs", numbProcs, getFunc, putFunc},
  {"svrHostPort", hostPort, getFunc, putFunc},
  {"somethingWO", jakeBuf, NULL, putFunc},  // Write only file (why?)
  {"timestamp", NULL, getTimestampFunc, NULL},  // Returns timestamp, R/O
  {0}
};

The mountpoint for the FUSE filesystem is '/ssm/fuse'... The 'ls -l' shows that each entry in the 'files' array shows up as a file, some R/O, some R/W, one W/O. The 'getTimestampFunc in the 'get' function position shows that a special function can be associated with a file to perform calculate repsonses.

ribo@box:~/c$ ls -l /ssm/fuse
total 0
-rw-r--r-- 1 ribo ribo 10 Dec 28 17:17 mode
-rw-r--r-- 1 ribo ribo  1 Dec 28 17:17 numbProcs
--w------- 1 ribo ribo  3 Dec 28 17:17 somethingWO
-rw-r--r-- 1 ribo ribo  5 Dec 28 17:17 svrHostPort
-r--r--r-- 1 ribo ribo 32 Dec 28 17:17 timestamp
ribo@box:~/c$ cat /ssm/fuse/timestamp
18/12/28 17:17:27ribo@box:~/c$cat  /ssm/fuse/mode
hyperSpeedribo@box:~/c$ echo slow >/ssm/fuse/mode
ribo@box:~/c$ cat  /ssm/fuse/mode
slow

The 'echo >' shows passing a value into the program. So its easy for me to peek and poke various parameters of the program as it runs.

  • 1
    I don't understand the question. Yes, you can mount a fuse filesystem under /proc or /sys, or even in place of them (see: lxcfs). The question is why you would want to do this when you can just as easily mount a filesystem in /run/myapp or similar? – CameronNemo Dec 29 '18 at 5:00
  • @CameronNemo -- Original purpose of question was to find a means to query/poke parameters in a long running program. My guess was adding a directory or a couple files to /proc or /sys would be easy and effective -- rather than using pipes, socket etc. I ended up creating my own fuse mount to do it. I hoped adding on to /sys or /proc would be simpler. – Ribo Dec 30 '18 at 16:26
1

I don’t think there’s any way to add /sys or /proc entries outside the kernel. For /sys it wouldn’t make much sense anyway — it’s a direct representation of kobject data structures.

You can however provide similar interfaces from userspace, for example using FIFOs; see mkfifo for details. You can see an implementation of this in sysvinit with its initctl FIFO.

  • Yes, mkfifo files are sort of like a socket, and could be used, but there is a lot of work for the daemon/long running program to have a thread to wait for incoming data and parse the request. I liked the callbacks you get in fuse-like virtual file systems, and the file system model provides sorting out different requests (ie different file names would be for different values) – Ribo Dec 27 '18 at 15:17
0

Surely they can. You can mount anything (actual disk filesystems, fuse filesystems, overlayfs, tmpfs, bind mounts, etc) below /sys or /proc, extending and/or replacing the interfaces provided by the kernel.

Whether that's a good idea, it's a completely different matter.

Example:

# unshare -m
# touch /tmp/foo
# mount -B /tmp/foo /proc/1/status
# echo FOR GREAT JUSTICE > /proc/1/status
# cat /proc/1/status
  • This is not what the question is asking, you're just abusing the ambiguity of human language. – 炸鱼薯条德里克 Dec 29 '18 at 0:47
  • That's exactly what the question was asking. Go read it again. I used a bind mount for simplicity's sake, I could've made a simple fuse mimicking an actual part of sysfs or procfs and have it mounted below /proc. But if you're actually able to read OP's mind (no matter how), please edit the question and the title to reflect that (eg. "Can a filesystem be implemented in userspace?"). – mosvy Dec 29 '18 at 3:57
  • Read the title again, it's about adding new entries in sysfs or procfs, that's the intent, not any arbitary filesystem mounted at /sys or /proc. – 炸鱼薯条德里克 Dec 29 '18 at 4:13
  • No. You can mount a different fs under /proc or /sys to "provide procfs or sysfs" (that's what cgroupfs and binfmt_misc are doing, and you can do the same from userland, which was point of my answer). But if you really know the actual OP's intent, don't argue with me, but edit the question and the title to reflect that (and move the sample fuse implementation into an answer, where it belongs). – mosvy Dec 29 '18 at 4:20
  • You can't provide procfs from userland, you can only provide /proc, that's different. Most distros is configured that they're the same doesn't mean they HAVE TO be the same. – 炸鱼薯条德里克 Dec 29 '18 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.