Skip to content

Linux Server Configuration

Zwetan Kjukov edited this page Jun 23, 2015 · 2 revisions

Redtamarin is the project that allow us to execute ActionScript 3.0 on the command line and by extension to run AS3 programs on the server side.

The same way you would install PHP, or Python, or Ruby executables to execute one of those languages on the backend you need to install a runtime (or a shell) to execute AS3.

Our runtime is the redshell (the redtamarin shell), it can execute plain AS3 source code (uncompiled), source code compiled to bytecode (ABC, ActionScript ByteCode), many ABC files embedded in a SWF (here the SWF is just a container of bytecodes, it can not contain assets of any other kind), or produce an independent executable (by merging the redshell and the bytecode into one file).

There is yet another option, installing a special redshell named as3shebang which will setup the system to be able to execute plain AS3 source code as shell scripts.

Here our goal is to be able to execute our AS3 program under Apache as a CGI executable, it is not the only option but it simplest and safest one to get started.

The most compatible way

Produce an independent executable, the same way you would compile C to to a binary to execute under CGI.

The scenario is to take our program index.abc and to merge it with one of the redshell to produce an all embedded no need for external dependencies executable.

It is the most compatible way because as you have many redshell available in both 32-bit and 64-bit for Windows, Macintosh (Mac OS X) and Linux, and that most HTTP servers (Apache, nginx, IIS, etc.) support CGI, producing such executable will just work everywhere.

The developer way

Compile the source code to index.abc and find a way to execute the bytecode under CGI.

The first thing would be to install the necessary redshell executables on the system, from the Redtamarin SDK you can simply move redshell to /usr/bin for example.

And here the problem, most languages that run under CGI will run as shell scripts and so do need to support the shebang line, for example: #!/usr/bin/perl, but because ABC files are bytecode not scripts we can not do that.

A poor man workaround would be to write a shell script to run our ABC file

#!/usr/bin/bash
./redshell index.abc

Another workaround would be to install as3shebang to write an AS3 shell script to run our ABC file

#!/usr/bin/as3shebang --

import C.stdlib.*;

system( "redshell index.abc" );

or

#!/usr/bin/as3shebang --

import shell.*;

var index:* = Domain.currentDomain.load( "index.abc" );
//either the index.abc execute itself
//or you can instantiate it from here
//for ex:
//import org.as3lang.www.WebSite;
//var website:WebSite = new WebSite();
//website.run();

But those are far from perfect, can cause problems with stdin/stdout redirection, environment variables, etc.

no what we really want is to execute the ABC file as if it was a shell script.

The hacker way

Under Linux, we need to install binfmt_misc, as the wikipedia page explains

binfmt_misc is a capability of the Linux kernel which allows arbitrary executable file formats to be recognized and passed to certain user space applications, such as emulators and virtual machines.

You still need to install the redshell binary in your executable path (eg. /usr/bin).

Note, this has been tested Under a Linux Ubuntu Server 14.04 your epxerience may vary on other distros.

Edit fstab to configure binfmt_misc

$ sudo pico /etc/fstab

add the following line

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

after that you need to mount it

$ sudo mount -a

Now because the redshell is a bit special we need to create a wrapper script to pass command line arguments

let's call it abcrun

#!/bin/sh

ABCNAME=$1
shift
/usr/bin/redshell "${ABCNAME}" -- $@

move this shell script to /usr/bin

Now let use binfmt_misc to register it to run .abc extensions

$ echo ':ABC:E::abc::/usr/bin/abcrun:' | sudo tee /proc/sys/fs/binfmt_misc/register

check if binfmt_misc is enabled

$ sudo cat /proc/sys/fs/binfmt_misc/status

let’s test an abc file

import C.unistd.getlogin;

trace( "hello world" );
trace( "username = " + getlogin() );

compile it to test.abc and run it

$ chmod +x test.abc
$ ./test.abc

If this works you are all setup.

To run those ABC files under Apache now we can simply use

AddHandler cgi-script .abc

As a final note, if you reboot the server you will lose the binfmt_misc configuration, to make it available on reboot you need to configure a script in your rc.local