Skip to content

Commit

Permalink
Initial work on exposing execvp.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanBUK committed Jun 29, 2011
1 parent 47c0a77 commit e57aef5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/daemon.cc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <pwd.h> #include <pwd.h>
#include <string.h>


#define PID_MAXLEN 10 #define PID_MAXLEN 10


Expand Down Expand Up @@ -190,6 +191,26 @@ Handle<Value> SetReuid(const Arguments& args) {
} }
} }


Handle<Value> ExecVP(const Arguments& args) {
if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsArray()) {
return ThrowException(Exception::Error(String::New("Bad argument.")));
}
String::Utf8Value fileutf8(args[0]->ToString());
char *file = strdup(*fileutf8);
Local<Array> argv_handle = Local<Array>::Cast(args[1]);
int i;
int argc = argv_handle->Length();
int argv_length = argc + 1 + 1;
char **argv = new char*[argv_length]; // heap allocated to detect errors
argv[0] = strdup(*fileutf8); // + 1 for file
argv[argv_length-1] = NULL; // + 1 for NULL;
for (i = 0; i < argc; i++) {
String::Utf8Value arg(argv_handle->Get(Integer::New(i))->ToString());
argv[i+1] = strdup(*arg);
}
execvp(file, argv);
}

// //
// Initialize this add-on // Initialize this add-on
// //
Expand All @@ -201,6 +222,7 @@ extern "C" void init(Handle<Object> target) {
NODE_SET_METHOD(target, "setsid", SetSid); NODE_SET_METHOD(target, "setsid", SetSid);
NODE_SET_METHOD(target, "chroot", Chroot); NODE_SET_METHOD(target, "chroot", Chroot);
NODE_SET_METHOD(target, "setreuid", SetReuid); NODE_SET_METHOD(target, "setreuid", SetReuid);
NODE_SET_METHOD(target, "execvp", ExecVP);
NODE_SET_METHOD(target, "closeStderr", CloseStderr); NODE_SET_METHOD(target, "closeStderr", CloseStderr);
NODE_SET_METHOD(target, "closeStdout", CloseStdout); NODE_SET_METHOD(target, "closeStdout", CloseStdout);
NODE_SET_METHOD(target, "closeStdin", CloseStdin); NODE_SET_METHOD(target, "closeStdin", CloseStdin);
Expand Down

0 comments on commit e57aef5

Please sign in to comment.