| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 4 | * | 
|  | 5 | * Redistribution and use in source and binary forms, with or without | 
|  | 6 | * modification, are permitted provided that the following conditions | 
|  | 7 | * are met: | 
|  | 8 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <pthread.h> | 
|  | 30 |  | 
|  | 31 | #include <fcntl.h> | 
|  | 32 | #include <stdio.h> // For snprintf. | 
|  | 33 | #include <sys/prctl.h> | 
|  | 34 | #include <sys/stat.h> | 
|  | 35 | #include <sys/types.h> | 
|  | 36 | #include <unistd.h> | 
|  | 37 |  | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 38 | #include "pthread_accessor.h" | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 39 | #include "pthread_internal.h" | 
|  | 40 | #include "private/ErrnoRestorer.h" | 
|  | 41 |  | 
|  | 42 | // This value is not exported by kernel headers. | 
|  | 43 | #define MAX_TASK_COMM_LEN 16 | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 44 | #define TASK_COMM_FMT "/proc/self/task/%d/comm" | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 45 |  | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 46 | int pthread_setname_np(pthread_t t, const char* thread_name) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 47 | ErrnoRestorer errno_restorer; | 
|  | 48 |  | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 49 | if (thread_name == NULL) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 50 | return EINVAL; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | size_t thread_name_len = strlen(thread_name); | 
|  | 54 | if (thread_name_len >= MAX_TASK_COMM_LEN) { | 
|  | 55 | return ERANGE; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | // Changing our own name is an easy special case. | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 59 | if (t == pthread_self()) { | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 60 | return prctl(PR_SET_NAME, thread_name) ? errno : 0; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 63 | // We have to change another thread's name. | 
|  | 64 | pid_t tid = 0; | 
|  | 65 | { | 
|  | 66 | pthread_accessor thread(t); | 
|  | 67 | if (thread.get() == NULL) { | 
|  | 68 | return ESRCH; | 
|  | 69 | } | 
|  | 70 | tid = thread->tid; | 
|  | 71 | } | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 72 | char comm_name[sizeof(TASK_COMM_FMT) + 8]; | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 73 | snprintf(comm_name, sizeof(comm_name), TASK_COMM_FMT, tid); | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 74 | int fd = open(comm_name, O_WRONLY); | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 75 | if (fd == -1) { | 
|  | 76 | return errno; | 
|  | 77 | } | 
|  | 78 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, thread_name, thread_name_len)); | 
|  | 79 | close(fd); | 
|  | 80 |  | 
|  | 81 | if (n < 0) { | 
|  | 82 | return errno; | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 83 | } else if (n != static_cast<ssize_t>(thread_name_len)) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 84 | return EIO; | 
|  | 85 | } | 
|  | 86 | return 0; | 
|  | 87 | } |