Skip to content

Commit

Permalink
Bugfix: set thread name will fail, when running as differnt user.
Browse files Browse the repository at this point in the history
When using setuid/setgid, main thread don't have access to
/proc/self/task/[tid]/comm, which is owned by newly created
thread (root).

To fix this, pthread_setname_np() was moved to newly created
thread, and now it changes name for itself.

Signed-off-by: Igor Podoski <igor.podoski@ts.fujitsu.com>
  • Loading branch information
Igor Podoski committed Feb 29, 2016
1 parent 67b6e4a commit f22a097
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/common/Thread.cc
Expand Up @@ -54,7 +54,8 @@ Thread::Thread()
pid(0),
ioprio_class(-1),
ioprio_priority(-1),
cpuid(-1)
cpuid(-1),
thread_name(NULL)
{
}

Expand All @@ -81,6 +82,8 @@ void *Thread::entry_wrapper()
}
if (pid && cpuid >= 0)
_set_affinity(cpuid);

pthread_setname_np(pthread_self(), thread_name);
return entry();
}

Expand Down Expand Up @@ -145,16 +148,16 @@ int Thread::try_create(size_t stacksize)

void Thread::create(const char *name, size_t stacksize)
{
assert(strlen(name) < 16);
thread_name = name;

int ret = try_create(stacksize);
if (ret != 0) {
char buf[256];
snprintf(buf, sizeof(buf), "Thread::try_create(): pthread_create "
"failed with error %d", ret);
dout_emergency(buf);
assert(ret == 0);
} else if (thread_id > 0) {
assert(strlen(name) < 16);
pthread_setname_np(thread_id, name);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/common/Thread.h
Expand Up @@ -25,6 +25,7 @@ class Thread {
pid_t pid;
int ioprio_class, ioprio_priority;
int cpuid;
const char *thread_name;

void *entry_wrapper();

Expand Down

0 comments on commit f22a097

Please sign in to comment.