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
2 changes: 2 additions & 0 deletions src/libraries/Common/src/Interop/Unix/Interop.Libraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ internal static partial class Interop
{
internal static partial class Libraries
{
internal const string Libc = "libc";

// Shims
internal const string SystemNative = "libSystem.Native";
internal const string NetSecurityNative = "libSystem.Net.Security.Native";
Expand Down
13 changes: 13 additions & 0 deletions src/libraries/Common/src/Interop/Unix/libc/Interop.GetParentPid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class libc
{
[GeneratedDllImport(Libraries.Libc, EntryPoint = "getppid")]
internal static partial int GetParentPid();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add this file next to

[GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPid")]
internal static partial int GetPid();
and export SystemNative_GetParentPid symbol from System.Native (see: https://grep.app/search?q=SystemNative_GetPid&regexp=true&filter[repo][0]=dotnet/runtime). If I remember it correctly, we intentionally try not to import symbol from libc directly (except for tests). getppid was indeed an exceptional case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try not to use SystemNative_* APIs from out-of-box assemblies like the Microsoft.Extensions.* assemblies as they're considered private APIs for the Microsoft.NETCore.App framework.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that libc was once removed from Interop.Libraries.cs 27bc21d#diff-c1db2d15cd6bf99da7a388eca0ae209ff70f2708d8c4ee6f10d11ad320bdb917, not sure if it makes sense to add it back for OOTB assemblies. (e.g. System.Drawing interop code is still maintained under its own hierarchy)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try not to use SystemNative_* APIs from out-of-box assemblies like the Microsoft.Extensions.* assemblies as they're considered private APIs for the Microsoft.NETCore.App framework.

It is even more of a stronger statement than "try not to". We explicitly can't. This is because the Microsoft.Extensions.* assemblies target netfx where the System.Native shim isn't present. (Note the netfx assembly would also be used on traditional Mono). And also new Extension assemblies can run on older .NET Core versions. In that case the System.Native shim will be present, but it won't have the new shim method.

So for assemblies that aren't in the Microsoft.NETCore.App shared framework, we can't use the System.Native shims.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<Compile Remove="SystemdLifetime.netstandard.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(CommonPath)Interop\Unix\libc\Interop.GetParentPid.cs" Link="Common\Interop\Unix\libc\Interop.GetParentPid.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs" Link="Common\Interop\Unix\Interop.Libraries.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Hosting\src\Microsoft.Extensions.Hosting.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Microsoft.Extensions.Hosting.Systemd
Expand Down Expand Up @@ -34,7 +33,7 @@ private static bool CheckParentIsSystemd()
try
{
// Check whether our direct parent is 'systemd'.
int parentPid = GetParentPid();
int parentPid = Interop.libc.GetParentPid();
string ppidString = parentPid.ToString(NumberFormatInfo.InvariantInfo);
byte[] comm = File.ReadAllBytes("/proc/" + ppidString + "/comm");
return comm.AsSpan().SequenceEqual(Encoding.ASCII.GetBytes("systemd\n"));
Expand All @@ -45,8 +44,5 @@ private static bool CheckParentIsSystemd()

return false;
}

[GeneratedDllImport("libc", EntryPoint = "getppid")]
private static partial int GetParentPid();
}
}