OSDN Git Service

mkostemp: fix implementation
[uclinux-h8/uClibc.git] / libpthread / nptl / pthread_getattr_np.c
1 /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
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    <http://www.gnu.org/licenses/>.  */
18
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>
30
31
32 int
33 pthread_getattr_np (
34      pthread_t thread_id,
35      pthread_attr_t *attr)
36 {
37   struct pthread *thread = (struct pthread *) thread_id;
38   struct pthread_attr *iattr = (struct pthread_attr *) attr;
39   int ret = 0;
40
41   lll_lock (thread->lock, LLL_PRIVATE);
42
43   /* The thread library is responsible for keeping the values in the
44      thread desriptor up-to-date in case the user changes them.  */
45   memcpy (&iattr->schedparam, &thread->schedparam,
46           sizeof (struct sched_param));
47   iattr->schedpolicy = thread->schedpolicy;
48
49   /* Clear the flags work.  */
50   iattr->flags = thread->flags;
51
52   /* The thread might be detached by now.  */
53   if (IS_DETACHED (thread))
54     iattr->flags |= ATTR_FLAG_DETACHSTATE;
55
56   /* This is the guardsize after adjusting it.  */
57   iattr->guardsize = thread->reported_guardsize;
58
59   /* The sizes are subject to alignment.  */
60   if (__builtin_expect (thread->stackblock != NULL, 1))
61     {
62       iattr->stacksize = thread->stackblock_size;
63       iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
64     }
65   else
66     {
67       /* No stack information available.  This must be for the initial
68          thread.  Get the info in some magical way.  */
69       assert (abs (thread->pid) == thread->tid);
70
71       /* Stack size limit.  */
72       struct rlimit rl;
73
74       /* The safest way to get the top of the stack is to read
75          /proc/self/maps and locate the line into which
76          __libc_stack_end falls.  */
77       FILE *fp = fopen ("/proc/self/maps", "rc");
78       if (fp == NULL)
79         ret = errno;
80       /* We need the limit of the stack in any case.  */
81       else
82         {
83           if (getrlimit (RLIMIT_STACK, &rl) != 0)
84             ret = errno;
85           else
86             {
87               /* We need no locking.  */
88               __fsetlocking (fp, FSETLOCKING_BYCALLER);
89
90               /* Until we found an entry (which should always be the case)
91                  mark the result as a failure.  */
92               ret = ENOENT;
93
94               char *line = NULL;
95               size_t linelen = 0;
96               uintptr_t last_to = 0;
97
98               while (! feof_unlocked (fp))
99                 {
100                   if (getdelim (&line, &linelen, '\n', fp) <= 0)
101                     break;
102
103                   uintptr_t from;
104                   uintptr_t to;
105                   if (sscanf (line, "%" SCNxPTR "-%" SCNxPTR, &from, &to) != 2)
106                     continue;
107                   if (from <= (uintptr_t) __libc_stack_end
108                       && (uintptr_t) __libc_stack_end < to)
109                     {
110                       /* Found the entry.  Now we have the info we need.  */
111                       iattr->stacksize = rl.rlim_cur;
112                       iattr->stackaddr = (void *) to;
113
114                       /* The limit might be too high.  */
115                       if ((size_t) iattr->stacksize
116                           > (size_t) iattr->stackaddr - last_to)
117                         iattr->stacksize = (size_t) iattr->stackaddr - last_to;
118
119                       /* We succeed and no need to look further.  */
120                       ret = 0;
121                       break;
122                     }
123                   last_to = to;
124                 }
125
126               free (line);
127             }
128
129           fclose (fp);
130         }
131     }
132
133   iattr->flags |= ATTR_FLAG_STACKADDR;
134
135   if (ret == 0)
136     {
137       size_t size = 16;
138       cpu_set_t *cpuset = NULL;
139
140       do
141         {
142           size <<= 1;
143
144           void *newp = realloc (cpuset, size);
145           if (newp == NULL)
146             {
147               ret = ENOMEM;
148               break;
149             }
150           cpuset = (cpu_set_t *) newp;
151
152           ret = __pthread_getaffinity_np (thread_id, size, cpuset);
153         }
154       /* Pick some ridiculous upper limit.  Is 8 million CPUs enough?  */
155       while (ret == EINVAL && size < 1024 * 1024);
156
157       if (ret == 0)
158         {
159           iattr->cpuset = cpuset;
160           iattr->cpusetsize = size;
161         }
162       else
163         {
164           free (cpuset);
165           if (ret == ENOSYS)
166             {
167               /* There is no such functionality.  */
168               ret = 0;
169               iattr->cpuset = NULL;
170               iattr->cpusetsize = 0;
171             }
172         }
173     }
174
175   lll_unlock (thread->lock, LLL_PRIVATE);
176
177   return ret;
178 }