Changeset 784

Show
Ignore:
Timestamp:
07/02/07 21:54:07 (1 year ago)
Author:
adefacc
Message:

Added cherokee_strerror_r() function (thread safe).

Files:

Legend:

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

    r783 r784  
     1 
     22007-07-02  A.D.F  <adefacc@tin.it> 
     3 
     4        * cherokee/util.h 
     5          - added new cherokee_strerror_r() prototype; 
     6 
     7        * cherokee/util.c 
     8          - added new cherokee_strerror_r() function 
     9            in order to ensure thread safety; 
     10 
     11        * cherokee/win32_misc.h 
     12          - changed win_strerror() prototype; 
     13 
     14        * cherokee/win32_misc.c 
     15          - changed win_strerror() function to accept 
     16            a buffer in formal parameters; 
     17 
     18        * cherokee/buffer.c, 
     19          cherokee/cherokee_logrotate.c, 
     20          cherokee/fdpoll_epoll.c, 
     21          cherokee/fdpoll_kqueue.c, 
     22          cherokee/fdpoll_port.c, 
     23          cherokee/handler_cgi.c, 
     24          cherokee/logger_writer.c, 
     25          cherokee/ncpus.c, 
     26          cherokee/server.c, 
     27          cherokee/socket.c, 
     28          cherokee/validator_ldap.c 
     29          - replaced strerror() with cherokee_strerror_r() calls. 
    130 
    2312007-06-29  A.D.F  <adefacc@tin.it> 
  • cherokee/trunk/cherokee/buffer.c

    r690 r784  
    884884                case EIO:        return ret_error; 
    885885                } 
    886  
    887                 PRINT_ERROR ("ERROR: read(%d, %u,..) -> errno=%d '%s'\n", fd, size, errno, strerror(errno)); 
     886                { 
     887                char buferr[ERROR_MAX_BUFSIZE]; 
     888                PRINT_ERROR ("ERROR: read(%d, %u,..) -> errno=%d '%s'\n", fd, size, errno, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
     889                } 
    888890                return ret_error; 
    889891        } 
  • cherokee/trunk/cherokee/cherokee_logrotate.c

    r597 r784  
    177177        re = rename (argv[2], logname.buf); 
    178178        if (re != 0) { 
    179                 PRINT_ERROR("Could not move '%s' to '%s': %s\n", argv[2], logname.buf, strerror(errno)); 
     179                char buferr[ERROR_MAX_BUFSIZE]; 
     180                PRINT_ERROR("Could not move '%s' to '%s': %s\n", argv[2], logname.buf, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    180181        } 
    181182        printf ("Log file '%s' moved to '%s' successfully\n", argv[2], logname.buf); 
  • cherokee/trunk/cherokee/fdpoll-epoll.c

    r731 r784  
    109109 
    110110        if (epoll_ctl (fdp->ep_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { 
     111                char buferr[ERROR_MAX_BUFSIZE]; 
    111112                PRINT_ERROR ("ERROR: epoll_ctl(%d, EPOLL_CTL_ADD, %d): %s\n",  
    112                                 fdp->ep_fd, fd, strerror(errno)); 
     113                                fdp->ep_fd, fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    113114                return ret_error; 
    114115        } 
     
    136137 
    137138        if (epoll_ctl(fdp->ep_fd, EPOLL_CTL_DEL, fd, &ev) < 0) { 
     139                char buferr[ERROR_MAX_BUFSIZE]; 
    138140                PRINT_ERROR ("ERROR: epoll_ctl(%d, EPOLL_CTL_DEL, %d): %s\n",  
    139                                 fdp->ep_fd, fd, strerror(errno)); 
     141                                fdp->ep_fd, fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    140142                return ret_error; 
    141143        } 
     
    226228        } 
    227229 
    228         if (epoll_ctl(fdp->ep_fd, EPOLL_CTL_MOD, fd, &ev) < 0)  
    229         { 
     230        if (epoll_ctl(fdp->ep_fd, EPOLL_CTL_MOD, fd, &ev) < 0) { 
     231               char buferr[ERROR_MAX_BUFSIZE]; 
    230232                PRINT_ERROR ("ERROR: epoll_ctl (%d, EPOLL_CTL_MOD, %d): %s\n", 
    231                              fdp->ep_fd, fd, strerror(errno)); 
     233                             fdp->ep_fd, fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    232234                return ret_error; 
    233235        } 
     
    303305                 */ 
    304306#if 0 
     307                char buferr[ERROR_MAX_BUFSIZE]; 
    305308                PRINT_ERROR ("ERROR: epoll_create(%d): %s\n",  
    306                              nfd->nfiles+1, strerror(errno)); 
     309                             nfd->nfiles+1, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    307310#endif 
    308311                _free (n); 
     
    312315        re = fcntl (n->ep_fd, F_SETFD, FD_CLOEXEC); 
    313316        if (re < 0) { 
     317                char buferr[ERROR_MAX_BUFSIZE]; 
    314318                PRINT_ERROR ("ERROR: could not set CloseExec to the epoll descriptor: fcntl: %s\n",  
    315                              strerror(errno)); 
     319                             cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    316320                _free (n); 
    317321                return ret_error;                
  • cherokee/trunk/cherokee/fdpoll-kqueue.c

    r731 r784  
    170170        fdp->nchanges=0; 
    171171        if ( n_events < 0 ) { 
    172                 PRINT_ERROR ("ERROR: kevent: %s\n", strerror(errno)); 
     172                char buferr[ERROR_MAX_BUFSIZE]; 
     173                PRINT_ERROR ("ERROR: kevent: %s\n", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    173174                return 0; 
    174175        } else if ( n_events > 0 ) { 
  • cherokee/trunk/cherokee/fdpoll-port.c

    r731 r784  
    8181 
    8282        if ( rc == -1 ) { 
    83                 PRINT_ERROR ("ERROR: port_associate: fd %d: %s\n", fd,  
    84                                strerror(errno)); 
     83                char buferr[ERROR_MAX_BUFSIZE]; 
     84                PRINT_ERROR ("ERROR: port_associate: fd %d: %s\n", fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    8585                return ret_error; 
    8686        } 
     
    120120        rc = fd_associate(fdp, fd, (rw == FDPOLL_MODE_WRITE ? WRITE : READ)); 
    121121        if ( rc == -1 ) { 
    122                 PRINT_ERROR ("ERROR: port_associate: fd %d: %s\n", fd,  
    123                                strerror(errno)); 
     122                char buferr[ERROR_MAX_BUFSIZE]; 
     123                PRINT_ERROR ("ERROR: port_associate: fd %d: %s\n", fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    124124                return ret_error; 
    125125        } 
     
    139139                              fd);            /* object */ 
    140140        if ( rc == -1 ) { 
    141                 PRINT_ERROR ("ERROR: port_dissociate: %d,%s\n", fd,  
    142                             strerror(errno)); 
     141                char buferr[ERROR_MAX_BUFSIZE]; 
     142                PRINT_ERROR ("ERROR: port_dissociate: %d,%s\n", fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    143143                return ret_error; 
    144144        } 
     
    172172                        &timeout); 
    173173        if ( rc < 0 ) { 
    174                 PRINT_ERROR ("ERROR: port_getn: %s\n", strerror(errno)); 
     174                char buferr[ERROR_MAX_BUFSIZE]; 
     175                PRINT_ERROR ("ERROR: port_getn: %s\n", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    175176                return 0; 
    176177        } 
     
    188189                        &fdp->port_readyfds, &timeout); 
    189190        if ( ( (rc < 0) && (errno != ETIME) ) || (fdp->port_readyfds == -1)) { 
    190                 PRINT_ERROR ("ERROR: port_getn: %s\n", strerror(errno)); 
     191                char buferr[ERROR_MAX_BUFSIZE]; 
     192                PRINT_ERROR ("ERROR: port_getn: %s\n", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    191193                return 0; 
    192194        } 
     
    201203                                   fdp->port_events[i].portev_user); 
    202204                if ( rc < 0 ) { 
    203                         PRINT_ERROR ("ERROR: port_associate: %s\n",  
    204                                        strerror(errno)); 
     205                        char buferr[ERROR_MAX_BUFSIZE]; 
     206                        PRINT_ERROR ("ERROR: port_associate: %s\n", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    205207                } 
    206208        } 
     
    253255                            (rw == FDPOLL_MODE_WRITE ? WRITE   : READ)); 
    254256        if ( rc == -1 ) { 
    255                 PRINT_ERROR ("ERROR: port_associate: fd %d: %s\n", fd,  
    256                                strerror(errno)); 
     257                char buferr[ERROR_MAX_BUFSIZE]; 
     258                PRINT_ERROR ("ERROR: port_associate: fd %d: %s\n", fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    257259                return ret_error; 
    258260        } 
  • cherokee/trunk/cherokee/handler_cgi.c

    r597 r784  
    455455 
    456456        if (fcntl (fd, F_SETFL, flags) == -1) { 
    457                 PRINT_ERROR ("ERROR: Setting pipe properties fd=%d: %s\n", fd, strerror(errno)); 
     457                char buferr[ERROR_MAX_BUFSIZE]; 
     458                PRINT_ERROR ("ERROR: Setting pipe properties fd=%d: %s\n", fd, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    458459                return ret_error; 
    459460        }        
     
    568569                } 
    569570 
     571                { 
     572                char buferr[ERROR_MAX_BUFSIZE]; 
    570573                cherokee_logger_write_string (CONN_VSRV(conn)->logger,  
    571                                               "couldn't execute '%s': %s", 
    572                                               absolute_path, strerror(err)); 
     574                        "couldn't execute '%s': %s", 
     575                        absolute_path, 
     576                        cherokee_strerror_r(err, buferr, sizeof(buferr))); 
     577                } 
    573578                exit(1); 
    574579        } 
  • cherokee/trunk/cherokee/logger_writer.c

    r650 r784  
    201201 
    202202        if (pipe (to_log_fds)) {  
    203                 PRINT_MSG ("Pipe error: %s\n", strerror(errno)); 
     203                char buferr[ERROR_MAX_BUFSIZE]; 
     204                PRINT_MSG ("Pipe error: %s\n", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    204205                return ret_error; 
    205206        } 
     
    222223 
    223224        case -1: 
    224                 PRINT_MSG ("Fork failed: %s\n", strerror(errno)); 
     225                { 
     226                char buferr[ERROR_MAX_BUFSIZE]; 
     227                PRINT_MSG ("Fork failed: %s\n", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
     228                } 
    225229                break; 
    226230 
  • cherokee/trunk/cherokee/ncpus.c

    r170 r784  
    2525#include "config.h" 
    2626#include "ncpus.h" 
     27#include "util.h" 
    2728 
    2829#include <stdio.h> 
     
    5758        return 0; 
    5859    } else { 
    59         fprintf (stderr, "pstat_getdynamic failed: %s", strerror(errno)); 
     60        char buferr[ERROR_MAX_BUFSIZE]; 
     61        fprintf (stderr, "pstat_getdynamic failed: %s", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    6062        *ncpus = -1; 
    6163        return EXIT_DISTCC_FAILED; 
     
    109111        return 0; 
    110112    else { 
     113        char buferr[ERROR_MAX_BUFSIZE]; 
    111114        fprintf(stderr,"sysctl(CTL_HW:HW_NCPU) failed: %s", 
    112                      strerror(errno)); 
     115                     cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    113116        return EXIT_DISTCC_FAILED; 
    114117    } 
     
    138141     
    139142    if (*ncpus == -1) { 
     143        char buferr[ERROR_MAX_BUFSIZE]; 
    140144        fprintf(stderr,"sysconf(_SC_NPROCESSORS_ONLN) failed: %s", 
    141                      strerror(errno)); 
     145                     cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    142146        return EXIT_DISTCC_FAILED; 
    143147    } else if (*ncpus == 0) { 
  • cherokee/trunk/cherokee/server.c

    r775 r784  
    10861086                srv->chrooted = (chroot (srv->chroot.buf) == 0); 
    10871087                if (srv->chrooted == 0) { 
    1088                         PRINT_ERROR ("Cannot chroot() to '%s': %s\n", srv->chroot.buf, strerror(errno)); 
     1088                        char buferr[ERROR_MAX_BUFSIZE]; 
     1089                        PRINT_ERROR ("Cannot chroot() to '%s': %s\n", srv->chroot.buf, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    10891090                        return ret_error; 
    10901091                } 
     
    11021103        re = chdir ("/"); 
    11031104        if (re < 0) { 
    1104                 PRINT_ERROR ("Couldn't chdir(\"/\"): %s\n", strerror(errno)); 
     1105                char buferr[ERROR_MAX_BUFSIZE]; 
     1106                PRINT_ERROR ("Couldn't chdir(\"/\"): %s\n", cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    11051107                return ret_error; 
    11061108        } 
     
    19441946        file = fopen (srv->pidfile.buf, "w"); 
    19451947        if (file == NULL) { 
    1946                 PRINT_MSG ("ERROR: Can't write PID file '%s': %s\n", srv->pidfile.buf, strerror(errno)); 
     1948                char buferr[ERROR_MAX_BUFSIZE]; 
     1949                PRINT_MSG ("ERROR: Can't write PID file '%s': %s\n", srv->pidfile.buf, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    19471950                return ret_error; 
    19481951        } 
  • cherokee/trunk/cherokee/socket.c

    r764 r784  
    896896                                return ret_error; 
    897897                        } 
    898          
     898                        {        
     899                        char buferr[ERROR_MAX_BUFSIZE]; 
    899900                        PRINT_ERROR ("ERROR: write(%d, ..) -> errno=%d '%s'\n",  
    900                                 SOCKET_FD(socket), err, strerror(err)); 
     901                                SOCKET_FD(socket), err, cherokee_strerror_r(err, buferr, sizeof(buferr))); 
     902                        } 
    901903                        return ret_error; 
    902904                } 
     
    10401042                                return ret_error; 
    10411043                        } 
    1042  
     1044                        { 
     1045                        char buferr[ERROR_MAX_BUFSIZE]; 
    10431046                        PRINT_ERROR ("ERROR: read(%d, ..) -> errno=%d '%s'\n",  
    1044                                 SOCKET_FD(socket), err, strerror(err)); 
     1047                                SOCKET_FD(socket), err, cherokee_strerror_r(err, buferr, sizeof(buferr))); 
     1048                        } 
    10451049                        return ret_error; 
    10461050                } 
     
    12231227                                return ret_error; 
    12241228                        } 
    1225  
     1229                        { 
     1230                        char buferr[ERROR_MAX_BUFSIZE]; 
    12261231                        PRINT_ERROR ("ERROR: writev(%d, ..) -> errno=%d '%s'\n",  
    1227                              SOCKET_FD(socket), err, strerror(err)); 
     1232                             SOCKET_FD(socket), err, cherokee_strerror_r(err, buferr, sizeof(buferr))); 
     1233                        } 
    12281234                        return ret_error; 
    12291235                } 
     
    15681574                default: 
    15691575#if 1 
    1570                         PRINT_ERROR ("ERROR: Can not connect: %s\n", strerror(err)); 
     1576                        { 
     1577                        char buferr[ERROR_MAX_BUFSIZE]; 
     1578                        PRINT_ERROR ("ERROR: Can not connect: %s\n", cherokee_strerror_r(err, buferr, sizeof(buferr))); 
     1579                        } 
    15711580#endif 
    15721581                        return ret_error; 
     
    17351744        if (re < 0) { 
    17361745                int err = errno; 
     1746                char buferr[ERROR_MAX_BUFSIZE]; 
    17371747                PRINT_ERROR ("Couldn't set SO_RCVTIMEO, fd=%d, timeout=%d: %s\n",  
    1738                              socket->socket, timeout, strerror(err)); 
     1748                             socket->socket, timeout, cherokee_strerror_r(err, buferr, sizeof(buferr))); 
    17391749                return ret_error; 
    17401750        } 
  • cherokee/trunk/cherokee/util.c

    r773 r784  
    8686 
    8787const char *cherokee_version    = PACKAGE_VERSION; 
     88 
     89/* Given an error number (errno) it returns an error string. 
     90 * Parameters "buf" and "bufsize" are passed by caller 
     91 * in order to make the function "thread safe". 
     92 * If the error number is unknown 
     93 * then an "Unknown error nnn" string is returned. 
     94 */ 
     95char * 
     96cherokee_strerror_r (int err, char *buf, size_t bufsize) 
     97{ 
     98#ifdef _WIN32 
     99        return win_strerror (err, buf, bufsize); 
     100#else 
     101        char *p; 
     102        if (buf == NULL) 
     103                return NULL; 
     104 
     105        if (bufsize < ERROR_MIN_BUFSIZE) 
     106                return NULL; 
     107 
     108        if ((p = strerror(err)) == NULL) { 
     109                buf[0] = '\0'; 
     110                snprintf(buf, bufsize, "Unknown error %d (errno)", err); 
     111                buf[bufsize-1] = '\0'; 
     112                return buf; 
     113        } 
     114 
     115        return p; 
     116#endif 
     117} 
    88118 
    89119 
  • cherokee/trunk/cherokee/util.h

    r644 r784  
    5656CHEROKEE_BEGIN_DECLS 
    5757 
     58/* Error buffer size for cherokee_strerror_r(). 
     59 */ 
     60#define ERROR_MIN_BUFSIZE       64      /* min. buffer size */ 
     61#define ERROR_MAX_BUFSIZE       512     /* max. buffer size */ 
     62 
    5863#ifdef _WIN32 
    5964# define cherokee_stat(path,buf) cherokee_win32_stat(path,buf) 
     
    7378/* String management functions 
    7479 */ 
     80char   *cherokee_strerror_r         (int err, char *buf, size_t bufsize); 
    7581int     cherokee_isbigendian        (void); 
    7682char   *cherokee_min_str            (char *s1, char *s2); 
  • cherokee/trunk/cherokee/validator_ldap.c

    r782 r784  
    159159        ldap->conn = ldap_init (props->server.buf, props->port); 
    160160        if (ldap->conn == NULL) { 
     161                char buferr[ERROR_MAX_BUFSIZE]; 
    161162                PRINT_ERROR ("ERROR: LDAP validator: Couldn't connect to LDAP: %s:%d: %s\n",  
    162                              props->server.buf, props->port, strerror(errno)); 
     163                             props->server.buf, props->port, cherokee_strerror_r(errno, buferr, sizeof(buferr))); 
    163164                return ret_error; 
    164165        } 
  • cherokee/trunk/cherokee/win32_misc.c

    r780 r784  
    7575        re = WSAStartup (MAKEWORD(1,1), &wsa_data); 
    7676        if (re != 0) { 
    77                 PRINT_ERROR ("WSAStartup failed; %s\n", win_strerror(GetLastError())); 
     77                char errbuf[ERROR_MAX_BUFSIZE]; 
     78                PRINT_ERROR ("WSAStartup failed; %s\n", win_strerror(GetLastError(), errbuf, sizeof(errbuf))); 
    7879                exit (-1); 
    7980        } 
     
    376377 * A smarter strerror() 
    377378 * 
    378  * TODO: make this function thread-safe (by using some smart trick). 
    379  */ 
    380 char *win_strerror (int err) 
    381 
    382         static char buf[512];   /* WARNING!! not thread-safe */ 
     379 * NOTE: buf is a buffer passed by caller of at least ERROR_MIN_BUFSIZE size. 
     380 */ 
     381char *win_strerror (int err, char *buf, size_t bufsize) 
     382
    383383        DWORD  lang  = MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT); 
    384384        DWORD  flags = FORMAT_MESSAGE_FROM_SYSTEM | 
     
    387387        char  *p; 
    388388 
     389        if (buf == NULL) 
     390                return NULL; 
     391 
     392        if (bufsize < ERROR_MIN_BUFSIZE) 
     393                return NULL; 
     394 
     395        buf[0] = '\0'; 
     396        buf [bufsize-1] = '\0'; 
     397 
    389398        if (err >= 0 && err < sys_nerr) { 
    390399                /* Call the strerror() func, do not use the macro! */ 
    391                 strncpy (buf, (strerror)(err), sizeof(buf)-1); 
    392                 buf [sizeof(buf)-1] = '\0'; 
     400                strncpy (buf, (strerror)(err), bufsize - 1); 
    393401        } else { 
    394                 if (!get_winsock_error (err, buf, sizeof(buf)) && 
     402                if (!get_winsock_error (err, buf, bufsize - 1) && 
    395403                    !FormatMessage (flags, NULL, err, lang, 
    396                         buf, sizeof(buf)-1, NULL)) 
    397                         sprintf (buf, "Unknown error %d (%#x)", err, err); 
     404                        buf, bufsize - 1, NULL)) 
     405                        snprintf (buf, bufsize, "Unknown error %d (%#x)", err, err); 
    398406        } 
    399407 
     
    404412        if ((p = strrchr(buf,'\r')) != NULL && (p - buf) >= 1) 
    405413                *p = '\0'; 
     414 
    406415        return (buf); 
    407416} 
     
    627636                return (NULL); 
    628637 
     638        {       /* FIXME, error buf should be passed by caller. */ 
     639        char buf[ERROR_MAX_BUFSIZE] 
    629640        snprintf (win_dlerror_buf, sizeof(win_dlerror_buf)-1, "%s(): %s", 
    630                         last_func, win_strerror(last_error)); 
    631  
     641                        last_func, win_strerror(last_error, buf, sizeof(buf))); 
     642        } 
    632643        return (win_dlerror_buf); 
    633644} 
  • cherokee/trunk/cherokee/win32_misc.h

    r597 r784  
    3333#undef  localtime_r       /* in <pthread.h> */ 
    3434#define SHUT_WR           SD_SEND 
    35 #define strerror(e)       win_strerror(e) 
    3635#define pipe(h)           _pipe(h,0,0) 
    3736 
    3837void          init_win32          (void); 
    39 char         *win_strerror        (int err); 
     38char         *win_strerror        (int err, char *buf, size_t bufsize); 
    4039struct tm    *localtime_r         (const time_t *time, struct tm *tm); 
    4140unsigned int  sleep               (unsigned int seconds);