Changeset 740 for vendor/current/lib/util/util.c
- Timestamp:
- Nov 14, 2012, 12:59:34 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/current/lib/util/util.c
r414 r740 26 26 #include "system/filesys.h" 27 27 #include "system/locale.h" 28 #include "system/shmem.h" 29 28 30 #undef malloc 29 31 #undef strcasecmp … … 31 33 #undef strdup 32 34 #undef realloc 35 36 #if defined(UID_WRAPPER) 37 #if !defined(UID_WRAPPER_REPLACE) && !defined(UID_WRAPPER_NOT_REPLACE) 38 #define UID_WRAPPER_REPLACE 39 #include "../uid_wrapper/uid_wrapper.h" 40 #endif 41 #else 42 #define uwrap_enabled() 0 43 #endif 33 44 34 45 /** … … 155 166 **/ 156 167 157 _PUBLIC_ void msleep(unsigned int t) 158 { 159 struct timeval tval; 160 161 tval.tv_sec = t/1000; 162 tval.tv_usec = 1000*(t%1000); 163 /* this should be the real select - do NOT replace 164 with sys_select() */ 165 select(0,NULL,NULL,NULL,&tval); 168 _PUBLIC_ void smb_msleep(unsigned int t) 169 { 170 #if defined(HAVE_NANOSLEEP) 171 struct timespec ts; 172 int ret; 173 174 ts.tv_sec = t/1000; 175 ts.tv_nsec = 1000000*(t%1000); 176 177 do { 178 errno = 0; 179 ret = nanosleep(&ts, &ts); 180 } while (ret < 0 && errno == EINTR && (ts.tv_sec > 0 || ts.tv_nsec > 0)); 181 #else 182 unsigned int tdiff=0; 183 struct timeval tval,t1,t2; 184 fd_set fds; 185 186 GetTimeOfDay(&t1); 187 t2 = t1; 188 189 while (tdiff < t) { 190 tval.tv_sec = (t-tdiff)/1000; 191 tval.tv_usec = 1000*((t-tdiff)%1000); 192 193 /* Never wait for more than 1 sec. */ 194 if (tval.tv_sec > 1) { 195 tval.tv_sec = 1; 196 tval.tv_usec = 0; 197 } 198 199 FD_ZERO(&fds); 200 errno = 0; 201 select(0,&fds,NULL,NULL,&tval); 202 203 GetTimeOfDay(&t2); 204 if (t2.tv_sec < t1.tv_sec) { 205 /* Someone adjusted time... */ 206 t1 = t2; 207 } 208 209 tdiff = usec_time_diff(&t2,&t1)/1000; 210 } 211 #endif 166 212 } 167 213 … … 255 301 } 256 302 303 static void debugadd_cb(const char *buf, void *private_data) 304 { 305 int *plevel = (int *)private_data; 306 DEBUGADD(*plevel, ("%s", buf)); 307 } 308 309 void print_asc_cb(const uint8_t *buf, int len, 310 void (*cb)(const char *buf, void *private_data), 311 void *private_data) 312 { 313 int i; 314 char s[2]; 315 s[1] = 0; 316 317 for (i=0; i<len; i++) { 318 s[0] = isprint(buf[i]) ? buf[i] : '.'; 319 cb(s, private_data); 320 } 321 } 322 257 323 void print_asc(int level, const uint8_t *buf,int len) 258 324 { 259 int i; 260 for (i=0;i<len;i++) 261 DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.')); 262 } 263 264 /** 265 * Write dump of binary data to the log file. 266 * 267 * The data is only written if the log level is at least level. 325 print_asc_cb(buf, len, debugadd_cb, &level); 326 } 327 328 /** 329 * Write dump of binary data to a callback 268 330 */ 269 static void _dump_data(int level, const uint8_t *buf, int len, 270 bool omit_zero_bytes) 331 void dump_data_cb(const uint8_t *buf, int len, 332 bool omit_zero_bytes, 333 void (*cb)(const char *buf, void *private_data), 334 void *private_data) 271 335 { 272 336 int i=0; 273 337 static const uint8_t empty[16] = { 0, }; 274 338 bool skipped = false; 339 char tmp[16]; 275 340 276 341 if (len<=0) return; 277 278 if (!DEBUGLVL(level)) return;279 342 280 343 for (i=0;i<len;) { … … 291 354 292 355 if (i<len) { 293 DEBUGADD(level,("[%04X] ",i)); 356 snprintf(tmp, sizeof(tmp), "[%04X] ", i); 357 cb(tmp, private_data); 294 358 } 295 359 } 296 360 297 DEBUGADD(level,("%02X ",(int)buf[i])); 361 snprintf(tmp, sizeof(tmp), "%02X ", (int)buf[i]); 362 cb(tmp, private_data); 298 363 i++; 299 if (i%8 == 0) DEBUGADD(level,(" ")); 364 if (i%8 == 0) { 365 cb(" ", private_data); 366 } 300 367 if (i%16 == 0) { 301 368 302 print_asc(level,&buf[i-16],8); DEBUGADD(level,(" ")); 303 print_asc(level,&buf[i-8],8); DEBUGADD(level,("\n")); 369 print_asc_cb(&buf[i-16], 8, cb, private_data); 370 cb(" ", private_data); 371 print_asc_cb(&buf[i-8], 8, cb, private_data); 372 cb("\n", private_data); 304 373 305 374 if ((omit_zero_bytes == true) && … … 307 376 (memcmp(&buf[i], &empty, 16) == 0)) { 308 377 if (!skipped) { 309 DEBUGADD(level,("skipping zero buffer bytes\n")); 378 cb("skipping zero buffer bytes\n", 379 private_data); 310 380 skipped = true; 311 381 } … … 317 387 int n; 318 388 n = 16 - (i%16); 319 DEBUGADD(level,(" ")); 320 if (n>8) DEBUGADD(level,(" ")); 321 while (n--) DEBUGADD(level,(" ")); 389 cb(" ", private_data); 390 if (n>8) { 391 cb(" ", private_data); 392 } 393 while (n--) { 394 cb(" ", private_data); 395 } 322 396 n = MIN(8,i%16); 323 print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " )); 397 print_asc_cb(&buf[i-(i%16)], n, cb, private_data); 398 cb(" ", private_data); 324 399 n = (i%16) - n; 325 if (n>0) print_asc(level,&buf[i-n],n); 326 DEBUGADD(level,("\n")); 400 if (n>0) { 401 print_asc_cb(&buf[i-n], n, cb, private_data); 402 } 403 cb("\n", private_data); 327 404 } 328 405 … … 336 413 _PUBLIC_ void dump_data(int level, const uint8_t *buf, int len) 337 414 { 338 _dump_data(level, buf, len, false); 415 if (!DEBUGLVL(level)) { 416 return; 417 } 418 dump_data_cb(buf, len, false, debugadd_cb, &level); 339 419 } 340 420 … … 343 423 * 344 424 * The data is only written if the log level is at least level. 345 * 16 zero bytes in a row are om mited425 * 16 zero bytes in a row are omitted 346 426 */ 347 427 _PUBLIC_ void dump_data_skip_zeros(int level, const uint8_t *buf, int len) 348 428 { 349 _dump_data(level, buf, len, true); 429 if (!DEBUGLVL(level)) { 430 return; 431 } 432 dump_data_cb(buf, len, true, debugadd_cb, &level); 350 433 } 351 434 … … 580 663 _PUBLIC_ size_t strhex_to_str(char *p, size_t p_len, const char *strhex, size_t strhex_len) 581 664 { 582 size_t i ;665 size_t i = 0; 583 666 size_t num_chars = 0; 584 667 uint8_t lonybble, hinybble; … … 586 669 char *p1 = NULL, *p2 = NULL; 587 670 588 for (i = 0; i < strhex_len && strhex[i] != 0; i++) {589 if (strncasecmp(hexchars, "0x", 2) == 0) {590 i++; /* skip two chars */591 continue;592 } 593 671 /* skip leading 0x prefix */ 672 if (strncasecmp(strhex, "0x", 2) == 0) { 673 i += 2; /* skip two chars */ 674 } 675 676 for (; i < strhex_len && strhex[i] != 0; i++) { 594 677 if (!(p1 = strchr(hexchars, toupper((unsigned char)strhex[i])))) 595 678 break; … … 765 848 bool ltrim) 766 849 { 767 c har *s;768 c har *saved_s;850 const char *s; 851 const char *saved_s; 769 852 char *pbuf; 770 853 bool quoted; … … 776 859 } 777 860 778 s = (char *)*ptr;861 s = *ptr; 779 862 780 863 /* default to simple separators */ … … 854 937 } 855 938 856 939 /** 940 * Get the next token from a string, return False if none found. 941 * Handles double-quotes. 942 * 943 * Based on a routine by GJC@VILLAGE.COM. 944 * Extensively modified by Andrew.Tridgell@anu.edu.au 945 **/ 946 _PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize) 947 { 948 const char *s; 949 bool quoted; 950 size_t len=1; 951 952 if (!ptr) 953 return false; 954 955 s = *ptr; 956 957 /* default to simple separators */ 958 if (!sep) 959 sep = " \t\n\r"; 960 961 /* find the first non sep char */ 962 while (*s && strchr_m(sep,*s)) 963 s++; 964 965 /* nothing left? */ 966 if (!*s) 967 return false; 968 969 /* copy over the token */ 970 for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) { 971 if (*s == '\"') { 972 quoted = !quoted; 973 } else { 974 len++; 975 *buff++ = *s; 976 } 977 } 978 979 *ptr = (*s) ? s+1 : s; 980 *buff = 0; 981 982 return true; 983 } 984 985 struct anonymous_shared_header { 986 union { 987 size_t length; 988 uint8_t pad[16]; 989 } u; 990 }; 991 992 /* Map a shared memory buffer of at least nelem counters. */ 993 void *anonymous_shared_allocate(size_t orig_bufsz) 994 { 995 void *ptr; 996 void *buf; 997 size_t pagesz = getpagesize(); 998 size_t pagecnt; 999 size_t bufsz = orig_bufsz; 1000 struct anonymous_shared_header *hdr; 1001 1002 bufsz += sizeof(*hdr); 1003 1004 /* round up to full pages */ 1005 pagecnt = bufsz / pagesz; 1006 if (bufsz % pagesz) { 1007 pagecnt += 1; 1008 } 1009 bufsz = pagesz * pagecnt; 1010 1011 if (orig_bufsz >= bufsz) { 1012 /* integer wrap */ 1013 errno = ENOMEM; 1014 return NULL; 1015 } 1016 1017 #ifdef MAP_ANON 1018 /* BSD */ 1019 buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, 1020 -1 /* fd */, 0 /* offset */); 1021 #else 1022 buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, 1023 open("/dev/zero", O_RDWR), 0 /* offset */); 1024 #endif 1025 1026 if (buf == MAP_FAILED) { 1027 return NULL; 1028 } 1029 1030 hdr = (struct anonymous_shared_header *)buf; 1031 hdr->u.length = bufsz; 1032 1033 ptr = (void *)(&hdr[1]); 1034 1035 return ptr; 1036 } 1037 1038 void anonymous_shared_free(void *ptr) 1039 { 1040 struct anonymous_shared_header *hdr; 1041 1042 if (ptr == NULL) { 1043 return; 1044 } 1045 1046 hdr = (struct anonymous_shared_header *)ptr; 1047 1048 hdr--; 1049 1050 munmap(hdr, hdr->u.length); 1051 } 1052 1053 #ifdef DEVELOPER 1054 /* used when you want a debugger started at a particular point in the 1055 code. Mostly useful in code that runs as a child process, where 1056 normal gdb attach is harder to organise. 1057 */ 1058 void samba_start_debugger(void) 1059 { 1060 char *cmd = NULL; 1061 if (asprintf(&cmd, "xterm -e \"gdb --pid %u\"&", getpid()) == -1) { 1062 return; 1063 } 1064 if (system(cmd) == -1) { 1065 free(cmd); 1066 return; 1067 } 1068 free(cmd); 1069 sleep(2); 1070 } 1071 #endif
Note:
See TracChangeset
for help on using the changeset viewer.