func run(s service.Service, m *supervisorgo.Manager, u *url.URL) int { var err error var l net.Listener path := jsonrpc.DefaultRPCPath switch u.Scheme { case "unix": l, err = net.Listen(u.Scheme, u.Opaque+u.Path) if l != nil { defer os.Remove(u.Opaque + u.Path) } case "http": l, err = net.Listen("tcp", u.Host) path = u.Path default: err = fmt.Errorf("not supported scheme: %s", u.Scheme) } if err != nil { log.Println(err) return 1 } m.HTTPServe(path) go http.Serve(l, nil) err = s.Run() if err != nil { log.Println(err) return 1 } return 0 }
func uninstallService(s service.Service) { err := s.Uninstall() if err != nil { log.Println(err) } else { log.Println("IPSecDiagTool Daemon successfully uninstalled.") } }
func (p *program) Start(s service.Service) error { msg := fmt.Sprintf("Service '%s' started.", serviceName()) logger.Printf(msg) sysLogger, err := s.Logger(nil) if err != nil { sysLogger.Info(msg) } doStart() go watchForReload() return nil }
func (p *program) runHttp(s service.Service) { var cmds []string = []string{"shut", "reboot"} for index, command := range cmds { http.HandleFunc(fmt.Sprintf("/%s", command), handleFunc(index, command)) } logger.Info("http server start ...") err := http.ListenAndServe(":7070", nil) if err != nil { logger.Error(err) s.Stop() return } }
func (p *program) runUDP(s service.Service) { localAddr := getLocalAddr() if localAddr == nil { s.Stop() return } socket, err := net.ListenUDP("udp4", &net.UDPAddr{ IP: net.IPv4(0, 0, 0, 0), Port: 6060, }) if err != nil { logger.Error(err) s.Stop() return } logger.Info("udp server has start ...") defer socket.Close() out := make([]byte, 1024) for { select { case <-exit: return default: _, addr, err := socket.ReadFromUDP(out) if err != nil { logger.Error(err) continue } //logger.Infof("receive [%v] data : %s \n", addr.IP, string(out)) hostname, err := os.Hostname() if err != nil { logger.Error(err) hostname = "unknown" } resp := map[string]string{ "ip": localAddr["IP"], "hostname": hostname, } r, err := json.Marshal(resp) if err != nil { logger.Error(err) continue } socket.WriteToUDP(r, &net.UDPAddr{ IP: addr.IP, Port: 6060, }) } } }
func (p *program) Stop(s service.Service) error { logger.Printf(fmt.Sprintf("Stopping '%s' service...", serviceName())) doStop() pidFile := config.ServiceConfig.PidFile if pidFile != "" { os.Remove(pidFile) } msg := fmt.Sprintf("Stopped '%s' service.", serviceName()) logger.Printf(msg) sysLogger, err := s.Logger(nil) if err != nil { sysLogger.Info(msg) } return nil }