public override void Lock(bool nonBlock = false) { if (LockedCount == 0) { IO.MakeDirIfNotExists(Env.UnixMutantDir); UnixPermissions perm = UnixPermissions.S_IRUSR | UnixPermissions.S_IWUSR | UnixPermissions.S_IRGRP | UnixPermissions.S_IWGRP | UnixPermissions.S_IROTH | UnixPermissions.S_IWOTH; IntPtr fd = UnixApi.Open(Filename, UnixApi.OpenFlags.O_CREAT | UnixApi.OpenFlags.O_CLOEXEC, (int)perm); if (fd.ToInt64() < 0) { throw new IOException("Open failed."); } try { if (UnixApi.FLock(fd, UnixApi.LockOperations.LOCK_EX | (nonBlock ? UnixApi.LockOperations.LOCK_NB : 0)) == -1) { throw new IOException("FLock failed."); } } catch { UnixApi.Close(fd); throw; } this.FileHandle = fd; LockedCount++; } }
public void StopService(int stopTimeout) { HiveData.SyncWithStorage(HiveSyncFlags.LoadFromFile, true); long pid = HiveData.ManagedData.Pid; if (pid != 0) { if (Env.IsWindows == false) { if (UnixApi.Kill((int)pid, UnixApi.Signals.SIGTERM) == 0) { Con.WriteLine($"Shutting down the daemon \"{Name}\" (pid = {pid}) ..."); if (UnixApi.WaitProcessExit((int)pid, stopTimeout) == false) { Con.WriteLine($"Shutting down the daemon \"{Name}\" (pid = {pid}) timed out."); throw new ApplicationException($"Shutting down the daemon \"{Name}\" (pid = {pid}) timed out."); } } else { Con.WriteLine($"The daemon \"{Name}\" is not running."); } } else { if (ManualResetEvent.TryOpenExisting(HiveData.ManagedData.EventName !, out EventWaitHandle? eventHandle)) { try { Con.WriteLine($"Stopping the daemon \"{Name}\" (pid = {pid}) ..."); eventHandle.Set(); if (Win32ApiUtil.WaitProcessExit((int)pid, stopTimeout) == false) { Con.WriteLine($"Stopping the daemon \"{Name}\" (pid = {pid}) timed out."); throw new ApplicationException($"Stopping the daemon \"{Name}\" (pid = {pid}) timed out."); } } finally { eventHandle._DisposeSafe(); } } else { Con.WriteLine($"The daemon \"{Name}\" is not running."); } }
public override void Unlock() { if (LockedCount <= 0) { throw new ApplicationException("locked_count <= 0"); } if (LockedCount == 1) { try { UnixApi.FLock(this.FileHandle, UnixApi.LockOperations.LOCK_UN); } catch { } UnixApi.Close(this.FileHandle); this.FileHandle = IntPtr.Zero; } LockedCount--; }
public static void DeleteUnusedMutantFiles() { if (Env.IsUnix == false) { return; } try { string[] fileFullPathList = Directory.GetFiles(Env.UnixMutantDir); foreach (string fileFullPath in fileFullPathList) { try { if (fileFullPath.EndsWith(Extension, StringComparison.OrdinalIgnoreCase)) { UnixPermissions perm = UnixPermissions.S_IRUSR | UnixPermissions.S_IWUSR | UnixPermissions.S_IRGRP | UnixPermissions.S_IWGRP | UnixPermissions.S_IROTH | UnixPermissions.S_IWOTH; bool okToDelete = false; IntPtr fd = UnixApi.Open(fileFullPath, UnixApi.OpenFlags.O_CREAT | UnixApi.OpenFlags.O_CLOEXEC, (int)perm); if (fd.ToInt64() >= 0) { try { if (UnixApi.FLock(fd, UnixApi.LockOperations.LOCK_EX | UnixApi.LockOperations.LOCK_NB) != -1) { okToDelete = true; } } finally { try { UnixApi.FLock(fd, UnixApi.LockOperations.LOCK_UN); } catch { } try { UnixApi.Close(fd); } catch { } } } if (okToDelete) { try { File.Delete(fileFullPath); } catch { } } } } catch { } } } catch { } }