1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
36 # include <unistd.h> // gethostname, sysconf
37 # include <sys/utsname.h>
44 static Handle<Value> GetEndianness(const Arguments& args) {
45 HandleScope scope(node_isolate);
47 bool big = (*(char *)&i) == 0;
48 Local<String> endianness = String::New(big ? "BE" : "LE");
49 return scope.Close(endianness);
52 static Handle<Value> GetHostname(const Arguments& args) {
53 HandleScope scope(node_isolate);
55 int r = gethostname(s, 255);
59 return ThrowException(ErrnoException(errno, "gethostname"));
61 return ThrowException(ErrnoException(WSAGetLastError(), "gethostname"));
65 return scope.Close(String::New(s));
68 static Handle<Value> GetOSType(const Arguments& args) {
69 HandleScope scope(node_isolate);
76 strncpy(type, info.sysname, strlen(info.sysname));
77 type[strlen(info.sysname)] = 0;
79 return scope.Close(String::New(type));
81 return scope.Close(String::New("Windows_NT"));
85 static Handle<Value> GetOSRelease(const Arguments& args) {
86 HandleScope scope(node_isolate);
93 strncpy(release, info.release, strlen(info.release));
94 release[strlen(info.release)] = 0;
98 info.dwOSVersionInfoSize = sizeof(info);
100 if (GetVersionEx(&info) == 0) {
101 return Undefined(node_isolate);
104 sprintf(release, "%d.%d.%d", static_cast<int>(info.dwMajorVersion),
105 static_cast<int>(info.dwMinorVersion), static_cast<int>(info.dwBuildNumber));
108 return scope.Close(String::New(release));
111 static Handle<Value> GetCPUInfo(const Arguments& args) {
112 HandleScope scope(node_isolate);
113 uv_cpu_info_t* cpu_infos;
116 uv_err_t err = uv_cpu_info(&cpu_infos, &count);
118 if (err.code != UV_OK) {
119 return Undefined(node_isolate);
122 Local<Array> cpus = Array::New();
124 for (i = 0; i < count; i++) {
125 Local<Object> times_info = Object::New();
126 times_info->Set(String::New("user"),
127 Integer::New(cpu_infos[i].cpu_times.user, node_isolate));
128 times_info->Set(String::New("nice"),
129 Integer::New(cpu_infos[i].cpu_times.nice, node_isolate));
130 times_info->Set(String::New("sys"),
131 Integer::New(cpu_infos[i].cpu_times.sys, node_isolate));
132 times_info->Set(String::New("idle"),
133 Integer::New(cpu_infos[i].cpu_times.idle, node_isolate));
134 times_info->Set(String::New("irq"),
135 Integer::New(cpu_infos[i].cpu_times.irq, node_isolate));
137 Local<Object> cpu_info = Object::New();
138 cpu_info->Set(String::New("model"), String::New(cpu_infos[i].model));
139 cpu_info->Set(String::New("speed"),
140 Integer::New(cpu_infos[i].speed, node_isolate));
141 cpu_info->Set(String::New("times"), times_info);
142 (*cpus)->Set(i,cpu_info);
145 uv_free_cpu_info(cpu_infos, count);
147 return scope.Close(cpus);
150 static Handle<Value> GetFreeMemory(const Arguments& args) {
151 HandleScope scope(node_isolate);
152 double amount = uv_get_free_memory();
155 return Undefined(node_isolate);
158 return scope.Close(Number::New(amount));
161 static Handle<Value> GetTotalMemory(const Arguments& args) {
162 HandleScope scope(node_isolate);
163 double amount = uv_get_total_memory();
166 return Undefined(node_isolate);
169 return scope.Close(Number::New(amount));
172 static Handle<Value> GetUptime(const Arguments& args) {
173 HandleScope scope(node_isolate);
176 uv_err_t err = uv_uptime(&uptime);
178 if (err.code != UV_OK) {
179 return Undefined(node_isolate);
182 return scope.Close(Number::New(uptime));
185 static Handle<Value> GetLoadAvg(const Arguments& args) {
186 HandleScope scope(node_isolate);
190 Local<Array> loads = Array::New(3);
191 loads->Set(0, Number::New(loadavg[0]));
192 loads->Set(1, Number::New(loadavg[1]));
193 loads->Set(2, Number::New(loadavg[2]));
195 return scope.Close(loads);
199 static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
200 HandleScope scope(node_isolate);
201 uv_interface_address_t* interfaces;
203 char ip[INET6_ADDRSTRLEN];
204 Local<Object> ret, o;
205 Local<String> name, family;
208 uv_err_t err = uv_interface_addresses(&interfaces, &count);
210 if (err.code != UV_OK)
211 return ThrowException(UVException(err.code, "uv_interface_addresses"));
215 for (i = 0; i < count; i++) {
216 name = String::New(interfaces[i].name);
217 if (ret->Has(name)) {
218 ifarr = Local<Array>::Cast(ret->Get(name));
220 ifarr = Array::New();
221 ret->Set(name, ifarr);
224 if (interfaces[i].address.address4.sin_family == AF_INET) {
225 uv_ip4_name(&interfaces[i].address.address4,ip, sizeof(ip));
226 family = String::New("IPv4");
227 } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
228 uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
229 family = String::New("IPv6");
231 strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
232 family = String::New("<unknown>");
236 o->Set(String::New("address"), String::New(ip));
237 o->Set(String::New("family"), family);
239 const bool internal = interfaces[i].is_internal;
240 o->Set(String::New("internal"),
241 internal ? True(node_isolate) : False(node_isolate));
243 ifarr->Set(ifarr->Length(), o);
246 uv_free_interface_addresses(interfaces, count);
248 return scope.Close(ret);
252 void OS::Initialize(v8::Handle<v8::Object> target) {
253 HandleScope scope(node_isolate);
255 NODE_SET_METHOD(target, "getEndianness", GetEndianness);
256 NODE_SET_METHOD(target, "getHostname", GetHostname);
257 NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg);
258 NODE_SET_METHOD(target, "getUptime", GetUptime);
259 NODE_SET_METHOD(target, "getTotalMem", GetTotalMemory);
260 NODE_SET_METHOD(target, "getFreeMem", GetFreeMemory);
261 NODE_SET_METHOD(target, "getCPUs", GetCPUInfo);
262 NODE_SET_METHOD(target, "getOSType", GetOSType);
263 NODE_SET_METHOD(target, "getOSRelease", GetOSRelease);
264 NODE_SET_METHOD(target, "getInterfaceAddresses", GetInterfaceAddresses);
270 NODE_MODULE(node_os, node::OS::Initialize)