in the name of zero

June 2, 2007

run mp3s anyone?

i posted an entry the other day about playing with binary formats and i’ve been reading linux/binfmt_script.c in my free time eversince. the logic is simple i think; linux kernel enters a loop calling a linked list of binary format loaders:

fs/exec.c::search_binary_handler()
	
    for (fmt = formats ; fmt ; fmt = fmt->next) {
                        int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary;
                        if (!fn)
                                continue;
                        if (!try_module_get(fmt->module))
                                continue;
                        read_unlock(&binfmt_lock);
                        retval = fn(bprm, regs);
			...

so binfmt_script.c adds script execution functionality to the linux kernel by registering a loader thru register_binfmt(&script_format). so we’d assume that when we call a script (text file), the kernel will eventually call binfmt_script.c::script_format() to take control. the function then takes care of checking for the sha bang line, parse the interpreter after found in the sha bang line including the arguments .. and then muck around the original linux_binprm struct so that the original executed file (script file in this case) now becomes the argument to the interpreter found in the sha bang line… finally, it restarts the entire exec procedure by calling search_binary_handler() again.

for practice, i decided to make mp3s runnable.

we obviously can’t do this…

$ ./kangta\ -\ propose.mp3
bash: ./kangta - propose.mp3: cannot execute binary file

now, in the spirit of binfmt_script.c, i took into consideration the intrepreter and the script. in my case, intrepreter happens to be /usr/bin/mplayer and consequently, the script is the mp3 file. i grepp’ed /usr/share/misc/file/magic for mp3 signatures and finally constructed binfmt_mp3.c

$ file kangta\ -\ propose.mp3
kangta - propose.mp3: MP3 file with ID3 version 2.3.0 tag
	
$ chmod +x kangta\ -\ propose.mp3 
	
$ ./kangta\ -\ propose.mp3
 ...
Starting playback...
A:  32.2 (32.1)  1.3% 33%

the next step would be to raise flexibility by adding a procfs entry to change the player on the fly. e.g. echo “/bin/mpg123″ > /proc/useless/binfmt_mp3 but that’s a learning project for some other time.

i’ve uploaded the files here if anyone’s curious.

Comments »

The URI to TrackBack this entry is: http://gnurbs.blogsome.com/2007/06/02/run-mp3s-anyone/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Get free blog up and running in minutes with Blogsome | Theme designs available here