blob: 5236e4dbe0e0db2b9d9a235066f4968bc9f913d6 [file] [log] [blame]
Elliott Hughes3e898472013-02-12 16:40:24 +00001/*
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 Hughes05fc1d72015-01-28 18:02:33 -080033#include <string.h>
Elliott Hughes3e898472013-02-12 16:40:24 +000034#include <sys/prctl.h>
35#include <sys/stat.h>
36#include <sys/types.h>
37#include <unistd.h>
38
Evgeny Eltsin8a18af52019-09-25 16:55:47 +020039#include "private/bionic_defs.h"
Elliott Hughes3e898472013-02-12 16:40:24 +000040#include "private/ErrnoRestorer.h"
Yabin Cui673b15e2015-03-19 14:19:19 -070041#include "pthread_internal.h"
Elliott Hughes3e898472013-02-12 16:40:24 +000042
43// This value is not exported by kernel headers.
44#define MAX_TASK_COMM_LEN 16
Elliott Hughes725b2a92016-03-23 11:20:47 -070045
Elliott Hughes5bb113c2019-02-01 16:31:10 -080046static int __open_task_comm_fd(pthread_t t, int flags, const char* caller) {
Elliott Hughes725b2a92016-03-23 11:20:47 -070047 char comm_name[64];
Elliott Hughes5bb113c2019-02-01 16:31:10 -080048 snprintf(comm_name, sizeof(comm_name), "/proc/self/task/%d/comm",
49 __pthread_internal_gettid(t, caller));
Elliott Hughes725b2a92016-03-23 11:20:47 -070050 return open(comm_name, O_CLOEXEC | flags);
51}
52
Evgeny Eltsin8a18af52019-09-25 16:55:47 +020053__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughes725b2a92016-03-23 11:20:47 -070054int pthread_getname_np(pthread_t t, char* buf, size_t buf_size) {
55 ErrnoRestorer errno_restorer;
56
57 if (buf_size < MAX_TASK_COMM_LEN) return ERANGE;
58
59 // Getting our own name is an easy special case.
60 if (t == pthread_self()) {
61 return prctl(PR_GET_NAME, buf) ? errno : 0;
62 }
63
64 // We have to get another thread's name.
Elliott Hughes5bb113c2019-02-01 16:31:10 -080065 int fd = __open_task_comm_fd(t, O_RDONLY, "pthread_getname_np");
Elliott Hughes725b2a92016-03-23 11:20:47 -070066 if (fd == -1) return errno;
67
68 ssize_t n = TEMP_FAILURE_RETRY(read(fd, buf, buf_size));
69 close(fd);
70
71 if (n == -1) return errno;
72
73 // The kernel adds a trailing '\n' to the /proc file,
74 // so this is actually the normal case for short names.
75 if (n > 0 && buf[n - 1] == '\n') {
76 buf[n - 1] = '\0';
77 return 0;
78 }
79
80 if (n == static_cast<ssize_t>(buf_size)) return ERANGE;
81 buf[n] = '\0';
82 return 0;
83}
Elliott Hughes3e898472013-02-12 16:40:24 +000084
Evgeny Eltsin8a18af52019-09-25 16:55:47 +020085__BIONIC_WEAK_FOR_NATIVE_BRIDGE
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070086int pthread_setname_np(pthread_t t, const char* thread_name) {
Elliott Hughes3e898472013-02-12 16:40:24 +000087 ErrnoRestorer errno_restorer;
88
Elliott Hughes3e898472013-02-12 16:40:24 +000089 size_t thread_name_len = strlen(thread_name);
Elliott Hughes725b2a92016-03-23 11:20:47 -070090 if (thread_name_len >= MAX_TASK_COMM_LEN) return ERANGE;
Elliott Hughes3e898472013-02-12 16:40:24 +000091
Elliott Hughes725b2a92016-03-23 11:20:47 -070092 // Setting our own name is an easy special case.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070093 if (t == pthread_self()) {
Elliott Hughes40eabe22013-02-14 18:59:37 -080094 return prctl(PR_SET_NAME, thread_name) ? errno : 0;
Elliott Hughes3e898472013-02-12 16:40:24 +000095 }
96
Elliott Hughes725b2a92016-03-23 11:20:47 -070097 // We have to set another thread's name.
Elliott Hughes5bb113c2019-02-01 16:31:10 -080098 int fd = __open_task_comm_fd(t, O_WRONLY, "pthread_setname_np");
Elliott Hughes725b2a92016-03-23 11:20:47 -070099 if (fd == -1) return errno;
Yabin Cui673b15e2015-03-19 14:19:19 -0700100
Elliott Hughes3e898472013-02-12 16:40:24 +0000101 ssize_t n = TEMP_FAILURE_RETRY(write(fd, thread_name, thread_name_len));
102 close(fd);
103
Elliott Hughes725b2a92016-03-23 11:20:47 -0700104 if (n == -1) return errno;
105 if (n != static_cast<ssize_t>(thread_name_len)) return EIO;
Elliott Hughes3e898472013-02-12 16:40:24 +0000106 return 0;
107}