(app *cli.App, graceShutdownC chan struct{})
| 43 | ) |
| 44 | |
| 45 | func runApp(app *cli.App, graceShutdownC chan struct{}) { |
| 46 | app.Commands = append(app.Commands, &cli.Command{ |
| 47 | Name: "service", |
| 48 | Usage: "Manages the cloudflared Windows service", |
| 49 | Subcommands: []*cli.Command{ |
| 50 | { |
| 51 | Name: "install", |
| 52 | Usage: "Install cloudflared as a Windows service", |
| 53 | Action: cliutil.ConfiguredAction(installWindowsService), |
| 54 | }, |
| 55 | { |
| 56 | Name: "uninstall", |
| 57 | Usage: "Uninstall the cloudflared service", |
| 58 | Action: cliutil.ConfiguredAction(uninstallWindowsService), |
| 59 | }, |
| 60 | }, |
| 61 | }) |
| 62 | |
| 63 | // `IsAnInteractiveSession()` isn't exactly equivalent to "should the |
| 64 | // process run as a normal EXE?" There are legitimate non-service cases, |
| 65 | // like running cloudflared in a GCP startup script, for which |
| 66 | // `IsAnInteractiveSession()` returns false. For more context, see: |
| 67 | // https://github.com/judwhite/go-svc/issues/6 |
| 68 | // It seems that the "correct way" to check "is this a normal EXE?" is: |
| 69 | // 1. attempt to connect to the Service Control Manager |
| 70 | // 2. get ERROR_FAILED_SERVICE_CONTROLLER_CONNECT |
| 71 | // This involves actually trying to start the service. |
| 72 | |
| 73 | log := logger.Create(nil) |
| 74 | |
| 75 | isIntSess, err := svc.IsAnInteractiveSession() |
| 76 | if err != nil { |
| 77 | log.Fatal().Err(err).Msg("failed to determine if we are running in an interactive session") |
| 78 | } |
| 79 | if isIntSess { |
| 80 | app.Run(os.Args) |
| 81 | return |
| 82 | } |
| 83 | |
| 84 | // Run executes service name by calling windowsService which is a Handler |
| 85 | // interface that implements Execute method. |
| 86 | // It will set service status to stop after Execute returns |
| 87 | err = svc.Run(windowsServiceName, &windowsService{app: app, graceShutdownC: graceShutdownC}) |
| 88 | if err != nil { |
| 89 | if errno, ok := err.(syscall.Errno); ok && int(errno) == serviceControllerConnectionFailure { |
| 90 | // Hack: assume this is a false negative from the IsAnInteractiveSession() check above. |
| 91 | // Run the app in "interactive" mode anyway. |
| 92 | app.Run(os.Args) |
| 93 | return |
| 94 | } |
| 95 | log.Fatal().Err(err).Msgf("%s service failed", windowsServiceName) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | type windowsService struct { |
| 100 | app *cli.App |
nothing calls this directly
no test coverage detected