Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProcessInfo implementations for other platforms #559

Merged
merged 11 commits into from Apr 29, 2024

Conversation

jmschonfeld
Copy link
Contributor

This adds the implementations of ProcessInfo APIs for other platforms such as Windows and WASI (copied from swift-corelibs-foundation) so that this type can fully replace the ProcessInfo present in swift-corelibs-foundation

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test Windows

Sources/FoundationEssentials/Platform.swift Outdated Show resolved Hide resolved
return ""
#elseif os(Windows)
return withUnsafeTemporaryAllocation(of: wchar_t.self, capacity: 1040) { usernameBuffer in
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this meant to be the user that the process is running as or whatever user is logged in? I wonder if we should query the information from the process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe it should be the "current user" (i.e. whichever user is logged in) based on what I've heard from @iCharlesHu - this code was mostly just copied from swift-corelibs-foundation as-is so I think it should behave the same as what SCF currently does

Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift Outdated Show resolved Hide resolved
Sources/FoundationEssentials/ProcessInfo/ProcessInfo.swift Outdated Show resolved Hide resolved
@@ -117,7 +117,11 @@ final class _ProcessInfo: Sendable {
}

var processIdentifier: Int32 {
return getpid()
#if os(Windows)
return Int32(GetProcessId(GetCurrentProcess()))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is unsafe isn't it? If the high bit is set, this will trap?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it will trap if high bits are set. Is it possible for Windows to return a process ID with high bits set? (on Darwin pid_t is defined as a 32-bit signed integer). If so, is there a behavior here that you think would be better? (I believe in SCF it will trap today if the high bits are set)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, it is. The ProcessId is a DWORD (UInt32). I guess we could do a Int32(bitPattern:) initializer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but that would be a behavioral change from SCF today. Do you think that change / this new behavior would be expected for Windows clients of Foundation?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that it would be more expected than a trap which would not make sense - I didn't create an Int32, why is this trapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I can update that to use a bit pattern then if you feel that'd be the better, more expected behavior for our windows clients

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@compnerd
Copy link
Collaborator

BTW, I do have some changes related to this area that I've put up as a draft at #561 I'll see if I can pull them over here as your change is more comprehensive.

@jmschonfeld
Copy link
Contributor Author

BTW, I do have some changes related to this area that I've put up as a draft at #561 I'll see if I can pull them over here as your change is more comprehensive.

Sounds good! I accepted your two changes above and the other changes make sense to pull in - let me know if you want me to pull them in myself or if you're planning to post suggestions to do it

@compnerd
Copy link
Collaborator

BTW, I do have some changes related to this area that I've put up as a draft at #561 I'll see if I can pull them over here as your change is more comprehensive.

Sounds good! I accepted your two changes above and the other changes make sense to pull in - let me know if you want me to pull them in myself or if you're planning to post suggestions to do it

If you could pull them in, that would be wonderful!

@jmschonfeld
Copy link
Contributor Author

BTW, I do have some changes related to this area that I've put up as a draft at #561 I'll see if I can pull them over here as your change is more comprehensive.

Sounds good! I accepted your two changes above and the other changes make sense to pull in - let me know if you want me to pull them in myself or if you're planning to post suggestions to do it

If you could pull them in, that would be wonderful!

Done! I think all of the changes from that draft PR should now be combined into this one

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test Windows

Copy link
Collaborator

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

The windows side looks good to me!

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test Windows

Copy link
Contributor

@iCharlesHu iCharlesHu left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the tests!!

@iCharlesHu
Copy link
Contributor

@swift-ci please test Windows

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld
Copy link
Contributor Author

@swift-ci please test

@jmschonfeld jmschonfeld merged commit ad9d6d4 into apple:main Apr 29, 2024
2 checks passed
@jmschonfeld jmschonfeld deleted the process-info-other-platforms branch May 7, 2024 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants