Beispiel #1
0
// If Group is configured, lookup and set Gid
func (p *Process) lookupGid(credential *syscall.Credential) error {
	if p.Gid == "" {
		return nil
	}

	gid, err := LookupGroupId(p.Gid)
	if err != nil {
		return err
	}

	credential.Gid = uint32(gid)

	if p.Uid == "" {
		credential.Uid = uint32(os.Getuid())
	}

	return nil
}
Beispiel #2
0
// If User is configured, lookup and set Uid
func (p *Process) lookupUid(credential *syscall.Credential) error {
	if p.Uid == "" {
		return nil
	}

	id, err := user.Lookup(p.Uid)
	if err != nil {
		return err
	}

	uid, _ := strconv.Atoi(id.Uid)
	gid, _ := strconv.Atoi(id.Gid)

	credential.Uid = uint32(uid)

	if p.Gid == "" {
		credential.Gid = uint32(gid)
	}

	return nil
}
Beispiel #3
0
func main() {
	flag.Parse()
	var err error
	//fmt.Printf("start: %v\n", os.Getpid())
	if *ncpu != 0 {
		runtime.GOMAXPROCS(*ncpu)
	}
	if *conf == "no" {
		fmt.Printf("No configure dir")
		os.Exit(1)
	}
	var nullFile *os.File
	var userinfo *user.User
	var credential *syscall.Credential
	if nullFile, err = os.Open(os.DevNull); err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}

	if os.Getuid() == 0 {
		if userinfo, err = user.Lookup(*username); err != nil {
			fmt.Printf("%v\n", err)
			os.Exit(1)
		}

		credential = new(syscall.Credential)
		var i int
		i, _ = strconv.Atoi(userinfo.Uid)
		credential.Uid = uint32(i)
		i, _ = strconv.Atoi(userinfo.Gid)
		credential.Gid = uint32(i)
	}
	if err = Daemon(
		pidfile,
		[]*os.File{nullFile, os.Stdin, os.Stderr},
		credential,
	); err != nil {
		fmt.Printf("%v\n", err)
		os.Exit(1)
	}
	go func(ipdispch chan *ipzone.IPDisp, action chan ipdAction, result chan ipdAction) {
		var ipdispIns = ipzone.New()
		err = ipdispIns.Init(*conf)
		if err != nil {
			fmt.Printf("Init false: %v\n", err)
			os.Remove(*pidfile)
			os.Exit(1)
		}
		ipdispch <- ipdispIns
		for {
			select {
			case doAction := <-action:
				switch {
				case doAction.action == "get":
					pm := doAction.param
					doAction.result = ipdispIns.GetCount(pm["host"], pm["node"], pm["last"])
				case doAction.action == "query":
					pm := doAction.param
					ip, zone, _ := ipdispIns.Query(pm["clip"], pm["host"], pm["path"])
					toip := make(map[string]string)
					toip["ip"] = ip
					toip["zonename"] = zone
					doAction.result = toip
				case doAction.action == "set":
					pm := doAction.param
					vv := doAction.result.([]string)
					err := ipdispIns.Set(pm["host"], pm["object"], vv)
					doAction.result = false
					if err == nil {
						doAction.result = true
					}
				}
				result <- doAction
			}
		}
	}(ipdCH, ipdActionCH, ipdResultCH)
	select {
	case ipdisp = <-ipdCH:
		break
	case <-time.After(time.Duration(3) * time.Second):
		fmt.Printf("Init false.\n")
	}
	gracehttp.Serve(&http.Server{Addr: *lport,
		Handler:        ipDisp(),
		ReadTimeout:    10 * time.Second,
		WriteTimeout:   10 * time.Second,
		MaxHeaderBytes: 2048})

}