Unbelievable: A web server with a file running on six operating systems

Unbelievable: A web server with a file running on six operating systems

A bunch of almost incredibly clever technical tricks go together into something practical with redbean 2: a web server plus content in a alone file running on all x86-64 operating systems.

The project is the culmination – so far – of a series of remarkable, inspired hacks by programmers Justine Tunney: αcτµαlly pδrταblε εxεcµταblε, Cosmopolitan libc and the original red bean. It may take some time to explain what it does, so bring it out with us. We promise you comes was impressed.

To begin with, redbean uses a notable hack known as APE, which stands for Actually portable drivable – as its author styles αcτµαlly pδrταblε εxεcµταblε. (If you know the Greek alphabet, this reads like “actmally pdrtable execmtable”, but hey, that looks cool.)

At the heart of the APE idea is this: whatever programming language you use, if you compile your code into a built-in binary, it produces x86-64 machine code – and any x86-64 processor runs the same machine code. If you wrote a program that did not call any external code at all, then basically the only difference between a Windows binary and (say) a Linux binary is the format of the file that contains the binary. So if you could just get around thatyour program could run on any operating system.

Linux uses a format called Executable and linkable format (FAIRY). In the old days, proprietary x86 Unixes were used Common object file format or COFF. That’s why it’s the latest conversion of Lotus 1-2-3 for Unix for Linux used a tool called coffsyrup to convert COFF to ELF.

The joke in the name APE comes from the fact that Windows NT still uses a format derived from the old COFF standard, called Portable drivable (PE). The PE format is “portable” only insofar as you are running a Windows .EXE file under DOS, it will actually run: PE files contain a small DOS stub that displays the message “This program can not be run in DOS mode.” But “Portable Executables” are not really portable at all – unlike say, family applicationswhich was built-in under both DOS and OS / 2.

APE files are, as the name implies, Actually Portable executables. A single APE file runs correctly on any x86-64 operating system. Take the file and run it on Windows, then it works as a Windows executable. Run same file on macOS, and it’s an executable macOS. Run it on Linux, and it’s a Linux binary … and the same thing on FreeBSD, and OpenBSD and NetBSD, even though they all have their own, different, executable formats.

The catch is that almost all real programs have to call a few the library, even if only the C standard library … which is there Cosmopolitan libc coming in. “Cosmo” is a libc for APE, so that C programs can be compiled and run on any operating system that APE supports.

With Cosmo and the APE format, you can write a C program and compile it into a single file that loads and runs on six completely different operating systems. Oh, and if that was not enough, the same binary can also be booted directly from the PC BIOS. Which means you can run it under Tunney’s Flashing lightsand see how the computer’s registry changes live, like an old mainframe.

Demos

At this point, the minds of many technicians may well be amazed. So to give you something beautiful to look at, there are some demo programs as well. The demo video on the Blinkenlights page shows printimage.com, which can print JPG files on the text console. If you’re still not impressed, printvideo.com do the same for video.

The original red bean made the combination of APE and Cosmo something a little more useful. After all, no matter how amazing the combination is, having only one libc in the 21st century is quite limiting. For example, you can not build any kind of graphic application. But any operating system that APE supports can run a web browser, so redbean embedded a web server (and SQLite database and Lua script) into a single binary as well. All you have to do is change the file extension to .ZIPadd your static web content using an archive manager, save it, rename it again and bingo: standalone web app.

Keep in mind that it’s a bit like an Electron app, except that instead of embedding the entire browser in your innocent little program, you use your existing browser.

Now, redbean 2.o (yes, it says two-dot-oh, not two-dot-zero) takes the original redbean and adds a REPL – for non-Lispy types, an interactive command line – and a Unix SysV API (yes, even on Windows), plus logging and more.

Speaking of Lisp, another of Tunney’s astonishing projects is SectorLispwhich embeds a Lisp interpreter in a PC boot sector. SectorLisp 2 also adds garbage collection, making it the smallest GC language in the world. Given this interest in programs that can be started directly, without an operating system at all, it may not surprise you that a future goal for redbean is to make it bootable: to embed a TCP / IP stack and network card drivers, for a complete standalone tool. ®

Bootnote

If you are not just taking our word for it and want to try redbean yourself, then it is worth knowing that there are problems with some operating systems. New versions of macOS (11 and 12) break some of the tricks that make APE work, like Ubuntu. Hacker News discussion if the project and its predecessors contain some solutions.

#Unbelievable #web #server #file #running #operating #systems

Leave a Comment

Your email address will not be published.