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