blob: c4e9fb85c2b690bf91d4f31b6e0cd8de07244c44 [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
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070039#include "pthread_accessor.h"
Elliott Hughes3e898472013-02-12 16:40:24 +000040#include "pthread_internal.h"
41#include "private/ErrnoRestorer.h"
42
43// This value is not exported by kernel headers.
44#define MAX_TASK_COMM_LEN 16
Elliott Hughes40eabe22013-02-14 18:59:37 -080045#define TASK_COMM_FMT "/proc/self/task/%d/comm"
Elliott Hughes3e898472013-02-12 16:40:24 +000046
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070047int pthread_setname_np(pthread_t t, const char* thread_name) {
Elliott Hughes3e898472013-02-12 16:40:24 +000048 ErrnoRestorer errno_restorer;
49
Elliott Hughes3e898472013-02-12 16:40:24 +000050 size_t thread_name_len = strlen(thread_name);
51 if (thread_name_len >= MAX_TASK_COMM_LEN) {
52 return ERANGE;
53 }
54
55 // Changing our own name is an easy special case.
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070056 if (t == pthread_self()) {
Elliott Hughes40eabe22013-02-14 18:59:37 -080057 return prctl(PR_SET_NAME, thread_name) ? errno : 0;
Elliott Hughes3e898472013-02-12 16:40:24 +000058 }
59
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070060 // We have to change another thread's name.
61 pid_t tid = 0;
62 {
63 pthread_accessor thread(t);
64 if (thread.get() == NULL) {
Elliott Hughes68d98d82014-11-12 21:03:26 -080065 return ENOENT;
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070066 }
67 tid = thread->tid;
68 }
Elliott Hughes3e898472013-02-12 16:40:24 +000069 char comm_name[sizeof(TASK_COMM_FMT) + 8];
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070070 snprintf(comm_name, sizeof(comm_name), TASK_COMM_FMT, tid);
Elliott Hughesf73183f2014-08-26 16:20:59 -070071 int fd = open(comm_name, O_CLOEXEC | O_WRONLY);
Elliott Hughes3e898472013-02-12 16:40:24 +000072 if (fd == -1) {
73 return errno;
74 }
75 ssize_t n = TEMP_FAILURE_RETRY(write(fd, thread_name, thread_name_len));
76 close(fd);
77
78 if (n < 0) {
79 return errno;
Elliott Hughesa41ba2f2013-03-21 20:02:35 -070080 } else if (n != static_cast<ssize_t>(thread_name_len)) {
Elliott Hughes3e898472013-02-12 16:40:24 +000081 return EIO;
82 }
83 return 0;
84}