Lesson 16advancedBehind the Scenes

How This Site Runs Your Code

Every Run button on this site uses the system calls from this course. A small C launcher forks, sets resource limits, creates private namespaces, gives up root with setuid() and execs bash. Read the real launcher, run it yourself, and watch the kernel enforce the rules.

What happens when you click Run

Every step uses a system call from this course. Click a lesson tag to revisit it.

  1. 1
    Your browserWebSocketL11

    opens a WebSocket and sends your files

  2. 2
    Cloudflare WorkerHTTP upgradeL11

    checks the website, rate-limits, picks a sandbox with room

  3. 3
    Container (tiny Linux VM)wait()L02

    tini is PID 1 — it reaps orphaned processes so no zombies pile up

  4. 4
    Supervisor (Node.js, root)adduser, chownL01

    creates a throw-away Unix user and a 0700 folder, writes your files

  5. 5
    forkpty()fork()L02

    a new process attached to a pseudo-terminal

  6. 6
    sandbox-launchunshare()L09

    private IPC, network and /tmp for this session

  7. 7
    sandbox-launchsetrlimit()L02

    64 processes, 120 s CPU, 512 MB, 128 open files

  8. 8
    sandbox-launchsetgid() / setuid()L04

    gives up root — for good

  9. 9
    bashexecve()L04

    the launcher becomes your shell; everything above is inherited

  10. 10
    Your programfork() + exec()L04

    gcc, then ./prog — a child of bash with all the limits

…and when you press Ctrl+C

  1. A
    Ctrl+C button

    types the ^C character into the terminal — it kills nothing itself

  2. B
    Terminal driver (kernel)

    sees ^C and sends SIGINT to the foreground process group

  3. C
    Your program + its children

    all get SIGINT: default action ends them, a handler can catch it

  4. D
    bash

    ignores SIGINT, prints a new prompt; the site reads the exit status (130 = 128 + 2)

Run ctrl-c-path.c below to watch steps B and C happen to two processes at once.

1

The Real Launcher

The exact program that starts every terminal on this site. Run it as a student and it refuses twice. Directly, setsid() fails: bash made your program a process-group leader for job control, and a leader may not start a new session (that is why daemons fork first). Started through sh, it gets further — until setgroups() refuses: only root may change who it is. That refusal is the security model.

sandbox-launch.c
$ gcc -Wall -o sandbox-launch sandbox-launch.c
$ ./sandbox-launch 1234 1234 . example; sh -c './sandbox-launch 1234 1234 . example; exit $?'
Expected Outputclick Run to execute live
[sandbox] setsid failed: Operation not permitted
[sandbox] setgroups failed: Operation not permitted
Exits 1 on purpose: EPERM — you are not root
2

Who Am I in Here?

Prints what the launcher left behind: your user, zero capabilities, your process group and session, every resource limit, and the namespace ids that keep you apart from other students.

whoami-sandbox.c
$ gcc -Wall -o whoami-sandbox whoami-sandbox.c
$ ./whoami-sandbox
Expected Outputclick Run to execute live
══════════════════════════════════════
  Who am I inside the course sandbox?
══════════════════════════════════════

[Identity]  uid=1002 gid=101 — not 0, so not root (setuid() in the launcher)
  CapEff:	0000000000000000
            all zeros = no special powers left

[Process group / session — Lessons 03 and 06]
  my pid=3304  process group=3304  session=3294
  foreground group of this terminal=3304  <- that's me: Ctrl+C will be sent to MY group

[Limits — set with setrlimit() before the launcher dropped root]
  processes      64         -> fork() fails with EAGAIN (Lesson 02)
  CPU seconds    120        -> SIGXCPU, then SIGKILL
  memory bytes   536870912  -> malloc() returns NULL
  file blocks    20971520   -> SIGXFSZ when a file grows too big
  open files     128        -> open()/pipe() fail with EMFILE (Lesson 08)

[Namespaces — this session's private copies]
  ipc  ipc:[4026532650]
  net  net:[4026532676]
  mnt  mnt:[4026532896]
  Open a second terminal (Two Terminals) and run this again:
  same ids there, different ids for every other student.
3

A Safe Fork Bomb

Forks until the kernel refuses with EAGAIN (the RLIMIT_NPROC limit), then kills and reaps every child. This is why a runaway fork() loop from Lesson 02 cannot take the site down.

fork-until-eagain.c
/*
 * fork-until-eagain.c — A *safe* fork bomb: why the course site survives one
 *
 * Demonstrates: RLIMIT_NPROC stops fork() with EAGAIN — the system protects itself
 * Key concepts: fork() return value -1, errno, EAGAIN, pause(), kill(), wait()
 * Compile: gcc -Wall -o fork-until-eagain fork-until-eagain.c
 * Run:     ./fork-until-eagain
 *
 * Lesson 02 showed fork() in a loop going wrong. Here we do it on purpose — but every
 * child just waits, and the parent cleans up. The limit (64 processes for your user,
 * set by sandbox-launch.c) is reached long before the machine is in danger.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/resource.h>

#define MAX_CHILDREN 1000

int main(void)
{
    pid_t children[MAX_CHILDREN];
    int count = 0;
    struct rlimit rl;
    getrlimit(RLIMIT_NPROC, &rl);

    printf("\n══════════════════════════════════════\n");
    printf("  fork() until the kernel says no\n");
    printf("══════════════════════════════════════\n\n");
    printf("[Parent PID %d] My user may own at most %llu processes (RLIMIT_NPROC).\n",
           (int)getpid(), (unsigned long long)rl.rlim_cur);
    fflush(stdout); /* flush before fork, or every child inherits the buffered text */

    while (count < MAX_CHILDREN) {
        pid_t pid = fork();
        if (pid == -1) {
            /* The interesting line: which error stopped us? */
            printf("[Parent] fork() #%d failed: errno=%d (%s)%s\n", count + 1, errno, strerror(errno),
                   errno == EAGAIN ? " — the process limit did its job" : "");
            break;
        }
        if (pid == 0) {
            pause();   /* child: sleep until a signal arrives */
            _exit(0);
        }
        children[count++] = pid;
    }

    printf("[Parent] Created %d children before the limit (the shell and I count too).\n", count);

    /* Clean up: every child gets SIGTERM, then we collect them so none stay zombies */
    for (int i = 0; i < count; i++)
        kill(children[i], SIGTERM);
    int reaped = 0;
    while (wait(NULL) > 0)
        reaped++;
    printf("[Parent] Sent SIGTERM to all of them and reaped %d with wait(). Nothing left behind.\n", reaped);
    return 0;
}
$ gcc -Wall -o fork-until-eagain fork-until-eagain.c
$ ./fork-until-eagain
Expected Outputclick Run to execute live
══════════════════════════════════════
  fork() until the kernel says no
══════════════════════════════════════

[Parent PID 3321] My user may own at most 64 processes (RLIMIT_NPROC).
[Parent] fork() #63 failed: errno=11 (Resource temporarily unavailable) — the process limit did its job
[Parent] Created 62 children before the limit (the shell and I count too).
[Parent] Sent SIGTERM to all of them and reaped 62 with wait(). Nothing left behind.
4

The Path of Ctrl+C

A parent and its child wait in the same process group. The Ctrl+C button types ^C into the terminal, and the kernel delivers SIGINT to the whole foreground group — both handlers run.

ctrl-c-path.c
/*
 * ctrl-c-path.c — Where does Ctrl+C go when you click the Ctrl+C button?
 *
 * Demonstrates: the terminal sends SIGINT to the whole FOREGROUND PROCESS GROUP
 * Key concepts: sigaction(), getpgrp(), tcgetpgrp(), fork() keeps the process group
 * Compile: gcc -Wall -o ctrl-c-path ctrl-c-path.c
 * Run:     ./ctrl-c-path      (then press Ctrl+C, or wait — the site presses it for you)
 *
 * The site's Ctrl+C button does not "kill your program". It types the ^C character into
 * the terminal (exactly like your keyboard). The terminal driver turns ^C into SIGINT and
 * delivers it to every process in the foreground group — so parent AND child both get it.
 */

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

static const char *who = "Parent";

static void on_sigint(int sig)
{
    /* printf is not async-signal-safe; fine for a demo, use write() in real code */
    printf("[%s PID %d] got signal %d (SIGINT) — I am in process group %d\n",
           who, (int)getpid(), sig, (int)getpgrp());
    fflush(stdout);
    _exit(0);
}

int main(void)
{
    struct sigaction sa;
    sa.sa_handler = on_sigint;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGINT, &sa, NULL); /* installed before fork(): the child inherits it (Lesson 06) */

    printf("\n══════════════════════════════════════\n");
    printf("  The path of Ctrl+C\n");
    printf("══════════════════════════════════════\n\n");
    printf("[Parent PID %d] process group %d; the terminal's foreground group is %d\n",
           (int)getpid(), (int)getpgrp(), (int)tcgetpgrp(STDIN_FILENO));
    fflush(stdout);

    if (fork() == 0) {
        who = "Child";
        printf("[Child  PID %d] same process group %d — fork() does not change it\n",
               (int)getpid(), (int)getpgrp());
        fflush(stdout);
        for (;;)
            pause();
    }

    printf("[Parent] Both of us now wait. Press Ctrl+C...\n");
    fflush(stdout);
    for (;;)
        pause();
}
$ gcc -Wall -o ctrl-c-path ctrl-c-path.c
$ ./ctrl-c-path
Expected Outputclick Run to execute live
══════════════════════════════════════
  The path of Ctrl+C
══════════════════════════════════════

[Parent PID 3402] process group 3402; the terminal's foreground group is 3402
[Parent] Both of us now wait. Press Ctrl+C...
[Child  PID 3404] same process group 3402 — fork() does not change it
^C[Child PID 3404] got signal 2 (SIGINT) — I am in process group 3402
[Parent PID 3402] got signal 2 (SIGINT) — I am in process group 3402

[runs until stopped — the site stops it after 4s]
Runs until stopped — stops automatically after 4s

Exercises

  1. 1. Count the processes

    Lesson 02

    Run fork-until-eagain.c, then open Two Terminals and run it in both at once. Why does the second one create fewer children? (Hint: the limit is per user, and both terminals are the same user.)

  2. 2. Ignore Ctrl+C

    Lesson 03

    Change ctrl-c-path.c so only the child ignores SIGINT (SIG_IGN). Press Ctrl+C: who exits, who stays? How do you stop the survivor from the second terminal?

  3. 3. What survives exec()

    Lesson 04

    whoami-sandbox.c prints limits that were set before bash was exec'd. Add a line that runs execlp("sh", "sh", "-c", "ulimit -u", NULL) — does the limit survive one more exec?

  4. 4. A handler that tells you who sent it

    Lesson 06

    Rewrite on_sigint() in ctrl-c-path.c with SA_SIGINFO and print info->si_pid. Who sends SIGINT when you press Ctrl+C — and who when you run kill -INT from the second terminal?

  5. 5. Run out of file descriptors

    Lesson 08

    Write a loop that calls pipe() until it fails. Which errno do you get, and how many pipes did you make? Compare with the "open files" limit printed by whoami-sandbox.c.

Open Full Terminal

Get a Linux terminal with all 4 files from this lesson pre-loaded. Compile, run, and experiment freely.