Binfmt misc

From Wikipedia, the free encyclopedia

The title of this article should be binfmt_misc. The initial letter is capitalized and an underscore is substituted or omitted because of technical restrictions.

binfmt_misc is a Linux kernel capability which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines. The executable formats are registered through a /proc file system interface.

For example, binfmt_misc allows Java programs to be passed directly to the Java virtual machine, or execute Windows PE executables through Wine.

Contents

[edit] How it works

If the binfmt_misc module is loaded, then the binfmt_misc filesystem can be mounted, like so:

 mount -t binfmt_misc none /proc/sys/fs/binfmt_misc

Or with an entry in /etc/fstab:

 none    /proc/sys/fs/binfmt_misc    binfmt_misc    defaults  0 0

If the binfmt_misc filesystem is properly mounted, then the following files will exist in /proc/sys/fs/binfmt_misc:

 --w------- 1 root root 0 register
 -rw-r--r-- 1 root root 0 status

When the status file is read, it indicates the status of the binfmt_misc module: either "enabled" or "disabled." It can also be written: '1' enables, '0' disables, and '-1' clears all of the registered executable formats.

Writing a line to the register file will cause a new binary format to be registered. The line must be of the form:

 :name:type:offset:magic:mask:interpreter:

name is the name of the new binary format. type is either E or M. If it is E, then the executable file format is identified by its file extension. If it is M, then the executable file format is identified by a magic number near the beginning of the file. If the type is E, then magic is the file extension to be associated with the binary format, and offset and mask should be left empty. Otherwise, magic is the magic number identifying the binary format, offset is the offset at which the magic number should be found in the file, and mask is bitwise ANDed with the magic string from the file, the result being that bits which are unset in the mask are ignored in the comparison to the magic.

interpreter is a path to an executable. When a file using a binfmt_misc-registered executable format is executed, this program is run with the executable as an argument.

When an executable file format is registered, a file with the name given to 'register' is created in the /proc/sys/fs/binfmt_misc directory. This file can be read to get information about the file format.

[edit] Examples

[edit] HTML

 # cd /proc/sys/fs/binfmt_misc
 # echo ':HTML:E::html::/usr/bin/firefox:' > register
 # cat HTML
 enabled
 interpreter /usr/bin/firefox
 flags: 
 extension .html
 # cd
 # chmod +x foo.html
 # ./foo.html
 -- If all goes well, Firefox opens foo.html --

[edit] Java

 # cd /proc/sys/fs/binfmt_misc
 # echo ':Java:M::\xca\xfe\xba\xbe::/usr/local/java/bin/javawrapper:' > register
 (N.B.: Put something sensible in /usr/local/java/bin/javawrapper.)
 # cat Java
 enabled
 interpreter /usr/local/java/bin/javawrapper
 flags: 
 offset 0
 magic cafebabe
 # cd
 # chmod +x foo.jar
 # ./foo.jar
 -- If all goes well, foo.jar executes --

[edit] See also

[edit] External links