Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Remove platform files, and use uv platform api
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Zinkovsky committed Dec 16, 2011
1 parent 88cc688 commit 500c8f4
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 1,952 deletions.
8 changes: 0 additions & 8 deletions node.gyp
Expand Up @@ -109,7 +109,6 @@
'src/node_string.h', 'src/node_string.h',
'src/node_version.h', 'src/node_version.h',
'src/pipe_wrap.h', 'src/pipe_wrap.h',
'src/platform.h',
'src/req_wrap.h', 'src/req_wrap.h',
'src/stream_wrap.h', 'src/stream_wrap.h',
'src/v8_typed_array.h', 'src/v8_typed_array.h',
Expand Down Expand Up @@ -152,9 +151,6 @@


[ 'OS=="win"', { [ 'OS=="win"', {
'sources': [ 'sources': [
'src/platform_win32.cc',
# headers to make for a more pleasant IDE experience
'src/platform_win32.h',
'tools/msvs/res/node.rc', 'tools/msvs/res/node.rc',
], ],
'defines': [ 'defines': [
Expand All @@ -172,25 +168,21 @@
] ]
}], }],
[ 'OS=="mac"', { [ 'OS=="mac"', {
'sources': [ 'src/platform_darwin.cc' ],
'libraries': [ '-framework Carbon' ], 'libraries': [ '-framework Carbon' ],
}], }],
[ 'OS=="linux"', { [ 'OS=="linux"', {
'sources': [ 'src/platform_linux.cc' ],
'libraries': [ 'libraries': [
'-ldl', '-ldl',
'-lutil' # needed for openpty '-lutil' # needed for openpty
], ],
}], }],
[ 'OS=="freebsd"', { [ 'OS=="freebsd"', {
'sources': [ 'src/platform_freebsd.cc' ],
'libraries': [ 'libraries': [
'-lutil', '-lutil',
'-lkvm', '-lkvm',
], ],
}], }],
[ 'OS=="solaris"', { [ 'OS=="solaris"', {
'sources': [ 'src/platform_sunos.cc' ],
'libraries': [ 'libraries': [
'-lkstat', '-lkstat',
], ],
Expand Down
59 changes: 42 additions & 17 deletions src/node.cc
Expand Up @@ -56,16 +56,11 @@ typedef int mode_t;
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>


#if defined(__MINGW32__) || defined(_MSC_VER)
# include <platform_win32.h> /* winapi_perror() */
#endif

#ifdef __POSIX__ #ifdef __POSIX__
# include <pwd.h> /* getpwnam() */ # include <pwd.h> /* getpwnam() */
# include <grp.h> /* getgrnam() */ # include <grp.h> /* getgrnam() */
#endif #endif


#include "platform.h"
#include <node_buffer.h> #include <node_buffer.h>
#ifdef __POSIX__ #ifdef __POSIX__
# include <node_io_watcher.h> # include <node_io_watcher.h>
Expand Down Expand Up @@ -137,7 +132,7 @@ extern char **environ;
#define module_load_list NODE_VAR(module_load_list) #define module_load_list NODE_VAR(module_load_list)
#define node_isolate NODE_VAR(node_isolate) #define node_isolate NODE_VAR(node_isolate)
#define debugger_running NODE_VAR(debugger_running) #define debugger_running NODE_VAR(debugger_running)

#define prog_start_time NODE_VAR(prog_start_time)


namespace node { namespace node {


Expand Down Expand Up @@ -854,6 +849,30 @@ Local<Value> UVException(int errorno,




#ifdef _WIN32 #ifdef _WIN32
// Does about the same as strerror(),
// but supports all windows error messages
static const char *winapi_strerror(const int errorno) {
char *errmsg = NULL;

FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&errmsg, 0, NULL);

if (errmsg) {
// Remove trailing newlines
for (int i = strlen(errmsg) - 1;
i >= 0 && (errmsg[i] == '\n' || errmsg[i] == '\r'); i--) {
errmsg[i] = '\0';
}

return errmsg;
} else {
// FormatMessage failed
return "Unknown error";
}
}


Local<Value> WinapiErrnoException(int errorno, Local<Value> WinapiErrnoException(int errorno,
const char* syscall, const char* syscall,
const char* msg, const char* msg,
Expand Down Expand Up @@ -1485,17 +1504,19 @@ static void CheckStatus(uv_timer_t* watcher, int status) {
} }
} }



static Handle<Value> Uptime(const Arguments& args) { static Handle<Value> Uptime(const Arguments& args) {
HandleScope scope; HandleScope scope;
assert(args.Length() == 0); assert(args.Length() == 0);
double uptime;


double uptime = Platform::GetUptime(true); uv_err_t err = uv_uptime(&uptime);


if (uptime < 0) { if (err.code != UV_OK) {
return Undefined(); return Undefined();
} }


return scope.Close(Number::New(uptime)); return scope.Close(Number::New(uptime - prog_start_time));
} }




Expand Down Expand Up @@ -1537,10 +1558,10 @@ v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {


size_t rss; size_t rss;


int r = Platform::GetMemory(&rss); uv_err_t err = uv_resident_set_memory(&rss);


if (r != 0) { if (err.code != UV_OK) {
return ThrowException(Exception::Error(String::New(strerror(errno)))); return ThrowException(UVException(err.code, "uv_resident_set_memory"));
} }


Local<Object> info = Object::New(); Local<Object> info = Object::New();
Expand Down Expand Up @@ -1829,9 +1850,9 @@ static Handle<Value> Binding(const Arguments& args) {
static Handle<Value> ProcessTitleGetter(Local<String> property, static Handle<Value> ProcessTitleGetter(Local<String> property,
const AccessorInfo& info) { const AccessorInfo& info) {
HandleScope scope; HandleScope scope;
int len; char buffer[512];
const char *s = Platform::GetProcessTitle(&len); uv_get_process_title(buffer, sizeof(buffer));
return scope.Close(s ? String::New(s, len) : String::Empty()); return scope.Close(String::New(buffer));
} }




Expand All @@ -1840,7 +1861,8 @@ static void ProcessTitleSetter(Local<String> property,
const AccessorInfo& info) { const AccessorInfo& info) {
HandleScope scope; HandleScope scope;
String::Utf8Value title(value->ToString()); String::Utf8Value title(value->ToString());
Platform::SetProcessTitle(*title); // TODO: protect with a lock
uv_set_process_title(*title);
} }




Expand Down Expand Up @@ -2490,8 +2512,11 @@ static Handle<Value> DebugProcess(const Arguments& args) {




char** Init(int argc, char *argv[]) { char** Init(int argc, char *argv[]) {
// Initialize prog_start_time to get relative uptime.
uv_uptime(&prog_start_time);

// Hack aroung with the argv pointer. Used for process.title = "blah". // Hack aroung with the argv pointer. Used for process.title = "blah".
argv = node::Platform::SetupArgs(argc, argv); argv = uv_setup_args(argc, argv);


// Parse a few arguments which are specific to Node. // Parse a few arguments which are specific to Node.
node::ParseArgs(argc, argv); node::ParseArgs(argc, argv);
Expand Down
4 changes: 0 additions & 4 deletions src/node_buffer.cc
Expand Up @@ -29,10 +29,6 @@
#include <stdlib.h> // malloc, free #include <stdlib.h> // malloc, free
#include <string.h> // memcpy #include <string.h> // memcpy


#ifdef __MINGW32__
# include "platform.h"
#endif

#ifdef __POSIX__ #ifdef __POSIX__
# include <arpa/inet.h> // htons, htonl # include <arpa/inet.h> // htons, htonl
#endif #endif
Expand Down
4 changes: 0 additions & 4 deletions src/node_constants.cc
Expand Up @@ -32,10 +32,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>


#if defined(__MINGW32__) || defined(_MSC_VER)
# include <platform_win32.h>
#endif

#if HAVE_OPENSSL #if HAVE_OPENSSL
# include <openssl/ssl.h> # include <openssl/ssl.h>
#endif #endif
Expand Down
1 change: 0 additions & 1 deletion src/node_file.cc
Expand Up @@ -37,7 +37,6 @@


#if defined(__MINGW32__) || defined(_MSC_VER) #if defined(__MINGW32__) || defined(_MSC_VER)
# include <io.h> # include <io.h>
# include <platform_win32.h>
#endif #endif




Expand Down
90 changes: 81 additions & 9 deletions src/node_os.cc
Expand Up @@ -22,7 +22,6 @@


#include <node.h> #include <node.h>
#include <node_os.h> #include <node_os.h>
#include "platform.h"


#include <v8.h> #include <v8.h>


Expand All @@ -31,8 +30,6 @@


#ifdef __MINGW32__ #ifdef __MINGW32__
# include <io.h> # include <io.h>

# include <platform_win32.h>
#endif #endif


#ifdef __POSIX__ #ifdef __POSIX__
Expand Down Expand Up @@ -105,13 +102,39 @@ static Handle<Value> GetOSRelease(const Arguments& args) {


static Handle<Value> GetCPUInfo(const Arguments& args) { static Handle<Value> GetCPUInfo(const Arguments& args) {
HandleScope scope; HandleScope scope;
Local<Array> cpus; uv_cpu_info_t* cpu_infos;
int r = Platform::GetCPUInfo(&cpus); int count, i;


if (r < 0) { uv_err_t err = uv_cpu_info(&cpu_infos, &count);

if (err.code != UV_OK) {
return Undefined(); return Undefined();
} }


Local<Array> cpus = Array::New();

for (i = 0; i < count; i++) {
Local<Object> times_info = Object::New();
times_info->Set(String::New("user"),
Integer::New(cpu_infos[i].cpu_times.user));
times_info->Set(String::New("nice"),
Integer::New(cpu_infos[i].cpu_times.nice));
times_info->Set(String::New("sys"),
Integer::New(cpu_infos[i].cpu_times.sys));
times_info->Set(String::New("idle"),
Integer::New(cpu_infos[i].cpu_times.idle));
times_info->Set(String::New("irq"),
Integer::New(cpu_infos[i].cpu_times.irq));

Local<Object> cpu_info = Object::New();
cpu_info->Set(String::New("model"), String::New(cpu_infos[i].model));
cpu_info->Set(String::New("speed"), Integer::New(cpu_infos[i].speed));
cpu_info->Set(String::New("times"), times_info);
(*cpus)->Set(i,cpu_info);
}

uv_free_cpu_info(cpu_infos, count);

return scope.Close(cpus); return scope.Close(cpus);
} }


Expand Down Expand Up @@ -139,9 +162,11 @@ static Handle<Value> GetTotalMemory(const Arguments& args) {


static Handle<Value> GetUptime(const Arguments& args) { static Handle<Value> GetUptime(const Arguments& args) {
HandleScope scope; HandleScope scope;
double uptime = Platform::GetUptime(); double uptime;


if (uptime < 0) { uv_err_t err = uv_uptime(&uptime);

if (err.code != UV_OK) {
return Undefined(); return Undefined();
} }


Expand All @@ -163,7 +188,54 @@ static Handle<Value> GetLoadAvg(const Arguments& args) {




static Handle<Value> GetInterfaceAddresses(const Arguments& args) { static Handle<Value> GetInterfaceAddresses(const Arguments& args) {
return Platform::GetInterfaceAddresses(); HandleScope scope;
uv_interface_address_t* interfaces;
int count, i;
char ip[INET6_ADDRSTRLEN];
Local<Object> ret, o;
Local<String> name, family;
Local<Array> ifarr;

uv_err_t err = uv_interface_addresses(&interfaces, &count);

if (err.code != UV_OK) {
return Undefined();
}

ret = Object::New();

for (i = 0; i < count; i++) {
name = String::New(interfaces[i].name);
if (ret->Has(name)) {
ifarr = Local<Array>::Cast(ret->Get(name));
} else {
ifarr = Array::New();
ret->Set(name, ifarr);
}

if (interfaces[i].address.address4.sin_family == AF_INET) {
uv_ip4_name(&interfaces[i].address.address4,ip, sizeof(ip));
family = String::New("IPv4");
} else if (interfaces[i].address.address4.sin_family == AF_INET6) {
uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
family = String::New("IPv6");
} else {
strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
family = String::New("<unknown>");
}

o = Object::New();
o->Set(String::New("address"), String::New(ip));
o->Set(String::New("family"), family);
o->Set(String::New("internal"), interfaces[i].is_internal ?
True() : False());

ifarr->Set(ifarr->Length(), o);
}

uv_free_interface_addresses(interfaces, count);

return scope.Close(ret);
} }




Expand Down
10 changes: 1 addition & 9 deletions src/node_vars.h
Expand Up @@ -70,8 +70,7 @@ struct globals {
v8::Persistent<v8::Array> module_load_list; v8::Persistent<v8::Array> module_load_list;
v8::Isolate* node_isolate; v8::Isolate* node_isolate;
volatile bool debugger_running; volatile bool debugger_running;

double prog_start_time;



// stream_wrap.cc // stream_wrap.cc
size_t slab_used; size_t slab_used;
Expand Down Expand Up @@ -176,13 +175,6 @@ struct globals {
v8::Persistent<v8::String> write_sym; v8::Persistent<v8::String> write_sym;
v8::Persistent<v8::FunctionTemplate> buffer_constructor_template; v8::Persistent<v8::FunctionTemplate> buffer_constructor_template;


// platform*.cc
char* process_title;
struct {
char *str;
size_t len;
} linux_process_title;

// node_signal_watcher.cc // node_signal_watcher.cc
v8::Persistent<v8::String> callback_symbol; v8::Persistent<v8::String> callback_symbol;
v8::Persistent<v8::FunctionTemplate> signal_watcher_constructor_template; v8::Persistent<v8::FunctionTemplate> signal_watcher_constructor_template;
Expand Down

0 comments on commit 500c8f4

Please sign in to comment.