1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
// hosty-cgo-hello is the smallest app that exercises what a dynamically linked
// hosty app needs at runtime. It exists for system-test.nix only.
//
// Every other test app is a statically linked Go binary, which runs inside its
// image whether or not the packed closure is complete. This one is built with
// cgo and calls into libc on purpose, so it is linked against an ELF
// interpreter at an absolute /nix/store path. Under RootDirectory= the host
// store is not there, so the process only gets as far as main() if the image
// carries that closure itself.
//
// Besides that it does just what the test asserts on:
//
//   - HOST is required config, so a first start without it must fail;
//   - on start it writes identity.json to $HOSTY_FS if it does not exist yet
//     (and must keep it across restarts) and rewrites config.json from HOST,
//     which exercises writes through the FUSE mount;
//   - it serves HTTP on $HOSTY_PORT.
package main

/*
#include <unistd.h>
*/
import "C"

import (
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"io/fs"
	"net/http"
	"os"
	"path/filepath"
)

func main() {
	if err := run(); err != nil {
		fmt.Fprintf(os.Stderr, "hosty-cgo-hello: %v\n", err)
		os.Exit(1)
	}
}

func run() error {
	port := os.Getenv("HOSTY_PORT")
	if port == "" {
		return errors.New("HOSTY_PORT not set")
	}
	fsDir := os.Getenv("HOSTY_FS")
	if fsDir == "" {
		return errors.New("HOSTY_FS not set")
	}
	host := os.Getenv("HOST")
	if host == "" {
		return errors.New("HOST config value not set")
	}

	ident, err := loadOrCreateIdentity(filepath.Join(fsDir, "identity.json"))
	if err != nil {
		return fmt.Errorf("identity: %w", err)
	}
	if err := writeJSON(filepath.Join(fsDir, "config.json"), map[string]string{"host": host}); err != nil {
		return fmt.Errorf("writing config.json: %w", err)
	}

	// The libc call is the point of the binary: it keeps it dynamically linked
	// even if nothing else in the Go stdlib would pull in cgo.
	pid := int(C.getpid())

	http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "hello from %s (pid %d, identity %s)\n", host, pid, ident)
	})
	fmt.Fprintf(os.Stderr, "hosty-cgo-hello: listening on 127.0.0.1:%s (host=%s)\n", port, host)
	return http.ListenAndServe("127.0.0.1:"+port, nil)
}

// loadOrCreateIdentity returns the identity stored at path, minting and
// persisting a fresh one only if there is none: like a federated server's
// keypair, it must never change once created.
func loadOrCreateIdentity(path string) (string, error) {
	var stored struct {
		Identity string `json:"identity"`
	}
	data, err := os.ReadFile(path)
	if err == nil {
		if err := json.Unmarshal(data, &stored); err != nil {
			return "", fmt.Errorf("parsing %s: %w", path, err)
		}
		return stored.Identity, nil
	}
	if !errors.Is(err, fs.ErrNotExist) {
		return "", err
	}
	b := make([]byte, 16)
	if _, err := rand.Read(b); err != nil {
		return "", err
	}
	stored.Identity = hex.EncodeToString(b)
	if err := writeJSON(path, stored); err != nil {
		return "", err
	}
	return stored.Identity, nil
}

func writeJSON(path string, v any) error {
	b, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		return err
	}
	return os.WriteFile(path, b, 0644)
}