Linux: consolidate chown implementation
[glibc.git] / nptl_db / td_thr_validate.c
blob93762d5f0b366e26e51475dd395ac4a6b09755ff
1 /* Validate a thread handle.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include "thread_dbP.h"
20 #include <stdbool.h>
22 td_err_e
23 __td_ta_stack_user (td_thragent_t *ta, psaddr_t *plist)
25 if (__td_ta_rtld_global (ta))
26 return DB_GET_FIELD_ADDRESS (*plist, ta, ta->ta_addr__rtld_global,
27 rtld_global, _dl_stack_user, 0);
28 else
30 if (ta->ta_addr__dl_stack_user == 0
31 && td_mod_lookup (ta->ph, NULL, SYM__dl_stack_user,
32 &ta->ta_addr__dl_stack_user) != PS_OK)
33 return TD_ERR;
34 *plist = ta->ta_addr__dl_stack_user;
35 return TD_OK;
39 td_err_e
40 __td_ta_stack_used (td_thragent_t *ta, psaddr_t *plist)
43 if (__td_ta_rtld_global (ta))
44 return DB_GET_FIELD_ADDRESS (*plist, ta, ta->ta_addr__rtld_global,
45 rtld_global, _dl_stack_used, 0);
46 else
48 if (ta->ta_addr__dl_stack_used == 0
49 && td_mod_lookup (ta->ph, NULL, SYM__dl_stack_used,
50 &ta->ta_addr__dl_stack_used) != PS_OK)
51 return TD_ERR;
52 *plist = ta->ta_addr__dl_stack_used;
53 return TD_OK;
57 static td_err_e
58 check_thread_list (const td_thrhandle_t *th, psaddr_t head, bool *uninit)
60 td_err_e err;
61 psaddr_t next, ofs;
63 err = DB_GET_FIELD (next, th->th_ta_p, head, list_t, next, 0);
64 if (err == TD_OK)
66 if (next == 0)
68 *uninit = true;
69 return TD_NOTHR;
71 err = DB_GET_FIELD_ADDRESS (ofs, th->th_ta_p, 0, pthread, list, 0);
74 while (err == TD_OK)
76 if (next == head)
77 return TD_NOTHR;
79 if (next - (ofs - (psaddr_t) 0) == th->th_unique)
80 return TD_OK;
82 err = DB_GET_FIELD (next, th->th_ta_p, next, list_t, next, 0);
85 return err;
89 td_err_e
90 td_thr_validate (const td_thrhandle_t *th)
92 td_err_e err;
93 psaddr_t list;
95 LOG ("td_thr_validate");
97 /* First check the list with threads using user allocated stacks. */
98 bool uninit = false;
99 err = __td_ta_stack_user (th->th_ta_p, &list);
100 if (err == TD_OK)
101 err = check_thread_list (th, list, &uninit);
103 /* If our thread is not on this list search the list with stack
104 using implementation allocated stacks. */
105 if (err == TD_NOTHR)
107 err = __td_ta_stack_used (th->th_ta_p, &list);
108 if (err == TD_OK)
109 err = check_thread_list (th, list, &uninit);
111 if (err == TD_NOTHR && uninit && th->th_unique == 0)
112 /* __pthread_initialize_minimal has not run yet.
113 There is only the special case thread handle. */
114 err = TD_OK;
117 return err;