sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- A replacement for the standard libpthread.so. ---*/ |
| 4 | /*--- vg_libpthread.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of Valgrind, an x86 protected-mode emulator |
| 9 | designed for debugging and profiling binaries on x86-Unixes. |
| 10 | |
| 11 | Copyright (C) 2000-2002 Julian Seward |
| 12 | jseward@acm.org |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file LICENSE. |
| 30 | */ |
| 31 | |
| 32 | /* ALL THIS CODE RUNS ON THE SIMULATED CPU. |
| 33 | |
| 34 | This is a replacement for the standard libpthread.so. It is loaded |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 35 | as part of the client's image (if required) and directs pthread |
| 36 | calls through to Valgrind's request mechanism. |
| 37 | |
| 38 | A couple of caveats. |
| 39 | |
| 40 | 1. Since it's a binary-compatible replacement for an existing library, |
| 41 | we must take care to used exactly the same data layouts, etc, as |
| 42 | the standard pthread.so does. |
| 43 | |
| 44 | 2. Since this runs as part of the client, there are no specific |
| 45 | restrictions on what headers etc we can include, so long as |
| 46 | this libpthread.so does not end up having dependencies on .so's |
| 47 | which the real one doesn't. |
| 48 | |
| 49 | Later ... it appears we cannot call file-related stuff in libc here, |
| 50 | perhaps fair enough. Be careful what you call from here. Even exit() |
| 51 | doesn't work (gives infinite recursion and then stack overflow); hence |
| 52 | myexit(). Also fprintf doesn't seem safe. |
| 53 | */ |
| 54 | |
| 55 | #include "valgrind.h" /* For the request-passing mechanism */ |
| 56 | #include "vg_include.h" /* For the VG_USERREQ__* constants */ |
| 57 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 58 | #include <unistd.h> |
| 59 | #include <string.h> |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 60 | #ifdef GLIBC_2_1 |
| 61 | #include <sys/time.h> |
| 62 | #endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 63 | |
| 64 | /* --------------------------------------------------------------------- |
| 65 | Helpers. We have to be pretty self-sufficient. |
| 66 | ------------------------------------------------------------------ */ |
| 67 | |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 68 | /* Number of times any given error message is printed. */ |
| 69 | #define N_MOANS 3 |
| 70 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 71 | /* Extract from Valgrind the value of VG_(clo_trace_pthread_level). |
| 72 | Returns 0 (none) if not running on Valgrind. */ |
| 73 | static |
| 74 | int get_pt_trace_level ( void ) |
| 75 | { |
| 76 | int res; |
| 77 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 78 | VG_USERREQ__GET_PTHREAD_TRACE_LEVEL, |
| 79 | 0, 0, 0, 0); |
| 80 | return res; |
| 81 | } |
| 82 | |
| 83 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 84 | static |
| 85 | void myexit ( int arg ) |
| 86 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 87 | int __res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 88 | __asm__ volatile ("movl %%ecx, %%ebx ; int $0x80" |
| 89 | : "=a" (__res) |
| 90 | : "0" (__NR_exit), |
| 91 | "c" (arg) ); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 92 | /* We don't bother to mention the fact that this asm trashes %ebx, |
| 93 | since it won't return. If you ever do let it return ... fix |
| 94 | this! */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 98 | /* We need this guy -- it's in valgrind.so. */ |
| 99 | extern void VG_(startup) ( void ); |
| 100 | |
| 101 | |
| 102 | /* Just start up Valgrind if it's not already going. VG_(startup)() |
| 103 | detects and ignores second and subsequent calls. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 104 | static __inline__ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 105 | void ensure_valgrind ( char* caller ) |
| 106 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 107 | VG_(startup)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 108 | } |
| 109 | |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 110 | /* While we're at it ... hook our own startup function into this |
| 111 | game. */ |
| 112 | __asm__ ( |
| 113 | ".section .init\n" |
| 114 | "\tcall vgPlain_startup" |
| 115 | ); |
| 116 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 117 | |
| 118 | static |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 119 | __attribute__((noreturn)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 120 | void barf ( char* str ) |
| 121 | { |
| 122 | char buf[100]; |
| 123 | buf[0] = 0; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 124 | strcat(buf, "\nvalgrind's libpthread.so: "); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 125 | strcat(buf, str); |
| 126 | strcat(buf, "\n\n"); |
| 127 | write(2, buf, strlen(buf)); |
| 128 | myexit(1); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 129 | /* We have to persuade gcc into believing this doesn't return. */ |
| 130 | while (1) { }; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 134 | static void ignored ( char* msg ) |
| 135 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 136 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 137 | char* ig = "valgrind's libpthread.so: IGNORED call to: "; |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 138 | write(2, ig, strlen(ig)); |
| 139 | write(2, msg, strlen(msg)); |
| 140 | ig = "\n"; |
| 141 | write(2, ig, strlen(ig)); |
| 142 | } |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 143 | } |
| 144 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 145 | static void kludged ( char* msg ) |
| 146 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 147 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 148 | char* ig = "valgrind's libpthread.so: KLUDGED call to: "; |
| 149 | write(2, ig, strlen(ig)); |
| 150 | write(2, msg, strlen(msg)); |
| 151 | ig = "\n"; |
| 152 | write(2, ig, strlen(ig)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void not_inside ( char* msg ) |
| 157 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 158 | VG_(startup)(); |
| 159 | return; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 160 | if (get_pt_trace_level() >= 0) { |
| 161 | char* ig = "valgrind's libpthread.so: NOT INSIDE VALGRIND " |
| 162 | "during call to: "; |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 163 | write(2, ig, strlen(ig)); |
| 164 | write(2, msg, strlen(msg)); |
| 165 | ig = "\n"; |
| 166 | write(2, ig, strlen(ig)); |
| 167 | } |
| 168 | } |
| 169 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 170 | void vgPlain_unimp ( char* what ) |
| 171 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 172 | char* ig = "valgrind's libpthread.so: UNIMPLEMENTED FUNCTION: "; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 173 | write(2, ig, strlen(ig)); |
| 174 | write(2, what, strlen(what)); |
| 175 | ig = "\n"; |
| 176 | write(2, ig, strlen(ig)); |
| 177 | barf("Please report this bug to me at: jseward@acm.org"); |
| 178 | } |
| 179 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 180 | |
| 181 | /* --------------------------------------------------------------------- |
| 182 | Pass pthread_ calls to Valgrind's request mechanism. |
| 183 | ------------------------------------------------------------------ */ |
| 184 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 185 | #include <pthread.h> |
| 186 | #include <stdio.h> |
| 187 | #include <errno.h> |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 188 | #include <assert.h> |
| 189 | #include <sys/time.h> /* gettimeofday */ |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 190 | |
| 191 | /* --------------------------------------------------- |
| 192 | THREAD ATTRIBUTES |
| 193 | ------------------------------------------------ */ |
| 194 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 195 | int pthread_attr_init(pthread_attr_t *attr) |
| 196 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 197 | static int moans = N_MOANS; |
| 198 | if (moans-- > 0) |
| 199 | ignored("pthread_attr_init"); |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) |
| 204 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 205 | static int moans = N_MOANS; |
| 206 | if (moans-- > 0) |
| 207 | ignored("pthread_attr_setdetachstate"); |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 211 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit) |
| 212 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 213 | static int moans = N_MOANS; |
| 214 | if (moans-- > 0) |
| 215 | ignored("pthread_attr_setinheritsched"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | } |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 218 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 219 | /* This is completely bogus. */ |
| 220 | int pthread_attr_getschedparam(const pthread_attr_t *attr, |
| 221 | struct sched_param *param) |
| 222 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 223 | static int moans = N_MOANS; |
| 224 | if (moans-- > 0) |
| 225 | kludged("pthread_attr_getschedparam"); |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 226 | # ifdef GLIBC_2_1 |
| 227 | if (param) param->sched_priority = 0; /* who knows */ |
| 228 | # else |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 229 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 230 | # endif |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int pthread_attr_setschedparam(pthread_attr_t *attr, |
| 235 | const struct sched_param *param) |
| 236 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 237 | static int moans = N_MOANS; |
| 238 | if (moans-- > 0) |
| 239 | ignored("pthread_attr_setschedparam"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int pthread_attr_destroy(pthread_attr_t *attr) |
| 244 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 245 | static int moans = N_MOANS; |
| 246 | if (moans-- > 0) |
| 247 | ignored("pthread_attr_destroy"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 248 | return 0; |
| 249 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 250 | |
| 251 | /* --------------------------------------------------- |
| 252 | THREADs |
| 253 | ------------------------------------------------ */ |
| 254 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 255 | int pthread_equal(pthread_t thread1, pthread_t thread2) |
| 256 | { |
| 257 | return thread1 == thread2 ? 1 : 0; |
| 258 | } |
| 259 | |
| 260 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 261 | int |
| 262 | pthread_create (pthread_t *__restrict __thread, |
| 263 | __const pthread_attr_t *__restrict __attr, |
| 264 | void *(*__start_routine) (void *), |
| 265 | void *__restrict __arg) |
| 266 | { |
| 267 | int res; |
| 268 | ensure_valgrind("pthread_create"); |
| 269 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 270 | VG_USERREQ__PTHREAD_CREATE, |
| 271 | __thread, __attr, __start_routine, __arg); |
| 272 | return res; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | |
| 277 | int |
| 278 | pthread_join (pthread_t __th, void **__thread_return) |
| 279 | { |
| 280 | int res; |
| 281 | ensure_valgrind("pthread_join"); |
| 282 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 283 | VG_USERREQ__PTHREAD_JOIN, |
| 284 | __th, __thread_return, 0, 0); |
| 285 | return res; |
| 286 | } |
| 287 | |
| 288 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 289 | void pthread_exit(void *retval) |
| 290 | { |
| 291 | int res; |
| 292 | ensure_valgrind("pthread_exit"); |
| 293 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 294 | VG_USERREQ__PTHREAD_EXIT, |
| 295 | retval, 0, 0, 0); |
| 296 | /* Doesn't return! */ |
| 297 | /* However, we have to fool gcc into knowing that. */ |
| 298 | barf("pthread_exit: still alive after request?!"); |
| 299 | } |
| 300 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 301 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 302 | pthread_t pthread_self(void) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 303 | { |
| 304 | int tid; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 305 | ensure_valgrind("pthread_self"); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 306 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 307 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 308 | 0, 0, 0, 0); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 309 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 310 | barf("pthread_self: invalid ThreadId"); |
| 311 | return tid; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 315 | int pthread_detach(pthread_t th) |
| 316 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 317 | static int moans = N_MOANS; |
| 318 | if (moans-- > 0) |
| 319 | ignored("pthread_detach"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 324 | /* --------------------------------------------------- |
| 325 | MUTEX ATTRIBUTES |
| 326 | ------------------------------------------------ */ |
| 327 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 328 | int __pthread_mutexattr_init(pthread_mutexattr_t *attr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 329 | { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 330 | attr->__mutexkind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 331 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 332 | } |
| 333 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 334 | int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 335 | { |
| 336 | switch (type) { |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 337 | # ifndef GLIBC_2_1 |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 338 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 339 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 340 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 341 | # ifdef GLIBC_2_1 |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 342 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 343 | # endif |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 344 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 345 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 346 | attr->__mutexkind = type; |
| 347 | return 0; |
| 348 | default: |
| 349 | return EINVAL; |
| 350 | } |
| 351 | } |
| 352 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 353 | int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 354 | { |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | /* --------------------------------------------------- |
| 360 | MUTEXes |
| 361 | ------------------------------------------------ */ |
| 362 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 363 | int __pthread_mutex_init(pthread_mutex_t *mutex, |
| 364 | const pthread_mutexattr_t *mutexattr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 365 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 366 | mutex->__m_count = 0; |
| 367 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 368 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
| 369 | if (mutexattr) |
| 370 | mutex->__m_kind = mutexattr->__mutexkind; |
| 371 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 372 | } |
| 373 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 374 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 375 | int __pthread_mutex_lock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 376 | { |
| 377 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 378 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 379 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 380 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 381 | VG_USERREQ__PTHREAD_MUTEX_LOCK, |
| 382 | mutex, 0, 0, 0); |
| 383 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 384 | } else { |
| 385 | if (moans-- > 0) |
| 386 | not_inside("pthread_mutex_lock"); |
| 387 | return 0; /* success */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 391 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 392 | int __pthread_mutex_trylock(pthread_mutex_t *mutex) |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 393 | { |
| 394 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 395 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 396 | if (RUNNING_ON_VALGRIND) { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 397 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 398 | VG_USERREQ__PTHREAD_MUTEX_TRYLOCK, |
| 399 | mutex, 0, 0, 0); |
| 400 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 401 | } else { |
| 402 | if (moans-- > 0) |
| 403 | not_inside("pthread_mutex_trylock"); |
| 404 | return 0; |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 408 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 409 | int __pthread_mutex_unlock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 410 | { |
| 411 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 412 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 413 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 414 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 415 | VG_USERREQ__PTHREAD_MUTEX_UNLOCK, |
| 416 | mutex, 0, 0, 0); |
| 417 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 418 | } else { |
| 419 | if (moans-- > 0) |
| 420 | not_inside("pthread_mutex_unlock"); |
| 421 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 425 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 426 | int __pthread_mutex_destroy(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 427 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 428 | /* Valgrind doesn't hold any resources on behalf of the mutex, so no |
| 429 | need to involve it. */ |
| 430 | if (mutex->__m_count > 0) |
| 431 | return EBUSY; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 432 | mutex->__m_count = 0; |
| 433 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 434 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 435 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 439 | /* --------------------------------------------------- |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 440 | CONDITION VARIABLES |
| 441 | ------------------------------------------------ */ |
| 442 | |
| 443 | /* LinuxThreads supports no attributes for conditions. Hence ... */ |
| 444 | |
| 445 | int pthread_condattr_init(pthread_condattr_t *attr) |
| 446 | { |
| 447 | return 0; |
| 448 | } |
| 449 | |
sewardj | 0738a59 | 2002-04-20 13:59:33 +0000 | [diff] [blame] | 450 | int pthread_condattr_destroy(pthread_condattr_t *attr) |
| 451 | { |
| 452 | return 0; |
| 453 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 454 | |
| 455 | int pthread_cond_init( pthread_cond_t *cond, |
| 456 | const pthread_condattr_t *cond_attr) |
| 457 | { |
| 458 | cond->__c_waiting = (_pthread_descr)VG_INVALID_THREADID; |
| 459 | return 0; |
| 460 | } |
| 461 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 462 | int pthread_cond_destroy(pthread_cond_t *cond) |
| 463 | { |
| 464 | /* should check that no threads are waiting on this CV */ |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 465 | static int moans = N_MOANS; |
| 466 | if (moans-- > 0) |
| 467 | kludged("pthread_cond_destroy"); |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 468 | return 0; |
| 469 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 470 | |
| 471 | /* --------------------------------------------------- |
| 472 | SCHEDULING |
| 473 | ------------------------------------------------ */ |
| 474 | |
| 475 | /* This is completely bogus. */ |
| 476 | int pthread_getschedparam(pthread_t target_thread, |
| 477 | int *policy, |
| 478 | struct sched_param *param) |
| 479 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 480 | static int moans = N_MOANS; |
| 481 | if (moans-- > 0) |
| 482 | kludged("pthread_getschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 483 | if (policy) *policy = SCHED_OTHER; |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 484 | # ifdef GLIBC_2_1 |
| 485 | if (param) param->sched_priority = 0; /* who knows */ |
| 486 | # else |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 487 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 488 | # endif |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | int pthread_setschedparam(pthread_t target_thread, |
| 493 | int policy, |
| 494 | const struct sched_param *param) |
| 495 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 496 | static int moans = N_MOANS; |
| 497 | if (moans-- > 0) |
| 498 | ignored("pthread_setschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 502 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 503 | { |
| 504 | int res; |
| 505 | ensure_valgrind("pthread_cond_wait"); |
| 506 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 507 | VG_USERREQ__PTHREAD_COND_WAIT, |
| 508 | cond, mutex, 0, 0); |
| 509 | return res; |
| 510 | } |
| 511 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 512 | int pthread_cond_timedwait ( pthread_cond_t *cond, |
| 513 | pthread_mutex_t *mutex, |
| 514 | const struct timespec *abstime ) |
| 515 | { |
| 516 | int res; |
| 517 | unsigned int ms_now, ms_end; |
| 518 | struct timeval timeval_now; |
| 519 | unsigned long long int ull_ms_now_after_1970; |
| 520 | unsigned long long int ull_ms_end_after_1970; |
| 521 | |
| 522 | ensure_valgrind("pthread_cond_timedwait"); |
| 523 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 524 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 525 | 0, 0, 0, 0); |
| 526 | assert(ms_now != 0xFFFFFFFF); |
| 527 | res = gettimeofday(&timeval_now, NULL); |
| 528 | assert(res == 0); |
| 529 | |
| 530 | ull_ms_now_after_1970 |
| 531 | = 1000ULL * ((unsigned long long int)(timeval_now.tv_sec)) |
| 532 | + ((unsigned long long int)(timeval_now.tv_usec / 1000000)); |
| 533 | ull_ms_end_after_1970 |
| 534 | = 1000ULL * ((unsigned long long int)(abstime->tv_sec)) |
| 535 | + ((unsigned long long int)(abstime->tv_nsec / 1000000)); |
| 536 | assert(ull_ms_end_after_1970 >= ull_ms_now_after_1970); |
| 537 | ms_end |
| 538 | = ms_now + (unsigned int)(ull_ms_end_after_1970 - ull_ms_now_after_1970); |
| 539 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 540 | VG_USERREQ__PTHREAD_COND_TIMEDWAIT, |
| 541 | cond, mutex, ms_end, 0); |
| 542 | return res; |
| 543 | } |
| 544 | |
| 545 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 546 | int pthread_cond_signal(pthread_cond_t *cond) |
| 547 | { |
| 548 | int res; |
| 549 | ensure_valgrind("pthread_cond_signal"); |
| 550 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 551 | VG_USERREQ__PTHREAD_COND_SIGNAL, |
| 552 | cond, 0, 0, 0); |
| 553 | return res; |
| 554 | } |
| 555 | |
| 556 | int pthread_cond_broadcast(pthread_cond_t *cond) |
| 557 | { |
| 558 | int res; |
| 559 | ensure_valgrind("pthread_cond_broadcast"); |
| 560 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 561 | VG_USERREQ__PTHREAD_COND_BROADCAST, |
| 562 | cond, 0, 0, 0); |
| 563 | return res; |
| 564 | } |
| 565 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 566 | |
| 567 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 568 | CANCELLATION |
| 569 | ------------------------------------------------ */ |
| 570 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 571 | int pthread_setcancelstate(int state, int *oldstate) |
| 572 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 573 | static int moans = N_MOANS; |
| 574 | if (moans-- > 0) |
| 575 | ignored("pthread_setcancelstate"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 579 | int pthread_setcanceltype(int type, int *oldtype) |
| 580 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 581 | static int moans = N_MOANS; |
| 582 | if (moans-- > 0) |
| 583 | ignored("pthread_setcanceltype"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 584 | return 0; |
| 585 | } |
| 586 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 587 | int pthread_cancel(pthread_t thread) |
| 588 | { |
| 589 | int res; |
| 590 | ensure_valgrind("pthread_cancel"); |
| 591 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 592 | VG_USERREQ__PTHREAD_CANCEL, |
| 593 | thread, 0, 0, 0); |
| 594 | return res; |
| 595 | } |
| 596 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 597 | void pthread_testcancel(void) |
| 598 | { |
| 599 | } |
| 600 | |
| 601 | /*-------------------*/ |
| 602 | static pthread_mutex_t massacre_mx = PTHREAD_MUTEX_INITIALIZER; |
| 603 | |
| 604 | void __pthread_kill_other_threads_np ( void ) |
| 605 | { |
| 606 | int i, res, me; |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 607 | __pthread_mutex_lock(&massacre_mx); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 608 | me = pthread_self(); |
| 609 | for (i = 1; i < VG_N_THREADS; i++) { |
| 610 | if (i == me) continue; |
| 611 | res = pthread_cancel(i); |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 612 | if (0 && res == 0) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 613 | printf("----------- NUKED %d\n", i); |
| 614 | } |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 615 | __pthread_mutex_unlock(&massacre_mx); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 616 | } |
| 617 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 618 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 619 | /* --------------------------------------------------- |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 620 | SIGNALS |
| 621 | ------------------------------------------------ */ |
| 622 | |
| 623 | #include <signal.h> |
| 624 | |
| 625 | int pthread_sigmask(int how, const sigset_t *newmask, |
| 626 | sigset_t *oldmask) |
| 627 | { |
| 628 | int res; |
| 629 | |
| 630 | /* A bit subtle, because the scheduler expects newmask and oldmask |
| 631 | to be vki_sigset_t* rather than sigset_t*, and the two are |
| 632 | different. Fortunately the first 64 bits of a sigset_t are |
| 633 | exactly a vki_sigset_t, so we just pass the pointers through |
| 634 | unmodified. Haaaack! |
| 635 | |
| 636 | Also mash the how value so that the SIG_ constants from glibc |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 637 | constants to VKI_ constants, so that the former do not have to |
| 638 | be included into vg_scheduler.c. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 639 | |
| 640 | ensure_valgrind("pthread_sigmask"); |
| 641 | |
| 642 | switch (how) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 643 | case SIG_SETMASK: how = VKI_SIG_SETMASK; break; |
| 644 | case SIG_BLOCK: how = VKI_SIG_BLOCK; break; |
| 645 | case SIG_UNBLOCK: how = VKI_SIG_UNBLOCK; break; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 646 | default: return EINVAL; |
| 647 | } |
| 648 | |
| 649 | /* Crude check */ |
| 650 | if (newmask == NULL) |
| 651 | return EFAULT; |
| 652 | |
| 653 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 654 | VG_USERREQ__PTHREAD_SIGMASK, |
| 655 | how, newmask, oldmask, 0); |
| 656 | |
| 657 | /* The scheduler tells us of any memory violations. */ |
| 658 | return res == 0 ? 0 : EFAULT; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | int sigwait ( const sigset_t* set, int* sig ) |
| 663 | { |
| 664 | int res; |
| 665 | ensure_valgrind("sigwait"); |
| 666 | /* As with pthread_sigmask we deliberately confuse sigset_t with |
| 667 | vki_ksigset_t. */ |
| 668 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 669 | VG_USERREQ__SIGWAIT, |
| 670 | set, sig, 0, 0); |
| 671 | return res; |
| 672 | } |
| 673 | |
| 674 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 675 | int pthread_kill(pthread_t thread, int signo) |
| 676 | { |
| 677 | int res; |
| 678 | ensure_valgrind("pthread_kill"); |
| 679 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 680 | VG_USERREQ__PTHREAD_KILL, |
| 681 | thread, signo, 0, 0); |
| 682 | return res; |
| 683 | } |
| 684 | |
| 685 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 686 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 687 | THREAD-SPECIFICs |
| 688 | ------------------------------------------------ */ |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 689 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 690 | int __pthread_key_create(pthread_key_t *key, |
| 691 | void (*destr_function) (void *)) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 692 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 693 | int res; |
| 694 | ensure_valgrind("pthread_key_create"); |
| 695 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 696 | VG_USERREQ__PTHREAD_KEY_CREATE, |
| 697 | key, destr_function, 0, 0); |
| 698 | return res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | int pthread_key_delete(pthread_key_t key) |
| 702 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 703 | static int moans = N_MOANS; |
| 704 | if (moans-- > 0) |
| 705 | ignored("pthread_key_delete"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 709 | int __pthread_setspecific(pthread_key_t key, const void *pointer) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 710 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 711 | int res; |
| 712 | ensure_valgrind("pthread_setspecific"); |
| 713 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 714 | VG_USERREQ__PTHREAD_SETSPECIFIC, |
| 715 | key, pointer, 0, 0); |
| 716 | return res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 717 | } |
| 718 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 719 | void * __pthread_getspecific(pthread_key_t key) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 720 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 721 | int res; |
| 722 | ensure_valgrind("pthread_getspecific"); |
| 723 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 724 | VG_USERREQ__PTHREAD_GETSPECIFIC, |
| 725 | key, 0 , 0, 0); |
| 726 | return (void*)res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 727 | } |
| 728 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 729 | |
| 730 | /* --------------------------------------------------- |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 731 | ONCEry |
| 732 | ------------------------------------------------ */ |
| 733 | |
| 734 | static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER; |
| 735 | |
| 736 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 737 | int __pthread_once ( pthread_once_t *once_control, |
| 738 | void (*init_routine) (void) ) |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 739 | { |
| 740 | int res; |
| 741 | ensure_valgrind("pthread_once"); |
| 742 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 743 | res = __pthread_mutex_lock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 744 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 745 | if (res != 0) { |
| 746 | printf("res = %d\n",res); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 747 | barf("pthread_once: Looks like your program's " |
| 748 | "init routine calls back to pthread_once() ?!"); |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 749 | } |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 750 | |
| 751 | if (*once_control == 0) { |
| 752 | *once_control = 1; |
| 753 | init_routine(); |
| 754 | } |
| 755 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 756 | __pthread_mutex_unlock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | /* --------------------------------------------------- |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 763 | MISC |
| 764 | ------------------------------------------------ */ |
| 765 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 766 | int __pthread_atfork ( void (*prepare)(void), |
| 767 | void (*parent)(void), |
| 768 | void (*child)(void) ) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 769 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 770 | static int moans = N_MOANS; |
| 771 | if (moans-- > 0) |
| 772 | ignored("pthread_atfork"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 777 | __attribute__((weak)) |
| 778 | void __pthread_initialize ( void ) |
| 779 | { |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 780 | ensure_valgrind("__pthread_initialize"); |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 784 | /* --------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 785 | LIBRARY-PRIVATE THREAD SPECIFIC STATE |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 786 | ------------------------------------------------ */ |
| 787 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 788 | #include <resolv.h> |
| 789 | static int thread_specific_errno[VG_N_THREADS]; |
| 790 | static int thread_specific_h_errno[VG_N_THREADS]; |
| 791 | static struct __res_state |
| 792 | thread_specific_res_state[VG_N_THREADS]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 793 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 794 | int* __errno_location ( void ) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 795 | { |
| 796 | int tid; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 797 | /* ensure_valgrind("__errno_location"); */ |
| 798 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 799 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 800 | 0, 0, 0, 0); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 801 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 802 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 803 | barf("__errno_location: invalid ThreadId"); |
| 804 | return & thread_specific_errno[tid]; |
| 805 | } |
| 806 | |
| 807 | int* __h_errno_location ( void ) |
| 808 | { |
| 809 | int tid; |
| 810 | /* ensure_valgrind("__h_errno_location"); */ |
| 811 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 812 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 813 | 0, 0, 0, 0); |
| 814 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 815 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 816 | barf("__h_errno_location: invalid ThreadId"); |
| 817 | return & thread_specific_h_errno[tid]; |
| 818 | } |
| 819 | |
| 820 | struct __res_state* __res_state ( void ) |
| 821 | { |
| 822 | int tid; |
| 823 | /* ensure_valgrind("__res_state"); */ |
| 824 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 825 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 826 | 0, 0, 0, 0); |
| 827 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 828 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 829 | barf("__res_state: invalid ThreadId"); |
| 830 | return & thread_specific_res_state[tid]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 834 | /* --------------------------------------------------- |
| 835 | LIBC-PRIVATE SPECIFIC DATA |
| 836 | ------------------------------------------------ */ |
| 837 | |
| 838 | /* Relies on assumption that initial private data is NULL. This |
| 839 | should be fixed somehow. */ |
| 840 | |
| 841 | /* The allowable keys (indices) (all 2 of them). |
| 842 | From sysdeps/pthread/bits/libc-tsd.h |
| 843 | */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 844 | #define N_LIBC_TSD_EXTRA_KEYS 1 |
| 845 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 846 | enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0, |
| 847 | _LIBC_TSD_KEY_DL_ERROR, |
| 848 | _LIBC_TSD_KEY_N }; |
| 849 | |
| 850 | /* Auto-initialising subsystem. libc_specifics_inited is set |
| 851 | after initialisation. libc_specifics_inited_mx guards it. */ |
| 852 | static int libc_specifics_inited = 0; |
| 853 | static pthread_mutex_t libc_specifics_inited_mx = PTHREAD_MUTEX_INITIALIZER; |
| 854 | |
| 855 | /* These are the keys we must initialise the first time. */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 856 | static pthread_key_t libc_specifics_keys[_LIBC_TSD_KEY_N |
| 857 | + N_LIBC_TSD_EXTRA_KEYS]; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 858 | |
| 859 | /* Initialise the keys, if they are not already initialise. */ |
| 860 | static |
| 861 | void init_libc_tsd_keys ( void ) |
| 862 | { |
| 863 | int res, i; |
| 864 | pthread_key_t k; |
| 865 | |
| 866 | res = pthread_mutex_lock(&libc_specifics_inited_mx); |
| 867 | if (res != 0) barf("init_libc_tsd_keys: lock"); |
| 868 | |
| 869 | if (libc_specifics_inited == 0) { |
| 870 | /* printf("INIT libc specifics\n"); */ |
| 871 | libc_specifics_inited = 1; |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 872 | for (i = 0; i < _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS; i++) { |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 873 | res = pthread_key_create(&k, NULL); |
| 874 | if (res != 0) barf("init_libc_tsd_keys: create"); |
| 875 | libc_specifics_keys[i] = k; |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | res = pthread_mutex_unlock(&libc_specifics_inited_mx); |
| 880 | if (res != 0) barf("init_libc_tsd_keys: unlock"); |
| 881 | } |
| 882 | |
| 883 | |
| 884 | static int |
| 885 | libc_internal_tsd_set ( enum __libc_tsd_key_t key, |
| 886 | const void * pointer ) |
| 887 | { |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 888 | int res; |
| 889 | static int moans = N_MOANS; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 890 | /* printf("SET SET SET key %d ptr %p\n", key, pointer); */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 891 | if (key < _LIBC_TSD_KEY_MALLOC |
| 892 | || key >= _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 893 | barf("libc_internal_tsd_set: invalid key"); |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 894 | if (key >= _LIBC_TSD_KEY_N && moans-- > 0) |
| 895 | fprintf(stderr, |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 896 | "valgrind's libpthread.so: libc_internal_tsd_set: " |
| 897 | "dubious key %d\n", key); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 898 | init_libc_tsd_keys(); |
| 899 | res = pthread_setspecific(libc_specifics_keys[key], pointer); |
| 900 | if (res != 0) barf("libc_internal_tsd_set: setspecific failed"); |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static void * |
| 905 | libc_internal_tsd_get ( enum __libc_tsd_key_t key ) |
| 906 | { |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 907 | void* v; |
| 908 | static int moans = N_MOANS; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 909 | /* printf("GET GET GET key %d\n", key); */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 910 | if (key < _LIBC_TSD_KEY_MALLOC |
| 911 | || key >= _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 912 | barf("libc_internal_tsd_get: invalid key"); |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 913 | if (key >= _LIBC_TSD_KEY_N && moans-- > 0) |
| 914 | fprintf(stderr, |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 915 | "valgrind's libpthread.so: libc_internal_tsd_get: " |
| 916 | "dubious key %d\n", key); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 917 | init_libc_tsd_keys(); |
| 918 | v = pthread_getspecific(libc_specifics_keys[key]); |
| 919 | /* if (v == NULL) barf("libc_internal_tsd_set: getspecific failed"); */ |
| 920 | return v; |
| 921 | } |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 926 | int (*__libc_internal_tsd_set) |
| 927 | (enum __libc_tsd_key_t key, const void * pointer) |
| 928 | = libc_internal_tsd_set; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 929 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 930 | void* (*__libc_internal_tsd_get) |
| 931 | (enum __libc_tsd_key_t key) |
| 932 | = libc_internal_tsd_get; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 933 | |
| 934 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 935 | /* --------------------------------------------------------------------- |
| 936 | These are here (I think) because they are deemed cancellation |
| 937 | points by POSIX. For the moment we'll simply pass the call along |
| 938 | to the corresponding thread-unaware (?) libc routine. |
| 939 | ------------------------------------------------------------------ */ |
| 940 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 941 | #include <stdlib.h> |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 942 | #include <sys/types.h> |
| 943 | #include <sys/socket.h> |
| 944 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 945 | #ifdef GLIBC_2_1 |
| 946 | extern |
| 947 | int __sigaction |
| 948 | (int signum, |
| 949 | const struct sigaction *act, |
| 950 | struct sigaction *oldact); |
| 951 | #else |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 952 | extern |
| 953 | int __libc_sigaction |
| 954 | (int signum, |
| 955 | const struct sigaction *act, |
| 956 | struct sigaction *oldact); |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 957 | #endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 958 | int sigaction(int signum, |
| 959 | const struct sigaction *act, |
| 960 | struct sigaction *oldact) |
| 961 | { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 962 | # ifdef GLIBC_2_1 |
| 963 | return __sigaction(signum, act, oldact); |
| 964 | # else |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 965 | return __libc_sigaction(signum, act, oldact); |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 966 | # endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | |
| 970 | extern |
| 971 | int __libc_connect(int sockfd, |
| 972 | const struct sockaddr *serv_addr, |
| 973 | socklen_t addrlen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 974 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 975 | int connect(int sockfd, |
| 976 | const struct sockaddr *serv_addr, |
| 977 | socklen_t addrlen) |
| 978 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 979 | return __libc_connect(sockfd, serv_addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | |
| 983 | extern |
| 984 | int __libc_fcntl(int fd, int cmd, long arg); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 985 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 986 | int fcntl(int fd, int cmd, long arg) |
| 987 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 988 | return __libc_fcntl(fd, cmd, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
| 992 | extern |
| 993 | ssize_t __libc_write(int fd, const void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 994 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 995 | ssize_t write(int fd, const void *buf, size_t count) |
| 996 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 997 | return __libc_write(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | |
| 1001 | extern |
| 1002 | ssize_t __libc_read(int fd, void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1003 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1004 | ssize_t read(int fd, void *buf, size_t count) |
| 1005 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1006 | return __libc_read(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1009 | |
| 1010 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1011 | int __libc_open64(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1012 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1013 | int open64(const char *pathname, int flags, mode_t mode) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1014 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1015 | return __libc_open64(pathname, flags, mode); |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1018 | |
| 1019 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1020 | int __libc_open(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1021 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1022 | int open(const char *pathname, int flags, mode_t mode) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1023 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1024 | return __libc_open(pathname, flags, mode); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | extern |
| 1029 | int __libc_close(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1030 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1031 | int close(int fd) |
| 1032 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1033 | return __libc_close(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | extern |
| 1038 | int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1039 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1040 | int accept(int s, struct sockaddr *addr, socklen_t *addrlen) |
| 1041 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1042 | return __libc_accept(s, addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | |
| 1046 | extern |
| 1047 | pid_t __libc_fork(void); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1048 | pid_t __fork(void) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1049 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1050 | return __libc_fork(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | extern |
| 1055 | pid_t __libc_waitpid(pid_t pid, int *status, int options); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1056 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1057 | pid_t waitpid(pid_t pid, int *status, int options) |
| 1058 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1059 | return __libc_waitpid(pid, status, options); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | extern |
| 1064 | int __libc_nanosleep(const struct timespec *req, struct timespec *rem); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1065 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1066 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 1067 | { |
| 1068 | return __libc_nanosleep(req, rem); |
| 1069 | } |
| 1070 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1071 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1072 | extern |
| 1073 | int __libc_fsync(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1074 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1075 | int fsync(int fd) |
| 1076 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1077 | return __libc_fsync(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1080 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1081 | extern |
| 1082 | off_t __libc_lseek(int fildes, off_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1083 | __attribute__((weak)) |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1084 | off_t lseek(int fildes, off_t offset, int whence) |
| 1085 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1086 | return __libc_lseek(fildes, offset, whence); |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1089 | |
| 1090 | extern |
| 1091 | __off64_t __libc_lseek64(int fildes, __off64_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1092 | __attribute__((weak)) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1093 | __off64_t lseek64(int fildes, __off64_t offset, int whence) |
| 1094 | { |
| 1095 | return __libc_lseek64(fildes, offset, whence); |
| 1096 | } |
| 1097 | |
| 1098 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1099 | extern |
| 1100 | void __libc_longjmp(jmp_buf env, int val) __attribute((noreturn)); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1101 | /* not weak: __attribute__((weak)) */ |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1102 | void longjmp(jmp_buf env, int val) |
| 1103 | { |
| 1104 | __libc_longjmp(env, val); |
| 1105 | } |
| 1106 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1107 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1108 | extern |
| 1109 | int __libc_send(int s, const void *msg, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1110 | __attribute__((weak)) |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1111 | int send(int s, const void *msg, size_t len, int flags) |
| 1112 | { |
| 1113 | return __libc_send(s, msg, len, flags); |
| 1114 | } |
| 1115 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1116 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1117 | extern |
| 1118 | int __libc_recv(int s, void *buf, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1119 | __attribute__((weak)) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1120 | int recv(int s, void *buf, size_t len, int flags) |
| 1121 | { |
| 1122 | return __libc_recv(s, buf, len, flags); |
| 1123 | } |
| 1124 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1125 | |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1126 | extern |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1127 | int __libc_recvfrom(int s, void *buf, size_t len, int flags, |
| 1128 | struct sockaddr *from, socklen_t *fromlen); |
| 1129 | __attribute__((weak)) |
| 1130 | int recvfrom(int s, void *buf, size_t len, int flags, |
| 1131 | struct sockaddr *from, socklen_t *fromlen) |
| 1132 | { |
| 1133 | return __libc_recvfrom(s, buf, len, flags, from, fromlen); |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | extern |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1138 | int __libc_sendto(int s, const void *msg, size_t len, int flags, |
| 1139 | const struct sockaddr *to, socklen_t tolen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1140 | __attribute__((weak)) |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1141 | int sendto(int s, const void *msg, size_t len, int flags, |
| 1142 | const struct sockaddr *to, socklen_t tolen) |
| 1143 | { |
| 1144 | return __libc_sendto(s, msg, len, flags, to, tolen); |
| 1145 | } |
| 1146 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1147 | |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 1148 | extern |
| 1149 | int __libc_system(const char* str); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1150 | __attribute__((weak)) |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 1151 | int system(const char* str) |
| 1152 | { |
| 1153 | return __libc_system(str); |
| 1154 | } |
| 1155 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1156 | |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 1157 | extern |
| 1158 | pid_t __libc_wait(int *status); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1159 | __attribute__((weak)) |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 1160 | pid_t wait(int *status) |
| 1161 | { |
| 1162 | return __libc_wait(status); |
| 1163 | } |
| 1164 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1165 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1166 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1167 | /* --------------------------------------------------------------------- |
| 1168 | Nonblocking implementations of select() and poll(). This stuff will |
| 1169 | surely rot your mind. |
| 1170 | ------------------------------------------------------------------ */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1171 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1172 | /*--------------------------------------------------*/ |
| 1173 | |
| 1174 | #include "vg_kerneliface.h" |
| 1175 | |
| 1176 | static |
| 1177 | __inline__ |
| 1178 | int is_kerror ( int res ) |
| 1179 | { |
| 1180 | if (res >= -4095 && res <= -1) |
| 1181 | return 1; |
| 1182 | else |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | static |
| 1188 | int my_do_syscall1 ( int syscallno, int arg1 ) |
| 1189 | { |
| 1190 | int __res; |
| 1191 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1192 | : "=a" (__res) |
| 1193 | : "0" (syscallno), |
| 1194 | "d" (arg1) ); |
| 1195 | return __res; |
| 1196 | } |
| 1197 | |
| 1198 | static |
| 1199 | int my_do_syscall2 ( int syscallno, |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1200 | int arg1, int arg2 ) |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1201 | { |
| 1202 | int __res; |
| 1203 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1204 | : "=a" (__res) |
| 1205 | : "0" (syscallno), |
| 1206 | "d" (arg1), |
| 1207 | "c" (arg2) ); |
| 1208 | return __res; |
| 1209 | } |
| 1210 | |
| 1211 | static |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1212 | int my_do_syscall3 ( int syscallno, |
| 1213 | int arg1, int arg2, int arg3 ) |
| 1214 | { |
| 1215 | int __res; |
| 1216 | __asm__ volatile ("pushl %%ebx; movl %%esi,%%ebx ; int $0x80 ; popl %%ebx" |
| 1217 | : "=a" (__res) |
| 1218 | : "0" (syscallno), |
| 1219 | "S" (arg1), |
| 1220 | "c" (arg2), |
| 1221 | "d" (arg3) ); |
| 1222 | return __res; |
| 1223 | } |
| 1224 | |
| 1225 | static |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1226 | int do_syscall_select( int n, |
| 1227 | vki_fd_set* readfds, |
| 1228 | vki_fd_set* writefds, |
| 1229 | vki_fd_set* exceptfds, |
| 1230 | struct vki_timeval * timeout ) |
| 1231 | { |
| 1232 | int res; |
| 1233 | int args[5]; |
| 1234 | args[0] = n; |
| 1235 | args[1] = (int)readfds; |
| 1236 | args[2] = (int)writefds; |
| 1237 | args[3] = (int)exceptfds; |
| 1238 | args[4] = (int)timeout; |
| 1239 | res = my_do_syscall1(__NR_select, (int)(&(args[0])) ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1240 | return res; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | |
| 1244 | /* This is a wrapper round select(), which makes it thread-safe, |
| 1245 | meaning that only this thread will block, rather than the entire |
| 1246 | process. This wrapper in turn depends on nanosleep() not to block |
| 1247 | the entire process, but I think (hope? suspect?) that POSIX |
| 1248 | pthreads guarantees that to be the case. |
| 1249 | |
| 1250 | Basic idea is: modify the timeout parameter to select so that it |
| 1251 | returns immediately. Poll like this until select returns non-zero, |
| 1252 | indicating something interesting happened, or until our time is up. |
| 1253 | Space out the polls with nanosleeps of say 20 milliseconds, which |
| 1254 | is required to be nonblocking; this allows other threads to run. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1255 | |
| 1256 | Assumes: |
| 1257 | * (checked via assert) types fd_set and vki_fd_set are identical. |
| 1258 | * (checked via assert) types timeval and vki_timeval are identical. |
| 1259 | * (unchecked) libc error numbers (EINTR etc) are the negation of the |
| 1260 | kernel's error numbers (VKI_EINTR etc). |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1261 | */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1262 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1263 | /* __attribute__((weak)) */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1264 | int select ( int n, |
| 1265 | fd_set *rfds, |
| 1266 | fd_set *wfds, |
| 1267 | fd_set *xfds, |
| 1268 | struct timeval *timeout ) |
| 1269 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1270 | unsigned int ms_now, ms_end; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1271 | int res; |
| 1272 | fd_set rfds_copy; |
| 1273 | fd_set wfds_copy; |
| 1274 | fd_set xfds_copy; |
| 1275 | struct vki_timeval t_now; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1276 | struct vki_timeval zero_timeout; |
| 1277 | struct vki_timespec nanosleep_interval; |
| 1278 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1279 | /* gcc's complains about ms_end being used uninitialised -- classic |
| 1280 | case it can't understand, where ms_end is both defined and used |
| 1281 | only if timeout != NULL. Hence ... */ |
| 1282 | ms_end = 0; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1283 | |
| 1284 | /* We assume that the kernel and libc data layouts are identical |
| 1285 | for the following types. These asserts provide a crude |
| 1286 | check. */ |
| 1287 | if (sizeof(fd_set) != sizeof(vki_fd_set) |
| 1288 | || sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1289 | barf("valgrind's hacky non-blocking select(): data sizes error"); |
| 1290 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1291 | /* Detect the current time and simultaneously find out if we are |
| 1292 | running on Valgrind. */ |
| 1293 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1294 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1295 | 0, 0, 0, 0); |
| 1296 | |
| 1297 | /* If a zero timeout specified, this call is harmless. Also go |
| 1298 | this route if we're not running on Valgrind, for whatever |
| 1299 | reason. */ |
| 1300 | if ( (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) |
| 1301 | || (ms_now == 0xFFFFFFFF) ) { |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1302 | res = do_syscall_select( n, (vki_fd_set*)rfds, |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1303 | (vki_fd_set*)wfds, |
| 1304 | (vki_fd_set*)xfds, |
| 1305 | (struct vki_timeval*)timeout); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1306 | if (is_kerror(res)) { |
| 1307 | * (__errno_location()) = -res; |
| 1308 | return -1; |
| 1309 | } else { |
| 1310 | return res; |
| 1311 | } |
| 1312 | } |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1313 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1314 | /* If a timeout was specified, set ms_end to be the end millisecond |
| 1315 | counter [wallclock] time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1316 | if (timeout) { |
| 1317 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
| 1318 | assert(res == 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1319 | ms_end = ms_now; |
| 1320 | ms_end += (timeout->tv_usec / 1000); |
| 1321 | ms_end += (timeout->tv_sec * 1000); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1322 | /* Stay sane ... */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1323 | assert (ms_end >= ms_now); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* fprintf(stderr, "MY_SELECT: before loop\n"); */ |
| 1327 | |
| 1328 | /* Either timeout == NULL, meaning wait indefinitely, or timeout != |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1329 | NULL, in which case ms_end holds the end time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1330 | while (1) { |
| 1331 | if (timeout) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1332 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1333 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1334 | 0, 0, 0, 0); |
| 1335 | assert(ms_now != 0xFFFFFFFF); |
| 1336 | if (ms_now >= ms_end) { |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1337 | /* timeout; nothing interesting happened. */ |
| 1338 | if (rfds) FD_ZERO(rfds); |
| 1339 | if (wfds) FD_ZERO(wfds); |
| 1340 | if (xfds) FD_ZERO(xfds); |
| 1341 | return 0; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /* These could be trashed each time round the loop, so restore |
| 1346 | them each time. */ |
| 1347 | if (rfds) rfds_copy = *rfds; |
| 1348 | if (wfds) wfds_copy = *wfds; |
| 1349 | if (xfds) xfds_copy = *xfds; |
| 1350 | |
| 1351 | zero_timeout.tv_sec = zero_timeout.tv_usec = 0; |
| 1352 | |
| 1353 | res = do_syscall_select( n, |
| 1354 | rfds ? (vki_fd_set*)(&rfds_copy) : NULL, |
| 1355 | wfds ? (vki_fd_set*)(&wfds_copy) : NULL, |
| 1356 | xfds ? (vki_fd_set*)(&xfds_copy) : NULL, |
| 1357 | & zero_timeout ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1358 | if (is_kerror(res)) { |
| 1359 | /* Some kind of error (including EINTR). Set errno and |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1360 | return. The sets are unspecified in this case. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1361 | * (__errno_location()) = -res; |
| 1362 | return -1; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1363 | } |
| 1364 | if (res > 0) { |
| 1365 | /* one or more fds is ready. Copy out resulting sets and |
| 1366 | return. */ |
| 1367 | if (rfds) *rfds = rfds_copy; |
| 1368 | if (wfds) *wfds = wfds_copy; |
| 1369 | if (xfds) *xfds = xfds_copy; |
| 1370 | return res; |
| 1371 | } |
| 1372 | /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ |
| 1373 | /* nanosleep and go round again */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1374 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1375 | nanosleep_interval.tv_nsec = 50 * 1000 * 1000; /* 50 milliseconds */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1376 | /* It's critical here that valgrind's nanosleep implementation |
| 1377 | is nonblocking. */ |
| 1378 | (void)my_do_syscall2(__NR_nanosleep, |
| 1379 | (int)(&nanosleep_interval), (int)NULL); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | |
| 1386 | #include <sys/poll.h> |
| 1387 | |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 1388 | #ifdef GLIBC_2_1 |
| 1389 | typedef unsigned long int nfds_t; |
| 1390 | #endif |
| 1391 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1392 | /* __attribute__((weak)) */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1393 | int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) |
| 1394 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1395 | unsigned int ms_now, ms_end; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1396 | int res, i; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1397 | struct vki_timespec nanosleep_interval; |
| 1398 | |
| 1399 | ensure_valgrind("poll"); |
| 1400 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1401 | /* Detect the current time and simultaneously find out if we are |
| 1402 | running on Valgrind. */ |
| 1403 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1404 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1405 | 0, 0, 0, 0); |
| 1406 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1407 | if (/* CHECK SIZES FOR struct pollfd */ |
| 1408 | sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1409 | barf("valgrind's hacky non-blocking poll(): data sizes error"); |
| 1410 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1411 | /* dummy initialisation to keep gcc -Wall happy */ |
| 1412 | ms_end = 0; |
| 1413 | |
| 1414 | /* If a zero timeout specified, this call is harmless. Also do |
| 1415 | this if not running on Valgrind. */ |
| 1416 | if (__timeout == 0 || ms_now == 0xFFFFFFFF) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1417 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, __timeout); |
| 1418 | if (is_kerror(res)) { |
| 1419 | * (__errno_location()) = -res; |
| 1420 | return -1; |
| 1421 | } else { |
| 1422 | return res; |
| 1423 | } |
| 1424 | } |
| 1425 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1426 | /* If a timeout was specified, set ms_end to be the end wallclock |
| 1427 | time. Easy considering that __timeout is in milliseconds. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1428 | if (__timeout > 0) { |
sewardj | 650ac00 | 2002-04-29 12:20:34 +0000 | [diff] [blame] | 1429 | ms_end = ms_now + (unsigned int)__timeout; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /* fprintf(stderr, "MY_POLL: before loop\n"); */ |
| 1433 | |
| 1434 | /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, |
| 1435 | in which case t_end holds the end time. */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1436 | assert(__timeout != 0); |
| 1437 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1438 | while (1) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1439 | if (__timeout > 0) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1440 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1441 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1442 | 0, 0, 0, 0); |
| 1443 | assert(ms_now != 0xFFFFFFFF); |
| 1444 | if (ms_now >= ms_end) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1445 | /* timeout; nothing interesting happened. */ |
| 1446 | for (i = 0; i < __nfds; i++) |
| 1447 | __fds[i].revents = 0; |
| 1448 | return 0; |
| 1449 | } |
| 1450 | } |
| 1451 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1452 | /* Do a return-immediately poll. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1453 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); |
| 1454 | if (is_kerror(res)) { |
| 1455 | /* Some kind of error. Set errno and return. */ |
| 1456 | * (__errno_location()) = -res; |
| 1457 | return -1; |
| 1458 | } |
| 1459 | if (res > 0) { |
| 1460 | /* One or more fds is ready. Return now. */ |
| 1461 | return res; |
| 1462 | } |
| 1463 | /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ |
| 1464 | /* nanosleep and go round again */ |
| 1465 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1466 | nanosleep_interval.tv_nsec = 51 * 1000 * 1000; /* 51 milliseconds */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1467 | /* It's critical here that valgrind's nanosleep implementation |
| 1468 | is nonblocking. */ |
| 1469 | (void)my_do_syscall2(__NR_nanosleep, |
| 1470 | (int)(&nanosleep_interval), (int)NULL); |
| 1471 | } |
| 1472 | } |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1473 | |
| 1474 | |
| 1475 | /* --------------------------------------------------------------------- |
| 1476 | B'stard. |
| 1477 | ------------------------------------------------------------------ */ |
| 1478 | |
| 1479 | # define strong_alias(name, aliasname) \ |
| 1480 | extern __typeof (name) aliasname __attribute__ ((alias (#name))); |
| 1481 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1482 | # define weak_alias(name, aliasname) \ |
| 1483 | extern __typeof (name) aliasname __attribute__ ((weak, alias (#name))); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1484 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1485 | strong_alias(__pthread_mutex_lock, pthread_mutex_lock) |
| 1486 | strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock) |
| 1487 | strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock) |
| 1488 | strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init) |
| 1489 | weak_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype) |
| 1490 | strong_alias(__pthread_mutex_init, pthread_mutex_init) |
| 1491 | strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy) |
| 1492 | strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy) |
| 1493 | strong_alias(__pthread_once, pthread_once) |
| 1494 | strong_alias(__pthread_atfork, pthread_atfork) |
| 1495 | strong_alias(__pthread_key_create, pthread_key_create) |
| 1496 | strong_alias(__pthread_getspecific, pthread_getspecific) |
| 1497 | strong_alias(__pthread_setspecific, pthread_setspecific) |
| 1498 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1499 | #ifndef GLIBC_2_1 |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1500 | strong_alias(sigaction, __sigaction) |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1501 | #endif |
| 1502 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1503 | strong_alias(close, __close) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1504 | strong_alias(fcntl, __fcntl) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1505 | strong_alias(lseek, __lseek) |
| 1506 | strong_alias(open, __open) |
| 1507 | strong_alias(open64, __open64) |
| 1508 | //strong_alias(pread64, __pread64) |
| 1509 | //strong_alias(pwrite64, __pwrite64) |
| 1510 | strong_alias(read, __read) |
| 1511 | strong_alias(wait, __wait) |
| 1512 | strong_alias(write, __write) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1513 | strong_alias(connect, __connect) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1514 | strong_alias(send, __send) |
| 1515 | |
| 1516 | weak_alias(__fork, fork) |
| 1517 | //weak_alias(__vfork, vfork) |
| 1518 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1519 | |
| 1520 | /*--------------------------------------------------*/ |
| 1521 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1522 | int |
| 1523 | pthread_rwlock_rdlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1524 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1525 | static int moans = N_MOANS; |
| 1526 | if (moans-- > 0) |
| 1527 | kludged("pthread_rwlock_rdlock"); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1528 | return 0; |
| 1529 | } |
| 1530 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1531 | weak_alias(pthread_rwlock_rdlock, __pthread_rwlock_rdlock) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1532 | |
| 1533 | |
| 1534 | int |
| 1535 | pthread_rwlock_unlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1536 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1537 | static int moans = N_MOANS; |
| 1538 | if (moans-- > 0) |
| 1539 | kludged("pthread_rwlock_unlock"); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1540 | return 0; |
| 1541 | } |
| 1542 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1543 | weak_alias(pthread_rwlock_unlock, __pthread_rwlock_unlock) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1544 | |
| 1545 | |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 1546 | int |
| 1547 | pthread_rwlock_wrlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1548 | { |
| 1549 | static int moans = N_MOANS; |
| 1550 | if (moans-- > 0) |
| 1551 | kludged("pthread_rwlock_wrlock"); |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
sewardj | 262b029 | 2002-05-01 00:03:16 +0000 | [diff] [blame] | 1555 | weak_alias(pthread_rwlock_wrlock, __pthread_rwlock_wrlock) |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 1556 | |
| 1557 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1558 | /* I've no idea what these are, but they get called quite a lot. |
| 1559 | Anybody know? */ |
| 1560 | |
| 1561 | #undef _IO_flockfile |
| 1562 | void _IO_flockfile ( _IO_FILE * file ) |
| 1563 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1564 | pthread_mutex_lock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1565 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1566 | weak_alias(_IO_flockfile, flockfile); |
| 1567 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1568 | |
| 1569 | #undef _IO_funlockfile |
| 1570 | void _IO_funlockfile ( _IO_FILE * file ) |
| 1571 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1572 | pthread_mutex_unlock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1573 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1574 | weak_alias(_IO_funlockfile, funlockfile); |
| 1575 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1576 | |
| 1577 | void _pthread_cleanup_push_defer ( void ) |
| 1578 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1579 | static int moans = N_MOANS; |
| 1580 | if (moans-- > 0) |
| 1581 | ignored("_pthread_cleanup_push_defer"); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | void _pthread_cleanup_pop_restore ( void ) |
| 1585 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1586 | static int moans = N_MOANS; |
| 1587 | if (moans-- > 0) |
| 1588 | ignored("_pthread_cleanup_pop_restore"); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1589 | } |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 1590 | |
sewardj | 60e3842 | 2002-05-08 14:08:22 +0000 | [diff] [blame] | 1591 | /*--------*/ |
| 1592 | void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, |
| 1593 | void (*__routine) (void *), |
| 1594 | void *__arg) |
| 1595 | { |
| 1596 | static int moans = N_MOANS; |
| 1597 | if (moans-- > 0) |
| 1598 | ignored("_pthread_cleanup_push"); |
| 1599 | } |
| 1600 | |
| 1601 | void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, |
| 1602 | int __execute) |
| 1603 | { |
| 1604 | static int moans = N_MOANS; |
| 1605 | if (moans-- > 0) { |
| 1606 | if (__execute) |
| 1607 | ignored("_pthread_cleanup_pop-EXECUTE"); |
| 1608 | else |
| 1609 | ignored("_pthread_cleanup_pop-NO-EXECUTE"); |
| 1610 | } |
| 1611 | } |
| 1612 | |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 1613 | |
| 1614 | /* This doesn't seem to be needed to simulate libpthread.so's external |
| 1615 | interface, but many people complain about its absence. */ |
| 1616 | |
| 1617 | strong_alias(__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np) |
| 1618 | weak_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np) |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1619 | |
| 1620 | |
| 1621 | /*--------------------------------------------------------------------*/ |
| 1622 | /*--- end vg_libpthread.c ---*/ |
| 1623 | /*--------------------------------------------------------------------*/ |