xref: /unit/test/go/ns_inspect/app.go (revision 1182)
1*1182St.nateldemoura@f5.compackage main
2*1182St.nateldemoura@f5.com
3*1182St.nateldemoura@f5.comimport (
4*1182St.nateldemoura@f5.com	"encoding/json"
5*1182St.nateldemoura@f5.com	"fmt"
6*1182St.nateldemoura@f5.com	"net/http"
7*1182St.nateldemoura@f5.com	"nginx/unit"
8*1182St.nateldemoura@f5.com	"os"
9*1182St.nateldemoura@f5.com	"strconv"
10*1182St.nateldemoura@f5.com)
11*1182St.nateldemoura@f5.com
12*1182St.nateldemoura@f5.comtype (
13*1182St.nateldemoura@f5.com	NS struct {
14*1182St.nateldemoura@f5.com		USER   uint64
15*1182St.nateldemoura@f5.com		PID    uint64
16*1182St.nateldemoura@f5.com		IPC    uint64
17*1182St.nateldemoura@f5.com		CGROUP uint64
18*1182St.nateldemoura@f5.com		UTS    uint64
19*1182St.nateldemoura@f5.com		MNT    uint64
20*1182St.nateldemoura@f5.com		NET    uint64
21*1182St.nateldemoura@f5.com	}
22*1182St.nateldemoura@f5.com
23*1182St.nateldemoura@f5.com	Output struct {
24*1182St.nateldemoura@f5.com		PID int
25*1182St.nateldemoura@f5.com		UID int
26*1182St.nateldemoura@f5.com		GID int
27*1182St.nateldemoura@f5.com		NS  NS
28*1182St.nateldemoura@f5.com	}
29*1182St.nateldemoura@f5.com)
30*1182St.nateldemoura@f5.com
31*1182St.nateldemoura@f5.comfunc abortonerr(err error) {
32*1182St.nateldemoura@f5.com	if err != nil {
33*1182St.nateldemoura@f5.com		panic(err)
34*1182St.nateldemoura@f5.com	}
35*1182St.nateldemoura@f5.com}
36*1182St.nateldemoura@f5.com
37*1182St.nateldemoura@f5.com// returns: [nstype]:[4026531835]
38*1182St.nateldemoura@f5.comfunc getns(nstype string) uint64 {
39*1182St.nateldemoura@f5.com	str, err := os.Readlink(fmt.Sprintf("/proc/self/ns/%s", nstype))
40*1182St.nateldemoura@f5.com	if err != nil {
41*1182St.nateldemoura@f5.com		return 0
42*1182St.nateldemoura@f5.com	}
43*1182St.nateldemoura@f5.com
44*1182St.nateldemoura@f5.com	str = str[len(nstype)+2:]
45*1182St.nateldemoura@f5.com	str = str[:len(str)-1]
46*1182St.nateldemoura@f5.com	val, err := strconv.ParseUint(str, 10, 64)
47*1182St.nateldemoura@f5.com	abortonerr(err)
48*1182St.nateldemoura@f5.com	return val
49*1182St.nateldemoura@f5.com}
50*1182St.nateldemoura@f5.com
51*1182St.nateldemoura@f5.comfunc handler(w http.ResponseWriter, r *http.Request) {
52*1182St.nateldemoura@f5.com	pid := os.Getpid()
53*1182St.nateldemoura@f5.com	out := &Output{
54*1182St.nateldemoura@f5.com		PID: pid,
55*1182St.nateldemoura@f5.com		UID: os.Getuid(),
56*1182St.nateldemoura@f5.com		GID: os.Getgid(),
57*1182St.nateldemoura@f5.com		NS: NS{
58*1182St.nateldemoura@f5.com			PID:    getns("pid"),
59*1182St.nateldemoura@f5.com			USER:   getns("user"),
60*1182St.nateldemoura@f5.com			MNT:    getns("mnt"),
61*1182St.nateldemoura@f5.com			IPC:    getns("ipc"),
62*1182St.nateldemoura@f5.com			UTS:    getns("uts"),
63*1182St.nateldemoura@f5.com			NET:    getns("net"),
64*1182St.nateldemoura@f5.com			CGROUP: getns("cgroup"),
65*1182St.nateldemoura@f5.com		},
66*1182St.nateldemoura@f5.com	}
67*1182St.nateldemoura@f5.com	data, err := json.Marshal(out)
68*1182St.nateldemoura@f5.com	if err != nil {
69*1182St.nateldemoura@f5.com		w.WriteHeader(http.StatusInternalServerError)
70*1182St.nateldemoura@f5.com		return
71*1182St.nateldemoura@f5.com	}
72*1182St.nateldemoura@f5.com
73*1182St.nateldemoura@f5.com	w.Write(data)
74*1182St.nateldemoura@f5.com}
75*1182St.nateldemoura@f5.com
76*1182St.nateldemoura@f5.comfunc main() {
77*1182St.nateldemoura@f5.com	http.HandleFunc("/", handler)
78*1182St.nateldemoura@f5.com	unit.ListenAndServe(":7080", nil)
79*1182St.nateldemoura@f5.com}
80