SchOS/cmd/resolvconf/main.c
Nexoniarz b08432b2c3 userland: 268 applets, an editor, names that resolve, and a web server
Four complaints, and only one of them was the thing it looked like.

**"toybox is too small."** Half true, and the fix cost no licensing at all:
236 of toybox's applets are what defconfig builds, and the rest live in
toys/pending/ -- awk, bc, diff, more, expr, tr, hexdump, stty among them. Same
0BSD tarball, switched off by a config default. 26 of them are on now, 236 ->
268. Not everything: strace wants ptrace, syslogd wants BSD's facilitynames[],
git wants OpenSSL, and those three break the link.

The toybox build also works again on this machine. Its scripts start
#!/bin/bash, which does not exist on NixOS, so the binary in the tree was one
nobody could reproduce; the shebangs are rewritten to /usr/bin/env bash on the
way in. Pending applets are enabled by editing .config directly, because
`make oldconfig` is the obvious way and undoes it -- every pending symbol is
`default n`.

**BusyBox was asked for and is not here.** It is GPL-2.0-only, and `make
ramroot` links the root image *into the kernel binary*, so it would put GPLv2
inside an Apache-2.0 executable -- which is the exact case ports/Makefile's
NOTPORTS filter and docs/licensing.md §4 exist for. nano is the same answer one
version later. Both recorded in licensing.md with what was done instead, since
"no" without an alternative is how a rule gets quietly broken later.

**An editor that is not vi.** kilo, BSD-2-Clause, one 1300-line file, no
dependencies beyond termios and TIOCGWINSZ, both of which zsh already needed.
Installed as /bin/edit as well as /bin/kilo, because somebody with a file to
fix will type `edit`.

**"wget doesn't work."** Not a userland problem, and BusyBox's wget would have
failed identically: nothing on this system could turn a name into an address,
so every program taking a hostname failed in its own way and each looked like
a separate bug. There is still no DNS client in SchOS and there does not need
to be -- musl has a complete resolver and it works over ordinary UDP sockets.
The one missing fact was which nameserver, which the kernel has had all along
from DHCP and never published. /proc/resolv.conf publishes it in resolv.conf's
exact syntax; cmd/resolvconf decides whether it becomes /etc/resolv.conf, and
will not overwrite a hand-written one without -f.

  # wget -O - http://example.com/ | head -1
  <!doctype html><html lang="en"><head><title>Example Domain</title>

**Something to host with.** cmd/httpd: GET and HEAD, static files, index.html
for a directory and a listing otherwise, a child per connection. No CGI, no
keep-alive, no TLS, no config file. The path check is the only
security-relevant code in it and it refuses rather than normalises -- no
component may be `..`, and percent-decoding happens first, because %2e%2e is
`..` and a check before decoding does not see it. Both forms answer 403.

And the disk is 100 MB, from 32. It is sparse, so the image is 21 MB on the
host. RAMDISK_BLOCKS went 6000 -> 8000 with it: the ramdisk was at 86% and the
next thing added would have broken `make ramroot`, which nobody would have
noticed until they used it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VQ7Qx8E1q1M1bbCq8QLHS2
2026-09-14 19:24:55 +02:00

136 lines
4.2 KiB
C

// SPDX-License-Identifier: Apache-2.0
//
// resolvconf -- put the nameserver where the resolver will look.
//
// SchOS has no DNS client, and the interesting part is that it does not need
// one. musl contains a complete resolver: `getaddrinfo` reads
// /etc/resolv.conf, sends a query over an ordinary UDP socket and parses the
// reply, and every one of those pieces already worked here. The single missing
// fact was *which* nameserver -- which arrives in a DHCP reply the kernel
// handled and never told anyone about.
//
// That is what made `wget` look broken. It was not wget, and it was not the
// core utilities: nothing on this system could turn a name into an address, so
// every program that took a hostname failed the same way and each one looked
// like its own separate bug.
//
// resolvconf wait for a nameserver, write it, exit
// resolvconf -n write whatever there is now, do not wait
// resolvconf -p print what the kernel has, change nothing
//
// The kernel publishes /proc/resolv.conf and stops there, deliberately: it
// renders resolv.conf's exact syntax so this is a copy rather than a
// translation, and *whether* /etc/resolv.conf should be overwritten is a
// policy question. A machine with a hand-written one should keep it, which is
// why -f exists and why it is not the default.
#include "types.h"
#include "user.h"
#include "fcntl.h"
#define SRC "/proc/resolv.conf"
#define DST "/etc/resolv.conf"
// DHCP takes a few seconds on a quiet network and longer on a busy one. Two
// minutes is past any of that and short enough that a machine with no DHCP
// server does not hold a boot job open for ever.
#define WAIT_SECONDS 120
static void
puts_(const char *s)
{
int n = 0;
while (s[n]) n++;
write(1, (void *)s, n);
}
static int
slurp(const char *path, char *buf, int cap)
{
int fd = open((char *)path, O_RDONLY);
int n, len = 0;
if (fd < 0)
return -1;
while (len < cap - 1 && (n = read(fd, buf + len, cap - 1 - len)) > 0)
len += n;
close(fd);
buf[len] = 0;
return len;
}
int
main(int argc, char *argv[])
{
char buf[512];
int i, n, fd, wait = 1, force = 0, print = 0;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-' || argv[i][2] != 0)
goto usage;
switch (argv[i][1]) {
case 'n': wait = 0; break;
case 'f': force = 1; break;
case 'p': print = 1; wait = 0; break;
default:
usage:
puts_("usage: resolvconf [-n] [-f] [-p]\n"
" -n do not wait for DHCP\n"
" -f overwrite an existing " DST "\n"
" -p print what the kernel has and exit\n");
return 1;
}
}
// Wait by polling rather than by blocking on the file: /proc has no
// readiness to report and inventing one for this would be a worse
// interface than a loop that ends.
for (i = 0; ; i++) {
n = slurp(SRC, buf, sizeof(buf));
if (n < 0) {
puts_("resolvconf: no " SRC " -- this kernel has no network\n");
return 1;
}
if (n > 0 || !wait || i >= WAIT_SECONDS)
break;
sleep(100); // one second, at 100 Hz
}
if (print) {
if (n > 0) write(1, buf, n);
else puts_("resolvconf: the kernel has no nameserver yet\n");
return n > 0 ? 0 : 1;
}
if (n == 0) {
puts_("resolvconf: no nameserver after ");
puts_(wait ? "two minutes" : "no wait");
puts_(" -- is there a DHCP server?\n");
return 1;
}
// An existing file is somebody's decision until told otherwise.
if (!force) {
char old[512];
if (slurp(DST, old, sizeof(old)) > 0) {
puts_("resolvconf: " DST " already exists; -f to replace it\n");
return 0;
}
}
fd = open(DST, O_WRONLY | O_CREAT | O_TRUNC);
if (fd < 0) {
puts_("resolvconf: cannot write " DST "\n");
return 1;
}
if (write(fd, buf, n) != n) {
puts_("resolvconf: short write to " DST "\n");
close(fd);
return 1;
}
close(fd);
puts_("resolvconf: ");
write(1, buf, n);
return 0;
}