Support to set thread name in ThreadUtil

Netd resolver uses ThreadUtil to create lookup threads.
This change facilitates DNS debugging by means of appending
uid to the thread name.

MDnsSdListener monitor thread is renamed to MDnsSdMonitor.

Bug: 128404051
Test: cd systen/netd && atest
Change-Id: I7ea2edef3f1eb0fffe8ea00fb7b281dac7b7ef2e
diff --git a/libnetdutils/include/netdutils/ThreadUtil.h b/libnetdutils/include/netdutils/ThreadUtil.h
index 2ef97ef..62e6f70 100644
--- a/libnetdutils/include/netdutils/ThreadUtil.h
+++ b/libnetdutils/include/netdutils/ThreadUtil.h
@@ -34,9 +34,22 @@
     pthread_attr_t attr;
 };
 
+inline void setThreadName(std::string name) {
+    // MAX_TASK_COMM_LEN=16 is not exported by bionic.
+    const size_t MAX_TASK_COMM_LEN = 16;
+
+    // Crop name to 16 bytes including the NUL byte, as required by pthread_setname_np()
+    if (name.size() >= MAX_TASK_COMM_LEN) name.resize(MAX_TASK_COMM_LEN - 1);
+
+    if (int ret = pthread_setname_np(pthread_self(), name.c_str()); ret != 0) {
+        LOG(WARNING) << "Unable to set thread name to " << name << ": " << strerror(ret);
+    }
+}
+
 template <typename T>
 inline void* runAndDelete(void* obj) {
     std::unique_ptr<T> handler(reinterpret_cast<T*>(obj));
+    setThreadName(handler->threadName().c_str());
     handler->run();
     return nullptr;
 }