What would need to be changed to create a new standard stream?

Hopefully this question isn't too abstract, it touches on a bunch of software throughout the stack.

We all know about /dev/stdin, /dev/stdout, and /dev/stderr. What if I wanted to create a new standard stream, /dev/stdjson? What software would need to support that? I'm assuming that I don't need to pass a file descriptor to /dev/stdjson to every program, that they can open that stream on an as-needed basis. So where could you add a new standard stream? Would it need to be a kernal module, a part of the shell?

Answers 1

  • The thing about the standard streams is that they're assumed to be available when a process starts, which means that file descriptors to them are passed to every program. (It's even allowed for the system to open some unspecified file on those file descriptors if they otherwise would remain closed after execve().) The other thing that makes those streams standard, is that the C runtime library creates the FILE objects corresponding to standard input, output and error so the user program can use e.g. fprintf() and other stdio.h functions on them immediately.

    The "devices" /dev/stdin etc. are unrelated to that, but are system-specific ways of accessing those file descriptors (or the underlying files) via names. If you look at e.g. /dev/stdin on Linux, it's a symlink that points to /proc/self/fd/0, which itself is a magic kernel interface to file descriptor 0 of the current process. Without that fd open /dev/stdin itself doesn't do anything.

    So, to add a new "standard stream", you'd need to change the C library, and possibly a number of programs that treat fds 0, 1 and 2 specially when starting programs.

    But if you're talking about processes opening the file as needed, it sounds a bit different than what the standard streams are. Of course, you can create a named file somewhere that your programs are allowed to assume is present. But what that file would do, and if it would be a regular file, a pipe or a socket, depends on what you're actually trying to do. Also, if it's specific to some program or set of programs, it might be better placed in something like /var/lib/mytool, than in /dev/.

Related Questions