diff --git a/Headers/Foundation/NSProcessInfo.h b/Headers/Foundation/NSProcessInfo.h index fd20a9747..e197ef878 100644 --- a/Headers/Foundation/NSProcessInfo.h +++ b/Headers/Foundation/NSProcessInfo.h @@ -83,6 +83,14 @@ enum { */ + (NSProcessInfo*) processInfo; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_6,GS_API_LATEST) +#if GS_HAS_DECLARED_PROPERTIES +@property (readonly) NSUInteger systemUptime; +#else +- (NSUInteger) systemUptime; +#endif +#endif + /** * Returns an array containing the arguments supplied to start this * process.
diff --git a/Source/NSProcessInfo.m b/Source/NSProcessInfo.m index 4768b6ad2..bf80546ba 100644 --- a/Source/NSProcessInfo.m +++ b/Source/NSProcessInfo.m @@ -1465,6 +1465,43 @@ static void determineOperatingSystem() return availMem; } +- (NSUInteger) systemUptime +{ + NSUInteger uptime = 0; + +#if defined(_WIN32) + uptime = GetTickCount64() / 1000; +#elif defined(HAVE_SYSCTLBYNAME) + struct timeval tval; + size_t len = sizeof(tval); + + if (sysctlbyname("kern.boottime", &tval, &len, 0, 0) == 0) + { + uptime = tval.tv_sec; + } +#elif defined(HAVE_PROCFS) + NSFileManager *fileManager = [NSFileManager defaultManager]; + + if ([fileManager fileExistsAtPath: @"/proc/uptime"]) + { + NSString *uptimeContent = [NSString + stringWithContentsOfFile: @"/proc/uptime"]; + NSString *uptimeString = [[uptimeContent + componentsSeparatedByString:@" "] objectAtIndex:0]; + uptime = [uptimeString intValue]; + } +#else +#warning "no known way to determine uptime on this system" +#endif + + if (uptime == 0) + { + NSLog(@"Cannot determine uptime."); + } + + return uptime; +} + @end void diff --git a/Tests/base/NSProcessInfo/general.m b/Tests/base/NSProcessInfo/general.m index 043f79852..07ba5333d 100644 --- a/Tests/base/NSProcessInfo/general.m +++ b/Tests/base/NSProcessInfo/general.m @@ -39,8 +39,14 @@ int main() PASS((obj != nil && [obj isKindOfClass:[NSString class]] && [obj length] > 0), "-operatingSystemName works"); NSLog(@"operatingSystemName %@", obj); + val = [info operatingSystem]; PASS(val != 0, "-operatingSystem works"); + + val = [info systemUptime]; + NSLog(@"systemUptime %lu", val); + PASS(val != 0, "-systemUptime works"); + obj = [info hostName]; PASS((obj != nil && [obj isKindOfClass:[NSString class]] && [obj length] > 0), "-hostName works");