xref: /freebsd/lib/libthr/thread/thr_info.c (revision f81cdf24ba5436367377f7c8e8f51f6df2a75ca7)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5  * Copyright (c) 2018 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Konstantin Belousov
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #include "namespace.h"
38 #include <sys/errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <pthread.h>
42 #include <pthread_np.h>
43 #include "un-namespace.h"
44 
45 #include "thr_private.h"
46 
47 static void
48 thr_set_name_np(struct pthread *thread, char **tmp_name)
49 {
50 
51 	free(thread->name);
52 	thread->name = *tmp_name;
53 	*tmp_name = NULL;
54 }
55 
56 /* Set the thread name. */
57 __weak_reference(_pthread_setname_np, pthread_setname_np);
58 int
59 _pthread_setname_np(pthread_t thread, const char *name)
60 {
61 	struct pthread *curthread;
62 	char *tmp_name;
63 	int res;
64 
65 	if (name != NULL) {
66 		tmp_name = strdup(name);
67 		if (tmp_name == NULL)
68 			return (ENOMEM);
69 	} else {
70 		tmp_name = NULL;
71 	}
72 	curthread = _get_curthread();
73 	if (curthread == thread) {
74 		res = 0;
75 		THR_THREAD_LOCK(curthread, thread);
76 		if (thr_set_name(thread->tid, name) == -1)
77 			res = errno;
78 		else
79 			thr_set_name_np(thread, &tmp_name);
80 		THR_THREAD_UNLOCK(curthread, thread);
81 	} else {
82 		res = ESRCH;
83 		if (_thr_find_thread(curthread, thread, 0) == 0) {
84 			if (thread->state != PS_DEAD) {
85 				if (thr_set_name(thread->tid, name) == -1) {
86 					res = errno;
87 				} else {
88 					thr_set_name_np(thread, &tmp_name);
89 					res = 0;
90 				}
91 			}
92 			THR_THREAD_UNLOCK(curthread, thread);
93 		}
94 	}
95 	free(tmp_name);
96 	return (res);
97 }
98 
99 /* Set the thread name for debug. */
100 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
101 void
102 _pthread_set_name_np(pthread_t thread, const char *name)
103 {
104 	(void)_pthread_setname_np(thread, name);
105 }
106 
107 static void
108 thr_get_name_np(struct pthread *thread, char *buf, size_t len)
109 {
110 
111 	if (thread->name != NULL)
112 		strlcpy(buf, thread->name, len);
113 	else if (len > 0)
114 		buf[0] = '\0';
115 }
116 
117 __weak_reference(_thr_getname_np, pthread_getname_np);
118 __weak_reference(_thr_getname_np, _pthread_getname_np);
119 int
120 _thr_getname_np(pthread_t thread, char *buf, size_t len)
121 {
122 	struct pthread *curthread;
123 	int res;
124 
125 	res = 0;
126 	curthread = _get_curthread();
127 	if (curthread == thread) {
128 		THR_THREAD_LOCK(curthread, thread);
129 		thr_get_name_np(thread, buf, len);
130 		THR_THREAD_UNLOCK(curthread, thread);
131 	} else {
132 		if (_thr_find_thread(curthread, thread, 0) == 0) {
133 			if (thread->state != PS_DEAD)
134 				thr_get_name_np(thread, buf, len);
135 			else
136 				res = ESRCH;
137 			THR_THREAD_UNLOCK(curthread, thread);
138 		} else {
139 			res = ESRCH;
140 			if (len > 0)
141 				buf[0] = '\0';
142 		}
143 	}
144 	return (res);
145 }
146 
147 __weak_reference(_pthread_get_name_np, pthread_get_name_np);
148 void
149 _pthread_get_name_np(pthread_t thread, char *buf, size_t len)
150 {
151 	(void)_thr_getname_np(thread, buf, len);
152 }
153