xref: /unit/test/go/ns_inspect/app.go (revision 1707:d718554dd379)
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"
10*1707St.nateldemoura@f5.com	"io/ioutil"
111182St.nateldemoura@f5.com)
121182St.nateldemoura@f5.com
131182St.nateldemoura@f5.comtype (
141182St.nateldemoura@f5.com	NS struct {
151182St.nateldemoura@f5.com		USER   uint64
161182St.nateldemoura@f5.com		PID    uint64
171182St.nateldemoura@f5.com		IPC    uint64
181182St.nateldemoura@f5.com		CGROUP uint64
191182St.nateldemoura@f5.com		UTS    uint64
201182St.nateldemoura@f5.com		MNT    uint64
211182St.nateldemoura@f5.com		NET    uint64
221182St.nateldemoura@f5.com	}
231182St.nateldemoura@f5.com
241182St.nateldemoura@f5.com	Output struct {
251490St.nateldemoura@f5.com		PID        int
261490St.nateldemoura@f5.com		UID        int
271490St.nateldemoura@f5.com		GID        int
281490St.nateldemoura@f5.com		NS         NS
291490St.nateldemoura@f5.com		FileExists bool
30*1707St.nateldemoura@f5.com		Mounts     string
311182St.nateldemoura@f5.com	}
321182St.nateldemoura@f5.com)
331182St.nateldemoura@f5.com
341182St.nateldemoura@f5.comfunc abortonerr(err error) {
351182St.nateldemoura@f5.com	if err != nil {
361182St.nateldemoura@f5.com		panic(err)
371182St.nateldemoura@f5.com	}
381182St.nateldemoura@f5.com}
391182St.nateldemoura@f5.com
401182St.nateldemoura@f5.com// returns: [nstype]:[4026531835]
411182St.nateldemoura@f5.comfunc getns(nstype string) uint64 {
421182St.nateldemoura@f5.com	str, err := os.Readlink(fmt.Sprintf("/proc/self/ns/%s", nstype))
431182St.nateldemoura@f5.com	if err != nil {
441182St.nateldemoura@f5.com		return 0
451182St.nateldemoura@f5.com	}
461182St.nateldemoura@f5.com
471182St.nateldemoura@f5.com	str = str[len(nstype)+2:]
481182St.nateldemoura@f5.com	str = str[:len(str)-1]
491182St.nateldemoura@f5.com	val, err := strconv.ParseUint(str, 10, 64)
501182St.nateldemoura@f5.com	abortonerr(err)
511182St.nateldemoura@f5.com	return val
521182St.nateldemoura@f5.com}
531182St.nateldemoura@f5.com
541182St.nateldemoura@f5.comfunc handler(w http.ResponseWriter, r *http.Request) {
551182St.nateldemoura@f5.com	pid := os.Getpid()
561182St.nateldemoura@f5.com	out := &Output{
571182St.nateldemoura@f5.com		PID: pid,
581182St.nateldemoura@f5.com		UID: os.Getuid(),
591182St.nateldemoura@f5.com		GID: os.Getgid(),
601182St.nateldemoura@f5.com		NS: NS{
611182St.nateldemoura@f5.com			PID:    getns("pid"),
621182St.nateldemoura@f5.com			USER:   getns("user"),
631182St.nateldemoura@f5.com			MNT:    getns("mnt"),
641182St.nateldemoura@f5.com			IPC:    getns("ipc"),
651182St.nateldemoura@f5.com			UTS:    getns("uts"),
661182St.nateldemoura@f5.com			NET:    getns("net"),
671182St.nateldemoura@f5.com			CGROUP: getns("cgroup"),
681182St.nateldemoura@f5.com		},
691182St.nateldemoura@f5.com	}
701490St.nateldemoura@f5.com
711490St.nateldemoura@f5.com	err := r.ParseForm()
721490St.nateldemoura@f5.com	if err != nil {
731490St.nateldemoura@f5.com		w.WriteHeader(http.StatusInternalServerError)
741490St.nateldemoura@f5.com		return
751490St.nateldemoura@f5.com	}
761490St.nateldemoura@f5.com
771490St.nateldemoura@f5.com	if fname := r.Form.Get("file"); fname != "" {
781490St.nateldemoura@f5.com		_, err = os.Stat(fname);
791490St.nateldemoura@f5.com		out.FileExists = err == nil
801490St.nateldemoura@f5.com	}
811490St.nateldemoura@f5.com
82*1707St.nateldemoura@f5.com	if mounts := r.Form.Get("mounts"); mounts != "" {
83*1707St.nateldemoura@f5.com		data, _ := ioutil.ReadFile("/proc/self/mountinfo")
84*1707St.nateldemoura@f5.com		out.Mounts = string(data)
85*1707St.nateldemoura@f5.com	}
86*1707St.nateldemoura@f5.com
871182St.nateldemoura@f5.com	data, err := json.Marshal(out)
881182St.nateldemoura@f5.com	if err != nil {
891182St.nateldemoura@f5.com		w.WriteHeader(http.StatusInternalServerError)
901182St.nateldemoura@f5.com		return
911182St.nateldemoura@f5.com	}
921182St.nateldemoura@f5.com
931296St.nateldemoura@f5.com	w.Header().Add("Content-Type", "application/json")
941296St.nateldemoura@f5.com
951182St.nateldemoura@f5.com	w.Write(data)
961182St.nateldemoura@f5.com}
971182St.nateldemoura@f5.com
981182St.nateldemoura@f5.comfunc main() {
991182St.nateldemoura@f5.com	http.HandleFunc("/", handler)
1001182St.nateldemoura@f5.com	unit.ListenAndServe(":7080", nil)
1011182St.nateldemoura@f5.com}
102