src: pass Isolate to all applicable api
[platform/upstream/nodejs.git] / src / node_os.cc
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22
23 #include "node.h"
24 #include "node_os.h"
25
26 #include "v8.h"
27
28 #include <errno.h>
29 #include <string.h>
30
31 #ifdef __MINGW32__
32 # include <io.h>
33 #endif
34
35 #ifdef __POSIX__
36 # include <unistd.h>  // gethostname, sysconf
37 # include <sys/utsname.h>
38 #endif
39
40 namespace node {
41
42 using namespace v8;
43
44 static Handle<Value> GetEndianness(const Arguments& args) {
45   HandleScope scope(node_isolate);
46   int i = 1;
47   bool big = (*(char *)&i) == 0;
48   Local<String> endianness = String::New(big ? "BE" : "LE");
49   return scope.Close(endianness);
50 }
51
52 static Handle<Value> GetHostname(const Arguments& args) {
53   HandleScope scope(node_isolate);
54   char s[255];
55   int r = gethostname(s, 255);
56
57   if (r < 0) {
58 #ifdef __POSIX__
59     return ThrowException(ErrnoException(errno, "gethostname"));
60 #else // __MINGW32__
61     return ThrowException(ErrnoException(WSAGetLastError(), "gethostname"));
62 #endif // __MINGW32__
63   }
64
65   return scope.Close(String::New(s));
66 }
67
68 static Handle<Value> GetOSType(const Arguments& args) {
69   HandleScope scope(node_isolate);
70
71 #ifdef __POSIX__
72   char type[256];
73   struct utsname info;
74
75   uname(&info);
76   strncpy(type, info.sysname, strlen(info.sysname));
77   type[strlen(info.sysname)] = 0;
78
79   return scope.Close(String::New(type));
80 #else // __MINGW32__
81   return scope.Close(String::New("Windows_NT"));
82 #endif
83 }
84
85 static Handle<Value> GetOSRelease(const Arguments& args) {
86   HandleScope scope(node_isolate);
87   char release[256];
88
89 #ifdef __POSIX__
90   struct utsname info;
91
92   uname(&info);
93   strncpy(release, info.release, strlen(info.release));
94   release[strlen(info.release)] = 0;
95
96 #else // __MINGW32__
97   OSVERSIONINFO info;
98   info.dwOSVersionInfoSize = sizeof(info);
99
100   if (GetVersionEx(&info) == 0) {
101     return Undefined(node_isolate);
102   }
103
104   sprintf(release, "%d.%d.%d", static_cast<int>(info.dwMajorVersion),
105       static_cast<int>(info.dwMinorVersion), static_cast<int>(info.dwBuildNumber));
106 #endif
107
108   return scope.Close(String::New(release));
109 }
110
111 static Handle<Value> GetCPUInfo(const Arguments& args) {
112   HandleScope scope(node_isolate);
113   uv_cpu_info_t* cpu_infos;
114   int count, i;
115
116   uv_err_t err = uv_cpu_info(&cpu_infos, &count);
117
118   if (err.code != UV_OK) {
119     return Undefined(node_isolate);
120   }
121
122   Local<Array> cpus = Array::New();
123
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));
136
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);
143   }
144
145   uv_free_cpu_info(cpu_infos, count);
146
147   return scope.Close(cpus);
148 }
149
150 static Handle<Value> GetFreeMemory(const Arguments& args) {
151   HandleScope scope(node_isolate);
152   double amount = uv_get_free_memory();
153
154   if (amount < 0) {
155     return Undefined(node_isolate);
156   }
157
158   return scope.Close(Number::New(amount));
159 }
160
161 static Handle<Value> GetTotalMemory(const Arguments& args) {
162   HandleScope scope(node_isolate);
163   double amount = uv_get_total_memory();
164
165   if (amount < 0) {
166     return Undefined(node_isolate);
167   }
168
169   return scope.Close(Number::New(amount));
170 }
171
172 static Handle<Value> GetUptime(const Arguments& args) {
173   HandleScope scope(node_isolate);
174   double uptime;
175
176   uv_err_t err = uv_uptime(&uptime);
177
178   if (err.code != UV_OK) {
179     return Undefined(node_isolate);
180   }
181
182   return scope.Close(Number::New(uptime));
183 }
184
185 static Handle<Value> GetLoadAvg(const Arguments& args) {
186   HandleScope scope(node_isolate);
187   double loadavg[3];
188   uv_loadavg(loadavg);
189
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]));
194
195   return scope.Close(loads);
196 }
197
198
199 static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
200   HandleScope scope(node_isolate);
201   uv_interface_address_t* interfaces;
202   int count, i;
203   char ip[INET6_ADDRSTRLEN];
204   Local<Object> ret, o;
205   Local<String> name, family;
206   Local<Array> ifarr;
207
208   uv_err_t err = uv_interface_addresses(&interfaces, &count);
209
210   if (err.code != UV_OK)
211     return ThrowException(UVException(err.code, "uv_interface_addresses"));
212
213   ret = Object::New();
214
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));
219     } else {
220       ifarr = Array::New();
221       ret->Set(name, ifarr);
222     }
223
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");
230     } else {
231       strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
232       family = String::New("<unknown>");
233     }
234
235     o = Object::New();
236     o->Set(String::New("address"), String::New(ip));
237     o->Set(String::New("family"), family);
238
239     const bool internal = interfaces[i].is_internal;
240     o->Set(String::New("internal"),
241            internal ? True(node_isolate) : False(node_isolate));
242
243     ifarr->Set(ifarr->Length(), o);
244   }
245
246   uv_free_interface_addresses(interfaces, count);
247
248   return scope.Close(ret);
249 }
250
251
252 void OS::Initialize(v8::Handle<v8::Object> target) {
253   HandleScope scope(node_isolate);
254
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);
265 }
266
267
268 }  // namespace node
269
270 NODE_MODULE(node_os, node::OS::Initialize)