Skip to content
Newer
Older
100644 260 lines (199 sloc) 6.81 KB
55048cd @ry Update copyright headers
ry authored Mar 10, 2011
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 @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
22
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored Mar 10, 2012
23 #include "node.h"
24 #include "node_os.h"
f1762ff @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
25
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored Mar 9, 2012
26 #include "v8.h"
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
27
f1762ff @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
28 #include <errno.h>
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored Dec 23, 2010
29 #include <string.h>
86518c3 @piscisaureus Port GetHostName
piscisaureus authored Dec 21, 2010
30
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored Jan 18, 2011
31 #ifdef __MINGW32__
61af420 @piscisaureus Fix dns on windows
piscisaureus authored Feb 7, 2011
32 # include <io.h>
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored Jan 17, 2011
33 #endif
34
86518c3 @piscisaureus Port GetHostName
piscisaureus authored Dec 20, 2010
35 #ifdef __POSIX__
33118df @piscisaureus Merge remote branch 'origin/master'
piscisaureus authored Jan 2, 2011
36 # include <unistd.h> // gethostname, sysconf
37 # include <sys/utsname.h>
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored Jan 17, 2011
38 #endif
f1762ff @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
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) {
0eb4c21 @piscisaureus Define winsock error numbers
piscisaureus authored Jan 18, 2011
50 #ifdef __POSIX__
f1762ff @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
51 return ThrowException(ErrnoException(errno, "gethostname"));
0eb4c21 @piscisaureus Define winsock error numbers
piscisaureus authored Jan 17, 2011
52 #else // __MINGW32__
53 return ThrowException(ErrnoException(WSAGetLastError(), "gethostname"));
54 #endif // __MINGW32__
f1762ff @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
55 }
56
57 return scope.Close(String::New(s));
58 }
59
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
60 static Handle<Value> GetOSType(const Arguments& args) {
61 HandleScope scope;
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored Jan 2, 2011
62
63 #ifdef __POSIX__
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
64 char type[256];
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored Dec 23, 2010
65 struct utsname info;
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
66
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored Dec 23, 2010
67 uname(&info);
68 strncpy(type, info.sysname, strlen(info.sysname));
69 type[strlen(info.sysname)] = 0;
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
70
71 return scope.Close(String::New(type));
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored Jan 2, 2011
72 #else // __MINGW32__
73 return scope.Close(String::New("Windows_NT"));
74 #endif
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
75 }
76
77 static Handle<Value> GetOSRelease(const Arguments& args) {
78 HandleScope scope;
79 char release[256];
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored Jan 2, 2011
80
81 #ifdef __POSIX__
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored Dec 23, 2010
82 struct utsname info;
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
83
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored Dec 23, 2010
84 uname(&info);
85 strncpy(release, info.release, strlen(info.release));
86 release[strlen(info.release)] = 0;
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
87
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored Jan 2, 2011
88 #else // __MINGW32__
89 OSVERSIONINFO info;
90 info.dwOSVersionInfoSize = sizeof(info);
91
92 if (GetVersionEx(&info) == 0) {
93 return Undefined();
94 }
95
c99962e @piscisaureus Misc. cleanups
piscisaureus authored Jan 17, 2011
96 sprintf(release, "%d.%d.%d", static_cast<int>(info.dwMajorVersion),
97 static_cast<int>(info.dwMinorVersion), static_cast<int>(info.dwBuildNumber));
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored Jan 2, 2011
98 #endif
99
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
100 return scope.Close(String::New(release));
101 }
102
103 static Handle<Value> GetCPUInfo(const Arguments& args) {
104 HandleScope scope;
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored Dec 15, 2011
105 uv_cpu_info_t* cpu_infos;
106 int count, i;
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
107
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored Dec 15, 2011
108 uv_err_t err = uv_cpu_info(&cpu_infos, &count);
109
110 if (err.code != UV_OK) {
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
111 return Undefined();
112 }
113
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored Dec 15, 2011
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"),
119 Integer::New(cpu_infos[i].cpu_times.user));
120 times_info->Set(String::New("nice"),
121 Integer::New(cpu_infos[i].cpu_times.nice));
122 times_info->Set(String::New("sys"),
123 Integer::New(cpu_infos[i].cpu_times.sys));
124 times_info->Set(String::New("idle"),
125 Integer::New(cpu_infos[i].cpu_times.idle));
126 times_info->Set(String::New("irq"),
127 Integer::New(cpu_infos[i].cpu_times.irq));
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
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
138 return scope.Close(cpus);
139 }
140
141 static Handle<Value> GetFreeMemory(const Arguments& args) {
142 HandleScope scope;
6984553 @indutny use uv for memory and loadavg functions
indutny authored Oct 7, 2011
143 double amount = uv_get_free_memory();
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
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;
6984553 @indutny use uv for memory and loadavg functions
indutny authored Oct 7, 2011
154 double amount = uv_get_total_memory();
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
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;
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored Dec 15, 2011
165 double uptime;
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
166
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored Dec 15, 2011
167 uv_err_t err = uv_uptime(&uptime);
168
169 if (err.code != UV_OK) {
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
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;
6984553 @indutny use uv for memory and loadavg functions
indutny authored Oct 7, 2011
178 double loadavg[3];
179 uv_loadavg(loadavg);
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
180
6984553 @indutny use uv for memory and loadavg functions
indutny authored Oct 7, 2011
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
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
186 return scope.Close(loads);
187 }
188
19e5351 @ry os.getNetworkInterfaces()
ry authored Mar 16, 2011
189
190 static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
500c8f4 Remove platform files, and use uv platform api
Igor Zinkovsky authored Dec 15, 2011
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);
19e5351 @ry os.getNetworkInterfaces()
ry authored Mar 16, 2011
239 }
240
241
f1762ff @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
242 void OS::Initialize(v8::Handle<v8::Object> target) {
243 HandleScope scope;
244
245 NODE_SET_METHOD(target, "getHostname", GetHostname);
d75c338 @mscdex Add more functionality to the os module
mscdex authored Dec 22, 2010
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);
19e5351 @ry os.getNetworkInterfaces()
ry authored Mar 16, 2011
253 NODE_SET_METHOD(target, "getInterfaceAddresses", GetInterfaceAddresses);
f1762ff @mscdex Add os module and getHostname
mscdex authored Dec 11, 2010
254 }
255
256
257 } // namespace node
258
cdcb111 @bnoordhuis Remove stray NODE_MODULE() semi-colons.
bnoordhuis authored Nov 9, 2011
259 NODE_MODULE(node_os, node::OS::Initialize)
Something went wrong with that request. Please try again.