←back to thread

185 points thunderbong | 9 comments | | HN request time: 0s | source | bottom
1. Imustaskforhelp ◴[] No.43647155[source]
Read your article, it's really nice. I really feel much less demystified by this.

But can you / somebody please explain what this means

According to the official Kernel Admin Guide:

This Kernel feature allows you to invoke almost (for restrictions see below) every program by simply typing its name in the shell. This includes for example compiled Java(TM), Python or Emacs programs. To achieve this you must tell binfmt_misc which interpreter has to be invoked with which binary. Binfmt_misc recognises the binary-type by matching some bytes at the beginning of the file with a magic byte sequence (masking out specified bits) you have supplied. Binfmt_misc can also recognise a filename extension aka .com or .exe.

It’s another way to tell the Kernel what interpreter to run when invoking a program that’s not native (ELF). For scripts (text files) we mostly use a shebang, but for byte-coded binaries, such as Java’s JAR or Mono EXE files, it’s the way to go!

Like, can you give me an example by what you mean. What are its use cases, if any. I read it many times and always with some sort of enthusiasm because of this sentence ending in exclamation point making me feel like it's huge yet I just can't understand it's significance.

Does it mean we can have .jar files which can then run shebang like, so we don't need #! , can this also be used for main.go or every other language which has some issues with #! ,

I see there being some interpreter for golang, rust etc. which just compiles it but it was just too complex. I am just imagining something like a simple go file which is valid golang but can be run by linux simply by ./ And it autocompiles it...

replies(4): >>43647278 #>>43647280 #>>43649406 #>>43649840 #
2. ckatri ◴[] No.43647278[source]
The best and most common uses for this are Wine and qemu-static.

For example, the following (which I grabbed from Wikipedia) `:DOSWin:M::MZ::/usr/bin/wine:` will register `/usr/bin/wine` to run as the wrapper for any .exe that gets executed, with no extra config needed. It simply sees that you tried to run a PE file and will run it in wine.

replies(1): >>43650577 #
3. pkaye ◴[] No.43647280[source]
Yes you can use binfmt_misc to allow arbitrary executable file format to be passed to an interpreter matched either by a filename extension or a magic number at a specific offset within the executable.

https://en.wikipedia.org/wiki/Binfmt_misc

replies(1): >>43651343 #
4. chrisweekly ◴[] No.43649406[source]
> "I really feel much less demystified by this."

Respectful correction: you feel less mystified, i.e. it has been demystified for you.

PS That nit aside, great question. Sorry I can't provide illumination.

replies(2): >>43650547 #>>43650569 #
5. ElectricalUnion ◴[] No.43649840[source]
> byte-coded binaries, such as Java’s JAR

Wait, aren't JARs ZIPs (so they have the headers appended on the end of the file)? How does prefix matching help that?

6. brookst ◴[] No.43650547[source]
I also can’t answer, but I will adopt “thanks, you’ve made me less demystified” to use alongside “I’ll give your suggestion all the attention it merits.”
7. Imustaskforhelp ◴[] No.43650569[source]
Um it was 2 in the morning when I had written that comment.

Yeah, I should've said less mystified. Gotta remember it in the future!

Thanks!

8. Imustaskforhelp ◴[] No.43650577[source]
okay so now you can have some command which you can run which would do somethings with procfs/what not and now you can simply run any jar or exe file as it is....

That is really nice actually!

this could theoretically be used as a scriptesto alternative but it requires some command to run , I was hoping there was a way to use something other than shebang

9. kreetx ◴[] No.43651343[source]
Yup, this way you can skip the shebang line for your scripts in some arbitrary language.

I have it set up for Haskell (it's somewhat hackish): there is preconfigured haskell project in a certain location with desired dependencies, imports, etc., so when executing a .hs file this file is copied into that as Main and ran. A similar setup will work for any language.

Edit: `cabal` and `stack` both have script commands now, so these would be an alternative to the above, downside being that every such script would need the shebang intro with dependencies, etc.