1/* Copyright (C) 2003-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#include <errno.h>
19#include <pthreadP.h>
20#include <string.h>
21#include <sysdep.h>
22#include <sys/types.h>
23#include <sys/param.h>
24#include <shlib-compat.h>
25
26
27int
28__pthread_attr_getaffinity_new (const pthread_attr_t *attr, size_t cpusetsize,
29 cpu_set_t *cpuset)
30{
31 const struct pthread_attr *iattr;
32
33 iattr = (const struct pthread_attr *) attr;
34
35 if (iattr->extension != NULL && iattr->extension->cpuset != NULL)
36 {
37 /* Check whether there are any bits set beyond the limits
38 the user requested. */
39 for (size_t cnt = cpusetsize; cnt < iattr->extension->cpusetsize; ++cnt)
40 if (((char *) iattr->extension->cpuset)[cnt] != 0)
41 return EINVAL;
42
43 /* Copy over the cpuset from the thread attribute object. Limit the copy
44 to the minimum of the source and destination sizes to prevent a buffer
45 overrun. If the destination is larger, fill the remaining space with
46 zeroes. */
47 void *p = mempcpy (cpuset, iattr->extension->cpuset,
48 MIN (iattr->extension->cpusetsize, cpusetsize));
49 if (cpusetsize > iattr->extension->cpusetsize)
50 memset (p, '\0', cpusetsize - iattr->extension->cpusetsize);
51 }
52 else
53 /* We have no information. */
54 memset (cpuset, -1, cpusetsize);
55
56 return 0;
57}
58versioned_symbol (libpthread, __pthread_attr_getaffinity_new,
59 pthread_attr_getaffinity_np, GLIBC_2_34);
60#if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_3_4, GLIBC_2_34)
61compat_symbol (libpthread, __pthread_attr_getaffinity_new,
62 pthread_attr_getaffinity_np, GLIBC_2_3_4);
63#endif
64
65
66#if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_3_3, GLIBC_2_3_4)
67int
68__pthread_attr_getaffinity_old (const pthread_attr_t *attr, cpu_set_t *cpuset)
69{
70 /* The old interface by default assumed a 1024 processor bitmap. */
71 return __pthread_attr_getaffinity_new (attr, 128, cpuset);
72}
73compat_symbol (libpthread, __pthread_attr_getaffinity_old,
74 pthread_attr_getaffinity_np, GLIBC_2_3_3);
75#endif
76

source code of glibc/nptl/pthread_attr_getaffinity.c