diff options
Diffstat (limited to 'src/mscorlib/corefx/Interop/Unix/System.Native')
18 files changed, 540 insertions, 0 deletions
diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Close.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Close.cs new file mode 100644 index 0000000000..8d192398a0 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Close.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Close", SetLastError = true)] + internal static extern int Close(IntPtr fd); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FLock.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FLock.cs new file mode 100644 index 0000000000..22934a3e77 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FLock.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum LockOperations : int + { + LOCK_SH = 1, /* shared lock */ + LOCK_EX = 2, /* exclusive lock */ + LOCK_NB = 4, /* don't block when locking*/ + LOCK_UN = 8, /* unlock */ + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FLock", SetLastError = true)] + internal static extern int FLock(SafeFileHandle fd, LockOperations operation); + + /// <summary> + /// Exposing this for SafeFileHandle.ReleaseHandle() to call. + /// Normal callers should use FLock(SafeFileHandle fd). + /// </summary> + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FLock", SetLastError = true)] + internal static extern int FLock(IntPtr fd, LockOperations operation); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FSync.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FSync.cs new file mode 100644 index 0000000000..e3ab970931 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FSync.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FSync", SetLastError = true)] + internal static extern int FSync(SafeFileHandle fd); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FTruncate.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FTruncate.cs new file mode 100644 index 0000000000..5dad650362 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.FTruncate.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FTruncate", SetLastError = true)] + internal static extern int FTruncate(SafeFileHandle fd, long length); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Fcntl.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Fcntl.cs new file mode 100644 index 0000000000..23b48a4f5d --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Fcntl.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum LockType : short + { + F_WRLCK = 1, // exclusive or write lock + F_UNLCK = 2 // unlock + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_LockFileRegion", SetLastError=true)] + internal static extern int LockFileRegion(SafeHandle fd, long offset, long length, LockType lockType); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetCwd.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetCwd.cs new file mode 100644 index 0000000000..724e342342 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetCwd.cs @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetCwd", SetLastError = true)] + private static unsafe extern byte* GetCwd(byte* buffer, int bufferLength); + + internal static unsafe string GetCwd() + { + const int StackLimit = 256; + + // First try to get the path into a buffer on the stack + byte* stackBuf = stackalloc byte[StackLimit]; + string result = GetCwdHelper(stackBuf, StackLimit); + if (result != null) + { + return result; + } + + // If that was too small, try increasing large buffer sizes + // until we get one that works or until we hit MaxPath. + int maxPath = Interop.Sys.MaxPath; + if (StackLimit < maxPath) + { + int bufferSize = StackLimit; + do + { + checked { bufferSize *= 2; } + var buf = new byte[Math.Min(bufferSize, maxPath)]; + fixed (byte* ptr = buf) + { + result = GetCwdHelper(ptr, buf.Length); + if (result != null) + { + return result; + } + } + } + while (bufferSize < maxPath); + } + + // If we couldn't get the cwd with a MaxPath-sized buffer, something's wrong. + throw Interop.GetExceptionForIoErrno(new ErrorInfo(Interop.Error.ENAMETOOLONG)); + } + + private static unsafe string GetCwdHelper(byte* ptr, int bufferSize) + { + // Call the real getcwd + byte* result = GetCwd(ptr, bufferSize); + + // If it returned non-null, the null-terminated path is in the buffer + if (result != null) + { + return Marshal.PtrToStringAnsi((IntPtr)ptr); + } + + // Otherwise, if it failed due to the buffer being too small, return null; + // for anything else, throw. + ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo(); + if (errorInfo.Error == Interop.Error.ERANGE) + { + return null; + } + throw Interop.GetExceptionForIoErrno(errorInfo); + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetUnixName.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetUnixName.cs new file mode 100644 index 0000000000..33664c4d39 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.GetUnixName.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetUnixName")] + private static extern IntPtr GetUnixNamePrivate(); + + internal static string GetUnixName() + { + IntPtr ptr = GetUnixNamePrivate(); + return Marshal.PtrToStringAnsi(ptr); + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.LSeek.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.LSeek.cs new file mode 100644 index 0000000000..7f8df7c6bf --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.LSeek.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum SeekWhence + { + SEEK_SET = 0, + SEEK_CUR = 1, + SEEK_END = 2 + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_LSeek", SetLastError = true)] + internal static extern long LSeek(SafeFileHandle fd, long offset, SeekWhence whence); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.MksTemps.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.MksTemps.cs new file mode 100644 index 0000000000..b8694d9007 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.MksTemps.cs @@ -0,0 +1,17 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MksTemps", SetLastError = true)] + internal static extern IntPtr MksTemps( + byte[] template, + int suffixlen); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Open.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Open.cs new file mode 100644 index 0000000000..a9a994c78c --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Open.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Open", SetLastError = true)] + internal static extern SafeFileHandle Open(string filename, OpenFlags flags, int mode); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.OpenFlags.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.OpenFlags.cs new file mode 100644 index 0000000000..f9e54c3cbc --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.OpenFlags.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +internal static partial class Interop +{ + internal static partial class Sys + { + [Flags] + internal enum OpenFlags + { + // Access modes (mutually exclusive) + O_RDONLY = 0x0000, + O_WRONLY = 0x0001, + O_RDWR = 0x0002, + + // Flags (combinable) + O_CLOEXEC = 0x0010, + O_CREAT = 0x0020, + O_EXCL = 0x0040, + O_TRUNC = 0x0080, + O_SYNC = 0x0100, + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs new file mode 100644 index 0000000000..4a1fcf67d0 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PathConf.cs @@ -0,0 +1,73 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal static int DEFAULT_PC_NAME_MAX = 255; + + internal enum PathConfName : int + { + PC_LINK_MAX = 1, + PC_MAX_CANON = 2, + PC_MAX_INPUT = 3, + PC_NAME_MAX = 4, + PC_PATH_MAX = 5, + PC_PIPE_BUF = 6, + PC_CHOWN_RESTRICTED = 7, + PC_NO_TRUNC = 8, + PC_VDISABLE = 9, + } + + /// <summary>The maximum path length for the system. -1 if it hasn't yet been initialized.</summary> + private static int s_maxPath = -1; + + /// <summary>The maximum name length for the system. -1 if it hasn't yet been initialized.</summary> + private static int s_maxName = -1; + + internal static int MaxPath + { + get + { + // Benign race condition on cached value + if (s_maxPath < 0) + { + // GetMaximumPath returns a long from PathConf + // but our callers expect an int so we need to convert. + long temp = GetMaximumPath(); + if (temp > int.MaxValue) + s_maxPath = int.MaxValue; + else + s_maxPath = Convert.ToInt32(temp); + } + return s_maxPath; + } + } + + internal static int MaxName + { + get + { + // Benign race condition on cached value + if (s_maxName < 0) + { + int result = PathConf("/", PathConfName.PC_NAME_MAX); + s_maxName = result >= 0 ? result : DEFAULT_PC_NAME_MAX; + } + + return s_maxName; + } + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_PathConf", SetLastError = true)] + private static extern int PathConf(string path, PathConfName name); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetMaximumPath")] + private static extern long GetMaximumPath(); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Permissions.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Permissions.cs new file mode 100644 index 0000000000..f1d13787d2 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Permissions.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +internal static partial class Interop +{ + internal static partial class Sys + { + [Flags] + internal enum Permissions + { + Mask = S_IRWXU | S_IRWXG | S_IRWXO, + + S_IRWXU = S_IRUSR | S_IWUSR | S_IXUSR, + S_IRUSR = 0x100, + S_IWUSR = 0x80, + S_IXUSR = 0x40, + + S_IRWXG = S_IRGRP | S_IWGRP | S_IXGRP, + S_IRGRP = 0x20, + S_IWGRP = 0x10, + S_IXGRP = 0x8, + + S_IRWXO = S_IROTH | S_IWOTH | S_IXOTH, + S_IROTH = 0x4, + S_IWOTH = 0x2, + S_IXOTH = 0x1, + } + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PosixFAdvise.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PosixFAdvise.cs new file mode 100644 index 0000000000..69e39b30d2 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.PosixFAdvise.cs @@ -0,0 +1,36 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + internal enum FileAdvice : int + { + POSIX_FADV_NORMAL = 0, /* no special advice, the default value */ + POSIX_FADV_RANDOM = 1, /* random I/O access */ + POSIX_FADV_SEQUENTIAL = 2, /* sequential I/O access */ + POSIX_FADV_WILLNEED = 3, /* will need specified pages */ + POSIX_FADV_DONTNEED = 4, /* don't need the specified pages */ + POSIX_FADV_NOREUSE = 5, /* data will only be acessed once */ + } + + /// <summary> + /// Notifies the OS kernel that the specified file will be accessed in a particular way soon; this allows the kernel to + /// potentially optimize the access pattern of the file. + /// </summary> + /// <param name="fd">The file descriptor of the file</param> + /// <param name="offset">The start of the region to advise about</param> + /// <param name="length">The number of bytes of the region (until the end of the file if 0)</param> + /// <param name="advice">The type of advice to give the kernel about the specified region</param> + /// <returns> + /// Returns 0 on success; otherwise, the error code is returned + /// </returns> + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_PosixFAdvise", SetLastError = false /* this is explicitly called out in the man page */)] + internal static extern int PosixFAdvise(SafeFileHandle fd, long offset, long length, FileAdvice advice); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Read.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Read.cs new file mode 100644 index 0000000000..812ae348dc --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Read.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + /// <summary> + /// Reads a number of bytes from an open file descriptor into a specified buffer. + /// </summary> + /// <param name="fd">The open file descriptor to try to read from</param> + /// <param name="buffer">The buffer to read info into</param> + /// <param name="count">The size of the buffer</param> + /// <returns> + /// Returns the number of bytes read on success; otherwise, -1 is returned + /// Note - on fail. the position of the stream may change depending on the platform; consult man 2 read for more info + /// </returns> + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Read", SetLastError = true)] + internal static unsafe extern int Read(SafeFileHandle fd, byte* buffer, int count); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Stat.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Stat.cs new file mode 100644 index 0000000000..a8bc2ec7d1 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Stat.cs @@ -0,0 +1,59 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + // Even though csc will by default use a sequential layout, a CS0649 warning as error + // is produced for un-assigned fields when no StructLayout is specified. + // + // Explicitly saying Sequential disables that warning/error for consumers which only + // use Stat in debug builds. + [StructLayout(LayoutKind.Sequential)] + internal struct FileStatus + { + internal FileStatusFlags Flags; + internal int Mode; + internal uint Uid; + internal uint Gid; + internal long Size; + internal long ATime; + internal long MTime; + internal long CTime; + internal long BirthTime; + } + + internal static class FileTypes + { + internal const int S_IFMT = 0xF000; + internal const int S_IFIFO = 0x1000; + internal const int S_IFCHR = 0x2000; + internal const int S_IFDIR = 0x4000; + internal const int S_IFREG = 0x8000; + internal const int S_IFLNK = 0xA000; + internal const int S_IFSOCK = 0xC000; + } + + [Flags] + internal enum FileStatusFlags + { + None = 0, + HasBirthTime = 1, + } + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FStat", SetLastError = true)] + internal static extern int FStat(SafeFileHandle fd, out FileStatus output); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Stat", SetLastError = true)] + internal static extern int Stat(string path, out FileStatus output); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_LStat", SetLastError = true)] + internal static extern int LStat(string path, out FileStatus output); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Unlink.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Unlink.cs new file mode 100644 index 0000000000..829210fa7e --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Unlink.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Sys + { + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Unlink", SetLastError = true)] + internal static extern int Unlink(string pathname); + } +} diff --git a/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Write.cs b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Write.cs new file mode 100644 index 0000000000..c14fc26263 --- /dev/null +++ b/src/mscorlib/corefx/Interop/Unix/System.Native/Interop.Write.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; + +internal static partial class Interop +{ + internal static partial class Sys + { + /// <summary> + /// Writes the specified buffer to the provided open file descriptor + /// </summary> + /// <param name="fd">The file descriptor to try and write to</param> + /// <param name="buffer">The data to attempt to write</param> + /// <param name="bufferSize">The amount of data to write, in bytes</param> + /// <returns> + /// Returns the number of bytes written on success; otherwise, returns -1 and sets errno + /// </returns> + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Write", SetLastError = true)] + internal static unsafe extern int Write(SafeFileHandle fd, byte* buffer, int bufferSize); + + [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Write", SetLastError = true)] + internal static unsafe extern int Write(int fd, byte* buffer, int bufferSize); + } +} |