Wed Jan 29 16:34:09 2020 UTC ()
Check thread->pt_magic with PT_MAGIC promptly


(kamil)
diff -r1.16 -r1.17 src/lib/libpthread/pthread_misc.c

cvs diff -r1.16 -r1.17 src/lib/libpthread/pthread_misc.c (expand / switch to unified diff)

--- src/lib/libpthread/pthread_misc.c 2020/01/13 18:22:56 1.16
+++ src/lib/libpthread/pthread_misc.c 2020/01/29 16:34:09 1.17
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: pthread_misc.c,v 1.16 2020/01/13 18:22:56 ad Exp $ */ 1/* $NetBSD: pthread_misc.c,v 1.17 2020/01/29 16:34:09 kamil Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams. 8 * by Nathan J. Williams.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -20,27 +20,27 @@ @@ -20,27 +20,27 @@
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__RCSID("$NetBSD: pthread_misc.c,v 1.16 2020/01/13 18:22:56 ad Exp $"); 33__RCSID("$NetBSD: pthread_misc.c,v 1.17 2020/01/29 16:34:09 kamil Exp $");
34 34
35#include <errno.h> 35#include <errno.h>
36#include <string.h> 36#include <string.h>
37#include <unistd.h> 37#include <unistd.h>
38 38
39#include <sys/types.h> 39#include <sys/types.h>
40#include <sys/pset.h> 40#include <sys/pset.h>
41#include <sys/signal.h> 41#include <sys/signal.h>
42#include <sys/time.h> 42#include <sys/time.h>
43 43
44#include <lwp.h> 44#include <lwp.h>
45#include <sched.h> 45#include <sched.h>
46 46
@@ -51,96 +51,114 @@ __RCSID("$NetBSD: pthread_misc.c,v 1.16  @@ -51,96 +51,114 @@ __RCSID("$NetBSD: pthread_misc.c,v 1.16
51int pthread__sched_yield(void); 51int pthread__sched_yield(void);
52 52
53int _sys___sigprocmask14(int, const sigset_t *, sigset_t *); 53int _sys___sigprocmask14(int, const sigset_t *, sigset_t *);
54int _sys_sched_yield(void); 54int _sys_sched_yield(void);
55 55
56__strong_alias(__libc_thr_sigsetmask,pthread_sigmask) 56__strong_alias(__libc_thr_sigsetmask,pthread_sigmask)
57__strong_alias(__sigprocmask14,pthread_sigmask) 57__strong_alias(__sigprocmask14,pthread_sigmask)
58__strong_alias(__libc_thr_yield,pthread__sched_yield) 58__strong_alias(__libc_thr_yield,pthread__sched_yield)
59 59
60int 60int
61pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param) 61pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
62{ 62{
63 63
 64 pthread__error(EINVAL, "Invalid thread",
 65 thread->pt_magic == PT_MAGIC);
 66
64 if (pthread__find(thread) != 0) 67 if (pthread__find(thread) != 0)
65 return ESRCH; 68 return ESRCH;
66 69
67 if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0) 70 if (_sched_getparam(getpid(), thread->pt_lid, policy, param) < 0)
68 return errno; 71 return errno;
69 72
70 return 0; 73 return 0;
71} 74}
72 75
73int 76int
74pthread_setschedparam(pthread_t thread, int policy,  77pthread_setschedparam(pthread_t thread, int policy,
75 const struct sched_param *param) 78 const struct sched_param *param)
76{ 79{
77 struct sched_param sp; 80 struct sched_param sp;
78 81
 82 pthread__error(EINVAL, "Invalid thread",
 83 thread->pt_magic == PT_MAGIC);
 84
79 if (pthread__find(thread) != 0) 85 if (pthread__find(thread) != 0)
80 return ESRCH; 86 return ESRCH;
81 87
82 memcpy(&sp, param, sizeof(struct sched_param)); 88 memcpy(&sp, param, sizeof(struct sched_param));
83 if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0) 89 if (_sched_setparam(getpid(), thread->pt_lid, policy, &sp) < 0)
84 return errno; 90 return errno;
85 91
86 return 0; 92 return 0;
87} 93}
88 94
89int 95int
90pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset) 96pthread_getaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
91{ 97{
92 98
 99 pthread__error(EINVAL, "Invalid thread",
 100 thread->pt_magic == PT_MAGIC);
 101
93 if (pthread__find(thread) != 0) 102 if (pthread__find(thread) != 0)
94 return ESRCH; 103 return ESRCH;
95 104
96 if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0) 105 if (_sched_getaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
97 return errno; 106 return errno;
98 107
99 return 0; 108 return 0;
100} 109}
101 110
102int 111int
103pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset) 112pthread_setaffinity_np(pthread_t thread, size_t size, cpuset_t *cpuset)
104{ 113{
105 114
 115 pthread__error(EINVAL, "Invalid thread",
 116 thread->pt_magic == PT_MAGIC);
 117
106 if (pthread__find(thread) != 0) 118 if (pthread__find(thread) != 0)
107 return ESRCH; 119 return ESRCH;
108 120
109 if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0) 121 if (_sched_setaffinity(getpid(), thread->pt_lid, size, cpuset) < 0)
110 return errno; 122 return errno;
111 123
112 return 0; 124 return 0;
113} 125}
114 126
115int 127int
116pthread_setschedprio(pthread_t thread, int prio) 128pthread_setschedprio(pthread_t thread, int prio)
117{ 129{
118 struct sched_param sp; 130 struct sched_param sp;
119 131
 132 pthread__error(EINVAL, "Invalid thread",
 133 thread->pt_magic == PT_MAGIC);
 134
120 if (pthread__find(thread) != 0) 135 if (pthread__find(thread) != 0)
121 return ESRCH; 136 return ESRCH;
122 137
123 sp.sched_priority = prio; 138 sp.sched_priority = prio;
124 if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0) 139 if (_sched_setparam(getpid(), thread->pt_lid, SCHED_NONE, &sp) < 0)
125 return errno; 140 return errno;
126 141
127 return 0; 142 return 0;
128} 143}
129 144
130int 145int
131pthread_kill(pthread_t thread, int sig) 146pthread_kill(pthread_t thread, int sig)
132{ 147{
133 148
 149 pthread__error(EINVAL, "Invalid thread",
 150 thread->pt_magic == PT_MAGIC);
 151
134 if ((sig < 0) || (sig >= _NSIG)) 152 if ((sig < 0) || (sig >= _NSIG))
135 return EINVAL; 153 return EINVAL;
136 if (pthread__find(thread) != 0) 154 if (pthread__find(thread) != 0)
137 return ESRCH; 155 return ESRCH;
138 if (_lwp_kill(thread->pt_lid, sig)) 156 if (_lwp_kill(thread->pt_lid, sig))
139 return errno; 157 return errno;
140 return 0; 158 return 0;
141} 159}
142 160
143int 161int
144pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 162pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
145{ 163{
146 if (_sys___sigprocmask14(how, set, oset)) 164 if (_sys___sigprocmask14(how, set, oset))