• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
34 #include <sys/prctl.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 
39 #include "private/ErrnoRestorer.h"
40 #include "pthread_internal.h"
41 
42 // This value is not exported by kernel headers.
43 #define MAX_TASK_COMM_LEN 16
44 
__open_task_comm_fd(pthread_t t,int flags)45 static int __open_task_comm_fd(pthread_t t, int flags) {
46   char comm_name[64];
47   snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm", pthread_gettid_np(t));
48   return open(comm_name, O_CLOEXEC | flags);
49 }
50 
pthread_getname_np(pthread_t t,char * buf,size_t buf_size)51 int pthread_getname_np(pthread_t t, char* buf, size_t buf_size) {
52   ErrnoRestorer errno_restorer;
53 
54   if (buf_size < MAX_TASK_COMM_LEN) return ERANGE;
55 
56   // Getting our own name is an easy special case.
57   if (t == pthread_self()) {
58     return prctl(PR_GET_NAME, buf) ? errno : 0;
59   }
60 
61   // We have to get another thread's name.
62   int fd = __open_task_comm_fd(t, O_RDONLY);
63   if (fd == -1) return errno;
64 
65   ssize_t n = TEMP_FAILURE_RETRY(read(fd, buf, buf_size));
66   close(fd);
67 
68   if (n == -1) return errno;
69 
70   // The kernel adds a trailing '\n' to the /proc file,
71   // so this is actually the normal case for short names.
72   if (n > 0 && buf[n - 1] == '\n') {
73     buf[n - 1] = '\0';
74     return 0;
75   }
76 
77   if (n == static_cast<ssize_t>(buf_size)) return ERANGE;
78   buf[n] = '\0';
79   return 0;
80 }
81 
pthread_setname_np(pthread_t t,const char * thread_name)82 int pthread_setname_np(pthread_t t, const char* thread_name) {
83   ErrnoRestorer errno_restorer;
84 
85   size_t thread_name_len = strlen(thread_name);
86   if (thread_name_len >= MAX_TASK_COMM_LEN) return ERANGE;
87 
88   // Setting our own name is an easy special case.
89   if (t == pthread_self()) {
90     return prctl(PR_SET_NAME, thread_name) ? errno : 0;
91   }
92 
93   // We have to set another thread's name.
94   int fd = __open_task_comm_fd(t, O_WRONLY);
95   if (fd == -1) return errno;
96 
97   ssize_t n = TEMP_FAILURE_RETRY(write(fd, thread_name, thread_name_len));
98   close(fd);
99 
100   if (n == -1) return errno;
101   if (n != static_cast<ssize_t>(thread_name_len)) return EIO;
102   return 0;
103 }
104