diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-04-28 17:43:46 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-04-28 17:43:46 -0400 |
commit | 4213c76de48f4e5c08009f8f347f817a77593019 (patch) | |
tree | ef66a18c65c3fb27d04ab0e67c949c5ca2201e00 | |
parent | 883f3b86836d03c2076ac6e2e601a34d9580ed34 (diff) | |
download | mongo-4213c76de48f4e5c08009f8f347f817a77593019.tar.gz |
SERVER-27281 pkill/pgrep cannot find mongod on Linux
(cherry picked from commit 726cafd713c7333640f8458ec9808ed4f678e3a7)
-rw-r--r-- | src/mongo/util/concurrency/thread_name.cpp | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/src/mongo/util/concurrency/thread_name.cpp b/src/mongo/util/concurrency/thread_name.cpp index 88859388d5f..78122ed213f 100644 --- a/src/mongo/util/concurrency/thread_name.cpp +++ b/src/mongo/util/concurrency/thread_name.cpp @@ -39,6 +39,10 @@ #if defined(__APPLE__) #include <sys/proc_info.h> #endif +#if defined(__linux__) +#include <sys/syscall.h> +#include <sys/types.h> +#endif #include "mongo/base/init.h" #include "mongo/config.h" @@ -116,21 +120,26 @@ void setThreadName(StringData name) { log() << "Ignoring error from setting thread name: " << errnoWithDescription(error); } #elif defined(__linux__) && defined(MONGO_CONFIG_HAVE_PTHREAD_SETNAME_NP) - // Maximum thread name length supported on Linux is 16 including the null terminator. Ideally - // we use short and descriptive thread names that fit: this helps for log readibility as well. - // Since several components set verbose thread names with a uniqifier at the end, we do a split - // truncation of "first7bytes.last7bytes". - int error = 0; - if (threadName->size() > 15) { - std::string shortName = - threadName->substr(0, 7) + '.' + threadName->substr(threadName->size() - 7); - error = pthread_setname_np(pthread_self(), shortName.c_str()); - } else { - error = pthread_setname_np(pthread_self(), threadName->c_str()); - } - - if (error) { - log() << "Ignoring error from setting thread name: " << errnoWithDescription(error); + // Do not set thread name on the main() thread. Setting the name on main thread breaks + // pgrep/pkill since these programs base this name on /proc/*/status which displays the thread + // name, not the executable name. + if (getpid() != syscall(SYS_gettid)) { + // Maximum thread name length supported on Linux is 16 including the null terminator. + // Ideally we use short and descriptive thread names that fit: this helps for log + // readibility as well. Still, as the limit is so low and a few current names exceed the + // limit, it's best to shorten long names. + int error = 0; + if (threadName->size() > 15) { + std::string shortName = + threadName->substr(0, 7) + '.' + threadName->substr(threadName->size() - 7); + error = pthread_setname_np(pthread_self(), shortName.c_str()); + } else { + error = pthread_setname_np(pthread_self(), threadName->c_str()); + } + + if (error) { + log() << "Ignoring error from setting thread name: " << errnoWithDescription(error); + } } #endif } |