# Reviving ApplixWare on FreeBSD 15.1

ApplixWare was a commercial office suite for UNIX. 
It originated from an earlier UNIX office environment called Alis in the 1980s, was renamed Aster*x around 1991, and became ApplixWare in 1993. It was designed primarily for UNIX workstations before being ported to Linux. There was even a release for FreeBSD.

## 1. ApplixWare Jail

I already have Bastille configured on a FreeBSD 15.1 amd64 server.
The below commands run as root on that host. Substitute an unused address on your
network for `192.168.3.51/24`.

Create and start a FreeBSD 15.1 jail named `applix151` with bastille:

```sh
bastille create applix151 15.1-RELEASE 192.168.3.51/24
bastille start applix151
```

Throughout the post, `jexec applix151 ...` means “run this command inside the
jail.” With `jexec applix151 sh -c '...'`, the quoted shell script runs there.
Commands beginning with `bastille` also run on the host; `bastille cmd` runs a
command in the named jail.


## 2. VNC Desktop

Install a minimal set of packages inside the jail:

```sh
bastille pkg applix151 install -y \
  tigervnc-server xset xauth xsetroot xterm font-misc-misc fluxbox curl
```

This gives us an ordinary amd64 jail and the packages needed for a small
graphical desktop.

ApplixWare is graphical, but the server does not need a physical monitor.
TigerVNC provides a virtual X display inside the jail and sends its desktop to
a VNC viewer on another computer. X11 is the window system the application
uses; Fluxbox supplies window borders and lets us move windows around.

The packages from the previous step provide that desktop. `DISPLAY=:1` tells
an X application to use the virtual display we are about to start. The VNC
viewer reaches the same desktop over network port `5901`.

The `xterm` package is included because it is the simplest X client to launch
inside the VNC session when debugging whether the display itself works.  The
`vncserver` wrapper was not a good fit for this minimal jail: its generated
`xstartup` tried to run `xsetroot`, `xterm`, and `twm`, and the wrapper exited
when those were missing.  Instead, run TigerVNC's `Xvnc`
directly with password authentication:

```sh
jexec applix151 sh -c '
  install -d -m 700 /root/.vnc
  pw=$(openssl rand -base64 18)
  printf "%s\n" "$pw" | vncpasswd -f > /root/.vnc/passwd
  chmod 600 /root/.vnc/passwd
  printf "%s\n" "$pw" > /root/.vnc/vnc-password
  chmod 600 /root/.vnc/vnc-password

  daemon -p /var/run/xvnc.pid -o /root/.vnc/xvnc.log \
    /usr/local/bin/Xvnc :1 \
    -geometry 800x600 \
    -depth 16 \
    -rfbport 5901 \
    -SecurityTypes VncAuth \
    -rfbauth /root/.vnc/passwd \
    -AlwaysShared=1 \
    -AcceptSetDesktopSize=0 \
    -localhost no \
    -fp /usr/local/share/fonts/misc,built-ins
'
```

The default `800x600` geometry is small enough for old Applix dialogs and most
remote viewers while still leaving the full application visible.  The remaining
options are:

- `-rfbport 5901` selects the VNC client port.
- `-AcceptSetDesktopSize=0` prevents a viewer from resizing the desktop and
  moving old Applix dialogs outside the visible framebuffer.  During debugging,
  a VNC client resized the server away from the intended geometry; Applix and
  Fluxbox were still running, but windows landed partly outside the visible
  framebuffer.  Disabling client desktop resize keeps the VNC desktop stable.
- `-localhost no` permits remote connections; expose this port only on a
  trusted network or through an appropriate firewall or tunnel.

Running `Xvnc` directly avoids relying on `vncserver` and a generated
`xstartup` file: in the minimal jail, that wrapper failed when its expected X
startup programs were absent.

When `built-ins` was missing, `xsetroot -cursor_name left_ptr` failed with:

```text
X Error of failed request:  BadName (named color or font does not exist)
Major opcode of failed request:  45 (X_OpenFont)
```

`BadName` is an X server error.  Here it means the server could not resolve the
named cursor font that `xsetroot` needs for `left_ptr`.  `X_OpenFont` is the X11
request being attempted.  Restoring `built-ins` to the font path gives the X
server its built-in cursor font names again.

### Initialize the desktop

After `Xvnc` is ready, restore its font path, set a visible cursor, and start
Fluxbox:

```sh
jexec applix151 sh -c '
  env DISPLAY=:1 xset fp= /usr/local/share/fonts/misc,built-ins
  env DISPLAY=:1 xset fp rehash
  env DISPLAY=:1 xsetroot -cursor_name left_ptr
  env DISPLAY=:1 fluxbox >/root/.vnc/fluxbox.log 2>&1 &
'
```

Without the `xsetroot` call, TigerVNC accepted connections but the pointer was
invisible.  The visible cursor is not cosmetic during debugging; it is the
confirmation that the X font path still contains the cursor font and that the
viewer is interacting with the intended display.  The `built-ins` entry is
required by `xsetroot -cursor_name left_ptr`; without it, `X_OpenFont` can fail
with `BadName` as shown above.

### Connect and verify the desktop

```sh
jexec applix151 env DISPLAY=:1 xset q
jexec applix151 sockstat -4 -l | grep 5901
```

`xset q` proves that the jail can connect to X display `:1`.  `sockstat` proves
that the VNC server is listening on port `5901`.  Optionally launch an X
terminal before testing ApplixWare, as the simplest check that the display
accepts clients:

```sh
jexec applix151 sh -c 'env DISPLAY=:1 xterm >/dev/null 2>&1 &'
```

The VNC password is stored in:

```sh
jexec applix151 cat /root/.vnc/vnc-password
```

Connect your VNC viewer to `192.168.3.51:5901` using that password. Seeing
Fluxbox and the test terminal establishes that a modern application can draw
windows here. Leave this desktop running while we investigate Applix.

If startup fails, inspect the server log:

```sh
jexec applix151 tail -50 /root/.vnc/xvnc.log
```

## 3. Download the CD and try its launcher

With the desktop ready, we can bring in the application. The preserved image
is `Applixware_Office_for_FreeBSD_Walnut_Creek_November_1999.iso`.

Download the Walnut Creek CD image on the host, with resume support:

```sh
ISO_URL=https://archive.org/download/Applixware_Office_for_FreeBSD_Walnut_Creek_November_1999/Applixware_Office_for_FreeBSD_Walnut_Creek_November_1999.iso
ISO=Applixware_Office_for_FreeBSD_Walnut_Creek_November_1999.iso

(
set -e
if [ ! -s "$ISO" ]; then
  if [ -s "$ISO.part" ]; then
    fetch -r -o "$ISO.part" "$ISO_URL"
  else
    fetch -o "$ISO.part" "$ISO_URL"
  fi
  mv "$ISO.part" "$ISO"
fi
)
```

The `.part` file makes an interrupted download resumable: `fetch -r` continues a
partial file, and the completed image is renamed to its final filename only
after `fetch` succeeds.

FreeBSD's `tar` can read the ISO filesystem directly.  Extract it with `tar`:

```sh
mkdir -p tmp/applix-iso-extract unzip/applixware1999cdrom
tar -xf "$ISO" -C tmp/applix-iso-extract
(cd tmp/applix-iso-extract && tar -cf - .) |
  (cd unzip/applixware1999cdrom && tar -xpf -)
test -x unzip/applixware1999cdrom/applix
```

The second `tar` pipeline copies the extracted CD tree while preserving its
permissions.  The final `test` verifies that the Applix launcher exists and is
executable.

The extracted files now live on the host at `unzip/applixware1999cdrom`.  The
rest of this guide runs Applix from `/applixware1999cdrom` inside the jail, so
copy the tree into the jail root.  The server used in this investigation stores the jail at:

```sh
JAIL_ROOT=/mnt/tank/finch/usr/local/bastille/jails/applix151/root
```

Adjust `JAIL_ROOT` if the jail is stored elsewhere, then copy the tree to the
path that will appear as `/applixware1999cdrom` inside the jail:

```sh
mkdir -p "$JAIL_ROOT/applixware1999cdrom"
(cd unzip/applixware1999cdrom && tar -cf - .) |
  (cd "$JAIL_ROOT/applixware1999cdrom" && tar -xpf -)

bastille cmd applix151 test -x /applixware1999cdrom/applix
```

This is an extracted copy of the CD contents, not the final writable ApplixWare
installation; we will make a separate runtime tree when we reach application
startup.

### The first attempt

The jail so far is an ordinary amd64 FreeBSD 15.1 system, so run the original
launcher directly and read what actually happens:

```sh
jexec applix151 /applixware1999cdrom/applix
ELF interpreter /usr/libexec/ld-elf.so.1 not found, error 8
Abort trap
```

ELF is the executable-file format used here. A dynamically linked binary
names a loader, the program that brings it and its shared libraries into memory.  This
launcher expects `/usr/libexec/ld-elf.so.1`, and on this amd64 jail that path is
the 64-bit loader.  `error 8` is `ENOEXEC`, "Exec format error": the kernel
opened the file, but the named interpreter cannot start this binary.  The shell
also reported the death as a signal: `Abort trap` is signal 6 (`SIGABRT`),
reported as status `134` (`128 + 6`).

Check what kind of executable this really is instead of guessing:

```sh
jexec applix151 file /applixware1999cdrom/applix
/applixware1999cdrom/applix: ELF 32-bit LSB executable, Intel i386, version 1 (SYSV), dynamically linked, interpreter /usr/libexec/ld-elf.so.1, not stripped
```

The binary is 32-bit i386, and its interpreter path points at a 64-bit loader,
so the sizes do not match and the kernel refuses to run it. The `ldd` tool
lists shared-library dependencies. Here it confirms the
same boundary from userland, because FreeBSD's `ldd` delegates a 32-bit binary
to the helper `/usr/bin/ldd32`, which is not installed yet:

```text
ldd: /usr/bin/ldd32: No such file or directory
```

FreeBSD can run 32-bit binaries on amd64 only when the 32-bit loader and
libraries are available, so the evidence now justifies installing them.  The
`FreeBSD-base` repository contains packaged base-system components and is
separate from the ordinary ports/packages repository; it is disabled by
default, so enable it first, then install the `FreeBSD-set-lib32` package:

```sh
bastille cmd applix151 sh -c '
  mkdir -p /usr/local/etc/pkg/repos/
  echo "FreeBSD-base: { enabled: yes }" > /usr/local/etc/pkg/repos/FreeBSD.conf
  pkg install -y FreeBSD-set-lib32
'
```

Verify the three installed pieces are present:

```sh
jexec applix151 ls -l /libexec/ld-elf32.so.1 /usr/bin/ldd32 /usr/lib32/libc.so.7
```

Their roles are:

- `/libexec/ld-elf32.so.1`: loads 32-bit FreeBSD ELF programs.
- `/usr/bin/ldd32`: lets the normal `ldd` command inspect 32-bit executables.
- `/usr/lib32/libc.so.7`: the modern 32-bit FreeBSD C library.

`FreeBSD-set-lib32` supplies the amd64 system's compatibility runtime for
executing 32-bit FreeBSD programs.  It does not supply ApplixWare's old
FreeBSD 3.x libraries; those are staged in Section 4.

Run the launcher again with the 32-bit runtime installed:

```sh
jexec applix151 /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libX11.so.6" not found, required by "applix"
```

This message is from the 32-bit runtime loader, `ld-elf32.so.1`, which the
`FreeBSD-set-lib32` package installed.  It has opened the Applix launcher and
read its shared-library dependency list, but it cannot find `libX11.so.6` in
the 32-bit library search path.  `libX11.so.6` is the X11 client library;
without it, a graphical X application cannot even finish dynamic linking.
`ldd` on a 32-bit executable now delegates to the helper `/usr/bin/ldd32`.

To find the missing `libX11.so.6`, start with the library name reported by the
loader and match the library to the operating-system ABI Applix was built for.
The FreeBSD 3.5.1 i386 package archive contains XFree86 4.0's `libX11.so.6`.
Using that library keeps X11 on the same FreeBSD 3.x side of the compatibility
boundary as Applix's `libc.so.3` and `libm.so.2`.

## 4. Follow the missing-library errors

ApplixWare's launcher now runs far enough for the dynamic loader to report the
old libraries it cannot find.  The commands
below obtain them from public FreeBSD release and package sources in the same
order the failures force them.

Start with a private library area.  `/usr/local/lib32/compat` holds the old
FreeBSD libraries.  `/usr/local/lib32/compat/applix` is the first directory in
ApplixWare's `LD_32_LIBRARY_PATH`, so it can contain the exact Applix-facing
view of the runtime.

```sh
jexec applix151 mkdir -p \
  /usr/local/lib32/compat \
  /usr/local/lib32/compat/applix \
  /usr/local/lib
```

The first direct-launch failure after adding 32-bit base support is
`libX11.so.6`, so add the matching FreeBSD 3.5.1 i386 X11 library next.  The
archived package is `XFree86-libraries-4.0.tgz`; although the package contains
the complete XFree86 library kit, only its `libX11` shared-library link and
versioned target are needed here.

Download the archived package without installing it into the jail package
database:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  archive=$work/freebsd351-packages
  mkdir -p "$archive"
  fetch -o "$archive/XFree86-libraries-4.0.tgz" \
    https://archive.freebsd.org/old-releases/i386/3.5.1-RELEASE/packages/All/XFree86-libraries-4.0.tgz
'
```

The package prefix is `/usr/X11R6`, so its archive members begin with `lib/`.
Confirm the two members and extract them directly into the Applix-first compat
directory.  `--strip-components 1` removes `lib/` and retains each filename:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  package=$work/freebsd351-packages/XFree86-libraries-4.0.tgz
  applix=/usr/local/lib32/compat/applix
  mkdir -p "$applix"
  tar -tzvf "$package" "lib/libX11.so*"
  tar -xzvf "$package" -C "$applix" --strip-components 1 \
    "lib/libX11.so*"
'
```

The `readelf -d` command reads the library’s dynamic-linking metadata. Inspect
the extracted library:

```sh
jexec applix151 readelf -d /usr/local/lib32/compat/applix/libX11.so.6
```

Its SONAME, the name used to identify the shared library, is `libX11.so.6`.
It has no `DT_NEEDED` entries naming further library dependencies.  Unlike modern
`libX11`, this version does not introduce `libxcb`,
`libXau`, or `libXdmcp` into the process.  It speaks the stable X11 wire
protocol successfully to the FreeBSD 15.1 jail's Xvnc server.

Register those directories with the 32-bit runtime linker cache.  This is the
32-bit form of `ldconfig`; plain `ldconfig` only updates the native 64-bit
linker cache on this host:

```sh
jexec applix151 ldconfig -32 \
  /usr/lib32 \
  /usr/local/lib32/compat \
  /usr/local/lib32/compat/applix \
  /applixware1999cdrom/axdata/axshlib
```

Verify that the 32-bit linker cache can now see the copied X11 library:

```sh
jexec applix151 ldconfig -32 -r | grep 'libX11\.so\.6'
```

Then try the launcher again with only the X11 library added:

```sh
# jexec applix151 /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libm.so.2" not found, required by "applix"
```

The failure moves forward.  The 32-bit X11 client library is now present, so
the loader reaches the older FreeBSD 3.x libraries Applix was linked against.

This is the same kind of dynamic-loader failure as `libX11.so.6`, but now the
missing library is from old FreeBSD base instead of X11.  `libm.so.2` is the old
math library ABI Applix was linked against.  The modern jail has newer 32-bit
libraries, but not this FreeBSD 3.x SONAME.

To identify the source, use the SONAME: `libm.so.2` is too old for modern
FreeBSD pkgbase, and Applix is already known to be a FreeBSD 3.x-era binary.
The matching place to look is the final FreeBSD 3.x i386 release archive:

```text
https://archive.freebsd.org/old-releases/i386/3.5.1-RELEASE/bin/
```

Inside that split `bin` distribution, the file is
`usr/lib/libm.so.2`.

`ldd` shows the remaining direct old-library failures:

```sh
jexec applix151 sh -c '
  env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    ldd /applixware1999cdrom/applix
'
```

```text
libm.so.2 => not found
libc.so.3 => not found
```

`libc.so.3` is the old FreeBSD 3.x C library ABI.  Once `ldd` reports both
`libm.so.2` and `libc.so.3` as `not found`, the next fix is no longer about X11;
it is about staging the old FreeBSD 3.x runtime libraries that Applix was built
to use.

`libc.so.3` is the central C runtime Applix was linked against: startup code,
system-call wrappers, stdio, memory allocation, account lookup, environment
handling, and many other basic C interfaces.  The matching source is the same
FreeBSD 3.5.1 i386 `bin` distribution as `libm.so.2`, where it lives as
`usr/lib/libc.so.3`.  A FreeBSD 4.11 `compat3x` archive also contains
`libc.so.3`, but the FreeBSD 3.5.1 `bin` archive is preferred here because it
also supplies the matching ELF `libm.so.2` needed by the same Applix binary.

Now fetch the archived FreeBSD 3.5.1 i386 base distribution rather than a newer
compatibility package:

```text
https://archive.freebsd.org/old-releases/i386/3.5.1-RELEASE/bin/
```

FreeBSD 3.5.1 was the final FreeBSD 3.x release, so it is a good source for the
latest FreeBSD 3 userland ABI libraries.  The release archive is split into
`bin.??` chunks.  For this release the real chunk range is `bin.aa` through
`bin.dz`, plus `bin.ea` through `bin.eg`.  Download the checksum file and those
chunks with `curl`:

```sh

jexec applix151 sh -c '
  set -eu
  pkg install -y curl;
  work=/var/tmp/applix-compat-libs
  src=https://archive.freebsd.org/old-releases/i386/3.5.1-RELEASE/bin
  mkdir -p "$work/freebsd351-bin"
  cd "$work/freebsd351-bin"
  curl -fL --retry 3 -O "$src/CHECKSUM.MD5"
  curl -fL --retry 3 -O "$src/bin.[a-d][a-z]"
  curl -fL --retry 3 -O "$src/bin.e[a-g]"
'
```

Verify the chunks before extracting anything; do not concatenate or extract the
archive until all chunk checksums match:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  cd "$work/freebsd351-bin"
  awk "/^MD5 \\(bin\\./ { gsub(/[()]/, \"\", \\$2); print \\$2, \\$4 }" CHECKSUM.MD5 |
  while read -r file expected; do
    actual=$(md5 -q "$file")
    [ "$actual" = "$expected" ] || {
      echo "checksum mismatch for $file: $actual != $expected" >&2
      exit 1
    }
  done
'
```

No output means every chunk listed in `CHECKSUM.MD5` matched.

Confirm that the old FreeBSD 3.x C and math libraries needed by the top-level
`applix` launcher are inside the archive:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  cd "$work/freebsd351-bin"
  cat bin.* | tar -tzf - \
    "usr/lib/lib[mc].so*"
'
```

Extract only those launcher libraries directly into the Applix-first compat
directory.  These archive paths do not have a leading `/`, so
`--strip-components 2` removes `usr/lib` and leaves the library filenames:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  applix=/usr/local/lib32/compat/applix
  mkdir -p "$applix"
  cd "$work/freebsd351-bin"
  cat bin.* | tar -xzvf - -C "$applix" --strip-components 2 \
    "usr/lib/lib[mc].so*"
'
```

Refresh the cache again and verify all direct launcher dependencies:

```sh
jexec applix151 ldconfig -32 \
  /usr/lib32 \
  /usr/local/lib32/compat \
  /usr/local/lib32/compat/applix \
  /applixware1999cdrom/axdata/axshlib

jexec applix151 sh -c '
  env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    ldd /applixware1999cdrom/applix
'
```

Expected key lines:

```text
libX11.so.6 => /usr/local/lib32/compat/applix/libX11.so.6
libm.so.2 => /usr/local/lib32/compat/applix/libm.so.2
libc.so.3 => /usr/local/lib32/compat/applix/libc.so.3
```

Run Applix again with the 32-bit library path:

```sh
jexec applix151 sh -c '
  env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    /applixware1999cdrom/applix
'
```

The loader is now satisfied, so the next failure is inside runtime startup:

```text
# /applixware1999cdrom/applix
Bad system call
return code=140, signal=12
```

We have moved past missing library files. The next question is which system
call caused the process to stop.

On FreeBSD, `Bad system call` corresponds to `SIGSYS`:

```sh
# awk '$3 == 12' /usr/include/sys/signal.h
#define SIGSYS          12      /* non-existent system call invoked */
```

Shells commonly encode signal termination as `128 + signal`, so status 140 is
`128 + 12`. The `truss` tool records calls from the process into the kernel.
Tracing this launch without a preload identifies the obsolete interface:

```text
compat.freebsd32_sigaction() ERR#78 'Function not implemented'
SIGNAL 12 (SIGSYS) code=SI_KERNEL
process killed, signal = 12
```

`ERR#78` is `ENOSYS`: FreeBSD 3.x libc attempted its old i386 `sigaction`
system-call interface, which FreeBSD 15.1 no longer implements in that form.
The kernel then delivered `SIGSYS`.  This is the first runtime boundary the
shim must translate.

## 5. Investigate the runtime failures

The old libraries get the process loaded, but they do not make old libc behavior
correct on a FreeBSD 15.1 kernel and jail.  `libc3_shim.c` is a preload shim:
it is a small shared library named in `LD_32_PRELOAD`, so the dynamic loader
loads it before the old `libc.so.3`.  That lets the shim intercept only the few
functions that need help while leaving the real `libc.so.3` in place.  In this
guide, "broken" means "the FreeBSD 3.x libc assumption is no longer true on
FreeBSD 15.1", not that `libc.so.3` was wrong for its original system.

The boundary here is the ABI, or application binary interface: the function
names, data layouts, and calling conventions that compiled code expects.
Finding a library with the right filename gets us through loading; its calls
must also make sense to the current system.

FreeBSD 15.1's C library is named `libc.so.7`. Below, “libc3” means the old
`libc.so.3` we extracted, and “libc7” means the modern 32-bit C library installed
with the base compatibility runtime. We will use modern implementations where
needed and translate the old arguments before passing them along.

### How a preload shim interposes

A dynamically linked executable does not contain all of the library code it
uses.  Its ELF dynamic section names required shared objects with `DT_NEEDED`
entries.  At startup, the runtime loader:

1. maps the executable into memory;
2. loads its required shared objects;
3. connects undefined dynamic symbols, such as `sigaction`, to definitions in
   the loaded objects; and
4. applies the relocations needed to call those definitions.

Normally, the loader searches the executable and its ordinary dependencies in
its standard global symbol order.  `LD_PRELOAD` asks it to load an additional
shared object before those ordinary dependencies.  A definition in the
preloaded object can therefore be selected before a definition with the same
name in libc.  This behavior is called **symbol interposition**.

Without a preload, the lookup for `sigaction` resolves to old libc3, which then
reaches the obsolete kernel interface:

```text
Applix reference to sigaction
              |
              v
FreeBSD 3.5.1 libc.so.3: sigaction
              |
              v
obsolete kernel interface -> ENOSYS -> SIGSYS
```

With the shim preloaded, the first matching dynamic definition changes:

```text
Applix reference to sigaction
              |
              v
preloaded libc3_shim.so: sigaction   <- selected first
              |
              v
translate the old structure
              |
              v
modern 32-bit libc.so.7: sigaction
```

Preloading affects dynamically resolved symbols only.  It does not rewrite the
executable, replace direct system calls, or automatically make incompatible
data structures compatible; the shim must export the expected name and perform
the required ABI translation itself.

Why `LD_32_PRELOAD` and not `LD_PRELOAD`?  FreeBSD amd64 distinguishes native
and 32-bit loader variables:

```text
LD_PRELOAD       native 64-bit processes
LD_32_PRELOAD    32-bit processes
```

ApplixWare is a 32-bit i386 executable, so plain `LD_PRELOAD` is the wrong
variable.  `LD_32_PRELOAD` names one or more 32-bit shared objects that
`ld-elf32.so.1` must load before the executable's normal dependencies.  It does
not remove `libc.so.3` (only the selected symbol is interposed), and it does not
add library search directories — `LD_32_LIBRARY_PATH` continues to be used for
that.

The shim cannot call libc7 through its own exported name.  Calling `sigaction`
by name from inside the shim would resolve back to the shim's own interposed
function and recurse.  The shim instead opens an explicit handle to
`libc.so.7` with `dlopen()` and retains libc7's real functions with `dlsym()`
function pointers, which select the intended implementations directly.

Start a shim source with the normal headers, a handle for modern `libc.so.7`,
and the old-to-new signal ABI translation required by the first failure.

We will save successive versions as `libc3-shim-01.c` through
`libc3-shim-04.c` inside `/root` in the jail. The first block is a complete
source file; later blocks explain what to add. Appendix A contains the final,
combined source for reference.

### Stage 01: the signal ABI

The `SIGSYS` from the trace is the first kernel boundary worth explaining
before writing the wrapper.  The generated 32-bit syscall table on the host
identifies the failing entry and its guard:

```text
46 = compat.freebsd32_sigaction
```

The entry is guarded by the old `COMPAT_43` kernel option.  The generated table
defines its `compat()` selector as:

```c
#ifdef COMPAT_43
#define compat(n, name) \
    .sy_narg = n, .sy_call = (sy_call_t *)__CONCAT(o, name)
#else
#define compat(n, name) \
    .sy_narg = 0, .sy_call = (sy_call_t *)nosys
#endif
```

Check the running kernel configuration:

```sh
sysctl -n kern.conftxt | grep COMPAT_43
```

It produces no output: this kernel does not include `COMPAT_43`; old syscall 46
points to `nosys`, which returns `ENOSYS` (`ERR#78`).  This explains the trace
without assuming that libc3 itself was defective on FreeBSD 3.x — its kernel
interface is simply no longer enabled.

Calling modern libc with the old structure pointer unchanged would be unsafe.
The old 32-bit action layout used by syscall 46 is 12 bytes in the order
`handler, mask, flags`:

```c
struct osigaction32 {
    uint32_t  sa_u;       /* 32-bit handler pointer */
    osigset_t sa_mask;    /* one 32-bit mask word */
    int       sa_flags;
};
```

The current 32-bit layout is 24 bytes in the order `handler, flags, mask[4]`,
because modern `sigset_t` contains four 32-bit words:

```c
#define _SIG_WORDS 4
typedef struct __sigset {
    __uint32_t __bits[_SIG_WORDS];
} __sigset_t;
```

The shim must preserve the old layout at its exported entry point, construct
the modern layout before calling libc7, and translate an output action back to
the old layout. The launcher’s dynamically resolved `sigaction` reference
allows the preload to intercept the call.

For this entry, the difference is:

- libc3 side: `sigaction()` uses the old i386 layout `handler, mask, flags`,
  with one 32-bit mask word; `sigprocmask()` likewise uses one mask word.
- libc7 / FreeBSD 15.1 side: the modern i386 layout is `handler, flags,
  mask[4]`.
- shim behavior: translate old structures to the modern layout, call the
  corresponding libc7 function, and translate returned values back.
- create in the shim: resolve modern `sigaction()`, `sigprocmask()`, and
  `signal()`; export the two translated interfaces; and ignore `SIGSYS` during
  initialization as protection while bringing up other old syscall paths.

```c
#include <dlfcn.h>
#include <string.h>

#ifndef RTLD_LOCAL
#define RTLD_LOCAL 0
#endif

static void *libc7_h;
static int (*rl_sigaction)(int, const void *, void *);
static int (*rl_sigprocmask)(int, const void *, void *);

#define APPLIX_SIGSYS 12
#define APPLIX_SIG_IGN ((void (*)(int))1)

static void (*(*rl_signal)(int, void (*)(int)))(int);

struct old_sigaction {
    void *handler;
    unsigned int mask;
    int flags;
};

struct new_sigaction {
    void *handler;
    int flags;
    unsigned int mask[4];
};

static void
libc7_init(void)
{
    if (libc7_h)
        return;

    libc7_h = dlopen("libc.so.7", RTLD_LAZY | RTLD_LOCAL);
    if (!libc7_h)
        return;

    rl_sigaction = dlsym(libc7_h, "sigaction");
    rl_sigprocmask = dlsym(libc7_h, "sigprocmask");
    rl_signal = dlsym(libc7_h, "signal");

    if (rl_signal)
        rl_signal(APPLIX_SIGSYS, APPLIX_SIG_IGN);
}

int
sigaction(int sig, const struct old_sigaction *act, struct old_sigaction *oact)
{
    struct new_sigaction m_act;
    struct new_sigaction m_oact;
    struct new_sigaction *ap = NULL;
    struct new_sigaction *op = NULL;
    int ret;

    if (!libc7_h)
        libc7_init();

    if (act) {
        memset(&m_act, 0, sizeof(m_act));
        m_act.handler = act->handler;
        m_act.flags = act->flags;
        m_act.mask[0] = act->mask;
        ap = &m_act;
    }

    if (oact)
        op = &m_oact;

    ret = rl_sigaction ? rl_sigaction(sig, ap, op) : -1;
    if (ret == 0 && oact && op) {
        oact->handler = m_oact.handler;
        oact->flags = m_oact.flags;
        oact->mask = m_oact.mask[0];
    }

    return ret;
}

int
sigprocmask(int how, const unsigned int *set, unsigned int *oset)
{
    unsigned int m_set[4];
    unsigned int m_oset[4];
    void *sp = NULL;
    void *op = NULL;
    int ret;

    if (!libc7_h)
        libc7_init();

    memset(m_set, 0, sizeof(m_set));
    memset(m_oset, 0, sizeof(m_oset));

    if (set) {
        m_set[0] = *set;
        sp = m_set;
    }

    if (oset)
        op = m_oset;

    ret = rl_sigprocmask ? rl_sigprocmask(how, sp, op) : -1;
    if (ret == 0 && oset && op)
        *oset = m_oset[0];

    return ret;
}
```

The launch also needs Applix's runtime locations: `AXHOME` is the application
root, `AXGFSDIR` points to its font metrics, `AXLANG` selects the language, and
`AX_TEMP_DIR` selects temporary storage. `-wp` requests Words, its word processor.

Use one shell function for every stage so every launch records both the raw
return code and the signal calculated as `return code - 128`:

```sh
run_shim()
{
  shim=$1
  jexec applix151 env \
    LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    LD_32_PRELOAD="$shim" \
    AXHOME=/applixware1999cdrom \
    AXGFSDIR=/applixware1999cdrom/axdata/fontmetrics/gallium \
    AXLANG=english_us \
    AX_TEMP_DIR=/tmp \
    DISPLAY=:1 \
    timeout 10 /applixware1999cdrom/applix -wp
  rc=$?
  printf 'return code=%s, signal=%s\n' "$rc" "$((rc >= 128 ? rc - 128 : 0))"
}
```

Save the complete first-stage block as `/root/libc3-shim-01.c`, then build it:

```sh
jexec applix151 cc -m32 -shared -fPIC -Wall -Wextra \
  -o /usr/local/lib/libc3-shim-01.so \
  /root/libc3-shim-01.c
```

The stage ships both `sigaction()` and `sigprocmask()` together.  The original
investigation tested the `sigaction` translation alone first; startup then
reached old syscall 48 (`compat.sigprocmask`), an identical `COMPAT_43 -> nosys
-> ENOSYS` boundary, so the second wrapper was added before recording a stage.
The kernel matches this: with the trace, `compat.sigprocmask()` reported
`ERR#78 ENOSYS`, and the failed call was the only evidence needed before
`nm -D` was used to confirm the launcher imports it.

Confirm the new library exports the expected surface before running it:

```sh
jexec applix151 nm -D /usr/local/lib/libc3-shim-01.so |
  egrep ' (sigaction|sigprocmask)$'
```

Expected symbol types:

```text
T sigaction
T sigprocmask
```

`T` means the shared object globally defines executable code with that name;
the addresses printed by `nm` vary between builds. These exports confirm the
wrappers are present; they do not establish the object’s architecture. The
`-m32` build option selects 32-bit output, which can also be checked with
`file`. Run it with the common capture function:

```sh
run_shim /usr/local/lib/libc3-shim-01.so
```

Result:

```text
get user name: No such file or directory
return code=1, signal=0
```

`get user name: No such file or directory` is an Applix-facing symptom of a
failed account lookup.  The process is now far enough into startup to ask libc
"who is the current user?", but old libc cannot read the modern jail account
database correctly.  FreeBSD 3.x libc expected the passwd/spwd database formats
and Berkeley DB details of its own time.  A FreeBSD 15.1 jail has different
passwd/spwd database behavior, so old `getpwuid()` can fail even though the user
exists.

### Stage 02: verifying the failing call

The printed suffix is not stable: another run produced `get user name: Invalid
argument`.  That variation is `perror(3)` appending `strerror(errno)` to the
same compiled-in label, so the suffix is a clue, not the message:

```sh
jexec applix151 egrep '^#define[[:space:]]+(ENOENT|EINVAL)' /usr/include/sys/errno.h
```

```text
#define ENOENT          2       /* No such file or directory */
#define EINVAL          22      /* Invalid argument */
```

Find where the label lives rather than guessing from the English message.
`strings` prints the absolute file offset of the string:

```sh
jexec applix151 strings -a -t x /applixware1999cdrom/applix | grep 'get user name'
```

```text
  10693 get user name
```

Map that offset into the executable's virtual address space using the `.data`
section line from `readelf`:

```sh
jexec applix151 readelf -S /applixware1999cdrom/applix | grep ' \.data '
```

```text
[12] .data PROGBITS 08058c24 00fc24 001438 00 WA 0 0 4
```

`.data` is `PROGBITS` (it occupies actual file bytes), starts at file offset
`0xfc24`, and maps to virtual address `0x8058c24`.  The string offset
`0x10693` lies inside the section (`0xfc24 <= 0x10693 < 0xfc24 + 0x1438`), so
the runtime address is:

```text
runtime address = section virtual address + (string file offset - section file offset)
                = 0x8058c24 + (0x10693 - 0xfc24)
                = 0x8059693
```

Search the disassembly for that address to find the code that pushes it:

```sh
jexec applix151 sh -c \
  'objdump -dr /applixware1999cdrom/applix | grep -B 12 -A 4 8059693 | egrep -v "addl|movl"'
```

```text
 804c7dc: e8 27 ed ff ff                calll   0x804b508 <getuid@plt>
 804c7e3: 50                            pushl   %eax
 804c7e4: e8 bf ef ff ff                calll   0x804b7a8 <getpwuid@plt>
 804c7f4: 83 bd 70 cc ff ff 00          cmpl    $0x0, -0x3390(%ebp)
 804c7fb: 75 17                         jne     0x804c814 <FindAxMain+0x80>
 804c7fd: 68 93 96 05 08                pushl   $0x8059693              # imm = "get user name"
 804c802: e8 d1 ec ff ff                calll   0x804b4d8 <perror@plt>
 804c80a: 6a 01                         pushl   $0x1
 804c80c: e8 03 18 00 00                calll   0x804e014 <ThimSafeExit>
```

This is where the names come from.  The code loads the current UID, calls
`getpwuid@plt`, tests the return value, and on `NULL` calls
`perror("get user name")` and exits, giving the static control flow:

```text
uid = getuid()
pw = getpwuid(uid)
if (pw == NULL) {
    perror("get user name")
    exit(1)
}
```

Only after those names appear in the code should `nm -D` confirm they are
imports to be satisfied by a loaded library:

```sh
jexec applix151 nm -D /applixware1999cdrom/applix | egrep ' U (getpwuid|perror)$'
```

```text
         U getpwuid
         U perror
```

Disassembly shows what the program could do. DTrace lets us observe function
calls and return values in a running process. The recorded probe checks whether
this is the branch actually taken. Trace `getpwuid`, `dbopen`, `perror`, and `strerror` entry/return:

```sh
jexec applix151 sh -c '
  cat >/root/02-getpwuid.d <<'\''DTRACE'\''
#pragma D option quiet

pid$target::getpwuid:entry
{ printf("%s`%s(%u)\n", probemod, probefunc, (uint32_t)arg0); }

pid$target::getpwuid:return
{ printf("%s`%s returned 0x%lx\n", probemod, probefunc, (uintptr_t)arg1); }

pid$target::dbopen:entry
{ printf("%s`%s(\"%s\")\n", probemod, probefunc, copyinstr((uintptr_t)arg0)); }

pid$target::perror:entry
{ printf("%s`%s(\"%s\")\n", probemod, probefunc, copyinstr((uintptr_t)arg0)); }

pid$target::strerror:entry
{ printf("%s`%s(%d)\n", probemod, probefunc, (int)arg0); }
DTRACE

  env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    LD_32_PRELOAD=/usr/local/lib/libc3-shim-01.so \
    AXHOME=/applixware1999cdrom \
    AXGFSDIR=/applixware1999cdrom/axdata/fontmetrics/gallium \
    AXLANG=english_us AX_TEMP_DIR=/tmp DISPLAY=:1 \
    dtrace -q -x evaltime=postinit \
      -s /root/02-getpwuid.d -c "/applixware1999cdrom/applix -wp"
'
```

The relevant measured output:

```text
get user name: Invalid argument
libc.so.3`getpwuid(0)
libc.so.3`dbopen("/etc/spwd.db")
libc.so.3`getpwuid returned 0x0
libc.so.3`perror("get user name")
libc.so.3`strerror(22)
```

This is the complete account-lookup failure: libc3's `getpwuid(0)` opens
`/etc/spwd.db`, but returns `NULL` even though the user exists.  The modern jail
can answer the same query:

```sh
jexec applix151 getent passwd 0
jexec applix151 ls -l /etc/spwd.db /usr/lib32/libc.so.7
```

```text
root:*:0:0:Charlie &:/root:/bin/sh
-rw------- 1 root wheel   40960 ... /etc/spwd.db
-r--r--r-- 1 root wheel 1920200 ... /usr/lib32/libc.so.7
```

The smallest compatibility change is therefore to intercept `getpwuid()` and
delegate it to the modern 32-bit `libc.so.7`, exactly as the account lookup
shows.  `getpwnam()` is included in the final shim because later modules use
by-name lookups against the same old database reader; the `dlopen()`/`dlsym()`
pattern below applies to both.

Add `getpwuid()` and `getpwnam()` wrappers that delegate directly to modern
`libc.so.7` through `dlopen()`/`dlsym()`.

This avoids the FreeBSD 3.x password database reader, which cannot reliably
parse the modern jail's passwd/spwd DB format.  Applix just needs a normal
passwd entry for the current user.

For this entry, the difference is:

- libc3 side: `getpwuid()` and `getpwnam()` use the old FreeBSD 3.x account
  database code and file/database assumptions.
- libc7 / FreeBSD 15.1 side: account lookup understands the jail's current
  passwd/spwd database format and NSS behavior.
- shim behavior: export the same old function names, but delegate the lookup to
  `libc.so.7` and return the modern result to Applix.
- create in the shim: export `struct passwd *getpwuid(uid_t)` and
  `struct passwd *getpwnam(const char *)`, resolve the real modern functions
  with `dlsym()`, and fall back to `errno = ENOENT` only if delegation is not
  available.

```c
#include <sys/types.h>
#include <errno.h>
#include <pwd.h>

static struct passwd *(*rl_getpwuid)(uid_t);
static struct passwd *(*rl_getpwnam)(const char *);
```

Add these two assignments to `libc7_init()` after the signal-symbol lookups:

```c
    rl_getpwuid = dlsym(libc7_h, "getpwuid");
    rl_getpwnam = dlsym(libc7_h, "getpwnam");
```

Then add the exported wrappers:

```c
struct passwd *
getpwuid(uid_t uid)
{
    if (!libc7_h)
        libc7_init();

    if (rl_getpwuid)
        return rl_getpwuid(uid);

    errno = ENOENT;
    return NULL;
}

struct passwd *
getpwnam(const char *name)
{
    if (!libc7_h)
        libc7_init();

    if (rl_getpwnam)
        return rl_getpwnam(name);

    errno = ENOENT;
    return NULL;
}
```

Copy stage 01 to `/root/libc3-shim-02.c` inside the jail and apply the additions
above. Put the new headers and function-pointer declarations before
`libc7_init()`, then build the password-lookup variant:

```sh
jexec applix151 cc -m32 -shared -fPIC -Wall -Wextra \
  -o /usr/local/lib/libc3-shim-02.so \
  /root/libc3-shim-02.c
```

Confirm it still exports the whole earlier surface plus the new wrapper:

```sh
jexec applix151 nm -D /usr/local/lib/libc3-shim-02.so |
  egrep ' (getpwuid|sigaction|sigprocmask)$'
```

Expected symbol types:

```text
T getpwuid
T sigaction
T sigprocmask
```

Run stage 02:

```sh
run_shim /usr/local/lib/libc3-shim-02.so
```

The observed launcher result is now:

```text
return code=0, signal=0
```

Rerunning the focused DTrace probe with the new preload inverts the earlier
result: the wrapper receives UID 0, calls libc7, and returns the same non-null
passwd pointer to Applix, so `perror("get user name")` is never reached:

```text
libc3-shim-02.so`getpwuid(0)
libc.so.7`getpwuid(0)
libc.so.7`getpwuid returned 0x283fe948
libc3-shim-02.so`getpwuid returned 0x283fe948
```

That before/after return value is the validation: stage 01 returned `NULL`
from libc3 and printed the error; stage 02 returned a passwd entry through
libc7 and crossed the account-lookup boundary.

A zero launcher status is progress, but it is not proof that Words has opened.
The launcher hands work to other processes: `axnet` handles IPC (communication
between processes), and `axmain` is an application helper. Their libraries and
the visible desktop are the next things to check.

### Resolving ApplixWare's own runtime libraries

An earlier test without the CD's `axdata/axshlib` directory in the search path
failed during the handoff to `axmain`:

```text
ld-elf32.so.1: Shared object "libshelf.so" not found, required by "axmain"
/applixware1999cdrom/axdata/axmain: Operation not permitted
```

The commands above already include that directory, so this particular error
should not recur when following them. It explains why that part of the path
matters: a preload adds an object, but does not add library search directories.

The loader reports only a name, so locate the file before editing any path:

```sh
jexec applix151 find /applixware1999cdrom -type f -name libshelf.so -print
```

```text
/applixware1999cdrom/axdata/axshlib/libshelf.so
```

With that directory in `LD_32_LIBRARY_PATH`, the next missing name belongs to
the helper’s old C++ runtime:

```sh
# jexec applix151 env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    LD_32_PRELOAD=/usr/local/lib/libc3-shim-02.so \
    /applixware1999cdrom/applix
ld-elf32.so.1: Shared object "libg++.so.4" not found, required by "axmain"
```

Each fixed name exposes the next.  Do not infer library names from the order of
the error messages; read the binary's dynamic section instead.  This is the
first point in the timeline where the old C++ and GNU regex libraries enter the
dependency graph.  Inspect the ELF `NEEDED` entries:

```sh
jexec applix151 readelf -d /applixware1999cdrom/axdata/axmain | grep NEEDED
jexec applix151 readelf -d /applixware1999cdrom/axdata/axshlib/libshelf.so | grep NEEDED
jexec applix151 readelf -d /applixware1999cdrom/axdata/axshlib/libfontfs.so | grep NEEDED
```

The relevant entries are:

```text
axmain:       libshelf.so, libX11.so.6, libg++.so.4, libstdc++.so.2, libm.so.2, libc.so.3
libshelf.so:  libfontfs.so, libg++.so.4, libstdc++.so.2
libfontfs.so: libgnuregex.so.2
```

Each `NEEDED` entry is a direct dependency; the loader stops startup when one
cannot be found, so fixing one library exposes the next.  `libg++.so.4` and
`libstdc++.so.2` are direct dependencies of `axmain` and `libshelf.so`, while
`libgnuregex.so.2` is a direct dependency of `libfontfs.so` and a transitive
dependency of `libshelf.so`.  The dependency path is therefore:

```text
axmain -> libshelf.so
       -> libg++.so.4
       -> libstdc++.so.2
       -> ... -> libfontfs.so -> libgnuregex.so.2
```

Extract them from the already downloaded FreeBSD 3.5.1 archive directly into
the Applix-first compat directory.  These must be the 32-bit FreeBSD 3.5.1
libraries; do not substitute modern libraries merely because their names are
similar:

```sh
jexec applix151 sh -c '
  set -eu
  work=/var/tmp/applix-compat-libs
  applix=/usr/local/lib32/compat/applix
  mkdir -p "$applix"
  cd "$work/freebsd351-bin"
  cat bin.* | tar -xzvf - -C "$applix" --strip-components 2 \
    "usr/lib/libgnuregex.so*" \
    "usr/lib/libg++.so*" \
    "usr/lib/libstdc++.so*"
'

jexec applix151 ldconfig -32 \
  /usr/lib32 \
  /usr/local/lib32/compat \
  /usr/local/lib32/compat/applix \
  /applixware1999cdrom/axdata/axshlib
```

Run Applix again in the background and inspect after five seconds.  The helper
processes now remain alive:

```text
/applixware1999cdrom/axdata/axnet /tmp/.axnetipc -fork
/applixware1999cdrom/axdata/axmain -helper ... -servicename axmain:root::1
```

That gets the Words startup path alive.  For the full Words UI, the next clue
came from looking at the module libraries.  With the FreeBSD 3.5.1
`libX11.so.6`, `truss` shows `axmain` connecting directly to
`/tmp/.X11-unix/X1` and exchanging X11 requests and replies.  There are no
loader probes for `libxcb`, `libXau`, `libXdmcp`, `libXcursor`, `libXfixes`, or
`libXrender`; those were artifacts of using a modern X11 stack, not Applix
dependencies.

### Stage 03: inspecting the module symbols

The next two stages are inspection-driven additions from the reconstructed
shim, rather than fixes justified by a new recorded startup error. That is a
limit of the evidence: the symbol scan shows what libraries reference, but
does not by itself show which definitions are missing at runtime.

`nm -D` lists dynamic symbols. `U` marks a reference whose definition must come
from elsewhere. Count those references
across the Applix shared libraries, largest first:

```sh
jexec applix151 sh -c \
  'nm -D /applixware1999cdrom/axdata/axshlib/*.so 2>/dev/null |
   awk '\''$1 == "U" { count[$2]++ }
        END { for (name in count) print count[name], name }'\'' |
   sort -nr | head -25'
```

`__sF` emerges from that scan as an undefined name used by a dozen Applix
libraries (roughly tied with `fwrite`, `fopen`, `fclose`, and `__error` in
frequency).  Only after the name emerges should a targeted scan list which of
the symbols need the shim:

```sh
jexec applix151 sh -c \
  'nm -D /applixware1999cdrom/axdata/axshlib/*.so | egrep " U (__sF|__builtin|__pure|__register)"'
```

Observed unresolved symbols included:

```text
U __sF
U __builtin_new
U __builtin_delete
U __builtin_vec_new
U __pure_virtual
U __register_exceptions
```

`U __sF` means "undefined in this object," not "missing from the process."
Before assuming the shim must provide it, check whether a loaded library already
defines the symbol.  The launcher resolves old `libc.so.3`, and that library's
dynamic table answers:

```sh
jexec applix151 sh -c \
  'env LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    ldd /applixware1999cdrom/applix | grep libc.so.3'
jexec applix151 nm -D /usr/local/lib32/compat/applix/libc.so.3 | grep ' __sF$'
```

```text
libc.so.3 => /usr/local/lib32/compat/applix/libc.so.3 (...)
0007116c D __sF
```

`D` means FreeBSD 3.x libc already exports `__sF` as initialized global data,
so the dependency is satisfied by the loaded runtime.  FreeBSD 3.x libc
exported the stdio `FILE` objects this way.  The shim's `FILE __sF[3]` is
therefore a preload-time strong `B` (zero-initialized) symbol that *interposes*
on libc3's `D` object; the tested Applix paths bind to whichever definition the
loader selects first and do not rely on modern libc's private stdio internals
being recreated.  It is compatibility-symbol coverage, added because the name
clustered at the top of the undefined-symbol scan and the original shim
exported it.

For this entry, the difference is:

- libc3 side: old code could link against the global `__sF` stdio object array.
- libc7 / FreeBSD 15.1 side: stdio internals are not exposed with that old ABI
  symbol.
- shim behavior: export a small `FILE __sF[3]` object so old Applix libraries can
  bind the symbol.  The tested paths need the symbol to exist; they do not rely
  on modern libc's private stdio internals being recreated.
- create in the shim: define a global `FILE __sF[3]` data symbol.  This is not a
  wrapper function; it is exported storage for an old link-time ABI name.

Save the following as `/root/libc3-shim-03.c` inside the jail:

```c
#include <stdio.h>

FILE __sF[3];

#include "libc3-shim-02.c"
```

Build the variant with `__sF`:

```sh
jexec applix151 cc -m32 -shared -fPIC -Wall -Wextra \
  -o /usr/local/lib/libc3-shim-03.so \
  /root/libc3-shim-03.c
```

Run stage 03:

```sh
run_shim /usr/local/lib/libc3-shim-03.so
```

The observed result remains:

```text
return code=0, signal=0
```

`axnet` and `axmain` remain alive.  This stage does not produce a new startup
message because `__sF` was found by inspecting later-loaded libraries rather
than by a new launcher failure.

### Stage 04: old GNU C++ helper symbols

The same `nm -D` scan surfaced unresolved old GNU C++ runtime symbols.  Those are not FreeBSD kernel or libc syscalls; they are helper entry points from the old C++ ABI used by the Applix shared libraries.  Modern FreeBSD's C++ runtime does not export that ABI surface.  When these names are missing, `axmain` can load part of the Applix library graph, create transient X objects, and then exit without a visible application window.  Adding the symbols to
`libc3_shim.c` keeps the Applix-facing preload self-contained.

For these entries, the difference is:

- libc3 / old toolchain side: Applix shared libraries were built against an old
  GNU C++ ABI where helper symbols such as `__builtin_new`,
  `__builtin_delete`, `__pure_virtual`, and exception-table helpers were global
  runtime entry points.
- libc7 / FreeBSD 15.1 side: modern FreeBSD uses a different C++ runtime ABI and
  does not export those old helper names for legacy `libg++` objects.
- shim behavior: provide just enough of the old helper surface for Applix startup
  and normal allocation paths: allocation maps to `malloc()`, deletion maps to
  `free()`, pure-virtual calls abort, and the unused exception-table hooks are
  harmless no-ops for the tested paths.
- create in the shim: export `__builtin_new()`, `__builtin_vec_new()`,
  `__builtin_delete()`, `__builtin_vec_delete()`, `__pure_virtual()`,
  `__register_exceptions()`, `__find_first_exception_table_match()`, and
  `__unwind_function()` with the minimal behavior shown below.

The allocation helpers are the important functional pieces.  Old C++ code calls
`__builtin_new()` and `__builtin_vec_new()` for scalar and array allocation.
For Applix startup, mapping them to `malloc()` is sufficient.  C++ permits
zero-size allocation requests; returning a real one-byte allocation avoids
surprising callers with `NULL`.

```c
#include <stdlib.h>

#include "libc3-shim-03.c"

void *
__builtin_new(size_t size)
{
    if (size == 0)
        size = 1;
    return malloc(size);
}

void *
__builtin_vec_new(size_t size)
{
    if (size == 0)
        size = 1;
    return malloc(size);
}
```

The matching delete helpers free scalar and array allocations.  They deliberately
do no extra bookkeeping because the old helper ABI does not pass any metadata here; Applix only needs the symbols to behave like ordinary deallocators.

```c
void
__builtin_delete(void *ptr)
{
    free(ptr);
}

void
__builtin_vec_delete(void *ptr)
{
    free(ptr);
}
```

`__pure_virtual()` is called only if old C++ code reaches a pure virtual method
body.  That is a programming error path, so the shim aborts rather than pretending there is a useful return value.

```c
void
__pure_virtual(void)
{
    abort();
}
```

The exception helpers are present because old `libg++`-era objects refer to
them while registering or searching exception tables.  The Applix startup paths
tested here do not depend on real C++ exception unwinding through modern
FreeBSD runtime code, so no-op registration, no match, and no-op unwind are
enough to satisfy the ABI references.

```c
void
__register_exceptions(void *table __attribute__((unused)))
{
}

void *
__find_first_exception_table_match(void *pc __attribute__((unused)))
{
    return NULL;
}

void
__unwind_function(void *frame __attribute__((unused)))
{
}
```

Save the stage 04 code blocks, in order, as `/root/libc3-shim-04.c` inside the
jail. Build it and run the same test with its preload:

```sh
jexec applix151 cc -m32 -shared -fPIC -Wall -Wextra \
  -o /usr/local/lib/libc3-shim-04.so \
  /root/libc3-shim-04.c
```

Run stage 04:

```sh
run_shim /usr/local/lib/libc3-shim-04.so
```

The observed result is:

```text
return code=0, signal=0
```

`axnet` and `axmain` remain alive.  Like stage 03, this stage adds symbol
coverage found with `nm`; it does not change the already-successful launcher
return code.

Verify that stage 04 exports the complete compatibility surface:

```sh
jexec applix151 nm -D /usr/local/lib/libc3-shim-04.so | \
  egrep " (sigaction|sigprocmask|getpwuid|getpwnam|__sF|__builtin_new|__builtin_delete|__builtin_vec_new|__builtin_vec_delete|__pure_virtual|__register_exceptions|__find_first_exception_table_match|__unwind_function)$"
```

Save the complete source from Appendix A as `/root/libc3_shim.c` inside the
jail. Compile it and run it with the same return-code capture:

```sh
jexec applix151 cc -m32 -shared -fPIC -Wall -Wextra \
  -o /usr/local/lib/libc3_shim.so \
  /root/libc3_shim.c

run_shim /usr/local/lib/libc3_shim.so
```

Observed final result:

```text
return code=0, signal=0
```

Use one targeted trace to confirm that the completed shim still routes signal
operations through the working modern interfaces:

```sh
jexec applix151 sh -c '
  env \
    LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/applixware1999cdrom/axdata/axshlib \
    LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
    AXHOME=/applixware1999cdrom \
    AXGFSDIR=/applixware1999cdrom/axdata/fontmetrics/gallium \
    AXLANG=english_us \
    AX_TEMP_DIR=/tmp \
    DISPLAY=:1 \
    timeout 6 truss -f -o /tmp/truss-final.log \
    /applixware1999cdrom/applix -builder
  egrep "compat\\.freebsd32_sigaction|compat\\.sigprocmask|freebsd32_sigaction\\(|sigprocmask\\(" \
    /tmp/truss-final.log | head
'
```

The no-preload trace above showed `compat.freebsd32_sigaction()` failing with
`ENOSYS`.  With the final shim, the relevant successful calls are:

```text
freebsd32_sigaction(SIGSYS, ...) = 0
freebsd32_sigaction(SIGINT, ...) = 0
freebsd32_sigaction(SIGUSR1, ...) = 0
sigprocmask(SIG_BLOCK, ...) = 0
```

The obsolete `compat.*` `ENOSYS` calls disappear.  That is the final shape of
`libc3_shim.c`:

- initialization through the first intercepted signal call installs
  `signal(SIGSYS, SIG_IGN)` as a bring-up safeguard.
- `getpwuid()` and `getpwnam()` delegate account lookup to modern `libc.so.7`.
- `FILE __sF[3]` satisfies old stdio data references.
- old GNU C++ ABI stubs satisfy allocation, delete, pure-virtual, and
  exception-table helper references from Applix shared libraries.
- `sigaction()` and `sigprocmask()` translate old signal ABI structures to the
  modern FreeBSD 15 i386 layout.

## 6. From a running helper to a visible Words window

The helpers can start without leaving a usable application window. At this
point we need to check application data and fonts as well as executable code.

One test used a mounted CD as `AXHOME`, rather than the extracted copy we made
above. Its trace exposed another assumption in Applix's startup:

```text
open("/applixware1999cdrom/ax_prof4x", O_WRONLY|O_CREAT|O_TRUNC, 0666) ERR#30 'Read-only file system'
access("/applixware1999cdrom", W_OK) ERR#30 'Read-only file system'
_exit(0x1)
```

`ERR#30` is `EROFS`, or "Read-only file system".  Applix is trying to create or
rewrite profile/state files under `AXHOME`, but in this run `AXHOME` still
points at the CD image mount.  ISO/CD-ROM content is read-only, so startup exits
even though the binary, libraries, shim, and X display are now mostly working.
`_exit(0x1)` is the process leaving with status 1 after deciding it cannot use
that read-only runtime tree.

Our extracted copy is already writable, so the same read-only error is not
expected in this sequence. The trace still establishes that Applix needs a
writable application root. Make a separate runtime tree at
`/usr/local/applixware` so the extracted CD remains our source copy:

```sh
jexec applix151 install -d -m 755 /usr/local/applixware
jexec applix151 sh -c '
  (cd /applixware1999cdrom && tar -cf - .) |
    (cd /usr/local/applixware && tar -xpf -)
'
```

If the copy is interrupted, compare the important runtime directories.  In the
failed run, `axdata` and `axart` were incomplete, so `axmain` loaded far enough
to create transient X windows and then exited:

```sh
jexec applix151 sh -c '
  for d in axdata axfonts axlocal axart axlang axfilter builder template system rtgate; do
    s=$(find "/applixware1999cdrom/$d" -type f 2>/dev/null | wc -l)
    t=$(find "/usr/local/applixware/$d" -type f 2>/dev/null | wc -l)
    printf "%s source=%s target=%s\n" "$d" "$s" "$t"
  done
'
```

For the verified tree, the key counts included:

```text
axdata source=6062 target=6062
axart  source=2109 target=2109
```

Now that `/usr/local/applixware` exists, add the Applix font directory to the
running VNC server's font path:

```sh
jexec applix151 sh -c '
  DISPLAY=:1 xset fp= /usr/local/share/fonts/misc,/usr/local/applixware/axfonts/all_pcf,built-ins
  DISPLAY=:1 xset fp rehash
  DISPLAY=:1 xsetroot -cursor_name left_ptr
'
```

The Applix font path must include three pieces:

- `/usr/local/share/fonts/misc`: ordinary X bitmap fonts used by Fluxbox and X
  tools.
- `/usr/local/applixware/axfonts/all_pcf`: ApplixWare's bundled PCF fonts,
  now available at the installed runtime path.
- `built-ins`: the X server built-in font set, including the cursor font used
  by named cursors.

When `built-ins` was missing, `xsetroot -cursor_name left_ptr` failed with:

```text
X Error of failed request:  BadName (named color or font does not exist)
Major opcode of failed request:  45 (X_OpenFont)
```

This is the same X font-path error from the first VNC setup step, but now the
font path also needs ApplixWare's own bitmap fonts.  `built-ins` provides the
cursor font; `/usr/local/applixware/axfonts/all_pcf` provides the legacy Applix
screen fonts used by its dialogs and document windows.

Without the `xsetroot` call, TigerVNC accepted connections but the pointer was
invisible.  The visible cursor is not cosmetic during debugging; it confirms
that the X font path still contains the cursor font and that the viewer is
interacting with the intended display.

The repeatable environment is:

```sh
jexec applix151 sh -c '
  rm -f /tmp/axl* /tmp/.axnetipc /tmp/.ax*
  DISPLAY=:1 xset fp= /usr/local/share/fonts/misc,/usr/local/applixware/axfonts/all_pcf,built-ins
  DISPLAY=:1 xset fp rehash
  DISPLAY=:1 xsetroot -cursor_name left_ptr

  env \
    LD_32_LIBRARY_PATH=/usr/local/lib32/compat/applix:/usr/local/lib32/compat:/usr/local/applixware/axdata/axshlib \
    LD_32_PRELOAD=/usr/local/lib/libc3_shim.so \
    AXHOME=/usr/local/applixware \
    AXGFSDIR=/usr/local/applixware/axdata/fontmetrics/gallium \
    AXLANG=english_us \
    AX_TEMP_DIR=/tmp \
    DISPLAY=:1 \
    /usr/local/applixware/applix -wp
'
```

Verified live process state after five seconds:

```text
/usr/local/applixware/axdata/axnet /tmp/.axnetipc -fork
/usr/local/applixware/axdata/axmain -helper ... -servicename axmain:root::1
/usr/local/applixware/applix -wp
```

That confirms the launcher, helper, old libraries, shim, writable runtime tree,
and X display all got far enough to start the Words module process tree from
the `applix151` jail.  A full-screen XWD capture confirmed that the VNC display
showed the Applix Words UI.

At this point "no errors" does not mean the launcher prints a success message;
Applix is a graphical program, so a successful command leaves the application
and its helper processes running while the UI appears in VNC.  The important
change is that the previous loader, libc, account lookup, signal, read-only
filesystem, font, and cursor errors no longer appear.

## 7. What the successful run establishes

The recorded Words window is the result we were looking for. We started with
a working VNC desktop, uncovered a 32-bit executable, supplied its missing
libraries, and traced runtime failures back to signal calls and account lookup.
A running launcher alone was insufficient evidence; the visible UI completed
that startup check. This does not establish that every office-suite feature
works, or that every symbol added to the reconstructed shim was necessary.

One remaining note concerns executable stacks. The investigation recorded:

```text
kern.elf32.nxstack: 1
```

That is the value observed before the final run. The notes also suggested
`sysctl kern.elf32.nxstack=0` for possible module-startup problems, but provide
no specific failure or before/after test establishing that it was required.
It therefore does not belong among the demonstrated fixes. This is a host-wide
setting affecting stack execution protection for all 32-bit processes, not a
jail-local application option.

### Repeatable launches and cleanup

Now that the manual startup works, Appendix B provides a generic `vnc-session`
helper, Appendix C provides `vnc-applix` for launching the suite in VNC, and
Appendix D provides `applix-wrapper` for the application environment. Install
the helpers as described in their appendices before using them.

```sh
jexec applix151 /usr/local/bin/vnc-applix wp
jexec applix151 /usr/local/bin/vnc-applix status
```

When finished testing, stop the Applix helpers and remove their temporary state
inside this dedicated jail:

```sh
jexec applix151 pkill axmain
jexec applix151 pkill axnet
jexec applix151 rm -f /tmp/axl* /tmp/.axnetipc /tmp/.ax*
```

Stop the manually started VNC desktop only after finishing the investigation:

```sh
jexec applix151 sh -c '
  if test -s /var/run/xvnc.pid; then
    kill "$(cat /var/run/xvnc.pid)"
    rm -f /var/run/xvnc.pid
  fi
  pkill -x fluxbox 2>/dev/null || true
'
```

## Appendix A. Complete `libc3_shim.c`

Copy this file into the jail as `/root/libc3_shim.c` before building
`/usr/local/lib/libc3_shim.so`.

```c
/*
 * Reconstructed FreeBSD/i386 preload shim for ApplixWare on amd64.
 *
 * This intentionally mirrors the behavior of libc_shim3.so without replacing
 * the deployed library.  It translates the old 32-bit signal structures used by
 * the legacy runtime, delegates selected libc calls to libc.so.7, and provides
 * the old exported __sF stdio object expected by Applix.
 */
#include <sys/types.h>
#include <dlfcn.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef RTLD_LOCAL
#define RTLD_LOCAL 0
#endif

#define APPLIX_SIGSYS 12
#define APPLIX_SIG_IGN ((void (*)(int))1)

/*
 * FreeBSD 3.x libc exported the stdio FILE objects as __sF.  Modern FreeBSD
 * no longer exposes that ABI shape, but old Applix libraries still resolve the
 * symbol during startup.  The original shim exported storage for the symbol;
 * Applix only needs the link-time symbol to exist in the tested paths.
 */
FILE __sF[3];

/*
 * The old i386 sigaction ABI used one 32-bit signal mask word and placed the
 * fields as handler/mask/flags.  FreeBSD 15's i386 libc expects
 * handler/flags/four-word-mask.  Passing the old structure directly makes the
 * modern syscall wrapper read the mask as flags and fail with EINVAL.
 */
struct old_sigaction {
    void *handler;
    unsigned int mask;
    int flags;
};

struct new_sigaction {
    void *handler;
    int flags;
    unsigned int mask[4];
};

static void *libc7_h;
static int (*rl_sigaction)(int, const void *, void *);
static int (*rl_sigprocmask)(int, const void *, void *);
static struct passwd *(*rl_getpwuid)(uid_t);
static struct passwd *(*rl_getpwnam)(const char *);
static void (*(*rl_signal)(int, void (*)(int)))(int);

static void
libc7_init(void)
{
    if (libc7_h)
        return;

    libc7_h = dlopen("libc.so.7", RTLD_LAZY | RTLD_LOCAL);
    if (!libc7_h)
        return;

    /*
     * This shim is preloaded ahead of the old libc.so.3.  Use direct libc.so.7
     * handles for functions that are safer to satisfy with the host runtime
     * than with the 1999-era libc implementation.
     */
    rl_sigaction = dlsym(libc7_h, "sigaction");
    rl_sigprocmask = dlsym(libc7_h, "sigprocmask");
    rl_getpwuid = dlsym(libc7_h, "getpwuid");
    rl_getpwnam = dlsym(libc7_h, "getpwnam");
    rl_signal = dlsym(libc7_h, "signal");

    /*
     * On this old i386 ABI, signal 12 is SIGSYS.  During bring-up, unsupported
     * or mismatched old libc syscall paths could terminate the process with
     * "Bad system call"; the original shim ignored SIGSYS early.
     */
    if (rl_signal)
        rl_signal(APPLIX_SIGSYS, APPLIX_SIG_IGN);
}

int
sigaction(int sig, const struct old_sigaction *act, struct old_sigaction *oact)
{
    struct new_sigaction m_act;
    struct new_sigaction m_oact;
    struct new_sigaction *ap = NULL;
    struct new_sigaction *op = NULL;
    int ret;

    if (!libc7_h)
        libc7_init();

    /* Expand the old one-word mask into the modern four-word i386 mask. */
    if (act) {
        memset(&m_act, 0, sizeof(m_act));
        m_act.handler = act->handler;
        m_act.flags = act->flags;
        m_act.mask[0] = act->mask;
        ap = &m_act;
    }

    if (oact)
        op = &m_oact;

    ret = rl_sigaction ? rl_sigaction(sig, ap, op) : -1;
    if (ret == 0 && oact && op) {
        /* Collapse the modern result back into the layout old callers expect. */
        oact->handler = m_oact.handler;
        oact->flags = m_oact.flags;
        oact->mask = m_oact.mask[0];
    }

    return ret;
}

int
sigprocmask(int how, const unsigned int *set, unsigned int *oset)
{
    unsigned int m_set[4];
    unsigned int m_oset[4];
    void *sp = NULL;
    void *op = NULL;
    int ret;

    if (!libc7_h)
        libc7_init();

    memset(m_set, 0, sizeof(m_set));
    memset(m_oset, 0, sizeof(m_oset));

    /* Old callers pass a pointer to one mask word; libc.so.7 expects four. */
    if (set) {
        m_set[0] = *set;
        sp = m_set;
    }

    if (oset)
        op = m_oset;

    ret = rl_sigprocmask ? rl_sigprocmask(how, sp, op) : -1;
    if (ret == 0 && oset && op)
        *oset = m_oset[0];

    return ret;
}

struct passwd *
getpwuid(uid_t uid)
{
    if (!libc7_h)
        libc7_init();

    /*
     * The FreeBSD 3.x password database reader cannot parse the modern jail's
     * passwd/spwd DB format reliably.  Delegate account lookup to libc.so.7 so
     * Applix sees a normal passwd entry for the current user.
     */
    if (rl_getpwuid)
        return rl_getpwuid(uid);

    errno = ENOENT;
    return NULL;
}

struct passwd *
getpwnam(const char *name)
{
    if (!libc7_h)
        libc7_init();

    /* Same passwd database compatibility issue as getpwuid(). */
    if (rl_getpwnam)
        return rl_getpwnam(name);

    errno = ENOENT;
    return NULL;
}

/*
 * Applix's shared libraries were linked against the old GNU C++ runtime ABI.
 * The old libraries expect these allocation and exception helper symbols to be
 * globally available.  Modern FreeBSD's C++ runtime does not provide that ABI,
 * and Applix only needs enough behavior for startup and normal allocation.
 */
void *
__builtin_new(size_t size)
{
    if (size == 0)
        size = 1;
    return malloc(size);
}

void
__builtin_delete(void *ptr)
{
    free(ptr);
}

void *
__builtin_vec_new(size_t size)
{
    if (size == 0)
        size = 1;
    return malloc(size);
}

void
__builtin_vec_delete(void *ptr)
{
    free(ptr);
}

void
__pure_virtual(void)
{
    abort();
}

void
__register_exceptions(void *table __attribute__((unused)))
{
}

void *
__find_first_exception_table_match(void *pc __attribute__((unused)))
{
    return NULL;
}

void
__unwind_function(void *frame __attribute__((unused)))
{
}
```

## Appendix B. Complete `vnc-session`

Install this inside the jail as `/usr/local/bin/vnc-session` and make it
executable with `chmod 755 /usr/local/bin/vnc-session`.  This helper is generic:
it starts a VNC desktop with Fluxbox and xterm, but it does not know anything
about ApplixWare.

```sh
#!/bin/sh
set -eu

DISPLAY_NUM=${DISPLAY_NUM:-1}
DISPLAY=":$DISPLAY_NUM"
GEOMETRY=${GEOMETRY:-800x600}
DEPTH=${DEPTH:-16}
RFBPORT=${RFBPORT:-5901}
VNC_DIR=${VNC_DIR:-/root/.vnc}
VNC_PASS_FILE=${VNC_PASS_FILE:-$VNC_DIR/passwd}
VNC_PASS_TEXT=${VNC_PASS_TEXT:-$VNC_DIR/vnc-password}
VNC_LOG=${VNC_LOG:-$VNC_DIR/xvnc.log}
VNC_PID=${VNC_PID:-/var/run/xvnc.pid}
FONT_PATH=${FONT_PATH:-/usr/local/share/fonts/misc,built-ins}

usage() {
    cat <<USAGE
usage: vnc-session {start|stop|restart|status|password|xterm}

Environment overrides:
  DISPLAY_NUM=$DISPLAY_NUM
  GEOMETRY=$GEOMETRY
  DEPTH=$DEPTH
  RFBPORT=$RFBPORT
  FONT_PATH=$FONT_PATH
USAGE
}

is_vnc_up() {
    env DISPLAY="$DISPLAY" xset q >/dev/null 2>&1
}

refresh_session() {
    env DISPLAY="$DISPLAY" xset fp= "$FONT_PATH" >/dev/null 2>&1 || true
    env DISPLAY="$DISPLAY" xset fp rehash >/dev/null 2>&1 || true
    env DISPLAY="$DISPLAY" xsetroot -cursor_name left_ptr >/dev/null 2>&1 || true

    if command -v fluxbox >/dev/null 2>&1 && ! pgrep -x fluxbox >/dev/null 2>&1; then
        env DISPLAY="$DISPLAY" fluxbox >/dev/null 2>&1 &
    fi
}

create_password() {
    install -d -m 700 "$VNC_DIR"
    if [ ! -s "$VNC_PASS_FILE" ]; then
        pw=$(openssl rand -base64 18)
        printf "%s\n" "$pw" | vncpasswd -f > "$VNC_PASS_FILE"
        chmod 600 "$VNC_PASS_FILE"
        printf "%s\n" "$pw" > "$VNC_PASS_TEXT"
        chmod 600 "$VNC_PASS_TEXT"
    fi
}

start_vnc() {
    if is_vnc_up; then
        refresh_session
        echo "Xvnc is already running on $DISPLAY"
        return
    fi

    create_password
    rm -f "/tmp/.X${DISPLAY_NUM}-lock" "/tmp/.X11-unix/X${DISPLAY_NUM}"

    daemon -p "$VNC_PID" -o "$VNC_LOG" \
        /usr/local/bin/Xvnc "$DISPLAY" \
        -geometry "$GEOMETRY" \
        -depth "$DEPTH" \
        -rfbport "$RFBPORT" \
        -SecurityTypes VncAuth \
        -rfbauth "$VNC_PASS_FILE" \
        -AlwaysShared=1 \
        -AcceptSetDesktopSize=0 \
        -localhost no \
        -fp "$FONT_PATH"

    for _ in 1 2 3 4 5 6 7 8 9 10; do
        if is_vnc_up; then
            refresh_session
            echo "Started Xvnc on $DISPLAY port $RFBPORT"
            return
        fi
        sleep 1
    done

    echo "Xvnc did not become ready; see $VNC_LOG" >&2
    exit 1
}

stop_vnc() {
    if [ -s "$VNC_PID" ]; then
        pid=$(cat "$VNC_PID")
        kill "$pid" 2>/dev/null || true
        rm -f "$VNC_PID"
    fi
    pkill -f "Xvnc $DISPLAY" 2>/dev/null || true
    pkill fluxbox 2>/dev/null || true
    rm -f "/tmp/.X${DISPLAY_NUM}-lock" "/tmp/.X11-unix/X${DISPLAY_NUM}"
    echo "Stopped Xvnc on $DISPLAY"
}

case "${1:-}" in
    start)
        start_vnc
        ;;
    stop)
        stop_vnc
        ;;
    restart)
        stop_vnc
        start_vnc
        ;;
    status)
        if is_vnc_up; then
            echo "Xvnc is running on $DISPLAY port $RFBPORT"
        else
            echo "Xvnc is not running on $DISPLAY"
            exit 1
        fi
        ;;
    password)
        create_password
        cat "$VNC_PASS_TEXT"
        ;;
    xterm)
        start_vnc
        env DISPLAY="$DISPLAY" xterm >/dev/null 2>&1 &
        ;;
    -h|--help|help|"")
        usage
        ;;
    *)
        echo "unknown command: $1" >&2
        usage >&2
        exit 64
        ;;
esac
```

## Appendix C. Complete `vnc-applix`

Install this inside the jail as `/usr/local/bin/vnc-applix` and make it
executable with `chmod 755 /usr/local/bin/vnc-applix`.

```sh
#!/bin/sh
set -eu

DISPLAY_NUM=${DISPLAY_NUM:-1}
DISPLAY=":$DISPLAY_NUM"
GEOMETRY=${GEOMETRY:-800x600}
DEPTH=${DEPTH:-16}
RFBPORT=${RFBPORT:-5901}
VNC_DIR=${VNC_DIR:-/root/.vnc}
VNC_PASS_FILE=${VNC_PASS_FILE:-$VNC_DIR/passwd}
VNC_PASS_TEXT=${VNC_PASS_TEXT:-$VNC_DIR/applix-vnc-password}
VNC_LOG=${VNC_LOG:-$VNC_DIR/applix-xvnc.log}
VNC_PID=${VNC_PID:-/var/run/applix-xvnc.pid}
DEFAULT_AXHOME=/usr/local/applixware
if [ ! -x "$DEFAULT_AXHOME/applix" ]; then
    DEFAULT_AXHOME=/applixware1999cdrom
fi
AXHOME=${AXHOME:-$DEFAULT_AXHOME}
FONT_PATH=${FONT_PATH:-/usr/local/share/fonts/misc,$AXHOME/axfonts/all_pcf,built-ins}

APPLIX_BIN=${APPLIX_BIN:-$AXHOME/applix}
LIBC3_SHIM=${LIBC3_SHIM:-/usr/local/lib/libc3_shim.so}
APPLIX_LIB32_COMPAT=${APPLIX_LIB32_COMPAT:-/usr/local/lib32/compat}
APPLIX_LIB32_APPLIX=${APPLIX_LIB32_APPLIX:-$APPLIX_LIB32_COMPAT/applix}
AXDATA=${AXDATA:-$AXHOME/axdata}
AXSHLIB=${AXSHLIB:-$AXDATA/axshlib}
AXGFSDIR=${AXGFSDIR:-$AXDATA/fontmetrics/gallium}
AXLANG=${AXLANG:-english_us}
AX_TEMP_DIR=${AX_TEMP_DIR:-/tmp}

usage() {
    cat <<USAGE
usage: vnc-applix {start|stop|restart|status|password|wp|gr|ss|builder|presents|applix}

Environment overrides:
  DISPLAY_NUM=$DISPLAY_NUM
  GEOMETRY=$GEOMETRY
  DEPTH=$DEPTH
  RFBPORT=$RFBPORT
USAGE
}

is_vnc_up() {
    env DISPLAY="$DISPLAY" xset q >/dev/null 2>&1
}

refresh_session() {
    env DISPLAY="$DISPLAY" xset fp= "$FONT_PATH" >/dev/null 2>&1 || true
    env DISPLAY="$DISPLAY" xset fp rehash >/dev/null 2>&1 || true
    env DISPLAY="$DISPLAY" xsetroot -cursor_name left_ptr >/dev/null 2>&1 || true

    if command -v fluxbox >/dev/null 2>&1 && ! pgrep -x fluxbox >/dev/null 2>&1; then
        env DISPLAY="$DISPLAY" fluxbox >/dev/null 2>&1 &
    fi
}

create_password() {
    install -d -m 700 "$VNC_DIR"
    if [ ! -s "$VNC_PASS_FILE" ]; then
        pw=$(openssl rand -base64 18)
        printf "%s\n" "$pw" | vncpasswd -f > "$VNC_PASS_FILE"
        chmod 600 "$VNC_PASS_FILE"
        printf "%s\n" "$pw" > "$VNC_PASS_TEXT"
        chmod 600 "$VNC_PASS_TEXT"
    fi
}

start_vnc() {
    if is_vnc_up; then
        refresh_session
        echo "Xvnc is already running on $DISPLAY"
        return
    fi

    create_password
    rm -f "/tmp/.X${DISPLAY_NUM}-lock" "/tmp/.X11-unix/X${DISPLAY_NUM}"

    daemon -p "$VNC_PID" -o "$VNC_LOG" \
        /usr/local/bin/Xvnc "$DISPLAY" \
        -geometry "$GEOMETRY" \
        -depth "$DEPTH" \
        -rfbport "$RFBPORT" \
        -SecurityTypes VncAuth \
        -rfbauth "$VNC_PASS_FILE" \
        -AlwaysShared=1 \
        -AcceptSetDesktopSize=0 \
        -localhost no \
        -fp "$FONT_PATH"

    for _ in 1 2 3 4 5 6 7 8 9 10; do
        if is_vnc_up; then
            refresh_session
            echo "Started Xvnc on $DISPLAY port $RFBPORT"
            return
        fi
        sleep 1
    done

    echo "Xvnc did not become ready; see $VNC_LOG" >&2
    exit 1
}

stop_vnc() {
    if [ -s "$VNC_PID" ]; then
        pid=$(cat "$VNC_PID")
        kill "$pid" 2>/dev/null || true
        rm -f "$VNC_PID"
    fi
    pkill -f "Xvnc $DISPLAY" 2>/dev/null || true
    pkill fluxbox 2>/dev/null || true
    rm -f "/tmp/.X${DISPLAY_NUM}-lock" "/tmp/.X11-unix/X${DISPLAY_NUM}"
    echo "Stopped Xvnc on $DISPLAY"
}

cleanup_applix_state() {
    pkill axmain 2>/dev/null || true
    pkill axnet 2>/dev/null || true
    rm -f "$AX_TEMP_DIR"/axl* "$AX_TEMP_DIR"/.axnetipc \
        "$AX_TEMP_DIR"/.ax* "$AX_TEMP_DIR"/.axsc* 2>/dev/null || true
}

run_applix() {
    start_vnc
    cleanup_applix_state

    env \
        LD_32_LIBRARY_PATH="$APPLIX_LIB32_APPLIX:$APPLIX_LIB32_COMPAT:$AXSHLIB" \
        LD_32_PRELOAD="$LIBC3_SHIM" \
        AXHOME="$AXHOME" \
        AXGFSDIR="$AXGFSDIR" \
        AXLANG="$AXLANG" \
        AX_TEMP_DIR="$AX_TEMP_DIR" \
        DISPLAY="$DISPLAY" \
        "$APPLIX_BIN" "$@"
}

case "${1:-}" in
    start)
        start_vnc
        ;;
    stop)
        stop_vnc
        ;;
    restart)
        stop_vnc
        start_vnc
        ;;
    status)
        if is_vnc_up; then
            echo "Xvnc is running on $DISPLAY port $RFBPORT"
        else
            echo "Xvnc is not running on $DISPLAY"
            exit 1
        fi
        ;;
    password)
        create_password
        cat "$VNC_PASS_TEXT"
        ;;
    wp)
        run_applix -wp
        ;;
    gr)
        run_applix -gr
        ;;
    ss)
        run_applix -ss
        ;;
    builder)
        run_applix -builder
        ;;
    presents)
        run_applix -presents
        ;;
    applix)
        shift
        run_applix "$@"
        ;;
    -h|--help|help|"")
        usage
        ;;
    *)
        echo "unknown command: $1" >&2
        usage >&2
        exit 64
        ;;
esac
```

## Appendix D. Complete `applix-wrapper`

Install this inside the jail as `/usr/local/bin/applix-wrapper` and make it
executable with `chmod 755 /usr/local/bin/applix-wrapper`.

```sh
#!/bin/sh
set -eu

DEFAULT_AXHOME=/usr/local/applixware
if [ ! -x "$DEFAULT_AXHOME/applix" ]; then
    DEFAULT_AXHOME=/applixware1999cdrom
fi

AXHOME=${AXHOME:-$DEFAULT_AXHOME}
AXDATA=${AXDATA:-$AXHOME/axdata}
AXSHLIB=${AXSHLIB:-$AXDATA/axshlib}
AXGFSDIR=${AXGFSDIR:-$AXDATA/fontmetrics/gallium}
AXLANG=${AXLANG:-english_us}
AX_TEMP_DIR=${AX_TEMP_DIR:-/tmp}
DISPLAY=${DISPLAY:-:1}
VNC_APPLIX=${VNC_APPLIX:-/usr/local/bin/vnc-applix}

APPLIX_BIN=${APPLIX_BIN:-$AXHOME/applix}
LIBC3_SHIM=${LIBC3_SHIM:-/usr/local/lib/libc3_shim.so}
APPLIX_LIB32_COMPAT=${APPLIX_LIB32_COMPAT:-/usr/local/lib32/compat}
APPLIX_LIB32_APPLIX=${APPLIX_LIB32_APPLIX:-$APPLIX_LIB32_COMPAT/applix}

LD_32_LIBRARY_PATH=${LD_32_LIBRARY_PATH:-$APPLIX_LIB32_APPLIX:$APPLIX_LIB32_COMPAT:$AXSHLIB}
LD_32_PRELOAD=${LD_32_PRELOAD:-$LIBC3_SHIM}

cleanup_applix_state() {
    pkill axmain 2>/dev/null || true
    pkill axnet 2>/dev/null || true
    rm -f \
        "$AX_TEMP_DIR"/axl* \
        "$AX_TEMP_DIR"/.axnetipc \
        "$AX_TEMP_DIR"/.ax* \
        "$AX_TEMP_DIR"/.axsc* \
        2>/dev/null || true
}

usage() {
    cat >&2 <<EOF
usage: $(basename "$0") [applix-flag-or-args...]

Examples:
  $(basename "$0") -wp
  $(basename "$0") -gr
  $(basename "$0") -ss
  $(basename "$0") -presents
  $(basename "$0") -builder

Environment overrides:
  AXHOME=$AXHOME
  DISPLAY=$DISPLAY
  LIBC3_SHIM=$LIBC3_SHIM
EOF
    exit 64
}

case "${1:-}" in
    -h|--help)
        usage
        ;;
esac

if [ -x "$VNC_APPLIX" ]; then
    "$VNC_APPLIX" start
fi

if [ ! -x "$APPLIX_BIN" ]; then
    echo "missing Applix launcher: $APPLIX_BIN" >&2
    exit 66
fi

if [ ! -r "$LIBC3_SHIM" ]; then
    echo "missing libc3 shim: $LIBC3_SHIM" >&2
    exit 66
fi

cleanup_applix_state

export AXHOME AXGFSDIR AXLANG AX_TEMP_DIR DISPLAY
export LD_32_LIBRARY_PATH LD_32_PRELOAD

exec "$APPLIX_BIN" "$@"
```
