summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-03-22 05:11:35 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2013-03-22 05:11:35 +0000
commitbabb72dc292a567674f6d7d2ccf7f63edaf77b80 (patch)
treef1881b3f5abe1695225de18c0a63227ce8f7ef79
parent26c815c489197259e3692d4a1e35463989f7c7a3 (diff)
parenta41ba2f0bfc4fce1ce8f06a9c289102c440c929d (diff)
downloadbionic-babb72dc292a567674f6d7d2ccf7f63edaf77b80.zip
bionic-babb72dc292a567674f6d7d2ccf7f63edaf77b80.tar.gz
bionic-babb72dc292a567674f6d7d2ccf7f63edaf77b80.tar.bz2
Merge "Fix pthread_setname_np's behavior on invalid pthread_ts."
-rw-r--r--libc/bionic/pthread_setname_np.cpp22
-rw-r--r--tests/pthread_test.cpp2
2 files changed, 16 insertions, 8 deletions
diff --git a/libc/bionic/pthread_setname_np.cpp b/libc/bionic/pthread_setname_np.cpp
index 6162aea..e0ecace 100644
--- a/libc/bionic/pthread_setname_np.cpp
+++ b/libc/bionic/pthread_setname_np.cpp
@@ -35,6 +35,7 @@
#include <sys/types.h>
#include <unistd.h>
+#include "pthread_accessor.h"
#include "pthread_internal.h"
#include "private/ErrnoRestorer.h"
@@ -42,10 +43,10 @@
#define MAX_TASK_COMM_LEN 16
#define TASK_COMM_FMT "/proc/self/task/%d/comm"
-int pthread_setname_np(pthread_t thread, const char* thread_name) {
+int pthread_setname_np(pthread_t t, const char* thread_name) {
ErrnoRestorer errno_restorer;
- if (thread == 0 || thread_name == NULL) {
+ if (thread_name == NULL) {
return EINVAL;
}
@@ -55,14 +56,21 @@ int pthread_setname_np(pthread_t thread, const char* thread_name) {
}
// Changing our own name is an easy special case.
- if (thread == pthread_self()) {
+ if (t == pthread_self()) {
return prctl(PR_SET_NAME, thread_name) ? errno : 0;
}
- // Have to change another thread's name.
- pthread_internal_t* t = reinterpret_cast<pthread_internal_t*>(thread);
+ // We have to change another thread's name.
+ pid_t tid = 0;
+ {
+ pthread_accessor thread(t);
+ if (thread.get() == NULL) {
+ return ESRCH;
+ }
+ tid = thread->tid;
+ }
char comm_name[sizeof(TASK_COMM_FMT) + 8];
- snprintf(comm_name, sizeof(comm_name), TASK_COMM_FMT, t->tid);
+ snprintf(comm_name, sizeof(comm_name), TASK_COMM_FMT, tid);
int fd = open(comm_name, O_WRONLY);
if (fd == -1) {
return errno;
@@ -72,7 +80,7 @@ int pthread_setname_np(pthread_t thread, const char* thread_name) {
if (n < 0) {
return errno;
- } else if ((size_t)n != thread_name_len) {
+ } else if (n != static_cast<ssize_t>(thread_name_len)) {
return EIO;
}
return 0;
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 98bc4e5..a86cadc 100644
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -239,7 +239,7 @@ TEST(pthread, pthread_setname_np__no_such_thread) {
MakeDeadThread(dead_thread);
// Call pthread_setname_np after thread has already exited.
- ASSERT_EQ(ENOENT, pthread_setname_np(dead_thread, "short 3"));
+ ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
}
#endif