blob: baeb1e5c0a1003c40f162d24226be02f48e53669 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Lorenz Brun8b0431a2020-07-13 16:56:36 +02002// SPDX-License-Identifier: Apache-2.0
Lorenz Brun8b0431a2020-07-13 16:56:36 +02003
4package main
5
Serge Bazanski591d8082023-03-16 00:26:59 +01006import (
7 "fmt"
8 "net/http"
Lorenz Brun2ecccae2024-11-27 22:03:35 +01009 "os"
10 "strconv"
11 "strings"
Serge Bazanski591d8082023-03-16 00:26:59 +010012)
Lorenz Brun8b0431a2020-07-13 16:56:36 +020013
14func main() {
15 fmt.Println("Hello world from preseeded image")
Serge Bazanski591d8082023-03-16 00:26:59 +010016 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
17 fmt.Fprintf(w, "Hello world from preseeded image\n")
18 })
Lorenz Brun2ecccae2024-11-27 22:03:35 +010019 http.HandleFunc("/ready_userns", func(w http.ResponseWriter, r *http.Request) {
20 uidMapRaw, err := os.ReadFile("/proc/self/uid_map")
21 if err != nil {
22 http.Error(w, err.Error(), http.StatusInternalServerError)
23 return
24 }
25 uidMapFields := strings.Fields(string(uidMapRaw))
26 if len(uidMapFields) != 3 {
27 http.Error(w, fmt.Sprintf("Bad uid_map contents, not 3 fields: %q", string(uidMapRaw)), http.StatusInternalServerError)
28 return
29 }
30 startId, err := strconv.ParseUint(uidMapFields[1], 10, 64)
31 if err != nil {
32 http.Error(w, fmt.Sprintf("while parsing start ID: %v", err), http.StatusInternalServerError)
33 return
34 }
35 if startId == 0 {
36 http.Error(w, "Not in a non-initial user namespace, UID space starts at 0", http.StatusInternalServerError)
37 return
38 }
39 fmt.Fprintf(w, "Hello world from a user namespace\n")
40 })
Serge Bazanski591d8082023-03-16 00:26:59 +010041 err := http.ListenAndServe(":80", nil)
42 if err != nil {
43 fmt.Printf("Serve failed: %v\n", err)
44 }
Lorenz Brun8b0431a2020-07-13 16:56:36 +020045}