diff --git a/doc/api.txt b/doc/api.txt index 757ca9c8fd..66befaab1f 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -136,6 +136,9 @@ success code 0. +process.cwd()+:: Returns the current working directory of the process. ++process.getuid(), process.setuid(id)+:: +Gets/sets the user identity of the process. (See setuid(2).) + +process.chdir(directory)+:: Changes the current working directory of the process. diff --git a/src/node.cc b/src/node.cc index 493fad1066..77b9700da5 100644 --- a/src/node.cc +++ b/src/node.cc @@ -9,6 +9,8 @@ #include #include #include /* dlopen(), dlsym() */ +#include +#include /* setuid, getuid */ #include #include @@ -464,6 +466,32 @@ static Handle Umask(const Arguments& args){ return scope.Close(Uint32::New(old)); } + +static Handle GetUid(const Arguments& args) { + HandleScope scope; + int uid = getuid(); + return scope.Close(Integer::New(uid)); +} + + +static Handle SetUid(const Arguments& args) { + HandleScope scope; + + if (args.Length() < 1) { + return ThrowException(Exception::Error( + String::New("setuid requires 1 argument"))); + } + + Local given_uid = args[0]->ToInteger(); + int uid = given_uid->Int32Value(); + int result; + if ((result = setuid(uid)) != 0) { + return ThrowException(Exception::Error(String::New(strerror(errno)))); + } + return Undefined(); +} + + v8::Handle Exit(const v8::Arguments& args) { HandleScope scope; fflush(stderr); @@ -953,6 +981,8 @@ static void Load(int argc, char *argv[]) { NODE_SET_METHOD(process, "reallyExit", Exit); NODE_SET_METHOD(process, "chdir", Chdir); NODE_SET_METHOD(process, "cwd", Cwd); + NODE_SET_METHOD(process, "getuid", GetUid); + NODE_SET_METHOD(process, "setuid", SetUid); NODE_SET_METHOD(process, "umask", Umask); NODE_SET_METHOD(process, "dlopen", DLOpen); NODE_SET_METHOD(process, "kill", Kill);