MonoApp.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.IsoMounter;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Startup.Common;
  5. using Mono.Unix.Native;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. using System.Text.RegularExpressions;
  10. using Emby.Server.Core;
  11. using MediaBrowser.Model.System;
  12. using MediaBrowser.Server.Startup.Common.Persistence;
  13. using MediaBrowser.Server.Startup.Common.FFMpeg;
  14. using MediaBrowser.Server.Startup.Common.Networking;
  15. using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
  16. namespace MediaBrowser.Server.Mono.Native
  17. {
  18. public class MonoApp : INativeApp
  19. {
  20. protected StartupOptions StartupOptions { get; private set; }
  21. protected ILogger Logger { get; private set; }
  22. public MonoApp(StartupOptions startupOptions, ILogger logger)
  23. {
  24. StartupOptions = startupOptions;
  25. Logger = logger;
  26. }
  27. /// <summary>
  28. /// Shutdowns this instance.
  29. /// </summary>
  30. public void Shutdown()
  31. {
  32. MainClass.Shutdown();
  33. }
  34. /// <summary>
  35. /// Determines whether this instance [can self restart].
  36. /// </summary>
  37. /// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value>
  38. public bool CanSelfRestart
  39. {
  40. get
  41. {
  42. // A restart script must be provided
  43. return StartupOptions.ContainsOption("-restartpath");
  44. }
  45. }
  46. /// <summary>
  47. /// Restarts this instance.
  48. /// </summary>
  49. public void Restart(StartupOptions startupOptions)
  50. {
  51. MainClass.Restart(startupOptions);
  52. }
  53. /// <summary>
  54. /// Gets a value indicating whether this instance can self update.
  55. /// </summary>
  56. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  57. public bool CanSelfUpdate
  58. {
  59. get
  60. {
  61. return false;
  62. }
  63. }
  64. public bool SupportsAutoRunAtStartup
  65. {
  66. get { return false; }
  67. }
  68. public List<Assembly> GetAssembliesWithParts()
  69. {
  70. var list = new List<Assembly>();
  71. if (Environment.OperatingSystem == Startup.Common.OperatingSystem.Linux)
  72. {
  73. list.AddRange(GetLinuxAssemblies());
  74. }
  75. list.Add(GetType().Assembly);
  76. return list;
  77. }
  78. private IEnumerable<Assembly> GetLinuxAssemblies()
  79. {
  80. var list = new List<Assembly>();
  81. list.Add(typeof(LinuxIsoManager).Assembly);
  82. return list;
  83. }
  84. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  85. {
  86. }
  87. private NativeEnvironment _nativeEnvironment;
  88. public NativeEnvironment Environment
  89. {
  90. get { return _nativeEnvironment ?? (_nativeEnvironment = GetEnvironmentInfo()); }
  91. }
  92. public bool SupportsRunningAsService
  93. {
  94. get
  95. {
  96. return false;
  97. }
  98. }
  99. public bool IsRunningAsService
  100. {
  101. get
  102. {
  103. return false;
  104. }
  105. }
  106. public bool SupportsLibraryMonitor
  107. {
  108. get
  109. {
  110. return Environment.OperatingSystem != Startup.Common.OperatingSystem.Osx;
  111. }
  112. }
  113. public void ConfigureAutoRun(bool autorun)
  114. {
  115. }
  116. public INetworkManager CreateNetworkManager(ILogger logger)
  117. {
  118. return new NetworkManager(logger);
  119. }
  120. private NativeEnvironment GetEnvironmentInfo()
  121. {
  122. var info = new NativeEnvironment
  123. {
  124. OperatingSystem = Startup.Common.OperatingSystem.Linux
  125. };
  126. var uname = GetUnixName();
  127. var sysName = uname.sysname ?? string.Empty;
  128. if (string.Equals(sysName, "Darwin", StringComparison.OrdinalIgnoreCase))
  129. {
  130. info.OperatingSystem = Startup.Common.OperatingSystem.Osx;
  131. }
  132. else if (string.Equals(sysName, "Linux", StringComparison.OrdinalIgnoreCase))
  133. {
  134. info.OperatingSystem = Startup.Common.OperatingSystem.Linux;
  135. }
  136. else if (string.Equals(sysName, "BSD", StringComparison.OrdinalIgnoreCase))
  137. {
  138. info.OperatingSystem = Startup.Common.OperatingSystem.Bsd;
  139. }
  140. var archX86 = new Regex("(i|I)[3-6]86");
  141. if (archX86.IsMatch(uname.machine))
  142. {
  143. info.SystemArchitecture = Architecture.X86;
  144. }
  145. else if (string.Equals(uname.machine, "x86_64", StringComparison.OrdinalIgnoreCase))
  146. {
  147. info.SystemArchitecture = Architecture.X64;
  148. }
  149. else if (uname.machine.StartsWith("arm", StringComparison.OrdinalIgnoreCase))
  150. {
  151. info.SystemArchitecture = Architecture.Arm;
  152. }
  153. else if (System.Environment.Is64BitOperatingSystem)
  154. {
  155. info.SystemArchitecture = Architecture.X64;
  156. }
  157. else
  158. {
  159. info.SystemArchitecture = Architecture.X86;
  160. }
  161. info.OperatingSystemVersionString = string.IsNullOrWhiteSpace(sysName) ?
  162. System.Environment.OSVersion.VersionString :
  163. sysName;
  164. return info;
  165. }
  166. private Uname _unixName;
  167. private Uname GetUnixName()
  168. {
  169. if (_unixName == null)
  170. {
  171. var uname = new Uname();
  172. try
  173. {
  174. Utsname utsname;
  175. var callResult = Syscall.uname(out utsname);
  176. if (callResult == 0)
  177. {
  178. uname.sysname = utsname.sysname ?? string.Empty;
  179. uname.machine = utsname.machine ?? string.Empty;
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. Logger.ErrorException("Error getting unix name", ex);
  185. }
  186. _unixName = uname;
  187. }
  188. return _unixName;
  189. }
  190. public class Uname
  191. {
  192. public string sysname = string.Empty;
  193. public string machine = string.Empty;
  194. }
  195. public FFMpegInstallInfo GetFfmpegInstallInfo()
  196. {
  197. return GetInfo(Environment);
  198. }
  199. public void LaunchUrl(string url)
  200. {
  201. throw new NotImplementedException();
  202. }
  203. public IDbConnector GetDbConnector()
  204. {
  205. return new DbConnector(Logger);
  206. }
  207. public static FFMpegInstallInfo GetInfo(NativeEnvironment environment)
  208. {
  209. var info = new FFMpegInstallInfo();
  210. // Windows builds: http://ffmpeg.zeranoe.com/builds/
  211. // Linux builds: http://johnvansickle.com/ffmpeg/
  212. // OS X builds: http://ffmpegmac.net/
  213. // OS X x64: http://www.evermeet.cx/ffmpeg/
  214. switch (environment.OperatingSystem)
  215. {
  216. case OperatingSystem.Osx:
  217. case OperatingSystem.Bsd:
  218. break;
  219. case OperatingSystem.Linux:
  220. info.ArchiveType = "7z";
  221. info.Version = "20160215";
  222. break;
  223. }
  224. // No version available - user requirement
  225. info.DownloadUrls = new string[] { };
  226. return info;
  227. }
  228. public void EnableLoopback(string appName)
  229. {
  230. }
  231. }
  232. }