service_windows.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. "fmt"
  7. "os"
  8. "os/signal"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "golang.org/x/sys/windows/registry"
  14. "golang.org/x/sys/windows/svc"
  15. "golang.org/x/sys/windows/svc/eventlog"
  16. "golang.org/x/sys/windows/svc/mgr"
  17. )
  18. const version = "windows-service"
  19. type windowsService struct {
  20. i Interface
  21. *Config
  22. errSync sync.Mutex
  23. stopStartErr error
  24. }
  25. // WindowsLogger allows using windows specific logging methods.
  26. type WindowsLogger struct {
  27. ev *eventlog.Log
  28. errs chan<- error
  29. }
  30. type windowsSystem struct{}
  31. func (windowsSystem) String() string {
  32. return version
  33. }
  34. func (windowsSystem) Detect() bool {
  35. return true
  36. }
  37. func (windowsSystem) Interactive() bool {
  38. return interactive
  39. }
  40. func (windowsSystem) New(i Interface, c *Config) (Service, error) {
  41. ws := &windowsService{
  42. i: i,
  43. Config: c,
  44. }
  45. return ws, nil
  46. }
  47. func init() {
  48. ChooseSystem(windowsSystem{})
  49. }
  50. func (l WindowsLogger) send(err error) error {
  51. if err == nil {
  52. return nil
  53. }
  54. if l.errs != nil {
  55. l.errs <- err
  56. }
  57. return err
  58. }
  59. // Error logs an error message.
  60. func (l WindowsLogger) Error(v ...interface{}) error {
  61. return l.send(l.ev.Error(3, fmt.Sprint(v...)))
  62. }
  63. // Warning logs an warning message.
  64. func (l WindowsLogger) Warning(v ...interface{}) error {
  65. return l.send(l.ev.Warning(2, fmt.Sprint(v...)))
  66. }
  67. // Info logs an info message.
  68. func (l WindowsLogger) Info(v ...interface{}) error {
  69. return l.send(l.ev.Info(1, fmt.Sprint(v...)))
  70. }
  71. // Errorf logs an error message.
  72. func (l WindowsLogger) Errorf(format string, a ...interface{}) error {
  73. return l.send(l.ev.Error(3, fmt.Sprintf(format, a...)))
  74. }
  75. // Warningf logs an warning message.
  76. func (l WindowsLogger) Warningf(format string, a ...interface{}) error {
  77. return l.send(l.ev.Warning(2, fmt.Sprintf(format, a...)))
  78. }
  79. // Infof logs an info message.
  80. func (l WindowsLogger) Infof(format string, a ...interface{}) error {
  81. return l.send(l.ev.Info(1, fmt.Sprintf(format, a...)))
  82. }
  83. // NError logs an error message and an event ID.
  84. func (l WindowsLogger) NError(eventID uint32, v ...interface{}) error {
  85. return l.send(l.ev.Error(eventID, fmt.Sprint(v...)))
  86. }
  87. // NWarning logs an warning message and an event ID.
  88. func (l WindowsLogger) NWarning(eventID uint32, v ...interface{}) error {
  89. return l.send(l.ev.Warning(eventID, fmt.Sprint(v...)))
  90. }
  91. // NInfo logs an info message and an event ID.
  92. func (l WindowsLogger) NInfo(eventID uint32, v ...interface{}) error {
  93. return l.send(l.ev.Info(eventID, fmt.Sprint(v...)))
  94. }
  95. // NErrorf logs an error message and an event ID.
  96. func (l WindowsLogger) NErrorf(eventID uint32, format string, a ...interface{}) error {
  97. return l.send(l.ev.Error(eventID, fmt.Sprintf(format, a...)))
  98. }
  99. // NWarningf logs an warning message and an event ID.
  100. func (l WindowsLogger) NWarningf(eventID uint32, format string, a ...interface{}) error {
  101. return l.send(l.ev.Warning(eventID, fmt.Sprintf(format, a...)))
  102. }
  103. // NInfof logs an info message and an event ID.
  104. func (l WindowsLogger) NInfof(eventID uint32, format string, a ...interface{}) error {
  105. return l.send(l.ev.Info(eventID, fmt.Sprintf(format, a...)))
  106. }
  107. var interactive = false
  108. func init() {
  109. var err error
  110. interactive, err = svc.IsAnInteractiveSession()
  111. if err != nil {
  112. panic(err)
  113. }
  114. }
  115. func (ws *windowsService) String() string {
  116. if len(ws.DisplayName) > 0 {
  117. return ws.DisplayName
  118. }
  119. return ws.Name
  120. }
  121. func (ws *windowsService) Platform() string {
  122. return version
  123. }
  124. func (ws *windowsService) setError(err error) {
  125. ws.errSync.Lock()
  126. defer ws.errSync.Unlock()
  127. ws.stopStartErr = err
  128. }
  129. func (ws *windowsService) getError() error {
  130. ws.errSync.Lock()
  131. defer ws.errSync.Unlock()
  132. return ws.stopStartErr
  133. }
  134. func (ws *windowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
  135. const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
  136. changes <- svc.Status{State: svc.StartPending}
  137. if err := ws.i.Start(ws); err != nil {
  138. ws.setError(err)
  139. return true, 1
  140. }
  141. changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
  142. loop:
  143. for {
  144. c := <-r
  145. switch c.Cmd {
  146. case svc.Interrogate:
  147. changes <- c.CurrentStatus
  148. case svc.Stop:
  149. changes <- svc.Status{State: svc.StopPending}
  150. if err := ws.i.Stop(ws); err != nil {
  151. ws.setError(err)
  152. return true, 2
  153. }
  154. break loop
  155. case svc.Shutdown:
  156. changes <- svc.Status{State: svc.StopPending}
  157. var err error
  158. if wsShutdown, ok := ws.i.(Shutdowner); ok {
  159. err = wsShutdown.Shutdown(ws)
  160. } else {
  161. err = ws.i.Stop(ws)
  162. }
  163. if err != nil {
  164. ws.setError(err)
  165. return true, 2
  166. }
  167. break loop
  168. default:
  169. continue loop
  170. }
  171. }
  172. return false, 0
  173. }
  174. func (ws *windowsService) Install() error {
  175. exepath, err := ws.execPath()
  176. if err != nil {
  177. return err
  178. }
  179. m, err := mgr.Connect()
  180. if err != nil {
  181. return err
  182. }
  183. defer m.Disconnect()
  184. s, err := m.OpenService(ws.Name)
  185. if err == nil {
  186. s.Close()
  187. return fmt.Errorf("service %s already exists", ws.Name)
  188. }
  189. s, err = m.CreateService(ws.Name, exepath, mgr.Config{
  190. DisplayName: ws.DisplayName,
  191. Description: ws.Description,
  192. StartType: mgr.StartAutomatic,
  193. ServiceStartName: ws.UserName,
  194. Password: ws.Option.string("Password", ""),
  195. Dependencies: ws.Dependencies,
  196. DelayedAutoStart: ws.Option.bool("DelayedAutoStart", false),
  197. }, ws.Arguments...)
  198. if err != nil {
  199. return err
  200. }
  201. defer s.Close()
  202. err = eventlog.InstallAsEventCreate(ws.Name, eventlog.Error|eventlog.Warning|eventlog.Info)
  203. if err != nil {
  204. if !strings.Contains(err.Error(), "exists") {
  205. s.Delete()
  206. return fmt.Errorf("SetupEventLogSource() failed: %s", err)
  207. }
  208. }
  209. return nil
  210. }
  211. func (ws *windowsService) Uninstall() error {
  212. m, err := mgr.Connect()
  213. if err != nil {
  214. return err
  215. }
  216. defer m.Disconnect()
  217. s, err := m.OpenService(ws.Name)
  218. if err != nil {
  219. return fmt.Errorf("service %s is not installed", ws.Name)
  220. }
  221. defer s.Close()
  222. err = s.Delete()
  223. if err != nil {
  224. return err
  225. }
  226. err = eventlog.Remove(ws.Name)
  227. if err != nil {
  228. return fmt.Errorf("RemoveEventLogSource() failed: %s", err)
  229. }
  230. return nil
  231. }
  232. func (ws *windowsService) Run() error {
  233. ws.setError(nil)
  234. if !interactive {
  235. // Return error messages from start and stop routines
  236. // that get executed in the Execute method.
  237. // Guarded with a mutex as it may run a different thread
  238. // (callback from windows).
  239. runErr := svc.Run(ws.Name, ws)
  240. startStopErr := ws.getError()
  241. if startStopErr != nil {
  242. return startStopErr
  243. }
  244. if runErr != nil {
  245. return runErr
  246. }
  247. return nil
  248. }
  249. err := ws.i.Start(ws)
  250. if err != nil {
  251. return err
  252. }
  253. sigChan := make(chan os.Signal)
  254. signal.Notify(sigChan, os.Interrupt)
  255. <-sigChan
  256. return ws.i.Stop(ws)
  257. }
  258. func (ws *windowsService) Status() (Status, error) {
  259. m, err := mgr.Connect()
  260. if err != nil {
  261. return StatusUnknown, err
  262. }
  263. defer m.Disconnect()
  264. s, err := m.OpenService(ws.Name)
  265. if err != nil {
  266. if err.Error() == "The specified service does not exist as an installed service." {
  267. return StatusUnknown, ErrNotInstalled
  268. }
  269. return StatusUnknown, err
  270. }
  271. defer s.Close()
  272. status, err := s.Query()
  273. if err != nil {
  274. return StatusUnknown, err
  275. }
  276. switch status.State {
  277. case svc.StartPending:
  278. fallthrough
  279. case svc.Running:
  280. return StatusRunning, nil
  281. case svc.PausePending:
  282. fallthrough
  283. case svc.Paused:
  284. fallthrough
  285. case svc.ContinuePending:
  286. fallthrough
  287. case svc.StopPending:
  288. fallthrough
  289. case svc.Stopped:
  290. return StatusStopped, nil
  291. default:
  292. return StatusUnknown, fmt.Errorf("unknown status %v", status)
  293. }
  294. }
  295. func (ws *windowsService) Start() error {
  296. m, err := mgr.Connect()
  297. if err != nil {
  298. return err
  299. }
  300. defer m.Disconnect()
  301. s, err := m.OpenService(ws.Name)
  302. if err != nil {
  303. return err
  304. }
  305. defer s.Close()
  306. return s.Start()
  307. }
  308. func (ws *windowsService) Stop() error {
  309. m, err := mgr.Connect()
  310. if err != nil {
  311. return err
  312. }
  313. defer m.Disconnect()
  314. s, err := m.OpenService(ws.Name)
  315. if err != nil {
  316. return err
  317. }
  318. defer s.Close()
  319. return ws.stopWait(s)
  320. }
  321. func (ws *windowsService) Restart() error {
  322. m, err := mgr.Connect()
  323. if err != nil {
  324. return err
  325. }
  326. defer m.Disconnect()
  327. s, err := m.OpenService(ws.Name)
  328. if err != nil {
  329. return err
  330. }
  331. defer s.Close()
  332. err = ws.stopWait(s)
  333. if err != nil {
  334. return err
  335. }
  336. return s.Start()
  337. }
  338. func (ws *windowsService) stopWait(s *mgr.Service) error {
  339. // First stop the service. Then wait for the service to
  340. // actually stop before starting it.
  341. status, err := s.Control(svc.Stop)
  342. if err != nil {
  343. return err
  344. }
  345. timeDuration := time.Millisecond * 50
  346. timeout := time.After(getStopTimeout() + (timeDuration * 2))
  347. tick := time.NewTicker(timeDuration)
  348. defer tick.Stop()
  349. for status.State != svc.Stopped {
  350. select {
  351. case <-tick.C:
  352. status, err = s.Query()
  353. if err != nil {
  354. return err
  355. }
  356. case <-timeout:
  357. break
  358. }
  359. }
  360. return nil
  361. }
  362. // getStopTimeout fetches the time before windows will kill the service.
  363. func getStopTimeout() time.Duration {
  364. // For default and paths see https://support.microsoft.com/en-us/kb/146092
  365. defaultTimeout := time.Millisecond * 20000
  366. key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control`, registry.READ)
  367. if err != nil {
  368. return defaultTimeout
  369. }
  370. sv, _, err := key.GetStringValue("WaitToKillServiceTimeout")
  371. if err != nil {
  372. return defaultTimeout
  373. }
  374. v, err := strconv.Atoi(sv)
  375. if err != nil {
  376. return defaultTimeout
  377. }
  378. return time.Millisecond * time.Duration(v)
  379. }
  380. func (ws *windowsService) Logger(errs chan<- error) (Logger, error) {
  381. if interactive {
  382. return ConsoleLogger, nil
  383. }
  384. return ws.SystemLogger(errs)
  385. }
  386. func (ws *windowsService) SystemLogger(errs chan<- error) (Logger, error) {
  387. el, err := eventlog.Open(ws.Name)
  388. if err != nil {
  389. return nil, err
  390. }
  391. return WindowsLogger{el, errs}, nil
  392. }