#include #include #include #include "platform.h" #include #include // gethostname, sysconf #include #include namespace node { using namespace v8; static Handle GetHostname(const Arguments& args) { HandleScope scope; char s[255]; if (gethostname(s, 255) < 0) { return Undefined(); } return scope.Close(String::New(s)); } static Handle GetOSType(const Arguments& args) { HandleScope scope; char type[256]; struct utsname info; uname(&info); strncpy(type, info.sysname, strlen(info.sysname)); type[strlen(info.sysname)] = 0; return scope.Close(String::New(type)); } static Handle GetOSRelease(const Arguments& args) { HandleScope scope; char release[256]; struct utsname info; uname(&info); strncpy(release, info.release, strlen(info.release)); release[strlen(info.release)] = 0; return scope.Close(String::New(release)); } static Handle GetCPUInfo(const Arguments& args) { HandleScope scope; Local cpus; int r = Platform::GetCPUInfo(&cpus); if (r < 0) { return Undefined(); } return scope.Close(cpus); } static Handle GetFreeMemory(const Arguments& args) { HandleScope scope; double amount = Platform::GetFreeMemory(); if (amount < 0) { return Undefined(); } return scope.Close(Number::New(amount)); } static Handle GetTotalMemory(const Arguments& args) { HandleScope scope; double amount = Platform::GetTotalMemory(); if (amount < 0) { return Undefined(); } return scope.Close(Number::New(amount)); } static Handle GetUptime(const Arguments& args) { HandleScope scope; double uptime = Platform::GetUptime(); if (uptime < 0) { return Undefined(); } return scope.Close(Number::New(uptime)); } static Handle GetLoadAvg(const Arguments& args) { HandleScope scope; Local loads = Array::New(3); int r = Platform::GetLoadAvg(&loads); if (r < 0) { return Undefined(); } return scope.Close(loads); } void OS::Initialize(v8::Handle target) { HandleScope scope; NODE_SET_METHOD(target, "getHostname", GetHostname); NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg); NODE_SET_METHOD(target, "getUptime", GetUptime); NODE_SET_METHOD(target, "getTotalMem", GetTotalMemory); NODE_SET_METHOD(target, "getFreeMem", GetFreeMemory); NODE_SET_METHOD(target, "getCPUs", GetCPUInfo); NODE_SET_METHOD(target, "getOSType", GetOSType); NODE_SET_METHOD(target, "getOSRelease", GetOSRelease); } } // namespace node NODE_MODULE(node_os, node::OS::Initialize);