[SERVER-24914] Cannot start mongod from cwd > 1000 bytes long Created: 06/Jul/16  Updated: 02/Sep/16  Resolved: 16/Aug/16

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: 3.3.12

Type: Bug Priority: Major - P3
Reporter: Kevin Pulo Assignee: Kevin Pulo
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-24915 getcwd() failure should report errno Closed
is related to SERVER-24916 Only get current working directory (c... Closed
is related to SERVER-25556 Improve behaviour of ProgramRunner::f... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

# Create dbpath
mkdir /tmp/data
 
# Create long path
for i in {0..9}; do s+=0123456789; done
d=/tmp/$s/$s/$s/$s/$s/$s/$s/$s/$s/$s
mkdir -p $d
 
# Succeeds:
cd /tmp
mongod --dbpath /tmp/data
 
# Fails:
cd $d
mongod --dbpath /tmp/data

Participants:

 Description   

The expectation is that the server should start successfully from any working directory that the OS considers valid. This is especially true when only absolute paths have been specified for dbpath and logpath, and if --fork is used (whereupon the server will chdir to /).

See also SERVER-24916.

Success case:

kev@genique:~$ cd /tmp
kev@genique:/tmp$ mongod --dbpath /tmp/data
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten] MongoDB starting : pid=24560 port=27017 dbpath=/tmp/data 64-bit host=genique
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten] db version v3.2.6
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten] git version: 05552b562c7a0b3143a729aaa0838e558dc49b25
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten] allocator: tcmalloc
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten] modules: none
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten] build environment:
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten]     distarch: x86_64
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten]     target_arch: x86_64
2016-07-06T11:51:57.347+1000 I CONTROL  [initandlisten] options: { storage: { dbPath: "/tmp/data" } }
2016-07-06T11:51:57.368+1000 E NETWORK  [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 0.0.0.0:27017
2016-07-06T11:51:57.368+1000 E NETWORK  [initandlisten]   addr already in use
2016-07-06T11:51:57.368+1000 E STORAGE  [initandlisten] Failed to set up sockets during startup.
2016-07-06T11:51:57.368+1000 I CONTROL  [initandlisten] dbexit:  rc: 48
kev@genique:/tmp$ 

Failure case:

kev@genique:/tmp$ cd $d
kev@genique:/tmp/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789$ mongod --dbpath /tmp/data
2016-07-06T11:52:10.470+1000 I -        [main] Assertion failure getcwd(buffer, 1000) src/mongo/db/server_options_helpers.cpp 450
2016-07-06T11:52:10.476+1000 I CONTROL  [main]
 0x12fa192 0x12a5538 0x1291074 0xf87cc1 0xcd0476 0x962ccb 0x95fa24 0x96ac2b 0x96b2dc 0x96b60e 0x962678 0x7f2d89ef2ec5 0x959d09
----- BEGIN BACKTRACE -----
{"backtrace":[{"b":"400000","o":"EFA192","s":"_ZN5mongo15printStackTraceERSo"},{"b":"400000","o":"EA5538","s":"_ZN5mongo10logContextEPKc"},{"b":"400000","o":"E91074","s":"_ZN5mongo12verifyFailedEPKcS1_j"},{"b":"400000","o":"B87CC1","s":"_ZN5mongo18storeServerOptionsERKNS_17optionenvironment11EnvironmentERKSt6vectorISsSaISsEE"},{"b":"400000","o":"8D0476","s":"_ZN5mongo18storeMongodOptionsERKNS_17optionenvironment11EnvironmentERKSt6vectorISsSaISsEE"},{"b":"400000","o":"562CCB","s":"_ZN5mongo45_mongoInitializerFunction_MongodOptions_StoreEPNS_18InitializerContextE"},{"b":"400000","o":"55FA24","s":"_ZNSt17_Function_handlerIFN5mongo6StatusEPNS0_18InitializerContextEEPS4_E9_M_invokeERKSt9_Any_dataS3_"},{"b":"400000","o":"56AC2B","s":"_ZNK5mongo11Initializer7executeERKSt6vectorISsSaISsEERKSt3mapISsSsSt4lessISsESaISt4pairIKSsSsEEE"},{"b":"400000","o":"56B2DC","s":"_ZN5mongo21runGlobalInitializersERKSt6vectorISsSaISsEERKSt3mapISsSsSt4lessISsESaISt4pairIKSsSsEEE"},{"b":"400000","o":"56B60E","s":"_ZN5mongo21runGlobalInitializersEiPKPKcS3_"},{"b":"400000","o":"562678","s":"main"},{"b":"7F2D89ED1000","o":"21EC5","s":"__libc_start_main"},{"b":"400000","o":"559D09"}]}
 mongod(_ZN5mongo15printStackTraceERSo+0x32) [0x12fa192]
 mongod(_ZN5mongo10logContextEPKc+0x138) [0x12a5538]
 mongod(_ZN5mongo12verifyFailedEPKcS1_j+0xB4) [0x1291074]
 mongod(_ZN5mongo18storeServerOptionsERKNS_17optionenvironment11EnvironmentERKSt6vectorISsSaISsEE+0x2C01) [0xf87cc1]
 mongod(_ZN5mongo18storeMongodOptionsERKNS_17optionenvironment11EnvironmentERKSt6vectorISsSaISsEE+0x26) [0xcd0476]
 mongod(_ZN5mongo45_mongoInitializerFunction_MongodOptions_StoreEPNS_18InitializerContextE+0x2B) [0x962ccb]
 mongod(_ZNSt17_Function_handlerIFN5mongo6StatusEPNS0_18InitializerContextEEPS4_E9_M_invokeERKSt9_Any_dataS3_+0x14) [0x95fa24]
 mongod(_ZNK5mongo11Initializer7executeERKSt6vectorISsSaISsEERKSt3mapISsSsSt4lessISsESaISt4pairIKSsSsEEE+0x1CB) [0x96ac2b]
 mongod(_ZN5mongo21runGlobalInitializersERKSt6vectorISsSaISsEERKSt3mapISsSsSt4lessISsESaISt4pairIKSsSsEEE+0x2C) [0x96b2dc]
 mongod(_ZN5mongo21runGlobalInitializersEiPKPKcS3_+0x31E) [0x96b60e]
 mongod(main+0x68) [0x962678]
 libc.so.6(__libc_start_main+0xF5) [0x7f2d89ef2ec5]
 mongod(+0x559D09) [0x959d09]
-----  END BACKTRACE  -----
2016-07-06T11:52:10.476+1000 F CONTROL  [main] Failed global initialization: UnknownError: assertion src/mongo/db/server_options_helpers.cpp:450
kev@genique:/tmp/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789/0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789$

https://github.com/mongodb/mongo/blob/r3.3.9/src/mongo/db/server_options_helpers.cpp#L458-L468

458
Status setupCwd() {
459
    // setup cwd
460
    char buffer[1024];
461
#ifdef _WIN32
462
    verify(_getcwd(buffer, 1000));
463
#else
464
    verify(getcwd(buffer, 1000));
465
#endif
466
    serverGlobalParams.cwd = buffer;
467
    return Status::OK();
468
}

A quick and easy (though partial) fix is to just change the hardcoded 1024 and 1000 to PATH_MAX (which POSIX defines as >= 4096), so as to be better aligned with OS limits (though not definitively so).

It turns out that POSIX requires PATH_MAX >= 256 (or >= 1024 for XSI). Darwin/OSX and Solaris are 1024, but Linux is 10k or more.

A fuller (but more involved) fix would be to dynamically allocate and grow the buffer as necessary.



 Comments   
Comment by Githook User [ 16/Aug/16 ]

Author:

{u'username': u'devkev', u'name': u'Kevin Pulo', u'email': u'kevin.pulo@mongodb.com'}

Message: SERVER-24914 SERVER-24915 improve getting cwd at server startup
Branch: master
https://github.com/mongodb/mongo/commit/512a1d57f6b3560cc3531355676cf06d362c93a1

Comment by Andy Schwerin [ 08/Jul/16 ]

Great, Kevin. Go ahead and put up a patch to use current_path() as you described.

Comment by Kevin Pulo [ 07/Jul/16 ]

boost::filesystem::current_path() can be used to do this the Right Way (doubling out the buffer size, etc), and is easy to use.

Generated at Thu Feb 08 04:07:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.