|
1 | 1 | /* |
2 | | - * Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
23 | 23 | * questions. |
24 | 24 | */ |
25 | 25 |
|
| 26 | +#include <stdbool.h> |
26 | 27 | #include <sys/socket.h> |
27 | 28 | #include <netinet/in.h> |
28 | 29 | #include <arpa/inet.h> |
@@ -229,33 +230,50 @@ void setOSNameAndVersion(java_props_t *sprops) { |
229 | 230 | NSString *nsVerStr = NULL; |
230 | 231 | char* osVersionCStr = NULL; |
231 | 232 | NSOperatingSystemVersion osVer = [[NSProcessInfo processInfo] operatingSystemVersion]; |
232 | | - // Copy out the char* if running on version other than 10.16 Mac OS (10.16 == 11.x) |
233 | | - // or explicitly requesting version compatibility |
234 | | - if (!((long)osVer.majorVersion == 10 && (long)osVer.minorVersion >= 16) || |
235 | | - (getenv("SYSTEM_VERSION_COMPAT") != NULL)) { |
236 | | - if (osVer.patchVersion == 0) { // Omit trailing ".0" |
| 233 | + // Some macOS versions require special handling. For example, |
| 234 | + // when the NSOperatingSystemVersion reports 10.16 as the version |
| 235 | + // then it should be treated as 11. Similarly, when it reports 16.0 |
| 236 | + // as the version then it should be treated as 26. |
| 237 | + // If the SYSTEM_VERSION_COMPAT environment variable (a macOS construct) |
| 238 | + // is set to 1, then we don't do any special handling for any versions |
| 239 | + // and just literally use the value that NSOperatingSystemVersion reports. |
| 240 | + const char* envVal = getenv("SYSTEM_VERSION_COMPAT"); |
| 241 | + const bool versionCompatEnabled = envVal != NULL |
| 242 | + && strncmp(envVal, "1", 1) == 0; |
| 243 | + const bool requiresSpecialHandling = |
| 244 | + ((long) osVer.majorVersion == 10 && (long) osVer.minorVersion >= 16) |
| 245 | + || ((long) osVer.majorVersion == 16 && (long) osVer.minorVersion >= 0); |
| 246 | + if (!requiresSpecialHandling || versionCompatEnabled) { |
| 247 | + // no special handling - just use the version reported |
| 248 | + // by NSOperatingSystemVersion |
| 249 | + if (osVer.patchVersion == 0) { |
| 250 | + // Omit trailing ".0" |
237 | 251 | nsVerStr = [NSString stringWithFormat:@"%ld.%ld", |
238 | 252 | (long)osVer.majorVersion, (long)osVer.minorVersion]; |
239 | 253 | } else { |
240 | 254 | nsVerStr = [NSString stringWithFormat:@"%ld.%ld.%ld", |
241 | | - (long)osVer.majorVersion, (long)osVer.minorVersion, (long)osVer.patchVersion]; |
| 255 | + (long)osVer.majorVersion, (long)osVer.minorVersion, |
| 256 | + (long)osVer.patchVersion]; |
242 | 257 | } |
243 | 258 | } else { |
244 | | - // Version 10.16, without explicit env setting of SYSTEM_VERSION_COMPAT |
245 | | - // AKA 11+ Read the *real* ProductVersion from the hidden link to avoid SYSTEM_VERSION_COMPAT |
246 | | - // If not found, fallback below to the SystemVersion.plist |
247 | | - NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile : |
248 | | - @"/System/Library/CoreServices/.SystemVersionPlatform.plist"]; |
| 259 | + // Requires special handling. We ignore the version reported |
| 260 | + // by the NSOperatingSystemVersion API and instead read the |
| 261 | + // *real* ProductVersion from |
| 262 | + // /System/Library/CoreServices/.SystemVersionPlatform.plist. |
| 263 | + // If not found there, then as a last resort we fallback to |
| 264 | + // /System/Library/CoreServices/SystemVersion.plist |
| 265 | + NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile: |
| 266 | + @"/System/Library/CoreServices/.SystemVersionPlatform.plist"]; |
249 | 267 | if (version != NULL) { |
250 | | - nsVerStr = [version objectForKey : @"ProductVersion"]; |
| 268 | + nsVerStr = [version objectForKey: @"ProductVersion"]; |
251 | 269 | } |
252 | 270 | } |
253 | | - // Fallback to reading the SystemVersion.plist |
| 271 | + // Last resort - fallback to reading the SystemVersion.plist |
254 | 272 | if (nsVerStr == NULL) { |
255 | | - NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile : |
256 | | - @"/System/Library/CoreServices/SystemVersion.plist"]; |
| 273 | + NSDictionary *version = [NSDictionary dictionaryWithContentsOfFile: |
| 274 | + @"/System/Library/CoreServices/SystemVersion.plist"]; |
257 | 275 | if (version != NULL) { |
258 | | - nsVerStr = [version objectForKey : @"ProductVersion"]; |
| 276 | + nsVerStr = [version objectForKey: @"ProductVersion"]; |
259 | 277 | } |
260 | 278 | } |
261 | 279 |
|
|
0 commit comments