WrapperService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.ServiceProcess;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using log4net;
  11. using winsw.Extensions;
  12. using winsw.Logging;
  13. using winsw.Native;
  14. using winsw.Util;
  15. namespace winsw
  16. {
  17. public class WrapperService : ServiceBase, EventLogger
  18. {
  19. private ServiceApis.SERVICE_STATUS _wrapperServiceStatus;
  20. private readonly Process _process = new Process();
  21. private readonly ServiceDescriptor _descriptor;
  22. private Dictionary<string, string>? _envs;
  23. internal WinSWExtensionManager ExtensionManager { get; private set; }
  24. private static readonly ILog Log = LogManager.GetLogger(
  25. #if NETCOREAPP
  26. Assembly.GetExecutingAssembly(),
  27. #endif
  28. "WinSW");
  29. internal static readonly WrapperServiceEventLogProvider eventLogProvider = new WrapperServiceEventLogProvider();
  30. /// <summary>
  31. /// Indicates to the watch dog thread that we are going to terminate the process,
  32. /// so don't try to kill us when the child exits.
  33. /// </summary>
  34. private bool _orderlyShutdown;
  35. private bool _systemShuttingdown;
  36. /// <summary>
  37. /// Version of Windows service wrapper
  38. /// </summary>
  39. /// <remarks>
  40. /// The version will be taken from <see cref="AssemblyInfo"/>
  41. /// </remarks>
  42. public static Version Version => Assembly.GetExecutingAssembly().GetName().Version!;
  43. /// <summary>
  44. /// Indicates that the system is shutting down.
  45. /// </summary>
  46. public bool IsShuttingDown => _systemShuttingdown;
  47. public WrapperService(ServiceDescriptor descriptor)
  48. {
  49. _descriptor = descriptor;
  50. ServiceName = _descriptor.Id;
  51. ExtensionManager = new WinSWExtensionManager(_descriptor);
  52. CanShutdown = true;
  53. CanStop = true;
  54. CanPauseAndContinue = false;
  55. AutoLog = true;
  56. _systemShuttingdown = false;
  57. // Register the event log provider
  58. eventLogProvider.service = this;
  59. }
  60. public WrapperService() : this(new ServiceDescriptor())
  61. {
  62. }
  63. /// <summary>
  64. /// Process the file copy instructions, so that we can replace files that are always in use while
  65. /// the service runs.
  66. /// </summary>
  67. private void HandleFileCopies()
  68. {
  69. var file = _descriptor.BasePath + ".copies";
  70. if (!File.Exists(file))
  71. return; // nothing to handle
  72. try
  73. {
  74. using var tr = new StreamReader(file, Encoding.UTF8);
  75. string? line;
  76. while ((line = tr.ReadLine()) != null)
  77. {
  78. LogEvent("Handling copy: " + line);
  79. string[] tokens = line.Split('>');
  80. if (tokens.Length > 2)
  81. {
  82. LogEvent("Too many delimiters in " + line);
  83. continue;
  84. }
  85. MoveFile(tokens[0], tokens[1]);
  86. }
  87. }
  88. finally
  89. {
  90. File.Delete(file);
  91. }
  92. }
  93. /// <summary>
  94. /// File replacement.
  95. /// </summary>
  96. private void MoveFile(string sourceFileName, string destFileName)
  97. {
  98. try
  99. {
  100. FileHelper.MoveOrReplaceFile(sourceFileName, destFileName);
  101. }
  102. catch (IOException e)
  103. {
  104. LogEvent("Failed to move :" + sourceFileName + " to " + destFileName + " because " + e.Message);
  105. }
  106. }
  107. /// <summary>
  108. /// Handle the creation of the logfiles based on the optional logmode setting.
  109. /// </summary>
  110. /// <returns>Log Handler, which should be used for the spawned process</returns>
  111. private LogHandler CreateExecutableLogHandler()
  112. {
  113. string logDirectory = _descriptor.LogDirectory;
  114. if (!Directory.Exists(logDirectory))
  115. {
  116. Directory.CreateDirectory(logDirectory);
  117. }
  118. LogHandler logAppender = _descriptor.LogHandler;
  119. logAppender.EventLogger = this;
  120. return logAppender;
  121. }
  122. public void LogEvent(string message)
  123. {
  124. if (_systemShuttingdown)
  125. {
  126. /* NOP - cannot call EventLog because of shutdown. */
  127. }
  128. else
  129. {
  130. try
  131. {
  132. EventLog.WriteEntry(message);
  133. }
  134. catch (Exception e)
  135. {
  136. Log.Error("Failed to log event in Windows Event Log: " + message + "; Reason: ", e);
  137. }
  138. }
  139. }
  140. public void LogEvent(string message, EventLogEntryType type)
  141. {
  142. if (_systemShuttingdown)
  143. {
  144. /* NOP - cannot call EventLog because of shutdown. */
  145. }
  146. else
  147. {
  148. try
  149. {
  150. EventLog.WriteEntry(message, type);
  151. }
  152. catch (Exception e)
  153. {
  154. Log.Error("Failed to log event in Windows Event Log. Reason: ", e);
  155. }
  156. }
  157. }
  158. protected override void OnStart(string[] args)
  159. {
  160. _envs = _descriptor.EnvironmentVariables;
  161. // TODO: Disabled according to security concerns in https://github.com/kohsuke/winsw/issues/54
  162. // Could be restored, but unlikely it's required in event logs at all
  163. /**
  164. foreach (string key in _envs.Keys)
  165. {
  166. LogEvent("envar " + key + '=' + _envs[key]);
  167. }*/
  168. HandleFileCopies();
  169. // handle downloads
  170. List<Download> downloads = _descriptor.Downloads;
  171. Task[] tasks = new Task[downloads.Count];
  172. for (int i = 0; i < downloads.Count; i++)
  173. {
  174. Download download = downloads[i];
  175. string downloadMessage = $"Downloading: {download.From} to {download.To}. failOnError={download.FailOnError.ToString()}";
  176. LogEvent(downloadMessage);
  177. Log.Info(downloadMessage);
  178. tasks[i] = download.PerformAsync();
  179. }
  180. try
  181. {
  182. Task.WaitAll(tasks);
  183. }
  184. catch (AggregateException e)
  185. {
  186. List<Exception> exceptions = new List<Exception>(e.InnerExceptions.Count);
  187. for (int i = 0; i < tasks.Length; i++)
  188. {
  189. if (tasks[i].IsFaulted)
  190. {
  191. Download download = downloads[i];
  192. string errorMessage = $"Failed to download {download.From} to {download.To}";
  193. AggregateException exception = tasks[i].Exception!;
  194. LogEvent($"{errorMessage}. {exception.Message}");
  195. Log.Error(errorMessage, exception);
  196. // TODO: move this code into the download logic
  197. if (download.FailOnError)
  198. {
  199. exceptions.Add(new IOException(errorMessage, exception));
  200. }
  201. }
  202. }
  203. throw new AggregateException(exceptions);
  204. }
  205. string? startArguments = _descriptor.StartArguments;
  206. if (startArguments is null)
  207. {
  208. startArguments = _descriptor.Arguments;
  209. }
  210. else
  211. {
  212. startArguments += " " + _descriptor.Arguments;
  213. }
  214. // Converting newlines, line returns, tabs into a single
  215. // space. This allows users to provide multi-line arguments
  216. // in the xml for readability.
  217. startArguments = Regex.Replace(startArguments, @"\s*[\n\r]+\s*", " ");
  218. LogEvent("Starting " + _descriptor.Executable + ' ' + startArguments);
  219. Log.Info("Starting " + _descriptor.Executable + ' ' + startArguments);
  220. // Load and start extensions
  221. ExtensionManager.LoadExtensions();
  222. ExtensionManager.FireOnWrapperStarted();
  223. LogHandler executableLogHandler = CreateExecutableLogHandler();
  224. StartProcess(_process, startArguments, _descriptor.Executable, executableLogHandler, true);
  225. ExtensionManager.FireOnProcessStarted(_process);
  226. _process.StandardInput.Close(); // nothing for you to read!
  227. }
  228. protected override void OnShutdown()
  229. {
  230. // WriteEvent("OnShutdown");
  231. try
  232. {
  233. _systemShuttingdown = true;
  234. StopIt();
  235. }
  236. catch (Exception ex)
  237. {
  238. Log.Error("Shutdown exception", ex);
  239. }
  240. }
  241. protected override void OnStop()
  242. {
  243. // WriteEvent("OnStop");
  244. try
  245. {
  246. StopIt();
  247. }
  248. catch (Exception ex)
  249. {
  250. Log.Error("Cannot stop exception", ex);
  251. }
  252. }
  253. internal void RaiseOnStart(string[] args) => this.OnStart(args);
  254. internal void RaiseOnStop() => this.OnStop();
  255. /// <summary>
  256. /// Called when we are told by Windows SCM to exit.
  257. /// </summary>
  258. private void StopIt()
  259. {
  260. string? stopArguments = _descriptor.StopArguments;
  261. LogEvent("Stopping " + _descriptor.Id);
  262. Log.Info("Stopping " + _descriptor.Id);
  263. _orderlyShutdown = true;
  264. if (stopArguments is null)
  265. {
  266. try
  267. {
  268. Log.Debug("ProcessKill " + _process.Id);
  269. ProcessHelper.StopProcessAndChildren(_process, _descriptor.StopTimeout, _descriptor.StopParentProcessFirst);
  270. ExtensionManager.FireOnProcessTerminated(_process);
  271. }
  272. catch (InvalidOperationException)
  273. {
  274. // already terminated
  275. }
  276. }
  277. else
  278. {
  279. SignalShutdownPending();
  280. stopArguments += " " + _descriptor.Arguments;
  281. Process stopProcess = new Process();
  282. string? executable = _descriptor.StopExecutable;
  283. executable ??= _descriptor.Executable;
  284. // TODO: Redirect logging to Log4Net once https://github.com/kohsuke/winsw/pull/213 is integrated
  285. StartProcess(stopProcess, stopArguments, executable, null, false);
  286. Log.Debug("WaitForProcessToExit " + _process.Id + "+" + stopProcess.Id);
  287. WaitForProcessToExit(_process);
  288. WaitForProcessToExit(stopProcess);
  289. }
  290. // Stop extensions
  291. ExtensionManager.FireBeforeWrapperStopped();
  292. if (_systemShuttingdown && _descriptor.BeepOnShutdown)
  293. {
  294. Console.Beep();
  295. }
  296. Log.Info("Finished " + _descriptor.Id);
  297. }
  298. private void WaitForProcessToExit(Process processoWait)
  299. {
  300. SignalShutdownPending();
  301. int effectiveProcessWaitSleepTime;
  302. if (_descriptor.SleepTime.TotalMilliseconds > int.MaxValue)
  303. {
  304. Log.Warn("The requested sleep time " + _descriptor.SleepTime.TotalMilliseconds + "is greater that the max value " +
  305. int.MaxValue + ". The value will be truncated");
  306. effectiveProcessWaitSleepTime = int.MaxValue;
  307. }
  308. else
  309. {
  310. effectiveProcessWaitSleepTime = (int)_descriptor.SleepTime.TotalMilliseconds;
  311. }
  312. try
  313. {
  314. // WriteEvent("WaitForProcessToExit [start]");
  315. while (!processoWait.WaitForExit(effectiveProcessWaitSleepTime))
  316. {
  317. SignalShutdownPending();
  318. // WriteEvent("WaitForProcessToExit [repeat]");
  319. }
  320. }
  321. catch (InvalidOperationException)
  322. {
  323. // already terminated
  324. }
  325. // WriteEvent("WaitForProcessToExit [finished]");
  326. }
  327. private void SignalShutdownPending()
  328. {
  329. int effectiveWaitHint;
  330. if (_descriptor.WaitHint.TotalMilliseconds > int.MaxValue)
  331. {
  332. Log.Warn("The requested WaitHint value (" + _descriptor.WaitHint.TotalMilliseconds + " ms) is greater that the max value " +
  333. int.MaxValue + ". The value will be truncated");
  334. effectiveWaitHint = int.MaxValue;
  335. }
  336. else
  337. {
  338. effectiveWaitHint = (int)_descriptor.WaitHint.TotalMilliseconds;
  339. }
  340. RequestAdditionalTime(effectiveWaitHint);
  341. }
  342. private void SignalShutdownComplete()
  343. {
  344. IntPtr handle = ServiceHandle;
  345. _wrapperServiceStatus.CheckPoint++;
  346. // WriteEvent("SignalShutdownComplete " + wrapperServiceStatus.checkPoint + ":" + wrapperServiceStatus.waitHint);
  347. _wrapperServiceStatus.CurrentState = ServiceControllerStatus.Stopped;
  348. ServiceApis.SetServiceStatus(handle, _wrapperServiceStatus);
  349. }
  350. private void StartProcess(Process processToStart, string arguments, string executable, LogHandler? logHandler, bool redirectStdin)
  351. {
  352. // Define handler of the completed process
  353. void OnProcessCompleted(Process proc)
  354. {
  355. string msg = processToStart.Id + " - " + processToStart.StartInfo.FileName + " " + processToStart.StartInfo.Arguments;
  356. try
  357. {
  358. if (_orderlyShutdown)
  359. {
  360. LogEvent("Child process [" + msg + "] terminated with " + proc.ExitCode, EventLogEntryType.Information);
  361. }
  362. else
  363. {
  364. LogEvent("Child process [" + msg + "] finished with " + proc.ExitCode, EventLogEntryType.Warning);
  365. // if we finished orderly, report that to SCM.
  366. // by not reporting unclean shutdown, we let Windows SCM to decide if it wants to
  367. // restart the service automatically
  368. if (proc.ExitCode == 0)
  369. SignalShutdownComplete();
  370. Environment.Exit(proc.ExitCode);
  371. }
  372. }
  373. catch (InvalidOperationException ioe)
  374. {
  375. LogEvent("WaitForExit " + ioe.Message);
  376. }
  377. finally
  378. {
  379. proc.Dispose();
  380. }
  381. }
  382. // Invoke process and exit
  383. ProcessHelper.StartProcessAndCallbackForExit(
  384. processToStart: processToStart,
  385. executable: executable,
  386. arguments: arguments,
  387. envVars: _envs,
  388. workingDirectory: _descriptor.WorkingDirectory,
  389. priority: _descriptor.Priority,
  390. callback: OnProcessCompleted,
  391. logHandler: logHandler,
  392. redirectStdin: redirectStdin,
  393. hideWindow: _descriptor.HideWindow);
  394. }
  395. }
  396. }