| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | |
| | | 4 | | namespace LockCheck.Linux; |
| | | 5 | | |
| | | 6 | | internal class LockInfo |
| | | 7 | | { |
| | 2 | 8 | | public string? LockType { get; private set; } |
| | 2 | 9 | | public string? LockMode { get; private set; } |
| | 2 | 10 | | public string? LockAccess { get; private set; } |
| | 2 | 11 | | public int ProcessId { get; private set; } |
| | 2 | 12 | | public InodeInfo InodeInfo { get; private set; } |
| | | 13 | | |
| | | 14 | | public static LockInfo ParseLine(string line) |
| | | 15 | | { |
| | | 16 | | // Each line has 8 (or 9 if you count the "->" marker) fields separated by spaces. |
| | | 17 | | // Additional fields after that are possible, but can be ignored. |
| | | 18 | | // The values for the LockType, LockMode, and LockAccess fields are manifold. |
| | | 19 | | // So we don't interpret them, but just store them as strings. They are only |
| | | 20 | | // provided as informational values anyway and not used for program logic. |
| | | 21 | | // |
| | | 22 | | // 1: POSIX ADVISORY READ 5433 08:01:7864448 128 128 |
| | | 23 | | // 2: FLOCK ADVISORY WRITE 2001 08:01:7864554 0 EOF |
| | | 24 | | // 3: FLOCK ADVISORY WRITE 1568 00:2f:32388 0 EOF |
| | | 25 | | // 4: POSIX ADVISORY WRITE 699 00:16:28457 0 EOF |
| | | 26 | | // 5: POSIX ADVISORY WRITE 764 00:16:21448 0 0 |
| | | 27 | | // 5: -> POSIX ADVISORY WRITE 766 00:16:21448 0 0 |
| | | 28 | | // 6: POSIX ADVISORY READ 3548 08:01:7867240 1 1 |
| | | 29 | | // 7: POSIX ADVISORY READ 3548 08:01:7865567 1826 2335 |
| | | 30 | | // 8: OFDLCK ADVISORY WRITE -1 08:01:8713209 128 191 |
| | | 31 | | // |
| | | 32 | | // The major:minor device numbers (e.g. 08:01:...) might not be actual inode numbers |
| | | 33 | | // in case the respective FS is not a physical one (e.g. not ext4, but tempfs or procfs). |
| | | 34 | | // https://utcc.utoronto.ca/~cks/space/blog/linux/ProcLocksNotes has a nice explanation. |
| | | 35 | | // In our case we don't care, because calling code looks up "in reverse" anyway. That is, |
| | | 36 | | // it has an inode for an actual file/directory and needs this information to see if it |
| | | 37 | | // is "locked". |
| | | 38 | | |
| | 2 | 39 | | var span = line.AsSpan(); |
| | 2 | 40 | | int count = span.Count(' ') + 1; |
| | 2 | 41 | | if (count < 6) |
| | | 42 | | { |
| | 0 | 43 | | throw new IOException($"Unexpected number of fields {count} in '/proc/locks' ({line})"); |
| | | 44 | | } |
| | | 45 | | |
| | 2 | 46 | | Span<Range> ranges = count < 128 ? stackalloc Range[count] : new Range[count]; |
| | 2 | 47 | | int num = MemoryExtensions.Split(span, ranges, ' ', StringSplitOptions.RemoveEmptyEntries); |
| | | 48 | | |
| | 2 | 49 | | int offset = 0; |
| | 2 | 50 | | offset++; // Ignore first item (always the "ID" (e.g. "1:") |
| | 2 | 51 | | if (span[ranges[offset]] == "->") |
| | | 52 | | { |
| | | 53 | | // "Blocked" optional marker |
| | 0 | 54 | | offset++; |
| | | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | var result = new LockInfo |
| | 2 | 58 | | { |
| | 2 | 59 | | LockType = span[ranges[offset++]].ToString(), |
| | 2 | 60 | | LockMode = span[ranges[offset++]].ToString(), |
| | 2 | 61 | | LockAccess = span[ranges[offset++]].ToString() |
| | 2 | 62 | | }; |
| | | 63 | | |
| | 2 | 64 | | if (!int.TryParse(span[ranges[offset++]], out int processId)) |
| | | 65 | | { |
| | 0 | 66 | | throw new IOException($"Invalid process ID '{span[ranges[offset]]}' in '/proc/locks' ({line})"); |
| | | 67 | | } |
| | | 68 | | |
| | 2 | 69 | | result.ProcessId = processId; |
| | | 70 | | |
| | 2 | 71 | | if (!InodeInfo.TryParse(span[ranges[offset++]], out var inodeInfo)) |
| | | 72 | | { |
| | 0 | 73 | | throw new IOException($"Invalid Inode '{span[ranges[offset]]}' specification in '/proc/locks' ({line})"); |
| | | 74 | | } |
| | | 75 | | |
| | 2 | 76 | | result.InodeInfo = inodeInfo; |
| | | 77 | | |
| | 2 | 78 | | return result; |
| | | 79 | | } |
| | | 80 | | } |