Changeset 773
- Timestamp:
- 06/22/07 03:53:23 (1 year ago)
- Files:
-
- cherokee/trunk/ChangeLog (modified) (1 diff)
- cherokee/trunk/cherokee/connection_info.c (modified) (3 diffs)
- cherokee/trunk/cherokee/thread.c (modified) (1 diff)
- cherokee/trunk/cherokee/util.c (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cherokee/trunk/ChangeLog
r772 r773 1 2007-05-21 A.D.F <adefacc@tin.it> 2 3 * cherokee/connection_info.c: 4 - fix, added MUTEX_UNLOCK where required; 5 - space cleanups; 6 7 * cherokee/thread: 8 - space cleanup; 9 10 * cherokee/util.c: 11 - fix, test return value of gmtime(), getpwnam(), etc. 12 calls because they may fail and we don't want 13 to crash the server because of a NULL pointer 14 dereference; 15 - improvement, if thread safe functions (i.e. gmtime_r()) 16 are available then always use them 17 because they are a bit faster and avoid code duplication 18 for #ifndef HAVE_PTHREAD case; 19 - fix, added MUTEX_UNLOCK where required; 20 - space cleanups. 21 1 22 2007-06-19 Alvaro Lopez Ortega <alvaro@alobbs.com> 2 23 cherokee/trunk/cherokee/connection_info.c
r625 r773 224 224 cherokee_connection_info_list_thread (cherokee_list_t *list, void *_thread, cherokee_handler_t *self_handler) 225 225 { 226 ret_t ret; 226 227 cherokee_list_t *i; 227 228 cherokee_boolean_t locked = false; … … 238 239 */ 239 240 if ((self_handler != NULL) && 240 (HANDLER_THREAD(self_handler) != thread)) 241 { 241 (HANDLER_THREAD(self_handler) != thread)) { 242 242 /* Adquire the ownership of the thread 243 243 */ … … 254 254 cherokee_list_add (LIST(n), list); 255 255 } 256 ret = ret_ok; 257 if (cherokee_list_empty (list)) 258 ret = ret_not_found; 259 260 /* Release it 261 */ 262 if (locked) 263 CHEROKEE_MUTEX_UNLOCK (&thread->ownership); 264 265 return ret; 266 } 267 268 269 ret_t 270 cherokee_connection_info_list_server (cherokee_list_t *list, cherokee_server_t *server, cherokee_handler_t *self) 271 { 272 cherokee_list_t *i; 273 274 cherokee_connection_info_list_thread (list, server->main_thread, self); 275 276 list_for_each (i, &server->thread_list) { 277 cherokee_connection_info_list_thread (list, i, self); 278 } 256 279 257 280 if (cherokee_list_empty (list)) 258 281 return ret_not_found; 259 282 260 /* Release it 261 */ 262 if (locked) 263 CHEROKEE_MUTEX_UNLOCK (&thread->ownership); 264 265 return ret_ok; 266 } 267 268 269 ret_t 270 cherokee_connection_info_list_server (cherokee_list_t *list, cherokee_server_t *server, cherokee_handler_t *self) 271 { 272 cherokee_list_t *i; 273 274 cherokee_connection_info_list_thread (list, server->main_thread, self); 275 276 list_for_each (i, &server->thread_list) { 277 cherokee_connection_info_list_thread (list, i, self); 278 } 279 280 if (cherokee_list_empty (list)) 281 return ret_not_found; 282 283 return ret_ok; 284 } 283 return ret_ok; 284 } cherokee/trunk/cherokee/thread.c
r768 r773 968 968 case ret_ok: 969 969 if (cherokee_buffer_is_empty (&conn->local_directory)) { 970 if (is_userdir) 970 if (is_userdir) 971 971 ret = cherokee_connection_build_local_directory_userdir (conn, CONN_VSRV(conn), &entry); 972 972 else cherokee/trunk/cherokee/util.c
r700 r773 357 357 cherokee_gmtime (const time_t *timep, struct tm *result) 358 358 { 359 #ifndef HAVE_PTHREAD 359 #ifdef HAVE_GMTIME_R 360 /* Use the thread safe version anyway (no data copy). 361 */ 362 return gmtime_r (timep, result); 363 #else 360 364 struct tm *tmp; 361 365 362 tmp = gmtime (timep);363 memcpy (result, tmp, sizeof(struct tm));364 return result;365 #else366 # ifdef HAVE_GMTIME_R367 return gmtime_r (timep, result);368 # else369 struct tm *tmp;370 371 366 CHEROKEE_MUTEX_LOCK (&gmtime_mutex); 372 tmp = gmtime (timep);373 memcpy (result, tmp, sizeof(struct tm));367 if (likely ((tmp = gmtime (timep)) != NULL)) 368 memcpy (result, tmp, sizeof(struct tm)); 374 369 CHEROKEE_MUTEX_UNLOCK (&gmtime_mutex); 375 370 376 return result; 377 # endif 371 return (tmp == NULL ? NULL : result); 378 372 #endif 379 373 } … … 387 381 cherokee_localtime (const time_t *timep, struct tm *result) 388 382 { 389 #ifndef HAVE_PTHREAD 383 #ifdef HAVE_LOCALTIME_R 384 /* Use the thread safe version anyway (no data copy). 385 */ 386 return localtime_r (timep, result); 387 #else 390 388 struct tm *tmp; 391 389 392 tmp = localtime (timep);393 memcpy (result, tmp, sizeof(struct tm));394 return result;395 #else396 # ifdef HAVE_LOCALTIME_R397 return localtime_r (timep, result);398 # else399 struct tm *tmp;400 401 390 CHEROKEE_MUTEX_LOCK (&localtime_mutex); 402 tmp = localtime (timep);403 memcpy (result, tmp, sizeof(struct tm));391 if (likely ((tmp = localtime (timep)) != NULL)) 392 memcpy (result, tmp, sizeof(struct tm)); 404 393 CHEROKEE_MUTEX_UNLOCK (&localtime_mutex); 405 394 406 return result; 407 # endif 395 return (tmp == NULL ? NULL : result); 408 396 #endif 409 397 } … … 449 437 return readdir_r (dirstream, entry, result); 450 438 # else 451 struct dirent *ptr;452 int ret = 0;439 struct dirent *ptr; 440 int ret = 0; 453 441 454 442 CHEROKEE_MUTEX_LOCK (&readdir_mutex); 455 443 456 errno = 0;457 ptr = readdir(dirstream);444 errno = 0; 445 ptr = readdir(dirstream); 458 446 459 if (!ptr && errno != 0)460 ret = errno;461 462 if (ptr)463 memcpy(entry, ptr, sizeof(*ptr));464 465 *result = ptr;447 if (!ptr && errno != 0) 448 ret = errno; 449 450 if (ptr) 451 memcpy(entry, ptr, sizeof(*ptr)); 452 453 *result = ptr; 466 454 467 455 CHEROKEE_MUTEX_UNLOCK (&readdir_mutex); 468 return ret; 456 457 return ret; 458 469 459 # endif 470 460 #endif … … 665 655 #if !defined(HAVE_PTHREAD) || (defined(HAVE_PTHREAD) && !defined(HAVE_GETHOSTBYNAME_R)) 666 656 667 struct hostent *host;668 669 CHEROKEE_MUTEX_LOCK (&__global_gethostbyname_mutex);670 /* Resolv the host name671 */672 host = gethostbyname (hostname);673 if (host == NULL) {674 CHEROKEE_MUTEX_UNLOCK (&__global_gethostbyname_mutex);675 return ret_error;676 }677 678 /* Copy the address679 */680 memcpy (addr, host->h_addr, host->h_length);681 CHEROKEE_MUTEX_UNLOCK (&__global_gethostbyname_mutex);682 return ret_ok;657 struct hostent *host; 658 659 CHEROKEE_MUTEX_LOCK (&__global_gethostbyname_mutex); 660 /* Resolv the host name 661 */ 662 host = gethostbyname (hostname); 663 if (host == NULL) { 664 CHEROKEE_MUTEX_UNLOCK (&__global_gethostbyname_mutex); 665 return ret_error; 666 } 667 668 /* Copy the address 669 */ 670 memcpy (addr, host->h_addr, host->h_length); 671 CHEROKEE_MUTEX_UNLOCK (&__global_gethostbyname_mutex); 672 return ret_ok; 683 673 684 674 #elif defined(HAVE_PTHREAD) && defined(HAVE_GETHOSTBYNAME_R) … … 689 679 # define GETHOSTBYNAME_R_BUF_LEN 512 690 680 691 int r;692 int h_errnop;693 struct hostent hs;694 struct hostent *hp = NULL;695 char tmp[GETHOSTBYNAME_R_BUF_LEN];681 int r; 682 int h_errnop; 683 struct hostent hs; 684 struct hostent *hp = NULL; 685 char tmp[GETHOSTBYNAME_R_BUF_LEN]; 696 686 697 687 698 688 # ifdef SOLARIS 699 /* Solaris 10: 700 * struct hostent *gethostbyname_r 701 * (const char *, struct hostent *, char *, int, int *h_errnop); 702 */ 703 hp = gethostbyname_r (hostname, &hs, tmp, 704 GETHOSTBYNAME_R_BUF_LEN - 1, &h_errnop); 705 if (hp == NULL) { 706 return ret_error; 707 } 689 /* Solaris 10: 690 * struct hostent *gethostbyname_r 691 * (const char *, struct hostent *, char *, int, int *h_errnop); 692 */ 693 hp = gethostbyname_r (hostname, &hs, tmp, 694 GETHOSTBYNAME_R_BUF_LEN - 1, &h_errnop); 695 696 if (hp == NULL) 697 return ret_error; 698 708 699 # else 709 /* Linux glibc2: 710 * int gethostbyname_r (const char *name, 711 * struct hostent *ret, char *buf, size_t buflen, 712 * struct hostent **result, int *h_errnop); 713 */ 714 r = gethostbyname_r (hostname, 715 &hs, tmp, GETHOSTBYNAME_R_BUF_LEN - 1, 716 &hp, &h_errnop); 717 if (r != 0) { 718 return ret_error; 719 } 700 /* Linux glibc2: 701 * int gethostbyname_r (const char *name, 702 * struct hostent *ret, char *buf, size_t buflen, 703 * struct hostent **result, int *h_errnop); 704 */ 705 r = gethostbyname_r (hostname, 706 &hs, tmp, GETHOSTBYNAME_R_BUF_LEN - 1, 707 &hp, &h_errnop); 708 if (r != 0) 709 return ret_error; 720 710 # endif 721 722 /* Copy the address 723 */ 711 /* Copy the address 712 */ 724 713 if (hp == NULL) 725 714 return ret_not_found; 726 715 727 memcpy (addr, hp->h_addr, hp->h_length);728 729 return ret_ok;716 memcpy (addr, hp->h_addr, hp->h_length); 717 718 return ret_ok; 730 719 #else 731 732 SHOULDNT_HAPPEN; 733 return ret_error; 720 /* Bad case ! 721 */ 722 SHOULDNT_HAPPEN; 723 return ret_error; 734 724 #endif 735 725 } … … 833 823 */ 834 824 union { 835 long l;836 char c[sizeof(long)];837 } u;838 839 u.l = 1;840 return (u.c[sizeof(long) - 1] == 1);825 long l; 826 char c[sizeof(long)]; 827 } u; 828 829 u.l = 1; 830 return (u.c[sizeof(long) - 1] == 1); 841 831 } 842 832 … … 1053 1043 string = qstring->buf; 1054 1044 1055 while ((token = (char *) strsep(&string, "&")) != NULL) 1056 { 1045 while ((token = (char *) strsep(&string, "&")) != NULL) { 1057 1046 char *equ, *key, *val; 1058 1047 1059 1048 if (token == NULL) continue; 1060 1049 1061 if ((equ = strchr(token, '='))) 1062 { 1050 if ((equ = strchr(token, '='))) { 1063 1051 *equ = '\0'; 1064 1052 … … 1103 1091 struct passwd *tmp; 1104 1092 1105 CHEROKEE_MUTEX_LOCK (&__global_getpwnam_mutex);1093 CHEROKEE_MUTEX_LOCK (&__global_getpwnam_mutex); 1106 1094 1107 1095 tmp = getpwnam (name); 1108 if (tmp == NULL) 1096 if (tmp == NULL) { 1097 CHEROKEE_MUTEX_UNLOCK (&__global_getpwnam_mutex); 1109 1098 return ret_error; 1110 1099 } 1111 1100 if (tmp->pw_name) pw_name_len = strlen(tmp->pw_name); 1112 1101 if (tmp->pw_passwd) pw_passwd_len = strlen(tmp->pw_passwd); … … 1116 1105 1117 1106 if ((pw_name_len + pw_passwd_len + 1118 pw_gecos_len + pw_dir_len + pw_shell_len) > buflen) 1107 pw_gecos_len + pw_dir_len + pw_shell_len + 5) >= buflen) { 1108 /* Buffer overflow. 1109 */ 1110 CHEROKEE_MUTEX_UNLOCK (&__global_getpwnam_mutex); 1119 1111 return ret_error; 1120 1112 } 1121 1113 memset (buf, 0, buflen); 1122 1114 ptr = buf; … … 1155 1147 } 1156 1148 1157 CHEROKEE_MUTEX_UNLOCK (&__global_getpwnam_mutex);1149 CHEROKEE_MUTEX_UNLOCK (&__global_getpwnam_mutex); 1158 1150 return ret_ok; 1159 1151 … … 1199 1191 struct group *tmp; 1200 1192 1201 CHEROKEE_MUTEX_LOCK (&__global_getgrnam_mutex);1193 CHEROKEE_MUTEX_LOCK (&__global_getgrnam_mutex); 1202 1194 1203 1195 tmp = getgrnam (name); 1204 if (tmp == NULL) 1196 if (tmp == NULL) { 1197 CHEROKEE_MUTEX_UNLOCK (&__global_getgrnam_mutex); 1205 1198 return ret_error; 1206 1207 if (tmp->gr_name) gr_name_len = strlen(tmp->gr_name); 1208 if (tmp->gr_passwd) gr_passwd_len = strlen(tmp->gr_passwd); 1209 1210 if ((gr_name_len + gr_passwd_len) > buflen) 1199 } 1200 1201 if (tmp->gr_name) 1202 gr_name_len = strlen(tmp->gr_name); 1203 if (tmp->gr_passwd) 1204 gr_passwd_len = strlen(tmp->gr_passwd); 1205 1206 if ((gr_name_len + gr_passwd_len + 2) >= buflen) { 1207 CHEROKEE_MUTEX_UNLOCK (&__global_getgrnam_mutex); 1211 1208 return ret_error; 1212 1209 } 1213 1210 memset (buf, 0, buflen); 1214 1211 ptr = buf; … … 1231 1228 */ 1232 1229 1233 CHEROKEE_MUTEX_UNLOCK (&__global_getgrnam_mutex); 1230 CHEROKEE_MUTEX_UNLOCK (&__global_getgrnam_mutex); 1231 1234 1232 return ret_ok; 1235 1233 … … 1239 1237 1240 1238 re = getgrnam_r (name, grbuf, buf, buflen, &tmp); 1241 if (re != 0) return ret_error; 1239 if (re != 0) 1240 return ret_error; 1242 1241 1243 1242 return ret_ok; … … 1249 1248 int re; 1250 1249 re = getgrnam_r (name, grbuf, buf, buflen, &result); 1251 if (re != 0) return ret_error; 1250 if (re != 0) 1251 return ret_error; 1252 1252 #else 1253 1253 result = getgrnam_r (name, grbuf, buf, buflen); 1254 if (result == NULL) return ret_error; 1254 if (result == NULL) 1255 return ret_error; 1255 1256 #endif 1256 1257