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