service_linux.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 Daniel Theophanes.
  2. // Use of this source code is governed by a zlib-style
  3. // license that can be found in the LICENSE file.
  4. package service
  5. import (
  6. "os"
  7. "strings"
  8. )
  9. type linuxSystemService struct {
  10. name string
  11. detect func() bool
  12. interactive func() bool
  13. new func(i Interface, c *Config) (Service, error)
  14. }
  15. func (sc linuxSystemService) String() string {
  16. return sc.name
  17. }
  18. func (sc linuxSystemService) Detect() bool {
  19. return sc.detect()
  20. }
  21. func (sc linuxSystemService) Interactive() bool {
  22. return sc.interactive()
  23. }
  24. func (sc linuxSystemService) New(i Interface, c *Config) (Service, error) {
  25. return sc.new(i, c)
  26. }
  27. func init() {
  28. ChooseSystem(linuxSystemService{
  29. name: "linux-systemd",
  30. detect: isSystemd,
  31. interactive: func() bool {
  32. is, _ := isInteractive()
  33. return is
  34. },
  35. new: newSystemdService,
  36. },
  37. // linuxSystemService{
  38. // name: "linux-upstart",
  39. // detect: isUpstart,
  40. // interactive: func() bool {
  41. // is, _ := isInteractive()
  42. // return is
  43. // },
  44. // new: newUpstartService,
  45. // },
  46. linuxSystemService{
  47. name: "unix-systemv",
  48. detect: func() bool { return true },
  49. interactive: func() bool {
  50. is, _ := isInteractive()
  51. return is
  52. },
  53. new: newSystemVService,
  54. },
  55. )
  56. }
  57. func isInteractive() (bool, error) {
  58. // TODO: This is not true for user services.
  59. return os.Getppid() != 1, nil
  60. }
  61. var tf = map[string]interface{}{
  62. "cmd": func(s string) string {
  63. return `"` + strings.Replace(s, `"`, `\"`, -1) + `"`
  64. },
  65. "cmdEscape": func(s string) string {
  66. return strings.Replace(s, " ", `\x20`, -1)
  67. },
  68. }