go master service supports log and configure

This commit is contained in:
zhengshuxin 2017-05-21 23:49:26 +08:00
parent 7637ada728
commit 808667c3de
8 changed files with 181 additions and 69 deletions

View File

@ -1,6 +1,6 @@
GOPATH := $(CURDIR)
all: echo web
all: echo web test
echo:
@go build echo
@ -8,7 +8,10 @@ echo:
web:
@go build web
test:
@go build test
cl clean:
@rm -f echo web
@rm -f echo web test
rb rebuild: cl all

View File

@ -3,9 +3,10 @@ package main
import (
"fmt"
"log"
"master"
"net"
"os"
"master"
)
func onAccept(conn net.Conn) {
@ -26,17 +27,6 @@ func onClose(conn net.Conn) {
}
func main() {
var logFile = "./log.txt"
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
fmt.Println("open file error", err)
return
}
log.SetOutput(f)
//log.SetOutput(io.MultiWriter(os.Stderr, f))
master.OnClose(onClose)
master.OnAccept(onAccept)

View File

@ -0,0 +1,75 @@
package master
import (
"bufio"
"io"
"os"
"strings"
)
type Config struct {
Entries map[string]string
}
func (c *Config) InitConfig(path string) {
c.Entries = make(map[string]string)
if len(path) == 0 {
return
}
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()
r := bufio.NewReader(f)
for {
line, _, err := r.ReadLine()
if err != nil {
if err == io.EOF {
break
}
panic(err)
}
s := strings.TrimSpace(string(line))
eq := strings.Index(s, "=")
if eq < 0 {
continue
}
name := strings.TrimSpace(s[:eq])
if len(name) == 0 {
continue
}
value := strings.TrimSpace(s[eq+1:])
pos := strings.Index(value, "\t#")
if pos > -1 {
value = value[0:pos]
}
pos = strings.Index(value, " #")
if pos > -1 {
value = value[0:pos]
}
if len(value) == 0 {
continue
}
c.Entries[name] = strings.TrimSpace(value)
}
}
func (c Config) Get(name string) string {
val, found := c.Entries[name]
if !found {
return ""
}
return val
}

View File

@ -55,13 +55,6 @@ func loopAccept(ln net.Listener) {
}
func OnAccept(handler AcceptFunc) {
//confPath := flag.String("f", "dummy", "service configure file")
// sockCount := flag.Int("s", 0, "socket count")
//flag.Parse()
//log.Printf("confPath: %s\r\n", *confPath)
for _, arg := range os.Args {
log.Println("arg=", arg)
}
@ -74,10 +67,14 @@ func OnClose(handler CloseFunc) {
}
func NetStart(addrs []string) {
prepare()
if preJailHandler != nil {
preJailHandler()
}
chroot()
if initHandler != nil {
initHandler()
}

View File

@ -1,17 +1,28 @@
package master
import (
"flag"
"log"
"net"
"os"
"os/user"
"strconv"
"sync"
"syscall"
"time"
)
const (
stateFd = 5
listenFdStart = 6
listenFdCount = 1
)
var (
listenFdCount int = 1
confPath string
sockType string
services string
listener bool
)
type PreJailFunc func()
@ -29,6 +40,70 @@ var (
waitExit int = 10
)
var (
logPath string
username string
masterArgs string
)
func parseArgs() {
flag.IntVar(&listenFdCount, "s", 1, "listen fd count")
flag.StringVar(&confPath, "f", "", "configure path")
flag.StringVar(&sockType, "t", "sock", "socket type")
flag.StringVar(&services, "n", "", "master services")
flag.BoolVar(&listener, "l", true, "listener")
flag.Parse()
log.Printf("listenFdCount=%d, sockType=%s, services=%s",
listenFdCount, sockType, services)
}
func prepare() {
parseArgs()
conf := new(Config)
conf.InitConfig(confPath)
logPath = conf.Get("master_log")
if len(logPath) > 0 {
f, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Println("OpenFile error", err)
} else {
log.SetOutput(f)
//log.SetOutput(io.MultiWriter(os.Stderr, f))
}
}
masterArgs = conf.Get("args")
username = conf.Get("fiber_owner")
log.Printf("Args: %s\r\n", masterArgs)
}
func chroot() {
if len(masterArgs) == 0 || len(username) == 0 {
return
}
user, err := user.Lookup(username)
if err != nil {
log.Printf("Lookup %s error %s", username, err)
return
}
gid, err := strconv.Atoi(user.Gid)
if err != nil {
log.Printf("invalid gid=%s, %s", user.Gid, err)
} else if err := syscall.Setgid(gid); err != nil {
log.Printf("Setgid error %s", err)
}
uid, err := strconv.Atoi(user.Uid)
if err != nil {
log.Printf("invalid uid=%s, %s", user.Uid, err)
} else if err := syscall.Setuid(uid); err != nil {
log.Printf("Setuid error %s", err)
}
}
func getListenersByAddrs(addrs []string) []*net.Listener {
listeners := []*net.Listener(nil)
for _, addr := range addrs {
@ -56,7 +131,8 @@ func getListeners() []*net.Listener {
return listeners
}
func monitorMaster(listeners []*net.Listener, onStopHandler func(), stopHandler func(bool)) {
func monitorMaster(listeners []*net.Listener,
onStopHandler func(), stopHandler func(bool)) {
file := os.NewFile(uintptr(stateFd), "")
conn, err1 := net.FileConn(file)

View File

@ -23,10 +23,14 @@ func webServ(ln net.Listener, daemon bool) {
}
func WebStart(addrs []string) {
prepare()
if preJailHandler != nil {
preJailHandler()
}
chroot()
if initHandler != nil {
initHandler()
}

View File

@ -1,43 +1,20 @@
package main
import (
"flag"
"fmt"
"log"
"master"
"net"
"os"
)
func onAccept(conn net.Conn) {
buf := make([]byte, 8192)
for {
n, err := conn.Read(buf)
if err != nil {
fmt.Println("read over", err)
break
}
conn.Write(buf[0:n])
}
}
func onClose(conn net.Conn) {
log.Println("---client onClose---")
}
func main() {
var logFile = "/home/zsx/tmp/acl/libexec/log.txt"
var n int
var conf string
flag.IntVar(&n, "d", 100, "")
flag.StringVar(&conf, "f", "./test.cf", "test configure")
flag.Parse()
fmt.Printf("n=%d, conf=%s\r\n", n, conf)
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
fmt.Println("open file error", err)
return
}
log.SetOutput(f)
//log.SetOutput(io.MultiWriter(os.Stderr, f))
master.OnClose(onClose)
master.OnAccept(onAccept)
master.Start()
myConf := new(master.Config)
myConf.InitConfig(conf)
fmt.Printf("log=%s\r\n", myConf.Get("master_log"))
}

View File

@ -2,10 +2,10 @@ package main
import (
"fmt"
"log"
"master"
"os"
"net/http"
"os"
"master"
)
func handler(w http.ResponseWriter, r *http.Request) {
@ -15,16 +15,6 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
func main() {
var logFile = "./log.txt"
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
fmt.Println("open file error", err)
return
}
log.SetOutput(f)
http.HandleFunc("/", handler)
if len(os.Args) > 1 && os.Args[1] == "alone" {