1

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?

4
  • Could you possibly give some background to your question, a reason for needing a new standard stream? Also, what would be communicated on this stream that could not be communicated on the standard input, standard output, or the standard error stream? Commented Aug 22, 2021 at 20:07
  • I'm not expecting to actually do this, to be clear. There are a lot of suggestions on how to pipe structured data between programs. There are tools like the relatively new "jc" which provide generic parsers for textual output of a bunch of programs. There's whatever power-shell is doing... A new stream is another way to handle it, and I'd love to talk about the advantages and disadvantages of that approach, but I don't think stack exchange will let me as it's too broad of a topic. Commented Aug 23, 2021 at 13:58
  • The thing about the standard streams is that there is nothing saying anything about what format the data should have on these streams. In fact, data may be JSON, GZip compressed binary, line-based text records, CSV, or whatever else. It sounds as if you're talking about a stream that enforces something like a standard format. Is that correct? Commented Aug 23, 2021 at 15:35
  • I think there's a big difference between machine-optimized and person-optimized formats. Json is an obvious option for a machine-optimized format, but I wouldn't want LS to output json by default. A /dev/stddata node could make sense as well, which could support multiple machine-readable data formats. I think the important thing is to have a separate stream for machine-readable data. Alternatively a way to tell if the command ahead in the pipeline wants machine-readable data could be nice. The same way color gets "magically" stripped if you're piping to not a tty. Commented Aug 24, 2021 at 15:49

1 Answer 1

2

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/.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.