diff --git a/src/libraries/Common/src/Interop/Linux/Interop.Errors.cs b/src/libraries/Common/src/Interop/Linux/Interop.Errors.cs new file mode 100644 index 00000000000..d371810d8d5 --- /dev/null +++ b/src/libraries/Common/src/Interop/Linux/Interop.Errors.cs @@ -0,0 +1,113 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +internal static partial class Interop +{ + /// Common Unix errno error codes. + internal static partial class Errors + { + // These values were defined in: + // include/asm-generic/errno.h + + internal const int EWOULDBLOCK = 11; + internal const int EAGAIN = 11; + + internal const int EDEADLK = 35; + internal const int EDEADLOCK = EDEADLK; + internal const int ENAMETOOLONG = 36; + internal const int ENOLCK = 37; + internal const int ENOSYS = 38; + internal const int ENOTEMPTY = 39; + internal const int ELOOP = 40; + internal const int ENOMSG = 42; + internal const int EIDRM = 43; + internal const int ECHRNG = 44; + internal const int EL2NSYNC = 45; + internal const int EL3HLT = 46; + internal const int EL3RST = 47; + internal const int ELNRNG = 48; + internal const int EUNATCH = 49; + internal const int ENOCSI = 50; + internal const int EL2HLT = 51; + internal const int EBADE = 52; + internal const int EBADR = 53; + internal const int EXFULL = 54; + internal const int ENOANO = 55; + internal const int EBADRQC = 56; + internal const int EBADSLT = 57; + internal const int EBFONT = 59; + internal const int ENOSTR = 60; + internal const int ENODATA = 61; + internal const int ETIME = 62; + internal const int ENOSR = 63; + internal const int ENONET = 64; + internal const int ENOPKG = 65; + internal const int EREMOTE = 66; + internal const int ENOLINK = 67; + internal const int EADV = 68; + internal const int ESRMNT = 69; + internal const int ECOMM = 70; + internal const int EPROTO = 71; + internal const int EMULTIHOP = 72; + internal const int EDOTDOT = 73; + internal const int EBADMSG = 74; + internal const int EOVERFLOW = 75; + internal const int ENOTUNIQ = 76; + internal const int EBADFD = 77; + internal const int EREMCHG = 78; + internal const int ELIBACC = 79; + internal const int ELIBBAD = 80; + internal const int ELIBSCN = 81; + internal const int ELIBMAX = 82; + internal const int ELIBEXEC = 83; + internal const int EILSEQ = 84; + internal const int ERESTART = 85; + internal const int ESTRPIPE = 86; + internal const int EUSERS = 87; + internal const int ENOTSOCK = 88; + internal const int EDESTADDRREQ = 89; + internal const int EMSGSIZE = 90; + internal const int EPROTOTYPE = 91; + internal const int ENOPROTOOPT = 92; + internal const int EPROTONOSUPPORT = 93; + internal const int ESOCKTNOSUPPORT = 94; + internal const int EOPNOTSUPP = 95; + internal const int EPFNOSUPPORT = 96; + internal const int EAFNOSUPPORT = 97; + internal const int EADDRINUSE = 98; + internal const int EADDRNOTAVAIL = 99; + internal const int ENETDOWN = 100; + internal const int ENETUNREACH = 101; + internal const int ENETRESET = 102; + internal const int ECONNABORTED = 103; + internal const int ECONNRESET = 104; + internal const int ENOBUFS = 105; + internal const int EISCONN = 106; + internal const int ENOTCONN = 107; + internal const int ESHUTDOWN = 108; + internal const int ETOOMANYREFS = 109; + internal const int ETIMEDOUT = 110; + internal const int ECONNREFUSED = 111; + internal const int EHOSTDOWN = 112; + internal const int EHOSTUNREACH = 113; + internal const int EALREADY = 114; + internal const int EINPROGRESS = 115; + internal const int ESTALE = 116; + internal const int EUCLEAN = 117; + internal const int ENOTNAM = 118; + internal const int ENAVAIL = 119; + internal const int EISNAM = 120; + internal const int EREMOTEIO = 121; + internal const int EDQUOT = 122; + internal const int ENOMEDIUM = 123; + internal const int EMEDIUMTYPE = 124; + internal const int ECANCELED = 125; + internal const int ENOKEY = 126; + internal const int EKEYEXPIRED = 127; + internal const int EKEYREVOKED = 128; + internal const int EKEYREJECTED = 129; + internal const int EOWNERDEAD = 130; + internal const int ENOTRECOVERABLE = 131; + internal const int ERFKILL = 132; + } +} diff --git a/src/libraries/Common/src/Interop/Linux/Interop.Libraries.cs b/src/libraries/Common/src/Interop/Linux/Interop.Libraries.cs new file mode 100644 index 00000000000..0e0e069ca54 --- /dev/null +++ b/src/libraries/Common/src/Interop/Linux/Interop.Libraries.cs @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +internal static partial class Interop +{ + private static partial class Libraries + { + internal const string LibRt = "librt"; // POSIX Realtime Extensions library + } +} diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.getenv.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.MemoryMappedFlags.cs similarity index 54% rename from src/libraries/Common/src/Interop/Unix/libc/Interop.getenv.cs rename to src/libraries/Common/src/Interop/Linux/libc/Interop.MemoryMappedFlags.cs index e6152dc0d79..0f9a9582ce4 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.getenv.cs +++ b/src/libraries/Common/src/Interop/Linux/libc/Interop.MemoryMappedFlags.cs @@ -8,12 +8,14 @@ internal static partial class Interop { internal static partial class libc { - internal static string getenv(string name) + [Flags] + internal enum MemoryMappedFlags { - return Marshal.PtrToStringAnsi(getenv_core(name)); // TODO: Use correct encoding + MAP_FILE = 0x0, + MAP_SHARED = 0x01, + MAP_PRIVATE = 0x02, + MAP_FIXED = 0x10, + MAP_ANONYMOUS = 0x20, } - - [DllImport(Libraries.Libc, EntryPoint = "getenv")] - private static extern IntPtr getenv_core(string name); } } diff --git a/src/libraries/Common/src/Interop/Linux/libc/Interop.MemoryMappedSyncFlags.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.MemoryMappedSyncFlags.cs new file mode 100644 index 00000000000..0d0311666a0 --- /dev/null +++ b/src/libraries/Common/src/Interop/Linux/libc/Interop.MemoryMappedSyncFlags.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +internal static partial class Interop +{ + internal static partial class libc + { + [Flags] + internal enum MemoryMappedSyncFlags + { + MS_ASYNC = 0x1, + MS_INVALIDATE = 0x2, + MS_SYNC = 0x4 + } + } +} diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.OpenFlags.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.OpenFlags.cs similarity index 94% rename from src/libraries/Common/src/Interop/Unix/libc/Interop.OpenFlags.cs rename to src/libraries/Common/src/Interop/Linux/libc/Interop.OpenFlags.cs index 2dd3ea0c820..4cceda77cc5 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.OpenFlags.cs +++ b/src/libraries/Common/src/Interop/Linux/libc/Interop.OpenFlags.cs @@ -18,7 +18,6 @@ internal static partial class Interop O_TRUNC = 0x200, O_SYNC = 0x1000, O_ASYNC = 0x2000, - O_LARGEFILE = 0x8000, O_CLOEXEC = 0x80000, } } diff --git a/src/libraries/Common/src/Interop/Linux/libc/Interop.PathConfNames.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.PathConfNames.cs new file mode 100644 index 00000000000..8b9e2dfdade --- /dev/null +++ b/src/libraries/Common/src/Interop/Linux/libc/Interop.PathConfNames.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +internal static partial class Interop +{ + internal static partial class libc + { + internal static class PathConfNames + { + internal const int _PC_NAME_MAX = 3; + internal const int _PC_PATH_MAX = 4; + } + + internal static int DEFAULT_PC_NAME_MAX = 255; + internal static int DEFAULT_PC_PATH_MAX = 4096; + } +} diff --git a/src/libraries/Common/src/Interop/Linux/libc/Interop.SysConfNames.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.SysConfNames.cs new file mode 100644 index 00000000000..3c1832feef2 --- /dev/null +++ b/src/libraries/Common/src/Interop/Linux/libc/Interop.SysConfNames.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class libc + { + internal static class SysConfNames + { + internal const int _SC_CLK_TCK = 2; + internal const int _SC_PAGESIZE = 30; + } + } +} diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.madvise.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.madvise.cs similarity index 100% rename from src/libraries/Common/src/Interop/Unix/libc/Interop.madvise.cs rename to src/libraries/Common/src/Interop/Linux/libc/Interop.madvise.cs diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.posix_fadvise.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.posix_fadvise.cs similarity index 100% rename from src/libraries/Common/src/Interop/Unix/libc/Interop.posix_fadvise.cs rename to src/libraries/Common/src/Interop/Linux/libc/Interop.posix_fadvise.cs diff --git a/src/libraries/Common/src/Interop/Unix/librt/Interop.shm_open.cs b/src/libraries/Common/src/Interop/Linux/librt/Interop.shm_open.cs similarity index 100% rename from src/libraries/Common/src/Interop/Unix/librt/Interop.shm_open.cs rename to src/libraries/Common/src/Interop/Linux/librt/Interop.shm_open.cs diff --git a/src/libraries/Common/src/Interop/OSX/Interop.Errors.cs b/src/libraries/Common/src/Interop/OSX/Interop.Errors.cs new file mode 100644 index 00000000000..df9685b1d6d --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/Interop.Errors.cs @@ -0,0 +1,90 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +internal static partial class Interop +{ + /// Common Unix errno error codes. + internal static partial class Errors + { + // These values were defined in: + // /usr/include/sys/errno.h + + internal const int EDEADLK = 11; + internal const int EDEADLOCK = EDEADLK; + + internal const int EAGAIN = 35; + internal const int EWOULDBLOCK = EAGAIN; + internal const int EINPROGRESS = 36; + internal const int EALREADY = 37; + internal const int ENOTSOCK = 38; + internal const int EDESTADDRREQ = 39; + internal const int EMSGSIZE = 40; + internal const int EPROTOTYPE = 41; + internal const int ENOPROTOOPT = 42; + internal const int EPROTONOSUPPORT = 43; + internal const int ESOCKTNOSUPPORT = 44; + internal const int ENOTSUP = 45; + internal const int EOPNOTSUPP = ENOTSUP; + internal const int EPFNOSUPPORT = 46; + internal const int EAFNOSUPPORT = 47; + internal const int EADDRINUSE = 48; + internal const int ADDRNOTAVAIL = 49; + internal const int ENETDOWN = 50; + internal const int ENETUNREACH = 51; + internal const int ENETRESET = 52; + internal const int ECONNABORTED = 53; + internal const int ECONNRESET = 54; + internal const int ENOBUFS = 55; + internal const int EISCONN = 56; + internal const int ENOTCONN = 57; + internal const int ESHUTDOWN = 58; + internal const int ETOOMANYREFS = 59; + internal const int ETIMEDOUT = 60; + internal const int ECONNREFUSED = 61; + internal const int ELOOP = 62; + internal const int ENAMETOOLONG = 63; + internal const int EHOSTDOWN = 64; + internal const int EHOSTUNREACH = 65; + internal const int ENOTEMPTY = 66; + internal const int EPROCLIM = 67; + internal const int EUSERS = 68; + internal const int EDQUOT = 69; + internal const int ESTALE = 70; + internal const int EREMOTE = 71; + internal const int EBADRPC = 72; + internal const int ERPCMISMATCH = 73; + internal const int EPROGUNAVAIL = 74; + internal const int EPROGMISMATCH = 75; + internal const int EPROCUNAVAIL = 76; + internal const int ENOLCK = 77; + internal const int ENOSYS = 78; + internal const int EFTYPE = 79; + internal const int EAUTH = 80; + internal const int ENEEDAUTH = 81; + internal const int EPWROFF = 82; + internal const int EDEVERR = 83; + internal const int EOVERFLOW = 84; + internal const int EBADEXEC = 85; + internal const int EBADARCH = 86; + internal const int ESHLIBVERS = 87; + internal const int EBADMACHO = 88; + internal const int ECANCELED = 89; + internal const int EIDRM = 90; + internal const int ENOMSG = 91; + internal const int EILSEQ = 92; + internal const int ENOATTR = 93; + internal const int EBADMSG = 94; + internal const int EMULTIHOP = 95; + internal const int ENODATA = 96; + internal const int ENOLINK = 97; + internal const int ENOSR = 98; + internal const int ENOSTR = 99; + internal const int EPROTO = 100; + internal const int ETIME = 101; + internal const int ENOPOLICY = 103; + internal const int ENOTRECOVERABLE = 104; + internal const int EOWNERDEAD = 105; + internal const int EQFULL = 106; + internal const int ELAST = 106; + } +} diff --git a/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs b/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs index d67f45557a4..0ed6073a396 100644 --- a/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs +++ b/src/libraries/Common/src/Interop/OSX/Interop.Libraries.cs @@ -8,5 +8,6 @@ internal static partial class Interop internal const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"; internal const string CoreServicesLibrary = "/System/Library/Frameworks/CoreServices.framework/CoreServices"; internal const string libproc = "libproc"; + internal const string LibSystemKernel = "/usr/lib/system/libsystem_kernel"; } } diff --git a/src/libraries/Common/src/Interop/OSX/libc/Interop.MemoryMappedFlags.cs b/src/libraries/Common/src/Interop/OSX/libc/Interop.MemoryMappedFlags.cs new file mode 100644 index 00000000000..f43e386c8b8 --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/libc/Interop.MemoryMappedFlags.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class libc + { + [Flags] + internal enum MemoryMappedFlags + { + MAP_FILE = 0x0, + MAP_SHARED = 0x01, + MAP_PRIVATE = 0x02, + MAP_FIXED = 0x10, + MAP_ANONYMOUS = 0x1000, + } + } +} diff --git a/src/libraries/Common/src/Interop/OSX/libc/Interop.MemoryMappedSyncFlags.cs b/src/libraries/Common/src/Interop/OSX/libc/Interop.MemoryMappedSyncFlags.cs new file mode 100644 index 00000000000..43c57f4cf0a --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/libc/Interop.MemoryMappedSyncFlags.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +internal static partial class Interop +{ + internal static partial class libc + { + [Flags] + internal enum MemoryMappedSyncFlags + { + MS_ASYNC = 0x1, + MS_INVALIDATE = 0x2, + MS_SYNC = 0x10 + } + } +} diff --git a/src/libraries/Common/src/Interop/OSX/libc/Interop.OpenFlags.cs b/src/libraries/Common/src/Interop/OSX/libc/Interop.OpenFlags.cs new file mode 100644 index 00000000000..de8eb49aba5 --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/libc/Interop.OpenFlags.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +internal static partial class Interop +{ + internal static partial class libc + { + [Flags] + internal enum OpenFlags + { + O_RDONLY = 0x0, + O_WRONLY = 0x1, + O_RDWR = 0x2, + O_CREAT = 0x200, + O_EXCL = 0x800, + O_TRUNC = 0x400, + O_SYNC = 0x80, + O_ASYNC = 0x40, + O_CLOEXEC = 0x1000000, + } + } +} diff --git a/src/libraries/Common/src/Interop/OSX/libc/Interop.PathConfNames.cs b/src/libraries/Common/src/Interop/OSX/libc/Interop.PathConfNames.cs new file mode 100644 index 00000000000..d300c47e938 --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/libc/Interop.PathConfNames.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +internal static partial class Interop +{ + internal static partial class libc + { + internal static class PathConfNames + { + internal const int _PC_NAME_MAX = 4; + internal const int _PC_PATH_MAX = 5; + } + + internal static int DEFAULT_PC_NAME_MAX = 255; + internal static int DEFAULT_PC_PATH_MAX = 1024; + } +} diff --git a/src/libraries/Common/src/Interop/OSX/libc/Interop.SysConfNames.cs b/src/libraries/Common/src/Interop/OSX/libc/Interop.SysConfNames.cs new file mode 100644 index 00000000000..dee94bfd733 --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/libc/Interop.SysConfNames.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class libc + { + internal static class SysConfNames + { + internal const int _SC_CLK_TCK = 3; + internal const int _SC_PAGESIZE = 29; + } + } +} diff --git a/src/libraries/Common/src/Interop/OSX/libsystem_kernel/Interop.shm_open.cs b/src/libraries/Common/src/Interop/OSX/libsystem_kernel/Interop.shm_open.cs new file mode 100644 index 00000000000..604790d560f --- /dev/null +++ b/src/libraries/Common/src/Interop/OSX/libsystem_kernel/Interop.shm_open.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Runtime.InteropServices; + +using mode_t = System.Int32; + +internal static partial class Interop +{ + internal static partial class libc + { + [DllImport(Libraries.LibSystemKernel, SetLastError = true)] + internal static extern int shm_open(string name, OpenFlags flags, mode_t mode); + + [DllImport(Libraries.LibSystemKernel, SetLastError = true)] + internal static extern int shm_unlink(string name); + } +} diff --git a/src/libraries/Common/src/Interop/Unix/Interop.Errors.cs b/src/libraries/Common/src/Interop/Unix/Interop.Errors.cs index ba959e63d05..d34cb0cf73b 100644 --- a/src/libraries/Common/src/Interop/Unix/Interop.Errors.cs +++ b/src/libraries/Common/src/Interop/Unix/Interop.Errors.cs @@ -4,11 +4,11 @@ internal static partial class Interop { /// Common Unix errno error codes. - internal static class Errors + internal static partial class Errors { // These values were defined in: // include/asm-generic/errno-base.h - // include/asm-generic/errno.h + // /usr/include/sys/errno.h internal const int EPERM = 1; internal const int ENOENT = 2; @@ -20,8 +20,7 @@ internal static partial class Interop internal const int ENOEXEC = 8; internal const int EBADF = 9; internal const int ECHILD = 10; - internal const int EAGAIN = 11; - internal const int EWOULDBLOCK = 11; + internal const int ENOMEM = 12; internal const int EACCES = 13; internal const int EFAULT = 14; @@ -45,102 +44,5 @@ internal static partial class Interop internal const int EPIPE = 32; internal const int EDOM = 33; internal const int ERANGE = 34; - internal const int EDEADLK = 35; - internal const int EDEADLOCK = EDEADLK; - internal const int ENAMETOOLONG = 36; - internal const int ENOLCK = 37; - internal const int ENOSYS = 38; - internal const int ENOTEMPTY = 39; - internal const int ELOOP = 40; - internal const int ENOMSG = 42; - internal const int EIDRM = 43; - internal const int ECHRNG = 44; - internal const int EL2NSYNC = 45; - internal const int EL3HLT = 46; - internal const int EL3RST = 47; - internal const int ELNRNG = 48; - internal const int EUNATCH = 49; - internal const int ENOCSI = 50; - internal const int EL2HLT = 51; - internal const int EBADE = 52; - internal const int EBADR = 53; - internal const int EXFULL = 54; - internal const int ENOANO = 55; - internal const int EBADRQC = 56; - internal const int EBADSLT = 57; - internal const int EBFONT = 59; - internal const int ENOSTR = 60; - internal const int ENODATA = 61; - internal const int ETIME = 62; - internal const int ENOSR = 63; - internal const int ENONET = 64; - internal const int ENOPKG = 65; - internal const int EREMOTE = 66; - internal const int ENOLINK = 67; - internal const int EADV = 68; - internal const int ESRMNT = 69; - internal const int ECOMM = 70; - internal const int EPROTO = 71; - internal const int EMULTIHOP = 72; - internal const int EDOTDOT = 73; - internal const int EBADMSG = 74; - internal const int EOVERFLOW = 75; - internal const int ENOTUNIQ = 76; - internal const int EBADFD = 77; - internal const int EREMCHG = 78; - internal const int ELIBACC = 79; - internal const int ELIBBAD = 80; - internal const int ELIBSCN = 81; - internal const int ELIBMAX = 82; - internal const int ELIBEXEC = 83; - internal const int EILSEQ = 84; - internal const int ERESTART = 85; - internal const int ESTRPIPE = 86; - internal const int EUSERS = 87; - internal const int ENOTSOCK = 88; - internal const int EDESTADDRREQ = 89; - internal const int EMSGSIZE = 90; - internal const int EPROTOTYPE = 91; - internal const int ENOPROTOOPT = 92; - internal const int EPROTONOSUPPORT = 93; - internal const int ESOCKTNOSUPPORT = 94; - internal const int EOPNOTSUPP = 95; - internal const int EPFNOSUPPORT = 96; - internal const int EAFNOSUPPORT = 97; - internal const int EADDRINUSE = 98; - internal const int EADDRNOTAVAIL = 99; - internal const int ENETDOWN = 100; - internal const int ENETUNREACH = 101; - internal const int ENETRESET = 102; - internal const int ECONNABORTED = 103; - internal const int ECONNRESET = 104; - internal const int ENOBUFS = 105; - internal const int EISCONN = 106; - internal const int ENOTCONN = 107; - internal const int ESHUTDOWN = 108; - internal const int ETOOMANYREFS = 109; - internal const int ETIMEDOUT = 110; - internal const int ECONNREFUSED = 111; - internal const int EHOSTDOWN = 112; - internal const int EHOSTUNREACH = 113; - internal const int EALREADY = 114; - internal const int EINPROGRESS = 115; - internal const int ESTALE = 116; - internal const int EUCLEAN = 117; - internal const int ENOTNAM = 118; - internal const int ENAVAIL = 119; - internal const int EISNAM = 120; - internal const int EREMOTEIO = 121; - internal const int EDQUOT = 122; - internal const int ENOMEDIUM = 123; - internal const int EMEDIUMTYPE = 124; - internal const int ECANCELED = 125; - internal const int ENOKEY = 126; - internal const int EKEYEXPIRED = 127; - internal const int EKEYREVOKED = 128; - internal const int EKEYREJECTED = 129; - internal const int EOWNERDEAD = 130; - internal const int ENOTRECOVERABLE = 131; - internal const int ERFKILL = 132; } } diff --git a/src/libraries/Common/src/Interop/Unix/Interop.IOErrors.cs b/src/libraries/Common/src/Interop/Unix/Interop.IOErrors.cs index 19114700d3d..4950ab48e49 100644 --- a/src/libraries/Common/src/Interop/Unix/Interop.IOErrors.cs +++ b/src/libraries/Common/src/Interop/Unix/Interop.IOErrors.cs @@ -17,15 +17,20 @@ internal static partial class Interop /// The result of the system call. /// The path with which this error is associated. This may be null. /// true if the is known to be a directory; otherwise, false. + /// Optional function to change an error code prior to processing it. /// /// true if the system call should be retried due to it being interrupted; otherwise, false. /// An exception will be thrown if the system call failed for any reason other than interruption. /// - internal static bool CheckIo(long result, string path = null, bool isDirectory = false) + internal static bool CheckIo(long result, string path = null, bool isDirectory = false, Func errorRewriter = null) { if (result < 0) { int errno = Marshal.GetLastWin32Error(); + if (errorRewriter != null) + { + errno = errorRewriter(errno); + } if (errno != Interop.Errors.EINTR) { throw Interop.GetExceptionForIoErrno(errno, path, isDirectory); @@ -74,7 +79,7 @@ internal static partial class Interop case Errors.EACCES: case Errors.EBADF: - case Errors.EISDIR: + case Errors.EPERM: return !string.IsNullOrEmpty(path) ? new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, path)) : new UnauthorizedAccessException(SR.UnauthorizedAccess_IODenied_NoPathName); @@ -101,7 +106,12 @@ internal static partial class Interop goto default; default: - return new IOException(libc.strerror(errno), errno); + return GetIOException(errno); } } + + internal static Exception GetIOException(int errno) + { + return new IOException(libc.strerror(errno), errno); + } } diff --git a/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs b/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs index 7bd16387f1a..52bd96f5d75 100644 --- a/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs +++ b/src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs @@ -5,9 +5,8 @@ internal static partial class Interop { private static partial class Libraries { - internal const string Libc = "libc"; // C library - internal const string LibRt = "librt"; // POSIX Realtime Extensions library + internal const string Libc = "libc"; // C library internal const string LibCoreClrPal = "libcoreclrpal"; // CoreCLR runtime - internal const string LibCrypto = "libcrypto"; // OpenSSL crypto library + internal const string LibCrypto = "libcrypto"; // OpenSSL crypto library } } diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.MemoryMappedProtections.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.MemoryMappedProtections.cs new file mode 100644 index 00000000000..66225adc681 --- /dev/null +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.MemoryMappedProtections.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class libc + { + [Flags] + internal enum MemoryMappedProtections + { + PROT_NONE = 0x0, + PROT_READ = 0x1, + PROT_WRITE = 0x2, + PROT_EXEC = 0x4 + } + } +} diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.Signals.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.Signals.cs index e48bff4a021..da2b529aa0a 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.Signals.cs +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.Signals.cs @@ -7,41 +7,7 @@ internal static partial class Interop { internal static partial class Signals { - internal const int SIGHUP = 1; - internal const int SIGINT = 2; - internal const int SIGQUIT = 3; - internal const int SIGILL = 4; - internal const int SIGTRAP = 5; - internal const int SIGABRT = 6; - internal const int SIGIOT = 6; - internal const int SIGBUS = 7; - internal const int SIGFPE = 8; internal const int SIGKILL = 9; - internal const int SIGUSR1 = 10; - internal const int SIGSEGV = 11; - internal const int SIGUSR2 = 12; - internal const int SIGPIPE = 13; - internal const int SIGALRM = 14; - internal const int SIGTERM = 15; - internal const int SIGSTKFLT = 16; - internal const int SIGCLD = SIGCHLD; - internal const int SIGCHLD = 17; - internal const int SIGCONT = 18; - internal const int SIGSTOP = 19; - internal const int SIGTSTP = 20; - internal const int SIGTTIN = 21; - internal const int SIGTTOU = 22; - internal const int SIGURG = 23; - internal const int SIGXCPU = 24; - internal const int SIGXFSZ = 25; - internal const int SIGVTALRM = 26; - internal const int SIGPROF = 27; - internal const int SIGWINCH = 28; - internal const int SIGIO = 29; - internal const int SIGPOLL = SIGIO; - internal const int SIGPWR = 30; - internal const int SIGSYS = 31; - internal const int SIGUNUSED = 31; } } } \ No newline at end of file diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.fnmatch.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.fnmatch.cs index 5a0435857cd..7f3c196e272 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.fnmatch.cs +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.fnmatch.cs @@ -14,14 +14,7 @@ internal static partial class Interop [Flags] internal enum FnmatchFlags { - None = 0, - FNM_PATHNAME = (1 << 0), - FNM_FILE_NAME = FNM_PATHNAME, - FNM_NOESCAPE = (1 << 1), - FNM_PERIOD = (1 << 2), - FNM_LEADING_DIR = (1 << 3), - FNM_CASEFOLD = (1 << 4), - FNM_EXTMATCH = (1 << 5) + None = 0 } } } diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.mmap.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.mmap.cs index b885392b348..d372731253c 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.mmap.cs +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.mmap.cs @@ -13,27 +13,8 @@ internal static partial class Interop { [DllImport(Libraries.Libc, SetLastError = true)] internal static extern IntPtr mmap( - IntPtr __addr, size_t __len, - MemoryMappedProtections __prot, MemoryMappedFlags __flags, - int __fd, off_t __offset); - - [Flags] - internal enum MemoryMappedProtections - { - PROT_NONE = 0x0, - PROT_READ = 0x1, - PROT_WRITE = 0x2, - PROT_EXEC = 0x4 - } - - [Flags] - internal enum MemoryMappedFlags - { - MAP_FILE = 0, - MAP_SHARED = 0x01, - MAP_PRIVATE = 0x02, - MAP_FIXED = 0x10, - MAP_ANONYMOUS = 0x20, - } + IntPtr addr, size_t len, + MemoryMappedProtections prot, MemoryMappedFlags flags, + int fd, off_t offset); } } diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.msync.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.msync.cs index c72ef828a7b..50f49a870a6 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.msync.cs +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.msync.cs @@ -11,14 +11,6 @@ internal static partial class Interop internal static partial class libc { [DllImport(Libraries.Libc, SetLastError = true)] - internal static extern int msync(IntPtr __addr, size_t __len, MemoryMappedSyncFlags __flags); - - [Flags] - internal enum MemoryMappedSyncFlags - { - MS_ASYNC = 1, - MS_INVALIDATE = 2, - MS_SYNC = 4 - } + internal static extern int msync(IntPtr addr, size_t len, MemoryMappedSyncFlags flags); } } diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.munmap.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.munmap.cs index efb320d592a..90cb730d1f1 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.munmap.cs +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.munmap.cs @@ -11,6 +11,6 @@ internal static partial class Interop internal static partial class libc { [DllImport(Libraries.Libc, SetLastError = true)] - internal static extern int munmap(IntPtr __addr, size_t __len); + internal static extern int munmap(IntPtr addr, size_t len); } } diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.pathconf.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.pathconf.cs index 00ea5f5a93a..cd59569e5da 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.pathconf.cs +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.pathconf.cs @@ -27,14 +27,5 @@ internal static partial class Interop [DllImport(Libraries.Libc, SetLastError = true)] private static extern int pathconf(string path, int name); - - internal static class PathConfNames - { - internal const int _PC_NAME_MAX = 3; - internal const int _PC_PATH_MAX = 4; - } - - internal static int DEFAULT_PC_NAME_MAX = 255; - internal static int DEFAULT_PC_PATH_MAX = 4096; } } diff --git a/src/libraries/Common/src/Interop/Unix/libc/Interop.sysconf.cs b/src/libraries/Common/src/Interop/Unix/libc/Interop.sysconf.cs index 172cdae85d9..da25c729de7 100644 --- a/src/libraries/Common/src/Interop/Unix/libc/Interop.sysconf.cs +++ b/src/libraries/Common/src/Interop/Unix/libc/Interop.sysconf.cs @@ -9,11 +9,5 @@ internal static partial class Interop { [DllImport(Libraries.Libc, SetLastError = true)] internal static extern int sysconf(int name); - - internal static class SysConfNames - { - internal const int _SC_CLK_TCK = 2; - internal const int _SC_PAGESIZE = 30; - } } } diff --git a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs index b3674483ed9..6c7a49b4342 100644 --- a/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs +++ b/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs @@ -43,7 +43,8 @@ namespace Microsoft.Win32.SafeHandles // Open the file. int fd; - while (Interop.CheckIo(fd = Interop.libc.open(path, flags, mode), path, isDirectory: enoentDueToDirectory)) ; + while (Interop.CheckIo(fd = Interop.libc.open(path, flags, mode), path, isDirectory: enoentDueToDirectory, + errorRewriter: errno => (errno == Interop.Errors.EISDIR) ? Interop.Errors.EACCES : errno)) ; Debug.Assert(fd >= 0); handle.SetHandle((IntPtr)fd); Debug.Assert(!handle.IsInvalid); diff --git a/src/libraries/System.Console/src/System.Console.csproj b/src/libraries/System.Console/src/System.Console.csproj index 41e51e3825b..38d892ef451 100644 --- a/src/libraries/System.Console/src/System.Console.csproj +++ b/src/libraries/System.Console/src/System.Console.csproj @@ -113,9 +113,6 @@ Common\Interop\Unix\Interop.close.cs - - Common\Interop\Unix\Interop.getenv.cs - Common\Interop\Unix\Interop.gnu_get_libc_version.cs @@ -125,9 +122,6 @@ Common\Interop\Unix\Interop.open.cs - - Common\Interop\Unix\Interop.OpenFlags.cs - Common\Interop\Unix\Interop.read.cs @@ -144,6 +138,22 @@ Common\Interop\Unix\Interop.GetFileInformation.cs" + + + Common\Interop\Linux\Interop.Errors.cs + + + Common\Interop\Linux\Interop.OpenFlags.cs + + + + + Common\Interop\OSX\Interop.Errors.cs + + + Common\Interop\OSX\Interop.OpenFlags.cs + + diff --git a/src/libraries/System.Console/src/System/ConsolePal.Unix.cs b/src/libraries/System.Console/src/System/ConsolePal.Unix.cs index e718e384149..75aa60d4899 100644 --- a/src/libraries/System.Console/src/System/ConsolePal.Unix.cs +++ b/src/libraries/System.Console/src/System/ConsolePal.Unix.cs @@ -98,7 +98,7 @@ namespace System string locale = null; foreach (string envVar in LocaleEnvVars) { - locale = Interop.libc.getenv(envVar); + locale = Environment.GetEnvironmentVariable(envVar); if (!string.IsNullOrWhiteSpace(locale)) break; } @@ -433,7 +433,7 @@ namespace System /// The database, or null if it could not be found. private static Database ReadDatabase() { - string term = Interop.libc.getenv("TERM"); + string term = Environment.GetEnvironmentVariable("TERM"); return !string.IsNullOrEmpty(term) ? ReadDatabase(term) : null; } @@ -456,14 +456,14 @@ namespace System Database db; // First try a location specified in the TERMINFO environment variable. - string terminfo = Interop.libc.getenv("TERMINFO"); + string terminfo = Environment.GetEnvironmentVariable("TERMINFO"); if (!string.IsNullOrWhiteSpace(terminfo) && (db = ReadDatabase(term, terminfo)) != null) { return db; } // Then try in the user's home directory. - string home = Interop.libc.getenv("HOME"); + string home = Environment.GetEnvironmentVariable("HOME"); if (!string.IsNullOrWhiteSpace(home) && (db = ReadDatabase(term, home + "/.terminfo")) != null) { return db; @@ -546,15 +546,7 @@ namespace System } finally { - int result = Interop.libc.close(fd); - if (result < 0) - { - int errno = Marshal.GetLastWin32Error(); - if (errno != Interop.Errors.EINTR) // Avoid retrying close on EINTR, e.g. https://lkml.org/lkml/2005/9/11/49 - { - throw Interop.GetExceptionForIoErrno(errno); - } - } + Interop.CheckIo(Interop.libc.close(fd)); // Avoid retrying close on EINTR, e.g. https://lkml.org/lkml/2005/9/11/49 } } diff --git a/src/libraries/System.Diagnostics.Debug/src/System.Diagnostics.Debug.csproj b/src/libraries/System.Diagnostics.Debug/src/System.Diagnostics.Debug.csproj index d533217b77b..7b10eeea3a9 100644 --- a/src/libraries/System.Diagnostics.Debug/src/System.Diagnostics.Debug.csproj +++ b/src/libraries/System.Diagnostics.Debug/src/System.Diagnostics.Debug.csproj @@ -66,9 +66,6 @@ Common\Interop\Unix\libc\Interop.open.cs - - Common\Interop\Unix\Interop.OpenFlags.cs - Common\Interop\Unix\libc\Interop.strerror.cs @@ -85,6 +82,24 @@ Common\Microsoft\Win32\SafeHandles\SafeFileHandle.Unix.cs + + + + Common\Interop\Linux\Interop.Errors.cs + + + Common\Interop\Linux\Interop.OpenFlags.cs + + + + + + Common\Interop\OSX\Interop.Errors.cs + + + Common\Interop\OSX\Interop.OpenFlags.cs + + diff --git a/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj b/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj index 130f79b152d..4679c9f800e 100644 --- a/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj +++ b/src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj @@ -270,6 +270,9 @@ + + Common\Interop\Linux\Interop.Errors.cs + Common\Interop\Linux\Interop.ProcFsStat.cs @@ -279,6 +282,12 @@ Common\Interop\Linux\Interop.prioritywhich.cs + + Common\Interop\Linux\Interop.PathConfNames.cs + + + Common\Interop\Linux\Interop.SysConfNames.cs + @@ -293,6 +302,12 @@ Common\Interop\OSX\Interop.prioritywhich.cs + + Common\Interop\OSX\Interop.PathConfNames.cs + + + Common\Interop\OSX\Interop.SysConfNames.cs + diff --git a/src/libraries/System.Dynamic.Runtime/tests/System.Dynamic.Runtime.Tests.csproj b/src/libraries/System.Dynamic.Runtime/tests/System.Dynamic.Runtime.Tests.csproj index a98e4bfb659..a189ca8efd0 100644 --- a/src/libraries/System.Dynamic.Runtime/tests/System.Dynamic.Runtime.Tests.csproj +++ b/src/libraries/System.Dynamic.Runtime/tests/System.Dynamic.Runtime.Tests.csproj @@ -10,7 +10,6 @@ System.Dynamic.Runtime.Tests true 67,168,219,414,162,184,458,464,78,169,114,693,108,1981,649,109,1066,3021,3026,3002,3014,3022,660,661,429 - Linux diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj index 7e47929abc6..ad5448e81af 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System.IO.FileSystem.DriveInfo.csproj @@ -102,6 +102,9 @@ Common\Interop\Unix\Interop.setmntent.cs + + Common\Interop\Linux\Interop.Errors.cs + @@ -109,6 +112,9 @@ Common\System\NotImplemented.cs + + Common\Interop\OSX\Interop.Errors.cs + diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj b/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj index 438de0ee6be..b18e3f99f65 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System.IO.FileSystem.Watcher.csproj @@ -38,12 +38,6 @@ - - - - Common\Interop\Linux\Interop.inotify.cs - - Common\Interop\Unix\Interop.Libraries.cs @@ -64,6 +58,15 @@ Common\src\Interop\Unix\libc\Interop.gnu_get_libc_version.cs + + + + Common\Interop\Linux\Interop.inotify.cs + + + Common\Interop\Linux\Interop.Errors.cs + + @@ -75,6 +78,9 @@ Common\Interop\OSX\Interop.Libraries.cs + + Common\Interop\OSX\Interop.Errors.cs + Common\Interop\OSX\Interop.RunLoop.cs diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs index 5da470f5e36..46809219c70 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs @@ -725,21 +725,9 @@ namespace System.IO int fd = (int)_inotifyHandle.DangerousGetHandle(); Debug.Assert(fd >= 0); - // System calls may fail due to EINTR (signal interruption). We need to retry in those cases. - while (true) - { - long result = sysCall(fd); - if (result < 0) - { - int errno = Marshal.GetLastWin32Error(); - if (errno == (int)Interop.Errors.EINTR) - { - continue; - } - throw Interop.GetExceptionForIoErrno(errno, isDirectory: true); - } - return result; - } + long result; + while (Interop.CheckIo(result = sysCall(fd), isDirectory: true)) ; + return result; } finally { diff --git a/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj b/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj index 4d21420bf38..ebb72cc319a 100644 --- a/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj +++ b/src/libraries/System.IO.FileSystem/src/System.IO.FileSystem.csproj @@ -272,9 +272,6 @@ Common\Interop\Unix\Interop.opendir.cs - - Common\Interop\Unix\Interop.OpenFlags.cs - Common\Interop\Unix\Interop.lseek64.cs @@ -287,9 +284,6 @@ Common\Interop\Unix\Interop.Permissions.cs - - Common\Interop\Unix\Interop.posix_fadvise.cs - Common\Interop\Unix\Interop.read.cs @@ -317,10 +311,23 @@ + + + Common\Interop\Linux\Interop.Errors.cs + Common\Interop\Linux\Interop.readdir.cs + + Common\Interop\Linux\Interop.OpenFlags.cs + + + Common\Interop\Linux\Interop.PathConfNames.cs + + + Common\Interop\Linux\Interop.posix_fadvise.cs + @@ -328,6 +335,15 @@ Common\Interop\OSX\Interop.readdir.cs + + Common\Interop\OSX\Interop.OpenFlags.cs + + + Common\Interop\OSX\Interop.PathConfNames.cs + + + Common\Interop\OSX\Interop.Errors.cs + diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileStream.Linux.cs b/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileStream.Linux.cs new file mode 100644 index 00000000000..db22fca958f --- /dev/null +++ b/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileStream.Linux.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace System.IO +{ + /// Provides an implementation of a file stream for Unix files. + internal sealed partial class UnixFileStream : FileStreamBase + { + /// Performs additional configuration of the opened stream based on provided options. + partial void PostOpenConfigureStreamFromOptions() + { + // Support additional options after the file has been opened. + // These provide hints around how the file will be accessed. + Interop.libc.Advice fadv = + _options == FileOptions.RandomAccess ? Interop.libc.Advice.POSIX_FADV_RANDOM : + _options == FileOptions.SequentialScan ? Interop.libc.Advice.POSIX_FADV_SEQUENTIAL : + 0; + if (fadv != 0) + { + SysCall((fd, advice, _) => Interop.libc.posix_fadvise(fd, 0, 0, advice), fadv); + } + } + } +} \ No newline at end of file diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileStream.cs b/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileStream.cs index aec06a5f756..1fb8b5081bd 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileStream.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileStream.cs @@ -12,7 +12,7 @@ using Microsoft.Win32.SafeHandles; namespace System.IO { /// Provides an implementation of a file stream for Unix files. - internal sealed class UnixFileStream : FileStreamBase + internal sealed partial class UnixFileStream : FileStreamBase { /// The file descriptor wrapped in a file handle. private readonly SafeFileHandle _fileHandle; @@ -105,16 +105,8 @@ namespace System.IO throw; } - // Support additional options after the file has been opened. - // These provide hints around how the file will be accessed. - Interop.libc.Advice fadv = - _options == FileOptions.RandomAccess ? Interop.libc.Advice.POSIX_FADV_RANDOM : - _options == FileOptions.SequentialScan ? Interop.libc.Advice.POSIX_FADV_SEQUENTIAL : - 0; - if (fadv != 0) - { - SysCall((fd, advice, _) => Interop.libc.posix_fadvise(fd, 0, 0, advice), fadv); - } + // Perform additional configurations on the stream based on the provided FileOptions + PostOpenConfigureStreamFromOptions(); // Jump to the end of the file if opened as Append. if (_mode == FileMode.Append) @@ -123,6 +115,9 @@ namespace System.IO } } + /// Performs additional configuration of the opened stream based on provided options. + partial void PostOpenConfigureStreamFromOptions(); + /// Initializes a stream from an already open file handle (file descriptor). /// The handle to the file. /// Whether the file will be read, written, or both. @@ -171,7 +166,7 @@ namespace System.IO private static Interop.libc.OpenFlags PreOpenConfigurationFromOptions(FileMode mode, FileAccess access, FileOptions options) { // Translate FileMode. Most of the values map cleanly to one or more options for open. - Interop.libc.OpenFlags flags = Interop.libc.OpenFlags.O_LARGEFILE; + Interop.libc.OpenFlags flags = default(Interop.libc.OpenFlags); switch (mode) { default: @@ -218,8 +213,8 @@ namespace System.IO case FileOptions.Asynchronous: // Handled in ctor, setting _useAsync and SafeFileHandle.IsAsync to true case FileOptions.DeleteOnClose: // DeleteOnClose doesn't have a Unix equivalent, but we approximate it in Dispose case FileOptions.Encrypted: // Encrypted does not have an equivalent on Unix and is ignored. - case FileOptions.RandomAccess: // Implemented after open via posix_fadvise - case FileOptions.SequentialScan: // Implemented after open via posix_fadvise + case FileOptions.RandomAccess: // Implemented after open if posix_fadvise is available + case FileOptions.SequentialScan: // Implemented after open if posix_fadvise is available break; case FileOptions.WriteThrough: diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileSystem.cs b/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileSystem.cs index b15b4b35d88..542bd782a33 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileSystem.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/UnixFileSystem.cs @@ -294,6 +294,9 @@ namespace System.IO case Interop.Errors.EINTR: // interrupted; try again continue; case Interop.Errors.EACCES: + case Interop.Errors.EPERM: + case Interop.Errors.EROFS: + case Interop.Errors.EISDIR: throw new IOException(SR.Format(SR.UnauthorizedAccess_IODenied_Path, fullPath)); // match Win32 exception case Interop.Errors.ENOENT: if (!throwOnTopLevelDirectoryNotFound) diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/Ansichars.cs b/src/libraries/System.IO.FileSystem/tests/Directory/Ansichars.cs index ce2e3e74b37..4cd7741a83b 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/Ansichars.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/Ansichars.cs @@ -8,7 +8,7 @@ using Xunit; public class Directory_AnsiChars { - [ActiveIssue(847)] + [Fact] public static void runTest() { bool bTestPassed = false; diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str.cs index 49c1df7f7b8..816cc8cd7d6 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str.cs @@ -279,7 +279,7 @@ public class Directory_GetDirectories_str dirArr = dir2.GetDirectories("*BB*"); iCountTestcases++; - if (dirArr.Length != (Interop.IsLinux ? 1 : 2)) // Linux is case-sensitive + if (dirArr.Length != (Interop.IsWindows ? 2 : 1)) { iCountErrors++; printerr("Error_4y190! Incorrect number of directories==" + dirArr.Length); @@ -296,7 +296,7 @@ public class Directory_GetDirectories_str printerr("Error_956yb! Incorrect name==" + dirArr[0]); } - if (!Interop.IsLinux) // linux is case-sensitive + if (Interop.IsWindows) { iCountTestcases++; if (Array.IndexOf(names, "aaabbcc") < 0) diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str_str.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str_str.cs index aa4c51f8ff7..75cfaf163c2 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str_str.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/GetDirectories_str_str.cs @@ -304,7 +304,7 @@ public class Directory_Co5674GetDirectories_Str_Str strArr = Directory.GetDirectories(Path.Combine(".", dirName), "*BB*"); iCountTestcases++; - if (strArr.Length != (Interop.IsLinux ? 1 : 2)) // Linux is case-sensitive + if (strArr.Length != (Interop.IsWindows ? 2 : 1)) { iCountErrors++; printerr("Error_4y190! Incorrect number of directories==" + strArr.Length); @@ -319,7 +319,7 @@ public class Directory_Co5674GetDirectories_Str_Str iCountErrors++; printerr("Error_956yb! Incorrect name==" + strArr[0]); } - if (!Interop.IsLinux) // Linux is case-sensitive + if (Interop.IsWindows) { iCountTestcases++; if (Array.IndexOf(names, "aaabbcc") < 0) diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str_str.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str_str.cs index cb1b1737387..13fc79a01db 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str_str.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str_str.cs @@ -433,7 +433,7 @@ public class Directory_GetFileSystemEntries_str_str strArr = Directory.GetFileSystemEntries(dir2.Name, "*BB*"); iCountTestcases++; - if (strArr.Length != (Interop.IsLinux ? 1 : 2)) // Linux is case-sensitive + if (strArr.Length != (Interop.IsWindows ? 2 : 1)) { iCountErrors++; printerr("Error_4y190! Incorrect number of files==" + strArr.Length); @@ -441,7 +441,7 @@ public class Directory_GetFileSystemEntries_str_str for (int iLoop = 0; iLoop < strArr.Length; iLoop++) strArr[iLoop] = Path.GetFileName(strArr[iLoop]); - if (!Interop.IsLinux) // Linux is case-sensitive + if (Interop.IsWindows) { iCountTestcases++; if (Array.IndexOf(strArr, "aaabbcc") < 0) diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str.cs index 3c21646667e..941b8e26061 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str.cs @@ -290,7 +290,7 @@ public class Directory_GetFiles_str filArr = dir2.GetFiles("*BB*"); iCountTestcases++; - if (filArr.Length != (Interop.IsLinux ? 1 : 2)) // Linux is case-sensitive + if (filArr.Length != (Interop.IsWindows ? 2 : 1)) { iCountErrors++; printerr("Error_4y190! Incorrect number of files==" + filArr.Length); @@ -308,7 +308,7 @@ public class Directory_GetFiles_str iCountErrors++; printerr("Error_956yb! Incorrect name==" + filArr[0].Name); } - if (!Interop.IsLinux) // Linux is case-sensitive + if (Interop.IsWindows) { iCountTestcases++; if (Array.IndexOf(names, "aaabbcc") < 0) diff --git a/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str_Str.cs b/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str_Str.cs index e0723d01511..c65814a09a5 100644 --- a/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str_Str.cs +++ b/src/libraries/System.IO.FileSystem/tests/Directory/GetFiles_str_Str.cs @@ -323,7 +323,7 @@ public class Directory_GetFiles_str_str strArr = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), dirName), "*BB*"); iCountTestcases++; - if (strArr.Length != (Interop.IsLinux ? 1 : 2)) // Linux is case-sensitive + if (strArr.Length != (Interop.IsWindows ? 2 : 1)) { iCountErrors++; printerr("Error_4y190! Incorrect number of files==" + strArr.Length); @@ -338,7 +338,7 @@ public class Directory_GetFiles_str_str iCountErrors++; printerr("Error_956yb! Incorrect name==" + strArr[0].ToString()); } - if (!Interop.IsLinux) // Linux is case-sensitive + if (Interop.IsWindows) { iCountTestcases++; if (Array.IndexOf(names, "aaabbcc") < 0) diff --git a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/GetFileSystemInfos_str.cs b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/GetFileSystemInfos_str.cs index 62fb454b9e3..5c794c2b571 100644 --- a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/GetFileSystemInfos_str.cs +++ b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/GetFileSystemInfos_str.cs @@ -330,7 +330,7 @@ public class DirectoryInfo_GetFileSystemInfos_str fsArr = dir2.GetFileSystemInfos("*BB*"); iCountTestcases++; - if (fsArr.Length != (Interop.IsLinux ? 1 : 2)) // Linux is case-sensitive + if (fsArr.Length != (Interop.IsWindows ? 2 : 1)) { iCountErrors++; printerr("Error_4y190! Incorrect number of files==" + fsArr.Length); @@ -340,7 +340,7 @@ public class DirectoryInfo_GetFileSystemInfos_str foreach (FileSystemInfo fs in fsArr) names[i++] = fs.Name; - if (!Interop.IsLinux) // Linux is case-sensitive + if (Interop.IsWindows) { iCountTestcases++; diff --git a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/get_CreationTime.cs b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/get_CreationTime.cs index 6b85b4026dc..35f8170f2cb 100644 --- a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/get_CreationTime.cs +++ b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/get_CreationTime.cs @@ -83,30 +83,33 @@ public class DirectoryInfo_get_CreationTime dir.Delete(true); //See VSWhidbey # 92050 - String path = TestInfo.CurrentDirectory + Path.DirectorySeparatorChar; - String tempPath; - int count = 0; - while (true) + if (Interop.IsWindows) // for back compat, rather than throwing, Windows returns default values when the file doesn't exist { - tempPath = path + "foo_" + count++; - if (!Directory.Exists(tempPath)) - break; - } - dir = new DirectoryInfo(tempPath); - try - { - DateTime d1 = dir.CreationTime; - if (d1 != DateTime.FromFileTime(0)) + String path = TestInfo.CurrentDirectory + Path.DirectorySeparatorChar; + String tempPath; + int count = 0; + while (true) + { + tempPath = path + "foo_" + count++; + if (!Directory.Exists(tempPath)) + break; + } + dir = new DirectoryInfo(tempPath); + try + { + DateTime d1 = dir.CreationTime; + if (d1 != DateTime.FromFileTime(0)) + { + iCountErrors++; + printerr("Error_20hjx! Creation time cannot be correct"); + } + } + catch (Exception exc) { iCountErrors++; - printerr("Error_20hjx! Creation time cannot be correct"); + printerr("Error_20fhd! Unexpected exceptiont thrown: " + exc.ToString()); } } - catch (Exception exc) - { - iCountErrors++; - printerr("Error_20fhd! Unexpected exceptiont thrown: " + exc.ToString()); - } /////////////////////////////////////////////////////////////////// /////////////////////////// END TESTS ///////////////////////////// diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs index 74dedf85d7d..4ab2c671c76 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs @@ -29,7 +29,7 @@ public class FileInfo_GetSetTimes foreach(TimeProperty timeProperty in Enum.GetValues(typeof(TimeProperty))) { - if (Interop.IsLinux && timeProperty == TimeProperty.CreationTime) // birthtime not supported on Linux + if (!Interop.IsWindows && timeProperty == TimeProperty.CreationTime) // birthtime not supported on Linux { continue; } diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/get_CreationTime.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/get_CreationTime.cs index c1bff54a3aa..0ee1856d8c6 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/get_CreationTime.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/get_CreationTime.cs @@ -49,25 +49,28 @@ public class FileInfo_get_CreationTime if (File.Exists(filName)) File.Delete(filName); - DirectoryInfo newDirectory = new DirectoryInfo("."); - fil2 = new FileInfo(newDirectory + filName); - iCountTestcases++; - try + if (Interop.IsWindows) // for back compat, rather than throwing, Windows returns default values when the file doesn't exist { - DateTime d1 = fil2.CreationTime; - //We threw in Whidbey upto 50606 but V1.1 behavior is as below - if (d1 != DateTime.FromFileTime(0)) + DirectoryInfo newDirectory = new DirectoryInfo("."); + fil2 = new FileInfo(Path.Combine(newDirectory.FullName, filName)); + iCountTestcases++; + try + { + DateTime d1 = fil2.CreationTime; + //We threw in Whidbey upto 50606 but V1.1 behavior is as below + if (d1 != DateTime.FromFileTime(0)) + { + iCountErrors++; + Console.WriteLine("Error_20hjx! Creation time cannot be correct: {0}", d1); + } + } + catch (Exception exc) { iCountErrors++; - Console.WriteLine("Error_20hjx! Creation time cannot be correct: {0}", d1); + printerr("Bug 14952 - Error_20fhd! Unexpected exceptiont thrown: " + exc.ToString()); } + fil2.Delete(); } - catch (Exception exc) - { - iCountErrors++; - printerr("Bug 14952 - Error_20fhd! Unexpected exceptiont thrown: " + exc.ToString()); - } - fil2.Delete(); // [] Create file, sleep for a while and check the creation time. diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/get_LastWriteTime.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/get_LastWriteTime.cs index e8b0bddcbd4..8c2f9b27f9c 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/get_LastWriteTime.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/get_LastWriteTime.cs @@ -58,12 +58,13 @@ public class FileInfo_get_LastWriteTime Task.Delay(4000).Wait(); fil2.Refresh(); iCountTestcases++; - if ((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds < 2000 || - (DateTime.Now - fil2.LastWriteTime).TotalMilliseconds > 5000) + DateTime lastWriteTime = fil2.LastWriteTime; + if ((DateTime.Now - lastWriteTime).TotalMilliseconds < 2000 || + (DateTime.Now - lastWriteTime).TotalMilliseconds > 5000) { iCountErrors++; - Console.WriteLine(fil2.LastWriteTime); - Console.WriteLine((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds); + Console.WriteLine(lastWriteTime); + Console.WriteLine((DateTime.Now - lastWriteTime).TotalMilliseconds); printerr("Error_20hjx! Last Write Time time cannot be correct"); } @@ -76,14 +77,10 @@ public class FileInfo_get_LastWriteTime stream.Read(new Byte[1], 0, 1); stream.Dispose(); fil2.Refresh(); - Task.Delay(2000).Wait(); - iCountTestcases++; - if ((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds < 4000 || - (DateTime.Now - fil2.LastWriteTime).TotalMilliseconds > 7000) + if (fil2.LastWriteTime != lastWriteTime) { iCountErrors++; - Console.WriteLine((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds); - printerr("Eror_209x9! LastWriteTime is way off"); + printerr("Eror_209x9! LastWriteTime should not have changed due to a read"); } stream = fil2.Open(FileMode.Open); diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/Microsoft/Win32/SafeMemoryMappedFileHandle.Unix.cs b/src/libraries/System.IO.MemoryMappedFiles/src/Microsoft/Win32/SafeMemoryMappedFileHandle.Unix.cs index 3fda0edb31c..9588c70fe9c 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/src/Microsoft/Win32/SafeMemoryMappedFileHandle.Unix.cs +++ b/src/libraries/System.IO.MemoryMappedFiles/src/Microsoft/Win32/SafeMemoryMappedFileHandle.Unix.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Diagnostics; using System.IO; using System.IO.MemoryMappedFiles; using System.Runtime.InteropServices; @@ -12,11 +13,26 @@ namespace Microsoft.Win32.SafeHandles { public sealed partial class SafeMemoryMappedFileHandle : SafeHandle { + /// Indicates where the FileHandle came from, which then controls if/how it should be cleaned up. + internal enum FileStreamSource + { + Provided, + ManufacturedFile, + ManufacturedSharedMemory, + } + /// Counter used to produce a unique handle value. private static long s_counter = 0; - /// The underlying SafeFileHandle. May be null. - internal readonly SafeFileHandle _fileHandle; + /// + /// The underlying FileStream. May be null. We hold onto the stream rather than just + /// onto the underlying handle to ensure that logic associated with disposing the stream + /// (e.g. deleting the file for DeleteOnClose) happens at the appropriate time. + /// + internal readonly FileStream _fileStream; + + /// Indication as to where the file stream came from, if it exists. + internal readonly FileStreamSource _fileStreamSource; /// /// The name of the map, currently used to give internal names to anonymous, @@ -39,21 +55,23 @@ namespace Microsoft.Win32.SafeHandles /// Initializes the memory-mapped file handle. /// The name of the map; may be null. - /// The underlying file handle; may be null. + /// The underlying file stream; may be null. + /// The source of the file stream. /// The inheritability of the memory-mapped file. /// The access for the memory-mapped file. /// The options for the memory-mapped file. /// The capacity of the memory-mapped file. internal SafeMemoryMappedFileHandle( string mapName, - SafeFileHandle fileHandle, HandleInheritability inheritability, + FileStream fileStream, FileStreamSource fileStreamSource, HandleInheritability inheritability, MemoryMappedFileAccess access, MemoryMappedFileOptions options, long capacity) : base(new IntPtr(-1), ownsHandle: true) { // Store the arguments. We'll actually open the map when the view is created. _mapName = mapName; - _fileHandle = fileHandle; + _fileStream = fileStream; + _fileStreamSource = fileStreamSource; _inheritability = inheritability; _access = access; _options = options; @@ -66,19 +84,27 @@ namespace Microsoft.Win32.SafeHandles protected override void Dispose(bool disposing) { - if (disposing && _mapName != null && _fileHandle != null) + if (disposing && _fileStream != null && _fileStreamSource != FileStreamSource.Provided) { - // with a non-null name, the file handle is a shared memory object we created; no one else references it - _fileHandle.Dispose(); + // Clean up the file if we created it + _fileStream.Dispose(); } base.Dispose(disposing); } protected override unsafe bool ReleaseHandle() { - return _mapName != null ? - Interop.libc.shm_unlink(_mapName) == 0 : - true; // if no mapName, nothing to release + if (_fileStreamSource == FileStreamSource.ManufacturedSharedMemory) + { + Debug.Assert(_mapName != null); + Debug.Assert(_fileStream != null); + return Interop.libc.shm_unlink(_mapName) == 0; + } + + // For _fileHandleSource == File, there's nothing to clean up, as it's either the caller's responsibility + // or it was created as DeleteOnClose (if it was a temporary backing store). + + return true; } public override bool IsInvalid diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System.IO.MemoryMappedFiles.csproj b/src/libraries/System.IO.MemoryMappedFiles/src/System.IO.MemoryMappedFiles.csproj index 8be3eac2681..06231ec311e 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/src/System.IO.MemoryMappedFiles.csproj +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System.IO.MemoryMappedFiles.csproj @@ -129,8 +129,8 @@ Common\Interop\Unix\Interop.gnu_get_libc_version.cs - - Common\Interop\Unix\Interop.madvise.cs + + Common\Interop\Unix\Interop.MemoryMappedProtections.cs Common\Interop\Unix\Interop.mmap.cs @@ -141,9 +141,6 @@ Common\Interop\Unix\Interop.msync.cs - - Common\Interop\Unix\Interop.OpenFlags.cs - Common\Interop\Unix\Interop.Permissions.cs @@ -153,14 +150,65 @@ Common\Interop\Unix\Interop.sysconf.cs - - Common\Interop\Unix\Interop.shm_open.cs - + + + + + + Common\Interop\Linux\Interop.Errors.cs + + + Common\Interop\Linux\Interop.Libraries.cs + + + Common\Interop\Linux\Interop.MemoryMappedFlags.cs + + + Common\Interop\Linux\Interop.MemoryMappedSyncFlags.cs + + + Common\Interop\Linux\Interop.madvise.cs + + + Common\Interop\Linux\Interop.OpenFlags.cs + + + Common\Interop\Linux\Interop.shm_open.cs + + + Common\Interop\Linux\Interop.SysConfNames.cs + + + + + + + Common\Interop\OSX\Interop.Errors.cs + + + Common\Interop\OSX\Interop.Libraries.cs + + + Common\Interop\OSX\Interop.MemoryMappedFlags.cs + + + Common\Interop\OSX\Interop.MemoryMappedSyncFlags.cs + + + Common\Interop\OSX\Interop.OpenFlags.cs + + + Common\Interop\OSX\Interop.SysConfNames.cs + + + Common\Interop\OSX\Interop.shm_open.cs + + diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Linux.cs b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Linux.cs new file mode 100644 index 00000000000..da46ecf4710 --- /dev/null +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Linux.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.Win32.SafeHandles; + +namespace System.IO.MemoryMappedFiles +{ + public partial class MemoryMappedFile + { + private static FileStream CreateSharedBackingObject( + Interop.libc.MemoryMappedProtections protections, long capacity, + out string mapName, out SafeMemoryMappedFileHandle.FileStreamSource fileStreamSource) + { + // The POSIX shared memory object name must begin with '/'. After that we just want something short and unique. + mapName = "/" + MemoryMapObjectFilePrefix + Guid.NewGuid().ToString("N"); + fileStreamSource = SafeMemoryMappedFileHandle.FileStreamSource.ManufacturedSharedMemory; + + // Determine the flags to use when creating the shared memory object + Interop.libc.OpenFlags flags = (protections & Interop.libc.MemoryMappedProtections.PROT_WRITE) != 0 ? + Interop.libc.OpenFlags.O_RDWR : + Interop.libc.OpenFlags.O_RDONLY; + flags |= Interop.libc.OpenFlags.O_CREAT | Interop.libc.OpenFlags.O_EXCL; // CreateNew + + // Determine the permissions with which to create the file + Interop.libc.Permissions perms = default(Interop.libc.Permissions); + if ((protections & Interop.libc.MemoryMappedProtections.PROT_READ) != 0) + perms |= Interop.libc.Permissions.S_IRUSR; + if ((protections & Interop.libc.MemoryMappedProtections.PROT_WRITE) != 0) + perms |= Interop.libc.Permissions.S_IWUSR; + if ((protections & Interop.libc.MemoryMappedProtections.PROT_EXEC) != 0) + perms |= Interop.libc.Permissions.S_IXUSR; + + // Create the shared memory object. Then enlarge it to the requested capacity. + int fd; + Interop.CheckIo(fd = Interop.libc.shm_open(mapName, flags, (int)perms), mapName); + SafeFileHandle fileHandle = new SafeFileHandle((IntPtr)fd, ownsHandle: true); + + // Wrap the handle in a stream and return it. + var fs = new FileStream(fileHandle, TranslateProtectionsToFileAccess(protections)); + fs.SetLength(capacity); + return fs; + } + } +} diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.OSX.cs b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.OSX.cs new file mode 100644 index 00000000000..930a5530c69 --- /dev/null +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.OSX.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.Win32.SafeHandles; + +namespace System.IO.MemoryMappedFiles +{ + public partial class MemoryMappedFile + { + private static FileStream CreateSharedBackingObject( + Interop.libc.MemoryMappedProtections protections, long capacity, + out string mapName, out SafeMemoryMappedFileHandle.FileStreamSource fileHandleSource) + { + mapName = MemoryMapObjectFilePrefix + Guid.NewGuid().ToString("N") + ".tmp"; + fileHandleSource = SafeMemoryMappedFileHandle.FileStreamSource.ManufacturedFile; + + FileAccess access = + (protections & (Interop.libc.MemoryMappedProtections.PROT_READ | Interop.libc.MemoryMappedProtections.PROT_WRITE)) != 0 ? FileAccess.ReadWrite : + (protections & (Interop.libc.MemoryMappedProtections.PROT_WRITE)) != 0 ? FileAccess.Write : + FileAccess.Read; + + var fs = new FileStream(Path.Combine(Path.GetTempPath(), mapName), + FileMode.CreateNew, TranslateProtectionsToFileAccess(protections), FileShare.ReadWrite, + 0x1000, FileOptions.DeleteOnClose); + fs.SetLength(capacity); + return fs; + } + } +} diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Unix.cs b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Unix.cs index 19300abdbb7..ef7664039a2 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Unix.cs +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Unix.cs @@ -29,12 +29,11 @@ namespace System.IO.MemoryMappedFiles throw CreateNamedMapsNotSupportedException(); } - SafeFileHandle fileHandle = null; + var fileStreamSource = SafeMemoryMappedFileHandle.FileStreamSource.Provided; if (fileStream != null) { // This map is backed by a file. Make sure the file's size is increased to be // at least as big as the requested capacity of the map. - fileHandle = fileStream.SafeFileHandle; if (fileStream.Length < capacity) { fileStream.SetLength(capacity); @@ -45,22 +44,21 @@ namespace System.IO.MemoryMappedFiles // This map is backed by memory-only. With files, multiple views over the same map // will end up being able to share data through the same file-based backing store; // for anonymous maps, we need a similar backing store, or else multiple views would logically - // each be their own map and wouldn't share any data. To achieve this, we create a POSIX shared - // memory object and use its file descriptor as the file handle. However, we only do this when the - // permission is more than read-only. We can't ftruncate to increase the size of a shared memory - // object that has read-only permissions, but we also don't need to worry about sharing + // each be their own map and wouldn't share any data. To achieve this, we create a backing object + // (either memory or on disk, depending on the system) and use its file descriptor as the file handle. + // However, we only do this when the permission is more than read-only. We can't change the size + // of an object that has read-only permissions, but we also don't need to worry about sharing // views over a read-only, anonymous, memory-backed map, because the data will never change, so all views // will always see zero and can't change that. In that case, we just use the built-in anonymous support of // the map by leaving fileHandle as null. Interop.libc.MemoryMappedProtections protections = MemoryMappedView.GetProtections(access, forVerification: false); if ((protections & Interop.libc.MemoryMappedProtections.PROT_WRITE) != 0 && capacity > 0) { - mapName = "/AnonCoreFxMemMap_" + Guid.NewGuid().ToString("N"); // unique name must start with "/" and be < NAME_MAX length - fileHandle = CreateNewSharedMemoryObject(mapName, protections, capacity); + fileStream = CreateSharedBackingObject(protections, capacity, out mapName, out fileStreamSource); } } - return new SafeMemoryMappedFileHandle(mapName, fileHandle, inheritability, access, options, capacity); + return new SafeMemoryMappedFileHandle(mapName, fileStream, fileStreamSource, inheritability, access, options, capacity); } /// @@ -111,37 +109,14 @@ namespace System.IO.MemoryMappedFiles return new PlatformNotSupportedException(SR.PlatformNotSupported_NamedMaps); } - private static SafeFileHandle CreateNewSharedMemoryObject( - string mapName, Interop.libc.MemoryMappedProtections protections, long capacity) + private const string MemoryMapObjectFilePrefix = "CoreFX_MMF_"; + + private static FileAccess TranslateProtectionsToFileAccess(Interop.libc.MemoryMappedProtections protections) { - // Determine the flags to use when creating the shared memory object - Interop.libc.OpenFlags flags = (protections & Interop.libc.MemoryMappedProtections.PROT_WRITE) != 0 ? - Interop.libc.OpenFlags.O_RDWR : - Interop.libc.OpenFlags.O_RDONLY; - flags |= Interop.libc.OpenFlags.O_CREAT | Interop.libc.OpenFlags.O_EXCL; // CreateNew - - // Create the shared memory object - int fd; - Interop.CheckIo(fd = Interop.libc.shm_open(mapName, flags, (int)Interop.libc.Permissions.S_IRWXU), mapName); - SafeFileHandle fileHandle = new SafeFileHandle((IntPtr)fd, ownsHandle: true); - - // Then enlarge it to the requested capacity - bool gotFd = false; - fileHandle.DangerousAddRef(ref gotFd); - try - { - while (Interop.CheckIo(Interop.libc.ftruncate((int)fileHandle.DangerousGetHandle(), capacity), mapName)) ; - } - finally - { - if (gotFd) - { - fileHandle.DangerousRelease(); - } - } - - // Return the fd for the object - return fileHandle; + return + (protections & (Interop.libc.MemoryMappedProtections.PROT_READ | Interop.libc.MemoryMappedProtections.PROT_WRITE)) != 0 ? FileAccess.ReadWrite : + (protections & (Interop.libc.MemoryMappedProtections.PROT_WRITE)) != 0 ? FileAccess.Write : + FileAccess.Read; } } diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Linux.cs b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Linux.cs new file mode 100644 index 00000000000..2e2d0e2d492 --- /dev/null +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Linux.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Diagnostics; + +namespace System.IO.MemoryMappedFiles +{ + internal partial class MemoryMappedView + { + /// Attempt to prevent the specified pages from being copied into forked processes. + /// The starting address. + /// The length. + static partial void DisableForkingIfPossible(IntPtr addr, IntPtr length) + { + if ((long)length > 0) + { + int adviseResult = Interop.libc.madvise(addr, length, Interop.libc.MemoryMappedAdvice.MADV_DONTFORK); + Debug.Assert(adviseResult == 0); // In release, ignore failures from advise; it's just a hint, anyway. + } + } + } +} \ No newline at end of file diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Unix.cs b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Unix.cs index 2263d81525c..256ec07e442 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Unix.cs +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Unix.cs @@ -47,10 +47,6 @@ namespace System.IO.MemoryMappedFiles ValidateSizeAndOffset( requestedSize, requestedOffset, pageSize, out nativeSize, out extraMemNeeded, out nativeOffset); - if (nativeSize == 0) - { - nativeSize = (ulong)pageSize; - } bool gotRefOnHandle = false; try @@ -64,12 +60,12 @@ namespace System.IO.MemoryMappedFiles // If we have a file handle, get the file descriptor from it. If the handle is null, // we'll use an anonymous backing store for the map. int fd; - if (memMappedFileHandle._fileHandle != null) + if (memMappedFileHandle._fileStream != null) { // Get the file descriptor from the SafeFileHandle - memMappedFileHandle._fileHandle.DangerousAddRef(ref gotRefOnHandle); + memMappedFileHandle._fileStream.SafeFileHandle.DangerousAddRef(ref gotRefOnHandle); Debug.Assert(gotRefOnHandle); - fd = (int)memMappedFileHandle._fileHandle.DangerousGetHandle(); + fd = (int)memMappedFileHandle._fileStream.SafeFileHandle.DangerousGetHandle(); Debug.Assert(fd >= 0); } else @@ -90,14 +86,36 @@ namespace System.IO.MemoryMappedFiles throw new UnauthorizedAccessException(); } + // viewProtections is strictly less than mapProtections, so use viewProtections for actually creating the map. + Interop.libc.MemoryMappedProtections viewProtForCreation = GetProtections(access, forVerification: false); + // Create the map - IntPtr addr = Interop.libc.mmap( - IntPtr.Zero, // don't specify an address; let the system choose one - (IntPtr)nativeSize, // specify the rounded-size we computed so as to page align; size + extraMemNeeded - GetProtections(access, forVerification: false), // viewProtections is strictly less than mapProtections, so use viewProtections - flags, - fd, // mmap adds a ref count to the fd, so there's no need to dup it. - (long)nativeOffset); // specify the rounded-offset we computed so as to page align; offset - extraMemNeeded + IntPtr addr = IntPtr.Zero; + if (nativeSize > 0) + { + addr = Interop.libc.mmap( + IntPtr.Zero, // don't specify an address; let the system choose one + (IntPtr)nativeSize, // specify the rounded-size we computed so as to page align; size + extraMemNeeded + viewProtForCreation, + flags, + fd, // mmap adds a ref count to the fd, so there's no need to dup it. + (long)nativeOffset); // specify the rounded-offset we computed so as to page align; offset - extraMemNeeded + } + else + { + // There are some corner cases where the .NET API allows the requested size to be zero, e.g. the caller is + // creating a map at the end of the capacity. We can't pass 0 to mmap, as that'll fail with EINVAL, nor can + // we create a map that extends beyond the end of the underlying file, as that'll fail on some platforms at the + // time of the map's creation. Instead, since there's no data to be read/written, it doesn't actually matter + // what backs the view, so we just create an anonymous mapping. + addr = Interop.libc.mmap( + IntPtr.Zero, + (IntPtr)1, // any length that's greater than zero will suffice + viewProtForCreation, + flags | Interop.libc.MemoryMappedFlags.MAP_ANONYMOUS, + -1, // ignore the actual fd even if there was one + 0); + } if ((long)addr < 0) { throw Interop.GetExceptionForIoErrno(Marshal.GetLastWin32Error()); @@ -107,8 +125,7 @@ namespace System.IO.MemoryMappedFiles // from being inherited by a forked process if (memMappedFileHandle._inheritability == HandleInheritability.None) { - int adviseResult = Interop.libc.madvise(addr, (IntPtr)nativeSize, Interop.libc.MemoryMappedAdvice.MADV_DONTFORK); - Debug.Assert(adviseResult == 0); // In release, ignore failures from advise; it's just a hint, anyway. + DisableForkingIfPossible(addr, (IntPtr)nativeSize); } // Create and return the view handle @@ -124,7 +141,7 @@ namespace System.IO.MemoryMappedFiles { if (gotRefOnHandle) { - memMappedFileHandle._fileHandle.DangerousRelease(); + memMappedFileHandle._fileStream.SafeFileHandle.DangerousRelease(); } } } @@ -132,6 +149,9 @@ namespace System.IO.MemoryMappedFiles [SecurityCritical] public unsafe void Flush(UIntPtr capacity) { + if (capacity == UIntPtr.Zero) + return; + byte* ptr = null; try { @@ -157,6 +177,11 @@ namespace System.IO.MemoryMappedFiles // ---- PAL layer ends here ---- // ----------------------------- + /// Attempt to prevent the specified pages from being copied into forked processes. + /// The starting address. + /// The length. + static partial void DisableForkingIfPossible(IntPtr addr, IntPtr length); + /// /// The Windows implementation limits maps to the size of the logical address space. /// We use the same value here. diff --git a/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj b/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj index 6bfc2bed0f2..cc891e14142 100644 --- a/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj +++ b/src/libraries/System.IO.Pipes/src/System.IO.Pipes.csproj @@ -182,9 +182,6 @@ Common\Interop\Unix\Interop.open.cs - - Common\Interop\Unix\Interop.OpenFlags.cs - Common\Interop\Unix\Interop.Permissions.cs @@ -207,6 +204,26 @@ Common\Interop\Unix\Interop.GetInformation.cs + + + + + Common\Interop\Linux\Interop.Errors.cs + + + Common\Interop\Linux\Interop.OpenFlags.cs + + + + + + + Common\Interop\OSX\Interop.Errors.cs + + + Common\Interop\OSX\Interop.OpenFlags.cs + + diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.Linux.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.Linux.cs new file mode 100644 index 00000000000..5f283d6a5e8 --- /dev/null +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.Linux.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace System.IO.Pipes +{ + public sealed partial class AnonymousPipeServerStream + { + private static unsafe void CreatePipe(HandleInheritability inheritability, int* fdsptr) + { + int flags = (inheritability & HandleInheritability.Inheritable) == 0 ? + (int)Interop.libc.OpenFlags.O_CLOEXEC : 0; + while (Interop.CheckIo(Interop.libc.pipe2(fdsptr, flags))) ; + } + + } +} diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.OSX.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.OSX.cs new file mode 100644 index 00000000000..d97b8bef5a1 --- /dev/null +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.OSX.cs @@ -0,0 +1,14 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace System.IO.Pipes +{ + public sealed partial class AnonymousPipeServerStream + { + private static unsafe void CreatePipe(HandleInheritability inheritability, int* fdsptr) + { + // Ignore inheritability, as on OSX pipe2 isn't available + while (Interop.CheckIo(Interop.libc.pipe(fdsptr))) ; + } + } +} diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.Unix.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.Unix.cs index e8fc5e457b3..7ea73d11bdd 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.Unix.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/AnonymousPipeServerStream.Unix.cs @@ -25,27 +25,7 @@ namespace System.IO.Pipes { fixed (int* fdsptr = fds) { - bool created = false; - - // If the caller asked for the handle to be non-inheritable, try to use pipe2 to create the pipe. - // Depending on the OS, it may not exist. - if ((inheritability & HandleInheritability.Inheritable) == 0) - { - try - { - while (Interop.CheckIo(Interop.libc.pipe2(fdsptr, (int)Interop.libc.OpenFlags.O_CLOEXEC))) ; - created = true; - } - catch (MissingMethodException) { } // pipe2 is Linux only - } - - // Fall back to using pipe if either the handle can be inherited or if pipe2 wasn't available to - // create it as non-inheritable. We don't just want to fail if we can't make the handle - // non-inheritable as non-inheritance is the default. - if (!created) - { - while (Interop.CheckIo(Interop.libc.pipe(fdsptr))) ; - } + CreatePipe(inheritability, fdsptr); } } diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs index 1cacece46fc..bde380506c8 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs @@ -287,24 +287,9 @@ namespace System.IO.Pipes int fd = (int)handle.DangerousGetHandle(); Debug.Assert(fd >= 0); - // System calls may fail due to EINTR (signal interruption). We need to retry in those cases. - while (true) - { - long result = sysCall(fd, arg1, arg2); - if (result < 0) - { - int errno = Marshal.GetLastWin32Error(); - if (errno == Interop.Errors.EINTR) - { - continue; - } - else - { - throw Interop.GetExceptionForIoErrno(errno); - } - } - return result; - } + long result; + while (Interop.CheckIo(result = sysCall(fd, arg1, arg2))) ; + return result; } finally { diff --git a/src/libraries/System.Security.SecureString/src/System.Security.SecureString.csproj b/src/libraries/System.Security.SecureString/src/System.Security.SecureString.csproj index 457fe4612f5..197093e0515 100644 --- a/src/libraries/System.Security.SecureString/src/System.Security.SecureString.csproj +++ b/src/libraries/System.Security.SecureString/src/System.Security.SecureString.csproj @@ -59,6 +59,9 @@ Common\Interop\Unix\Interop.gnu_get_libc_version.cs + + Common\Interop\Unix\Interop.MemoryMappedProtections.cs + Common\Interop\Unix\Interop.mlock.cs @@ -78,6 +81,22 @@ Common\Interop\Unix\Interop.sysconf.cs + + + Common\Interop\Linux\Interop.MemoryMappedFlags.cs + + + Common\Interop\Linux\Interop.SysConfNames.cs + + + + + Common\Interop\OSX\Interop.MemoryMappedFlags.cs + + + Common\Interop\OSX\Interop.SysConfNames.cs + +