Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 271 lines (208 sloc) 7.372 kb
55048cd ry Update copyright headers
ry authored
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
f1762ff Brian White Add os module and getHostname
mscdex authored
22
ff4a9d3 Ben Noordhuis core: use proper #include directives
bnoordhuis authored
23 #include "node.h"
24 #include "node_os.h"
f1762ff Brian White Add os module and getHostname
mscdex authored
25
ff4a9d3 Ben Noordhuis core: use proper #include directives
bnoordhuis authored
26 #include "v8.h"
d75c338 Brian White Add more functionality to the os module
mscdex authored
27
f1762ff Brian White Add os module and getHostname
mscdex authored
28 #include <errno.h>
8275d7c Brian White Fix Cygwin compatibility in the os module
mscdex authored
29 #include <string.h>
86518c3 Bert Belder Port GetHostName
piscisaureus authored
30
e0f47be Bert Belder Clean up the way windows headers are included
piscisaureus authored
31 #ifdef __MINGW32__
61af420 Bert Belder Fix dns on windows
piscisaureus authored
32 # include <io.h>
e0f47be Bert Belder Clean up the way windows headers are included
piscisaureus authored
33 #endif
34
86518c3 Bert Belder Port GetHostName
piscisaureus authored
35 #ifdef __POSIX__
33118df Bert Belder Merge remote branch 'origin/master'
piscisaureus authored
36 # include <unistd.h> // gethostname, sysconf
37 # include <sys/utsname.h>
e0f47be Bert Belder Clean up the way windows headers are included
piscisaureus authored
38 #endif
f1762ff Brian White Add os module and getHostname
mscdex authored
39
40 namespace node {
41
42 using namespace v8;
43
5e4e87a Nathan Rajlich os: add os.endianness() function
TooTallNate authored
44 static Handle<Value> GetEndianness(const Arguments& args) {
45 HandleScope scope;
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
f1762ff Brian White Add os module and getHostname
mscdex authored
52 static Handle<Value> GetHostname(const Arguments& args) {
53 HandleScope scope;
54 char s[255];
55 int r = gethostname(s, 255);
56
57 if (r < 0) {
0eb4c21 Bert Belder Define winsock error numbers
piscisaureus authored
58 #ifdef __POSIX__
f1762ff Brian White Add os module and getHostname
mscdex authored
59 return ThrowException(ErrnoException(errno, "gethostname"));
0eb4c21 Bert Belder Define winsock error numbers
piscisaureus authored
60 #else // __MINGW32__
61 return ThrowException(ErrnoException(WSAGetLastError(), "gethostname"));
62 #endif // __MINGW32__
f1762ff Brian White Add os module and getHostname
mscdex authored
63 }
64
65 return scope.Close(String::New(s));
66 }
67
d75c338 Brian White Add more functionality to the os module
mscdex authored
68 static Handle<Value> GetOSType(const Arguments& args) {
69 HandleScope scope;
c3ffbf2 Bert Belder Fix the OS module for win32
piscisaureus authored
70
71 #ifdef __POSIX__
d75c338 Brian White Add more functionality to the os module
mscdex authored
72 char type[256];
8275d7c Brian White Fix Cygwin compatibility in the os module
mscdex authored
73 struct utsname info;
d75c338 Brian White Add more functionality to the os module
mscdex authored
74
8275d7c Brian White Fix Cygwin compatibility in the os module
mscdex authored
75 uname(&info);
76 strncpy(type, info.sysname, strlen(info.sysname));
77 type[strlen(info.sysname)] = 0;
d75c338 Brian White Add more functionality to the os module
mscdex authored
78
79 return scope.Close(String::New(type));
c3ffbf2 Bert Belder Fix the OS module for win32
piscisaureus authored
80 #else // __MINGW32__
81 return scope.Close(String::New("Windows_NT"));
82 #endif
d75c338 Brian White Add more functionality to the os module
mscdex authored
83 }
84
85 static Handle<Value> GetOSRelease(const Arguments& args) {
86 HandleScope scope;
87 char release[256];
c3ffbf2 Bert Belder Fix the OS module for win32
piscisaureus authored
88
89 #ifdef __POSIX__
8275d7c Brian White Fix Cygwin compatibility in the os module
mscdex authored
90 struct utsname info;
d75c338 Brian White Add more functionality to the os module
mscdex authored
91
8275d7c Brian White Fix Cygwin compatibility in the os module
mscdex authored
92 uname(&info);
93 strncpy(release, info.release, strlen(info.release));
94 release[strlen(info.release)] = 0;
d75c338 Brian White Add more functionality to the os module
mscdex authored
95
c3ffbf2 Bert Belder Fix the OS module for win32
piscisaureus authored
96 #else // __MINGW32__
97 OSVERSIONINFO info;
98 info.dwOSVersionInfoSize = sizeof(info);
99
100 if (GetVersionEx(&info) == 0) {
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
101 return Undefined();
c3ffbf2 Bert Belder Fix the OS module for win32
piscisaureus authored
102 }
103
c99962e Bert Belder Misc. cleanups
piscisaureus authored
104 sprintf(release, "%d.%d.%d", static_cast<int>(info.dwMajorVersion),
105 static_cast<int>(info.dwMinorVersion), static_cast<int>(info.dwBuildNumber));
c3ffbf2 Bert Belder Fix the OS module for win32
piscisaureus authored
106 #endif
107
d75c338 Brian White Add more functionality to the os module
mscdex authored
108 return scope.Close(String::New(release));
109 }
110
111 static Handle<Value> GetCPUInfo(const Arguments& args) {
112 HandleScope scope;
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
113 uv_cpu_info_t* cpu_infos;
114 int count, i;
d75c338 Brian White Add more functionality to the os module
mscdex authored
115
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
116 uv_err_t err = uv_cpu_info(&cpu_infos, &count);
117
118 if (err.code != UV_OK) {
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
119 return Undefined();
d75c338 Brian White Add more functionality to the os module
mscdex authored
120 }
121
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
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"),
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
127 Integer::New(cpu_infos[i].cpu_times.user));
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
128 times_info->Set(String::New("nice"),
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
129 Integer::New(cpu_infos[i].cpu_times.nice));
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
130 times_info->Set(String::New("sys"),
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
131 Integer::New(cpu_infos[i].cpu_times.sys));
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
132 times_info->Set(String::New("idle"),
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
133 Integer::New(cpu_infos[i].cpu_times.idle));
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
134 times_info->Set(String::New("irq"),
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
135 Integer::New(cpu_infos[i].cpu_times.irq));
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
136
137 Local<Object> cpu_info = Object::New();
138 cpu_info->Set(String::New("model"), String::New(cpu_infos[i].model));
6573fc3 Ben Noordhuis src: pass node_isolate to Integer::New
bnoordhuis authored
139 cpu_info->Set(String::New("speed"),
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
140 Integer::New(cpu_infos[i].speed));
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
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
d75c338 Brian White Add more functionality to the os module
mscdex authored
147 return scope.Close(cpus);
148 }
149
150 static Handle<Value> GetFreeMemory(const Arguments& args) {
151 HandleScope scope;
6984553 Fedor Indutny use uv for memory and loadavg functions
indutny authored
152 double amount = uv_get_free_memory();
d75c338 Brian White Add more functionality to the os module
mscdex authored
153
154 if (amount < 0) {
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
155 return Undefined();
d75c338 Brian White Add more functionality to the os module
mscdex authored
156 }
157
158 return scope.Close(Number::New(amount));
159 }
160
161 static Handle<Value> GetTotalMemory(const Arguments& args) {
162 HandleScope scope;
6984553 Fedor Indutny use uv for memory and loadavg functions
indutny authored
163 double amount = uv_get_total_memory();
d75c338 Brian White Add more functionality to the os module
mscdex authored
164
165 if (amount < 0) {
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
166 return Undefined();
d75c338 Brian White Add more functionality to the os module
mscdex authored
167 }
168
169 return scope.Close(Number::New(amount));
170 }
171
172 static Handle<Value> GetUptime(const Arguments& args) {
173 HandleScope scope;
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
174 double uptime;
d75c338 Brian White Add more functionality to the os module
mscdex authored
175
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
176 uv_err_t err = uv_uptime(&uptime);
177
178 if (err.code != UV_OK) {
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
179 return Undefined();
d75c338 Brian White Add more functionality to the os module
mscdex authored
180 }
181
182 return scope.Close(Number::New(uptime));
183 }
184
185 static Handle<Value> GetLoadAvg(const Arguments& args) {
186 HandleScope scope;
6984553 Fedor Indutny use uv for memory and loadavg functions
indutny authored
187 double loadavg[3];
188 uv_loadavg(loadavg);
d75c338 Brian White Add more functionality to the os module
mscdex authored
189
6984553 Fedor Indutny use uv for memory and loadavg functions
indutny authored
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
d75c338 Brian White Add more functionality to the os module
mscdex authored
195 return scope.Close(loads);
196 }
197
19e5351 ry os.getNetworkInterfaces()
ry authored
198
199 static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
200 HandleScope scope;
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
03b00dc Bert Belder os: throw when os.networkInterfaces() fails
piscisaureus authored
210 if (err.code != UV_OK)
211 return ThrowException(UVException(err.code, "uv_interface_addresses"));
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
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);
109f73b Ben Noordhuis src: pass node_isolate to True() and False()
bnoordhuis authored
238
239 const bool internal = interfaces[i].is_internal;
240 o->Set(String::New("internal"),
51f6e6a Ben Noordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
241 internal ? True() : False());
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored
242
243 ifarr->Set(ifarr->Length(), o);
244 }
245
246 uv_free_interface_addresses(interfaces, count);
247
248 return scope.Close(ret);
19e5351 ry os.getNetworkInterfaces()
ry authored
249 }
250
251
f1762ff Brian White Add os module and getHostname
mscdex authored
252 void OS::Initialize(v8::Handle<v8::Object> target) {
253 HandleScope scope;
254
5e4e87a Nathan Rajlich os: add os.endianness() function
TooTallNate authored
255 NODE_SET_METHOD(target, "getEndianness", GetEndianness);
f1762ff Brian White Add os module and getHostname
mscdex authored
256 NODE_SET_METHOD(target, "getHostname", GetHostname);
d75c338 Brian White Add more functionality to the os module
mscdex authored
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);
19e5351 ry os.getNetworkInterfaces()
ry authored
264 NODE_SET_METHOD(target, "getInterfaceAddresses", GetInterfaceAddresses);
f1762ff Brian White Add os module and getHostname
mscdex authored
265 }
266
267
268 } // namespace node
269
cdcb111 Ben Noordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored
270 NODE_MODULE(node_os, node::OS::Initialize)
Something went wrong with that request. Please try again.