public static ProcessInfoLinux Create(LockInfo li) { if (li.ProcessId == -1) { // Can happen for OFD (open file descriptor) locks which are not // bound to a specific process. return(new ProcessInfoLinux(li.ProcessId, DateTime.MinValue) { LockAccess = li.LockAccess, LockMode = li.LockMode, LockType = li.LockType, ApplicationName = $"({li.LockType};{li.LockAccess};{li.LockMode})" }); } ProcessInfoLinux result = null; try { using (var process = Process.GetProcessById(li.ProcessId)) { result = new ProcessInfoLinux(li.ProcessId, process.StartTime) { SessionId = process.SessionId, ApplicationName = process.ProcessName }; // MainModule may be null, if no permissions, etc. // Note: alternative of "readlink -f /proc/<pid>/exe" will // also yield results in this case. if (process.MainModule != null) { result.ExecutableFullPath = process.MainModule.FileName; result.ExecutableName = Path.GetFileName(result.ExecutableFullPath); } else { result.ExecutableFullPath = process.ProcessName; result.ExecutableName = process.ProcessName; } } } catch (ArgumentException) { // Process already gone/does not exist. } if (result != null) { // TryGetUid() fails if process is gone (because directory is gone); // GetUserName() should not fail because it looks up information in // passwd, which is not bound in lifetime to the process of course. if (NativeMethods.TryGetUid($"/proc/{li.ProcessId}", out uint uid)) { result.Owner = NativeMethods.GetUserName(uid); } } return(result); }
public static IEnumerable <ProcessInfo> GetLockingProcessInfos(params string[] paths) { if (paths == null) { throw new ArgumentNullException(nameof(paths)); } Dictionary <long, string> inodesToPaths = null; using (var reader = new StreamReader("/proc/locks")) { string line; while ((line = reader.ReadLine()) != null) { if (inodesToPaths == null) { inodesToPaths = GetInodeToPaths(paths); } var lockInfo = LockInfo.ParseLine(line); if (inodesToPaths.ContainsKey(lockInfo.InodeInfo.INodeNumber)) { var processInfo = ProcessInfoLinux.Create(lockInfo); if (processInfo != null) { yield return(processInfo); } } } } }