Skip to content

Conversation

@MattKotsenas
Copy link
Member

@MattKotsenas MattKotsenas commented Jan 5, 2022

Refactor the DllImports in Microsoft.Extensions.Hosting.Systemd into
Common/src/Interop in order to adhere to the interop guidelines.

This fixes part but not all of #28035

Refactor the DllImports in Microsoft.Extensions.Hosting.Systemd into
Common/src/Interop following the interop guidelines.
@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Jan 5, 2022
@ghost
Copy link

ghost commented Jan 5, 2022

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@MattKotsenas
Copy link
Member Author

I see that in commit 27bc21d, @eerhardt shimmed calls to libc. If that's appropriate in this case as well, please let me know and I'll look into doing that as well. Thanks!

@ghost
Copy link

ghost commented Jan 6, 2022

Tagging subscribers to this area: @dotnet/area-extensions-hosting
See info in area-owners.md if you want to be subscribed.

Issue Details

Refactor the DllImports in Microsoft.Extensions.Hosting.Systemd into
Common/src/Interop in order to adhere to the interop guidelines.

This fixes part but not all of #28035

Author: MattKotsenas
Assignees: -
Labels:

area-Extensions-Hosting, community-contribution

Milestone: -

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM

@tarekgh tarekgh added this to the 7.0.0 milestone Jan 6, 2022
@MattKotsenas
Copy link
Member Author

Thanks! I don't have merge permissions, so assuming everything passes, would someone be able to complete the PR on my behalf? Thanks!

@tarekgh
Copy link
Member

tarekgh commented Jan 6, 2022

@MattKotsenas yes, we'll merge it when we get green CI. @eerhardt let me know if you have any comment about this change.

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.

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@tarekgh tarekgh merged commit 0b7bef0 into dotnet:main Jan 6, 2022
@MattKotsenas MattKotsenas deleted the refactor/28035-Microsoft.Extensions.Hosting.Systemd branch January 6, 2022 20:54
MattKotsenas added a commit to MattKotsenas/runtime that referenced this pull request Jan 6, 2022
The discussion in PR dotnet#63421 clarified that System.Native shims for UNIX
APIs aren't appropriate for assemblies that don't ship as part of the
Microsoft.NETCore.App framework.

Updating the interop guidelines to capture that clarification.
stephentoub added a commit that referenced this pull request Jan 7, 2022
* Clarify P/Invoke shims guidance for OOB assemblies

The discussion in PR #63421 clarified that System.Native shims for UNIX
APIs aren't appropriate for assemblies that don't ship as part of the
Microsoft.NETCore.App framework.

Updating the interop guidelines to capture that clarification.

* Update docs/coding-guidelines/interop-guidelines.md

Co-authored-by: Stephen Toub <[email protected]>

Co-authored-by: Stephen Toub <[email protected]>
@ghost ghost locked as resolved and limited conversation to collaborators Feb 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Extensions-Hosting community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants