Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comment thread
adamsitnik marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.RemoteExecutor;
using Microsoft.DotNet.XUnitExtensions;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;

namespace System.IO.Tests
{
[PlatformSpecific(TestPlatforms.AnyUnix)]
public class SafeFileHandle_GetFileType_Unix : FileSystemTest
public partial class SafeFileHandle_GetFileType_Unix : FileSystemTest
{
[Fact]
public void GetFileType_Directory()
Expand Down Expand Up @@ -94,5 +96,53 @@ public void GetFileType_BlockDevice()
throw new SkipTestException("Insufficient privileges to open block device");
}
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[PlatformSpecific(TestPlatforms.AnyUnix)]
public void WriteCanUseFileDescriptorAboveCurrentLimit()
{
RemoteExecutor.Invoke(() =>
{
const ulong MaximumFileDescriptorLimit = 4_096;

Assert.Equal(0, Interop.Sys.GetRLimit(Interop.Sys.RlimitResources.RLIMIT_NOFILE, out Interop.Sys.RLimit limits));

limits.CurrentLimit = Math.Min(limits.CurrentLimit, MaximumFileDescriptorLimit);
Assert.InRange(limits.CurrentLimit, 2UL, (ulong)int.MaxValue);
Assert.Equal(0, Interop.Sys.SetRLimit(Interop.Sys.RlimitResources.RLIMIT_NOFILE, ref limits));

int maxAllowedFileDescriptor = checked((int)limits.CurrentLimit - 1);
string filePath = Path.GetTempFileName();

try
{
using (SafeFileHandle fileHandle = File.OpenHandle(filePath, FileMode.Open, FileAccess.ReadWrite))
{
Assert.Equal(maxAllowedFileDescriptor, dup2((int)fileHandle.DangerousGetHandle(), maxAllowedFileDescriptor));

limits.CurrentLimit--;
Assert.Equal(0, Interop.Sys.SetRLimit(Interop.Sys.RlimitResources.RLIMIT_NOFILE, ref limits));

using (SafeFileHandle duplicatedHandle = new(maxAllowedFileDescriptor, ownsHandle: true))
{
RandomAccess.Write(duplicatedHandle, new byte[] { 1 }, fileOffset: 0);
}

byte[] buffer = new byte[1];
Assert.Equal(1, RandomAccess.Read(fileHandle, buffer, fileOffset: 0));
Assert.Equal(new byte[] { 1 }, buffer);
}
}
finally
{
File.Delete(filePath);
}

return RemoteExecutor.SuccessExitCode;
}).Dispose();
}

[LibraryImport("libc", SetLastError = true)]
private static partial int dup2(int oldFileDescriptor, int newFileDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'unix'">
<Compile Include="FileSystemTest.Unix.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs" Link="Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.ResourceLimits.cs"
Link="Common\Interop\Unix\Interop.ResourceLimits.cs" />
<Compile Include="FileStream\ctor_options.Unix.cs" />
<Compile Include="Directory\CreateDirectory_UnixFileMode.Unix.cs" />
<Compile Include="SafeFileHandle\GetFileType.Unix.cs" />
Expand Down
3 changes: 2 additions & 1 deletion src/native/libs/Common/pal_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ inline static int ToFileDescriptorUnchecked(intptr_t fd)
inline static int ToFileDescriptor(intptr_t fd)
{
#ifndef TARGET_WASI // the valid range of file descriptors is probably INT32_MIN <= fd && fd <= INT32_MAX, the negative handles are valid for console.
assert(0 <= fd && fd < sysconf(_SC_OPEN_MAX));
assert(0 <= fd);
#endif
Comment thread
adamsitnik marked this conversation as resolved.
Comment thread
adamsitnik marked this conversation as resolved.
assert(fd <= INT_MAX);

return ToFileDescriptorUnchecked(fd);
}
Expand Down
Loading