A sneak peak into the UNIX Underworld

A sneak peak into the UNIX Underworld

Did you know that Jurassic Park made a reference to Unix System

Yes, Jurssic Park made a reference to Unix

There are numerous things that a programmer finds to be quite esoteric or just doesn't simply pay attention to.

How does "ls" magically happen to list out all the files in a directory?

Why do we need to export "x/y/z" to $PATH variable after every installation?

Unix is a command-based OS. We use numerous of these commands regularly and may not pay much attention to the underworld of Unix.

Unix was built on the philosophy of "Simple is Beautiful". It is this philosophy that makes it portable, elegant, and foundational to the existing operating systems today.

Unix operating system is divided into kernel and shell at the core.

Part 1: Shell and User

Shell is what we interact with and give commands to execute.

The sleek, and mysterious Terminal is the interface between the user and the shell.

~ echo Hello There 
Hello There

~ echo General Kenobiiii!
General Kenobiiii!

Now I wonder how was one command echo able to print out my string without me importing any libraries and compiling any code.

Now if we didn't write it and it's already written isn't it?!

The next question that comes to mind is, "Where does the echo executable lie?".

And how does the terminal know where to search for it?

~ echo $PATH
/usr2/username/bin:/usr/local/bin:/usr/bin:.

$PATH variable contains exactly what it says.
It stores a list of "paths" that will be searched once we run a command/variable.

If the executable is found, it is executed otherwise we get:

~ bash: command not found: xyz

Unix external commands like echo, cat, mv, chmod, etc have their respective executables stored in /bin or usr/bin directory. (explore and see for yourself !)

Here's fun exercise to try: Write a bash file called hello.sh and store it in the same folder as your external commands location

#!/bin/bash
echo "Hello there from /usr/local/bin !"

Now when we execute bash hello.sh in your /home directory

It will output the string even though you didn't specify where the bash script was located. It was automatically scanned and found because "usr/local/bin" is stored in $PATH variable

Hello there from /usr/local/bin !

Part 2: Kernel and Shell

Unix System comprising of kernel, shell and applications is written in C. After all, it was invented by Dennis Ritchie and Ken Thompson who also developed C programming language together.

We can even find the source code of these commands. Clone the coreutils package and explore away with git clone git://git.sv.gnu.org/coreutils

Shell executables make System calls to the kernel and the kernel directly interacts with hardware to make our command a reality on screen!

All Unix-based OS use the same system calls to execute our commands. As it is the kernel is the heart of the OS. If the kernel changes, the foundation differs and OS doesn't remain the same.

Part 3: Dive into Sys Calls with Golang

Well, why Golang? Ken Thomson himself co-developed it at Google and that's why it makes for an interesting case study

Now if we recall our basics correctly what will happen when we run the command

go run .

The shell searches the $PATH variable and finds the executable called go at /usr/local/go/bin

Let's make a way to go location at /usr/local/go and look at some of the source code!

~ usr/local/go
~ cd src

Time to dig in and look for sys calls in the source code

Let's say, I want to make a directory that doesn't exist through my program in Golang.

package main

import (
    "log"
    "os"
)

func main() {
    err := os.Mkdir("testdir", 0750)
    if err != nil && !os.IsExist(err) {
        log.Fatal(err)
    }
    err = os.WriteFile("testdir/testfile.txt", []byte("Hello, Gophers!"), 0660)
    if err != nil {
        log.Fatal(err)
    }
}

Now if we dig deeper into the OS import on GitHub : https://github.com/golang/go/blob/ebb572d82f97d19d0016a49956eb1fddc658eb76/src/os/file.go#L253

func Mkdir(name string, perm FileMode) error {
    longName := fixLongPath(name)
    e := ignoringEINTR(func() error {
        return syscall.Mkdir(longName, syscallMode(perm))
    })
    ...
    return nil
}

If we follow the trail of references and definitions on GitHub we end up at

https://github.com/golang/go/blob/ebb572d82f97d19d0016a49956eb1fddc658eb76/src/cmd/vendor/golang.org/x/sys/unix/syscall_linux.go#L100

func Mkdir(path string, mode uint32) (err error) {
    return Mkdirat(AT_FDCWD, path, mode)
}

https://github.com/golang/go/blob/ebb572d82f97d19d0016a49956eb1fddc658eb76/src/cmd/vendor/golang.org/x/sys/unix/zsyscall_aix_ppc.go#L497

import "C"

func Mkdirat(dirfd int, path string, mode uint32) (err error) {
    _p0 := uintptr(unsafe.Pointer(C.CString(path)))
    r0, er := C.mkdirat(C.int(dirfd), C.uintptr_t(_p0), C.uint(mode))
    if r0 == -1 && er != nil {
        err = er
    }
    return
}

So under the hood the os package makes a syscall using mkdirat to create our directory (via C library).

No wonder there ^__^

There was no need to reinvent the wheel when Dennis Ritchie and Ken Thompson already developed C to use sys calls.

Basically, Go developers just built a wrapper over the existing functionality provided by C.

How smart is that?!

Now perhaps we can appreciate the intricacy of Unix and how foundational it has been in the Software Era.

Fun Fact: Linux, a flavour of Unix developed by Linus Torvalds was supplied many of its tools by GNU (Formerly known as Free Software Foundation)

Linux was distributed under GNU General Public License which makes it mandatory for developers and sellers to make the code public.

The culture of Open Source and collaboration stems from the days of Unix and it has benefited individuals all around the world and continues to do so.

The most Linux based distribution is Ubuntu. Ubuntu originated in African culture.

Ubuntu is a Nguni Bantu term meaning "humanity". It is sometimes translated as "I am because we are" (also "I am because you are"), or "humanity towards others" (Zulu umuntu ngumuntu ngabantu).

In Xhosa, the latter term is used, but is often meant in a more philosophical sense to mean "the belief in a universal bond of sharing that connects all humanity".

Ubuntu represents what Open Source Foundations aspire to be!

Thank you and Cheers!

Hope you had fun and learned something as well.

Just going to leave this here xD