/// <summary> /// Waits for any Zero Install instances running in <see cref="TargetDir"/> to terminate and then prevents new ones from starting. /// </summary> /// <remarks>The <see cref="TargetDir"/> is encoded into an <see cref="AppMutex"/> name using <see cref="object.GetHashCode"/>.</remarks> private void MutexAcquire() { if (FileUtils.PathEquals(TargetDir, Locations.InstallBase)) { Log.Info("Cannot use Mutex because source and target directory are the same: " + TargetDir); return; } if (ZeroInstallEnvironment.MutexName(TargetDir) == ZeroInstallEnvironment.MutexName(Locations.InstallBase)) { Log.Warn($"Hash collision between {TargetDir} and {Locations.InstallBase}! Not using Mutex."); return; } Handler.RunTask(new SimpleTask(Resources.MutexWait, () => { // Wait for existing instances to terminate while (AppMutex.Probe(ZeroInstallEnvironment.MutexName(TargetDir)) || AppMutex.Probe(ZeroInstallEnvironment.LegacyMutexName(TargetDir))) { Thread.Sleep(1000); Handler.CancellationToken.ThrowIfCancellationRequested(); } // Prevent new instances from starting during the update _updateMutex = AppMutex.Create(ZeroInstallEnvironment.UpdateMutexName(TargetDir)); _legacyUpdateMutex = AppMutex.Create(ZeroInstallEnvironment.LegacyUpdateMutexName(TargetDir)); // Detect any new instances that started in the short time between detecting existing ones and blocking new ones while (AppMutex.Probe(ZeroInstallEnvironment.MutexName(TargetDir)) || AppMutex.Probe(ZeroInstallEnvironment.LegacyMutexName(TargetDir))) { Thread.Sleep(1000); Handler.CancellationToken.ThrowIfCancellationRequested(); } })); }
public ZeroInstallLauncher(string commandLine) : base(ProcessUtils.FromCommandLine(commandLine)) { string?installBase = Path.GetDirectoryName(FileName); _mutexName = ZeroInstallEnvironment.MutexName(installBase); _updateMutexName = ZeroInstallEnvironment.UpdateMutexName(installBase); _legacyMutexName = ZeroInstallEnvironment.LegacyMutexName(installBase); _legacyUpdateMutexName = ZeroInstallEnvironment.LegacyUpdateMutexName(installBase); }
/// <summary> /// Common initialization code to be called by every Zero Install executable right after startup. /// </summary> public static void Init() { AppMutex.Create(ZeroInstallEnvironment.MutexName()); AppMutex.Create(ZeroInstallEnvironment.LegacyMutexName()); if (AppMutex.Probe(ZeroInstallEnvironment.UpdateMutexName()) || AppMutex.Probe(ZeroInstallEnvironment.LegacyUpdateMutexName())) { Environment.Exit(999); } if (UILanguage != null) { Languages.SetUI(UILanguage); } ProcessUtils.SanitizeEnvironmentVariables(); NetUtils.ApplyProxy(); ServicePointManager.DefaultConnectionLimit = 16; }