Changeset 1924

Show
Ignore:
Timestamp:
09/01/08 12:06:36 (3 months ago)
Author:
alo
Message:

--

Files:

Legend:

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

    r1922 r1924  
     12008-09-01  Alvaro Lopez Ortega  <alvaro@alobbs.com> 
     2 
     3        * cherokee/main_worker.c, cherokee/handler_cgi_base.c, 
     4        cherokee/thread.c, cherokee/server-protected.h, cherokee/server.c: 
     5        Some signal management work. 
     6 
    172008-08-31  Alvaro Lopez Ortega  <alvaro@alobbs.com> 
    28 
  • cherokee/trunk/cherokee/handler_cgi_base.c

    r1910 r1924  
    533533 
    534534        /* SCRIPT_NAME: 
    535          * It is the request without the pathinfo if it exists 
     535         * RFC 3875: "URI path identifying the CGI script". 
     536         * 
     537         * For a CGI, it is the request without the PATHINFO, if any 
     538         * 
     539         * For a SCGI and FCGI, it is the request minus the pathinfo, 
     540         * which is request until the second slash character beginning 
     541         * at webdir length. 
     542         *  
    536543         */ 
    537544        cherokee_buffer_clean (&tmp); 
    538545 
    539546        if (! cgi_props->check_file) { 
    540                 /* SCGI or FastCGI */ 
    541  
    542                 if (conn->web_directory.len > 1) { 
    543                         cherokee_buffer_add_buffer (&tmp, &conn->web_directory); 
    544                 } 
    545                  
     547                /* SCGI or FastCGI 
     548                 */ 
     549                cherokee_buffer_add (&tmp, 
     550                                    conn->request.buf, 
     551                                    conn->request.len - conn->pathinfo.len); 
     552 
    546553                cgi->add_env_pair (cgi, "SCRIPT_NAME", 11, tmp.buf, tmp.len); 
    547554 
     
    588595        cint_t                             local_len; 
    589596        struct stat                        st; 
     597        cuint_t                            len          = 0; 
     598        cuint_t                            slashes      = 0; 
    590599        cint_t                             pathinfo_len = 0; 
    591600        cherokee_connection_t             *conn         = HANDLER_CONN(cgi); 
     
    615624 
    616625        /* No file checking: mainly for FastCGI and SCGI 
     626         * Examples: 
     627         * 
     628         * Webdir:         /demo/subdirectory 
     629         * Request:        http://localhost/demo/subdirectory/ 
     630         * SCRIPT_NAME':   /demo/subdirectory/ 
     631         * PATH_INFO:      <empty> 
     632         *  
     633         * Webdir:         / 
     634         * Request         http://localhost/another/large/one/foo 
     635         * SCRIPT_NAME:    /another 
     636         * PATH_INFO:      /large/one/foo 
     637         * 
     638         * Webdir:         / 
     639         * Request:        http://localhost/ 
     640         * SCRIPT_NAME:    / 
     641         * PATH_INFO:      <empty> 
    617642         */ 
    618643        if ((! props->check_file) && 
    619             (! cherokee_buffer_is_empty(&conn->web_directory)))  
     644            (! cherokee_buffer_is_empty (&conn->web_directory)))  
    620645        { 
    621                 if (conn->request.len == 1) { 
    622                         cherokee_buffer_add_str (&conn->pathinfo, "/"); 
    623  
    624                 } else if (conn->web_directory.len == 1) { 
    625                         cherokee_buffer_add_buffer (&conn->pathinfo, &conn->request); 
    626                                                      
    627                 } else { 
    628                         cherokee_buffer_add (&conn->pathinfo, 
    629                                              conn->request.buf + conn->web_directory.len, 
    630                                              conn->request.len - conn->web_directory.len); 
    631                 } 
     646                if (conn->web_directory.len > 1) { 
     647                        len += conn->web_directory.len; 
     648                } 
     649 
     650                while (len < conn->request.len) { 
     651                        if (conn->request.buf[len] == '/') { 
     652                                if (slashes == 1)  
     653                                        break; 
     654                                slashes = 1; 
     655                        } 
     656                        len ++; 
     657                } 
     658 
     659                cherokee_buffer_add (&conn->pathinfo,  
     660                                     conn->request.buf + len, 
     661                                     conn->request.len - len); 
     662 
    632663                return ret_ok; 
    633664        } 
  • cherokee/trunk/cherokee/main_worker.c

    r1921 r1924  
    2727#include <signal.h> 
    2828#include <unistd.h> 
     29#include <sys/types.h> 
     30#include <sys/wait.h> 
    2931 
    3032#include "init.h" 
     
    8587 
    8688 
    87 static void 
    88 panic_handler (int code) 
    89 
    90         UNUSED(code); 
    91         cherokee_server_handle_panic (srv); 
    92 
    93  
    94 static void 
    95 prepare_to_die (int code) 
    96 
    97         UNUSED(code); 
    98         cherokee_server_handle_TERM (srv); 
    99 
    100  
    101 static void 
    102 graceful_restart (int code) 
    103 {        
    104         /* Graceful restart sent by the 'guardian' 
    105          */ 
    106         UNUSED(code); 
    107         printf ("Handling HUP signal..\n"); 
    108         cherokee_server_handle_HUP (srv); 
    109 
    110  
    111 static void 
    112 reopen_log_files (int code) 
    113 {        
    114         UNUSED(code); 
    115         printf ("Reopeing log files..\n"); 
    116         cherokee_server_log_reopen (srv); 
    117 
     89static void  
     90signals_handler (int sig, siginfo_t *si, void *context)  
     91
     92        int exitcode; 
     93 
     94        UNUSED(si); 
     95        UNUSED(context); 
     96 
     97        switch (sig) { 
     98        case SIGHUP: 
     99                printf ("Handling Graceful Restart..\n"); 
     100                cherokee_server_handle_HUP (srv); 
     101                break; 
     102 
     103        case SIGUSR2: 
     104                printf ("Reopening log files..\n"); 
     105                cherokee_server_log_reopen (srv); 
     106                break; 
     107 
     108        case SIGINT: 
     109        case SIGTERM: 
     110                printf ("Server is exiting..\n"); 
     111                cherokee_server_handle_TERM (srv); 
     112                break; 
     113 
     114        case SIGCHLD: 
     115                wait (&exitcode); 
     116                break; 
     117 
     118        case SIGSEGV: 
     119#ifdef SIGBUS 
     120        case SIGBUS: 
     121#endif 
     122                cherokee_server_handle_panic (srv); 
     123                break; 
     124 
     125        default: 
     126                PRINT_ERROR ("Unknown signal: %d\n", sig); 
     127        } 
     128
     129 
    118130 
    119131static ret_t 
     
    134146common_server_initialization (cherokee_server_t *srv) 
    135147{ 
    136         ret_t ret; 
    137  
    138 #ifdef SIGPIPE 
    139         signal (SIGPIPE, SIG_IGN); 
    140 #endif 
    141 #ifdef SIGHUP 
    142         signal (SIGHUP, graceful_restart); 
    143 #endif 
    144 #ifdef SIGUSR2 
    145         signal (SIGUSR2, reopen_log_files); 
    146 #endif 
    147 #ifdef SIGSEGV 
    148         signal (SIGSEGV, panic_handler); 
    149 #endif 
     148        ret_t            ret; 
     149        struct sigaction act; 
     150 
     151        /* Signals it handles 
     152         */ 
     153        memset(&act, 0, sizeof(act)); 
     154 
     155        /* SIGPIPE */ 
     156        act.sa_handler = SIG_IGN; 
     157        sigaction (SIGPIPE, &act, NULL); 
     158 
     159        /* Signal Handler */ 
     160        act.sa_sigaction = signals_handler; 
     161        sigaction (SIGHUP,  &act, NULL); 
     162        sigaction (SIGUSR2, &act, NULL); 
     163        sigaction (SIGSEGV, &act, NULL); 
     164        sigaction (SIGTERM, &act, NULL); 
     165        sigaction (SIGINT,  &act, NULL); 
    150166#ifdef SIGBUS 
    151         signal (SIGBUS, panic_handler); 
    152 #endif 
    153 #ifdef SIGTERM 
    154         signal (SIGTERM, prepare_to_die); 
    155 #endif 
    156 #ifdef SIGINT 
    157         signal (SIGINT, prepare_to_die); 
     167        sigaction (SIGBUS,  &act, NULL); 
    158168#endif 
    159169 
  • cherokee/trunk/cherokee/server-protected.h

    r1912 r1924  
    6363        time_t                     start_time; 
    6464        cherokee_buffer_t          panic_action; 
     65 
     66        /* Restarts 
     67         */ 
    6568        cherokee_boolean_t         wanna_exit; 
    6669        cherokee_boolean_t         wanna_reinit; 
    67          
     70 
    6871        /* Virtual servers 
    6972         */ 
  • cherokee/trunk/cherokee/server.c

    r1918 r1924  
    121121        n->wanna_exit      = false; 
    122122        n->wanna_reinit    = false; 
    123          
     123 
    124124        /* Server config 
    125125         */ 
     
    10741074 
    10751075        list_for_each (i, &srv->thread_list) { 
    1076                 CHEROKEE_THREAD_JOIN (THREAD(i)->thread); 
     1076                cherokee_thread_wait_end (THREAD(i)); 
    10771077        } 
    10781078} 
     
    11021102                return ret_ok; 
    11031103 
     1104        /* Close all connections 
     1105         */ 
     1106        close_all_connections (srv); 
     1107 
     1108        /* Flush logs 
     1109         */ 
     1110        flush_logs (srv); 
     1111 
    11041112        /* Stop all the threads (may be slow) 
    11051113         */ 
    11061114        stop_threads (srv); 
    1107  
    1108         /* Close all connections 
    1109          */ 
    1110         close_all_connections (srv); 
    1111  
    1112         /* Flush logs 
    1113          */ 
    1114         flush_logs (srv); 
    11151115 
    11161116        return ret_ok; 
     
    11491149cherokee_server_step (cherokee_server_t *srv) 
    11501150{ 
     1151        /* Wanna exit ? 
     1152         */ 
     1153        if (unlikely (srv->wanna_exit)) { 
     1154                return ret_eof; 
     1155        } 
     1156 
    11511157        /* Get the server time. 
    11521158         */ 
     
    11751181#endif 
    11761182 
    1177         /* Wanna reinit ? 
    1178          */ 
    1179         if (unlikely (srv->wanna_reinit)) { 
    1180                 cherokee_list_t    *i; 
    1181                 cherokee_boolean_t  empty = true; 
     1183        /* Gracefull restart: 
     1184         * The main thread waits for the rest 
     1185         */ 
     1186        if (unlikely ((srv->wanna_reinit) && 
     1187                      (srv->main_thread->conns_num == 0))) 
     1188        { 
     1189                cherokee_list_t *i; 
    11821190 
    11831191                list_for_each (i, &srv->thread_list) { 
    1184                         if (THREAD(i)->conns_num != 0) { 
    1185                                 empty = false; 
    1186                                 break; 
    1187                         } 
    1188                 } 
    1189                  
    1190                 if (empty) 
    1191                         empty = (srv->main_thread->conns_num == 0); 
    1192  
    1193                 if (empty) 
    1194                         return ret_eof; 
    1195         } 
    1196          
    1197         /* Wanna exit ? 
    1198          */ 
    1199         if (unlikely (srv->wanna_exit)) { 
     1192                        cherokee_thread_wait_end (THREAD(i)); 
     1193                } 
    12001194                return ret_eof; 
    12011195        } 
     
    17131707cherokee_server_handle_HUP (cherokee_server_t *srv) 
    17141708{ 
    1715         srv->wanna_reinit    = true; 
    1716         srv->keepalive       = false; 
    1717         srv->keepalive_max   = 0; 
     1709        srv->wanna_reinit    = true; 
     1710        srv->keepalive        = false; 
     1711        srv->keepalive_max    = 0; 
    17181712 
    17191713        cherokee_socket_close (&srv->socket); 
  • cherokee/trunk/cherokee/thread.c

    r1912 r1924  
    116116cherokee_thread_wait_end (cherokee_thread_t *thd) 
    117117{ 
    118 #ifdef HAVE_PTHREAD 
    119118        /* Wait until the thread exits 
    120119         */ 
    121         pthread_join (thd->thread, NULL); 
    122 #endif 
     120        CHEROKEE_THREAD_JOIN (thd->thread); 
    123121        return ret_ok;   
    124122} 
     
    14671465        if (unlikely (thd->conns_num >= thd->conns_max)) 
    14681466                return 0; 
     1467 
     1468        if (unlikely ((THREAD_SRV(thd)->wanna_reinit) || 
     1469                      (THREAD_SRV(thd)->wanna_exit))) 
     1470                return 0; 
    14691471#if 0 
    14701472        if (unlikely (cherokee_fdpoll_is_full(thd->fdpoll))) { 
     
    15171519        } 
    15181520#endif 
     1521 
     1522        /* Graceful restart 
     1523         */ 
     1524        if (srv->wanna_reinit) 
     1525                goto out; 
    15191526 
    15201527        /* If thread has pending connections, it should do a  
     
    18671874        } 
    18681875#endif 
     1876 
     1877        /* Server wants to exit, and the thread has nothing to do 
     1878         */ 
     1879        if (unlikely (srv->wanna_exit)) { 
     1880                thd->exit = true; 
     1881                return ret_eof; 
     1882        } 
     1883 
     1884        if (unlikely (srv->wanna_reinit)) 
     1885        { 
     1886                if ((thd->active_list_num == 0) &&  
     1887                    (thd->polling_list_num == 0)) 
     1888                { 
     1889                        thd->exit = true; 
     1890                        return ret_eof; 
     1891                } 
     1892                 
     1893                cherokee_fdpoll_watch (thd->fdpoll, fdwatch_msecs); 
     1894                goto out; 
     1895        } 
    18691896 
    18701897        /* If thread has pending connections, it should do a