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