| 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. | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 33 | #include <string.h> | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 34 | #include <sys/prctl.h> | 
|  | 35 | #include <sys/stat.h> | 
|  | 36 | #include <sys/types.h> | 
|  | 37 | #include <unistd.h> | 
|  | 38 |  | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 39 | #include "private/ErrnoRestorer.h" | 
| Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 40 | #include "pthread_internal.h" | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 41 |  | 
|  | 42 | // This value is not exported by kernel headers. | 
|  | 43 | #define MAX_TASK_COMM_LEN 16 | 
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | static int __open_task_comm_fd(pthread_t t, int flags) { | 
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 46 | pthread_internal_t* thread = __pthread_internal_find(t); | 
|  | 47 | if (thread == nullptr) { | 
|  | 48 | errno = ENOENT; | 
|  | 49 | return -1; | 
|  | 50 | } | 
|  | 51 |  | 
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 52 | char comm_name[64]; | 
| Elliott Hughes | bcb1529 | 2017-02-07 21:05:30 +0000 | [diff] [blame] | 53 | snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm", thread->tid); | 
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 54 | return open(comm_name, O_CLOEXEC | flags); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | int pthread_getname_np(pthread_t t, char* buf, size_t buf_size) { | 
|  | 58 | ErrnoRestorer errno_restorer; | 
|  | 59 |  | 
|  | 60 | if (buf_size < MAX_TASK_COMM_LEN) return ERANGE; | 
|  | 61 |  | 
|  | 62 | // Getting our own name is an easy special case. | 
|  | 63 | if (t == pthread_self()) { | 
|  | 64 | return prctl(PR_GET_NAME, buf) ? errno : 0; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // We have to get another thread's name. | 
|  | 68 | int fd = __open_task_comm_fd(t, O_RDONLY); | 
|  | 69 | if (fd == -1) return errno; | 
|  | 70 |  | 
|  | 71 | ssize_t n = TEMP_FAILURE_RETRY(read(fd, buf, buf_size)); | 
|  | 72 | close(fd); | 
|  | 73 |  | 
|  | 74 | if (n == -1) return errno; | 
|  | 75 |  | 
|  | 76 | // The kernel adds a trailing '\n' to the /proc file, | 
|  | 77 | // so this is actually the normal case for short names. | 
|  | 78 | if (n > 0 && buf[n - 1] == '\n') { | 
|  | 79 | buf[n - 1] = '\0'; | 
|  | 80 | return 0; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | if (n == static_cast<ssize_t>(buf_size)) return ERANGE; | 
|  | 84 | buf[n] = '\0'; | 
|  | 85 | return 0; | 
|  | 86 | } | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 87 |  | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 88 | int pthread_setname_np(pthread_t t, const char* thread_name) { | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 89 | ErrnoRestorer errno_restorer; | 
|  | 90 |  | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 91 | size_t thread_name_len = strlen(thread_name); | 
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 92 | if (thread_name_len >= MAX_TASK_COMM_LEN) return ERANGE; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 93 |  | 
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 94 | // Setting our own name is an easy special case. | 
| Elliott Hughes | a41ba2f | 2013-03-21 20:02:35 -0700 | [diff] [blame] | 95 | if (t == pthread_self()) { | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 96 | return prctl(PR_SET_NAME, thread_name) ? errno : 0; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 99 | // We have to set another thread's name. | 
|  | 100 | int fd = __open_task_comm_fd(t, O_WRONLY); | 
|  | 101 | if (fd == -1) return errno; | 
| Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 102 |  | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 103 | ssize_t n = TEMP_FAILURE_RETRY(write(fd, thread_name, thread_name_len)); | 
|  | 104 | close(fd); | 
|  | 105 |  | 
| Elliott Hughes | 725b2a9 | 2016-03-23 11:20:47 -0700 | [diff] [blame] | 106 | if (n == -1) return errno; | 
|  | 107 | if (n != static_cast<ssize_t>(thread_name_len)) return EIO; | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 108 | return 0; | 
|  | 109 | } |