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