ServiceDescriptor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using System.ServiceProcess;
  8. using System.Text;
  9. using System.IO;
  10. using System.Net;
  11. using WMI;
  12. using System.Xml;
  13. using System.Threading;
  14. using Microsoft.Win32;
  15. namespace winsw
  16. {
  17. /// <summary>
  18. /// In-memory representation of the configuration file.
  19. /// </summary>
  20. public class ServiceDescriptor
  21. {
  22. private readonly XmlDocument dom = new XmlDocument();
  23. /// <summary>
  24. /// Where did we find the configuration file?
  25. ///
  26. /// This string is "c:\abc\def\ghi" when the configuration XML is "c:\abc\def\ghi.xml"
  27. /// </summary>
  28. public readonly string BasePath;
  29. /// <summary>
  30. /// The file name portion of the configuration file.
  31. ///
  32. /// In the above example, this would be "ghi".
  33. /// </summary>
  34. public readonly string BaseName;
  35. public static string ExecutablePath
  36. {
  37. get
  38. {
  39. // this returns the executable name as given by the calling process, so
  40. // it needs to be absolutized.
  41. string p = Environment.GetCommandLineArgs()[0];
  42. return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p);
  43. }
  44. }
  45. public ServiceDescriptor()
  46. {
  47. // find co-located configuration xml. We search up to the ancestor directories to simplify debugging,
  48. // as well as trimming off ".vshost" suffix (which is used during debugging)
  49. string p = ExecutablePath;
  50. string baseName = Path.GetFileNameWithoutExtension(p);
  51. if (baseName.EndsWith(".vshost")) baseName = baseName.Substring(0, baseName.Length - 7);
  52. while (true)
  53. {
  54. p = Path.GetDirectoryName(p);
  55. if (File.Exists(Path.Combine(p, baseName + ".xml")))
  56. break;
  57. }
  58. // register the base directory as environment variable so that future expansions can refer to this.
  59. Environment.SetEnvironmentVariable("BASE", p);
  60. BaseName = baseName;
  61. BasePath = Path.Combine(p, BaseName);
  62. dom.Load(BasePath + ".xml");
  63. }
  64. private string SingleElement(string tagName)
  65. {
  66. var n = dom.SelectSingleNode("//" + tagName);
  67. if (n == null) throw new InvalidDataException("<" + tagName + "> is missing in configuration XML");
  68. return Environment.ExpandEnvironmentVariables(n.InnerText);
  69. }
  70. /// <summary>
  71. /// Path to the executable.
  72. /// </summary>
  73. public string Executable
  74. {
  75. get
  76. {
  77. return SingleElement("executable");
  78. }
  79. }
  80. /// <summary>
  81. /// Optionally specify a different Path to an executable to shutdown the service.
  82. /// </summary>
  83. public string StopExecutable
  84. {
  85. get
  86. {
  87. return SingleElement("stopexecutable");
  88. }
  89. }
  90. /// <summary>
  91. /// Arguments or multiple optional argument elements which overrule the arguments element.
  92. /// </summary>
  93. public string Arguments
  94. {
  95. get
  96. {
  97. string arguments = AppendTags("argument");
  98. if (arguments == null)
  99. {
  100. var tagName = "arguments";
  101. var argumentsNode = dom.SelectSingleNode("//" + tagName);
  102. if (argumentsNode == null)
  103. {
  104. if (AppendTags("startargument") == null)
  105. {
  106. throw new InvalidDataException("<" + tagName + "> is missing in configuration XML");
  107. }
  108. else
  109. {
  110. return "";
  111. }
  112. }
  113. return Environment.ExpandEnvironmentVariables(argumentsNode.InnerText);
  114. }
  115. else
  116. {
  117. return arguments;
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// Multiple optional startargument elements.
  123. /// </summary>
  124. public string Startarguments
  125. {
  126. get
  127. {
  128. return AppendTags("startargument");
  129. }
  130. }
  131. /// <summary>
  132. /// Multiple optional stopargument elements.
  133. /// </summary>
  134. public string Stoparguments
  135. {
  136. get
  137. {
  138. return AppendTags("stopargument");
  139. }
  140. }
  141. /// <summary>
  142. /// Combines the contents of all the elements of the given name,
  143. /// or return null if no element exists.
  144. /// </summary>
  145. private string AppendTags(string tagName)
  146. {
  147. XmlNode argumentNode = dom.SelectSingleNode("//" + tagName);
  148. if (argumentNode == null)
  149. {
  150. return null;
  151. }
  152. else
  153. {
  154. string arguments = "";
  155. foreach (XmlNode argument in dom.SelectNodes("//" + tagName))
  156. {
  157. arguments += " " + argument.InnerText;
  158. }
  159. return Environment.ExpandEnvironmentVariables(arguments);
  160. }
  161. }
  162. /// <summary>
  163. /// LogDirectory is the service wrapper executable directory or the optionally specified logpath element.
  164. /// </summary>
  165. public string LogDirectory
  166. {
  167. get
  168. {
  169. XmlNode loggingNode = dom.SelectSingleNode("//logpath");
  170. if (loggingNode != null)
  171. {
  172. return Environment.ExpandEnvironmentVariables(loggingNode.InnerText);
  173. }
  174. else
  175. {
  176. return Path.GetDirectoryName(ExecutablePath);
  177. }
  178. }
  179. }
  180. /// <summary>
  181. /// Logmode to 'reset', 'rotate' once or 'append' [default] the out.log and err.log files.
  182. /// </summary>
  183. public string Logmode
  184. {
  185. get
  186. {
  187. XmlNode logmodeNode = dom.SelectSingleNode("//logmode");
  188. if (logmodeNode == null)
  189. {
  190. return "append";
  191. }
  192. else
  193. {
  194. return logmodeNode.InnerText;
  195. }
  196. }
  197. }
  198. /// <summary>
  199. /// Optionally specified depend services that must start before this service starts.
  200. /// </summary>
  201. public string[] ServiceDependencies
  202. {
  203. get
  204. {
  205. System.Collections.ArrayList serviceDependencies = new System.Collections.ArrayList();
  206. foreach (XmlNode depend in dom.SelectNodes("//depend"))
  207. {
  208. serviceDependencies.Add(depend.InnerText);
  209. }
  210. return (string[])serviceDependencies.ToArray(typeof(string));
  211. }
  212. }
  213. public string Id
  214. {
  215. get
  216. {
  217. return SingleElement("id");
  218. }
  219. }
  220. public string Caption
  221. {
  222. get
  223. {
  224. return SingleElement("name");
  225. }
  226. }
  227. public string Description
  228. {
  229. get
  230. {
  231. return SingleElement("description");
  232. }
  233. }
  234. /// <summary>
  235. /// True if the service should when finished on shutdown.
  236. /// </summary>
  237. public bool BeepOnShutdown
  238. {
  239. get
  240. {
  241. return dom.SelectSingleNode("//beeponshutdown") != null;
  242. }
  243. }
  244. /// <summary>
  245. /// The estimated time required for a pending stop operation, in milliseconds (default 15 secs).
  246. /// Before the specified amount of time has elapsed, the service should make its next call to the SetServiceStatus function
  247. /// with either an incremented checkPoint value or a change in currentState. (see http://msdn.microsoft.com/en-us/library/ms685996.aspx)
  248. /// </summary>
  249. public int WaitHint
  250. {
  251. get
  252. {
  253. XmlNode waithintNode = dom.SelectSingleNode("//waithint");
  254. if (waithintNode == null)
  255. {
  256. return 15000;
  257. }
  258. else
  259. {
  260. return int.Parse(waithintNode.InnerText);
  261. }
  262. }
  263. }
  264. /// <summary>
  265. /// The time, in milliseconds (default 1 sec), before the service should make its next call to the SetServiceStatus function
  266. /// with an incremented checkPoint value.
  267. /// Do not wait longer than the wait hint. A good interval is one-tenth of the wait hint but not less than 1 second and not more than 10 seconds.
  268. /// </summary>
  269. public int SleepTime
  270. {
  271. get
  272. {
  273. XmlNode sleeptimeNode = dom.SelectSingleNode("//sleeptime");
  274. if (sleeptimeNode == null)
  275. {
  276. return 1000;
  277. }
  278. else
  279. {
  280. return int.Parse(sleeptimeNode.InnerText);
  281. }
  282. }
  283. }
  284. /// <summary>
  285. /// True if the service can interact with the desktop.
  286. /// </summary>
  287. public bool Interactive
  288. {
  289. get
  290. {
  291. return dom.SelectSingleNode("//interactive") != null;
  292. }
  293. }
  294. /// <summary>
  295. /// Environment variable overrides
  296. /// </summary>
  297. public Dictionary<string, string> EnvironmentVariables
  298. {
  299. get
  300. {
  301. Dictionary<string, string> map = new Dictionary<string, string>();
  302. foreach (XmlNode n in dom.SelectNodes("//env"))
  303. {
  304. string key = n.Attributes["name"].Value;
  305. string value = Environment.ExpandEnvironmentVariables(n.Attributes["value"].Value);
  306. map[key] = value;
  307. Environment.SetEnvironmentVariable(key, value);
  308. }
  309. return map;
  310. }
  311. }
  312. /// <summary>
  313. /// List of downloads to be performed by the wrapper before starting
  314. /// a service.
  315. /// </summary>
  316. public List<Download> Downloads
  317. {
  318. get
  319. {
  320. List<Download> r = new List<Download>();
  321. foreach (XmlNode n in dom.SelectNodes("//download"))
  322. {
  323. r.Add(new Download(n));
  324. }
  325. return r;
  326. }
  327. }
  328. }
  329. }