~ubuntu-branches/ubuntu/hardy/lighttpd/hardy

« back to all changes in this revision

Viewing changes to src/mod_fastcgi.c

  • Committer: Bazaar Package Importer
  • Author(s): Michele Angrisano
  • Date: 2007-07-28 20:33:22 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070728203322-ut5ym6udh0xjxkma
Tags: 1.4.16-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add fam/gamin stat cache engine support.
  - Replace Depends: on perl with Depends: on libterm-readline-perl-perl.
  - Make sure that upgrades succeed, even if we can't restart lighttpd.
  - Clean environment in init.d script.
  - Update maintainer field in debian/control.

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
        buffer *unixsocket; /* config.socket + "-" + id */
70
70
        unsigned port;  /* config.port + pno */
71
71
 
72
 
        buffer *connection_name; /* either tcp:<host>:<port> or unix:<socket> for debuggin purposes */
 
72
        buffer *connection_name; /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
73
73
 
74
74
        pid_t pid;   /* PID of the spawned process (0 if not spawned locally) */
75
75
 
80
80
        size_t requests;  /* see max_requests */
81
81
        struct fcgi_proc *prev, *next; /* see first */
82
82
 
83
 
        time_t disabled_until; /* this proc is disabled until, use something else until than */
 
83
        time_t disabled_until; /* this proc is disabled until, use something else until then */
84
84
 
85
85
        int is_local;
86
86
 
88
88
                PROC_STATE_UNSET,    /* init-phase */
89
89
                PROC_STATE_RUNNING,  /* alive */
90
90
                PROC_STATE_OVERLOADED, /* listen-queue is full,
91
 
                                          don't send something to this proc for the next 2 seconds */
 
91
                                          don't send anything to this proc for the next 2 seconds */
92
92
                PROC_STATE_DIED_WAIT_FOR_PID, /* */
93
93
                PROC_STATE_DIED,     /* marked as dead, should be restarted */
94
94
                PROC_STATE_KILLED    /* was killed as we don't have the load anymore */
145
145
        unsigned short disable_time;
146
146
 
147
147
        /*
148
 
         * same fastcgi processes get a little bit larger
 
148
         * some fastcgi processes get a little bit larger
149
149
         * than wanted. max_requests_per_proc kills a
150
150
         * process after a number of handled requests.
151
151
         *
184
184
         * bin-path is the path to the binary
185
185
         *
186
186
         * check min_procs and max_procs for the number
187
 
         * of process to start-up
 
187
         * of process to start up
188
188
         */
189
189
        buffer *bin_path;
190
190
 
217
217
        unsigned short mode;
218
218
 
219
219
        /*
220
 
         * check_local tell you if the phys file is stat()ed
 
220
         * check_local tells you if the phys file is stat()ed
221
221
         * or not. FastCGI doesn't care if the service is
222
222
         * remote. If the web-server side doesn't contain
223
223
         * the fastcgi-files we should not stat() for them
228
228
        /*
229
229
         * append PATH_INFO to SCRIPT_FILENAME
230
230
         *
231
 
         * php needs this if cgi.fix_pathinfo is provied
 
231
         * php needs this if cgi.fix_pathinfo is provided
232
232
         *
233
233
         */
234
234
 
247
247
        num_procs.
248
248
 
249
249
        only if a process is killed max_id waits for the process itself
250
 
        to die and decrements its afterwards */
 
250
        to die and decrements it afterwards */
251
251
 
252
252
        buffer *strip_request_uri;
253
253
 
826
826
                } else {
827
827
                        struct hostent *he;
828
828
 
829
 
                        /* set a usefull default */
 
829
                        /* set a useful default */
830
830
                        fcgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
831
831
 
832
832
 
869
869
        }
870
870
 
871
871
        if (-1 == connect(fcgi_fd, fcgi_addr, servlen)) {
872
 
                /* server is not up, spawn in  */
 
872
                /* server is not up, spawn it  */
873
873
                pid_t child;
874
874
                int val;
875
875
 
1029
1029
                                                        "child exited with status",
1030
1030
                                                        WEXITSTATUS(status), host->bin_path);
1031
1031
                                        log_error_write(srv, __FILE__, __LINE__, "s",
1032
 
                                                        "if you try do run PHP as FastCGI backend make sure you use the FastCGI enabled version.\n"
 
1032
                                                        "If you're trying to run PHP as a FastCGI backend, make sure you're using the FastCGI-enabled version.\n"
1033
1033
                                                        "You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' "
1034
 
                                                        "in the output, NOT (cgi) NOR (cli)\n"
1035
 
                                                        "For more information check http://www.lighttpd.net/documentation/fastcgi.html#preparing-php-as-a-fastcgi-program");
 
1034
                                                        "in the output, NOT '(cgi)' NOR '(cli)'.\n"
 
1035
                                                        "For more information, check http://trac.lighttpd.net/trac/wiki/Docs%3AModFastCGI#preparing-php-as-a-fastcgi-program"
 
1036
                                                        "If this is PHP on Gentoo, add 'fastcgi' to the USE flags.");
1036
1037
                                } else if (WIFSIGNALED(status)) {
1037
1038
                                        log_error_write(srv, __FILE__, __LINE__, "sd",
1038
1039
                                                        "terminated by signal:",
1040
1041
 
1041
1042
                                        if (WTERMSIG(status) == 11) {
1042
1043
                                                log_error_write(srv, __FILE__, __LINE__, "s",
1043
 
                                                                "to be exact: it seg-fault, crashed, died, ... you get the idea." );
 
1044
                                                                "to be exact: it segfaulted, crashed, died, ... you get the idea." );
1044
1045
                                                log_error_write(srv, __FILE__, __LINE__, "s",
1045
 
                                                                "If this is PHP try to remove the byte-code caches for now and try again.");
 
1046
                                                                "If this is PHP, try removing the bytecode caches for now and try again.");
1046
1047
                                        }
1047
1048
                                } else {
1048
1049
                                        log_error_write(srv, __FILE__, __LINE__, "sd",
1066
1067
 
1067
1068
                if (p->conf.debug) {
1068
1069
                        log_error_write(srv, __FILE__, __LINE__, "sb",
1069
 
                                        "(debug) socket is already used, won't spawn:",
 
1070
                                        "(debug) socket is already used; won't spawn:",
1070
1071
                                        proc->connection_name);
1071
1072
                }
1072
1073
        }
1508
1509
         *
1509
1510
         * next step is resetting this attemp and setup a connection again
1510
1511
         *
1511
 
         * if we have more then 5 reconnects for the same request, die
 
1512
         * if we have more than 5 reconnects for the same request, die
1512
1513
         *
1513
1514
         * 2.
1514
1515
         *
1626
1627
        CONNECTION_UNSET,
1627
1628
        CONNECTION_OK,
1628
1629
        CONNECTION_DELAYED, /* retry after event, take same host */
1629
 
        CONNECTION_OVERLOADED, /* disable for 1 seconds, take another backend */
 
1630
        CONNECTION_OVERLOADED, /* disable for 1 second, take another backend */
1630
1631
        CONNECTION_DEAD /* disable for 60 seconds, take another backend */
1631
1632
} connection_result_t;
1632
1633
 
1669
1670
                fcgi_addr_in.sin_family = AF_INET;
1670
1671
                if (0 == inet_aton(host->host->ptr, &(fcgi_addr_in.sin_addr))) {
1671
1672
                        log_error_write(srv, __FILE__, __LINE__, "sbs",
1672
 
                                        "converting IP-adress failed for", host->host,
 
1673
                                        "converting IP address failed for", host->host,
1673
1674
                                        "\nBe sure to specify an IP address here");
1674
1675
 
1675
1676
                        return -1;
1694
1695
                    errno == EINTR) {
1695
1696
                        if (hctx->conf.debug > 2) {
1696
1697
                                log_error_write(srv, __FILE__, __LINE__, "sb",
1697
 
                                        "connect delayed, will continue later:", proc->connection_name);
 
1698
                                        "connect delayed; will continue later:", proc->connection_name);
1698
1699
                        }
1699
1700
 
1700
1701
                        return CONNECTION_DELAYED;
1701
1702
                } else if (errno == EAGAIN) {
1702
1703
                        if (hctx->conf.debug) {
1703
1704
                                log_error_write(srv, __FILE__, __LINE__, "sbsd",
1704
 
                                        "This means that the you have more incoming requests than your fastcgi-backend can handle in parallel. "
1705
 
                                        "Perhaps it helps to spawn more fastcgi backend or php-children, if not decrease server.max-connections."
1706
 
                                        "The load for this fastcgi backend", proc->connection_name, "is", proc->load);
 
1705
                                        "This means that you have more incoming requests than your FastCGI backend can handle in parallel."
 
1706
                                        "It might help to spawn more FastCGI backends or PHP children; if not, decrease server.max-connections."
 
1707
                                        "The load for this FastCGI backend", proc->connection_name, "is", proc->load);
1707
1708
                        }
1708
1709
 
1709
1710
                        return CONNECTION_OVERLOADED;
1881
1882
        fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REMOTE_ADDR"), s, strlen(s));
1882
1883
 
1883
1884
        if (!buffer_is_empty(con->authed_user)) {
1884
 
                fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REMOTE_USER"), CONST_BUF_LEN(con->authed_user));
1885
 
        
1886
1885
                /* AUTH_TYPE fix by Troy Kruthoff (tkruthoff@gmail.com)
1887
1886
                 * section 4.1.1 of RFC 3875 (cgi spec) requires the server to set a AUTH_TYPE env
1888
1887
                 * declaring the type of authentication used.    (see http://tools.ietf.org/html/rfc3875#page-11)
1896
1895
                char *http_authorization = NULL;
1897
1896
                data_string *ds;
1898
1897
                
 
1898
                fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REMOTE_USER"), CONST_BUF_LEN(con->authed_user));
 
1899
        
1899
1900
                if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Authorization"))) {
1900
1901
                        http_authorization = ds->value->ptr;
1901
1902
                }
2055
2056
                        off_t written = 0;
2056
2057
                        off_t weHave = 0;
2057
2058
 
2058
 
                        /* we announce toWrite octects
2059
 
                         * now take all the request_content chunk that we need to fill this request
 
2059
                        /* we announce toWrite octets
 
2060
                         * now take all the request_content chunks that we need to fill this request
2060
2061
                         * */
2061
2062
 
2062
2063
                        b = chunkqueue_get_append_buffer(hctx->wb);
2356
2357
                }
2357
2358
 
2358
2359
                if (packet->b->used < packet->len + 1) {
2359
 
                        /* we didn't got the full packet */
 
2360
                        /* we didn't get the full packet */
2360
2361
 
2361
2362
                        buffer_free(packet->b);
2362
2363
                        return -1;
2439
2440
                b->used = r + 1; /* one extra for the fake \0 */
2440
2441
                b->ptr[b->used - 1] = '\0';
2441
2442
        } else {
2442
 
                if (errno == EAGAIN) return 0;
2443
2443
                log_error_write(srv, __FILE__, __LINE__, "ssdsb",
2444
2444
                                "unexpected end-of-file (perhaps the fastcgi process died):",
2445
2445
                                "pid:", proc->pid,
2558
2558
                        if (host->mode != FCGI_AUTHORIZER ||
2559
2559
                            !(con->http_status == 0 ||
2560
2560
                              con->http_status == 200)) {
2561
 
                                /* send chunk-end if nesseary */
 
2561
                                /* send chunk-end if necessary */
2562
2562
                                http_chunk_append_mem(srv, con, NULL, 0);
2563
2563
                                joblist_append(srv, con);
2564
2564
                        }
2653
2653
                        if (proc->state != PROC_STATE_DIED) break;
2654
2654
 
2655
2655
                case PROC_STATE_DIED:
2656
 
                        /* local proc get restarted by us,
 
2656
                        /* local procs get restarted by us,
2657
2657
                         * remote ones hopefully by the admin */
2658
2658
 
2659
2659
                        if (proc->is_local) {
2774
2774
                     proc && proc->state != PROC_STATE_RUNNING;
2775
2775
                     proc = proc->next);
2776
2776
 
2777
 
                /* all childs are dead */
 
2777
                /* all children are dead */
2778
2778
                if (proc == NULL) {
2779
2779
                        hctx->fde_ndx = -1;
2780
2780
 
2834
2834
                         * -> EAGAIN */
2835
2835
 
2836
2836
                        log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2837
 
                                "backend is overloaded, we disable it for a 2 seconds and send the request to another backend instead:",
 
2837
                                "backend is overloaded; we'll disable it for 2 seconds and send the request to another backend instead:",
2838
2838
                                "reconnects:", hctx->reconnects,
2839
2839
                                "load:", host->load);
2840
2840
 
2864
2864
                        }
2865
2865
 
2866
2866
                        log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2867
 
                                "backend died, we disable it for a 5 seconds and send the request to another backend instead:",
 
2867
                                "backend died; we'll disable it for 5 seconds and send the request to another backend instead:",
2868
2868
                                "reconnects:", hctx->reconnects,
2869
2869
                                "load:", host->load);
2870
2870
 
2950
2950
                                if (hctx->wb->bytes_out == 0 &&
2951
2951
                                    hctx->reconnects < 5) {
2952
2952
                                        usleep(10000); /* take away the load of the webserver
2953
 
                                                        * to let the php a chance to restart
 
2953
                                                        * to give the php a chance to restart
2954
2954
                                                        */
2955
2955
 
2956
2956
                                        fcgi_reconnect(srv, hctx);
2964
2964
                                 *
2965
2965
                                 */
2966
2966
 
2967
 
                                log_error_write(srv, __FILE__, __LINE__, "ssdsd",
 
2967
                                log_error_write(srv, __FILE__, __LINE__, "ssosd",
2968
2968
                                                "[REPORT ME] connection was dropped after accept(). reconnect() denied:",
2969
2969
                                                "write-offset:", hctx->wb->bytes_out,
2970
2970
                                                "reconnect attempts:", hctx->reconnects);
3152
3152
                            (con->http_status == 200 ||
3153
3153
                             con->http_status == 0)) {
3154
3154
                                /*
3155
 
                                 * If we are here in AUTHORIZER mode then a request for autorizer
3156
 
                                 * was proceeded already, and status 200 has been returned. We need
3157
 
                                 * now to handle autorized request.
 
3155
                                 * If we are here in AUTHORIZER mode then a request for authorizer
 
3156
                                 * was processed already, and status 200 has been returned. We need
 
3157
                                 * now to handle authorized request.
3158
3158
                                 */
3159
3159
 
3160
3160
                                buffer_copy_string_buffer(con->physical.doc_root, host->docroot);
3220
3220
                        }
3221
3221
 
3222
3222
                        if (con->file_started == 0) {
3223
 
                                /* nothing has been send out yet, try to use another child */
 
3223
                                /* nothing has been sent out yet, try to use another child */
3224
3224
 
3225
3225
                                if (hctx->wb->bytes_out == 0 &&
3226
3226
                                    hctx->reconnects < 5) {
3270
3270
                    hctx->state == FCGI_STATE_WRITE) {
3271
3271
                        /* we are allowed to send something out
3272
3272
                         *
3273
 
                         * 1. in a unfinished connect() call
3274
 
                         * 2. in a unfinished write() call (long POST request)
 
3273
                         * 1. in an unfinished connect() call
 
3274
                         * 2. in an unfinished write() call (long POST request)
3275
3275
                         */
3276
3276
                        return mod_fastcgi_handle_subrequest(srv, con, p);
3277
3277
                } else {
3286
3286
                if (hctx->state == FCGI_STATE_CONNECT_DELAYED) {
3287
3287
                        /* getoptsock will catch this one (right ?)
3288
3288
                         *
3289
 
                         * if we are in connect we might get a EINPROGRESS
3290
 
                         * in the first call and a FDEVENT_HUP in the
 
3289
                         * if we are in connect we might get an EINPROGRESS
 
3290
                         * in the first call and an FDEVENT_HUP in the
3291
3291
                         * second round
3292
3292
                         *
3293
3293
                         * FIXME: as it is a bit ugly.
3485
3485
                return HANDLER_FINISHED;
3486
3486
        }
3487
3487
 
3488
 
        /* a note about no handler is not sent yey */
 
3488
        /* a note about no handler is not sent yet */
3489
3489
        extension->note_is_sent = 0;
3490
3490
 
3491
3491
        /*
3520
3520
                        }
3521
3521
 
3522
3522
                        /* the prefix is the SCRIPT_NAME,
3523
 
                         * everthing from start to the next slash
 
3523
                         * everything from start to the next slash
3524
3524
                         * this is important for check-local = "disable"
3525
3525
                         *
3526
3526
                         * if prefix = /admin.fcgi
3630
3630
 
3631
3631
        /* perhaps we should kill a connect attempt after 10-15 seconds
3632
3632
         *
3633
 
         * currently we wait for the TCP timeout which is on Linux 180 seconds
 
3633
         * currently we wait for the TCP timeout which is 180 seconds on Linux
3634
3634
         *
3635
3635
         *
3636
3636
         *
3637
3637
         */
3638
3638
 
3639
 
        /* check all childs if they are still up */
 
3639
        /* check all children if they are still up */
3640
3640
 
3641
3641
        for (i = 0; i < srv->config_context->used; i++) {
3642
3642
                plugin_config *conf;
3718
3718
 
3719
3719
                                        if (srv->cur_ts - proc->last_used > host->idle_timeout) {
3720
3720
                                                /* a proc is idling for a long time now,
3721
 
                                                 * terminated it */
 
3721
                                                 * terminate it */
3722
3722
 
3723
3723
                                                if (p->conf.debug) {
3724
3724
                                                        log_error_write(srv, __FILE__, __LINE__, "ssbsd",
3725
 
                                                                        "idle-timeout reached, terminating child:",
 
3725
                                                                        "idle-timeout reached; terminating child:",
3726
3726
                                                                        "socket:", proc->connection_name,
3727
3727
                                                                        "pid", proc->pid);
3728
3728
                                                }