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
Replace IOMainPort/IOMasterPort() and kIOMainPortDefault/kIOMasterPortDefault with NULL #1333
Conversation
…tDefault with NULL
Indeed, here's another project fixing the same deprecation using NULL libusb/hidapi#406 Looks good to me, though I have no way to test this without any darwin host and I'm not sure how much automatic tests cover this code. |
Thank you for this awesome PR. It works great, and I agree to change NULL, but I got this warning when I build. Is there any idea to resolve this warning? Thank you!
|
I don't get that error on an Intel Mac running macOS 10.15, but yes, In order to affirm it works, I made a small test program and ran it on an Intel Mac with macOS 10.15. It compiles and runs the same with this patch, without this patch, and with the patch changed to use package main
import (
"fmt"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
)
func main() {
fmt.Println(host.SensorsTemperatures())
fmt.Println(disk.IOCounters())
}
|
Sorry, I have not write my environment. I run with M1-Mac macOS 12.0.1 on scaleway.com.
I have confirmed changed to 0 like this works without warnings. Awesome!
So could you update this PR ? I will merge it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution!
The code introduced in #1191 needlessly breaks backwards compatibility with macOS 10.15 and macOS 11, when code is compiled on a macOS 12 host.
Code built on macOS 12 results in binaries that fail because there is no
_kIOMainPortDefault
symbol in macOS 10 or macOS 11.It is more useful if the code is OS version invariant.
Let's look at the documentation of the function being called,
IOServiceGetMatchingServices()
:(Yes, Apple hastily and only partially renamed this parameter, but it's a key core value of Apple that they needlessly break backwards compatibility)
We do not need to call IOMasterPort() or IOMainPort(). We can supply kIOMasterPortDefault or kIOMainPortDefault and it will get the default port for us.
Furthemore, note the kIOMasterPortDefault / kIOMainPortDefault discussion, which makes it clear that it is merely a synonym for NULL. You can use NULL as an alternative:
So this PR replaces all calls to either function and constant with the documented and acceptable alternative of passing NULL (cast to mach_port_t) as the first parameter to IOServiceGetMatchingServices()