#include "asterisk.h"
#include <ctype.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include "asterisk/utils.h"

Go to the source code of this file.
Defines | |
| #define | LONG_MAX 9223372036854775807L |
| #define | LONG_MIN (-9223372036854775807L-1L) |
| #define | MKTEMP_DIR 2 |
| #define | MKTEMP_FILE 1 |
| #define | MKTEMP_NAME 0 |
| #define | NUM_CHARS (sizeof(TEMPCHARS) - 1) |
| #define | TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_." |
Functions | |
| int | asprintf (char **str, const char *fmt,...) |
| void | closefrom (int n) |
| int | ffsll (long long n) |
| int | getloadavg (double *list, int nelem) |
| Return something that won't cancel the call, but still return -1, in case we correct the implementation to check return value. | |
| uint64_t | htonll (uint64_t host64) |
| char * | mkdtemp (char *path) |
| static int | mktemp_internal (char *path, int slen, int mode) |
| uint64_t | ntohll (uint64_t net64) |
| int | setenv (const char *name, const char *value, int overwrite) |
| char * | strcasestr (const char *haystack, const char *needle) |
| char * | strndup (const char *s, size_t n) |
| size_t | strnlen (const char *s, size_t n) |
| char * | strsep (char **str, const char *delims) |
| uint64_t | strtoq (const char *nptr, char **endptr, int base) |
| Convert a string to a quad integer. | |
| void | timersub (struct timeval *tvend, struct timeval *tvstart, struct timeval *tvdiff) |
| int | unsetenv (const char *name) |
| static char * | upper (const char *orig, char *buf, int bufsize) |
| int | vasprintf (char **strp, const char *fmt, va_list ap) |
Definition in file strcompat.c.
| #define LONG_MAX 9223372036854775807L |
| #define LONG_MIN (-9223372036854775807L-1L) |
| #define MKTEMP_DIR 2 |
| #define MKTEMP_FILE 1 |
| #define MKTEMP_NAME 0 |
| #define NUM_CHARS (sizeof(TEMPCHARS) - 1) |
| #define TEMPCHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_." |
| int asprintf | ( | char ** | str, | |
| const char * | fmt, | |||
| ... | ||||
| ) |
| void closefrom | ( | int | n | ) |
Definition at line 424 of file strcompat.c.
References errno.
Referenced by ast_close_fds_above_n().
00425 { 00426 long x; 00427 struct rlimit rl; 00428 DIR *dir; 00429 char path[16], *result; 00430 struct dirent *entry; 00431 00432 snprintf(path, sizeof(path), "/proc/%d/fd", (int) getpid()); 00433 if ((dir = opendir(path))) { 00434 while ((entry = readdir(dir))) { 00435 /* Skip . and .. */ 00436 if (entry->d_name[0] == '.') { 00437 continue; 00438 } 00439 if ((x = strtol(entry->d_name, &result, 10)) && x >= n) { 00440 #ifdef STRICT_COMPAT 00441 close(x); 00442 #else 00443 /* This isn't strictly compatible, but it's actually faster 00444 * for our purposes to set the CLOEXEC flag than to close 00445 * file descriptors. 00446 */ 00447 long flags = fcntl(x, F_GETFD); 00448 if (flags == -1 && errno == EBADF) { 00449 continue; 00450 } 00451 fcntl(x, F_SETFD, flags | FD_CLOEXEC); 00452 #endif 00453 } 00454 } 00455 closedir(dir); 00456 } else { 00457 getrlimit(RLIMIT_NOFILE, &rl); 00458 if (rl.rlim_cur > 65535) { 00459 /* A more reasonable value. Consider that the primary source of 00460 * file descriptors in Asterisk are UDP sockets, of which we are 00461 * limited to 65,535 per address. We additionally limit that down 00462 * to about 10,000 sockets per protocol. While the kernel will 00463 * allow us to set the fileno limit higher (up to 4.2 billion), 00464 * there really is no practical reason for it to be that high. 00465 */ 00466 rl.rlim_cur = 65535; 00467 } 00468 for (x = n; x < rl.rlim_cur; x++) { 00469 #ifdef STRICT_COMPAT 00470 close(x); 00471 #else 00472 long flags = fcntl(x, F_GETFD); 00473 if (flags == -1 && errno == EBADF) { 00474 continue; 00475 } 00476 fcntl(x, F_SETFD, flags | FD_CLOEXEC); 00477 #endif 00478 } 00479 } 00480 }
| int ffsll | ( | long long | n | ) |
Definition at line 411 of file strcompat.c.
00412 { 00413 int i; 00414 for (i = 0; i < 64; i++) { 00415 if ((1LL << i) & n) { 00416 return i + 1; 00417 } 00418 } 00419 return 0; 00420 }
| int getloadavg | ( | double * | list, | |
| int | nelem | |||
| ) |
Return something that won't cancel the call, but still return -1, in case we correct the implementation to check return value.
Definition at line 346 of file strcompat.c.
Referenced by ast_readconfig(), cli_prompt(), increase_call_count(), and sysinfo_helper().
00347 { 00348 int i; 00349 00350 for (i = 0; i < nelem; i++) { 00351 list[i] = 0.1; 00352 } 00353 return -1; 00354 }
| uint64_t htonll | ( | uint64_t | host64 | ) |
Definition at line 385 of file strcompat.c.
Referenced by iax_ie_append_versioned_uint64().
00386 { 00387 #if BYTE_ORDER == BIG_ENDIAN 00388 return host64; 00389 #elif BYTE_ORDER == LITTLE_ENDIAN 00390 union { 00391 unsigned char c[8]; 00392 uint64_t u; 00393 } number; 00394 number.u = host64; 00395 return 00396 (((uint64_t) number.c[0]) << 56) | 00397 (((uint64_t) number.c[1]) << 48) | 00398 (((uint64_t) number.c[2]) << 40) | 00399 (((uint64_t) number.c[3]) << 32) | 00400 (((uint64_t) number.c[4]) << 24) | 00401 (((uint64_t) number.c[5]) << 16) | 00402 (((uint64_t) number.c[6]) << 8) | 00403 (((uint64_t) number.c[7]) << 0); 00404 #else 00405 #error "Unknown byte order" 00406 #endif 00407 }
| char* mkdtemp | ( | char * | path | ) |
Definition at line 566 of file strcompat.c.
References MKTEMP_DIR, and mktemp_internal().
00567 { 00568 return mktemp_internal(path, 0, MKTEMP_DIR) ? NULL : path; 00569 }
| static int mktemp_internal | ( | char * | path, | |
| int | slen, | |||
| int | mode | |||
| ) | [static] |
Definition at line 509 of file strcompat.c.
References ast_random(), errno, len(), MKTEMP_DIR, MKTEMP_FILE, MKTEMP_NAME, NUM_CHARS, and TEMPCHARS.
Referenced by mkdtemp().
00510 { 00511 char *start, *cp, *ep; 00512 const char *tempchars = TEMPCHARS; 00513 unsigned int r, tries; 00514 struct stat sb; 00515 size_t len; 00516 int fd; 00517 00518 len = strlen(path); 00519 if (len == 0 || slen >= len) { 00520 errno = EINVAL; 00521 return(-1); 00522 } 00523 ep = path + len - slen; 00524 00525 tries = 1; 00526 for (start = ep; start > path && start[-1] == 'X'; start--) { 00527 if (tries < INT_MAX / NUM_CHARS) { 00528 tries *= NUM_CHARS; 00529 } 00530 } 00531 tries *= 2; 00532 00533 do { 00534 for (cp = start; cp != ep; cp++) { 00535 r = ast_random() % NUM_CHARS; 00536 *cp = tempchars[r]; 00537 } 00538 00539 switch (mode) { 00540 case MKTEMP_NAME: 00541 if (lstat(path, &sb) != 0) { 00542 return (errno == ENOENT ? 0 : -1); 00543 } 00544 break; 00545 case MKTEMP_FILE: 00546 fd = open(path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR); 00547 if (fd != -1 || errno != EEXIST) { 00548 return (fd); 00549 } 00550 break; 00551 case MKTEMP_DIR: 00552 if (mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR) == 0) { 00553 return (0); 00554 } 00555 if (errno != EEXIST) { 00556 return (-1); 00557 } 00558 break; 00559 } 00560 } while (--tries); 00561 00562 errno = EEXIST; 00563 return(-1); 00564 }
| uint64_t ntohll | ( | uint64_t | net64 | ) |
Definition at line 359 of file strcompat.c.
Referenced by dump_versioned_codec(), and iax_parse_ies().
00360 { 00361 #if BYTE_ORDER == BIG_ENDIAN 00362 return net64; 00363 #elif BYTE_ORDER == LITTLE_ENDIAN 00364 union { 00365 unsigned char c[8]; 00366 uint64_t u; 00367 } number; 00368 number.u = net64; 00369 return 00370 (((uint64_t) number.c[0]) << 56) | 00371 (((uint64_t) number.c[1]) << 48) | 00372 (((uint64_t) number.c[2]) << 40) | 00373 (((uint64_t) number.c[3]) << 32) | 00374 (((uint64_t) number.c[4]) << 24) | 00375 (((uint64_t) number.c[5]) << 16) | 00376 (((uint64_t) number.c[6]) << 8) | 00377 (((uint64_t) number.c[7]) << 0); 00378 #else 00379 #error "Unknown byte order" 00380 #endif 00381 }
| int setenv | ( | const char * | name, | |
| const char * | value, | |||
| int | overwrite | |||
| ) |
Definition at line 62 of file strcompat.c.
Referenced by env_init(), env_write(), launch_script(), load_odbc_config(), read_environment(), and unsetenv().
00063 { 00064 unsigned char *buf; 00065 int buflen; 00066 00067 buflen = strlen(name) + strlen(value) + 2; 00068 buf = alloca(buflen); 00069 00070 if (!overwrite && getenv(name)) 00071 return 0; 00072 00073 snprintf(buf, buflen, "%s=%s", name, value); 00074 00075 return putenv(buf); 00076 }
| char* strcasestr | ( | const char * | haystack, | |
| const char * | needle | |||
| ) |
Definition at line 101 of file strcompat.c.
References upper().
Referenced by action_originate(), anti_injection(), common_exec(), find_sdp(), find_table_cb(), frame_trace_helper(), get_rdnis(), get_refer_info(), gettag(), handle_request_invite(), handle_response_register(), handle_show_applications(), modlist_modentry(), parse_moved_contact(), parse_register_contact(), playback_exec(), process_dahdi(), realtime_multi_odbc(), realtime_odbc(), register_verify(), reqprep(), respprep(), search_directory_sub(), sip_sipredirect(), sip_uri_headers_cmp(), and word_match().
00102 { 00103 char *u1, *u2; 00104 int u1len = strlen(haystack) + 1, u2len = strlen(needle) + 1; 00105 00106 u1 = alloca(u1len); 00107 u2 = alloca(u2len); 00108 if (u1 && u2) { 00109 char *offset; 00110 if (u2len > u1len) { 00111 /* Needle bigger than haystack */ 00112 return NULL; 00113 } 00114 offset = strstr(upper(haystack, u1, u1len), upper(needle, u2, u2len)); 00115 if (offset) { 00116 /* Return the offset into the original string */ 00117 return ((char *)((unsigned long)haystack + (unsigned long)(offset - u1))); 00118 } else { 00119 return NULL; 00120 } 00121 } else { 00122 return NULL; 00123 } 00124 }
| char* strndup | ( | const char * | s, | |
| size_t | n | |||
| ) |
| size_t strnlen | ( | const char * | s, | |
| size_t | n | |||
| ) |
Definition at line 128 of file strcompat.c.
References len().
Referenced by strndup().
00129 { 00130 size_t len; 00131 00132 for (len = 0; len < n; len++) 00133 if (s[len] == '\0') 00134 break; 00135 00136 return len; 00137 }
| char* strsep | ( | char ** | str, | |
| const char * | delims | |||
| ) |
Definition at line 35 of file strcompat.c.
Referenced by __analog_ss_thread(), __ast_play_and_record(), _ast_device_state(), _build_port_config(), _macro_exec(), _parse(), acf_curl_helper(), acf_vm_info(), acf_vmcount_exec(), action_playback(), action_playback_and_continue(), actual_load_config(), add_hintdevice(), add_peer_mailboxes(), add_redirect(), adsi_load(), adsi_message(), aji_handle_pubsub_event(), analog_ss_thread(), append_history_va(), append_mailbox(), append_mailbox_mapping(), apply_options(), apply_outgoing(), ast_aji_get_client(), ast_app_getdata(), ast_append_ha(), ast_bridge_timelimit(), ast_build_timing(), ast_eivr_getvariable(), ast_eivr_setvariable(), ast_el_strtoarr(), ast_extension_state3(), ast_format_str_reduce(), ast_get_group(), ast_http_get_post_vars(), ast_parse_allow_disallow(), ast_parse_arg(), ast_parse_digest(), ast_playtones_start(), ast_read_image(), ast_remotecontrol(), ast_utils_which(), auth_exec(), authenticate_verify(), build_calendar(), build_channels(), build_gateway(), build_peer(), build_route(), builtin_atxfer(), check_blacklist(), check_user_full(), cleanup_stale_contexts(), complete_dialplan_add_ignorepat(), complete_dialplan_add_include(), complete_dialplan_remove_ignorepat(), complete_dialplan_remove_include(), complete_meetmecmd(), conf_exec(), conf_run(), config_curl(), config_line(), config_parse_variables(), console_dial(), create_queue_member(), cut_internal(), data_filter_alloc(), decrypt_frame(), del_exec(), deltree_exec(), dial_exec_full(), dial_trunk(), do_say(), ewscal_write_event(), exec_exec(), exts_compare(), extstate_read(), feature_interpret_helper(), filehelper(), find_gtalk(), forward_message(), function_fieldnum_helper(), function_fieldqty_helper(), function_sippeer(), get_range(), get_rdnis(), get_timerange(), gettag(), gosub_exec(), gtalk_alloc(), gtalk_request(), handle_cli_dialplan_add_extension(), handle_common_options(), handle_debug_dialplan(), handle_request_invite(), handle_request_notify(), handle_show_dialplan(), handle_t38_options(), handle_uri(), has_voicemail(), hint_read(), httpd_helper_thread(), iax2_register(), iftime(), inboxcount2(), is_prefix(), ivr_dispatch(), jingle_request(), leave_voicemail(), load_channelvars(), load_column_config(), make_components(), man_do_variable_value(), mark_parsed_methods(), metermaidstate(), misdn_set_opt_exec(), mkintf(), msg_send_cb(), msg_send_exec(), next_node_name(), notify_message(), notify_new_message(), orig_app(), orig_exten(), originate_exec(), page_exec(), parkandannounce_exec(), parse_apps(), parse_cookies(), parse_dial_string(), parse_empty_options(), parse_events(), parse_register_contact(), parse_session_expires(), parse_uri_full(), parse_via(), pbx_builtin_background(), pbx_builtin_execiftime(), pbx_builtin_gotoif(), pbx_builtin_gotoiftime(), pbx_builtin_importvar(), pbx_builtin_saynumber(), pbx_builtin_setvar(), pbx_find_extension(), pbx_load_config(), pbx_load_users(), pbx_parseable_goto(), pickup_exec(), pickupchan_exec(), playback_exec(), process_applicationmap_line(), process_dahdi(), process_sdp_o(), process_text_line(), queue_mwi_event(), queue_set_param(), read_config_maps(), readfile_exec(), realtime_curl(), realtime_multi_curl(), realtime_multi_odbc(), realtime_multi_pgsql(), realtime_odbc(), realtime_pgsql(), register_exten(), register_peer_exten(), register_verify(), reload_config(), reload_queue_members(), reply_digest(), sdp_crypto_process(), search_directory_sub(), send_dial_tone(), sendfax_exec(), sendmail(), set(), set_config_flags(), set_insecure_flags(), sip_digest_parser(), sip_get_cc_information(), sip_parse_nat_option(), sip_sipredirect(), sip_uri_cmp(), sip_uri_headers_cmp(), sip_uri_params_cmp(), skinny_call(), sla_add_trunk_to_station(), sla_check_device(), sla_queue_event_conf(), sla_ring_station(), sla_state(), sla_station_exec(), sort_internal(), spawn_mp3(), spawn_ras(), speech_background(), stat_read(), state_notify_build_xml(), store_tone_zone_ring_cadence(), timezone_add(), transmit_fake_auth_response(), tryexec_exec(), unistim_send_mwi_to_peer(), unregister_exten(), update_registry(), vmauthenticate(), write_htmldump(), and xml_translate().
00036 { 00037 char *token; 00038 00039 if (!*str) { 00040 /* No more tokens */ 00041 return NULL; 00042 } 00043 00044 token = *str; 00045 while (**str != '\0') { 00046 if (strchr(delims, **str)) { 00047 **str = '\0'; 00048 (*str)++; 00049 return token; 00050 } 00051 (*str)++; 00052 } 00053 00054 /* There is no other token */ 00055 *str = NULL; 00056 00057 return token; 00058 }
| uint64_t strtoq | ( | const char * | nptr, | |
| char ** | endptr, | |||
| int | base | |||
| ) |
Convert a string to a quad integer.
Definition at line 237 of file strcompat.c.
References LONG_MAX, and LONG_MIN.
00238 { 00239 const char *s; 00240 uint64_t acc; 00241 unsigned char c; 00242 uint64_t qbase, cutoff; 00243 int neg, any, cutlim; 00244 00245 /* 00246 * Skip white space and pick up leading +/- sign if any. 00247 * If base is 0, allow 0x for hex and 0 for octal, else 00248 * assume decimal; if base is already 16, allow 0x. 00249 */ 00250 s = nptr; 00251 do { 00252 c = *s++; 00253 } while (isspace(c)); 00254 if (c == '-') { 00255 neg = 1; 00256 c = *s++; 00257 } else { 00258 neg = 0; 00259 if (c == '+') 00260 c = *s++; 00261 } 00262 if ((base == 0 || base == 16) && 00263 c == '\0' && (*s == 'x' || *s == 'X')) { 00264 c = s[1]; 00265 s += 2; 00266 base = 16; 00267 } 00268 if (base == 0) 00269 base = c == '\0' ? 8 : 10; 00270 00271 /* 00272 * Compute the cutoff value between legal numbers and illegal 00273 * numbers. That is the largest legal value, divided by the 00274 * base. An input number that is greater than this value, if 00275 * followed by a legal input character, is too big. One that 00276 * is equal to this value may be valid or not; the limit 00277 * between valid and invalid numbers is then based on the last 00278 * digit. For instance, if the range for quads is 00279 * [-9223372036854775808..9223372036854775807] and the input base 00280 * is 10, cutoff will be set to 922337203685477580 and cutlim to 00281 * either 7 (neg==0) or 8 (neg==1), meaning that if we have 00282 * accumulated a value > 922337203685477580, or equal but the 00283 * next digit is > 7 (or 8), the number is too big, and we will 00284 * return a range error. 00285 * 00286 * Set any if any `digits' consumed; make it negative to indicate 00287 * overflow. 00288 */ 00289 qbase = (unsigned)base; 00290 cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX; 00291 cutlim = cutoff % qbase; 00292 cutoff /= qbase; 00293 for (acc = 0, any = 0;; c = *s++) { 00294 if (!isascii(c)) 00295 break; 00296 if (isdigit(c)) 00297 c -= '\0'; 00298 else if (isalpha(c)) 00299 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 00300 else 00301 break; 00302 if (c >= base) 00303 break; 00304 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) 00305 any = -1; 00306 else { 00307 any = 1; 00308 acc *= qbase; 00309 acc += c; 00310 } 00311 } 00312 if (any < 0) { 00313 acc = neg ? LONG_MIN : LONG_MAX; 00314 } else if (neg) 00315 acc = -acc; 00316 if (endptr != 0) 00317 *((const char **)endptr) = any ? s - 1 : nptr; 00318 return acc; 00319 }
| void timersub | ( | struct timeval * | tvend, | |
| struct timeval * | tvstart, | |||
| struct timeval * | tvdiff | |||
| ) |
Definition at line 175 of file strcompat.c.
Referenced by ast_rtcp_write_rr(), ast_rtcp_write_sr(), and ast_select().
00176 { 00177 tvdiff->tv_sec = tvend->tv_sec - tvstart->tv_sec; 00178 tvdiff->tv_usec = tvend->tv_usec - tvstart->tv_usec; 00179 if (tvdiff->tv_usec < 0) { 00180 tvdiff->tv_sec --; 00181 tvdiff->tv_usec += 1000000; 00182 } 00183 00184 }
| int unsetenv | ( | const char * | name | ) |
| static char* upper | ( | const char * | orig, | |
| char * | buf, | |||
| int | bufsize | |||
| ) | [static] |
Definition at line 87 of file strcompat.c.
Referenced by strcasestr().
00088 { 00089 int i = 0; 00090 00091 while (i < (bufsize - 1) && orig[i]) { 00092 buf[i] = toupper(orig[i]); 00093 i++; 00094 } 00095 00096 buf[i] = '\0'; 00097 00098 return buf; 00099 }
| int vasprintf | ( | char ** | strp, | |
| const char * | fmt, | |||
| va_list | ap | |||
| ) |
Definition at line 155 of file strcompat.c.
References malloc.
00156 { 00157 int size; 00158 va_list ap2; 00159 char s; 00160 00161 *strp = NULL; 00162 va_copy(ap2, ap); 00163 size = vsnprintf(&s, 1, fmt, ap2); 00164 va_end(ap2); 00165 *strp = malloc(size + 1); 00166 if (!*strp) 00167 return -1; 00168 vsnprintf(*strp, size + 1, fmt, ap); 00169 00170 return size; 00171 }
1.5.6