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