xref: /freebsd/lib/libthr/thread/thr_info.c (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
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 "namespace.h"
37 #include <sys/errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <pthread.h>
41 #include <pthread_np.h>
42 #include "un-namespace.h"
43 
44 #include "thr_private.h"
45 
46 static void
47 thr_set_name_np(struct pthread *thread, char **tmp_name)
48 {
49 
50 	free(thread->name);
51 	thread->name = *tmp_name;
52 	*tmp_name = NULL;
53 }
54 
55 /* Set the thread name. */
56 __weak_reference(_pthread_setname_np, pthread_setname_np);
57 int
58 _pthread_setname_np(pthread_t thread, const char *name)
59 {
60 	struct pthread *curthread;
61 	char *tmp_name;
62 	int res;
63 
64 	if (name != NULL) {
65 		tmp_name = strdup(name);
66 		if (tmp_name == NULL)
67 			return (ENOMEM);
68 	} else {
69 		tmp_name = NULL;
70 	}
71 	curthread = _get_curthread();
72 	if (curthread == thread) {
73 		res = 0;
74 		THR_THREAD_LOCK(curthread, thread);
75 		if (thr_set_name(thread->tid, name) == -1)
76 			res = errno;
77 		else
78 			thr_set_name_np(thread, &tmp_name);
79 		THR_THREAD_UNLOCK(curthread, thread);
80 	} else {
81 		res = ESRCH;
82 		if (_thr_find_thread(curthread, thread, 0) == 0) {
83 			if (thread->state != PS_DEAD) {
84 				if (thr_set_name(thread->tid, name) == -1) {
85 					res = errno;
86 				} else {
87 					thr_set_name_np(thread, &tmp_name);
88 					res = 0;
89 				}
90 			}
91 			THR_THREAD_UNLOCK(curthread, thread);
92 		}
93 	}
94 	free(tmp_name);
95 	return (res);
96 }
97 
98 /* Set the thread name for debug. */
99 __weak_reference(_pthread_set_name_np, pthread_set_name_np);
100 void
101 _pthread_set_name_np(pthread_t thread, const char *name)
102 {
103 	(void)_pthread_setname_np(thread, name);
104 }
105 
106 static void
107 thr_get_name_np(struct pthread *thread, char *buf, size_t len)
108 {
109 
110 	if (thread->name != NULL)
111 		strlcpy(buf, thread->name, len);
112 	else if (len > 0)
113 		buf[0] = '\0';
114 }
115 
116 __weak_reference(_thr_getname_np, pthread_getname_np);
117 __weak_reference(_thr_getname_np, _pthread_getname_np);
118 int
119 _thr_getname_np(pthread_t thread, char *buf, size_t len)
120 {
121 	struct pthread *curthread;
122 	int res;
123 
124 	res = 0;
125 	curthread = _get_curthread();
126 	if (curthread == thread) {
127 		THR_THREAD_LOCK(curthread, thread);
128 		thr_get_name_np(thread, buf, len);
129 		THR_THREAD_UNLOCK(curthread, thread);
130 	} else {
131 		if (_thr_find_thread(curthread, thread, 0) == 0) {
132 			if (thread->state != PS_DEAD)
133 				thr_get_name_np(thread, buf, len);
134 			else
135 				res = ESRCH;
136 			THR_THREAD_UNLOCK(curthread, thread);
137 		} else {
138 			res = ESRCH;
139 			if (len > 0)
140 				buf[0] = '\0';
141 		}
142 	}
143 	return (res);
144 }
145 
146 __weak_reference(_pthread_get_name_np, pthread_get_name_np);
147 void
148 _pthread_get_name_np(pthread_t thread, char *buf, size_t len)
149 {
150 	(void)_thr_getname_np(thread, buf, len);
151 }
152