2015-07-03 05:32:48 +00:00
|
|
|
/*
|
2016-02-11 19:48:58 +00:00
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
2015-07-03 05:32:48 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osquery/tables.h>
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
2016-07-25 22:34:17 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/sysctl.h>
|
2015-07-03 05:32:48 +00:00
|
|
|
#elif defined(__linux__)
|
2016-07-25 22:34:17 +00:00
|
|
|
#include <sys/sysinfo.h>
|
2017-01-12 05:25:39 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
#include <windows.h>
|
2015-07-03 05:32:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
namespace tables {
|
|
|
|
|
|
|
|
long getUptime() {
|
2016-07-25 22:34:17 +00:00
|
|
|
#if defined(DARWIN)
|
|
|
|
struct timeval boot_time;
|
|
|
|
size_t len = sizeof(boot_time);
|
|
|
|
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
|
2015-07-03 05:32:48 +00:00
|
|
|
|
2016-10-21 18:20:28 +00:00
|
|
|
if (sysctl(mib, 2, &boot_time, &len, nullptr, 0) < 0) {
|
2016-07-25 22:34:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-03 05:32:48 +00:00
|
|
|
|
2016-07-25 22:34:17 +00:00
|
|
|
time_t seconds_since_boot = boot_time.tv_sec;
|
2016-10-21 18:20:28 +00:00
|
|
|
time_t current_seconds = time(nullptr);
|
2015-07-03 05:32:48 +00:00
|
|
|
|
2016-07-25 22:34:17 +00:00
|
|
|
return long(difftime(current_seconds, seconds_since_boot));
|
|
|
|
#elif defined(__linux__)
|
|
|
|
struct sysinfo sys_info;
|
2015-07-03 05:32:48 +00:00
|
|
|
|
2016-07-25 22:34:17 +00:00
|
|
|
if (sysinfo(&sys_info) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-03 05:32:48 +00:00
|
|
|
|
2016-07-25 22:34:17 +00:00
|
|
|
return sys_info.uptime;
|
2017-01-12 05:25:39 +00:00
|
|
|
#elif defined(WIN32)
|
|
|
|
return (long)GetTickCount64() / 1000;
|
2016-07-25 22:34:17 +00:00
|
|
|
#endif
|
2015-09-24 23:12:26 +00:00
|
|
|
|
|
|
|
return -1;
|
2015-07-03 05:32:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QueryData genUptime(QueryContext& context) {
|
|
|
|
Row r;
|
|
|
|
QueryData results;
|
|
|
|
long uptime_in_seconds = getUptime();
|
|
|
|
|
|
|
|
if (uptime_in_seconds >= 0) {
|
|
|
|
r["days"] = INTEGER(uptime_in_seconds / 60 / 60 / 24);
|
|
|
|
r["hours"] = INTEGER((uptime_in_seconds / 60 / 60) % 24);
|
|
|
|
r["minutes"] = INTEGER((uptime_in_seconds / 60) % 60);
|
|
|
|
r["seconds"] = INTEGER(uptime_in_seconds % 60);
|
|
|
|
r["total_seconds"] = BIGINT(uptime_in_seconds);
|
|
|
|
results.push_back(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|