xref: /unit/test/go/ns_inspect/app.go (revision 1490)
11182St.nateldemoura@f5.compackage main
21182St.nateldemoura@f5.com
31182St.nateldemoura@f5.comimport (
41182St.nateldemoura@f5.com	"encoding/json"
51182St.nateldemoura@f5.com	"fmt"
61182St.nateldemoura@f5.com	"net/http"
71329Smax.romanov@nginx.com	"unit.nginx.org/go"
81182St.nateldemoura@f5.com	"os"
91182St.nateldemoura@f5.com	"strconv"
101182St.nateldemoura@f5.com)
111182St.nateldemoura@f5.com
121182St.nateldemoura@f5.comtype (
131182St.nateldemoura@f5.com	NS struct {
141182St.nateldemoura@f5.com		USER   uint64
151182St.nateldemoura@f5.com		PID    uint64
161182St.nateldemoura@f5.com		IPC    uint64
171182St.nateldemoura@f5.com		CGROUP uint64
181182St.nateldemoura@f5.com		UTS    uint64
191182St.nateldemoura@f5.com		MNT    uint64
201182St.nateldemoura@f5.com		NET    uint64
211182St.nateldemoura@f5.com	}
221182St.nateldemoura@f5.com
231182St.nateldemoura@f5.com	Output struct {
24*1490St.nateldemoura@f5.com		PID        int
25*1490St.nateldemoura@f5.com		UID        int
26*1490St.nateldemoura@f5.com		GID        int
27*1490St.nateldemoura@f5.com		NS         NS
28*1490St.nateldemoura@f5.com		FileExists bool
291182St.nateldemoura@f5.com	}
301182St.nateldemoura@f5.com)
311182St.nateldemoura@f5.com
321182St.nateldemoura@f5.comfunc abortonerr(err error) {
331182St.nateldemoura@f5.com	if err != nil {
341182St.nateldemoura@f5.com		panic(err)
351182St.nateldemoura@f5.com	}
361182St.nateldemoura@f5.com}
371182St.nateldemoura@f5.com
381182St.nateldemoura@f5.com// returns: [nstype]:[4026531835]
391182St.nateldemoura@f5.comfunc getns(nstype string) uint64 {
401182St.nateldemoura@f5.com	str, err := os.Readlink(fmt.Sprintf("/proc/self/ns/%s", nstype))
411182St.nateldemoura@f5.com	if err != nil {
421182St.nateldemoura@f5.com		return 0
431182St.nateldemoura@f5.com	}
441182St.nateldemoura@f5.com
451182St.nateldemoura@f5.com	str = str[len(nstype)+2:]
461182St.nateldemoura@f5.com	str = str[:len(str)-1]
471182St.nateldemoura@f5.com	val, err := strconv.ParseUint(str, 10, 64)
481182St.nateldemoura@f5.com	abortonerr(err)
491182St.nateldemoura@f5.com	return val
501182St.nateldemoura@f5.com}
511182St.nateldemoura@f5.com
521182St.nateldemoura@f5.comfunc handler(w http.ResponseWriter, r *http.Request) {
531182St.nateldemoura@f5.com	pid := os.Getpid()
541182St.nateldemoura@f5.com	out := &Output{
551182St.nateldemoura@f5.com		PID: pid,
561182St.nateldemoura@f5.com		UID: os.Getuid(),
571182St.nateldemoura@f5.com		GID: os.Getgid(),
581182St.nateldemoura@f5.com		NS: NS{
591182St.nateldemoura@f5.com			PID:    getns("pid"),
601182St.nateldemoura@f5.com			USER:   getns("user"),
611182St.nateldemoura@f5.com			MNT:    getns("mnt"),
621182St.nateldemoura@f5.com			IPC:    getns("ipc"),
631182St.nateldemoura@f5.com			UTS:    getns("uts"),
641182St.nateldemoura@f5.com			NET:    getns("net"),
651182St.nateldemoura@f5.com			CGROUP: getns("cgroup"),
661182St.nateldemoura@f5.com		},
671182St.nateldemoura@f5.com	}
68*1490St.nateldemoura@f5.com
69*1490St.nateldemoura@f5.com	err := r.ParseForm()
70*1490St.nateldemoura@f5.com	if err != nil {
71*1490St.nateldemoura@f5.com		w.WriteHeader(http.StatusInternalServerError)
72*1490St.nateldemoura@f5.com		return
73*1490St.nateldemoura@f5.com	}
74*1490St.nateldemoura@f5.com
75*1490St.nateldemoura@f5.com	if fname := r.Form.Get("file"); fname != "" {
76*1490St.nateldemoura@f5.com		_, err = os.Stat(fname);
77*1490St.nateldemoura@f5.com		out.FileExists = err == nil
78*1490St.nateldemoura@f5.com	}
79*1490St.nateldemoura@f5.com
801182St.nateldemoura@f5.com	data, err := json.Marshal(out)
811182St.nateldemoura@f5.com	if err != nil {
821182St.nateldemoura@f5.com		w.WriteHeader(http.StatusInternalServerError)
831182St.nateldemoura@f5.com		return
841182St.nateldemoura@f5.com	}
851182St.nateldemoura@f5.com
861296St.nateldemoura@f5.com	w.Header().Add("Content-Type", "application/json")
871296St.nateldemoura@f5.com
881182St.nateldemoura@f5.com	w.Write(data)
891182St.nateldemoura@f5.com}
901182St.nateldemoura@f5.com
911182St.nateldemoura@f5.comfunc main() {
921182St.nateldemoura@f5.com	http.HandleFunc("/", handler)
931182St.nateldemoura@f5.com	unit.ListenAndServe(":7080", nil)
941182St.nateldemoura@f5.com}
95