| 1 |
/* This is the file to support POSIX threads. |
| 2 |
* We initialize the global data structure and the global access variable. |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "regina_c.h" |
| 6 |
#include "rexxsaa.h" |
| 7 |
#define DONT_TYPEDEF_PFN |
| 8 |
#include "rexx.h" |
| 9 |
#include <pthread.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
#include <pwd.h> |
| 13 |
#ifdef HAVE_NETDB_H |
| 14 |
# include <netdb.h> |
| 15 |
#endif |
| 16 |
#ifdef HAVE_NETINET_IN_H |
| 17 |
# include <netinet/in.h> |
| 18 |
#endif |
| 19 |
#ifdef HAVE_ARPA_INET_H |
| 20 |
# include <arpa/inet.h> |
| 21 |
#endif |
| 22 |
#ifdef HAVE_SYS_SOCKET_H |
| 23 |
# include <sys/socket.h> |
| 24 |
#endif |
| 25 |
#ifdef HAVE_GRP_H |
| 26 |
# include <grp.h> |
| 27 |
#endif |
| 28 |
#include <time.h> |
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
typedef struct _MT_mem { |
| 32 |
struct _MT_mem *prev; |
| 33 |
struct _MT_mem *next; |
| 34 |
/* real data follows here */ |
| 35 |
} MT_mem; |
| 36 |
|
| 37 |
typedef struct { /* mt_tsd: static variables of this module (thread-safe) */ |
| 38 |
struct group getgrgid_retval; |
| 39 |
char getgrgid_buf[2048]; /* AT LEAST 1K, better 2K or 4K */ |
| 40 |
struct passwd getpwuid_retval; |
| 41 |
char getpwuid_buf[2048]; /* AT LEAST 1K, better 2K or 4K */ |
| 42 |
struct hostent gethbyn_retval; |
| 43 |
char gethbyn_buf[8192]; /* sign, size copied from glibc */ |
| 44 |
char inetntoa_buf[16]; |
| 45 |
struct tm gmtime_retval; |
| 46 |
struct tm localtime_retval; |
| 47 |
#ifdef HAVE_RANDOM_DATA |
| 48 |
struct random_data random; |
| 49 |
char random_buf[64]; /* at least sizeof(clock_t) and time_t */ |
| 50 |
int random_init; |
| 51 |
#endif |
| 52 |
char strerror_buf[1024]; |
| 53 |
MT_mem * mem_base; |
| 54 |
} mt_tsd_t; /* thread-specific but only needed by this module. see |
| 55 |
* ReginaInitializeThread |
| 56 |
*/ |
| 57 |
|
| 58 |
/* every time a Regina thread is initialised by ReginaInitializeThread, the TSD |
| 59 |
* for that thread is added to an empty slot in this array. When a thread |
| 60 |
* terminates and calls Deinitialize, the used slot is freed up |
| 61 |
*/ |
| 62 |
tsd_t *tsds[MAX_CONCURRENT_REGINA_THREADS] = {0,}; |
| 63 |
|
| 64 |
static pthread_key_t ThreadIndex; /* index of the TSD, no initial value */ |
| 65 |
static pthread_once_t ThreadOnce = PTHREAD_ONCE_INIT; /* for pthread_once */ |
| 66 |
|
| 67 |
/* The following global structure may seem incongruous in this file. |
| 68 |
* When we run with the OPTION SINGLE_INTERPRETER, then we need to save |
| 69 |
* the thread-safe data from the thread-safe functions in a global variable |
| 70 |
* so that the data is available elsewhere. |
| 71 |
* The need for this is if you call a Rexx callback and that callback was |
| 72 |
* initiated by a thread controlled by an external API, then the thread-safe |
| 73 |
* structures in this file have not been set up, causing a SEGFAULT. |
| 74 |
* It is easier having a single global structure than replicating each field |
| 75 |
* of the structure in each of the below functions. |
| 76 |
* It would have been simpler to just call the non-thread-safe versions of the |
| 77 |
* below functions than setting this up, but then we get a stack overflow :-) |
| 78 |
*/ |
| 79 |
mt_tsd_t g_mt_tsd; |
| 80 |
|
| 81 |
/* Deinitialize is called when the thread terminates. |
| 82 |
* This is a wonderful position to place code which frees all allocated stuff! |
| 83 |
*/ |
| 84 |
static void Deinitialize(void *buf) |
| 85 |
{ |
| 86 |
tsd_t *TSD = (tsd_t *)buf; |
| 87 |
mt_tsd_t *mt; |
| 88 |
MT_mem *chunk; |
| 89 |
int i; |
| 90 |
|
| 91 |
if (TSD == NULL) /* Should never happen but be sure */ |
| 92 |
return; |
| 93 |
|
| 94 |
deinit_rexxsaa(TSD); |
| 95 |
|
| 96 |
mt = (mt_tsd_t *)TSD->mt_tsd; |
| 97 |
if (mt) |
| 98 |
{ |
| 99 |
while ((chunk = mt->mem_base) != NULL) |
| 100 |
{ |
| 101 |
TSD->MTFree(TSD,chunk + 1); |
| 102 |
if (mt->mem_base == chunk) |
| 103 |
break; /* something goes wrong. Don't run into an endless loop */ |
| 104 |
} |
| 105 |
free(mt); |
| 106 |
} |
| 107 |
/* |
| 108 |
* Free up our slot in the list of process TSDs. |
| 109 |
* Probably does not need to be protected (only allow one thread at a time) |
| 110 |
*/ |
| 111 |
for ( i = 0; i < MAX_CONCURRENT_REGINA_THREADS; i++ ) |
| 112 |
{ |
| 113 |
if ( tsds[i] == TSD ) |
| 114 |
{ |
| 115 |
tsds[i] = NULL; |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
free(TSD); |
| 120 |
} |
| 121 |
|
| 122 |
int IfcReginaCleanup( VOID ) |
| 123 |
{ |
| 124 |
tsd_t *TSD = __regina_get_tsd(); |
| 125 |
|
| 126 |
if (TSD == NULL) |
| 127 |
return 0; |
| 128 |
Deinitialize(TSD); |
| 129 |
pthread_setspecific(ThreadIndex,NULL); |
| 130 |
|
| 131 |
return 1; |
| 132 |
} |
| 133 |
|
| 134 |
/* ThreadGetKey creates a new index key into the TSD table. This function |
| 135 |
* must only be used by pthread_once. |
| 136 |
*/ |
| 137 |
static void ThreadGetKey(void) |
| 138 |
{ |
| 139 |
pthread_key_create(&ThreadIndex,Deinitialize); |
| 140 |
} |
| 141 |
|
| 142 |
/* Lowest level memory allocation function for normal circumstances. */ |
| 143 |
static void *MTMalloc( const tsd_t *TSD, size_t size ) |
| 144 |
{ |
| 145 |
mt_tsd_t *mt; |
| 146 |
MT_mem *newptr = (MT_mem *)malloc(size + sizeof(MT_mem)); |
| 147 |
|
| 148 |
if (newptr == NULL) /* may happen. errors are detected in the above layers */ |
| 149 |
return(NULL); |
| 150 |
|
| 151 |
mt = (mt_tsd_t *)TSD->mt_tsd; |
| 152 |
newptr->prev = NULL; |
| 153 |
newptr->next = mt->mem_base; |
| 154 |
if (mt->mem_base) |
| 155 |
mt->mem_base->prev = newptr; |
| 156 |
mt->mem_base = newptr; |
| 157 |
return(newptr + 1); /* jump over the head */ |
| 158 |
} |
| 159 |
|
| 160 |
/* Lowest level memory deallocation function for normal circumstances. */ |
| 161 |
static void MTFree( const tsd_t *TSD, void *chunk ) |
| 162 |
{ |
| 163 |
mt_tsd_t *mt = (mt_tsd_t *)TSD->mt_tsd; |
| 164 |
MT_mem *thisptr; |
| 165 |
|
| 166 |
/* |
| 167 |
* Just in case... |
| 168 |
*/ |
| 169 |
if (chunk == NULL) |
| 170 |
return; |
| 171 |
|
| 172 |
thisptr = (MT_mem *)chunk; |
| 173 |
thisptr--; /* Go to the header of the chunk */ |
| 174 |
|
| 175 |
if (thisptr->prev) |
| 176 |
{ |
| 177 |
if (thisptr->prev->next != thisptr) |
| 178 |
return; |
| 179 |
} |
| 180 |
if (thisptr->next) |
| 181 |
{ |
| 182 |
if (thisptr->next->prev != thisptr) |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
/* This is a chunk allocated by MTMalloc */ |
| 187 |
if (thisptr->prev) |
| 188 |
thisptr->prev->next = thisptr->next; |
| 189 |
if (thisptr->next) |
| 190 |
thisptr->next->prev = thisptr->prev; |
| 191 |
if (thisptr == mt->mem_base) |
| 192 |
mt->mem_base = thisptr->next; |
| 193 |
|
| 194 |
/* Last not least we set the pointers to NULL. This prevents a double-free*/ |
| 195 |
thisptr->next = NULL; |
| 196 |
thisptr->prev = NULL; |
| 197 |
free(thisptr); |
| 198 |
} |
| 199 |
|
| 200 |
/* Lowest level exit handler. */ |
| 201 |
static void MTExit(int code) |
| 202 |
{ |
| 203 |
/* pthread_exit's argument has a different semantic as exit has. */ |
| 204 |
pthread_exit(NULL); |
| 205 |
} |
| 206 |
|
| 207 |
/* ReginaInitializeThread creates a new thread structure and returns a ptr |
| 208 |
* to the initialized value. |
| 209 |
* The function may be called more than once. |
| 210 |
*/ |
| 211 |
tsd_t *ReginaInitializeThread(void) |
| 212 |
{ |
| 213 |
int OK; |
| 214 |
int i, found = 0; |
| 215 |
tsd_t *retval; |
| 216 |
|
| 217 |
/* get a unique access variable for the whole process */ |
| 218 |
pthread_once(&ThreadOnce,ThreadGetKey); |
| 219 |
|
| 220 |
/* fetch the value of the access variable */ |
| 221 |
retval = (tsd_t *)pthread_getspecific(ThreadIndex); |
| 222 |
|
| 223 |
if (retval != NULL) |
| 224 |
return(retval); |
| 225 |
|
| 226 |
/* First call in this thread... */ |
| 227 |
retval = (tsd_t *)malloc(sizeof(tsd_t)); /* no Malloc, etc! */ |
| 228 |
|
| 229 |
if (retval == NULL) /* THIS is really a problem. I don't know what we */ |
| 230 |
return(NULL); /* should do now. Let the caller run into a crash... */ |
| 231 |
|
| 232 |
pthread_setspecific(ThreadIndex,retval); |
| 233 |
|
| 234 |
memset(retval,0,sizeof(tsd_t)); |
| 235 |
retval->MTMalloc = MTMalloc; |
| 236 |
retval->MTFree = MTFree; |
| 237 |
retval->MTExit = MTExit; |
| 238 |
|
| 239 |
/* Since the local data structure contains a memory chain for the memory |
| 240 |
* management we initialize it first. |
| 241 |
*/ |
| 242 |
if ( ( retval->mt_tsd = (mt_tsd_t *)malloc( sizeof(mt_tsd_t) ) ) == NULL ) |
| 243 |
return(NULL); /* This is a catastrophy */ |
| 244 |
memset( retval->mt_tsd, 0, sizeof(mt_tsd_t) ); |
| 245 |
|
| 246 |
OK = init_memory(retval); /* Initialize the memory module FIRST*/ |
| 247 |
|
| 248 |
/* Without the initial memory we don't have ANY chance! */ |
| 249 |
if (!OK) |
| 250 |
return(NULL); |
| 251 |
|
| 252 |
{ |
| 253 |
extern OS_Dep_funcs __regina_OS_Unx; |
| 254 |
retval->OS = &__regina_OS_Unx; |
| 255 |
} |
| 256 |
|
| 257 |
OK &= init_vars(retval); /* Initialize the variable module */ |
| 258 |
OK &= init_stacks(retval); /* Initialize the stack module */ |
| 259 |
OK &= init_filetable(retval); /* Initialize the files module */ |
| 260 |
OK &= init_math(retval); /* Initialize the math module */ |
| 261 |
OK &= init_spec_vars(retval); /* Initialize the interprt module */ |
| 262 |
OK &= init_tracing(retval); /* Initialize the tracing module */ |
| 263 |
OK &= init_builtin(retval); /* Initialize the builtin module */ |
| 264 |
OK &= init_client(retval); /* Initialize the client module */ |
| 265 |
OK &= init_library(retval); /* Initialize the library module */ |
| 266 |
OK &= init_rexxsaa(retval); /* Initialize the rexxsaa module */ |
| 267 |
OK &= init_shell(retval); /* Initialize the shell module */ |
| 268 |
OK &= init_envir(retval); /* Initialize the envir module */ |
| 269 |
OK &= init_expr(retval); /* Initialize the expr module */ |
| 270 |
OK &= init_error(retval); /* Initialize the error module */ |
| 271 |
#ifdef VMS |
| 272 |
OK &= init_vms(retval); /* Initialize the vmscmd module */ |
| 273 |
OK &= init_vmf(retval); /* Initialize the vmsfuncs module */ |
| 274 |
#endif |
| 275 |
OK &= init_arexxf(retval); /* Initialize the arxfuncs modules */ |
| 276 |
retval->loopcnt = 1; /* stupid r2perl-module */ |
| 277 |
retval->traceparse = -1; |
| 278 |
retval->thread_id = (unsigned long)pthread_self(); |
| 279 |
|
| 280 |
if (!OK) |
| 281 |
exiterror( ERR_STORAGE_EXHAUSTED, 0 ) ; |
| 282 |
/* |
| 283 |
* Update the global list of threadid/TSD. |
| 284 |
* Should this be protected (only allow one thread at a time)??? |
| 285 |
*/ |
| 286 |
for ( i = 0; i < MAX_CONCURRENT_REGINA_THREADS; i++ ) |
| 287 |
{ |
| 288 |
if ( tsds[i] == 0 ) |
| 289 |
{ |
| 290 |
tsds[i] = retval; |
| 291 |
found = 1; |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
if ( found == 0 ) |
| 296 |
exiterror( ERR_STORAGE_EXHAUSTED, 1, "MAX_CONCURRENT_REGINA_THREADS exceeded." ) ; |
| 297 |
|
| 298 |
return(retval); |
| 299 |
} |
| 300 |
|
| 301 |
int __regina_get_number_concurrent_regina_threads(void) |
| 302 |
{ |
| 303 |
return MAX_CONCURRENT_REGINA_THREADS; |
| 304 |
} |
| 305 |
|
| 306 |
tsd_t *__regina_get_tsd_for_threadid( unsigned long threadid ) |
| 307 |
{ |
| 308 |
int i; |
| 309 |
for ( i = 0; i < MAX_CONCURRENT_REGINA_THREADS; i++ ) |
| 310 |
{ |
| 311 |
if ( tsds[i]->thread_id == threadid ) |
| 312 |
return tsds[i]; |
| 313 |
} |
| 314 |
return NULL; |
| 315 |
} |
| 316 |
|
| 317 |
tsd_t *__regina_get_next_tsd( int idx ) |
| 318 |
{ |
| 319 |
return tsds[idx]; |
| 320 |
} |
| 321 |
|
| 322 |
/* __regina_get_tsd returns a pointer to the thread specific data. Be sure to |
| 323 |
* call this only after a call to ReginaInitializeThread(). |
| 324 |
*/ |
| 325 |
tsd_t *__regina_get_tsd(void) |
| 326 |
{ |
| 327 |
/* See above for comments */ |
| 328 |
return( (tsd_t *)pthread_getspecific( ThreadIndex ) ); |
| 329 |
} |
| 330 |
|
| 331 |
/****************************************************************************** |
| 332 |
****************************************************************************** |
| 333 |
* Emulation functions to support multi-threading ***************************** |
| 334 |
* The functions will be opaque to the caller ********************************* |
| 335 |
* The "original" functions at this place are not multi-threading safe ******** |
| 336 |
****************************************************************************** |
| 337 |
*****************************************************************************/ |
| 338 |
#if defined(HAVE_GETGRGID_R_RETURNS_INT_5_PARAMS) || defined(HAVE_GETGRGID_R_RETURNS_INT_4_PARAMS) ||defined(HAVE_GETGRGID_R_RETURNS_STRUCT) |
| 339 |
/* see documentation of getgrgid and getgrgid_r */ |
| 340 |
struct group *getgrgid(gid_t gid) |
| 341 |
{ |
| 342 |
struct group *ptr=NULL; |
| 343 |
int rc=0; |
| 344 |
# ifdef HAVE_GETGRGID_R_RETURNS_INT_5_PARAMS |
| 345 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 346 |
{ |
| 347 |
rc = getgrgid_r(gid, |
| 348 |
&g_mt_tsd.getgrgid_retval, |
| 349 |
g_mt_tsd.getgrgid_buf, |
| 350 |
sizeof(g_mt_tsd.getgrgid_buf), |
| 351 |
&ptr); |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 356 |
rc = getgrgid_r(gid, |
| 357 |
&mt->getgrgid_retval, |
| 358 |
mt->getgrgid_buf, |
| 359 |
sizeof(mt->getgrgid_buf), |
| 360 |
&ptr); |
| 361 |
} |
| 362 |
# endif |
| 363 |
# ifdef HAVE_GETGRGID_R_RETURNS_INT_4_PARAMS |
| 364 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 365 |
{ |
| 366 |
rc = getgrgid_r(gid, |
| 367 |
&mt->getgrgid_retval, |
| 368 |
mt->getgrgid_buf, |
| 369 |
sizeof(mt->getgrgid_buf)); |
| 370 |
|
| 371 |
ptr = &mt->getgrgid_retval; |
| 372 |
# endif |
| 373 |
# ifdef HAVE_GETGRGID_R_RETURNS_STRUCT |
| 374 |
ptr = getgrgid_r(gid, |
| 375 |
&mt->getgrgid_retval, |
| 376 |
mt->getgrgid_buf, |
| 377 |
sizeof(mt->getgrgid_buf) ); |
| 378 |
# endif |
| 379 |
if ((rc != 0) || (ptr == NULL)) |
| 380 |
{ |
| 381 |
errno = rc; |
| 382 |
return(NULL); |
| 383 |
} |
| 384 |
return(ptr); |
| 385 |
} |
| 386 |
#endif |
| 387 |
|
| 388 |
#if defined(HAVE_GETPWUID_R_RETURNS_INT) || defined(HAVE_GETPWUID_R_RETURNS_STRUCT) |
| 389 |
/* see documentation of getpwuid and getpwuid_r */ |
| 390 |
struct passwd *getpwuid(uid_t uid) |
| 391 |
{ |
| 392 |
struct passwd *ptr=NULL; |
| 393 |
int rc=0; |
| 394 |
|
| 395 |
# ifdef HAVE_GETPWUID_R_RETURNS_INT |
| 396 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 397 |
{ |
| 398 |
rc = getpwuid_r(uid, |
| 399 |
&g_mt_tsd.getpwuid_retval, |
| 400 |
g_mt_tsd.getpwuid_buf, |
| 401 |
sizeof(g_mt_tsd.getpwuid_buf), |
| 402 |
&ptr); |
| 403 |
} |
| 404 |
else |
| 405 |
{ |
| 406 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 407 |
rc = getpwuid_r(uid, |
| 408 |
&mt->getpwuid_retval, |
| 409 |
mt->getpwuid_buf, |
| 410 |
sizeof(mt->getpwuid_buf), |
| 411 |
&ptr); |
| 412 |
} |
| 413 |
# else |
| 414 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 415 |
{ |
| 416 |
ptr = getpwuid_r(uid, |
| 417 |
&g_mt_tsd.getpwuid_retval, |
| 418 |
g_mt_tsd.getpwuid_buf, |
| 419 |
sizeof(g_mt_tsd.getpwuid_buf) ); |
| 420 |
} |
| 421 |
else |
| 422 |
{ |
| 423 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 424 |
ptr = getpwuid_r(uid, |
| 425 |
&mt->getpwuid_retval, |
| 426 |
mt->getpwuid_buf, |
| 427 |
sizeof(mt->getpwuid_buf) ); |
| 428 |
} |
| 429 |
# endif |
| 430 |
if ((rc != 0) || (ptr == NULL)) |
| 431 |
{ |
| 432 |
errno = rc; |
| 433 |
return(NULL); |
| 434 |
} |
| 435 |
return(ptr); |
| 436 |
} |
| 437 |
#endif |
| 438 |
|
| 439 |
/* see documentation of gmtime and gmtime_r */ |
| 440 |
struct tm *gmtime(const time_t *time) |
| 441 |
{ |
| 442 |
#ifdef HAVE_GMTIME_R |
| 443 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 444 |
{ |
| 445 |
return(gmtime_r(time,&g_mt_tsd.gmtime_retval)); |
| 446 |
} |
| 447 |
else |
| 448 |
{ |
| 449 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 450 |
return(gmtime_r(time,&mt->gmtime_retval)); |
| 451 |
} |
| 452 |
#else |
| 453 |
return(NULL); |
| 454 |
#endif |
| 455 |
} |
| 456 |
|
| 457 |
/* see documentation of localtime and localtime_r */ |
| 458 |
struct tm *localtime(const time_t *time) |
| 459 |
{ |
| 460 |
#ifdef HAVE_LOCALTIME_R |
| 461 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 462 |
{ |
| 463 |
return(localtime_r(time,&g_mt_tsd.localtime_retval)); |
| 464 |
} |
| 465 |
else |
| 466 |
{ |
| 467 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 468 |
return(localtime_r(time,&mt->localtime_retval)); |
| 469 |
} |
| 470 |
#else |
| 471 |
return(NULL); |
| 472 |
#endif |
| 473 |
} |
| 474 |
|
| 475 |
/* see documentation of gethostbyname and gethostbyname_r */ |
| 476 |
struct hostent *gethostbyname(const char *name) |
| 477 |
{ |
| 478 |
int herr; |
| 479 |
struct hostent *he=NULL; |
| 480 |
|
| 481 |
#ifdef HAVE_GETHOSTBYNAME_R_RETURNS_INT_6_PARAMS |
| 482 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 483 |
{ |
| 484 |
if (gethostbyname_r(name, |
| 485 |
&g_mt_tsd.gethbyn_retval, |
| 486 |
g_mt_tsd.gethbyn_buf, |
| 487 |
sizeof(g_mt_tsd.gethbyn_buf), |
| 488 |
&he, |
| 489 |
&herr) != 0) |
| 490 |
return(NULL); |
| 491 |
} |
| 492 |
else |
| 493 |
{ |
| 494 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 495 |
if (gethostbyname_r(name, |
| 496 |
&mt->gethbyn_retval, |
| 497 |
mt->gethbyn_buf, |
| 498 |
sizeof(mt->gethbyn_buf), |
| 499 |
&he, |
| 500 |
&herr) != 0) |
| 501 |
return(NULL); |
| 502 |
} |
| 503 |
#endif |
| 504 |
#ifdef HAVE_GETHOSTBYNAME_R_RETURNS_STRUCT_5_PARAMS |
| 505 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 506 |
{ |
| 507 |
he = gethostbyname_r(name, |
| 508 |
&g_mt_tsd.gethbyn_retval, |
| 509 |
g_mt_tsd.gethbyn_buf, |
| 510 |
sizeof(g_mt_tsd.gethbyn_buf), |
| 511 |
&herr); |
| 512 |
else |
| 513 |
{ |
| 514 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 515 |
he = gethostbyname_r(name, |
| 516 |
&mt->gethbyn_retval, |
| 517 |
mt->gethbyn_buf, |
| 518 |
sizeof(mt->gethbyn_buf), |
| 519 |
&herr); |
| 520 |
} |
| 521 |
#endif |
| 522 |
return(he); |
| 523 |
} |
| 524 |
|
| 525 |
/* see documentation of inet_ntoa and inet_ntop */ |
| 526 |
char *inet_ntoa(struct in_addr in) |
| 527 |
{ |
| 528 |
#ifdef HAVE_INET_NTOP |
| 529 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 530 |
{ |
| 531 |
return((char *) inet_ntop(AF_INET, |
| 532 |
&in, |
| 533 |
g_mt_tsd.inetntoa_buf, |
| 534 |
sizeof(g_mt_tsd.inetntoa_buf))); |
| 535 |
} |
| 536 |
else |
| 537 |
{ |
| 538 |
mt_tsd_t *mt = (mt_tsd_t *)__regina_get_tsd()->mt_tsd; |
| 539 |
|
| 540 |
return((char *) inet_ntop(AF_INET, |
| 541 |
&in, |
| 542 |
mt->inetntoa_buf, |
| 543 |
sizeof(mt->inetntoa_buf))); |
| 544 |
} |
| 545 |
#else |
| 546 |
return NULL; |
| 547 |
#endif |
| 548 |
} |
| 549 |
|
| 550 |
#if defined( HAVE_RANDOM_DATA ) && !defined( HAVE_RANDOM ) |
| 551 |
/* see documentation of random and random_r. If you get compile time errors |
| 552 |
* exclude this function. |
| 553 |
*/ |
| 554 |
int32_t random(void) |
| 555 |
{ |
| 556 |
int32_t retval; |
| 557 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 558 |
{ |
| 559 |
if (!g_mt_tsd.random_init) /* according to the ANSI this is equivalent to */ |
| 560 |
srandom(1); /* an initial value of 1 for the seed! */ |
| 561 |
random_r(&g_mt_tsd.random,&retval); /* ignore errors, we MUST return anything */ |
| 562 |
} |
| 563 |
else |
| 564 |
{ |
| 565 |
mt_tsd_t *mt = __regina_get_tsd()->mt_tsd; |
| 566 |
|
| 567 |
if (!mt->random_init) /* according to the ANSI this is equivalent to */ |
| 568 |
srandom(1); /* an initial value of 1 for the seed! */ |
| 569 |
random_r(&mt->random,&retval); /* ignore errors, we MUST return anything */ |
| 570 |
} |
| 571 |
return(retval); |
| 572 |
} |
| 573 |
|
| 574 |
/* see documentation of srandom and srandom_r. If you get compile time errors |
| 575 |
* exclude this function. |
| 576 |
*/ |
| 577 |
void srandom(unsigned seed) |
| 578 |
{ |
| 579 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 580 |
{ |
| 581 |
/* The stack is normally not cleared: wonderful random stuff. */ |
| 582 |
char this_is_random_stuff[sizeof(g_mt_tsd.random_buf)]; |
| 583 |
|
| 584 |
if (!g_mt_tsd.random_init) |
| 585 |
{ |
| 586 |
g_mt_tsd.random_init = 1; |
| 587 |
/* In case of a cleared new stack seqment we initialize our buffer |
| 588 |
* with at least one random stuff: |
| 589 |
*/ |
| 590 |
time((time_t *) this_is_random_stuff); |
| 591 |
/* If the buffer is large enough, use a second one */ |
| 592 |
if (sizeof(time_t) + sizeof(clock_t) < sizeof(this_is_random_stuff)) |
| 593 |
*((clock_t *) (this_is_random_stuff + sizeof(time_t))) = clock(); |
| 594 |
|
| 595 |
memcpy(g_mt_tsd.random_buf,this_is_random_stuff,sizeof(g_mt_tsd.random_buf)); |
| 596 |
|
| 597 |
initstate_r(seed, |
| 598 |
g_mt_tsd.random_buf, |
| 599 |
sizeof(g_mt_tsd.random_buf), |
| 600 |
&g_mt_tsd.random); |
| 601 |
/* initstate calls srandom. We can return savely. */ |
| 602 |
return; |
| 603 |
} |
| 604 |
srandom_r(seed,&g_mt_tsd.random); |
| 605 |
} |
| 606 |
else |
| 607 |
{ |
| 608 |
mt_tsd_t *mt = __regina_get_tsd()->mt_tsd; |
| 609 |
/* The stack is normally not cleared: wonderful random stuff. */ |
| 610 |
char this_is_random_stuff[sizeof(mt->random_buf)]; |
| 611 |
|
| 612 |
if (!mt->random_init) |
| 613 |
{ |
| 614 |
mt->random_init = 1; |
| 615 |
/* In case of a cleared new stack seqment we initialize our buffer |
| 616 |
* with at least one random stuff: |
| 617 |
*/ |
| 618 |
time((time_t *) this_is_random_stuff); |
| 619 |
/* If the buffer is large enough, use a second one */ |
| 620 |
if (sizeof(time_t) + sizeof(clock_t) < sizeof(this_is_random_stuff)) |
| 621 |
*((clock_t *) (this_is_random_stuff + sizeof(time_t))) = clock(); |
| 622 |
|
| 623 |
memcpy(mt->random_buf,this_is_random_stuff,sizeof(mt->random_buf)); |
| 624 |
|
| 625 |
initstate_r(seed, |
| 626 |
mt->random_buf, |
| 627 |
sizeof(mt->random_buf), |
| 628 |
&mt->random); |
| 629 |
/* initstate calls srandom. We can return savely. */ |
| 630 |
return; |
| 631 |
} |
| 632 |
srandom_r(seed,&mt->random); |
| 633 |
} |
| 634 |
} |
| 635 |
#endif |
| 636 |
|
| 637 |
#ifdef STRERROR_R |
| 638 |
/* see documentation of strerror and strerror_r. */ |
| 639 |
char *strerror(int errnum) |
| 640 |
{ |
| 641 |
if ( getGlobalTSD() || __regina_get_tsd() == NULL ) |
| 642 |
{ |
| 643 |
return(strerror_r(errnum,g_mt_tsd.strerror_buf,sizeof(g_mt_tsd.strerror_buf))); |
| 644 |
} |
| 645 |
else |
| 646 |
{ |
| 647 |
mt_tsd_t *mt = __regina_get_tsd()->mt_tsd; |
| 648 |
return(strerror_r(errnum,mt->strerror_buf,sizeof(mt->strerror_buf))); |
| 649 |
} |
| 650 |
} |
| 651 |
#endif |
| 652 |
|