Skip to content
This repository
Newer
Older
100644 268 lines (206 sloc) 7.341 kb
55048cdf »
2011-03-10 Update copyright headers
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
f1762ff8 »
2010-12-11 Add os module and getHostname
22
ff4a9d38 »
2012-03-10 core: use proper #include directives
23 #include "node.h"
24 #include "node_os.h"
f1762ff8 »
2010-12-11 Add os module and getHostname
25
ff4a9d38 »
2012-03-10 core: use proper #include directives
26 #include "v8.h"
d75c338d »
2010-12-22 Add more functionality to the os module
27
f1762ff8 »
2010-12-11 Add os module and getHostname
28 #include <errno.h>
8275d7cd »
2010-12-23 Fix Cygwin compatibility in the os module
29 #include <string.h>
86518c3b »
2010-12-21 Port GetHostName
30
e0f47be9 »
2011-01-18 Clean up the way windows headers are included
31 #ifdef __MINGW32__
61af4207 »
2011-02-07 Fix dns on windows
32 # include <io.h>
e0f47be9 »
2011-01-18 Clean up the way windows headers are included
33 #endif
34
86518c3b »
2010-12-21 Port GetHostName
35 #ifdef __POSIX__
33118df8 »
2011-01-02 Merge remote branch 'origin/master'
36 # include <unistd.h> // gethostname, sysconf
37 # include <sys/utsname.h>
e0f47be9 »
2011-01-18 Clean up the way windows headers are included
38 #endif
f1762ff8 »
2010-12-11 Add os module and getHostname
39
40 namespace node {
41
42 using namespace v8;
43
5e4e87ad »
2012-11-08 os: add os.endianness() function
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
f1762ff8 »
2010-12-11 Add os module and getHostname
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) {
0eb4c215 »
2011-01-18 Define winsock error numbers
58 #ifdef __POSIX__
f1762ff8 »
2010-12-11 Add os module and getHostname
59 return ThrowException(ErrnoException(errno, "gethostname"));
0eb4c215 »
2011-01-18 Define winsock error numbers
60 #else // __MINGW32__
61 return ThrowException(ErrnoException(WSAGetLastError(), "gethostname"));
62 #endif // __MINGW32__
f1762ff8 »
2010-12-11 Add os module and getHostname
63 }
64
65 return scope.Close(String::New(s));
66 }
67
d75c338d »
2010-12-22 Add more functionality to the os module
68 static Handle<Value> GetOSType(const Arguments& args) {
69 HandleScope scope;
c3ffbf21 »
2011-01-02 Fix the OS module for win32
70
71 #ifdef __POSIX__
d75c338d »
2010-12-22 Add more functionality to the os module
72 char type[256];
8275d7cd »
2010-12-23 Fix Cygwin compatibility in the os module
73 struct utsname info;
d75c338d »
2010-12-22 Add more functionality to the os module
74
8275d7cd »
2010-12-23 Fix Cygwin compatibility in the os module
75 uname(&info);
76 strncpy(type, info.sysname, strlen(info.sysname));
77 type[strlen(info.sysname)] = 0;
d75c338d »
2010-12-22 Add more functionality to the os module
78
79 return scope.Close(String::New(type));
c3ffbf21 »
2011-01-02 Fix the OS module for win32
80 #else // __MINGW32__
81 return scope.Close(String::New("Windows_NT"));
82 #endif
d75c338d »
2010-12-22 Add more functionality to the os module
83 }
84
85 static Handle<Value> GetOSRelease(const Arguments& args) {
86 HandleScope scope;
87 char release[256];
c3ffbf21 »
2011-01-02 Fix the OS module for win32
88
89 #ifdef __POSIX__
8275d7cd »
2010-12-23 Fix Cygwin compatibility in the os module
90 struct utsname info;
d75c338d »
2010-12-22 Add more functionality to the os module
91
8275d7cd »
2010-12-23 Fix Cygwin compatibility in the os module
92 uname(&info);
93 strncpy(release, info.release, strlen(info.release));
94 release[strlen(info.release)] = 0;
d75c338d »
2010-12-22 Add more functionality to the os module
95
c3ffbf21 »
2011-01-02 Fix the OS module for win32
96 #else // __MINGW32__
97 OSVERSIONINFO info;
98 info.dwOSVersionInfoSize = sizeof(info);
99
100 if (GetVersionEx(&info) == 0) {
101 return Undefined();
102 }
103
c99962e4 »
2011-01-17 Misc. cleanups
104 sprintf(release, "%d.%d.%d", static_cast<int>(info.dwMajorVersion),
105 static_cast<int>(info.dwMinorVersion), static_cast<int>(info.dwBuildNumber));
c3ffbf21 »
2011-01-02 Fix the OS module for win32
106 #endif
107
d75c338d »
2010-12-22 Add more functionality to the os module
108 return scope.Close(String::New(release));
109 }
110
111 static Handle<Value> GetCPUInfo(const Arguments& args) {
112 HandleScope scope;
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
113 uv_cpu_info_t* cpu_infos;
114 int count, i;
d75c338d »
2010-12-22 Add more functionality to the os module
115
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
116 uv_err_t err = uv_cpu_info(&cpu_infos, &count);
117
118 if (err.code != UV_OK) {
d75c338d »
2010-12-22 Add more functionality to the os module
119 return Undefined();
120 }
121
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
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));
128 times_info->Set(String::New("nice"),
129 Integer::New(cpu_infos[i].cpu_times.nice));
130 times_info->Set(String::New("sys"),
131 Integer::New(cpu_infos[i].cpu_times.sys));
132 times_info->Set(String::New("idle"),
133 Integer::New(cpu_infos[i].cpu_times.idle));
134 times_info->Set(String::New("irq"),
135 Integer::New(cpu_infos[i].cpu_times.irq));
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"), Integer::New(cpu_infos[i].speed));
140 cpu_info->Set(String::New("times"), times_info);
141 (*cpus)->Set(i,cpu_info);
142 }
143
144 uv_free_cpu_info(cpu_infos, count);
145
d75c338d »
2010-12-22 Add more functionality to the os module
146 return scope.Close(cpus);
147 }
148
149 static Handle<Value> GetFreeMemory(const Arguments& args) {
150 HandleScope scope;
69845538 »
2011-10-07 use uv for memory and loadavg functions
151 double amount = uv_get_free_memory();
d75c338d »
2010-12-22 Add more functionality to the os module
152
153 if (amount < 0) {
154 return Undefined();
155 }
156
157 return scope.Close(Number::New(amount));
158 }
159
160 static Handle<Value> GetTotalMemory(const Arguments& args) {
161 HandleScope scope;
69845538 »
2011-10-07 use uv for memory and loadavg functions
162 double amount = uv_get_total_memory();
d75c338d »
2010-12-22 Add more functionality to the os module
163
164 if (amount < 0) {
165 return Undefined();
166 }
167
168 return scope.Close(Number::New(amount));
169 }
170
171 static Handle<Value> GetUptime(const Arguments& args) {
172 HandleScope scope;
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
173 double uptime;
d75c338d »
2010-12-22 Add more functionality to the os module
174
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
175 uv_err_t err = uv_uptime(&uptime);
176
177 if (err.code != UV_OK) {
d75c338d »
2010-12-22 Add more functionality to the os module
178 return Undefined();
179 }
180
181 return scope.Close(Number::New(uptime));
182 }
183
184 static Handle<Value> GetLoadAvg(const Arguments& args) {
185 HandleScope scope;
69845538 »
2011-10-07 use uv for memory and loadavg functions
186 double loadavg[3];
187 uv_loadavg(loadavg);
d75c338d »
2010-12-22 Add more functionality to the os module
188
69845538 »
2011-10-07 use uv for memory and loadavg functions
189 Local<Array> loads = Array::New(3);
190 loads->Set(0, Number::New(loadavg[0]));
191 loads->Set(1, Number::New(loadavg[1]));
192 loads->Set(2, Number::New(loadavg[2]));
193
d75c338d »
2010-12-22 Add more functionality to the os module
194 return scope.Close(loads);
195 }
196
19e53512 »
2011-03-16 os.getNetworkInterfaces()
197
198 static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
199 HandleScope scope;
200 uv_interface_address_t* interfaces;
201 int count, i;
202 char ip[INET6_ADDRSTRLEN];
203 Local<Object> ret, o;
204 Local<String> name, family;
205 Local<Array> ifarr;
206
207 uv_err_t err = uv_interface_addresses(&interfaces, &count);
208
03b00dcc »
2012-12-03 os: throw when os.networkInterfaces() fails
209 if (err.code != UV_OK)
210 return ThrowException(UVException(err.code, "uv_interface_addresses"));
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
211
212 ret = Object::New();
213
214 for (i = 0; i < count; i++) {
215 name = String::New(interfaces[i].name);
216 if (ret->Has(name)) {
217 ifarr = Local<Array>::Cast(ret->Get(name));
218 } else {
219 ifarr = Array::New();
220 ret->Set(name, ifarr);
221 }
222
223 if (interfaces[i].address.address4.sin_family == AF_INET) {
224 uv_ip4_name(&interfaces[i].address.address4,ip, sizeof(ip));
225 family = String::New("IPv4");
226 } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
227 uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
228 family = String::New("IPv6");
229 } else {
230 strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
231 family = String::New("<unknown>");
232 }
233
234 o = Object::New();
235 o->Set(String::New("address"), String::New(ip));
236 o->Set(String::New("family"), family);
237 o->Set(String::New("internal"), interfaces[i].is_internal ?
03b00dcc »
2012-12-03 os: throw when os.networkInterfaces() fails
238 True() : False());
500c8f45 »
2011-12-15 Remove platform files, and use uv platform api
239
240 ifarr->Set(ifarr->Length(), o);
241 }
242
243 uv_free_interface_addresses(interfaces, count);
244
245 return scope.Close(ret);
19e53512 »
2011-03-16 os.getNetworkInterfaces()
246 }
247
248
f1762ff8 »
2010-12-11 Add os module and getHostname
249 void OS::Initialize(v8::Handle<v8::Object> target) {
250 HandleScope scope;
251
5e4e87ad »
2012-11-08 os: add os.endianness() function
252 NODE_SET_METHOD(target, "getEndianness", GetEndianness);
f1762ff8 »
2010-12-11 Add os module and getHostname
253 NODE_SET_METHOD(target, "getHostname", GetHostname);
d75c338d »
2010-12-22 Add more functionality to the os module
254 NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg);
255 NODE_SET_METHOD(target, "getUptime", GetUptime);
256 NODE_SET_METHOD(target, "getTotalMem", GetTotalMemory);
257 NODE_SET_METHOD(target, "getFreeMem", GetFreeMemory);
258 NODE_SET_METHOD(target, "getCPUs", GetCPUInfo);
259 NODE_SET_METHOD(target, "getOSType", GetOSType);
260 NODE_SET_METHOD(target, "getOSRelease", GetOSRelease);
19e53512 »
2011-03-16 os.getNetworkInterfaces()
261 NODE_SET_METHOD(target, "getInterfaceAddresses", GetInterfaceAddresses);
f1762ff8 »
2010-12-11 Add os module and getHostname
262 }
263
264
265 } // namespace node
266
cdcb1118 »
2011-11-09 Remove stray NODE_MODULE() semi-colons.
267 NODE_MODULE(node_os, node::OS::Initialize)
Something went wrong with that request. Please try again.