Changeset 773

Show
Ignore:
Timestamp:
06/22/07 03:53:23 (1 year ago)
Author:
alo
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • cherokee/trunk/ChangeLog

    r772 r773  
     12007-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         
    1222007-06-19  Alvaro Lopez Ortega  <alvaro@alobbs.com> 
    223 
  • cherokee/trunk/cherokee/connection_info.c

    r625 r773  
    224224cherokee_connection_info_list_thread (cherokee_list_t *list, void *_thread, cherokee_handler_t *self_handler) 
    225225{ 
     226        ret_t               ret; 
    226227        cherokee_list_t    *i; 
    227228        cherokee_boolean_t  locked = false; 
     
    238239         */ 
    239240        if ((self_handler != NULL) && 
    240             (HANDLER_THREAD(self_handler) != thread)) 
    241         { 
     241            (HANDLER_THREAD(self_handler) != thread)) { 
    242242                /* Adquire the ownership of the thread 
    243243                 */ 
     
    254254                cherokee_list_add (LIST(n), list); 
    255255        } 
     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 
     269ret_t  
     270cherokee_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        } 
    256279 
    257280        if (cherokee_list_empty (list)) 
    258281                return ret_not_found; 
    259282 
    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  
    968968                        case ret_ok: 
    969969                                if (cherokee_buffer_is_empty (&conn->local_directory)) { 
    970                                         if (is_userdir)  
     970                                        if (is_userdir) 
    971971                                                ret = cherokee_connection_build_local_directory_userdir (conn, CONN_VSRV(conn), &entry); 
    972972                                        else 
  • cherokee/trunk/cherokee/util.c

    r700 r773  
    357357cherokee_gmtime (const time_t *timep, struct tm *result) 
    358358{ 
    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 
    360364        struct tm *tmp; 
    361365 
    362         tmp = gmtime (timep); 
    363         memcpy (result, tmp, sizeof(struct tm)); 
    364         return result;   
    365 #else  
    366 # ifdef HAVE_GMTIME_R 
    367         return gmtime_r (timep, result); 
    368 # else 
    369         struct tm *tmp; 
    370  
    371366        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)); 
    374369        CHEROKEE_MUTEX_UNLOCK (&gmtime_mutex); 
    375370 
    376         return result; 
    377 # endif 
     371        return (tmp == NULL ? NULL : result); 
    378372#endif 
    379373} 
     
    387381cherokee_localtime (const time_t *timep, struct tm *result) 
    388382{ 
    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 
    390388        struct tm *tmp; 
    391389 
    392         tmp = localtime (timep); 
    393         memcpy (result, tmp, sizeof(struct tm)); 
    394         return result;   
    395 #else  
    396 # ifdef HAVE_LOCALTIME_R 
    397         return localtime_r (timep, result); 
    398 # else 
    399         struct tm *tmp; 
    400  
    401390        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)); 
    404393        CHEROKEE_MUTEX_UNLOCK (&localtime_mutex); 
    405394 
    406         return result; 
    407 # endif 
     395        return (tmp == NULL ? NULL : result); 
    408396#endif 
    409397} 
     
    449437        return readdir_r (dirstream, entry, result); 
    450438# else 
    451         struct dirent *ptr; 
    452         int            ret = 0; 
     439       struct dirent *ptr; 
     440       int            ret = 0; 
    453441 
    454442        CHEROKEE_MUTEX_LOCK (&readdir_mutex); 
    455443 
    456         errno = 0; 
    457         ptr = readdir(dirstream); 
     444       errno = 0; 
     445       ptr = readdir(dirstream); 
    458446         
    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; 
    466454         
    467455        CHEROKEE_MUTEX_UNLOCK (&readdir_mutex); 
    468         return ret; 
     456 
     457        return ret; 
     458 
    469459# endif 
    470460#endif 
     
    665655#if !defined(HAVE_PTHREAD) || (defined(HAVE_PTHREAD) && !defined(HAVE_GETHOSTBYNAME_R)) 
    666656 
    667         struct hostent *host; 
    668  
    669         CHEROKEE_MUTEX_LOCK (&__global_gethostbyname_mutex); 
    670         /* Resolv the host name 
    671          */ 
    672         host = gethostbyname (hostname); 
    673         if (host == NULL) { 
    674                 CHEROKEE_MUTEX_UNLOCK (&__global_gethostbyname_mutex); 
    675                 return ret_error; 
    676        
    677  
    678         /* Copy the address 
    679          */ 
    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; 
    683673 
    684674#elif defined(HAVE_PTHREAD) && defined(HAVE_GETHOSTBYNAME_R) 
     
    689679# define GETHOSTBYNAME_R_BUF_LEN 512 
    690680 
    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]; 
    696686         
    697687 
    698688# 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 
    708699# 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; 
    720710# endif   
    721  
    722         /* Copy the address 
    723          */ 
     711        /* Copy the address 
     712         */ 
    724713        if (hp == NULL) 
    725714                return ret_not_found; 
    726715 
    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; 
    730719#else 
    731  
    732         SHOULDNT_HAPPEN; 
    733         return ret_error; 
     720        /* Bad case ! 
     721         */ 
     722        SHOULDNT_HAPPEN; 
     723        return ret_error; 
    734724#endif 
    735725} 
     
    833823         */ 
    834824        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); 
    841831} 
    842832 
     
    10531043        string = qstring->buf; 
    10541044 
    1055         while ((token = (char *) strsep(&string, "&")) != NULL) 
    1056         { 
     1045        while ((token = (char *) strsep(&string, "&")) != NULL) { 
    10571046                char *equ, *key, *val; 
    10581047 
    10591048                if (token == NULL) continue; 
    10601049 
    1061                 if ((equ = strchr(token, '='))) 
    1062                 { 
     1050                if ((equ = strchr(token, '='))) { 
    10631051                        *equ = '\0'; 
    10641052 
     
    11031091        struct passwd *tmp; 
    11041092 
    1105       CHEROKEE_MUTEX_LOCK (&__global_getpwnam_mutex); 
     1093      CHEROKEE_MUTEX_LOCK (&__global_getpwnam_mutex); 
    11061094         
    11071095        tmp = getpwnam (name); 
    1108         if (tmp == NULL)  
     1096        if (tmp == NULL) { 
     1097                CHEROKEE_MUTEX_UNLOCK (&__global_getpwnam_mutex); 
    11091098                return ret_error; 
    1110  
     1099        } 
    11111100        if (tmp->pw_name)   pw_name_len   = strlen(tmp->pw_name); 
    11121101        if (tmp->pw_passwd) pw_passwd_len = strlen(tmp->pw_passwd); 
     
    11161105 
    11171106        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); 
    11191111                return ret_error; 
    1120  
     1112        } 
    11211113        memset (buf, 0, buflen); 
    11221114        ptr = buf; 
     
    11551147        } 
    11561148 
    1157         CHEROKEE_MUTEX_UNLOCK (&__global_getpwnam_mutex); 
     1149       CHEROKEE_MUTEX_UNLOCK (&__global_getpwnam_mutex); 
    11581150        return ret_ok; 
    11591151 
     
    11991191        struct group *tmp; 
    12001192 
    1201         CHEROKEE_MUTEX_LOCK (&__global_getgrnam_mutex); 
     1193       CHEROKEE_MUTEX_LOCK (&__global_getgrnam_mutex); 
    12021194 
    12031195        tmp = getgrnam (name); 
    1204         if (tmp == NULL)  
     1196        if (tmp == NULL) { 
     1197                CHEROKEE_MUTEX_UNLOCK (&__global_getgrnam_mutex); 
    12051198                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); 
    12111208                return ret_error; 
    1212  
     1209        } 
    12131210        memset (buf, 0, buflen); 
    12141211        ptr = buf; 
     
    12311228         */ 
    12321229 
    1233         CHEROKEE_MUTEX_UNLOCK (&__global_getgrnam_mutex); 
     1230        CHEROKEE_MUTEX_UNLOCK (&__global_getgrnam_mutex); 
     1231 
    12341232        return ret_ok; 
    12351233 
     
    12391237 
    12401238        re = getgrnam_r (name, grbuf, buf, buflen, &tmp); 
    1241         if (re != 0) return ret_error; 
     1239        if (re != 0) 
     1240                return ret_error; 
    12421241 
    12431242        return ret_ok; 
     
    12491248        int re; 
    12501249        re = getgrnam_r (name, grbuf, buf, buflen, &result); 
    1251         if (re != 0) return ret_error; 
     1250        if (re != 0) 
     1251                return ret_error; 
    12521252#else 
    12531253        result = getgrnam_r (name, grbuf, buf, buflen); 
    1254         if (result == NULL) return ret_error;    
     1254        if (result == NULL) 
     1255                return ret_error;        
    12551256#endif 
    12561257