public override bool OnCheckSingleInstance() { try { int currentId = Process.GetCurrentProcess().Id; string path = UtilsCore.GetTempPath() + "/" + Constants.Name + "_" + Constants.AppID + ".pid"; if (File.Exists(path) == false) { } else { int otherId; if (int.TryParse(Platform.Instance.FileContentsReadText(path), out otherId)) { string procFile = "/proc/" + otherId.ToString() + "/cmdline"; if (File.Exists(procFile)) { return(false); } } } Platform.Instance.FileContentsWriteText(path, currentId.ToString(), Encoding.ASCII); return(true); } catch (Exception e) { Engine.Instance.Logs.Log(e); return(true); } }
public override void OnCheckSingleInstanceClear() { try { int currentId = Process.GetCurrentProcess().Id; string path = UtilsCore.GetTempPath() + "/" + Constants.Name + "_" + Constants.AppID + ".pid"; if (File.Exists(path)) { int otherId; if (int.TryParse(Platform.Instance.FileContentsReadText(path), out otherId)) { if (otherId == currentId) { Platform.Instance.FileDelete(path); } } } } catch (Exception e) { Engine.Instance.Logs.Log(e); } }
public override void Start() { ServiceEdition = false; ServiceUninstallAtEnd = false; string tempPathToDelete = ""; try { if (Connect(Constants.ElevatedServiceTcpPort) == false) // Will work if the service is active { Engine.Instance.UiManager.Broadcast("init.step", "message", LanguageManager.GetText("InitStepRaiseSystemPrivileges")); Engine.Instance.Logs.LogVerbose(LanguageManager.GetText("InitStepRaiseSystemPrivileges")); string helperPath = Platform.Instance.GetElevatedHelperPath(); // Special environment: AppImage // pkexec/sudo can't see the file. Workaround: Copy, execute, remove. bool appImageEnvironment = Platform.Instance.NeedExecuteOutsideAppPath(helperPath); if (appImageEnvironment) { tempPathToDelete = UtilsCore.GetTempPath() + "/eddie-cli-elevated-" + RandomGenerator.GetHash(); if (File.Exists(tempPathToDelete)) { File.Delete(tempPathToDelete); } File.Copy(helperPath, tempPathToDelete); helperPath = tempPathToDelete; } int pid = Platform.Instance.StartProcessAsRoot(helperPath, new string[] { "spot" }, Engine.Instance.ConsoleMode); System.Diagnostics.Process process = null; if (pid > 0) { process = System.Diagnostics.Process.GetProcessById(pid); } for (; ;) { System.Threading.Thread.Sleep(1000); if (process == null) { throw new Exception("Unable to start (1)"); } if (process.HasExited) { throw new Exception("Unable to start (2)"); } if (Connect(9345)) { break; } } } else { ServiceEdition = true; } base.Start(); } catch (Exception ex) { Stop(); throw new Exception(LanguageManager.GetText("HelperPrivilegesFailed", ex.Message)); } finally { if (tempPathToDelete != "") { Platform.Instance.FileDelete(tempPathToDelete); } } }