Skip to content
Newer
Older
100644 210 lines (157 sloc) 5.13 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 @mscdex Add os module and getHostname
mscdex authored
22
23 #include <node.h>
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored
24 #include <node_os.h>
a7bdaab @bnoordhuis Include "platform.h", not <platform.h> - conflicts with system headers
bnoordhuis authored
25 #include "platform.h"
f1762ff @mscdex Add os module and getHostname
mscdex authored
26
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored
27 #include <v8.h>
d75c338 @mscdex Add more functionality to the os module
mscdex authored
28
f1762ff @mscdex Add os module and getHostname
mscdex authored
29 #include <errno.h>
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
30 #include <string.h>
86518c3 @piscisaureus Port GetHostName
piscisaureus authored
31
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored
32 #ifdef __MINGW32__
61af420 @piscisaureus Fix dns on windows
piscisaureus authored
33 # include <io.h>
34
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored
35 # include <platform_win32.h>
36 #endif
37
86518c3 @piscisaureus Port GetHostName
piscisaureus authored
38 #ifdef __POSIX__
33118df @piscisaureus Merge remote branch 'origin/master'
piscisaureus authored
39 # include <unistd.h> // gethostname, sysconf
40 # include <sys/utsname.h>
e0f47be @piscisaureus Clean up the way windows headers are included
piscisaureus authored
41 #endif
f1762ff @mscdex Add os module and getHostname
mscdex authored
42
43 namespace node {
44
45 using namespace v8;
46
47 static Handle<Value> GetHostname(const Arguments& args) {
48 HandleScope scope;
49 char s[255];
50 int r = gethostname(s, 255);
51
52 if (r < 0) {
0eb4c21 @piscisaureus Define winsock error numbers
piscisaureus authored
53 #ifdef __POSIX__
f1762ff @mscdex Add os module and getHostname
mscdex authored
54 return ThrowException(ErrnoException(errno, "gethostname"));
0eb4c21 @piscisaureus Define winsock error numbers
piscisaureus authored
55 #else // __MINGW32__
56 return ThrowException(ErrnoException(WSAGetLastError(), "gethostname"));
57 #endif // __MINGW32__
f1762ff @mscdex Add os module and getHostname
mscdex authored
58 }
59
60 return scope.Close(String::New(s));
61 }
62
d75c338 @mscdex Add more functionality to the os module
mscdex authored
63 static Handle<Value> GetOSType(const Arguments& args) {
64 HandleScope scope;
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored
65
66 #ifdef __POSIX__
d75c338 @mscdex Add more functionality to the os module
mscdex authored
67 char type[256];
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
68 struct utsname info;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
69
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
70 uname(&info);
71 strncpy(type, info.sysname, strlen(info.sysname));
72 type[strlen(info.sysname)] = 0;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
73
74 return scope.Close(String::New(type));
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored
75 #else // __MINGW32__
76 return scope.Close(String::New("Windows_NT"));
77 #endif
d75c338 @mscdex Add more functionality to the os module
mscdex authored
78 }
79
80 static Handle<Value> GetOSRelease(const Arguments& args) {
81 HandleScope scope;
82 char release[256];
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored
83
84 #ifdef __POSIX__
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
85 struct utsname info;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
86
8275d7c @mscdex Fix Cygwin compatibility in the os module
mscdex authored
87 uname(&info);
88 strncpy(release, info.release, strlen(info.release));
89 release[strlen(info.release)] = 0;
d75c338 @mscdex Add more functionality to the os module
mscdex authored
90
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored
91 #else // __MINGW32__
92 OSVERSIONINFO info;
93 info.dwOSVersionInfoSize = sizeof(info);
94
95 if (GetVersionEx(&info) == 0) {
96 return Undefined();
97 }
98
c99962e @piscisaureus Misc. cleanups
piscisaureus authored
99 sprintf(release, "%d.%d.%d", static_cast<int>(info.dwMajorVersion),
100 static_cast<int>(info.dwMinorVersion), static_cast<int>(info.dwBuildNumber));
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored
101 #endif
102
d75c338 @mscdex Add more functionality to the os module
mscdex authored
103 return scope.Close(String::New(release));
104 }
105
106 static Handle<Value> GetCPUInfo(const Arguments& args) {
107 HandleScope scope;
108 Local<Array> cpus;
109 int r = Platform::GetCPUInfo(&cpus);
110
111 if (r < 0) {
112 return Undefined();
113 }
114
115 return scope.Close(cpus);
116 }
117
118 static Handle<Value> GetFreeMemory(const Arguments& args) {
119 HandleScope scope;
6984553 @indutny use uv for memory and loadavg functions
indutny authored
120 double amount = uv_get_free_memory();
d75c338 @mscdex Add more functionality to the os module
mscdex authored
121
122 if (amount < 0) {
123 return Undefined();
124 }
125
126 return scope.Close(Number::New(amount));
127 }
128
129 static Handle<Value> GetTotalMemory(const Arguments& args) {
130 HandleScope scope;
6984553 @indutny use uv for memory and loadavg functions
indutny authored
131 double amount = uv_get_total_memory();
d75c338 @mscdex Add more functionality to the os module
mscdex authored
132
133 if (amount < 0) {
134 return Undefined();
135 }
136
137 return scope.Close(Number::New(amount));
138 }
139
140 static Handle<Value> GetUptime(const Arguments& args) {
141 HandleScope scope;
142 double uptime = Platform::GetUptime();
143
144 if (uptime < 0) {
145 return Undefined();
146 }
147
148 return scope.Close(Number::New(uptime));
149 }
150
151 static Handle<Value> GetLoadAvg(const Arguments& args) {
152 HandleScope scope;
6984553 @indutny use uv for memory and loadavg functions
indutny authored
153 double loadavg[3];
154 uv_loadavg(loadavg);
d75c338 @mscdex Add more functionality to the os module
mscdex authored
155
6984553 @indutny use uv for memory and loadavg functions
indutny authored
156 if (loadavg[0] < 0) {
d75c338 @mscdex Add more functionality to the os module
mscdex authored
157 return Undefined();
158 }
159
6984553 @indutny use uv for memory and loadavg functions
indutny authored
160 Local<Array> loads = Array::New(3);
161 loads->Set(0, Number::New(loadavg[0]));
162 loads->Set(1, Number::New(loadavg[1]));
163 loads->Set(2, Number::New(loadavg[2]));
164
d75c338 @mscdex Add more functionality to the os module
mscdex authored
165 return scope.Close(loads);
166 }
167
19e5351 @ry os.getNetworkInterfaces()
ry authored
168
169 static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
170 return Platform::GetInterfaceAddresses();
171 }
172
173
61af420 @piscisaureus Fix dns on windows
piscisaureus authored
174 #ifdef __MINGW32__
175 static Handle<Value> OpenOSHandle(const Arguments& args) {
176 HandleScope scope;
177
178 intptr_t handle = args[0]->IntegerValue();
179
180 int fd = _open_osfhandle(handle, 0);
181 if (fd < 0)
182 return ThrowException(ErrnoException(errno, "_open_osfhandle"));
183
184 return scope.Close(Integer::New(fd));
185 }
186 #endif // __MINGW32__
187
f1762ff @mscdex Add os module and getHostname
mscdex authored
188 void OS::Initialize(v8::Handle<v8::Object> target) {
189 HandleScope scope;
190
191 NODE_SET_METHOD(target, "getHostname", GetHostname);
d75c338 @mscdex Add more functionality to the os module
mscdex authored
192 NODE_SET_METHOD(target, "getLoadAvg", GetLoadAvg);
193 NODE_SET_METHOD(target, "getUptime", GetUptime);
194 NODE_SET_METHOD(target, "getTotalMem", GetTotalMemory);
195 NODE_SET_METHOD(target, "getFreeMem", GetFreeMemory);
196 NODE_SET_METHOD(target, "getCPUs", GetCPUInfo);
197 NODE_SET_METHOD(target, "getOSType", GetOSType);
198 NODE_SET_METHOD(target, "getOSRelease", GetOSRelease);
19e5351 @ry os.getNetworkInterfaces()
ry authored
199 NODE_SET_METHOD(target, "getInterfaceAddresses", GetInterfaceAddresses);
61af420 @piscisaureus Fix dns on windows
piscisaureus authored
200
201 #ifdef __MINGW32__
202 NODE_SET_METHOD(target, "openOSHandle", OpenOSHandle);
203 #endif
f1762ff @mscdex Add os module and getHostname
mscdex authored
204 }
205
206
207 } // namespace node
208
c3ffbf2 @piscisaureus Fix the OS module for win32
piscisaureus authored
209 NODE_MODULE(node_os, node::OS::Initialize);
Something went wrong with that request. Please try again.