Update install.texi, and regenerate INSTALL.
[glibc.git] / nptl / pthread_getattr_np.c
blob25807cb529880d67a6561b6ebcd45042e89dea3e
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/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdio_ext.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/resource.h>
27 #include "pthreadP.h"
28 #include <lowlevellock.h>
29 #include <ldsodefs.h>
32 int
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);
39 if (ret != 0)
40 return ret;
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
66 guard size. */
67 iattr->stacksize = thread->stackblock_size - thread->guardsize;
68 #if _STACK_GROWS_DOWN
69 iattr->stackaddr = (char *) thread->stackblock
70 + thread->stackblock_size;
71 #else
72 iattr->stackaddr = (char *) thread->stackblock;
73 #endif
75 else
77 /* No stack information available. This must be for the initial
78 thread. Get the info in some magical way. */
80 /* Stack size limit. */
81 struct rlimit rl;
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");
87 if (fp == NULL)
88 ret = errno;
89 /* We need the limit of the stack in any case. */
90 else
92 if (__getrlimit (RLIMIT_STACK, &rl) != 0)
93 ret = errno;
94 else
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
102 it. */
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);
107 #endif
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. */
113 ret = ENOENT;
115 char *line = NULL;
116 size_t linelen = 0;
117 #if _STACK_GROWS_DOWN
118 uintptr_t last_to = 0;
119 #endif
121 while (! feof_unlocked (fp))
123 if (__getline (&line, &linelen, fp) <= 0)
124 break;
126 uintptr_t from;
127 uintptr_t to;
128 if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
129 continue;
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;
135 iattr->stacksize =
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;
148 #else
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;
153 #endif
154 /* We succeed and no need to look further. */
155 ret = 0;
156 break;
158 #if _STACK_GROWS_DOWN
159 last_to = to;
160 #endif
163 free (line);
166 fclose (fp);
170 iattr->flags |= ATTR_FLAG_STACKADDR;
172 if (ret == 0)
174 size_t size = 16;
175 cpu_set_t *cpuset = NULL;
179 size <<= 1;
181 void *newp = realloc (cpuset, size);
182 if (newp == NULL)
184 ret = ENOMEM;
185 break;
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);
194 if (ret == 0)
195 ret = __pthread_attr_setaffinity_np (attr, size, cpuset);
196 else if (ret == ENOSYS)
197 /* There is no such functionality. */
198 ret = 0;
199 free (cpuset);
202 lll_unlock (thread->lock, LLL_PRIVATE);
204 if (ret != 0)
205 __pthread_attr_destroy (attr);
207 return ret;
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);
215 #endif