1 /* Copyright (C) 2002-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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/>. */
23 #include <stdio_ext.h>
26 #include <sys/resource.h>
28 #include <lowlevellock.h>
33 __pthread_getattr_np (pthread_t thread_id
, pthread_attr_t
*attr
)
35 struct pthread
*thread
= (struct pthread
*) thread_id
;
37 /* Prepare the new thread attribute. */
38 int ret
= __pthread_attr_init (attr
);
42 struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
44 lll_lock (thread
->lock
, LLL_PRIVATE
);
46 /* The thread library is responsible for keeping the values in the
47 thread desriptor up-to-date in case the user changes them. */
48 memcpy (&iattr
->schedparam
, &thread
->schedparam
,
49 sizeof (struct sched_param
));
50 iattr
->schedpolicy
= thread
->schedpolicy
;
52 /* Clear the flags work. */
53 iattr
->flags
= thread
->flags
;
55 /* The thread might be detached by now. */
56 if (IS_DETACHED (thread
))
57 iattr
->flags
|= ATTR_FLAG_DETACHSTATE
;
59 /* This is the guardsize after adjusting it. */
60 iattr
->guardsize
= thread
->reported_guardsize
;
62 /* The sizes are subject to alignment. */
63 if (__glibc_likely (thread
->stackblock
!= NULL
))
65 /* The stack size reported to the user should not include the
67 iattr
->stacksize
= thread
->stackblock_size
- thread
->guardsize
;
69 iattr
->stackaddr
= (char *) thread
->stackblock
70 + thread
->stackblock_size
;
72 iattr
->stackaddr
= (char *) thread
->stackblock
;
77 /* No stack information available. This must be for the initial
78 thread. Get the info in some magical way. */
80 /* Stack size limit. */
83 /* The safest way to get the top of the stack is to read
84 /proc/self/maps and locate the line into which
85 __libc_stack_end falls. */
86 FILE *fp
= fopen ("/proc/self/maps", "rce");
89 /* We need the limit of the stack in any case. */
92 if (__getrlimit (RLIMIT_STACK
, &rl
) != 0)
96 /* We consider the main process stack to have ended with
97 the page containing __libc_stack_end. There is stuff below
98 it in the stack too, like the program arguments, environment
99 variables and auxv info, but we ignore those pages when
100 returning size so that the output is consistent when the
101 stack is marked executable due to a loaded DSO requiring
103 void *stack_end
= (void *) ((uintptr_t) __libc_stack_end
104 & -(uintptr_t) GLRO(dl_pagesize
));
105 #if _STACK_GROWS_DOWN
106 stack_end
+= GLRO(dl_pagesize
);
108 /* We need no locking. */
109 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
111 /* Until we found an entry (which should always be the case)
112 mark the result as a failure. */
117 #if _STACK_GROWS_DOWN
118 uintptr_t last_to
= 0;
121 while (! feof_unlocked (fp
))
123 if (__getline (&line
, &linelen
, fp
) <= 0)
128 if (sscanf (line
, "%" SCNxPTR
"-%" SCNxPTR
, &from
, &to
) != 2)
130 if (from
<= (uintptr_t) __libc_stack_end
131 && (uintptr_t) __libc_stack_end
< to
)
133 /* Found the entry. Now we have the info we need. */
134 iattr
->stackaddr
= stack_end
;
136 rl
.rlim_cur
- (size_t) (to
- (uintptr_t) stack_end
);
138 /* Cut it down to align it to page size since otherwise we
139 risk going beyond rlimit when the kernel rounds up the
140 stack extension request. */
141 iattr
->stacksize
= (iattr
->stacksize
142 & -(intptr_t) GLRO(dl_pagesize
));
143 #if _STACK_GROWS_DOWN
144 /* The limit might be too high. */
145 if ((size_t) iattr
->stacksize
146 > (size_t) iattr
->stackaddr
- last_to
)
147 iattr
->stacksize
= (size_t) iattr
->stackaddr
- last_to
;
149 /* The limit might be too high. */
150 if ((size_t) iattr
->stacksize
151 > to
- (size_t) iattr
->stackaddr
)
152 iattr
->stacksize
= to
- (size_t) iattr
->stackaddr
;
154 /* We succeed and no need to look further. */
158 #if _STACK_GROWS_DOWN
170 iattr
->flags
|= ATTR_FLAG_STACKADDR
;
175 cpu_set_t
*cpuset
= NULL
;
181 void *newp
= realloc (cpuset
, size
);
187 cpuset
= (cpu_set_t
*) newp
;
189 ret
= __pthread_getaffinity_np (thread_id
, size
, cpuset
);
191 /* Pick some ridiculous upper limit. Is 8 million CPUs enough? */
192 while (ret
== EINVAL
&& size
< 1024 * 1024);
195 ret
= __pthread_attr_setaffinity_np (attr
, size
, cpuset
);
196 else if (ret
== ENOSYS
)
197 /* There is no such functionality. */
202 lll_unlock (thread
->lock
, LLL_PRIVATE
);
205 __pthread_attr_destroy (attr
);
209 versioned_symbol (libc
, __pthread_getattr_np
, pthread_getattr_np
, GLIBC_2_32
);
211 #if SHLIB_COMPAT (libc, GLIBC_2_2_3, GLIBC_2_32)
212 strong_alias (__pthread_getattr_np
, __pthread_getattr_np_alias
)
213 compat_symbol (libc
, __pthread_getattr_np_alias
,
214 pthread_getattr_np
, GLIBC_2_2_3
);