[BZ #2510, BZ #2830, BZ #3137, BZ #3313, BZ #3426, BZ #3465, BZ #3480, BZ #3483,...
[glibc.git] / nptl / pthread_getattr_np.c
blob4bdc7b5b150a853e26162f4c342257b9a1642f4d
1 /* Copyright (C) 2002, 2003, 2004, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <stdio.h>
24 #include <stdio_ext.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/resource.h>
28 #include "pthreadP.h"
29 #include <lowlevellock.h>
30 #include <ldsodefs.h>
33 int
34 pthread_getattr_np (thread_id, attr)
35 pthread_t thread_id;
36 pthread_attr_t *attr;
38 struct pthread *thread = (struct pthread *) thread_id;
39 struct pthread_attr *iattr = (struct pthread_attr *) attr;
40 int ret = 0;
42 /* We have to handle cancellation in the following code since we are
43 locking another threads desriptor. */
44 pthread_cleanup_push ((void (*) (void *)) lll_unlock_wake_cb, &thread->lock);
46 lll_lock (thread->lock);
48 /* The thread library is responsible for keeping the values in the
49 thread desriptor up-to-date in case the user changes them. */
50 memcpy (&iattr->schedparam, &thread->schedparam,
51 sizeof (struct sched_param));
52 iattr->schedpolicy = thread->schedpolicy;
54 /* Clear the flags work. */
55 iattr->flags = thread->flags;
57 /* The thread might be detached by now. */
58 if (IS_DETACHED (thread))
59 iattr->flags |= ATTR_FLAG_DETACHSTATE;
61 /* This is the guardsize after adjusting it. */
62 iattr->guardsize = thread->reported_guardsize;
64 /* The sizes are subject to alignment. */
65 if (__builtin_expect (thread->stackblock != NULL, 1))
67 iattr->stacksize = thread->stackblock_size;
68 iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
70 else
72 /* No stack information available. This must be for the initial
73 thread. Get the info in some magical way. */
74 assert (abs (thread->pid) == thread->tid);
76 /* Stack size limit. */
77 struct rlimit rl;
79 /* The safest way to get the top of the stack is to read
80 /proc/self/maps and locate the line into which
81 __libc_stack_end falls. */
82 FILE *fp = fopen ("/proc/self/maps", "rc");
83 if (fp == NULL)
84 ret = errno;
85 /* We need the limit of the stack in any case. */
86 else
88 if (getrlimit (RLIMIT_STACK, &rl) != 0)
89 ret = errno;
90 else
92 /* We need no locking. */
93 __fsetlocking (fp, FSETLOCKING_BYCALLER);
95 /* Until we found an entry (which should always be the case)
96 mark the result as a failure. */
97 ret = ENOENT;
99 char *line = NULL;
100 size_t linelen = 0;
101 uintptr_t last_to = 0;
103 while (! feof_unlocked (fp))
105 if (__getdelim (&line, &linelen, '\n', fp) <= 0)
106 break;
108 uintptr_t from;
109 uintptr_t to;
110 if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
111 continue;
112 if (from <= (uintptr_t) __libc_stack_end
113 && (uintptr_t) __libc_stack_end < to)
115 /* Found the entry. Now we have the info we need. */
116 iattr->stacksize = rl.rlim_cur;
117 iattr->stackaddr = (void *) to;
119 /* The limit might be too high. */
120 if ((size_t) iattr->stacksize
121 > (size_t) iattr->stackaddr - last_to)
122 iattr->stacksize = (size_t) iattr->stackaddr - last_to;
124 /* We succeed and no need to look further. */
125 ret = 0;
126 break;
128 last_to = to;
131 free (line);
134 fclose (fp);
138 iattr->flags |= ATTR_FLAG_STACKADDR;
140 if (ret == 0)
142 size_t size = 16;
143 cpu_set_t *cpuset = NULL;
147 size <<= 1;
149 void *newp = realloc (cpuset, size);
150 if (newp == NULL)
152 ret = ENOMEM;
153 break;
155 cpuset = (cpu_set_t *) newp;
157 ret = __pthread_getaffinity_np (thread_id, size, cpuset);
159 /* Pick some ridiculous upper limit. Is 8 million CPUs enough? */
160 while (ret == EINVAL && size < 1024 * 1024);
162 if (ret == 0)
164 iattr->cpuset = cpuset;
165 iattr->cpusetsize = size;
167 else
169 free (cpuset);
170 if (ret == ENOSYS)
171 /* There is no such functionality. */
172 ret = 0;
176 lll_unlock (thread->lock);
178 pthread_cleanup_pop (0);
180 return ret;