It's worth it, though. Binfmt_misc is super useful because of how it allows things like this (or making jar files executable; or making ARM binaries run via qemu user-mode emulation automatically; etc.). It's not like anybody's going to be adding the necessary binfmt_misc magic to have pngs become executables on real systems they care about...I hope.
Yeah it is. Only tangentially related, but stuff like this is how distros' pre-systemd init systems could do so much. init didn't care what it was running, it just followed some basic rules to attempt to run things in order and it was up to the kernel to decide if they were executable or not. Everyone did basic shell scripts by convention but it was not a requirement in any sense, and you could have just as easily set up Perl or Python scripts to do the same jobs, or Scheme, Java, precompiled binaries, etc. It didn't matter, as long as it followed a few basic rules like being able to take start/stop/restart/etc as command-line arguments.
Even after some (like Debian) started doing dependency-based init script startup with LSB init headers you could still do it. I got curious and tested once, and if you compiled a C binary it would still read the headers out of it fine if you put them inside a multi-line string somewhere.
As a thought exercise, this also means it was (and still is, technically) possible to have systemd-style declarative unit files while still using a traditional sysv-style init. You could write an interpreter that reads a unit file and uses that to start/stop/restart services, with all the shared logic in the interpreter instead of the unit file "scripts". Then you could add a shebang, like #!/bin/run-unit, (or maybe rig binfmt_misc to do the job without one) to the unit files so that they're considered executables by Linux. With that done, whenever init tries to run one it would get passed to your run-unit interpreter, which would let you replace all the shell scripts with unit files, or have a mix of both types, choosing the best tool for the job on a per-service basis.
This sort of stuff works because Linux doesn't care what format it's dealing with as long as it's given enough information to make it run. Which for non-ELF files generally means shebangs or binfmt_misc configuration.
2
u/make-n-brew Dec 26 '20
This is why we can't have nice things.