service.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package cli
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "os/signal"
  19. "path"
  20. "strconv"
  21. "syscall"
  22. "zerotier/pkg/zerotier"
  23. )
  24. func Service(basePath string, args []string) int {
  25. if len(args) > 0 {
  26. Help()
  27. return 1
  28. }
  29. pidPath := path.Join(basePath, "zerotier.pid")
  30. _ = ioutil.WriteFile(pidPath, []byte(strconv.FormatInt(int64(os.Getpid()), 10)), 0644)
  31. node, err := zerotier.NewNode(basePath)
  32. if err != nil {
  33. fmt.Println("FATAL: error initializing node: " + err.Error())
  34. } else {
  35. osSignalChannel := make(chan os.Signal, 2)
  36. signal.Notify(osSignalChannel, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGINT)
  37. <-osSignalChannel
  38. node.Close()
  39. }
  40. _ = os.Remove(pidPath)
  41. return 0
  42. }