osquery-1/osquery/utils/system/uptime.cpp
Jesse Kornblum c7355b19aa Update osquery licensing wording (#5452)
Summary:
Pull Request resolved: https://github.com/facebook/osquery/pull/5452

As suggested in another diff, this diff updates the language we use to describe the osquery licensing terms. We are changing all instances of

//This source code is licensed as defined on the LICENSE file found in the root directory of this source tree.//

to

//This source code is licensed in accordance with the terms specified in the LICENSE file found in the root directory of this source tree.//

We accomplish this with a codemod:

  $ codemod -md xplat/osquery/oss --extensions cpp,h,in,py,sh,mm,ps1 "(.\s+)This source code is licensed as defined on the LICENSE file found in the(.*)root directory of this source tree\." "\1This source code is licensed in accordance with the terms specified in\2the LICENSE file found in the root directory of this source tree."

Reviewed By: fmanco

Differential Revision: D14131290

fbshipit-source-id: 52c90da342263e2a80f5a678ecd760c19cf7513e
2019-02-19 10:59:48 -08:00

53 lines
1.1 KiB
C++

/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed in accordance with the terms specified in
* the LICENSE file found in the root directory of this source tree.
*/
#include <osquery/utils/system/uptime.h>
#if defined(__APPLE__)
#include <errno.h>
#include <sys/sysctl.h>
#include <time.h>
#elif defined(__linux__)
#include <sys/sysinfo.h>
#elif defined(WIN32)
#include <osquery/utils/system/system.h>
#endif
namespace osquery {
long getUptime() {
#if defined(DARWIN)
struct timeval boot_time;
size_t len = sizeof(boot_time);
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
if (sysctl(mib, 2, &boot_time, &len, nullptr, 0) < 0) {
return -1;
}
time_t seconds_since_boot = boot_time.tv_sec;
time_t current_seconds = time(nullptr);
return long(difftime(current_seconds, seconds_since_boot));
#elif defined(__linux__)
struct sysinfo sys_info;
if (sysinfo(&sys_info) != 0) {
return -1;
}
return sys_info.uptime;
#elif defined(WIN32)
return static_cast<long>(GetTickCount64() / 1000);
#endif
return -1;
}
} // namespace osquery