|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace Avalonia.X11; |
| 9 | + |
| 10 | +internal struct UtsName : IDisposable |
| 11 | +{ |
| 12 | + // https://github.com/torvalds/linux/blob/master/include/uapi/linux/utsname.h |
| 13 | + /* |
| 14 | + #define __NEW_UTS_LEN 64 |
| 15 | +
|
| 16 | + struct new_utsname |
| 17 | + { |
| 18 | + char sysname[__NEW_UTS_LEN + 1]; |
| 19 | + char nodename[__NEW_UTS_LEN + 1]; |
| 20 | + char release[__NEW_UTS_LEN + 1]; |
| 21 | + char version[__NEW_UTS_LEN + 1]; |
| 22 | + char machine[__NEW_UTS_LEN + 1]; |
| 23 | + char domainname[__NEW_UTS_LEN + 1]; |
| 24 | + }; |
| 25 | + */ |
| 26 | + |
| 27 | + private UtsName(IntPtr buffer) |
| 28 | + { |
| 29 | + _buffer = buffer; |
| 30 | + } |
| 31 | + |
| 32 | + public static UtsName GetUtsName() |
| 33 | + { |
| 34 | + var ntsNameStructSize = (UtsLength + 1) * FieldCount; |
| 35 | + |
| 36 | + IntPtr buffer = Marshal.AllocHGlobal(ntsNameStructSize); |
| 37 | + try |
| 38 | + { |
| 39 | + if (uname(buffer) != 0) |
| 40 | + { |
| 41 | + throw new InvalidOperationException("uname failed"); |
| 42 | + } |
| 43 | + |
| 44 | + return new UtsName(buffer); |
| 45 | + } |
| 46 | + catch |
| 47 | + { |
| 48 | + Marshal.FreeHGlobal(buffer); |
| 49 | + throw; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private const int SystemNameFieldIndex = 0; |
| 54 | + public Span<byte> SystemNameSpan => GetValue(SystemNameFieldIndex); |
| 55 | + public string SystemName => Encoding.UTF8.GetString(SystemNameSpan); |
| 56 | + |
| 57 | + private const int NodeNameFieldIndex = 1; |
| 58 | + public Span<byte> NodeNameSpan => GetValue(NodeNameFieldIndex); |
| 59 | + public string NodeName => Encoding.UTF8.GetString(NodeNameSpan); |
| 60 | + |
| 61 | + private const int ReleaseFieldIndex = 2; |
| 62 | + public Span<byte> ReleaseSpan => GetValue(ReleaseFieldIndex); |
| 63 | + public string Release => Encoding.UTF8.GetString(ReleaseSpan); |
| 64 | + |
| 65 | + private const int VersionFieldIndex = 3; |
| 66 | + public Span<byte> VersionSpan => GetValue(VersionFieldIndex); |
| 67 | + public string Version => Encoding.UTF8.GetString(VersionSpan); |
| 68 | + |
| 69 | + private const int MachineFieldIndex = 4; |
| 70 | + public Span<byte> MachineSpan => GetValue(MachineFieldIndex); |
| 71 | + public string Machine => Encoding.UTF8.GetString(MachineSpan); |
| 72 | + |
| 73 | + private const int DomainNameFieldIndex = 5; |
| 74 | + public Span<byte> DomainNameSpan => GetValue(DomainNameFieldIndex); |
| 75 | + public string DomainName => Encoding.UTF8.GetString(DomainNameSpan); |
| 76 | + |
| 77 | + private const int UtsLength = 64; |
| 78 | + |
| 79 | + private const int FieldCount = 6; |
| 80 | + |
| 81 | + private Span<byte> GetValue(int fieldIndex) |
| 82 | + { |
| 83 | + var startOffset = (UtsLength + 1) * fieldIndex; |
| 84 | + var length = 0; |
| 85 | + while (Marshal.ReadByte(_buffer, startOffset + length) != 0) |
| 86 | + { |
| 87 | + length++; |
| 88 | + } |
| 89 | + |
| 90 | + unsafe |
| 91 | + { |
| 92 | + return new Span<byte>((byte*)_buffer + startOffset, length); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + [DllImport("libc")] |
| 97 | + private static extern int uname(IntPtr buf); |
| 98 | + |
| 99 | + private readonly IntPtr _buffer; |
| 100 | + |
| 101 | + public void Dispose() |
| 102 | + { |
| 103 | + Marshal.FreeHGlobal(_buffer); |
| 104 | + } |
| 105 | +} |
0 commit comments