Skip to content
New issue

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

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

Already on GitHub? # to your account

common: Add bootSessionUUID to SNTSystemInfo #210

Merged
merged 3 commits into from
Jan 15, 2025
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
8 changes: 8 additions & 0 deletions Source/common/SNTSystemInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
///
+ (NSString *)hardwareUUID;

///
/// The boot session UUID is generated by IOKit during early startup and is
/// persistent until the system reboots.
///
/// @return Boot Session UUID
///
+ (NSString *)bootSessionUUID;

///
/// @return OS Version, e.g.: 10.10.2
///
Expand Down
59 changes: 38 additions & 21 deletions Source/common/SNTSystemInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,51 @@
@implementation SNTSystemInfo

+ (NSString *)serialNumber {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
io_service_t platformExpert = IOServiceGetMatchingService(
kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
#pragma clang diagnostic pop
if (!platformExpert) return nil;

NSString *serial = CFBridgingRelease(IORegistryEntryCreateCFProperty(
platformExpert, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0));

IOObjectRelease(platformExpert);

static NSString *serial;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
io_service_t platformExpert = IOServiceGetMatchingService(
kIOMainPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
if (!platformExpert) return;

serial = CFBridgingRelease(IORegistryEntryCreateCFProperty(
platformExpert, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, 0));

IOObjectRelease(platformExpert);
});
return serial;
}

+ (NSString *)hardwareUUID {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
io_service_t platformExpert = IOServiceGetMatchingService(
kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
#pragma clang diagnostic pop
if (!platformExpert) return nil;
static NSString *uuid;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
io_service_t platformExpert = IOServiceGetMatchingService(
kIOMainPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
if (!platformExpert) return;

NSString *uuid = CFBridgingRelease(IORegistryEntryCreateCFProperty(
platformExpert, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0));
uuid = CFBridgingRelease(IORegistryEntryCreateCFProperty(
platformExpert, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0));

IOObjectRelease(platformExpert);
IOObjectRelease(platformExpert);
});

return uuid;
}

+ (NSString *)bootSessionUUID {
static NSString *uuid;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
uuid_string_t bootSessionUUID = {};
size_t uuidLength = sizeof(bootSessionUUID);
// If this fails, bootSessionUUID is left as a fixed stack array of 0's,
// so the line following will create a zero-length NSString. There's not
// much we can really do if this fails, there are no other ways to get this
// information.
sysctlbyname("kern.bootsessionuuid", bootSessionUUID, &uuidLength, NULL, 0);
uuid = @(bootSessionUUID);
});
return uuid;
}

Expand Down
Loading