[0] | 1 | //libroar.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[5961] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014 |
---|
[690] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[690] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
[4916] | 36 | /* ckport options: |
---|
[4923] | 37 | * ckport: ignore-symbol: sleep of target win32 -- checked by configure |
---|
[4916] | 38 | */ |
---|
| 39 | |
---|
[0] | 40 | #include "libroar.h" |
---|
| 41 | |
---|
[4243] | 42 | #ifndef roar_mm_mlock |
---|
| 43 | int roar_mm_mlock(const void *addr, size_t len) { |
---|
[1434] | 44 | #if defined(ROAR_TARGET_WIN32) |
---|
[4945] | 45 | /* GlobalLock() generates errors. Maybe we do not use correctly. |
---|
| 46 | * We just ignore that at the moment and throw NOSYS. |
---|
| 47 | * |
---|
| 48 | * return GlobalLock(addr) == addr ? 0 : -1; |
---|
| 49 | */ |
---|
| 50 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 51 | return -1; |
---|
[1434] | 52 | #elif defined(ROAR_TARGET_MICROCONTROLLER) |
---|
| 53 | return 0; |
---|
[4554] | 54 | #elif defined(_SC_PAGESIZE) |
---|
[700] | 55 | long sz = sysconf(_SC_PAGESIZE); |
---|
| 56 | unsigned long int pos = (unsigned long int) addr; |
---|
| 57 | |
---|
| 58 | len += sz - (len % sz); |
---|
| 59 | |
---|
| 60 | pos -= pos % sz; |
---|
| 61 | |
---|
| 62 | return mlock((void*)pos, len); |
---|
[4554] | 63 | #else |
---|
[4945] | 64 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[4554] | 65 | return -1; |
---|
[1076] | 66 | #endif |
---|
[700] | 67 | } |
---|
[4243] | 68 | #endif |
---|
| 69 | |
---|
| 70 | #ifndef roar_mm_munlock |
---|
| 71 | int roar_mm_munlock(const void *addr, size_t len) { |
---|
| 72 | #if defined(ROAR_TARGET_WIN32) |
---|
| 73 | // TODO: find out what do do here. GlobalUnLock()? does such a function exist? |
---|
| 74 | // return GlobalLock(addr) == addr ? 0 : -1; |
---|
[4945] | 75 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[4243] | 76 | return -1; |
---|
| 77 | #elif defined(ROAR_TARGET_MICROCONTROLLER) |
---|
| 78 | return 0; |
---|
[4554] | 79 | #elif defined(_SC_PAGESIZE) |
---|
[4243] | 80 | long sz = sysconf(_SC_PAGESIZE); |
---|
| 81 | unsigned long int pos = (unsigned long int) addr; |
---|
| 82 | |
---|
| 83 | len += sz - (len % sz); |
---|
| 84 | |
---|
| 85 | pos -= pos % sz; |
---|
| 86 | |
---|
| 87 | return munlock((void*)pos, len); |
---|
[4554] | 88 | #else |
---|
[4945] | 89 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[4554] | 90 | return -1; |
---|
[4243] | 91 | #endif |
---|
| 92 | } |
---|
| 93 | #endif |
---|
| 94 | |
---|
| 95 | // for compatibility with old versions: |
---|
| 96 | int _ROAR_MLOCK(const void *addr, size_t len) { |
---|
| 97 | roar_debug_warn_obsolete("_ROAR_MLOCK", "roar_mm_mlock", NULL); |
---|
| 98 | return roar_mm_mlock(addr, len); |
---|
| 99 | } |
---|
[700] | 100 | |
---|
[6007] | 101 | int_least32_t roar_str2usec(const char * str) { |
---|
| 102 | int_least32_t man = 0, expo = 0, extra_expo = 0; |
---|
| 103 | int_least32_t * cur = &man; |
---|
| 104 | register unsigned char c; |
---|
| 105 | int mneg = 0, eneg = 0; |
---|
| 106 | int * neg = &mneg; |
---|
| 107 | int after_point = 0; |
---|
| 108 | |
---|
| 109 | for (; (c = *str); str++) { |
---|
| 110 | if ( c >= '0' && c <= '9' ) { |
---|
| 111 | *cur *= 10; |
---|
| 112 | *cur += c - '0'; |
---|
| 113 | if ( after_point ) |
---|
| 114 | extra_expo--; |
---|
| 115 | } else if ( c == '.' ) { |
---|
| 116 | after_point = 1; |
---|
| 117 | } else if ( c == '-' ) { |
---|
| 118 | *neg = 1; |
---|
| 119 | } else if ( c == '+' ) { |
---|
| 120 | *neg = 0; |
---|
| 121 | } else if ( c == 'e' ) { |
---|
| 122 | cur = &expo; |
---|
| 123 | neg = &eneg; |
---|
| 124 | after_point = 0; |
---|
| 125 | } else if ( c == 'm' ) { |
---|
| 126 | extra_expo += -3; |
---|
| 127 | } else if ( c == 'u' || c == 181 ) { |
---|
| 128 | extra_expo += -6; |
---|
| 129 | } else if ( c == 's' ) { |
---|
| 130 | // ignore. |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if ( eneg ) |
---|
| 135 | expo *= -1; |
---|
| 136 | |
---|
| 137 | expo += 6 + extra_expo; |
---|
| 138 | |
---|
| 139 | if ( expo < 0 ) { |
---|
| 140 | for (; expo; expo++) |
---|
| 141 | man /= 10; |
---|
| 142 | } else if ( expo > 0 ) { |
---|
| 143 | for (; expo; expo--) |
---|
| 144 | man *= 10; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | return mneg ? -man : man; |
---|
| 148 | } |
---|
| 149 | |
---|
[4913] | 150 | int roar_usleep(uint_least32_t t) { |
---|
[4896] | 151 | #ifdef ROAR_TARGET_WIN32 |
---|
| 152 | Sleep(t/(uint_least32_t)1000); |
---|
[4913] | 153 | return 0; |
---|
[4896] | 154 | #elif defined(ROAR_HAVE_NANOSLEEP) |
---|
| 155 | struct timespec tv; |
---|
| 156 | struct timespec left; |
---|
| 157 | |
---|
[5619] | 158 | if ( t >= (uint_least32_t)1000000 ) { |
---|
[4896] | 159 | tv.tv_sec = t/(uint_least32_t)1000000; |
---|
| 160 | t -= tv.tv_sec*(uint_least32_t)1000000; |
---|
| 161 | } else { |
---|
| 162 | tv.tv_sec = 0; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | tv.tv_nsec = t*(uint_least32_t)1000; |
---|
| 166 | |
---|
| 167 | while (nanosleep(&tv, &left) == -1) |
---|
| 168 | memcpy(&tv, &left, sizeof(tv)); |
---|
[4913] | 169 | return 0; |
---|
[4896] | 170 | #elif defined(ROAR_HAVE_USLEEP) |
---|
| 171 | usleep(t); |
---|
[4913] | 172 | return 0; |
---|
[4896] | 173 | #else |
---|
[5837] | 174 | ROAR_ERR("roar_usleep(t=%" LIBROAR__ll "u): can not sleep: not implemented", (LIBROAR__longlong unsigned int)t); |
---|
[4896] | 175 | roar_strap(ROAR_TRAP_GROUP_LIBROAR, "usleep.not-implemented"); |
---|
| 176 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[4913] | 177 | return -1; |
---|
[4896] | 178 | #endif |
---|
| 179 | } |
---|
| 180 | |
---|
[4913] | 181 | int roar_sleep(int t) { |
---|
| 182 | if ( t < 0 ) { |
---|
| 183 | roar_err_set(ROAR_ERROR_CAUSALITY); |
---|
| 184 | return -1; |
---|
| 185 | } |
---|
| 186 | |
---|
[4915] | 187 | #ifdef ROAR_TARGET_WIN32 |
---|
| 188 | Sleep(1000L*(long)t); |
---|
[5020] | 189 | #elif defined(ROAR_HAVE_SLEEP) |
---|
[4913] | 190 | while (t) |
---|
| 191 | t = sleep(t); |
---|
[5020] | 192 | #else |
---|
| 193 | while (t) { |
---|
| 194 | if ( roar_usleep(500000) == -1 ) |
---|
| 195 | return -1; |
---|
| 196 | if ( roar_usleep(500000) == -1 ) |
---|
| 197 | return -1; |
---|
| 198 | t--; |
---|
| 199 | } |
---|
[4915] | 200 | #endif |
---|
[4913] | 201 | |
---|
| 202 | return 0; |
---|
| 203 | } |
---|
| 204 | |
---|
[5373] | 205 | static pid_t _libroar_fork(void ** context, void * userdata) { |
---|
[5390] | 206 | #ifdef ROAR_HAVE_FORK |
---|
[5373] | 207 | pid_t ret; |
---|
| 208 | (void)context, (void)userdata; |
---|
| 209 | ret = fork(); |
---|
| 210 | if ( ret == -1 ) |
---|
| 211 | roar_err_from_errno(); |
---|
| 212 | return ret; |
---|
[5390] | 213 | #else |
---|
| 214 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
| 215 | return (pid_t)-1; |
---|
| 216 | #endif |
---|
[5373] | 217 | } |
---|
| 218 | |
---|
| 219 | static const struct roar_libroar_forkapi _libroar_forkapi = { |
---|
| 220 | .prefork = NULL, |
---|
| 221 | .fork = _libroar_fork, |
---|
| 222 | .failed = NULL, |
---|
| 223 | .parent = NULL, |
---|
| 224 | .child = NULL, |
---|
| 225 | .userdata = NULL |
---|
| 226 | }; |
---|
| 227 | |
---|
| 228 | pid_t roar_fork(const struct roar_libroar_forkapi * api) { |
---|
| 229 | void * context = NULL; |
---|
| 230 | pid_t ret; |
---|
| 231 | |
---|
| 232 | if ( api == NULL ) |
---|
| 233 | api = &_libroar_forkapi; |
---|
| 234 | |
---|
| 235 | if ( api->fork == NULL ) { |
---|
| 236 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 237 | return -1; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | if ( api->prefork != NULL ) { |
---|
| 241 | if ( api->prefork(&context, api->userdata) != 0 ) { |
---|
| 242 | if ( api->failed != NULL ) |
---|
| 243 | api->failed(&context, api->userdata); |
---|
| 244 | |
---|
[5901] | 245 | if ( context != NULL ) |
---|
| 246 | roar_mm_free_noerror(context); |
---|
[5373] | 247 | return (pid_t)-1; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | if ( (ret = api->fork(&context, api->userdata)) == (pid_t)-1 ) { |
---|
| 252 | if ( api->failed != NULL ) |
---|
| 253 | api->failed(&context, api->userdata); |
---|
| 254 | |
---|
[5901] | 255 | if ( context != NULL ) |
---|
| 256 | roar_mm_free_noerror(context); |
---|
[5373] | 257 | return (pid_t)-1; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | if ( ret == (pid_t)0 ) { |
---|
| 261 | if ( api->child != NULL ) |
---|
| 262 | api->child(&context, api->userdata); |
---|
| 263 | } else { |
---|
| 264 | if ( api->parent != NULL ) |
---|
| 265 | api->parent(&context, api->userdata, ret); |
---|
| 266 | } |
---|
| 267 | |
---|
[5901] | 268 | if ( context != NULL ) |
---|
| 269 | roar_mm_free_noerror(context); |
---|
[5373] | 270 | |
---|
| 271 | return ret; |
---|
| 272 | } |
---|
| 273 | |
---|
[5010] | 274 | int roar_reset(enum roar_reset what) { |
---|
[4965] | 275 | char c; |
---|
| 276 | int i; |
---|
[5010] | 277 | enum roar_reset subsets = ROAR_RESET_NONE; |
---|
[4965] | 278 | |
---|
| 279 | roar_err_set(ROAR_ERROR_NONE); |
---|
| 280 | |
---|
[5010] | 281 | switch (what) { |
---|
| 282 | case ROAR_RESET_NONE: |
---|
| 283 | return 0; |
---|
| 284 | break; |
---|
| 285 | case ROAR_RESET_UNKNOWN: |
---|
| 286 | case ROAR_RESET_EOL: |
---|
| 287 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 288 | return -1; |
---|
| 289 | break; |
---|
| 290 | case ROAR_RESET_ON_FORK: |
---|
| 291 | subsets |= ROAR_RESET_MEMORY|ROAR_RESET_RANDOMPOOL; |
---|
| 292 | break; |
---|
| 293 | case ROAR_RESET_ON_EXIT: |
---|
| 294 | case ROAR_RESET_ON_PRE_EXEC: |
---|
| 295 | subsets |= ROAR_RESET_MEMORY|ROAR_RESET_CONFIG; |
---|
| 296 | break; |
---|
| 297 | default: |
---|
| 298 | if ( what & 0x80 ) { |
---|
| 299 | subsets |= what; |
---|
| 300 | } else { |
---|
| 301 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 302 | return -1; |
---|
| 303 | } |
---|
| 304 | break; |
---|
[4965] | 305 | } |
---|
| 306 | |
---|
[5010] | 307 | // strip 0x80 so we can easly test on the other bit per subsystem. |
---|
| 308 | what |= 0x80; |
---|
| 309 | what -= 0x80; |
---|
| 310 | |
---|
| 311 | if ( what & ROAR_RESET_MEMORY ) { |
---|
[5017] | 312 | (void)roar_mm_reset(); |
---|
[5010] | 313 | } |
---|
| 314 | |
---|
| 315 | if ( what & ROAR_RESET_CONFIG ) { |
---|
| 316 | roar_libroar_reset_config(); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | if ( what & ROAR_RESET_RANDOMPOOL ) { |
---|
| 320 | for (i = 0; i < 16; i++) { |
---|
| 321 | roar_random_gen_nonce(&c, 1); |
---|
| 322 | roar_random_uint16(); |
---|
| 323 | roar_random_uint32(); |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | roar_err_set(ROAR_ERROR_NONE); |
---|
[4965] | 328 | return 0; |
---|
| 329 | } |
---|
| 330 | |
---|
[4970] | 331 | void roar_panic_real(enum roar_fatal_error error, const char * info, |
---|
| 332 | unsigned long int line, const char * file, const char * prefix, const char * func) { |
---|
| 333 | const char * errname = NULL; |
---|
| 334 | //#define ROAR_ERR(format, args...) roar_debug_msg(ROAR_DEBUG_TYPE_ERROR, __LINE__, __FILE__, ROAR_DBG_PREFIX, format, ## args) |
---|
| 335 | |
---|
[5270] | 336 | // we do not support info-text at the moment. |
---|
| 337 | (void)info; |
---|
| 338 | |
---|
[4970] | 339 | if ( func == NULL ) |
---|
| 340 | func = "<unknown>"; |
---|
| 341 | |
---|
| 342 | switch (error) { |
---|
| 343 | case ROAR_FATAL_ERROR_MEMORY_CORRUPTION_GUARD: |
---|
[4972] | 344 | roar_debug_msg(ROAR_DEBUG_TYPE_ERROR, line, file, prefix, "in %s() a guard segment memory corruption was detected. Backup your data and terminate this program. Report this lion to developer. Thanks.", func); |
---|
[4970] | 345 | return; |
---|
| 346 | break; |
---|
| 347 | case ROAR_FATAL_ERROR_UNKNOWN: |
---|
| 348 | errname = "<unknown error>"; |
---|
| 349 | break; |
---|
| 350 | case ROAR_FATAL_ERROR_MEMORY_CORRUPTION: |
---|
| 351 | errname = "Memory corruption"; |
---|
| 352 | break; |
---|
[4998] | 353 | case ROAR_FATAL_ERROR_CPU_FAILURE: |
---|
| 354 | errname = "CPU failure, replace hardware"; |
---|
| 355 | break; |
---|
| 356 | case ROAR_FATAL_ERROR_MEMORY_USED_AFTER_FREE: |
---|
| 357 | errname = "Memory used after free"; |
---|
| 358 | break; |
---|
| 359 | case ROAR_FATAL_ERROR_MEMORY_DOUBLE_FREE: |
---|
| 360 | errname = "Memory double freed"; |
---|
[4970] | 361 | break; |
---|
[5619] | 362 | case ROAR_FATAL_ERROR_WATCHDOG: |
---|
| 363 | errname = "Watchdog Timeout"; |
---|
| 364 | break; |
---|
[4970] | 365 | default: |
---|
| 366 | errname = "<unknown error code, BAD>"; |
---|
| 367 | break; |
---|
| 368 | } |
---|
| 369 | |
---|
[4972] | 370 | roar_debug_msg(ROAR_DEBUG_TYPE_ERROR, line, file, prefix, "in %s() a _FATAL_ error of type \"%s\" was detected. Terminating program. Report this lion to developer. Thanks.", func, errname); |
---|
[4970] | 371 | |
---|
| 372 | #ifdef SIGKILL |
---|
| 373 | raise(SIGKILL); |
---|
| 374 | #endif |
---|
| 375 | abort(); |
---|
[5619] | 376 | #ifdef ROAR_HAVE_U_EXIT |
---|
| 377 | ROAR_U_EXIT(1); |
---|
| 378 | #endif |
---|
[4970] | 379 | |
---|
| 380 | while(1); |
---|
| 381 | } |
---|
| 382 | |
---|
[4976] | 383 | const char * roar_version_string(void) { |
---|
| 384 | return ROAR_VERSION_STRING; |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | uint32_t roar_version_num(void) { |
---|
| 388 | return _ROAR_MKVERSION(ROAR_VERSION_MAJOR, ROAR_VERSION_MINOR, ROAR_VERSION_REV); |
---|
| 389 | } |
---|
| 390 | |
---|
[0] | 391 | //ll |
---|