Quote:
Originally Posted by netjack
How do I activate (and, possibly) deactivate that in the kernel?
|
Before we do this, let’s create a jar-file loader (which we will later use). The prospect of this loader is that you execute your program:
Code:
jarloader /path/to/program.jar
We will place it in
/usr/local/bin so it doesn’t interfere with system files (all further excerpts assume you have root access):
Code:
cat > /usr/local/bin/jarloader << EOF
#!/bin/sh
/usr/bin/java -jar $@
EOF
(you might change the
/usr/bin/java so it matches your actual java interpreter)
Let’s make it executable:
Code:
chmod +x /usr/local/bin/jarloader
Now let’s get on with using
binfmt_misc.The concept is pretty simple (a more detailed explanation may be found
here). First, you load the binfmt_misc module:
Code:
modprobe binfmt_misc
Then, you mount the virtual binfmt_misc filesystem. This will allow you to see and register miscellaneous “binary formats” with the kernel. The canonical place to mount it is within the
/proc directory itself:
Code:
mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
Now, we register our file by writing a string containing a sequence of colon-delimited fields (which are explained in detail in
the documentation) to the file
register:
Code:
echo ":JAR:E::jar::/usr/local/bin/jarloader:" > /proc/sys/fs/binfmt_misc/register
You now see new file in the virtual filesystem. Its contents may be viewed by simply reading them:
Code:
cat /proc/sys/fs/binfmt_misc/JAR
You may temporarily disable this format by writing a 0 to it:
Code:
echo "0" > /proc/sys/fs/binfmt_misc/JAR
It may be re-enabled by writing a 1 to it:
Code:
echo "1" > /proc/sys/fs/binfmt_misc/JAR
It may be removed completely by writing -1 to it:
Code:
echo "-1" > /proc/sys/fs/binfmt_misc/JAR
A different solution may be found for naked class files (usually shorter programs). It is different for two reasons:
- The java interpreter does not expect the full path to the class file as an argument. Instead, it assumes you have a suitable definition of CLASSPATH and that you specify the name of the class itself (without the extension). So your wrapper program may look more like this (which is a tad more complicated).
- Instead of matching the extension (which is somewhat crude), we can use the fact that all java classes begin with the first four bytes 0xCAFEBABE. So instead of matching an extension, we match a magic number. So your registration might end up looking like:
Code:
echo ':Class:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:' > register
instead.
You can also add the relevant commands to your startup scripts to get automatic registering of formats.