1/*
2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3 * Copyright (c) 2018 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Konstantin Belousov
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of any co-contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: stable/11/lib/libthr/thread/thr_info.c 349984 2019-07-14 05:41:14Z kib $");
36
37#include "namespace.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__weak_reference(_pthread_set_name_np, pthread_set_name_np);
47
48static void
49thr_set_name_np(struct pthread *thread, const char *name)
50{
51
52	free(thread->name);
53	thread->name = name != NULL ? strdup(name) : NULL;
54}
55
56/* Set the thread name for debug. */
57void
58_pthread_set_name_np(pthread_t thread, const char *name)
59{
60	struct pthread *curthread;
61
62	curthread = _get_curthread();
63	if (curthread == thread) {
64		THR_THREAD_LOCK(curthread, thread);
65		thr_set_name(thread->tid, name);
66		thr_set_name_np(thread, name);
67		THR_THREAD_UNLOCK(curthread, thread);
68	} else {
69		if (_thr_find_thread(curthread, thread, 0) == 0) {
70			if (thread->state != PS_DEAD) {
71				thr_set_name(thread->tid, name);
72				thr_set_name_np(thread, name);
73			}
74			THR_THREAD_UNLOCK(curthread, thread);
75		}
76	}
77}
78
79static void
80thr_get_name_np(struct pthread *thread, char *buf, size_t len)
81{
82
83	if (thread->name != NULL)
84		strlcpy(buf, thread->name, len);
85	else if (len > 0)
86		buf[0] = '\0';
87}
88
89__weak_reference(_pthread_get_name_np, pthread_get_name_np);
90
91void
92_pthread_get_name_np(pthread_t thread, char *buf, size_t len)
93{
94	struct pthread *curthread;
95
96	curthread = _get_curthread();
97	if (curthread == thread) {
98		THR_THREAD_LOCK(curthread, thread);
99		thr_get_name_np(thread, buf, len);
100		THR_THREAD_UNLOCK(curthread, thread);
101	} else {
102		if (_thr_find_thread(curthread, thread, 0) == 0) {
103			if (thread->state != PS_DEAD)
104				thr_get_name_np(thread, buf, len);
105			THR_THREAD_UNLOCK(curthread, thread);
106		} else if (len > 0)
107			buf[0] = '\0';
108	}
109}
110