JF Linux Kernel 2.4 Documentation: /usr/src/linux/Documentation/binfmt_misc.txt

binfmt_misc.txt

色々なバイナリ形式のカーネルによるサポート [プレインテキスト版]


     いろんな (お好みの) バイナリフォーマットのカーネルサポート v1.1
    
=====================================================================

このカーネルではシェルからその名前をタイプするだけでほとんどのプログラ
ムを(制限は下を参照) 起動することができます。この中には例えば Java(TM)、
Python、Emacs プログラムが含まれます。

これができるようにするには、バイナリと一緒にインタープリタが起動される
ようにbinfmt_misc に教えてやらなければいけません。Binfmt_misc はファイ
ルの先頭の何バイトかをあなたの指定したマジックバイトシーケンスと(特定
のビットはマスクして)比較し、バイナリタイプを判定します。Binfmt_misc 
はファイル名の拡張子、つまり 「.com」 や 「.exe」も認識することができ
ます。

新しいバイナリタイプを登録するには、次のような文字列を
  :name:type:offset:magic:mask:interpreter: 
準備して、それを /proc/sys/fs/binfmt_misc/register に echo します
(必要に応じて 「:」 は選ぶことができます)。

各フィールドの意味は、

 - name は識別のための文字列です。新しい /proc ファイルが
   /proc/sys/fs/binfmt_misc の下にこの名前で作られます。

 - type は認識のタイプです。マジックの場合は M、拡張子の場合は E を指
   定して下さい。

 - offset はファイル中での magic/mask のオフセットバイト数です。省略す
   ると (つまり 「:name:type::magic...」 と書くと)デフォルトの 0 にな
   ります。

 - magic は binfmt_misc がマッチングに使うバイトシーケンスです。マジッ
   ク文字列の中には \x0a や \xA4 のような16進数の文字を含めることがで
   きます。
   シェルの環境では、シェルが \ を食ってしまわないように \\x0a と書か
   なければいけません。
   もしファイル名拡張子によるマッチングを選んだのなら、ここは認識され
   るべき拡張子になります (ピリオド「.」は不要。\x0a のような表記はで
   きません)。
   拡張子によるマッチングは大文字小文字を区別します!

 - mask はマスクです (オプション。デフォルトはすべて0xff)。マジックと
   同様の文字列を指定することで、特定のビットをマッチングから外すこと
   ができます。
   マスクはファイルのバイト列と AND 演算されます。

 - interpreter はバイナリを最初の引数として起動されるべきプログラムです
   (フルパスで指定)。

いくつかの制限があります。

 - 設定文字列のトータルは 255 文字を超えてはいけません

 - マジックはファイルの最初の 128 バイト以内に位置しなければいけません。
   すなわち、offset+size(magic) が 128 より小さくなければいけません。

 - インタプリタ文字列は 127 文字を超えてはいけません

ブート時に /etc/rc スクリプトの中でバイナリフォーマットを追加したいこ
とがあるかも知れません。これを正しく行う方法を見つけるにはあなたの 
init プログラムのマニュアルを読んで下さい。

エントリを追加する順番に注意して下さい! 後から追加したエントリが先に
マッチングされます。


例を示します(/proc/sys/fs/binfmt_misc ディレクトリにいるものと仮定して
います)。

- em86 をサポートする(binfmt_em86 のようなもの。Alpha AXP 専用)
  echo
':i386:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:/bin/em86:'
> register
  echo
':i486:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:/bin/em86:'
> register

- パックされた DOS アプリケーションをサポートする (プレコンフィギュア
  ド dosemu hd イメージ)
  echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register

- wine を使って Windows の実行形式をサポートする
  echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register

Java のサポートについては Documentation/java.txt を見て下さい。


Binfmt_misc や、あるバイナリタイプだけの有効/無効を切り替えるには、
/proc/sys/fs/binfmt_misc/status または /proc/.../the_name に 0 をエコー
するか(無効にする)、1 をエコーします(有効にする)。
ファイルを cat すれば binfmt_misc/the entry の現在の状態がわかります。

あるエントリまたはすべてのエントリを削除するためには、/proc/.../the_name
または /proc/sys/fs/binfmt_misc/status に -1  をエコーします。


ヒント
======

インタプリタに特別な引数を渡したい時は、そのためのラッパースクリプト
(wrapper script) を書くことができます。サンプルは Documentation/java.txt
を見て下さい。

あなたのインタプリタはファイル名の PATH を参照しては「いけません」。
カーネルはフルファイル名を渡しています。PATH を使うと予期せぬ動作を
したりセキュリティ的に危険なことがあります。


binfmt_misc に関するページは
http://www.anatom.uni-tuebingen.de/~richi/linux/binfmt_misc.html
です。

Richard G・ther, richard.guenther@student.uni-tuebingen.de

日本語訳:林(hayashi884@geocities.co.jp) 1999.5.8 (Ver.0.1)
-----------------------------------------------------------
(以下原文)

     Kernel Support for miscellaneous (your favourite) Binary Formats v1.1
     =====================================================================

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

To actually register a new binary type, you have to set up a string looking like
:name:type:offset:magic:mask:interpreter: (where you can choose the ':' upon
your needs) and echo it to /proc/sys/fs/binfmt_misc/register.
Here is what the fields mean:
 - 'name' is an identifier string. A new /proc file will be created with this
   name below /proc/sys/fs/binfmt_misc
 - 'type' is the type of recognition. Give 'M' for magic and 'E' for extension.
 - 'offset' is the offset of the magic/mask in the file, counted in bytes. This
   defaults to 0 if you omit it (i.e. you write ':name:type::magic...')
 - 'magic' is the byte sequence binfmt_misc is matching for. The magic string
   may contain hex-encoded characters like \x0a or \xA4. In a shell environment
   you will have to write \\x0a to prevent the shell from eating your \.
   If you chose filename extension matching, this is the extension to be
   recognised (without the '.', the \x0a specials are not allowed). Extension
   matching is case sensitive!
 - 'mask' is an (optional, defaults to all 0xff) mask. You can mask out some
   bits from matching by supplying a string like magic and as long as magic.
   The mask is anded with the byte sequence of the file.
 - 'interpreter' is the program that should be invoked with the binary as first
   argument (specify the full path)

There are some restrictions:
 - the whole register string may not exceed 255 characters
 - the magic must reside in the first 128 bytes of the file, i.e.
   offset+size(magic) has to be less than 128
 - the interpreter string may not exceed 127 characters

You may want to add the binary formats in one of your /etc/rc scripts during
boot-up. Read the manual of your init program to figure out how to do this
right.

Think about the order of adding entries! Later added entries are matched first!


A few examples (assumed you are in /proc/sys/fs/binfmt_misc):

- enable support for em86 (like binfmt_em86, for Alpha AXP only):
  echo ':i386:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x03:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:/bin/em86:' > register
  echo ':i486:M::\x7fELF\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06:\xff\xff\xff\xff\xff\xfe\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfb\xff\xff:/bin/em86:' > register

- enable support for packed DOS applications (pre-configured dosemu hdimages):
  echo ':DEXE:M::\x0eDEX::/usr/bin/dosexec:' > register

- enable support for Windows executables using wine:
  echo ':DOSWin:M::MZ::/usr/local/bin/wine:' > register

For java support see Documentation/java.txt


You can enable/disable binfmt_misc or one binary type by echoing 0 (to disable)
or 1 (to enable) to /proc/sys/fs/binfmt_misc/status or /proc/.../the_name.
Catting the file tells you the current status of binfmt_misc/the entry.

You can remove one entry or all entries by echoing -1 to /proc/.../the_name
or /proc/sys/fs/binfmt_misc/status.


HINTS:
======

If you want to pass special arguments to your interpreter, you can
write a wrapper script for it. See Documentation/java.txt for an
example.

Your interpreter should NOT look in the PATH for the filename; the
kernel passes it the full filename to use.  Using the PATH can cause
unexpected behaviour and be a security hazard.


There is a web page about binfmt_misc at
http://www.anatom.uni-tuebingen.de/~richi/linux/binfmt_misc.html

Richard Gther, richard.guenther@student.uni-tuebingen.de

Linux カーネル 2.4 付属文書一覧へ戻る