xref: /unit/test/go/ns_inspect/app.go (revision 1296)
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"
71182St.nateldemoura@f5.com	"nginx/unit"
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 {
241182St.nateldemoura@f5.com		PID int
251182St.nateldemoura@f5.com		UID int
261182St.nateldemoura@f5.com		GID int
271182St.nateldemoura@f5.com		NS  NS
281182St.nateldemoura@f5.com	}
291182St.nateldemoura@f5.com)
301182St.nateldemoura@f5.com
311182St.nateldemoura@f5.comfunc abortonerr(err error) {
321182St.nateldemoura@f5.com	if err != nil {
331182St.nateldemoura@f5.com		panic(err)
341182St.nateldemoura@f5.com	}
351182St.nateldemoura@f5.com}
361182St.nateldemoura@f5.com
371182St.nateldemoura@f5.com// returns: [nstype]:[4026531835]
381182St.nateldemoura@f5.comfunc getns(nstype string) uint64 {
391182St.nateldemoura@f5.com	str, err := os.Readlink(fmt.Sprintf("/proc/self/ns/%s", nstype))
401182St.nateldemoura@f5.com	if err != nil {
411182St.nateldemoura@f5.com		return 0
421182St.nateldemoura@f5.com	}
431182St.nateldemoura@f5.com
441182St.nateldemoura@f5.com	str = str[len(nstype)+2:]
451182St.nateldemoura@f5.com	str = str[:len(str)-1]
461182St.nateldemoura@f5.com	val, err := strconv.ParseUint(str, 10, 64)
471182St.nateldemoura@f5.com	abortonerr(err)
481182St.nateldemoura@f5.com	return val
491182St.nateldemoura@f5.com}
501182St.nateldemoura@f5.com
511182St.nateldemoura@f5.comfunc handler(w http.ResponseWriter, r *http.Request) {
521182St.nateldemoura@f5.com	pid := os.Getpid()
531182St.nateldemoura@f5.com	out := &Output{
541182St.nateldemoura@f5.com		PID: pid,
551182St.nateldemoura@f5.com		UID: os.Getuid(),
561182St.nateldemoura@f5.com		GID: os.Getgid(),
571182St.nateldemoura@f5.com		NS: NS{
581182St.nateldemoura@f5.com			PID:    getns("pid"),
591182St.nateldemoura@f5.com			USER:   getns("user"),
601182St.nateldemoura@f5.com			MNT:    getns("mnt"),
611182St.nateldemoura@f5.com			IPC:    getns("ipc"),
621182St.nateldemoura@f5.com			UTS:    getns("uts"),
631182St.nateldemoura@f5.com			NET:    getns("net"),
641182St.nateldemoura@f5.com			CGROUP: getns("cgroup"),
651182St.nateldemoura@f5.com		},
661182St.nateldemoura@f5.com	}
671182St.nateldemoura@f5.com	data, err := json.Marshal(out)
681182St.nateldemoura@f5.com	if err != nil {
691182St.nateldemoura@f5.com		w.WriteHeader(http.StatusInternalServerError)
701182St.nateldemoura@f5.com		return
711182St.nateldemoura@f5.com	}
721182St.nateldemoura@f5.com
73*1296St.nateldemoura@f5.com	w.Header().Add("Content-Type", "application/json")
74*1296St.nateldemoura@f5.com
751182St.nateldemoura@f5.com	w.Write(data)
761182St.nateldemoura@f5.com}
771182St.nateldemoura@f5.com
781182St.nateldemoura@f5.comfunc main() {
791182St.nateldemoura@f5.com	http.HandleFunc("/", handler)
801182St.nateldemoura@f5.com	unit.ListenAndServe(":7080", nil)
811182St.nateldemoura@f5.com}
82