1 // Copyright 2009 Ryan Dahl <ry@tinyclouds.org>
9 #include <limits.h> /* PATH_MAX */
13 #include <dlfcn.h> /* dlopen(), dlsym() */
14 #include <sys/types.h>
15 #include <unistd.h> /* setuid, getuid */
17 #include <node_buffer.h>
18 #include <node_io_watcher.h>
19 #include <node_net2.h>
20 #include <node_events.h>
21 #include <node_cares.h>
22 #include <node_file.h>
25 # include <node_idle_watcher.h>
27 #include <node_http_parser.h>
28 #include <node_signal_watcher.h>
29 #include <node_stat_watcher.h>
30 #include <node_timer.h>
31 #include <node_child_process.h>
32 #include <node_constants.h>
33 #include <node_stdio.h>
34 #include <node_natives.h>
35 #include <node_version.h>
37 #include <node_crypto.h>
39 #include <node_script.h>
45 extern char **environ;
49 static Persistent<Object> process;
51 static Persistent<String> errno_symbol;
52 static Persistent<String> syscall_symbol;
54 static Persistent<String> dev_symbol;
55 static Persistent<String> ino_symbol;
56 static Persistent<String> mode_symbol;
57 static Persistent<String> nlink_symbol;
58 static Persistent<String> uid_symbol;
59 static Persistent<String> gid_symbol;
60 static Persistent<String> rdev_symbol;
61 static Persistent<String> size_symbol;
62 static Persistent<String> blksize_symbol;
63 static Persistent<String> blocks_symbol;
64 static Persistent<String> atime_symbol;
65 static Persistent<String> mtime_symbol;
66 static Persistent<String> ctime_symbol;
68 static Persistent<String> rss_symbol;
69 static Persistent<String> vsize_symbol;
70 static Persistent<String> heap_total_symbol;
71 static Persistent<String> heap_used_symbol;
73 static Persistent<String> listeners_symbol;
74 static Persistent<String> uncaught_exception_symbol;
75 static Persistent<String> emit_symbol;
77 static int option_end_index = 0;
78 static bool use_debug_agent = false;
79 static bool debug_wait_connect = false;
80 static int debug_port=5858;
82 static ev_prepare next_tick_watcher;
83 static ev_idle tick_spinner;
84 static bool need_tick_cb;
85 static Persistent<String> tick_callback_sym;
87 static ev_async eio_want_poll_notifier;
88 static ev_async eio_done_poll_notifier;
89 static ev_idle eio_poller;
91 // We need to notify V8 when we're idle so that it can run the garbage
92 // collector. The interface to this is V8::IdleNotification(). It returns
93 // true if the heap hasn't be fully compacted, and needs to be run again.
94 // Returning false means that it doesn't have anymore work to do.
96 // A rather convoluted algorithm has been devised to determine when Node is
97 // idle. You'll have to figure it out for yourself.
98 static ev_check gc_check;
99 static ev_idle gc_idle;
100 static ev_timer gc_timer;
104 #define FAST_TICK 0.7
105 #define GC_WAIT_TIME 5.
106 #define RPM_SAMPLES 100
107 #define TICK_TIME(n) tick_times[(tick_time_head - (n)) % RPM_SAMPLES]
108 static ev_tstamp tick_times[RPM_SAMPLES];
109 static int tick_time_head;
111 static void StartGCTimer () {
112 if (!ev_is_active(&gc_timer)) {
113 ev_timer_start(EV_DEFAULT_UC_ &gc_timer);
114 ev_unref(EV_DEFAULT_UC);
118 static void StopGCTimer () {
119 if (ev_is_active(&gc_timer)) {
120 ev_ref(EV_DEFAULT_UC);
121 ev_timer_stop(&gc_timer);
125 static void Idle(EV_P_ ev_idle *watcher, int revents) {
126 assert(watcher == &gc_idle);
127 assert(revents == EV_IDLE);
129 //fprintf(stderr, "idle\n");
131 if (V8::IdleNotification()) {
132 ev_idle_stop(EV_A_ watcher);
138 // Called directly after every call to select() (or epoll, or whatever)
139 static void Check(EV_P_ ev_check *watcher, int revents) {
140 assert(watcher == &gc_check);
141 assert(revents == EV_CHECK);
143 tick_times[tick_time_head] = ev_now();
144 tick_time_head = (tick_time_head + 1) % RPM_SAMPLES;
148 for (int i = 0; i < (int)(GC_WAIT_TIME/FAST_TICK); i++) {
149 double d = TICK_TIME(i+1) - TICK_TIME(i+2);
150 //printf("d = %f\n", d);
151 // If in the last 5 ticks the difference between
152 // ticks was less than 0.7 seconds, then continue.
159 // Otherwise start the gc!
161 //fprintf(stderr, "start idle 2\n");
162 ev_idle_start(EV_A_ &gc_idle);
166 static Handle<Value> NeedTickCallback(const Arguments& args) {
169 ev_idle_start(EV_DEFAULT_UC_ &tick_spinner);
174 static void Spin(EV_P_ ev_idle *watcher, int revents) {
175 assert(watcher == &tick_spinner);
176 assert(revents == EV_IDLE);
180 static void Tick(EV_P_ ev_prepare *watcher, int revents) {
181 assert(watcher == &next_tick_watcher);
182 assert(revents == EV_PREPARE);
184 // Avoid entering a V8 scope.
185 if (!need_tick_cb) return;
187 need_tick_cb = false;
188 ev_idle_stop(EV_DEFAULT_UC_ &tick_spinner);
192 if (tick_callback_sym.IsEmpty()) {
193 // Lazily set the symbol
195 Persistent<String>::New(String::NewSymbol("_tickCallback"));
198 Local<Value> cb_v = process->Get(tick_callback_sym);
199 if (!cb_v->IsFunction()) return;
200 Local<Function> cb = Local<Function>::Cast(cb_v);
204 cb->Call(process, 0, NULL);
206 if (try_catch.HasCaught()) {
207 FatalException(try_catch);
212 static void DoPoll(EV_P_ ev_idle *watcher, int revents) {
213 assert(watcher == &eio_poller);
214 assert(revents == EV_IDLE);
216 //printf("eio_poller\n");
218 if (eio_poll() != -1) {
219 //printf("eio_poller stop\n");
220 ev_idle_stop(EV_DEFAULT_UC_ watcher);
225 // Called from the main thread.
226 static void WantPollNotifier(EV_P_ ev_async *watcher, int revents) {
227 assert(watcher == &eio_want_poll_notifier);
228 assert(revents == EV_ASYNC);
230 //printf("want poll notifier\n");
232 if (eio_poll() == -1) {
233 //printf("eio_poller start\n");
234 ev_idle_start(EV_DEFAULT_UC_ &eio_poller);
239 static void DonePollNotifier(EV_P_ ev_async *watcher, int revents) {
240 assert(watcher == &eio_done_poll_notifier);
241 assert(revents == EV_ASYNC);
243 //printf("done poll notifier\n");
245 if (eio_poll() != -1) {
246 //printf("eio_poller stop\n");
247 ev_idle_stop(EV_DEFAULT_UC_ &eio_poller);
252 // EIOWantPoll() is called from the EIO thread pool each time an EIO
253 // request (that is, one of the node.fs.* functions) has completed.
254 static void EIOWantPoll(void) {
255 // Signal the main thread that eio_poll need to be processed.
256 ev_async_send(EV_DEFAULT_UC_ &eio_want_poll_notifier);
260 static void EIODonePoll(void) {
261 // Signal the main thread that we should stop calling eio_poll().
262 // from the idle watcher.
263 ev_async_send(EV_DEFAULT_UC_ &eio_done_poll_notifier);
267 static inline const char *errno_string(int errorno) {
268 #define ERRNO_CASE(e) case e: return #e;
276 ERRNO_CASE(EADDRINUSE);
280 ERRNO_CASE(EADDRNOTAVAIL);
284 ERRNO_CASE(EAFNOSUPPORT);
292 # if EAGAIN != EWOULDBLOCK
293 ERRNO_CASE(EWOULDBLOCK);
298 ERRNO_CASE(EALREADY);
314 ERRNO_CASE(ECANCELED);
322 ERRNO_CASE(ECONNABORTED);
326 ERRNO_CASE(ECONNREFUSED);
330 ERRNO_CASE(ECONNRESET);
338 ERRNO_CASE(EDESTADDRREQ);
362 ERRNO_CASE(EHOSTUNREACH);
374 ERRNO_CASE(EINPROGRESS);
410 ERRNO_CASE(EMSGSIZE);
414 ERRNO_CASE(EMULTIHOP);
418 ERRNO_CASE(ENAMETOOLONG);
422 ERRNO_CASE(ENETDOWN);
426 ERRNO_CASE(ENETRESET);
430 ERRNO_CASE(ENETUNREACH);
462 # if ENOLINK != ENOLCK
476 ERRNO_CASE(ENOPROTOOPT);
496 ERRNO_CASE(ENOTCONN);
504 ERRNO_CASE(ENOTEMPTY);
508 ERRNO_CASE(ENOTSOCK);
515 ERRNO_CASE(EOPNOTSUPP);
529 ERRNO_CASE(EOVERFLOW);
544 #ifdef EPROTONOSUPPORT
545 ERRNO_CASE(EPROTONOSUPPORT);
549 ERRNO_CASE(EPROTOTYPE);
577 ERRNO_CASE(ETIMEDOUT);
592 const char *signo_string(int signo) {
593 #define SIGNO_CASE(e) case e: return #e;
621 # if SIGABRT != SIGIOT
662 SIGNO_CASE(SIGSTKFLT);
699 SIGNO_CASE(SIGVTALRM);
707 SIGNO_CASE(SIGWINCH);
715 # if SIGPOLL != SIGIO
737 Local<Value> ErrnoException(int errorno,
740 Local<String> estring = String::NewSymbol(errno_string(errorno));
741 if (!msg[0]) msg = strerror(errorno);
742 Local<String> message = String::NewSymbol(msg);
744 Local<String> cons1 = String::Concat(estring, String::NewSymbol(", "));
745 Local<String> cons2 = String::Concat(cons1, message);
747 Local<Value> e = Exception::Error(cons2);
749 Local<Object> obj = e->ToObject();
751 if (errno_symbol.IsEmpty()) {
752 syscall_symbol = NODE_PSYMBOL("syscall");
753 errno_symbol = NODE_PSYMBOL("errno");
756 obj->Set(errno_symbol, Integer::New(errorno));
757 if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall));
762 enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
765 if (!encoding_v->IsString()) return _default;
767 String::Utf8Value encoding(encoding_v->ToString());
769 if (strcasecmp(*encoding, "utf8") == 0) {
771 } else if (strcasecmp(*encoding, "utf-8") == 0) {
773 } else if (strcasecmp(*encoding, "ascii") == 0) {
775 } else if (strcasecmp(*encoding, "binary") == 0) {
777 } else if (strcasecmp(*encoding, "raw") == 0) {
778 fprintf(stderr, "'raw' (array of integers) has been removed. "
781 } else if (strcasecmp(*encoding, "raws") == 0) {
782 fprintf(stderr, "'raws' encoding has been renamed to 'binary'. "
783 "Please update your code.\n");
790 Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
793 if (!len) return scope.Close(String::Empty());
795 if (encoding == BINARY) {
796 const unsigned char *cbuf = static_cast<const unsigned char*>(buf);
797 uint16_t * twobytebuf = new uint16_t[len];
798 for (size_t i = 0; i < len; i++) {
799 // XXX is the following line platform independent?
800 twobytebuf[i] = cbuf[i];
802 Local<String> chunk = String::New(twobytebuf, len);
803 delete [] twobytebuf; // TODO use ExternalTwoByteString?
804 return scope.Close(chunk);
807 // utf8 or ascii encoding
808 Local<String> chunk = String::New((const char*)buf, len);
809 return scope.Close(chunk);
812 // Returns -1 if the handle was not valid for decoding
813 ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
816 if (val->IsArray()) {
817 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
823 Local<String> str = val->ToString();
825 if (encoding == UTF8) return str->Utf8Length();
827 return str->Length();
831 # define MIN(a, b) ((a) < (b) ? (a) : (b))
834 // Returns number of bytes written.
835 ssize_t DecodeWrite(char *buf,
837 v8::Handle<v8::Value> val,
838 enum encoding encoding) {
842 // A lot of improvement can be made here. See:
843 // http://code.google.com/p/v8/issues/detail?id=270
844 // http://groups.google.com/group/v8-dev/browse_thread/thread/dba28a81d9215291/ece2b50a3b4022c
845 // http://groups.google.com/group/v8-users/browse_thread/thread/1f83b0ba1f0a611
847 if (val->IsArray()) {
848 fprintf(stderr, "'raw' encoding (array of integers) has been removed. "
854 Local<String> str = val->ToString();
856 if (encoding == UTF8) {
857 str->WriteUtf8(buf, buflen, NULL, String::HINT_MANY_WRITES_EXPECTED);
861 if (encoding == ASCII) {
862 str->WriteAscii(buf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED);
866 // THIS IS AWFUL!!! FIXME
868 assert(encoding == BINARY);
870 uint16_t * twobytebuf = new uint16_t[buflen];
872 str->Write(twobytebuf, 0, buflen, String::HINT_MANY_WRITES_EXPECTED);
874 for (size_t i = 0; i < buflen; i++) {
875 unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]);
880 delete [] twobytebuf;
885 static Persistent<FunctionTemplate> stats_constructor_template;
887 Local<Object> BuildStatsObject(struct stat * s) {
890 if (dev_symbol.IsEmpty()) {
891 dev_symbol = NODE_PSYMBOL("dev");
892 ino_symbol = NODE_PSYMBOL("ino");
893 mode_symbol = NODE_PSYMBOL("mode");
894 nlink_symbol = NODE_PSYMBOL("nlink");
895 uid_symbol = NODE_PSYMBOL("uid");
896 gid_symbol = NODE_PSYMBOL("gid");
897 rdev_symbol = NODE_PSYMBOL("rdev");
898 size_symbol = NODE_PSYMBOL("size");
899 blksize_symbol = NODE_PSYMBOL("blksize");
900 blocks_symbol = NODE_PSYMBOL("blocks");
901 atime_symbol = NODE_PSYMBOL("atime");
902 mtime_symbol = NODE_PSYMBOL("mtime");
903 ctime_symbol = NODE_PSYMBOL("ctime");
906 Local<Object> stats =
907 stats_constructor_template->GetFunction()->NewInstance();
909 /* ID of device containing file */
910 stats->Set(dev_symbol, Integer::New(s->st_dev));
913 stats->Set(ino_symbol, Integer::New(s->st_ino));
916 stats->Set(mode_symbol, Integer::New(s->st_mode));
918 /* number of hard links */
919 stats->Set(nlink_symbol, Integer::New(s->st_nlink));
921 /* user ID of owner */
922 stats->Set(uid_symbol, Integer::New(s->st_uid));
924 /* group ID of owner */
925 stats->Set(gid_symbol, Integer::New(s->st_gid));
927 /* device ID (if special file) */
928 stats->Set(rdev_symbol, Integer::New(s->st_rdev));
930 /* total size, in bytes */
931 stats->Set(size_symbol, Number::New(s->st_size));
933 /* blocksize for filesystem I/O */
934 stats->Set(blksize_symbol, Integer::New(s->st_blksize));
936 /* number of blocks allocated */
937 stats->Set(blocks_symbol, Integer::New(s->st_blocks));
939 /* time of last access */
940 stats->Set(atime_symbol, NODE_UNIXTIME_V8(s->st_atime));
942 /* time of last modification */
943 stats->Set(mtime_symbol, NODE_UNIXTIME_V8(s->st_mtime));
945 /* time of last status change */
946 stats->Set(ctime_symbol, NODE_UNIXTIME_V8(s->st_ctime));
948 return scope.Close(stats);
952 // Extracts a C str from a V8 Utf8Value.
953 const char* ToCString(const v8::String::Utf8Value& value) {
954 return *value ? *value : "<str conversion failed>";
957 static void ReportException(TryCatch &try_catch, bool show_line) {
958 Handle<Message> message = try_catch.Message();
960 Handle<Value> error = try_catch.Exception();
961 Handle<String> stack;
963 if (error->IsObject()) {
964 Handle<Object> obj = Handle<Object>::Cast(error);
965 Handle<Value> raw_stack = obj->Get(String::New("stack"));
966 if (raw_stack->IsString()) stack = Handle<String>::Cast(raw_stack);
969 if (show_line && !message.IsEmpty()) {
970 // Print (filename):(line number): (message).
971 String::Utf8Value filename(message->GetScriptResourceName());
972 const char* filename_string = ToCString(filename);
973 int linenum = message->GetLineNumber();
974 fprintf(stderr, "%s:%i\n", filename_string, linenum);
975 // Print line of source code.
976 String::Utf8Value sourceline(message->GetSourceLine());
977 const char* sourceline_string = ToCString(sourceline);
983 // Because of how CommonJS modules work, all scripts are wrapped with a
984 // "function (function (exports, __filename, ...) {"
985 // to provide script local variables.
987 // When reporting errors on the first line of a script, this wrapper
988 // function is leaked to the user. This HACK is to remove it. The length
989 // of the wrapper is 62. That wrapper is defined in lib/module.js
991 // If that wrapper is ever changed, then this number also has to be
992 // updated. Or - someone could clean this up so that the two peices
993 // don't need to be changed.
995 // Even better would be to get support into V8 for wrappers that
996 // shouldn't be reported to users.
997 int offset = linenum == 1 ? 62 : 0;
999 fprintf(stderr, "%s\n", sourceline_string + offset);
1000 // Print wavy underline (GetUnderline is deprecated).
1001 int start = message->GetStartColumn();
1002 for (int i = offset; i < start; i++) {
1003 fprintf(stderr, " ");
1005 int end = message->GetEndColumn();
1006 for (int i = start; i < end; i++) {
1007 fprintf(stderr, "^");
1009 fprintf(stderr, "\n");
1012 if (stack.IsEmpty()) {
1013 message->PrintCurrentStackTrace(stderr);
1015 String::Utf8Value trace(stack);
1016 fprintf(stderr, "%s\n", *trace);
1021 // Executes a str within the current v8 context.
1022 Local<Value> ExecuteString(Local<String> source, Local<Value> filename) {
1026 Local<v8::Script> script = v8::Script::Compile(source, filename);
1027 if (script.IsEmpty()) {
1028 ReportException(try_catch, true);
1032 Local<Value> result = script->Run();
1033 if (result.IsEmpty()) {
1034 ReportException(try_catch, true);
1038 return scope.Close(result);
1041 static Handle<Value> ByteLength(const Arguments& args) {
1044 if (args.Length() < 1 || !args[0]->IsString()) {
1045 return ThrowException(Exception::Error(String::New("Bad argument.")));
1048 Local<Integer> length = Integer::New(DecodeBytes(args[0], ParseEncoding(args[1], UTF8)));
1050 return scope.Close(length);
1053 static Handle<Value> Loop(const Arguments& args) {
1055 assert(args.Length() == 0);
1057 // TODO Probably don't need to start this each time.
1058 // Avoids failing on test/mjsunit/test-eio-race3.js though
1059 ev_idle_start(EV_DEFAULT_UC_ &eio_poller);
1061 ev_loop(EV_DEFAULT_UC_ 0);
1065 static Handle<Value> Unloop(const Arguments& args) {
1066 fprintf(stderr, "Deprecation: Don't use process.unloop(). It will be removed soon.\n");
1068 int how = EVUNLOOP_ONE;
1069 if (args[0]->IsString()) {
1070 String::Utf8Value how_s(args[0]->ToString());
1071 if (0 == strcmp(*how_s, "all")) {
1075 ev_unloop(EV_DEFAULT_ how);
1079 static Handle<Value> Chdir(const Arguments& args) {
1082 if (args.Length() != 1 || !args[0]->IsString()) {
1083 return ThrowException(Exception::Error(String::New("Bad argument.")));
1086 String::Utf8Value path(args[0]->ToString());
1088 int r = chdir(*path);
1091 return ThrowException(Exception::Error(String::New(strerror(errno))));
1097 static Handle<Value> Cwd(const Arguments& args) {
1099 assert(args.Length() == 0);
1101 char output[PATH_MAX];
1102 char *r = getcwd(output, PATH_MAX);
1104 return ThrowException(Exception::Error(String::New(strerror(errno))));
1106 Local<String> cwd = String::New(output);
1108 return scope.Close(cwd);
1111 static Handle<Value> Umask(const Arguments& args){
1114 if(args.Length() < 1) {
1118 else if(!args[0]->IsInt32()) {
1119 return ThrowException(Exception::TypeError(
1120 String::New("argument must be an integer.")));
1123 old = umask((mode_t)args[0]->Uint32Value());
1125 return scope.Close(Uint32::New(old));
1129 static Handle<Value> GetUid(const Arguments& args) {
1131 assert(args.Length() == 0);
1133 return scope.Close(Integer::New(uid));
1136 static Handle<Value> GetGid(const Arguments& args) {
1138 assert(args.Length() == 0);
1140 return scope.Close(Integer::New(gid));
1144 static Handle<Value> SetGid(const Arguments& args) {
1147 if (args.Length() < 1) {
1148 return ThrowException(Exception::Error(
1149 String::New("setgid requires 1 argument")));
1152 Local<Integer> given_gid = args[0]->ToInteger();
1153 int gid = given_gid->Int32Value();
1155 if ((result = setgid(gid)) != 0) {
1156 return ThrowException(Exception::Error(String::New(strerror(errno))));
1161 static Handle<Value> SetUid(const Arguments& args) {
1164 if (args.Length() < 1) {
1165 return ThrowException(Exception::Error(
1166 String::New("setuid requires 1 argument")));
1169 Local<Integer> given_uid = args[0]->ToInteger();
1170 int uid = given_uid->Int32Value();
1172 if ((result = setuid(uid)) != 0) {
1173 return ThrowException(Exception::Error(String::New(strerror(errno))));
1179 v8::Handle<v8::Value> Exit(const v8::Arguments& args) {
1183 exit(args[0]->IntegerValue());
1188 #define HAVE_GETMEM 1
1189 #include <unistd.h> /* getpagesize() */
1191 #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
1192 #define PROCFS_FILE_OFFSET_BITS_HACK 1
1193 #undef _FILE_OFFSET_BITS
1195 #define PROCFS_FILE_OFFSET_BITS_HACK 0
1200 #if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
1201 #define _FILE_OFFSET_BITS 64
1204 int getmem(size_t *rss, size_t *vsize) {
1205 pid_t pid = getpid();
1207 size_t page_size = getpagesize();
1209 sprintf(pidpath, "/proc/%d/psinfo", pid);
1212 FILE *f = fopen(pidpath, "r");
1215 if (fread(&psinfo, sizeof(psinfo_t), 1, f) != 1) {
1222 *vsize = (size_t) psinfo.pr_size * page_size;
1223 *rss = (size_t) psinfo.pr_rssize * 1024;
1233 #define HAVE_GETMEM 1
1235 #include <sys/param.h>
1236 #include <sys/sysctl.h>
1237 #include <sys/user.h>
1242 int getmem(size_t *rss, size_t *vsize) {
1244 struct kinfo_proc *kinfo = NULL;
1247 size_t page_size = getpagesize();
1251 kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open");
1252 if (kd == NULL) goto error;
1254 kinfo = kvm_getprocs(kd, KERN_PROC_PID, pid, &nprocs);
1255 if (kinfo == NULL) goto error;
1257 *rss = kinfo->ki_rssize * page_size;
1258 *vsize = kinfo->ki_size;
1265 if (kd) kvm_close(kd);
1268 #endif // __FreeBSD__
1272 #define HAVE_GETMEM 1
1273 /* Researched by Tim Becker and Michael Knight
1274 * http://blog.kuriositaet.de/?p=257
1277 #include <mach/task.h>
1278 #include <mach/mach_init.h>
1280 int getmem(size_t *rss, size_t *vsize) {
1281 struct task_basic_info t_info;
1282 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
1284 int r = task_info(mach_task_self(),
1286 (task_info_t)&t_info,
1289 if (r != KERN_SUCCESS) return -1;
1291 *rss = t_info.resident_size;
1292 *vsize = t_info.virtual_size;
1299 # define HAVE_GETMEM 1
1300 # include <sys/param.h> /* for MAXPATHLEN */
1302 int getmem(size_t *rss, size_t *vsize) {
1303 FILE *f = fopen("/proc/self/stat", "r");
1308 char buffer[MAXPATHLEN];
1309 size_t page_size = getpagesize();
1312 if (fscanf(f, "%d ", &itmp) == 0) goto error;
1314 if (fscanf (f, "%s ", &buffer[0]) == 0) goto error;
1316 if (fscanf (f, "%c ", &ctmp) == 0) goto error;
1317 /* Parent process */
1318 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1320 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1322 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1324 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1325 /* TTY owner process group */
1326 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1328 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1329 /* Minor faults (no memory page) */
1330 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1331 /* Minor faults, children */
1332 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1333 /* Major faults (memory page faults) */
1334 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1335 /* Major faults, children */
1336 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1338 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1340 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1341 /* utime, children */
1342 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1343 /* stime, children */
1344 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1345 /* jiffies remaining in current time slice */
1346 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1348 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1349 /* jiffies until next timeout */
1350 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1351 /* jiffies until next SIGALRM */
1352 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1353 /* start time (jiffies since system boot) */
1354 if (fscanf (f, "%d ", &itmp) == 0) goto error;
1356 /* Virtual memory size */
1357 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1358 *vsize = (size_t) itmp;
1360 /* Resident set size */
1361 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1362 *rss = (size_t) itmp * page_size;
1365 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1367 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1369 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1370 /* Start of stack */
1371 if (fscanf (f, "%u ", &itmp) == 0) goto error;
1384 static void CheckStatus(EV_P_ ev_timer *watcher, int revents) {
1385 assert(watcher == &gc_timer);
1386 assert(revents == EV_TIMER);
1391 if (!ev_is_active(&gc_idle) && getmem(&rss, &vsize) == 0) {
1392 if (rss > 1024*1024*128) {
1393 // larger than 128 megs, just start the idle watcher
1394 ev_idle_start(EV_A_ &gc_idle);
1398 #endif // HAVE_GETMEM
1400 double d = ev_now() - TICK_TIME(3);
1402 //printfb("timer d = %f\n", d);
1404 if (d >= GC_WAIT_TIME - 1.) {
1405 //fprintf(stderr, "start idle\n");
1406 ev_idle_start(EV_A_ &gc_idle);
1411 v8::Handle<v8::Value> MemoryUsage(const v8::Arguments& args) {
1413 assert(args.Length() == 0);
1416 return ThrowException(Exception::Error(String::New("Not support on your platform. (Talk to Ryan.)")));
1420 int r = getmem(&rss, &vsize);
1423 return ThrowException(Exception::Error(String::New(strerror(errno))));
1426 Local<Object> info = Object::New();
1428 if (rss_symbol.IsEmpty()) {
1429 rss_symbol = NODE_PSYMBOL("rss");
1430 vsize_symbol = NODE_PSYMBOL("vsize");
1431 heap_total_symbol = NODE_PSYMBOL("heapTotal");
1432 heap_used_symbol = NODE_PSYMBOL("heapUsed");
1435 info->Set(rss_symbol, Integer::NewFromUnsigned(rss));
1436 info->Set(vsize_symbol, Integer::NewFromUnsigned(vsize));
1439 HeapStatistics v8_heap_stats;
1440 V8::GetHeapStatistics(&v8_heap_stats);
1441 info->Set(heap_total_symbol,
1442 Integer::NewFromUnsigned(v8_heap_stats.total_heap_size()));
1443 info->Set(heap_used_symbol,
1444 Integer::NewFromUnsigned(v8_heap_stats.used_heap_size()));
1446 return scope.Close(info);
1451 v8::Handle<v8::Value> Kill(const v8::Arguments& args) {
1454 if (args.Length() < 1 || !args[0]->IsNumber()) {
1455 return ThrowException(Exception::Error(String::New("Bad argument.")));
1458 pid_t pid = args[0]->IntegerValue();
1462 if (args.Length() >= 2) {
1463 if (args[1]->IsNumber()) {
1464 sig = args[1]->Int32Value();
1465 } else if (args[1]->IsString()) {
1466 Local<String> signame = args[1]->ToString();
1468 Local<Value> sig_v = process->Get(signame);
1469 if (!sig_v->IsNumber()) {
1470 return ThrowException(Exception::Error(String::New("Unknown signal")));
1472 sig = sig_v->Int32Value();
1476 int r = kill(pid, sig);
1479 return ThrowException(Exception::Error(String::New(strerror(errno))));
1485 typedef void (*extInit)(Handle<Object> exports);
1487 // DLOpen is node.dlopen(). Used to load 'module.node' dynamically shared
1489 Handle<Value> DLOpen(const v8::Arguments& args) {
1492 if (args.Length() < 2) return Undefined();
1494 String::Utf8Value filename(args[0]->ToString()); // Cast
1495 Local<Object> target = args[1]->ToObject(); // Cast
1497 // Actually call dlopen().
1498 // FIXME: This is a blocking function and should be called asynchronously!
1499 // This function should be moved to file.cc and use libeio to make this
1501 void *handle = dlopen(*filename, RTLD_LAZY);
1504 if (handle == NULL) {
1505 Local<Value> exception = Exception::Error(String::New(dlerror()));
1506 return ThrowException(exception);
1509 // Get the init() function from the dynamically shared object.
1510 void *init_handle = dlsym(handle, "init");
1511 // Error out if not found.
1512 if (init_handle == NULL) {
1513 Local<Value> exception =
1514 Exception::Error(String::New("No 'init' symbol found in module."));
1515 return ThrowException(exception);
1517 extInit init = (extInit)(init_handle); // Cast
1519 // Execute the C++ module
1526 Handle<Value> Compile(const Arguments& args) {
1529 if (args.Length() < 2) {
1530 return ThrowException(Exception::TypeError(
1531 String::New("needs two arguments.")));
1534 Local<String> source = args[0]->ToString();
1535 Local<String> filename = args[1]->ToString();
1539 Local<v8::Script> script = v8::Script::Compile(source, filename);
1540 if (try_catch.HasCaught()) {
1541 // Hack because I can't get a proper stacktrace on SyntaxError
1542 ReportException(try_catch, true);
1546 Local<Value> result = script->Run();
1547 if (try_catch.HasCaught()) {
1548 ReportException(try_catch, false);
1552 return scope.Close(result);
1555 static void OnFatalError(const char* location, const char* message) {
1557 fprintf(stderr, "FATAL ERROR: %s %s\n", location, message);
1559 fprintf(stderr, "FATAL ERROR: %s\n", message);
1564 static int uncaught_exception_counter = 0;
1566 void FatalException(TryCatch &try_catch) {
1569 // Check if uncaught_exception_counter indicates a recursion
1570 if (uncaught_exception_counter > 0) {
1571 ReportException(try_catch, true);
1575 if (listeners_symbol.IsEmpty()) {
1576 listeners_symbol = NODE_PSYMBOL("listeners");
1577 uncaught_exception_symbol = NODE_PSYMBOL("uncaughtException");
1578 emit_symbol = NODE_PSYMBOL("emit");
1581 Local<Value> listeners_v = process->Get(listeners_symbol);
1582 assert(listeners_v->IsFunction());
1584 Local<Function> listeners = Local<Function>::Cast(listeners_v);
1586 Local<String> uncaught_exception_symbol_l = Local<String>::New(uncaught_exception_symbol);
1587 Local<Value> argv[1] = { uncaught_exception_symbol_l };
1588 Local<Value> ret = listeners->Call(process, 1, argv);
1590 assert(ret->IsArray());
1592 Local<Array> listener_array = Local<Array>::Cast(ret);
1594 uint32_t length = listener_array->Length();
1595 // Report and exit if process has no "uncaughtException" listener
1597 ReportException(try_catch, true);
1601 // Otherwise fire the process "uncaughtException" event
1602 Local<Value> emit_v = process->Get(emit_symbol);
1603 assert(emit_v->IsFunction());
1605 Local<Function> emit = Local<Function>::Cast(emit_v);
1607 Local<Value> error = try_catch.Exception();
1608 Local<Value> event_argv[2] = { uncaught_exception_symbol_l, error };
1610 uncaught_exception_counter++;
1611 emit->Call(process, 2, event_argv);
1612 // Decrement so we know if the next exception is a recursion or not
1613 uncaught_exception_counter--;
1617 static ev_async debug_watcher;
1618 volatile static bool debugger_msg_pending = false;
1620 static void DebugMessageCallback(EV_P_ ev_async *watcher, int revents) {
1622 assert(watcher == &debug_watcher);
1623 assert(revents == EV_ASYNC);
1624 Debug::ProcessDebugMessages();
1627 static void DebugMessageDispatch(void) {
1628 // This function is called from V8's debug thread when a debug TCP client
1629 // has sent a message.
1631 // Send a signal to our main thread saying that it should enter V8 to
1632 // handle the message.
1633 debugger_msg_pending = true;
1634 ev_async_send(EV_DEFAULT_UC_ &debug_watcher);
1637 static Handle<Value> CheckBreak(const Arguments& args) {
1639 assert(args.Length() == 0);
1641 // TODO FIXME This function is a hack to wait until V8 is ready to accept
1642 // commands. There seems to be a bug in EnableAgent( _ , _ , true) which
1643 // makes it unusable here. Ideally we'd be able to bind EnableAgent and
1644 // get it to halt until Eclipse connects.
1646 if (!debug_wait_connect)
1649 printf("Waiting for remote debugger connection...\n");
1651 const int halfSecond = 50;
1652 const int tenMs=10000;
1653 debugger_msg_pending = false;
1655 if (debugger_msg_pending) {
1656 Debug::DebugBreak();
1657 Debug::ProcessDebugMessages();
1658 debugger_msg_pending = false;
1660 // wait for 500 msec of silence from remote debugger
1661 int cnt = halfSecond;
1663 debugger_msg_pending = false;
1665 if (debugger_msg_pending) {
1666 debugger_msg_pending = false;
1677 Persistent<Object> binding_cache;
1679 static Handle<Value> Binding(const Arguments& args) {
1682 Local<String> module = args[0]->ToString();
1683 String::Utf8Value module_v(module);
1685 if (binding_cache.IsEmpty()) {
1686 binding_cache = Persistent<Object>::New(Object::New());
1689 Local<Object> exports;
1691 // TODO DRY THIS UP!
1693 if (!strcmp(*module_v, "stdio")) {
1694 if (binding_cache->Has(module)) {
1695 exports = binding_cache->Get(module)->ToObject();
1697 exports = Object::New();
1698 Stdio::Initialize(exports);
1699 binding_cache->Set(module, exports);
1702 } else if (!strcmp(*module_v, "cares")) {
1703 if (binding_cache->Has(module)) {
1704 exports = binding_cache->Get(module)->ToObject();
1706 exports = Object::New();
1707 Cares::Initialize(exports);
1708 binding_cache->Set(module, exports);
1711 } else if (!strcmp(*module_v, "fs")) {
1712 if (binding_cache->Has(module)) {
1713 exports = binding_cache->Get(module)->ToObject();
1715 exports = Object::New();
1717 // Initialize the stats object
1718 Local<FunctionTemplate> stat_templ = FunctionTemplate::New();
1719 stats_constructor_template = Persistent<FunctionTemplate>::New(stat_templ);
1720 exports->Set(String::NewSymbol("Stats"),
1721 stats_constructor_template->GetFunction());
1722 StatWatcher::Initialize(exports);
1723 File::Initialize(exports);
1724 binding_cache->Set(module, exports);
1727 } else if (!strcmp(*module_v, "signal_watcher")) {
1728 if (binding_cache->Has(module)) {
1729 exports = binding_cache->Get(module)->ToObject();
1731 exports = Object::New();
1732 SignalWatcher::Initialize(exports);
1733 binding_cache->Set(module, exports);
1736 } else if (!strcmp(*module_v, "net")) {
1737 if (binding_cache->Has(module)) {
1738 exports = binding_cache->Get(module)->ToObject();
1740 exports = Object::New();
1742 binding_cache->Set(module, exports);
1745 } else if (!strcmp(*module_v, "http_parser")) {
1746 if (binding_cache->Has(module)) {
1747 exports = binding_cache->Get(module)->ToObject();
1749 exports = Object::New();
1750 InitHttpParser(exports);
1751 binding_cache->Set(module, exports);
1754 } else if (!strcmp(*module_v, "child_process")) {
1755 if (binding_cache->Has(module)) {
1756 exports = binding_cache->Get(module)->ToObject();
1758 exports = Object::New();
1759 ChildProcess::Initialize(exports);
1760 binding_cache->Set(module, exports);
1763 } else if (!strcmp(*module_v, "buffer")) {
1764 if (binding_cache->Has(module)) {
1765 exports = binding_cache->Get(module)->ToObject();
1767 exports = Object::New();
1768 Buffer::Initialize(exports);
1769 binding_cache->Set(module, exports);
1772 } else if (!strcmp(*module_v, "crypto")) {
1773 if (binding_cache->Has(module)) {
1774 exports = binding_cache->Get(module)->ToObject();
1776 exports = Object::New();
1777 InitCrypto(exports);
1778 binding_cache->Set(module, exports);
1781 } else if (!strcmp(*module_v, "evals")) {
1782 if (binding_cache->Has(module)) {
1783 exports = binding_cache->Get(module)->ToObject();
1785 exports = Object::New();
1786 node::Script::Initialize(exports);
1787 binding_cache->Set(module, exports);
1790 } else if (!strcmp(*module_v, "natives")) {
1791 if (binding_cache->Has(module)) {
1792 exports = binding_cache->Get(module)->ToObject();
1794 exports = Object::New();
1795 // Explicitly define native sources.
1796 // TODO DRY/automate this?
1797 exports->Set(String::New("assert"), String::New(native_assert));
1798 exports->Set(String::New("buffer"), String::New(native_buffer));
1799 exports->Set(String::New("child_process"),String::New(native_child_process));
1800 exports->Set(String::New("dns"), String::New(native_dns));
1801 exports->Set(String::New("events"), String::New(native_events));
1802 exports->Set(String::New("file"), String::New(native_file));
1803 exports->Set(String::New("freelist"), String::New(native_freelist));
1804 exports->Set(String::New("fs"), String::New(native_fs));
1805 exports->Set(String::New("http"), String::New(native_http));
1806 exports->Set(String::New("crypto"), String::New(native_crypto));
1807 exports->Set(String::New("ini"), String::New(native_ini));
1808 exports->Set(String::New("mjsunit"), String::New(native_mjsunit));
1809 exports->Set(String::New("net"), String::New(native_net));
1810 exports->Set(String::New("posix"), String::New(native_posix));
1811 exports->Set(String::New("querystring"), String::New(native_querystring));
1812 exports->Set(String::New("repl"), String::New(native_repl));
1813 exports->Set(String::New("sys"), String::New(native_sys));
1814 exports->Set(String::New("tcp"), String::New(native_tcp));
1815 exports->Set(String::New("uri"), String::New(native_uri));
1816 exports->Set(String::New("url"), String::New(native_url));
1817 exports->Set(String::New("utils"), String::New(native_utils));
1818 exports->Set(String::New("path"), String::New(native_path));
1819 exports->Set(String::New("module"), String::New(native_module));
1820 binding_cache->Set(module, exports);
1824 return ThrowException(Exception::Error(String::New("No such module")));
1827 return scope.Close(exports);
1831 static void Load(int argc, char *argv[]) {
1834 Local<FunctionTemplate> process_template = FunctionTemplate::New();
1835 node::EventEmitter::Initialize(process_template);
1837 process = Persistent<Object>::New(process_template->GetFunction()->NewInstance());
1839 // Add a reference to the global object
1840 Local<Object> global = Context::GetCurrent()->Global();
1841 process->Set(String::NewSymbol("global"), global);
1844 process->Set(String::NewSymbol("version"), String::New(NODE_VERSION));
1845 // process.installPrefix
1846 process->Set(String::NewSymbol("installPrefix"), String::New(NODE_PREFIX));
1849 #define xstr(s) str(s)
1851 process->Set(String::NewSymbol("platform"), String::New(xstr(PLATFORM)));
1855 Local<Array> arguments = Array::New(argc - option_end_index + 1);
1856 arguments->Set(Integer::New(0), String::New(argv[0]));
1857 for (j = 1, i = option_end_index + 1; i < argc; j++, i++) {
1858 Local<String> arg = String::New(argv[i]);
1859 arguments->Set(Integer::New(j), arg);
1862 process->Set(String::NewSymbol("ARGV"), arguments);
1863 process->Set(String::NewSymbol("argv"), arguments);
1865 // create process.env
1866 Local<Object> env = Object::New();
1867 for (i = 0; environ[i]; i++) {
1868 // skip entries without a '=' character
1869 for (j = 0; environ[i][j] && environ[i][j] != '='; j++) { ; }
1870 // create the v8 objects
1871 Local<String> field = String::New(environ[i], j);
1872 Local<String> value = Local<String>();
1873 if (environ[i][j] == '=') {
1874 value = String::New(environ[i]+j+1);
1877 env->Set(field, value);
1879 // assign process.ENV
1880 process->Set(String::NewSymbol("ENV"), env);
1881 process->Set(String::NewSymbol("env"), env);
1883 process->Set(String::NewSymbol("pid"), Integer::New(getpid()));
1885 // define various internal methods
1886 NODE_SET_METHOD(process, "loop", Loop);
1887 NODE_SET_METHOD(process, "unloop", Unloop);
1888 NODE_SET_METHOD(process, "compile", Compile);
1889 NODE_SET_METHOD(process, "_byteLength", ByteLength);
1890 NODE_SET_METHOD(process, "_needTickCallback", NeedTickCallback);
1891 NODE_SET_METHOD(process, "reallyExit", Exit);
1892 NODE_SET_METHOD(process, "chdir", Chdir);
1893 NODE_SET_METHOD(process, "cwd", Cwd);
1894 NODE_SET_METHOD(process, "getuid", GetUid);
1895 NODE_SET_METHOD(process, "setuid", SetUid);
1897 NODE_SET_METHOD(process, "setgid", SetGid);
1898 NODE_SET_METHOD(process, "getgid", GetGid);
1900 NODE_SET_METHOD(process, "umask", Umask);
1901 NODE_SET_METHOD(process, "dlopen", DLOpen);
1902 NODE_SET_METHOD(process, "kill", Kill);
1903 NODE_SET_METHOD(process, "memoryUsage", MemoryUsage);
1904 NODE_SET_METHOD(process, "checkBreak", CheckBreak);
1906 NODE_SET_METHOD(process, "binding", Binding);
1908 // Assign the EventEmitter. It was created in main().
1909 process->Set(String::NewSymbol("EventEmitter"),
1910 EventEmitter::constructor_template->GetFunction());
1913 // Initialize the C++ modules..................filename of module
1914 IOWatcher::Initialize(process); // io_watcher.cc
1915 // Not in use at the moment.
1916 //IdleWatcher::Initialize(process); // idle_watcher.cc
1917 Timer::Initialize(process); // timer.cc
1918 DefineConstants(process); // constants.cc
1920 // Compile, execute the src/node.js file. (Which was included as static C
1921 // string in node_natives.h. 'natve_node' is the string containing that
1924 // The node.js file returns a function 'f'
1928 Local<Value> f_value = ExecuteString(String::New(native_node),
1929 String::New("node.js"));
1930 if (try_catch.HasCaught()) {
1931 ReportException(try_catch, true);
1934 assert(f_value->IsFunction());
1935 Local<Function> f = Local<Function>::Cast(f_value);
1937 // Now we call 'f' with the 'process' variable that we've built up with
1938 // all our bindings. Inside node.js we'll take care of assigning things to
1941 // We start the process this way in order to be more modular. Developers
1942 // who do not like how 'src/node.js' setups the module system but do like
1943 // Node's I/O bindings may want to replace 'f' with their own function.
1945 Local<Value> args[1] = { Local<Value>::New(process) };
1947 f->Call(global, 1, args);
1949 if (try_catch.HasCaught()) {
1950 ReportException(try_catch, true);
1955 static void PrintHelp();
1957 static void ParseDebugOpt(const char* arg) {
1960 use_debug_agent = true;
1961 if (!strcmp (arg, "--debug-brk")) {
1962 debug_wait_connect = true;
1964 } else if (!strcmp(arg, "--debug")) {
1966 } else if (strstr(arg, "--debug-brk=") == arg) {
1967 debug_wait_connect = true;
1968 p = 1 + strchr(arg, '=');
1969 debug_port = atoi(p);
1970 } else if (strstr(arg, "--debug=") == arg) {
1971 p = 1 + strchr(arg, '=');
1972 debug_port = atoi(p);
1974 if (p && debug_port > 1024 && debug_port < 65536)
1977 fprintf(stderr, "Bad debug option.\n");
1978 if (p) fprintf(stderr, "Debug port must be in range 1025 to 65535.\n");
1984 static void PrintHelp() {
1985 printf("Usage: node [options] script.js [arguments] \n"
1987 " -v, --version print node's version\n"
1988 " --debug[=port] enable remote debugging via given TCP port\n"
1989 " without stopping the execution\n"
1990 " --debug-brk[=port] as above, but break in script.js and\n"
1991 " wait for remote debugger to connect\n"
1992 " --v8-options print v8 command line options\n"
1993 " --vars print various compiled-in variables\n"
1995 "Enviromental variables:\n"
1996 "NODE_PATH ':'-separated list of directories\n"
1997 " prefixed to the module search path,\n"
1999 "NODE_DEBUG Print additional debugging output.\n"
2001 "Documentation can be found at http://nodejs.org/api.html"
2002 " or with 'man node'\n");
2005 // Parse node command line arguments.
2006 static void ParseArgs(int *argc, char **argv) {
2007 // TODO use parse opts
2008 for (int i = 1; i < *argc; i++) {
2009 const char *arg = argv[i];
2010 if (strstr(arg, "--debug") == arg) {
2012 argv[i] = const_cast<char*>("");
2013 option_end_index = i;
2014 } else if (strcmp(arg, "--version") == 0 || strcmp(arg, "-v") == 0) {
2015 printf("%s\n", NODE_VERSION);
2017 } else if (strcmp(arg, "--vars") == 0) {
2018 printf("NODE_PREFIX: %s\n", NODE_PREFIX);
2019 printf("NODE_CFLAGS: %s\n", NODE_CFLAGS);
2021 } else if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
2024 } else if (strcmp(arg, "--v8-options") == 0) {
2025 argv[i] = const_cast<char*>("--help");
2026 option_end_index = i+1;
2027 } else if (argv[i][0] != '-') {
2028 option_end_index = i-1;
2037 int main(int argc, char *argv[]) {
2038 // Parse a few arguments which are specific to Node.
2039 node::ParseArgs(&argc, argv);
2040 // Parse the rest of the args (up to the 'option_end_index' (where '--' was
2041 // in the command line))
2042 V8::SetFlagsFromCommandLine(&node::option_end_index, argv, false);
2044 // Error out if we don't have a script argument.
2046 fprintf(stderr, "No script was specified.\n");
2053 struct sigaction sa;
2054 bzero(&sa, sizeof(sa));
2055 sa.sa_handler = SIG_IGN;
2056 sigaction(SIGPIPE, &sa, NULL);
2059 // Initialize the default ev loop.
2061 // TODO(Ryan) I'm experiencing abnormally high load using Solaris's
2062 // EVBACKEND_PORT. Temporarally forcing select() until I debug.
2063 ev_default_loop(EVBACKEND_SELECT);
2065 ev_default_loop(EVFLAG_AUTO);
2068 ev_prepare_init(&node::next_tick_watcher, node::Tick);
2069 ev_prepare_start(EV_DEFAULT_UC_ &node::next_tick_watcher);
2070 ev_unref(EV_DEFAULT_UC);
2072 ev_idle_init(&node::tick_spinner, node::Spin);
2074 ev_check_init(&node::gc_check, node::Check);
2075 ev_check_start(EV_DEFAULT_UC_ &node::gc_check);
2076 ev_unref(EV_DEFAULT_UC);
2078 ev_idle_init(&node::gc_idle, node::Idle);
2079 ev_timer_init(&node::gc_timer, node::CheckStatus, 5., 5.);
2082 // Setup the EIO thread pool
2083 { // It requires 3, yes 3, watchers.
2084 ev_idle_init(&node::eio_poller, node::DoPoll);
2086 ev_async_init(&node::eio_want_poll_notifier, node::WantPollNotifier);
2087 ev_async_start(EV_DEFAULT_UC_ &node::eio_want_poll_notifier);
2088 ev_unref(EV_DEFAULT_UC);
2090 ev_async_init(&node::eio_done_poll_notifier, node::DonePollNotifier);
2091 ev_async_start(EV_DEFAULT_UC_ &node::eio_done_poll_notifier);
2092 ev_unref(EV_DEFAULT_UC);
2094 eio_init(node::EIOWantPoll, node::EIODonePoll);
2095 // Don't handle more than 10 reqs on each eio_poll(). This is to avoid
2096 // race conditions. See test/mjsunit/test-eio-race.js
2097 eio_set_max_poll_reqs(10);
2101 HandleScope handle_scope;
2103 V8::SetFatalErrorHandler(node::OnFatalError);
2105 // If the --debug flag was specified then initialize the debug thread.
2106 if (node::use_debug_agent) {
2107 // Initialize the async watcher for receiving messages from the debug
2108 // thread and marshal it into the main thread. DebugMessageCallback()
2109 // is called from the main thread to execute a random bit of javascript
2110 // - which will give V8 control so it can handle whatever new message
2111 // had been received on the debug thread.
2112 ev_async_init(&node::debug_watcher, node::DebugMessageCallback);
2113 ev_set_priority(&node::debug_watcher, EV_MAXPRI);
2114 // Set the callback DebugMessageDispatch which is called from the debug
2116 Debug::SetDebugMessageDispatchHandler(node::DebugMessageDispatch);
2117 // Start the async watcher.
2118 ev_async_start(EV_DEFAULT_UC_ &node::debug_watcher);
2119 // unref it so that we exit the event loop despite it being active.
2120 ev_unref(EV_DEFAULT_UC);
2122 // Start the debug thread and it's associated TCP server on port 5858.
2123 bool r = Debug::EnableAgent("node " NODE_VERSION, node::debug_port);
2125 // Crappy check that everything went well. FIXME
2127 // Print out some information.
2128 printf("debugger listening on port %d\n", node::debug_port);
2131 // Create the one and only Context.
2132 Persistent<Context> context = Context::New();
2133 Context::Scope context_scope(context);
2135 // Create all the objects, load modules, do everything.
2136 // so your next reading stop should be node::Load()!
2137 node::Load(argc, argv);
2139 node::Stdio::Flush();