The early days of exaequOS

by Benoit, 16 September 2024


Genesis

April 2022. I was studying remote frame buffer protocol and X window system in order to see how I could use them in a web page as an alternative to html/css. I started from scratch a basic implementation in Javascript of a worker sending display commands to the main thread. But the amount of work needed for having a working demo is huge and this technical approach has a very limited usage. And I stopped there...
October 2022. I was thinking about how to teach the basis of computer science when I found my 6 month old project in my laptop. Computer science started with text terminal long before graphical display. Thus, from this chronological observation, I decided to port, for the web, an existing app working in a console. Moreover this app should be quite simple for easing the porting task and should reflect, if possible, the early days of Unix. The choice of a Web platform is to allow maximum of people to access it with no installation.

ed/GNU ed

After a few days of thinking, I decided to port ed for the Web and more precisely GNU ed, the open source version. 'ed' is the standard text editor of any Unix system. It was developped by Ken Thompson, one of the designer and developper of Unix operating system. The source code of Gnu ed is commposed of a few C files. For all these reasons, I thought 'ed' was a good candidate.
For compiling GNU ed for the Web, I installed emscripten compiler toolchain that is able to transform a C/C++ program into a Javascript/WebAssembly executable running in a browser (or in Node.js). I previously used emscripten for compiling a C++/OpenGL rendering engine into a Web application. But, this time, work was a bit different: I had to circumvent a limitation of emscripten. It is able to run OpenGL or SDL applications but not able to read/write from/to a terminal. A 'printf' command outputs text onto the debug console of the Web browser (that only developpers are using). I had to find something better.
The simpler approach I found was to modify slightly GNU ed for receiving/sending text from Xterm.js, a Javascript library that displays a terminal in a Web page. The challenge was to make work together the synchronous C code of GNU ed with the asynchronous API of Xterm.js. Hopefully emscripten comes with a wonderful feature called asyncify. It allows to call asynchronous function in a synchronous code.
That is how I managed, only in a few days, to get GNU ed running in a Web page. A first step of my new idea was achieved. It was the occasion for me and others to learn how to use 'ed' and to discover how powerful it is. But, running alone in a Web page, this editor is not very useful. I could neither open/save files nor execute commands in it. Given the sucess of this milestone, I decided to go one step further.

GNU bash

The next step was much more ambitious: running GNU bash in a Web page, because I needed a command line for starting GNU ed. bash is much more complex than GNU ed and modifying bash as I did for GNU ed was not an option. Digging into this complex and large source code provokes headaches. Instead, I decided to dig into emscripten toolchain source code and to modify the implementation of the read/write syscalls. That is how I started to fork emscripten repo and created emscripten-exa repo. After a few days of work, I managed to make bash running in a Web page. It was a kind of miracle for me. Of course all was not working, I couldn't start programs for example but I could get the help, echo some text, navigate in the command history. The second step of my projet was achieved. The key function of bash was missing: to be able to start other programs.

fork/exec syscalls

Starting a new program means creating a new process running in the system. In a Unix-like operating system, this is achieved in two phases. First, a child process is created by forking the running program (i.e the parent process). Second, the child process executes a new program (the memory is replaced with this new program). But what is a process in case of a program running in a Web browser ? emscripten does not handle any process, it only generates one program per Web page. I decided to associate one iframe to one process. So forking means duplicating the iframe and executing a new program means replacing the source of the iframe. My prototype of fork/exec syscalls worked in a few days. I had the foundation for creating a new kind of operating system running in a web browser.
December 2022, EXA kernel is born...