diff options
Diffstat (limited to 'shell/shell_utils.cpp')
-rw-r--r-- | shell/shell_utils.cpp | 103 |
1 files changed, 47 insertions, 56 deletions
diff --git a/shell/shell_utils.cpp b/shell/shell_utils.cpp index 09a3e46..e09309c 100644 --- a/shell/shell_utils.cpp +++ b/shell/shell_utils.cpp @@ -60,6 +60,10 @@ namespace mongo { inline int pipe(int fds[2]) { return _pipe(fds, 4096, _O_TEXT | _O_NOINHERIT); } #endif + namespace JSFiles { + extern const JSFile servers; + } + // these functions have not been audited for thread safety - currently they are called with an exclusive js mutex namespace shellUtils { @@ -86,19 +90,8 @@ namespace mongo { // real methods - mongo::BSONObj JSSleep(const mongo::BSONObj &args) { - assert( args.nFields() == 1 ); - assert( args.firstElement().isNumber() ); - int ms = int( args.firstElement().number() ); - { - auto_ptr< ScriptEngine::Unlocker > u = globalScriptEngine->newThreadUnlocker(); - sleepmillis( ms ); - } - return undefined_; - } - void goingAwaySoon(); - BSONObj Quit(const BSONObj& args) { + BSONObj Quit(const BSONObj& args, void* data) { // If not arguments are given first element will be EOO, which // converts to the integer value 0. goingAwaySoon(); @@ -107,7 +100,7 @@ namespace mongo { return undefined_; } - BSONObj JSGetMemInfo( const BSONObj& args ) { + BSONObj JSGetMemInfo( const BSONObj& args, void* data ) { ProcessInfo pi; uassert( 10258 , "processinfo not supported" , pi.supported() ); @@ -124,13 +117,13 @@ namespace mongo { #ifndef MONGO_SAFE_SHELL - BSONObj listFiles(const BSONObj& _args) { + BSONObj listFiles(const BSONObj& _args, void* data) { static BSONObj cd = BSON( "0" << "." ); BSONObj args = _args.isEmpty() ? cd : _args; uassert( 10257 , "need to specify 1 argument to listFiles" , args.nFields() == 1 ); - BSONObjBuilder lst; + BSONArrayBuilder lst; string rootname = args.firstElement().valuestrsafe(); path root( rootname ); @@ -142,7 +135,6 @@ namespace mongo { directory_iterator end; directory_iterator i( root); - int num =0; while ( i != end ) { path p = *i; BSONObjBuilder b; @@ -158,21 +150,17 @@ namespace mongo { } } - stringstream ss; - ss << num; - string name = ss.str(); - lst.append( name, b.done() ); - num++; + lst.append( b.obj() ); i++; } - + BSONObjBuilder ret; ret.appendArray( "", lst.done() ); return ret.obj(); } - BSONObj ls(const BSONObj& args) { - BSONObj o = listFiles(args); + BSONObj ls(const BSONObj& args, void* data) { + BSONObj o = listFiles(args, data); if( !o.isEmpty() ) { for( BSONObj::iterator i = o.firstElement().Obj().begin(); i.more(); ) { BSONObj f = i.next().Obj(); @@ -185,7 +173,7 @@ namespace mongo { return BSONObj(); } - BSONObj cd(const BSONObj& args) { + BSONObj cd(const BSONObj& args, void* data) { #if defined(_WIN32) std::wstring dir = toWideString( args.firstElement().String().c_str() ); if( SetCurrentDirectory(dir.c_str()) ) @@ -200,12 +188,12 @@ namespace mongo { return BSON( "" << "change directory failed" ); } - BSONObj pwd(const BSONObj&) { + BSONObj pwd(const BSONObj&, void* data) { boost::filesystem::path p = boost::filesystem::current_path(); return BSON( "" << p.string() ); } - BSONObj hostname(const BSONObj&) { + BSONObj hostname(const BSONObj&, void* data) { return BSON( "" << getHostName() ); } @@ -216,7 +204,7 @@ namespace mongo { const int CANT_OPEN_FILE = 13300; - BSONObj cat(const BSONObj& args) { + BSONObj cat(const BSONObj& args, void* data) { BSONElement e = oneArg(args); stringstream ss; ifstream f(e.valuestrsafe()); @@ -235,7 +223,7 @@ namespace mongo { return BSON( "" << ss.str() ); } - BSONObj md5sumFile(const BSONObj& args) { + BSONObj md5sumFile(const BSONObj& args, void* data) { BSONElement e = oneArg(args); stringstream ss; FILE* f = fopen(e.valuestrsafe(), "rb"); @@ -256,12 +244,12 @@ namespace mongo { return BSON( "" << digestToString( d ) ); } - BSONObj mkdir(const BSONObj& args) { + BSONObj mkdir(const BSONObj& args, void* data) { boost::filesystem::create_directories(args.firstElement().String()); return BSON( "" << true ); } - BSONObj removeFile(const BSONObj& args) { + BSONObj removeFile(const BSONObj& args, void* data) { BSONElement e = oneArg(args); bool found = false; @@ -280,7 +268,7 @@ namespace mongo { * @param args - [ name, byte index ] * In this initial implementation, all bits in the specified byte are flipped. */ - BSONObj fuzzFile(const BSONObj& args) { + BSONObj fuzzFile(const BSONObj& args, void* data) { uassert( 13619, "fuzzFile takes 2 arguments", args.nFields() == 2 ); shared_ptr< File > f( new File() ); f->open( args.getStringField( "0" ) ); @@ -314,7 +302,7 @@ namespace mongo { if( mongo::dbexitCalled ) throw "program is terminating"; stringstream buf; if ( port > 0 ) - buf << "m" << port << "| " << line; + buf << " m" << port << "| " << line; else buf << "sh" << pid << "| " << line; cout << buf.str() << endl; @@ -322,7 +310,7 @@ namespace mongo { } // only returns last 100000 characters - BSONObj RawMongoProgramOutput( const BSONObj &args ) { + BSONObj RawMongoProgramOutput( const BSONObj &args, void* data ) { mongo::mutex::scoped_lock lk( mongoProgramOutputMutex ); string out = mongoProgramOutput_.str(); size_t len = out.length(); @@ -331,7 +319,7 @@ namespace mongo { return BSON( "" << out ); } - BSONObj ClearRawMongoProgramOutput( const BSONObj &args ) { + BSONObj ClearRawMongoProgramOutput( const BSONObj &args, void* data ) { mongo::mutex::scoped_lock lk( mongoProgramOutputMutex ); mongoProgramOutput_.str( "" ); return undefined_; @@ -466,12 +454,16 @@ namespace mongo { try { // This assumes there aren't any 0's in the mongo program output. // Hope that's ok. - const unsigned bufSize = 64000; + const unsigned bufSize = 128 * 1024; char buf[ bufSize ]; char temp[ bufSize ]; char *start = buf; while( 1 ) { int lenToRead = ( bufSize - 1 ) - ( start - buf ); + if ( lenToRead <= 0 ) { + cout << "error: lenToRead: " << lenToRead << endl; + cout << "first 300: " << string(buf,0,300) << endl; + } assert( lenToRead > 0 ); int ret = read( pipe_, (void *)start, lenToRead ); if( mongo::dbexitCalled ) @@ -541,7 +533,7 @@ namespace mongo { { stringstream ss; ss << "couldn't start process " << argv_[0]; - uassert(13294, ss.str(), success); + uassert(14042, ss.str(), success); } CloseHandle(pi.hThread); @@ -624,14 +616,14 @@ namespace mongo { #endif } - BSONObj WaitProgram( const BSONObj& a ) { + BSONObj WaitProgram( const BSONObj& a, void* data ) { int pid = oneArg( a ).numberInt(); BSONObj x = BSON( "" << wait_for_pid( pid ) ); shells.erase( pid ); return x; } - BSONObj WaitMongoProgramOnPort( const BSONObj &a ) { + BSONObj WaitMongoProgramOnPort( const BSONObj &a, void* data ) { int port = oneArg( a ).numberInt(); uassert( 13621, "no known mongo program on port", dbs.count( port ) != 0 ); log() << "waiting port: " << port << ", pid: " << dbs[ port ].first << endl; @@ -642,7 +634,7 @@ namespace mongo { return BSON( "" << ret ); } - BSONObj StartMongoProgram( const BSONObj &a ) { + BSONObj StartMongoProgram( const BSONObj &a, void* data ) { _nokillop = true; ProgramRunner r( a ); r.start(); @@ -650,7 +642,7 @@ namespace mongo { return BSON( string( "" ) << int( r.pid() ) ); } - BSONObj RunMongoProgram( const BSONObj &a ) { + BSONObj RunMongoProgram( const BSONObj &a, void* data ) { ProgramRunner r( a ); r.start(); boost::thread t( r ); @@ -665,7 +657,7 @@ namespace mongo { return BSON( string( "" ) << exit_code ); } - BSONObj RunProgram(const BSONObj &a) { + BSONObj RunProgram(const BSONObj &a, void* data) { ProgramRunner r( a, false ); r.start(); boost::thread t( r ); @@ -675,7 +667,7 @@ namespace mongo { return BSON( string( "" ) << exit_code ); } - BSONObj ResetDbpath( const BSONObj &a ) { + BSONObj ResetDbpath( const BSONObj &a, void* data ) { assert( a.nFields() == 1 ); string path = a.firstElement().valuestrsafe(); assert( !path.empty() ); @@ -705,7 +697,7 @@ namespace mongo { } // NOTE target dbpath will be cleared first - BSONObj CopyDbpath( const BSONObj &a ) { + BSONObj CopyDbpath( const BSONObj &a, void* data ) { assert( a.nFields() == 2 ); BSONObjIterator i( a ); string from = i.next().str(); @@ -816,22 +808,22 @@ namespace mongo { } /** stopMongoProgram(port[, signal]) */ - BSONObj StopMongoProgram( const BSONObj &a ) { + BSONObj StopMongoProgram( const BSONObj &a, void* data ) { assert( a.nFields() == 1 || a.nFields() == 2 ); - assert( a.firstElement().isNumber() ); + uassert( 15853 , "stopMongo needs a number" , a.firstElement().isNumber() ); int port = int( a.firstElement().number() ); int code = killDb( port, 0, getSignal( a ) ); cout << "shell: stopped mongo program on port " << port << endl; - return BSON( "" << code ); + return BSON( "" << (double)code ); } - BSONObj StopMongoProgramByPid( const BSONObj &a ) { + BSONObj StopMongoProgramByPid( const BSONObj &a, void* data ) { assert( a.nFields() == 1 || a.nFields() == 2 ); - assert( a.firstElement().isNumber() ); + uassert( 15852 , "stopMongoByPid needs a number" , a.firstElement().isNumber() ); int pid = int( a.firstElement().number() ); int code = killDb( 0, pid, getSignal( a ) ); cout << "shell: stopped mongo program on pid " << pid << endl; - return BSON( "" << code ); + return BSON( "" << (double)code ); } void KillMongoProgramInstances() { @@ -853,20 +845,20 @@ namespace mongo { MongoProgramScope::~MongoProgramScope() { DESTRUCTOR_GUARD( KillMongoProgramInstances(); - ClearRawMongoProgramOutput( BSONObj() ); + ClearRawMongoProgramOutput( BSONObj(), 0 ); ) } unsigned _randomSeed; - BSONObj JSSrand( const BSONObj &a ) { + BSONObj JSSrand( const BSONObj &a, void* data ) { uassert( 12518, "srand requires a single numeric argument", a.nFields() == 1 && a.firstElement().isNumber() ); _randomSeed = (unsigned)a.firstElement().numberLong(); // grab least significant digits return undefined_; } - BSONObj JSRand( const BSONObj &a ) { + BSONObj JSRand( const BSONObj &a, void* data ) { uassert( 12519, "rand accepts no arguments", a.nFields() == 0 ); unsigned r; #if !defined(_WIN32) @@ -877,7 +869,7 @@ namespace mongo { return BSON( "" << double( r ) / ( double( RAND_MAX ) + 1 ) ); } - BSONObj isWindows(const BSONObj& a) { + BSONObj isWindows(const BSONObj& a, void* data) { uassert( 13006, "isWindows accepts no arguments", a.nFields() == 0 ); #ifdef _WIN32 return BSON( "" << true ); @@ -886,7 +878,7 @@ namespace mongo { #endif } - BSONObj getHostName(const BSONObj& a) { + BSONObj getHostName(const BSONObj& a, void* data) { uassert( 13411, "getHostName accepts no arguments", a.nFields() == 0 ); char buf[260]; // HOST_NAME_MAX is usually 255 assert(gethostname(buf, 260) == 0); @@ -897,7 +889,6 @@ namespace mongo { void installShellUtils( Scope& scope ) { theScope = &scope; - scope.injectNative( "sleep" , JSSleep ); scope.injectNative( "quit", Quit ); scope.injectNative( "getMemInfo" , JSGetMemInfo ); scope.injectNative( "_srand" , JSSrand ); |