Changeset 218:76a04f491a6c for uwsgi.c

Show
Ignore:
Timestamp:
02/07/10 23:06:24 (6 months ago)
Author:
roberto@…
Branch:
default
Message:

big update: hooks and plugins

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • uwsgi.c

    r217 r218  
    3030 
    3131 
    32 #ifndef ROCK_SOLID 
    33 #ifdef __linux__ 
    34         #include <sys/sendfile.h> 
    35 #endif 
    36 #endif 
    3732 
    3833#ifndef UNBIT 
     
    4641 
    4742 
    48 #include <sys/wait.h> 
    49  
    50  
    5143#include "uwsgi.h" 
    5244 
    5345#if PY_MINOR_VERSION < 5 
    54         #define Py_ssize_t int 
     46#define Py_ssize_t int 
    5547#endif 
    5648 
     
    5850 
    5951static char *nl = "\r\n"; 
    60 static char *h_sep = ": " ; 
    61 static const char *http_protocol = "HTTP/1.1" ; 
    62 #ifndef ROCK_SOLID 
    63 static const char *app_slash = "/" ; 
    64 #endif 
    65  
    66 int find_worker_id(pid_t pid) { 
    67         int i ; 
    68         for(i = 1 ; i<= uwsgi.numproc ; i++) { 
     52static char *h_sep = ": "; 
     53static const char *http_protocol = "HTTP/1.1"; 
     54#ifndef ROCK_SOLID 
     55static const char *app_slash = "/"; 
     56#endif 
     57 
     58int find_worker_id (pid_t pid) { 
     59        int i; 
     60        for (i = 1; i <= uwsgi.numproc; i++) { 
    6961                /* fprintf(stderr,"%d of %d\n", pid, uwsgi.workers[i].pid); */ 
    7062                if (uwsgi.workers[i].pid == pid) 
    71                         return i ; 
    72         } 
    73  
    74         return -1 ; 
     63                        return i; 
     64        } 
     65 
     66        return -1; 
    7567} 
    7668 
     
    7870 
    7971PyMethodDef null_methods[] = { 
    80   {NULL, NULL}, 
     72        {NULL, NULL}, 
    8173}; 
    8274 
    8375#ifdef UNBIT 
    84 int save_to_disk = -1 ; 
    85 int tmp_dir_fd = -1 ; 
    86 char *tmp_filename ; 
    87 int uri_to_hex(void); 
    88 int check_for_memory_errors = 0 ; 
    89 #endif 
    90  
    91 PyObject *wsgi_writeout ; 
    92  
    93 // iovec 
    94 struct iovec *hvec ; 
     76int save_to_disk = -1; 
     77int tmp_dir_fd = -1; 
     78char *tmp_filename; 
     79int uri_to_hex (void); 
     80int check_for_memory_errors = 0; 
     81#endif 
     82 
     83PyObject *wsgi_writeout; 
     84 
    9585 
    9686#ifdef ROCK_SOLID 
     
    9888#endif 
    9989 
    100 void warn_pipe() { 
    101         fprintf(stderr,"writing to a closed pipe/socket/fd !!!\n"); 
     90void warn_pipe () { 
     91        fprintf (stderr, "writing to a closed pipe/socket/fd !!!\n"); 
    10292} 
    10393 
    104 void gracefully_kill() { 
    105         fprintf(stderr, "Gracefully killing worker %d...\n", uwsgi.mypid); 
     94void gracefully_kill () { 
     95        fprintf (stderr, "Gracefully killing worker %d...\n", uwsgi.mypid); 
    10696        if (uwsgi.workers[uwsgi.mywid].in_request) { 
    107                 uwsgi.workers[uwsgi.mywid].manage_next_request = 0 ;     
     97                uwsgi.workers[uwsgi.mywid].manage_next_request = 0; 
    10898        } 
    10999        else { 
    110                 reload_me(); 
     100                reload_me (); 
    111101        } 
    112102} 
    113103 
    114 void reload_me() { 
    115         exit(UWSGI_RELOAD_CODE); 
     104void reload_me () { 
     105        exit (UWSGI_RELOAD_CODE); 
    116106} 
    117107 
    118 void end_me() { 
    119         exit(UWSGI_END_CODE); 
     108void end_me () { 
     109        exit (UWSGI_END_CODE); 
    120110} 
    121111 
    122 void goodbye_cruel_world() { 
    123         fprintf(stderr, "...The work of process %d is done. Seeya!\n", getpid()); 
    124         exit(0); 
     112void goodbye_cruel_world () { 
     113        fprintf (stderr, "...The work of process %d is done. Seeya!\n", getpid ()); 
     114        exit (0); 
    125115} 
    126116 
    127 void kill_them_all() { 
    128         int i ; 
    129         uwsgi.to_hell = 1 ; 
    130         fprintf(stderr,"SIGINT/SIGQUIT received...killing workers...\n"); 
    131         for(i=1;i<=uwsgi.numproc;i++) {  
    132                 kill(uwsgi.workers[i].pid, SIGINT); 
     117void kill_them_all () { 
     118        int i; 
     119        uwsgi.to_hell = 1; 
     120        fprintf (stderr, "SIGINT/SIGQUIT received...killing workers...\n"); 
     121        for (i = 1; i <= uwsgi.numproc; i++) { 
     122                kill (uwsgi.workers[i].pid, SIGINT); 
    133123        } 
    134124} 
    135125 
    136 void grace_them_all() { 
    137         int i ; 
    138         uwsgi.to_heaven = 1 ; 
    139         fprintf(stderr,"...gracefully killing workers...\n"); 
    140         for(i=1;i<=uwsgi.numproc;i++) {  
    141                 kill(uwsgi.workers[i].pid, SIGHUP); 
     126void grace_them_all () { 
     127        int i; 
     128        uwsgi.to_heaven = 1; 
     129        fprintf (stderr, "...gracefully killing workers...\n"); 
     130        for (i = 1; i <= uwsgi.numproc; i++) { 
     131                kill (uwsgi.workers[i].pid, SIGHUP); 
    142132        } 
    143133} 
    144134 
    145 void reap_them_all() { 
    146         int i ; 
    147         fprintf(stderr,"...brutally killing workers...\n"); 
    148         for(i=1;i<=uwsgi.numproc;i++) { 
    149                 kill(uwsgi.workers[i].pid, SIGTERM); 
     135void reap_them_all () { 
     136        int i; 
     137        fprintf (stderr, "...brutally killing workers...\n"); 
     138        for (i = 1; i <= uwsgi.numproc; i++) { 
     139                kill (uwsgi.workers[i].pid, SIGTERM); 
    150140        } 
    151141} 
    152142 
    153 void harakiri() { 
     143void harakiri () { 
    154144 
    155145        PyThreadState *_myself; 
    156146#ifndef ROCK_SOLID 
    157         struct uwsgi_app *wi = NULL ; 
     147        struct uwsgi_app *wi = NULL; 
    158148        if (wsgi_req.app_id >= 0) { 
    159                 wi = &uwsgi.wsgi_apps[wsgi_req.app_id] ; 
    160         } 
    161 #endif 
    162         PyGILState_Ensure(); 
    163         _myself = PyThreadState_Get(); 
     149                wi = &uwsgi.wsgi_apps[wsgi_req.app_id]; 
     150        } 
     151#endif 
     152        PyGILState_Ensure (); 
     153        _myself = PyThreadState_Get (); 
    164154        if (wi) { 
    165         #ifdef ROCK_SOLID 
    166                 fprintf(stderr,"\nF*CK !!! i must kill myself (pid: %d) wi: %p wi->wsgi_harakiri: %p thread_state: %p frame: %p...\n", uwsgi.mypid, wi, wi->wsgi_harakiri, _myself, _myself->frame ); 
    167         #else 
    168                 fprintf(stderr,"\nF*CK !!! i must kill myself (pid: %d app_id: %d) wi: %p wi->wsgi_harakiri: %p thread_state: %p frame: %p...\n", uwsgi.mypid, wsgi_req.app_id, wi, wi->wsgi_harakiri, _myself, _myself->frame ); 
    169         #endif 
     155#ifdef ROCK_SOLID 
     156                fprintf (stderr, "\nF*CK !!! i must kill myself (pid: %d) wi: %p wi->wsgi_harakiri: %p thread_state: %p frame: %p...\n", uwsgi.mypid, wi, wi->wsgi_harakiri, _myself, _myself->frame); 
     157#else 
     158                fprintf (stderr, "\nF*CK !!! i must kill myself (pid: %d app_id: %d) wi: %p wi->wsgi_harakiri: %p thread_state: %p frame: %p...\n", uwsgi.mypid, wsgi_req.app_id, wi, wi->wsgi_harakiri, _myself, _myself->frame); 
     159#endif 
    170160 
    171161                if (wi->wsgi_harakiri) { 
    172                         PyEval_CallObject(wi->wsgi_harakiri, wi->wsgi_args); 
    173                         if (PyErr_Occurred()) { 
    174                                 PyErr_Print(); 
    175                         } 
    176                 } 
    177         } 
    178         Py_FatalError("HARAKIRI !\n"); 
     162                        PyEval_CallObject (wi->wsgi_harakiri, wi->wsgi_args); 
     163                        if (PyErr_Occurred ()) { 
     164                                PyErr_Print (); 
     165                        } 
     166                } 
     167        } 
     168        Py_FatalError ("HARAKIRI !\n"); 
    179169} 
    180170 
    181171#ifndef UNBIT 
    182172#ifndef ROCK_SOLID 
    183 void stats() { 
     173void stats () { 
    184174        struct uwsgi_app *ua = NULL; 
    185175        int i; 
    186176 
    187         fprintf(stderr, "*** pid %d stats ***\n", getpid()); 
    188         fprintf(stderr, "\ttotal requests: %llu\n", uwsgi.workers[0].requests); 
    189         for(i=0;i<uwsgi.wsgi_cnt;i++) { 
     177        fprintf (stderr, "*** pid %d stats ***\n", getpid ()); 
     178        fprintf (stderr, "\ttotal requests: %llu\n", uwsgi.workers[0].requests); 
     179        for (i = 0; i < uwsgi.wsgi_cnt; i++) { 
    190180                ua = &uwsgi.wsgi_apps[i]; 
    191181                if (ua) { 
    192                         fprintf(stderr, "\tapp %d requests: %d\n", i, ua->requests); 
    193                 } 
    194         } 
    195         fprintf(stderr, "\n"); 
     182                        fprintf (stderr, "\tapp %d requests: %d\n", i, ua->requests); 
     183                } 
     184        } 
     185        fprintf (stderr, "\n"); 
    196186} 
    197187#endif 
    198188#endif 
    199189 
    200 void internal_server_error(int fd, char *message) { 
     190void internal_server_error (int fd, char *message) { 
    201191#ifndef UNBIT 
    202192#ifndef ROCK_SOLID 
     
    204194#endif 
    205195#endif 
    206                 wsgi_req.headers_size = write(fd, "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 63); 
     196                wsgi_req.headers_size = write (fd, "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 63); 
    207197#ifndef UNBIT 
    208198#ifndef ROCK_SOLID 
    209199        } 
    210200        else { 
    211                 wsgi_req.headers_size = write(fd, "Status: 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 62); 
    212         } 
    213 #endif 
    214         wsgi_req.header_cnt = 2 ; 
    215 #endif 
    216         wsgi_req.response_size = write(fd, "<h1>uWSGI Error</h1>", 20); 
    217         wsgi_req.response_size += write(fd, message, strlen(message)); 
     201                wsgi_req.headers_size = write (fd, "Status: 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 62); 
     202        } 
     203#endif 
     204        wsgi_req.header_cnt = 2; 
     205#endif 
     206        wsgi_req.response_size = write (fd, "<h1>uWSGI Error</h1>", 20); 
     207        wsgi_req.response_size += write (fd, message, strlen (message)); 
    218208} 
    219209 
    220210#ifndef ROCK_SOLID 
    221 PyObject *py_uwsgi_sendfile(PyObject *self, PyObject *args) { 
    222  
    223         //PyObject *zero ; 
    224  
    225         uwsgi.py_sendfile = PyTuple_GetItem(args, 0); 
     211PyObject *py_uwsgi_sendfile (PyObject * self, PyObject * args) { 
     212 
     213        //PyObject *zero ; 
     214 
     215        uwsgi.py_sendfile = PyTuple_GetItem (args, 0); 
    226216 
    227217#ifdef PYTHREE 
    228         if ((wsgi_req.sendfile_fd = PyObject_AsFileDescriptor(uwsgi.py_sendfile)) >= 0) { 
    229                 Py_INCREF(uwsgi.py_sendfile); 
     218        if ((wsgi_req.sendfile_fd = PyObject_AsFileDescriptor (uwsgi.py_sendfile)) >= 0) { 
     219                Py_INCREF (uwsgi.py_sendfile); 
    230220        } 
    231221#else 
    232         if (PyFile_Check(uwsgi.py_sendfile)) { 
    233                 //zero = PyFile_Name(uwsgi.py_sendfile) ; 
    234                 //fprintf(stderr,"->serving %s as static file...", PyString_AsString(zero)); 
    235                 wsgi_req.sendfile_fd = PyObject_AsFileDescriptor(uwsgi.py_sendfile); 
    236                 Py_INCREF(uwsgi.py_sendfile); 
    237         } 
    238 #endif 
    239  
    240  
    241         return PyTuple_New(0); 
     222        if (PyFile_Check (uwsgi.py_sendfile)) { 
     223                //zero = PyFile_Name(uwsgi.py_sendfile) ; 
     224                //fprintf(stderr,"->serving %s as static file...", PyString_AsString(zero)); 
     225                wsgi_req.sendfile_fd = PyObject_AsFileDescriptor (uwsgi.py_sendfile); 
     226                Py_INCREF (uwsgi.py_sendfile); 
     227        } 
     228#endif 
     229 
     230 
     231        return PyTuple_New (0); 
    242232} 
    243233#endif 
    244234 
    245 PyObject *py_uwsgi_write(PyObject *self, PyObject *args) { 
    246         PyObject *data; 
    247         char *content ; 
    248         int len; 
    249         data = PyTuple_GetItem(args, 0); 
    250         if (PyString_Check(data)) { 
    251                 content = PyString_AsString(data) ; 
    252                 len = PyString_Size(data); 
    253 #ifndef ROCK_SOLID 
    254                 if (uwsgi.has_threads && uwsgi.options[UWSGI_OPTION_THREADS] == 1) { 
    255                         Py_BEGIN_ALLOW_THREADS 
    256                         wsgi_req.response_size = write(uwsgi.poll.fd, content, len); 
    257                         Py_END_ALLOW_THREADS 
    258                 } 
    259                 else { 
    260 #endif 
    261                         wsgi_req.response_size = write(uwsgi.poll.fd, content, len); 
     235PyObject *py_uwsgi_write (PyObject * self, PyObject * args) { 
     236        PyObject *data; 
     237        char *content; 
     238        int len; 
     239        data = PyTuple_GetItem (args, 0); 
     240        if (PyString_Check (data)) { 
     241                content = PyString_AsString (data); 
     242                len = PyString_Size (data); 
     243#ifndef ROCK_SOLID 
     244                if (uwsgi.has_threads && uwsgi.options[UWSGI_OPTION_THREADS] == 1) { 
     245                        Py_BEGIN_ALLOW_THREADS wsgi_req.response_size = write (uwsgi.poll.fd, content, len); 
     246                Py_END_ALLOW_THREADS} 
     247                else { 
     248#endif 
     249                        wsgi_req.response_size = write (uwsgi.poll.fd, content, len); 
    262250#ifdef UNBIT 
    263251                        if (save_to_disk >= 0) { 
    264                                 if (write(save_to_disk, content, len) != len) { 
    265                                         perror("write()"); 
    266                                         close(save_to_disk); 
    267                                         save_to_disk = -1 ; 
    268                                 } 
    269                         } 
    270 #endif 
    271 #ifndef ROCK_SOLID 
    272                 } 
    273 #endif 
    274         } 
     252                                if (write (save_to_disk, content, len) != len) { 
     253                                        perror ("write()"); 
     254                                        close (save_to_disk); 
     255                                        save_to_disk = -1; 
     256                                } 
     257                        } 
     258#endif 
     259#ifndef ROCK_SOLID 
     260                } 
     261#endif 
     262        } 
    275263#ifdef UNBIT 
    276264        if (save_to_disk >= 0) { 
    277                 close(save_to_disk); 
    278                 save_to_disk = -1 ; 
    279                 fprintf(stderr,"[uWSGI cacher] output of request %llu (%.*s) on pid %d written to cache file %s\n",uwsgi.workers[0].requests, wsgi_req.uri_len, wsgi_req.uri, uwsgi.mypid,tmp_filename); 
    280         } 
    281 #endif 
    282         Py_INCREF(Py_None); 
    283         return Py_None; 
     265                close (save_to_disk); 
     266                save_to_disk = -1; 
     267                fprintf (stderr, "[uWSGI cacher] output of request %llu (%.*s) on pid %d written to cache file %s\n", uwsgi.workers[0].requests, wsgi_req.uri_len, wsgi_req.uri, uwsgi.mypid, tmp_filename); 
     268        } 
     269#endif 
     270        Py_INCREF (Py_None); 
     271        return Py_None; 
    284272} 
    285273 
     
    287275PyObject *wsgi_spitout; 
    288276 
    289 PyObject *py_uwsgi_spit(PyObject *self, PyObject *args) { 
    290         PyObject *headers, *head ; 
    291         PyObject *h_key, *h_value; 
    292         int i,j ; 
     277PyObject *py_uwsgi_spit (PyObject * self, PyObject * args) { 
     278        PyObject *headers, *head; 
     279        PyObject *h_key, *h_value; 
     280        int i, j; 
    293281#ifndef UNBIT 
    294282#ifdef ROCK_SOLID 
    295         int base = 4 ; 
     283        int base = 4; 
    296284#else 
    297         int base = 0 ; 
     285        int base = 0; 
    298286#endif 
    299287#else 
    300         int base = 4 ; 
    301 #endif 
    302  
    303         // use writev() 
    304  
    305  
    306         head = PyTuple_GetItem(args,0) ; 
     288        int base = 4; 
     289#endif 
     290 
     291        // use writev() 
     292 
     293 
     294        head = PyTuple_GetItem (args, 0); 
    307295        if (!head) { 
    308296                goto clear; 
    309297        } 
    310          
    311         if (!PyString_Check(head)) { 
    312                 fprintf(stderr,"http status must be a string !\n"); 
     298 
     299        if (!PyString_Check (head)) { 
     300                fprintf (stderr, "http status must be a string !\n"); 
    313301                goto clear; 
    314302        } 
     
    318306#ifndef ROCK_SOLID 
    319307        if (uwsgi.options[UWSGI_OPTION_CGI_MODE] == 0) { 
    320                 base = 4 ; 
     308                base = 4; 
    321309#endif 
    322310#endif 
     
    324312 
    325313                if (wsgi_req.protocol_len == 0) { 
    326                         hvec[0].iov_base = (char * )http_protocol ; 
    327                         wsgi_req.protocol_len = 8 ; 
     314                        uwsgi.hvec[0].iov_base = (char *) http_protocol; 
     315                        wsgi_req.protocol_len = 8; 
    328316                } 
    329317                else { 
    330                         hvec[0].iov_base = wsgi_req.protocol ; 
    331                 } 
    332  
    333                 hvec[0].iov_len = wsgi_req.protocol_len ; 
    334                 hvec[1].iov_base = " " ; 
    335                 hvec[1].iov_len = 1 ; 
     318                        uwsgi.hvec[0].iov_base = wsgi_req.protocol; 
     319                } 
     320 
     321                uwsgi.hvec[0].iov_len = wsgi_req.protocol_len; 
     322                uwsgi.hvec[1].iov_base = " "; 
     323                uwsgi.hvec[1].iov_len = 1; 
    336324#ifdef PYTHREE 
    337                 hvec[2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)) ; 
    338                 hvec[2].iov_len = strlen(hvec[2].iov_base); 
     325                uwsgi.hvec[2].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString (head)); 
     326                uwsgi.hvec[2].iov_len = strlen (uwsgi.hvec[2].iov_base); 
    339327#else 
    340                 hvec[2].iov_base = PyString_AsString(head) ; 
    341                 hvec[2].iov_len = PyString_Size(head) ; 
    342 #endif 
    343                 wsgi_req.status = atoi(hvec[2].iov_base) ; 
    344                 hvec[3].iov_base = nl ; 
    345                 hvec[3].iov_len = NL_SIZE ; 
     328                uwsgi.hvec[2].iov_base = PyString_AsString (head); 
     329                uwsgi.hvec[2].iov_len = PyString_Size (head); 
     330#endif 
     331                wsgi_req.status = atoi (uwsgi.hvec[2].iov_base); 
     332                uwsgi.hvec[3].iov_base = nl; 
     333                uwsgi.hvec[3].iov_len = NL_SIZE; 
    346334#ifndef UNBIT 
    347335#ifndef ROCK_SOLID 
     
    349337        else { 
    350338                // drop http status on cgi mode 
    351                 base = 3 ; 
    352                 hvec[0].iov_base = "Status: " ; 
    353                 hvec[0].iov_len = 8 ; 
     339                base = 3; 
     340                uwsgi.hvec[0].iov_base = "Status: "; 
     341                uwsgi.hvec[0].iov_len = 8; 
    354342#ifdef PYTHREE 
    355                 hvec[1].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)) ; 
    356                 hvec[1].iov_len = strlen(hvec[1].iov_base); 
     343                uwsgi.hvec[1].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString (head)); 
     344                uwsgi.hvec[1].iov_len = strlen (uwsgi.hvec[1].iov_base); 
    357345#else 
    358                 hvec[1].iov_base = PyString_AsString(head) ; 
    359                 hvec[1].iov_len = PyString_Size(head) ; 
    360 #endif 
    361                 wsgi_req.status = atoi(hvec[1].iov_base) ; 
    362                 hvec[2].iov_base = nl ; 
    363                 hvec[2].iov_len = NL_SIZE ; 
     346                uwsgi.hvec[1].iov_base = PyString_AsString (head); 
     347                uwsgi.hvec[1].iov_len = PyString_Size (head); 
     348#endif 
     349                wsgi_req.status = atoi (uwsgi.hvec[1].iov_base); 
     350                uwsgi.hvec[2].iov_base = nl; 
     351                uwsgi.hvec[2].iov_len = NL_SIZE; 
    364352        } 
    365353#endif 
     
    370358        if (wsgi_req.unbit_flags & (unsigned long long) 1) { 
    371359                if (tmp_dir_fd >= 0 && tmp_filename[0] != 0 && wsgi_req.status == 200 && wsgi_req.method_len == 3 && wsgi_req.method[0] == 'G' && wsgi_req.method[1] == 'E' && wsgi_req.method[2] == 'T') { 
    372                         save_to_disk = openat(tmp_dir_fd, tmp_filename,O_CREAT | O_TRUNC | O_WRONLY , S_IRUSR |S_IWUSR |S_IRGRP); 
    373                 } 
    374         } 
    375 #endif 
    376  
    377          
    378         headers = PyTuple_GetItem(args,1) ; 
     360                        save_to_disk = openat (tmp_dir_fd, tmp_filename, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP); 
     361                } 
     362        } 
     363#endif 
     364 
     365 
     366        headers = PyTuple_GetItem (args, 1); 
    379367        if (!headers) { 
    380368                goto clear; 
    381         }        
    382         if (!PyList_Check(headers)) { 
    383                 fprintf(stderr,"http headers must be in a python list\n"); 
     369        } 
     370        if (!PyList_Check (headers)) { 
     371                fprintf (stderr, "http headers must be in a python list\n"); 
    384372                goto clear; 
    385373        } 
    386         wsgi_req.header_cnt = PyList_Size(headers) ; 
    387  
    388  
    389  
    390         if (wsgi_req.header_cnt > uwsgi.max_vars) { 
    391                 wsgi_req.header_cnt = uwsgi.max_vars ; 
    392         } 
    393         for(i=0;i<wsgi_req.header_cnt;i++) { 
    394                 j = (i*4)+base ; 
    395                 head = PyList_GetItem(headers, i); 
     374        wsgi_req.header_cnt = PyList_Size (headers); 
     375 
     376 
     377 
     378        if (wsgi_req.header_cnt > uwsgi.max_vars) { 
     379                wsgi_req.header_cnt = uwsgi.max_vars; 
     380        } 
     381        for (i = 0; i < wsgi_req.header_cnt; i++) { 
     382                j = (i * 4) + base; 
     383                head = PyList_GetItem (headers, i); 
    396384                if (!head) { 
    397                         fprintf(stderr,"NON e' UNA LISTA\n"); 
    398385                        goto clear; 
    399386                } 
    400                 if (!PyTuple_Check(head)) { 
    401                         fprintf(stderr,"http header must be defined in a tuple !\n"); 
     387                if (!PyTuple_Check (head)) { 
     388                        fprintf (stderr, "http header must be defined in a tuple !\n"); 
    402389                        goto clear; 
    403390                } 
    404                 h_key = PyTuple_GetItem(head,0) ; 
    405                 if (!h_key) { fprintf(stderr,"NIENTE CHIAVE\n");goto clear; } 
    406                 h_value = PyTuple_GetItem(head,1) ; 
    407                 if (!h_value) { fprintf(stderr,"NIENTE VALORE\n");goto clear; } 
     391                h_key = PyTuple_GetItem (head, 0); 
     392                if (!h_key) { 
     393                        goto clear; 
     394                } 
     395                h_value = PyTuple_GetItem (head, 1); 
     396                if (!h_value) { 
     397                        goto clear; 
     398                } 
    408399#ifdef PYTHREE 
    409                 hvec[j].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_key)) ; 
    410                 hvec[j].iov_len = strlen(hvec[j].iov_base); 
     400                uwsgi.hvec[j].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString (h_key)); 
     401                uwsgi.hvec[j].iov_len = strlen (uwsgi.hvec[j].iov_base); 
    411402#else 
    412                 hvec[j].iov_base = PyString_AsString(h_key) ; 
    413                 hvec[j].iov_len = PyString_Size(h_key) ; 
    414 #endif 
    415                 hvec[j+1].iov_base = h_sep; 
    416                 hvec[j+1].iov_len = H_SEP_SIZE; 
     403                uwsgi.hvec[j].iov_base = PyString_AsString (h_key); 
     404                uwsgi.hvec[j].iov_len = PyString_Size (h_key); 
     405#endif 
     406                uwsgi.hvec[j + 1].iov_base = h_sep; 
     407                uwsgi.hvec[j + 1].iov_len = H_SEP_SIZE; 
    417408#ifdef PYTHREE 
    418                 hvec[j+2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_value)) ; 
    419                 hvec[j+2].iov_len = strlen(hvec[j+2].iov_base); 
     409                uwsgi.hvec[j + 2].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString (h_value)); 
     410                uwsgi.hvec[j + 2].iov_len = strlen (uwsgi.hvec[j + 2].iov_base); 
    420411#else 
    421                 hvec[j+2].iov_base = PyString_AsString(h_value) ; 
    422                 hvec[j+2].iov_len = PyString_Size(h_value) ; 
    423 #endif 
    424                 hvec[j+3].iov_base = nl; 
    425                 hvec[j+3].iov_len = NL_SIZE; 
    426                 //fprintf(stderr, "%.*s: %.*s\n", hvec[j].iov_len, (char *)hvec[j].iov_base, hvec[j+2].iov_len, (char *) hvec[j+2].iov_base); 
    427         } 
     412                uwsgi.hvec[j + 2].iov_base = PyString_AsString (h_value); 
     413                uwsgi.hvec[j + 2].iov_len = PyString_Size (h_value); 
     414#endif 
     415                uwsgi.hvec[j + 3].iov_base = nl; 
     416                uwsgi.hvec[j + 3].iov_len = NL_SIZE; 
     417                //fprintf(stderr, "%.*s: %.*s\n", uwsgi.hvec[j].iov_len, (char *)uwsgi.hvec[j].iov_base, uwsgi.hvec[j+2].iov_len, (char *) uwsgi.hvec[j+2].iov_base); 
     418        } 
    428419 
    429420 
    430421#ifdef UNBIT 
    431422        if (save_to_disk >= 0) { 
    432                 for(j=0;j<i;j+=4) { 
    433                         if (!strncasecmp(hvec[j].iov_base,"Set-Cookie",hvec[j].iov_len)) { 
    434                                 close(save_to_disk); 
    435                                 save_to_disk = -1 ; 
     423                for (j = 0; j < i; j += 4) { 
     424                        if (!strncasecmp (uwsgi.hvec[j].iov_base, "Set-Cookie", uwsgi.hvec[j].iov_len)) { 
     425                                close (save_to_disk); 
     426                                save_to_disk = -1; 
    436427                                break; 
    437428                        } 
     
    440431#endif 
    441432 
    442         // \r\n 
    443         j = (i*4)+base ; 
    444         hvec[j].iov_base = nl; 
    445         hvec[j].iov_len = NL_SIZE; 
    446  
    447         wsgi_req.headers_size = writev(uwsgi.poll.fd, hvec,j+1); 
     433        // \r\n 
     434        j = (i * 4) + base; 
     435        uwsgi.hvec[j].iov_base = nl; 
     436        uwsgi.hvec[j].iov_len = NL_SIZE; 
     437 
     438        wsgi_req.headers_size = writev (uwsgi.poll.fd, uwsgi.hvec, j + 1); 
    448439        if (wsgi_req.headers_size < 0) { 
    449                 perror("writev()"); 
    450         } 
    451         Py_INCREF(wsgi_writeout); 
    452  
    453  
    454         return wsgi_writeout ; 
    455  
    456 clear: 
    457  
    458         Py_INCREF(Py_None); 
     440                perror ("writev()"); 
     441        } 
     442        Py_INCREF (wsgi_writeout); 
     443 
     444 
     445        return wsgi_writeout; 
     446 
     447      clear: 
     448 
     449        Py_INCREF (Py_None); 
    459450        return Py_None; 
    460451} 
    461452 
    462453 
    463 PyMethodDef uwsgi_spit_method[] = {{"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""}} ; 
    464 PyMethodDef uwsgi_write_method[] = {{"uwsgi_write", py_uwsgi_write, METH_VARARGS, ""}} ; 
    465  
    466 #ifndef ROCK_SOLID 
    467 PyMethodDef uwsgi_sendfile_method[] = {{"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""}} ; 
     454PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} 
     455}; 
     456PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, METH_VARARGS, ""} 
     457}; 
     458 
     459#ifndef ROCK_SOLID 
     460PyMethodDef uwsgi_sendfile_method[] = { {"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""} 
     461}; 
    468462#endif 
    469463 
     
    479473#endif 
    480474 
    481 char *spool_dir = NULL ; 
    482  
    483 int main(int argc, char *argv[], char *envp[]) { 
    484  
    485         struct timeval check_interval = {.tv_sec = 1, .tv_usec = 0 }; 
    486          
     475char *spool_dir = NULL; 
     476 
     477int main (int argc, char *argv[], char *envp[]) { 
     478 
     479        struct timeval check_interval = {.tv_sec = 1,.tv_usec = 0 }; 
     480 
    487481#ifndef PYTHREE 
    488482#ifndef ROCK_SOLID 
    489483        PyObject *uwsgi_module; 
    490 #endif 
    491 #endif 
    492         PyObject *wsgi_result, *wsgi_chunks, *wchunk; 
    493         PyObject *zero, *wsgi_socket; 
    494 #ifndef ROCK_SOLID 
    495         PyThreadState *_save = NULL; 
    496 #endif 
    497  
    498         char *pyargv[MAX_PYARGV] ; 
    499         int pyargc = 1 ; 
    500  
    501         FILE *wsgi_file; 
    502         struct sockaddr_un c_addr ; 
    503         int c_len = sizeof(struct sockaddr_un); 
    504         int i ; 
     484 
     485#endif 
     486#endif 
     487        char *pyargv[MAX_PYARGV]; 
     488        int pyargc = 1; 
     489 
     490        struct sockaddr_un c_addr; 
     491        int c_len = sizeof (struct sockaddr_un); 
     492        int i; 
     493        int ret; 
    505494#ifndef ROCK_SOLID 
    506495        int rlen; 
    507         int i_have_gil = 1 ; 
    508 #endif 
    509         pid_t pid ; 
    510         int no_server = 0 ; 
    511  
    512         int serverfd = 0 ; 
    513 #ifndef UNBIT 
    514         char *socket_name = NULL ; 
     496#ifdef SCTP 
     497        int i_am_sctp = 0; 
     498        struct sctp_sndrcvinfo sctp_ss; 
     499#endif 
     500 
     501        char *udp_socket = NULL; 
     502        struct pollfd udp_poll; 
     503        struct sockaddr_in udp_client; 
     504        socklen_t udp_len; 
     505        char udp_client_addr[16]; 
     506#endif 
     507        pid_t pid; 
     508        int no_server = 0; 
     509 
     510        int serverfd = 0; 
     511#ifndef UNBIT 
     512        char *socket_name = NULL; 
    515513#ifndef ROCK_SOLID 
    516514        FILE *pidfile; 
     
    519517 
    520518#ifndef ROCK_SOLID 
    521         char spool_filename[1024]; 
    522         pid_t spooler_pid = 0 ; 
    523 #endif 
    524  
    525         int working_workers = 0 ; 
    526         int blocking_workers = 0 ; 
    527  
    528         char *cwd ; 
    529         char *binary_path ; 
     519        pid_t spooler_pid = 0; 
     520#ifndef UNBIT 
     521        char *snmp = NULL; 
     522        pid_t snmp_pid; 
     523#endif 
     524#endif 
     525 
     526        int working_workers = 0; 
     527        int blocking_workers = 0; 
     528 
     529        char *cwd; 
     530        char *binary_path; 
    530531        int ready_to_reload = 0; 
    531532        int ready_to_die = 0; 
    532          
    533         int is_a_reload = 0 ; 
    534  
    535         PyObject *pydictkey, *pydictvalue; 
    536  
    537         char *buffer ; 
    538         char *ptrbuf ; 
    539         char *bufferend ; 
    540  
    541         unsigned short strsize = 0; 
    542         struct uwsgi_app *wi; 
    543  
    544         struct rlimit rl ; 
     533 
     534        int is_a_reload = 0; 
     535 
     536        char *buffer; 
     537 
     538        struct rlimit rl; 
    545539 
    546540#ifdef UNBIT 
     
    548542#endif 
    549543 
    550         int socket_type;  
    551         socklen_t socket_type_len ;  
    552         char *path_info; 
     544        int socket_type; 
     545        socklen_t socket_type_len; 
    553546 
    554547        /* anti signal bombing */ 
    555         signal(SIGHUP, SIG_IGN); 
    556         signal(SIGTERM, SIG_IGN); 
    557  
    558         memset(&uwsgi, 0, sizeof(struct uwsgi_server)); 
     548        signal (SIGHUP, SIG_IGN); 
     549        signal (SIGTERM, SIG_IGN); 
     550 
     551        for(i=0;i<0xFF;i++) { 
     552                uwsgi.hooks[i] = NULL; 
     553                uwsgi.after_hooks[i] = NULL; 
     554        } 
     555        memset (&uwsgi, 0, sizeof (struct uwsgi_server)); 
    559556 
    560557        /* shared area for dynamic options */ 
    561         uwsgi.options = (uint32_t *) mmap(NULL, sizeof(uint32_t)*(0xFF+1), PROT_READ|PROT_WRITE , MAP_SHARED|MAP_ANON , -1, 0); 
     558        uwsgi.options = (uint32_t *) mmap (NULL, sizeof (uint32_t) * (0xFF + 1), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 
    562559        if (!uwsgi.options) { 
    563                 perror("mmap()"); 
    564                 exit(1); 
    565         } 
    566         memset(uwsgi.options, 0, sizeof(uint32_t)*(0xFF+1)); 
     560                perror ("mmap()"); 
     561                exit (1); 
     562        } 
     563        memset (uwsgi.options, 0, sizeof (uint32_t) * (0xFF + 1)); 
    567564 
    568565#ifndef ROCK_SOLID 
     
    575572        uwsgi.listen_queue = 64; 
    576573#endif 
    577          
    578         uwsgi.maxworkers = 64 ; 
    579          
    580         uwsgi.max_vars = MAX_VARS ; 
    581         uwsgi.vec_size = 4+1+(4*MAX_VARS) ; 
    582  
    583         uwsgi.options[UWSGI_OPTION_SOCKET_TIMEOUT] = 4 ; 
    584         uwsgi.options[UWSGI_OPTION_LOGGING] = 1 ; 
     574 
     575        uwsgi.maxworkers = 64; 
     576 
     577        uwsgi.max_vars = MAX_VARS; 
     578        uwsgi.vec_size = 4 + 1 + (4 * MAX_VARS); 
     579 
     580        uwsgi.options[UWSGI_OPTION_SOCKET_TIMEOUT] = 4; 
     581        uwsgi.options[UWSGI_OPTION_LOGGING] = 1; 
     582 
     583#ifndef ROCK_SOLID 
     584        uwsgi.i_have_gil = 1; 
     585#endif 
    585586 
    586587#ifndef UNBIT 
     
    627628                {"no-defer-accept", no_argument, &uwsgi.no_defer_accept, 1}, 
    628629                {"limit-as", required_argument, 0, LONG_ARGS_LIMIT_AS}, 
     630                {"udp", required_argument, 0, LONG_ARGS_UDP}, 
    629631                {"check-interval", required_argument, 0, LONG_ARGS_CHECK_INTERVAL}, 
    630632                {0, 0, 0, 0} 
     
    634636 
    635637 
    636         gettimeofday(&uwsgi.start_tv, NULL) ; 
    637  
    638         setlinebuf(stdout); 
    639  
    640         rl.rlim_cur = 0 ; 
    641         rl.rlim_max = 0 ; 
    642  
    643         cwd = uwsgi_get_cwd(); 
    644         binary_path = malloc(strlen(argv[0])+1) ; 
     638        gettimeofday (&uwsgi.start_tv, NULL); 
     639 
     640        setlinebuf (stdout); 
     641 
     642        rl.rlim_cur = 0; 
     643        rl.rlim_max = 0; 
     644 
     645        cwd = uwsgi_get_cwd (); 
     646        binary_path = malloc (strlen (argv[0]) + 1); 
    645647        if (binary_path == NULL) { 
    646                 perror("malloc()"); 
    647                 exit(1); 
    648         } 
    649         strcpy(binary_path, argv[0]); 
    650  
    651         socket_type_len = sizeof(int); 
    652         if (!getsockopt(3, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 
    653                 fprintf(stderr, "...fd 3 is a socket, i suppose this is a graceful reload of uWSGI, i will try to do my best...\n"); 
    654                 is_a_reload = 1 ; 
     648                perror ("malloc()"); 
     649                exit (1); 
     650        } 
     651        strcpy (binary_path, argv[0]); 
     652 
     653        socket_type_len = sizeof (int); 
     654        if (!getsockopt (3, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 
     655                fprintf (stderr, "...fd 3 is a socket, i suppose this is a graceful reload of uWSGI, i will try to do my best...\n"); 
     656                is_a_reload = 1; 
    655657#ifdef UNBIT 
    656658                /* discard the 3'th fd as we will use the fd 0 */ 
    657                 close(3); 
     659                close (3); 
    658660#else 
    659661                serverfd = 3; 
     
    662664 
    663665#ifndef UNBIT 
    664         #ifndef ROCK_SOLID 
    665         while ((i = getopt_long(argc, argv, "s:p:t:x:d:l:O:v:b:mcaCTPiMhrR:z:w:j:H:A:Q:L", long_options, &option_index)) != -1) { 
    666         #else 
    667         while ((i = getopt (argc, argv, "s:p:t:d:l:v:b:aCMhrR:z:j:H:A:L")) != -1) { 
    668         #endif 
     666#ifndef ROCK_SOLID 
     667        while ((i = getopt_long (argc, argv, "s:p:t:x:d:l:O:v:b:mcaCTPiMhrR:z:w:j:H:A:Q:L", long_options, &option_index)) != -1) { 
    669668#else 
    670         while ((i = getopt (argc, argv, "p:t:mTPiv:b:rMR:Sz:w:C:j:H:A:EQ:L")) != -1) { 
    671 #endif 
    672                 switch(i) { 
    673 #ifndef ROCK_SOLID 
    674 #ifndef UNBIT 
    675                         case LONG_ARGS_PIDFILE: 
    676                                 uwsgi.pidfile = optarg; 
    677                                 break; 
    678                         case LONG_ARGS_CHROOT: 
    679                                 uwsgi.chroot = optarg; 
    680                                 break; 
    681                         case LONG_ARGS_GID: 
    682                                 uwsgi.gid = atoi(optarg); 
    683                                 break; 
    684                         case LONG_ARGS_UID: 
    685                                 uwsgi.uid = atoi(optarg); 
    686                                 break; 
    687                         case LONG_ARGS_PYTHONPATH: 
    688                                 uwsgi.python_path[0] = optarg; 
    689                                 uwsgi.python_path_cnt = 1; 
    690                                 break; 
    691                         case LONG_ARGS_LIMIT_AS: 
    692                                 rl.rlim_cur = (atoi(optarg))*1024*1024; 
    693                                 rl.rlim_max = rl.rlim_cur; 
    694                                 break; 
    695 #endif 
    696                         case LONG_ARGS_PASTE: 
    697                                 uwsgi.single_interpreter = 1; 
    698                                 uwsgi.paste = optarg; 
    699                                 break; 
    700                         case LONG_ARGS_CHECK_INTERVAL: 
    701                                 uwsgi.options[UWSGI_OPTION_MASTER_INTERVAL] = atoi(optarg); 
    702                                 break; 
    703                         case LONG_ARGS_PYARGV: 
    704                                 uwsgi.pyargv = optarg; 
    705                                 break; 
    706 #endif 
    707                         case 'j': 
    708                                 uwsgi.test_module = optarg; 
    709                                 break; 
    710                         case 'H': 
    711                                 uwsgi.pyhome = optarg; 
    712                                 break; 
    713                         case 'A': 
    714                                 uwsgi.sharedareasize = atoi(optarg);     
    715                                 break; 
    716                         case 'L': 
    717                                 uwsgi.options[UWSGI_OPTION_LOGGING] = 0 ; 
    718                                 break; 
    719 #ifndef ROCK_SOLID 
    720                         case 'Q': 
    721                                 spool_dir = optarg; 
    722                                 if (access(spool_dir, R_OK|W_OK|X_OK)) { 
    723                                         perror("access()"); 
    724                                         exit(1); 
    725                                 } 
    726                                 uwsgi.master_process = 1; 
    727                                 break; 
     669        while ((i = getopt (argc, argv, "s:p:t:d:l:v:b:aCMhrR:z:j:H:A:L")) != -1) { 
     670#endif 
     671#else 
     672        while ((i = getopt (argc, argv, "p:t:mTPiv:b:rMR:Sz:w:C:j:H:A:EQ:L")) != -1) { 
     673#endif 
     674                switch (i) { 
     675#ifndef ROCK_SOLID 
     676#ifndef UNBIT 
     677                case LONG_ARGS_PIDFILE: 
     678                        uwsgi.pidfile = optarg; 
     679                        break; 
     680                case LONG_ARGS_UDP: 
     681                        udp_socket = optarg; 
     682                        uwsgi.master_process = 1; 
     683                        break; 
     684                case LONG_ARGS_CHROOT: 
     685                        uwsgi.chroot = optarg; 
     686                        break; 
     687                case LONG_ARGS_GID: 
     688                        uwsgi.gid = atoi (optarg); 
     689                        break; 
     690                case LONG_ARGS_UID: 
     691                        uwsgi.uid = atoi (optarg); 
     692                        break; 
     693                case LONG_ARGS_PYTHONPATH: 
     694                        uwsgi.python_path[0] = optarg; 
     695                        uwsgi.python_path_cnt = 1; 
     696                        break; 
     697                case LONG_ARGS_LIMIT_AS: 
     698                        rl.rlim_cur = (atoi (optarg)) * 1024 * 1024; 
     699                        rl.rlim_max = rl.rlim_cur; 
     700                        break; 
     701#endif 
     702                case LONG_ARGS_PASTE: 
     703                        uwsgi.single_interpreter = 1; 
     704                        uwsgi.paste = optarg; 
     705                        break; 
     706                case LONG_ARGS_CHECK_INTERVAL: 
     707                        uwsgi.options[UWSGI_OPTION_MASTER_INTERVAL] = atoi (optarg); 
     708                        break; 
     709                case LONG_ARGS_PYARGV: 
     710                        uwsgi.pyargv = optarg; 
     711                        break; 
     712#endif 
     713                case 'j': 
     714                        uwsgi.test_module = optarg; 
     715                        break; 
     716                case 'H': 
     717                        uwsgi.pyhome = optarg; 
     718                        break; 
     719                case 'A': 
     720                        uwsgi.sharedareasize = atoi (optarg); 
     721                        break; 
     722                case 'L': 
     723                        uwsgi.options[UWSGI_OPTION_LOGGING] = 0; 
     724                        break; 
     725#ifndef ROCK_SOLID 
     726                case 'Q': 
     727                        spool_dir = optarg; 
     728                        if (access (spool_dir, R_OK | W_OK | X_OK)) { 
     729                                perror ("access()"); 
     730                                exit (1); 
     731                        } 
     732                        uwsgi.master_process = 1; 
     733                        break; 
    728734#endif 
    729735#ifdef UNBIT 
    730                         case 'E': 
    731                                 check_for_memory_errors = 1 ; 
    732                                 break; 
    733                         case 'S': 
    734                                 uwsgi.single_interpreter = 1; 
    735                                 single_app_mode = 1; 
    736                                 uwsgi.default_app = 0; 
    737                                 break; 
    738                         case 'C': 
    739                                 tmp_dir_fd = open(optarg, O_DIRECTORY); 
    740                                 if (tmp_dir_fd <0) { 
    741                                         perror("open()"); 
    742                                         exit(1); 
    743                                 } 
    744                                 tmp_filename = malloc(8192); 
    745                                 if (!tmp_filename) { 
    746                                         fprintf(stderr,"unable to allocate space (8k) for tmp_filename\n"); 
    747                                         exit(1); 
    748                                 } 
    749                                 memset(tmp_filename, 0 ,8192); 
    750                                 break; 
    751 #endif 
    752 #ifndef UNBIT 
    753                         case 'd': 
    754                                 if (!is_a_reload) { 
    755                                         daemonize(optarg); 
    756                                 } 
    757                                 break; 
    758                         case 's': 
    759                                 socket_name = optarg; 
    760                                 break; 
    761 #ifndef ROCK_SOLID 
    762                         case 'x': 
    763                                 uwsgi.xml_config = optarg; 
    764                                 break; 
    765 #endif 
    766                         case 'l': 
    767                                 uwsgi.listen_queue = atoi(optarg); 
    768                                 break; 
    769 #endif 
    770                         case 'v': 
    771                                 uwsgi.max_vars = atoi(optarg); 
    772                                 uwsgi.vec_size = 4+1+(4*uwsgi.max_vars) ; 
    773                                 break; 
    774                         case 'p': 
    775                                 uwsgi.numproc = atoi(optarg); 
    776                                 break; 
    777                         case 'r': 
    778                                 uwsgi.options[UWSGI_OPTION_REAPER] = 1; 
    779                                 break; 
    780 #ifndef ROCK_SOLID 
    781                         case 'w': 
    782                                 uwsgi.single_interpreter = 1; 
    783                                 uwsgi.wsgi_config = optarg; 
    784                                 break; 
    785                         case 'm': 
    786                                 uwsgi.options[UWSGI_OPTION_MEMORY_DEBUG] = 1 ; 
    787                                 break; 
    788                         case 'O': 
    789                                 uwsgi.py_optimize = atoi(optarg) ; 
    790                                 break; 
    791 #endif 
    792                         case 't': 
    793                                 uwsgi.options[UWSGI_OPTION_HARAKIRI] = atoi(optarg); 
    794                                 break; 
    795                         case 'b': 
    796                                 uwsgi.buffer_size = atoi(optarg); 
    797                                 break; 
    798 #ifndef UNBIT 
    799 #ifndef ROCK_SOLID 
    800                         case 'c': 
    801                                 uwsgi.options[UWSGI_OPTION_CGI_MODE] = 1; 
    802                                 break; 
    803 #endif 
    804                         case 'a': 
    805                                 uwsgi.abstract_socket = 1; 
    806                                 break; 
    807                         case 'C': 
    808                                 uwsgi.chmod_socket = 1; 
    809                                 break; 
    810 #endif 
    811                         case 'M': 
    812                                 uwsgi.master_process = 1; 
    813                                 break; 
    814                         case 'R': 
    815                                 uwsgi.options[UWSGI_OPTION_MAX_REQUESTS] = atoi(optarg); 
    816                                 break; 
    817                         case 'z': 
    818                                 if (atoi(optarg) > 0) { 
    819                                         uwsgi.options[UWSGI_OPTION_SOCKET_TIMEOUT] = atoi(optarg) ; 
    820                                 } 
    821                                 break; 
    822 #ifndef ROCK_SOLID 
    823                         case 'T': 
    824                                 uwsgi.has_threads = 1; 
    825                                 uwsgi.options[UWSGI_OPTION_THREADS] = 1 ; 
    826                                 break; 
    827                         case 'P': 
    828                                 uwsgi.enable_profiler = 1; 
    829                                 break; 
    830                         case 'i': 
    831                                 uwsgi.single_interpreter = 1; 
    832                                 break; 
    833 #endif 
    834 #ifndef UNBIT 
    835                         case 'h': 
    836                                 fprintf(stderr, "Usage: %s [options...]\n\ 
     736                case 'E': 
     737                        check_for_memory_errors = 1; 
     738                        break; 
     739                case 'S': 
     740                        uwsgi.single_interpreter = 1; 
     741                        single_app_mode = 1; 
     742                        uwsgi.default_app = 0; 
     743                        break; 
     744                case 'C': 
     745                        tmp_dir_fd = open (optarg, O_DIRECTORY); 
     746                        if (tmp_dir_fd < 0) { 
     747                                perror ("open()"); 
     748                                exit (1); 
     749                        } 
     750                        tmp_filename = malloc (8192); 
     751                        if (!tmp_filename) { 
     752                                fprintf (stderr, "unable to allocate space (8k) for tmp_filename\n"); 
     753                                exit (1); 
     754                        } 
     755                        memset (tmp_filename, 0, 8192); 
     756                        break; 
     757#endif 
     758#ifndef UNBIT 
     759                case 'd': 
     760                        if (!is_a_reload) { 
     761                                daemonize (optarg); 
     762                        } 
     763                        break; 
     764                case 's': 
     765                        socket_name = optarg; 
     766                        break; 
     767#ifndef ROCK_SOLID 
     768                case 'x': 
     769                        uwsgi.xml_config = optarg; 
     770                        break; 
     771#endif 
     772                case 'l': 
     773                        uwsgi.listen_queue = atoi (optarg); 
     774                        break; 
     775#endif 
     776                case 'v': 
     777                        uwsgi.max_vars = atoi (optarg); 
     778                        uwsgi.vec_size = 4 + 1 + (4 * uwsgi.max_vars); 
     779                        break; 
     780                case 'p': 
     781                        uwsgi.numproc = atoi (optarg); 
     782                        break; 
     783                case 'r': 
     784                        uwsgi.options[UWSGI_OPTION_REAPER] = 1; 
     785                        break; 
     786#ifndef ROCK_SOLID 
     787                case 'w': 
     788                        uwsgi.single_interpreter = 1; 
     789                        uwsgi.wsgi_config = optarg; 
     790                        break; 
     791                case 'm': 
     792                        uwsgi.options[UWSGI_OPTION_MEMORY_DEBUG] = 1; 
     793                        break; 
     794                case 'O': 
     795                        uwsgi.py_optimize = atoi (optarg); 
     796                        break; 
     797#endif 
     798                case 't': 
     799                        uwsgi.options[UWSGI_OPTION_HARAKIRI] = atoi (optarg); 
     800                        break; 
     801                case 'b': 
     802                        uwsgi.buffer_size = atoi (optarg); 
     803                        break; 
     804#ifndef UNBIT 
     805#ifndef ROCK_SOLID 
     806                case 'c': 
     807                        uwsgi.options[UWSGI_OPTION_CGI_MODE] = 1; 
     808                        break; 
     809#endif 
     810                case 'a': 
     811                        uwsgi.abstract_socket = 1; 
     812                        break; 
     813                case 'C': 
     814                        uwsgi.chmod_socket = 1; 
     815                        break; 
     816#endif 
     817                case 'M': 
     818                        uwsgi.master_process = 1; 
     819                        break; 
     820                case 'R': 
     821                        uwsgi.options[UWSGI_OPTION_MAX_REQUESTS] = atoi (optarg); 
     822                        break; 
     823                case 'z': 
     824                        if (atoi (optarg) > 0) { 
     825                                uwsgi.options[UWSGI_OPTION_SOCKET_TIMEOUT] = atoi (optarg); 
     826                        } 
     827                        break; 
     828#ifndef ROCK_SOLID 
     829                case 'T': 
     830                        uwsgi.has_threads = 1; 
     831                        uwsgi.options[UWSGI_OPTION_THREADS] = 1; 
     832                        break; 
     833                case 'P': 
     834                        uwsgi.enable_profiler = 1; 
     835                        break; 
     836                case 'i': 
     837                        uwsgi.single_interpreter = 1; 
     838                        break; 
     839#endif 
     840#ifndef UNBIT 
     841                case 'h': 
     842                        fprintf (stderr, "Usage: %s [options...]\n\ 
    837843\t-s|--socket <name>\t\tpath (or name) of UNIX/TCP socket to bind to\n\ 
    838844\t-l|--listen <num>\t\tset socket listen queue to <n> (default 64, maximum is system dependent)\n\ 
     
    873879\t--pyargv <args>\t\t\tassign args to python sys.argv\n\ 
    874880\t--limit-as <MB>\t\t\tlimit the address space of processes to MB megabytes\n\ 
     881\t--udp <ip:port>\t\t\tbind master process to udp socket on ip:port\n\ 
    875882\t-d|--daemonize <logfile>\tdaemonize and log into <logfile>\n", argv[0]); 
    876                                 exit(1); 
    877                         case 0: 
    878                                 break; 
    879                         default: 
    880                                 exit(1); 
    881 #endif 
    882                 } 
    883         } 
     883                        exit (1); 
     884                case 0: 
     885                        break; 
     886                default: 
     887                        exit (1); 
     888#endif 
     889                } 
     890        } 
    884891 
    885892#ifndef UNBIT 
     
    889896#endif 
    890897                if (uwsgi.test_module == NULL) { 
    891                         fprintf(stderr,"*** Starting uWSGI (%dbit) on [%.*s] ***\n", (int) (sizeof(void *))*8, 24, ctime( (const time_t *) &uwsgi.start_tv.tv_sec)); 
     898                        fprintf (stderr, "*** Starting uWSGI (%dbit) on [%.*s] ***\n", (int) (sizeof (void *)) * 8, 24, ctime ((const time_t *) &uwsgi.start_tv.tv_sec)); 
    892899                } 
    893900#ifndef UNBIT 
     
    895902        } 
    896903        else { 
    897                 fprintf(stderr,"*** Starting uWSGI (CGI mode) (%dbit) on [%.*s] ***\n", (int) (sizeof(void *))*8, 24, ctime( (const time_t *) &uwsgi.start_tv.tv_sec)); 
     904                fprintf (stderr, "*** Starting uWSGI (CGI mode) (%dbit) on [%.*s] ***\n", (int) (sizeof (void *)) * 8, 24, ctime ((const time_t *) &uwsgi.start_tv.tv_sec)); 
    898905        } 
    899906#endif 
     
    901908 
    902909#ifdef __BIG_ENDIAN__ 
    903         fprintf(stderr,"*** big endian arch detected ***\n"); 
     910        fprintf (stderr, "*** big endian arch detected ***\n"); 
    904911#endif 
    905912 
    906913#ifdef PYTHREE 
    907         fprintf(stderr,"*** Warning Python3.x support is experimental, do not use it in production environment ***\n"); 
    908 #endif 
    909  
    910         fprintf(stderr,"Python version: %s\n", Py_GetVersion()); 
    911  
    912 #ifndef UNBIT 
    913         if (!getuid()) { 
    914                 fprintf(stderr,"uWSGI running as root, you can use --uid/--gid/--chroot options\n"); 
     914        fprintf (stderr, "*** Warning Python3.x support is experimental, do not use it in production environment ***\n"); 
     915#endif 
     916 
     917        fprintf (stderr, "Python version: %s\n", Py_GetVersion ()); 
     918 
     919#ifndef UNBIT 
     920        if (!getuid ()) { 
     921                fprintf (stderr, "uWSGI running as root, you can use --uid/--gid/--chroot options\n"); 
    915922                if (uwsgi.chroot) { 
    916                         fprintf(stderr,"chroot() to %s\n", uwsgi.chroot); 
    917                         if (chroot(uwsgi.chroot)) { 
    918                                 perror("chroot()"); 
    919                                 exit(1); 
     923                        fprintf (stderr, "chroot() to %s\n", uwsgi.chroot); 
     924                        if (chroot (uwsgi.chroot)) { 
     925                                perror ("chroot()"); 
     926                                exit (1); 
    920927                        } 
    921928#ifdef __linux__ 
    922929                        if (uwsgi.options[UWSGI_OPTION_MEMORY_DEBUG]) { 
    923                                 fprintf(stderr,"*** Warning, on linux system you have to bind-mount the /proc fs in your chroot to get memory debug/report.\n"); 
    924                         }                        
     930                                fprintf (stderr, "*** Warning, on linux system you have to bind-mount the /proc fs in your chroot to get memory debug/report.\n"); 
     931                        } 
    925932#endif 
    926933                } 
    927934                if (uwsgi.gid) { 
    928                         fprintf(stderr,"setgid() to %d\n", uwsgi.gid); 
    929                         if (setgid(uwsgi.gid)) { 
    930                                 perror("setgid()"); 
    931                                 exit(1); 
     935                        fprintf (stderr, "setgid() to %d\n", uwsgi.gid); 
     936                        if (setgid (uwsgi.gid)) { 
     937                                perror ("setgid()"); 
     938                                exit (1); 
    932939                        } 
    933940                } 
    934941                if (uwsgi.uid) { 
    935                         fprintf(stderr,"setuid() to %d\n", uwsgi.uid); 
    936                         if (setuid(uwsgi.uid)) { 
    937                                 perror("setuid()"); 
    938                                 exit(1); 
     942                        fprintf (stderr, "setuid() to %d\n", uwsgi.uid); 
     943                        if (setuid (uwsgi.uid)) { 
     944                                perror ("setuid()"); 
     945                                exit (1); 
    939946                        } 
    940947                } 
    941948        } 
    942949        else { 
    943                 if (uwsgi.chroot) { fprintf(stderr,"cannot chroot() as non-root user\n"); exit(1); } 
    944                 if (uwsgi.gid) { fprintf(stderr,"cannot setgid() as non-root user\n"); exit(1); } 
    945                 if (uwsgi.uid) { fprintf(stderr,"cannot setuid() as non-root user\n"); exit(1); } 
    946         } 
    947  
    948 #endif 
    949  
     950                if (uwsgi.chroot) { 
     951                        fprintf (stderr, "cannot chroot() as non-root user\n"); 
     952                        exit (1); 
     953                } 
     954                if (uwsgi.gid) { 
     955                        fprintf (stderr, "cannot setgid() as non-root user\n"); 
     956                        exit (1); 
     957                } 
     958                if (uwsgi.uid) { 
     959                        fprintf (stderr, "cannot setuid() as non-root user\n"); 
     960                        exit (1); 
     961                } 
     962        } 
     963 
     964#endif 
     965 
     966#ifndef UNBIT 
    950967        if (rl.rlim_max > 0) { 
    951                 fprintf(stderr,"limiting address space of processes...\n"); 
    952                 if (setrlimit(RLIMIT_AS, &rl)) { 
    953                         perror("setrlimit()"); 
    954                 } 
    955         } 
    956          
    957         if (!getrlimit(RLIMIT_AS, &rl)) { 
     968                fprintf (stderr, "limiting address space of processes...\n"); 
     969                if (setrlimit (RLIMIT_AS, &rl)) { 
     970                        perror ("setrlimit()"); 
     971                } 
     972        } 
     973#endif 
     974 
     975        if (!getrlimit (RLIMIT_AS, &rl)) { 
     976#ifndef UNBIT 
    958977                // check for overflow 
    959                 if ( (sizeof(void *) == 4 && (uint32_t) rl.rlim_max < UINT32_MAX) || (sizeof(void *) == 8 && (uint64_t) rl.rlim_max < UINT64_MAX)) { 
    960                         fprintf(stderr,"your process address space limit is %lld bytes (%lld MB)\n", (long long) rl.rlim_max, (long long) rl.rlim_max/1024/1024); 
    961                 } 
    962         } 
    963  
    964         uwsgi.page_size = getpagesize(); 
    965         fprintf(stderr,"your memory page size is %d bytes\n", uwsgi.page_size); 
    966  
    967 #ifndef UNBIT 
    968         fprintf(stderr,"your server socket listen backlog is limited to %d connections\n", uwsgi.listen_queue); 
     978                if ((sizeof (void *) == 4 && (uint32_t) rl.rlim_max < UINT32_MAX) || (sizeof (void *) == 8 && (uint64_t) rl.rlim_max < UINT64_MAX)) { 
     979#endif 
     980                        fprintf (stderr, "your process address space limit is %lld bytes (%lld MB)\n", (long long) rl.rlim_max, (long long) rl.rlim_max / 1024 / 1024); 
     981#ifndef UNBIT 
     982                } 
     983#endif 
     984        } 
     985 
     986        uwsgi.page_size = getpagesize (); 
     987        fprintf (stderr, "your memory page size is %d bytes\n", uwsgi.page_size); 
     988 
     989#ifndef UNBIT 
     990        fprintf (stderr, "your server socket listen backlog is limited to %d connections\n", uwsgi.listen_queue); 
    969991#endif 
    970992 
    971993        if (uwsgi.synclog) { 
    972                 fprintf(stderr,"allocating a memory page for synced logging.\n"); 
    973                 uwsgi.sync_page = malloc(uwsgi.page_size); 
     994                fprintf (stderr, "allocating a memory page for synced logging.\n"); 
     995                uwsgi.sync_page = malloc (uwsgi.page_size); 
    974996                if (!uwsgi.sync_page) { 
    975                         perror("malloc()"); 
    976                         exit(1); 
     997                        perror ("malloc()"); 
     998                        exit (1); 
    977999                } 
    9781000        } 
    9791001 
    9801002        if (uwsgi.pyhome != NULL) { 
    981                 fprintf(stderr,"Setting PythonHome to %s...\n", uwsgi.pyhome); 
     1003                fprintf (stderr, "Setting PythonHome to %s...\n", uwsgi.pyhome); 
    9821004#ifdef PYTHREE 
    983                 wchar_t *wpyhome ; 
    984                 wpyhome = malloc((sizeof(wchar_t)*strlen(uwsgi.pyhome))+2) ; 
     1005                wchar_t *wpyhome; 
     1006                wpyhome = malloc ((sizeof (wchar_t) * strlen (uwsgi.pyhome)) + 2); 
    9851007                if (!wpyhome) { 
    986                         perror("malloc()"); 
    987                         exit(1); 
    988                 } 
    989                 mbstowcs(wpyhome, uwsgi.pyhome, strlen(uwsgi.pyhome)); 
    990                 Py_SetPythonHome(wpyhome);               
    991                 free(wpyhome); 
     1008                        perror ("malloc()"); 
     1009                        exit (1); 
     1010                } 
     1011                mbstowcs (wpyhome, uwsgi.pyhome, strlen (uwsgi.pyhome)); 
     1012                Py_SetPythonHome (wpyhome); 
     1013                free (wpyhome); 
    9921014#else 
    993                 Py_SetPythonHome(uwsgi.pyhome);          
    994 #endif 
    995         } 
    996  
    997  
    998          
     1015                Py_SetPythonHome (uwsgi.pyhome); 
     1016#endif 
     1017        } 
     1018 
     1019 
     1020 
    9991021 
    10001022#ifdef PYTHREE 
    1001         wchar_t pname[6] ; 
    1002         mbstowcs(pname, "uWSGI", 6); 
    1003         Py_SetProgramName(pname); 
     1023        wchar_t pname[6]; 
     1024        mbstowcs (pname, "uWSGI", 6); 
     1025        Py_SetProgramName (pname); 
    10041026#else 
    1005         Py_SetProgramName("uWSGI"); 
    1006 #endif 
    1007  
    1008  
    1009         Py_Initialize() ; 
    1010  
    1011         pyargv[0] = "uwsgi" ; 
     1027        Py_SetProgramName ("uWSGI"); 
     1028#endif 
     1029 
     1030 
     1031        Py_Initialize (); 
     1032 
     1033        pyargv[0] = "uwsgi"; 
    10121034 
    10131035        if (uwsgi.pyargv != NULL) { 
    10141036                char *ap; 
    1015                 while( (ap = strsep(&uwsgi.pyargv, " \t")) != NULL) { 
     1037                while ((ap = strsep (&uwsgi.pyargv, " \t")) != NULL) { 
    10161038                        if (*ap != '\0') { 
    1017                                 pyargv[pyargc] = ap ;                    
     1039                                pyargv[pyargc] = ap; 
    10181040                                pyargc++; 
    10191041                        } 
    1020                         if (pyargc+1 > MAX_PYARGV) 
     1042                        if (pyargc + 1 > MAX_PYARGV) 
    10211043                                break; 
    10221044                } 
    10231045        } 
    10241046 
    1025         PySys_SetArgv(pyargc, pyargv); 
    1026  
    1027  
    1028 #ifndef ROCK_SOLID 
    1029         uwsgi.py_apps = PyDict_New(); 
    1030         if (!uwsgi.py_apps) { 
    1031                 PyErr_Print(); 
    1032                 exit(1); 
    1033         } 
    1034  
    1035 #endif 
    1036  
    1037  
    1038         wsgi_spitout = PyCFunction_New(uwsgi_spit_method,NULL) ; 
    1039         wsgi_writeout = PyCFunction_New(uwsgi_write_method,NULL) ; 
     1047        PySys_SetArgv (pyargc, pyargv); 
     1048 
     1049 
     1050#ifndef ROCK_SOLID 
     1051        uwsgi.py_apps = PyDict_New (); 
     1052        if (!uwsgi.py_apps) { 
     1053                PyErr_Print (); 
     1054                exit (1); 
     1055        } 
     1056 
     1057#endif 
     1058 
     1059 
     1060        wsgi_spitout = PyCFunction_New (uwsgi_spit_method, NULL); 
     1061        wsgi_writeout = PyCFunction_New (uwsgi_write_method, NULL); 
    10401062 
    10411063#ifndef PYTHREE 
    10421064#ifndef ROCK_SOLID 
    1043         uwsgi_module = Py_InitModule("uwsgi", null_methods); 
    1044         if (uwsgi_module == NULL) { 
    1045                 fprintf(stderr,"could not initialize the uwsgi python module\n"); 
    1046                 exit(1); 
     1065        uwsgi_module = Py_InitModule ("uwsgi", null_methods); 
     1066        if (uwsgi_module == NULL) { 
     1067                fprintf (stderr, "could not initialize the uwsgi python module\n"); 
     1068                exit (1); 
    10471069        } 
    10481070        if (uwsgi.sharedareasize > 0) { 
    1049                 #ifndef __OpenBSD__ 
    1050                 uwsgi.sharedareamutex = mmap(NULL, sizeof(pthread_mutexattr_t) + sizeof(pthread_mutex_t), PROT_READ|PROT_WRITE , MAP_SHARED|MAP_ANON , -1, 0); 
     1071#ifndef __OpenBSD__ 
     1072                uwsgi.sharedareamutex = mmap (NULL, sizeof (pthread_mutexattr_t) + sizeof (pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 
    10511073                if (!uwsgi.sharedareamutex) { 
    1052                         perror("mmap()"); 
    1053                         exit(1); 
    1054                 } 
    1055                 #else 
    1056                         fprintf(stderr,"***WARNING*** the sharedarea on OpenBSD is not SMP-safe. Beware of race conditions !!!\n"); 
    1057                 #endif 
    1058                 uwsgi.sharedarea = mmap(NULL, uwsgi.page_size * uwsgi.sharedareasize, PROT_READ|PROT_WRITE , MAP_SHARED|MAP_ANON , -1, 0); 
    1059                 if (uwsgi.sharedarea) {  
    1060                         fprintf(stderr,"shared area mapped at %p, you can access it with uwsgi.sharedarea* functions.\n", uwsgi.sharedarea); 
     1074                        perror ("mmap()"); 
     1075                        exit (1); 
     1076                } 
     1077#else 
     1078                fprintf (stderr, "***WARNING*** the sharedarea on OpenBSD is not SMP-safe. Beware of race conditions !!!\n"); 
     1079#endif 
     1080                uwsgi.sharedarea = mmap (NULL, uwsgi.page_size * uwsgi.sharedareasize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 
     1081                if (uwsgi.sharedarea) { 
     1082                        fprintf (stderr, "shared area mapped at %p, you can access it with uwsgi.sharedarea* functions.\n", uwsgi.sharedarea); 
    10611083 
    10621084#ifdef __APPLE__ 
    1063                         memset(uwsgi.sharedareamutex,0, sizeof(OSSpinLock)); 
     1085                        memset (uwsgi.sharedareamutex, 0, sizeof (OSSpinLock)); 
    10641086#else 
    1065                 #ifndef __OpenBSD__ 
    1066                         if (pthread_mutexattr_init((pthread_mutexattr_t *)uwsgi.sharedareamutex)) { 
    1067                                 fprintf(stderr,"unable to allocate mutexattr structure\n"); 
    1068                                 exit(1); 
    1069                         } 
    1070                         if (pthread_mutexattr_setpshared((pthread_mutexattr_t *)uwsgi.sharedareamutex, PTHREAD_PROCESS_SHARED)) { 
    1071                                 fprintf(stderr,"unable to share mutex\n"); 
    1072                                 exit(1); 
    1073                         } 
    1074                         if (pthread_mutex_init((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t), (pthread_mutexattr_t *)uwsgi.sharedareamutex)) { 
    1075                                 fprintf(stderr,"unable to initialize mutex\n"); 
    1076                                 exit(1); 
    1077                         } 
    1078                 #endif 
    1079 #endif 
    1080                          
     1087#ifndef __OpenBSD__ 
     1088                        if (pthread_mutexattr_init ((pthread_mutexattr_t *) uwsgi.sharedareamutex)) { 
     1089                                fprintf (stderr, "unable to allocate mutexattr structure\n"); 
     1090                                exit (1); 
     1091                        } 
     1092                        if (pthread_mutexattr_setpshared ((pthread_mutexattr_t *) uwsgi.sharedareamutex, PTHREAD_PROCESS_SHARED)) { 
     1093                                fprintf (stderr, "unable to share mutex\n"); 
     1094                                exit (1); 
     1095                        } 
     1096                        if (pthread_mutex_init ((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof (pthread_mutexattr_t), (pthread_mutexattr_t *) uwsgi.sharedareamutex)) { 
     1097                                fprintf (stderr, "unable to initialize mutex\n"); 
     1098                                exit (1); 
     1099                        } 
     1100#endif 
     1101#endif 
     1102 
    10811103                } 
    10821104                else { 
    1083                         perror("mmap()"); 
    1084                         exit(1); 
    1085                 } 
    1086  
    1087         } 
    1088  
    1089         init_uwsgi_embedded_module(); 
     1105                        perror ("mmap()"); 
     1106                        exit (1); 
     1107                } 
     1108 
     1109        } 
     1110 
     1111        init_uwsgi_embedded_module (); 
    10901112#endif 
    10911113#endif 
     
    10961118 
    10971119 
    1098         wi = malloc(sizeof(struct uwsgi_app)); 
     1120        wi = malloc (sizeof (struct uwsgi_app)); 
    10991121        if (wi == NULL) { 
    1100                 perror("malloc()"); 
    1101                 exit(1); 
    1102         } 
    1103         memset(wi, 0, sizeof(struct uwsgi_app)); 
     1122                perror ("malloc()"); 
     1123                exit (1); 
     1124        } 
     1125        memset (wi, 0, sizeof (struct uwsgi_app)); 
    11041126 
    11051127        // load wsgi module/script 
    11061128        for (i = optind; i < argc; i++) { 
    1107                 init_uwsgi_vars(); 
    1108                 wi->wsgi_module = PyImport_ImportModule(argv[i]); 
     1129                init_uwsgi_vars (); 
     1130                wi->wsgi_module = PyImport_ImportModule (argv[i]); 
    11091131                if (!wi->wsgi_module) { 
    1110                         PyErr_Print(); 
    1111                         exit(1); 
    1112                 } 
    1113                 wi->wsgi_dict = PyModule_GetDict(wi->wsgi_module); 
     1132                        PyErr_Print (); 
     1133                        exit (1); 
     1134                } 
     1135                wi->wsgi_dict = PyModule_GetDict (wi->wsgi_module); 
    11141136                if (!wi->wsgi_dict) { 
    1115                         PyErr_Print(); 
    1116                         exit(1); 
    1117                 } 
    1118                 wi->wsgi_callable = PyDict_GetItemString(wi->wsgi_dict, "application"); 
     1137                        PyErr_Print (); 
     1138                        exit (1); 
     1139                } 
     1140                wi->wsgi_callable = PyDict_GetItemString (wi->wsgi_dict, "application"); 
    11191141                if (!wi->wsgi_callable) { 
    1120                         PyErr_Print(); 
    1121                         exit(1); 
    1122                 } 
    1123                 wi->wsgi_environ = PyDict_New(); 
     1142                        PyErr_Print (); 
     1143                        exit (1); 
     1144                } 
     1145                wi->wsgi_environ = PyDict_New (); 
    11241146                if (!wi->wsgi_environ) { 
    1125                         PyErr_Print(); 
    1126                         exit(1); 
    1127                 } 
    1128  
    1129                 wi->wsgi_harakiri = PyDict_GetItemString(wi->wsgi_dict, "harakiri"); 
    1130  
    1131                 wi->wsgi_args = PyTuple_New(2) ; 
     1147                        PyErr_Print (); 
     1148                        exit (1); 
     1149                } 
     1150 
     1151                wi->wsgi_harakiri = PyDict_GetItemString (wi->wsgi_dict, "harakiri"); 
     1152 
     1153                wi->wsgi_args = PyTuple_New (2); 
    11321154                if (!wi->wsgi_args) { 
    1133                         PyErr_Print(); 
    1134                         exit(1); 
    1135                 } 
    1136                 if (PyTuple_SetItem(wi->wsgi_args,0, wi->wsgi_environ)) { 
    1137                         PyErr_Print(); 
    1138                         exit(1); 
    1139                 } 
    1140                 if (PyTuple_SetItem(wi->wsgi_args,1, wsgi_spitout)) { 
    1141                         PyErr_Print(); 
    1142                         exit(1); 
    1143                 } 
    1144                 break;   
     1155                        PyErr_Print (); 
     1156                        exit (1); 
     1157                } 
     1158                if (PyTuple_SetItem (wi->wsgi_args, 0, wi->wsgi_environ)) { 
     1159                        PyErr_Print (); 
     1160                        exit (1); 
     1161                } 
     1162                if (PyTuple_SetItem (wi->wsgi_args, 1, wsgi_spitout)) { 
     1163                        PyErr_Print (); 
     1164                        exit (1); 
     1165                } 
     1166                break; 
    11451167        } 
    11461168 
    11471169        if (!wi->wsgi_module) { 
    1148                 fprintf(stderr,"unable to find the wsgi script. Have you specified it ?\n"); 
    1149                 exit(1); 
     1170                fprintf (stderr, "unable to find the wsgi script. Have you specified it ?\n"); 
     1171                exit (1); 
    11501172        } 
    11511173#endif 
     
    11541176        Py_OptimizeFlag = uwsgi.py_optimize; 
    11551177 
    1156         uwsgi.main_thread = PyThreadState_Get(); 
    1157  
    1158  
    1159         if (uwsgi.has_threads) { 
    1160                 PyEval_InitThreads() ; 
    1161                 fprintf(stderr, "threads support enabled\n"); 
    1162         } 
     1178        uwsgi.main_thread = PyThreadState_Get (); 
     1179 
     1180 
     1181        if (uwsgi.has_threads) { 
     1182                PyEval_InitThreads (); 
     1183                fprintf (stderr, "threads support enabled\n"); 
     1184        } 
    11631185 
    11641186#endif 
     
    11661188        if (!no_server) { 
    11671189#ifndef UNBIT 
    1168         if (socket_name != NULL && !is_a_reload) { 
    1169                 char *tcp_port = strchr(socket_name, ':'); 
    1170                 if (tcp_port == NULL) { 
    1171                         serverfd = bind_to_unix(socket_name, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket); 
    1172                 } 
    1173                 else { 
    1174                         serverfd = bind_to_tcp(socket_name, uwsgi.listen_queue, tcp_port); 
    1175                 } 
    1176  
    1177                 if (serverfd < 0) { 
    1178                         fprintf(stderr,"unable to create the server socket.\n"); 
    1179                         exit(1); 
    1180                 } 
    1181         } 
    1182 #endif 
    1183  
    1184         socket_type_len = sizeof(int); 
    1185         if (getsockopt(serverfd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 
    1186                 perror("getsockopt()"); 
    1187                 exit(1); 
    1188         } 
     1190                if (socket_name != NULL && !is_a_reload) { 
     1191#ifdef SCTP 
     1192                        if (!strncmp (socket_name, "sctp:", 5)) { 
     1193                                char *sctp_port = strchr (socket_name + 5, ':'); 
     1194                                if (sctp_port == NULL) { 
     1195                                        fprintf (stderr, "invalid SCTP port ! syntax: sctp:ip1,ip2,ipN:port\n"); 
     1196                                        exit (1); 
     1197                                } 
     1198                                serverfd = bind_to_sctp (socket_name + 5, uwsgi.listen_queue, sctp_port); 
     1199                                i_am_sctp = 1; 
     1200                        } 
     1201                        else { 
     1202#endif 
     1203                                char *tcp_port = strchr (socket_name, ':'); 
     1204                                if (tcp_port == NULL) { 
     1205                                        serverfd = bind_to_unix (socket_name, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket); 
     1206                                } 
     1207                                else { 
     1208                                        serverfd = bind_to_tcp (socket_name, uwsgi.listen_queue, tcp_port); 
     1209                                } 
     1210 
     1211                                if (serverfd < 0) { 
     1212                                        fprintf (stderr, "unable to create the server socket.\n"); 
     1213                                        exit (1); 
     1214                                } 
     1215#ifdef SCTP 
     1216                        } 
     1217#endif 
     1218                } 
     1219#endif 
     1220 
     1221                socket_type_len = sizeof (int); 
     1222                if (getsockopt (serverfd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 
     1223                        perror ("getsockopt()"); 
     1224                        exit (1); 
     1225                } 
    11891226 
    11901227        } 
     
    11941231#ifndef ROCK_SOLID 
    11951232        if (uwsgi.single_interpreter == 1) { 
    1196                 init_uwsgi_vars(); 
    1197         } 
    1198  
    1199         memset(uwsgi.wsgi_apps, 0, sizeof(uwsgi.wsgi_apps)); 
    1200  
    1201  
    1202 #endif 
    1203  
    1204  
    1205  
    1206         uwsgi.poll.events = POLLIN ; 
    1207  
    1208         memset(&wsgi_req, 0, sizeof(struct wsgi_request)); 
     1233                init_uwsgi_vars (); 
     1234        } 
     1235 
     1236        memset (uwsgi.wsgi_apps, 0, sizeof (uwsgi.wsgi_apps)); 
     1237 
     1238 
     1239#endif 
     1240 
     1241 
     1242 
     1243        uwsgi.poll.events = POLLIN; 
     1244 
     1245        memset (&wsgi_req, 0, sizeof (struct wsgi_request)); 
     1246 
     1247        /* shared area for workers */ 
     1248        uwsgi.workers = (struct uwsgi_worker *) mmap (NULL, sizeof (struct uwsgi_worker) * uwsgi.maxworkers + 1, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 
     1249        if (!uwsgi.workers) { 
     1250                perror ("mmap()"); 
     1251                exit (1); 
     1252        } 
     1253        memset (uwsgi.workers, 0, sizeof (struct uwsgi_worker) * uwsgi.maxworkers + 1); 
     1254 
     1255        uwsgi.mypid = getpid (); 
     1256        masterpid = uwsgi.mypid; 
     1257 
     1258#ifndef ROCK_SOLID 
     1259#ifndef UNBIT 
     1260        if (uwsgi.pidfile) { 
     1261                fprintf (stderr, "writing pidfile to %s\n", uwsgi.pidfile); 
     1262                pidfile = fopen (uwsgi.pidfile, "w"); 
     1263                if (!pidfile) { 
     1264                        perror ("fopen"); 
     1265                        exit (1); 
     1266                } 
     1267                if (fprintf (pidfile, "%d\n", masterpid) < 0) { 
     1268                        fprintf (stderr, "could not write pidfile.\n"); 
     1269                } 
     1270                fclose (pidfile); 
     1271        } 
     1272#endif 
     1273#endif 
     1274 
     1275        if (uwsgi.buffer_size > 65536) { 
     1276                fprintf (stderr, "invalid buffer size.\n"); 
     1277                exit (1); 
     1278        } 
     1279        buffer = malloc (uwsgi.buffer_size); 
     1280        if (buffer == NULL) { 
     1281                fprintf (stderr, "unable to allocate memory for buffer.\n"); 
     1282                exit (1); 
     1283        } 
     1284 
     1285        fprintf (stderr, "request/response buffer (%d bytes) allocated.\n", uwsgi.buffer_size); 
     1286 
     1287        /* save the masterpid */ 
     1288        uwsgi.workers[0].pid = masterpid; 
     1289 
     1290        uwsgi.workers[0].current_workers = uwsgi.numproc; 
     1291 
     1292        fprintf(stderr,"initializing hooks..."); 
     1293 
     1294        uwsgi.hooks[0] = uwsgi_request_wsgi ;  
     1295        uwsgi.after_hooks[0] = uwsgi_after_request_wsgi ;  
     1296 
     1297        uwsgi.hooks[UWSGI_MODIFIER_ADMIN_REQUEST] = uwsgi_request_admin ; //10 
     1298        uwsgi.hooks[UWSGI_MODIFIER_SPOOL_REQUEST] = uwsgi_request_spooler ; //17 
     1299        uwsgi.hooks[UWSGI_MODIFIER_FASTFUNC] = uwsgi_request_fastfunc ; //26 
     1300 
     1301        uwsgi.hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_request_wsgi ; // 30 
     1302        uwsgi.after_hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_after_request_wsgi; // 30 
     1303 
     1304        uwsgi.hooks[UWSGI_MODIFIER_MESSAGE_MARSHAL] = uwsgi_request_marshal ; //33 
     1305        uwsgi.hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping ; //100 
     1306 
     1307        fprintf(stderr,"done.\n"); 
    12091308 
    12101309 
    12111310#ifndef ROCK_SOLID 
    12121311        if (uwsgi.wsgi_config != NULL) { 
    1213                 uwsgi_wsgi_config(); 
     1312                uwsgi_wsgi_config (); 
    12141313        } 
    12151314#endif 
     
    12181317#ifndef ROCK_SOLID 
    12191318        else if (uwsgi.xml_config != NULL) { 
    1220                 uwsgi_xml_config(); 
     1319                uwsgi_xml_config (); 
    12211320        } 
    12221321#endif 
     
    12251324#ifndef ROCK_SOLID 
    12261325        else if (uwsgi.paste != NULL) { 
    1227                 uwsgi_paste_config(); 
     1326                uwsgi_paste_config (); 
    12281327        } 
    12291328#endif 
    12301329 
    12311330        if (uwsgi.test_module != NULL) { 
    1232                 if (PyImport_ImportModule(uwsgi.test_module)) { 
    1233                         exit(0); 
    1234                 } 
    1235                 exit(1); 
    1236         } 
    1237  
    1238  
    1239         uwsgi.mypid = getpid(); 
    1240         masterpid = uwsgi.mypid ; 
    1241  
    1242 #ifndef ROCK_SOLID 
    1243 #ifndef UNBIT 
    1244         if (uwsgi.pidfile) { 
    1245                 fprintf(stderr,"writing pidfile to %s\n", uwsgi.pidfile); 
    1246                 pidfile = fopen(uwsgi.pidfile, "w"); 
    1247                 if (!pidfile) { 
    1248                         perror("fopen"); 
    1249                         exit(1); 
    1250                 } 
    1251                 if (fprintf(pidfile, "%d\n", masterpid) < 0) { 
    1252                         fprintf(stderr,"could not write pidfile.\n"); 
    1253                 } 
    1254                 fclose(pidfile); 
    1255         } 
    1256 #endif 
    1257 #endif 
    1258  
    1259         if (uwsgi.buffer_size > 65536) { 
    1260                 fprintf(stderr,"invalid buffer size.\n"); 
    1261                 exit(1); 
    1262         } 
    1263         buffer = malloc(uwsgi.buffer_size); 
    1264         if (buffer == NULL) { 
    1265                 fprintf(stderr,"unable to allocate memory for buffer.\n"); 
    1266                 exit(1); 
    1267         } 
    1268  
    1269         fprintf(stderr,"request/response buffer (%d bytes) allocated.\n", uwsgi.buffer_size); 
    1270  
    1271          
    1272  
    1273  
    1274         /* shared area for workers */ 
    1275         uwsgi.workers = (struct uwsgi_worker *) mmap(NULL, sizeof(struct uwsgi_worker)*uwsgi.maxworkers+1, PROT_READ|PROT_WRITE , MAP_SHARED|MAP_ANON , -1, 0); 
    1276         if (!uwsgi.workers) { 
    1277                 perror("mmap()"); 
    1278                 exit(1); 
    1279         } 
    1280         memset(uwsgi.workers, 0, sizeof(struct uwsgi_worker)*uwsgi.maxworkers+1); 
     1331                if (PyImport_ImportModule (uwsgi.test_module)) { 
     1332                        exit (0); 
     1333                } 
     1334                exit (1); 
     1335        } 
     1336 
     1337 
    12811338 
    12821339#ifndef UNBIT 
    12831340#ifndef ROCK_SOLID 
    12841341        if (no_server) { 
    1285                 fprintf(stderr,"no-server mode requested. Goodbye.\n"); 
    1286                 exit(0); 
    1287         } 
    1288 #endif 
    1289 #endif 
    1290  
    1291         /* preforking() */ 
     1342                fprintf (stderr, "no-server mode requested. Goodbye.\n"); 
     1343                exit (0); 
     1344        } 
     1345#endif 
     1346#endif 
     1347 
     1348        /* preforking() */ 
    12921349        if (uwsgi.master_process) { 
    12931350                if (is_a_reload) { 
    1294                         fprintf(stderr, "gracefully (RE)spawned uWSGI master process (pid: %d)\n", uwsgi.mypid); 
     1351                        fprintf (stderr, "gracefully (RE)spawned uWSGI master process (pid: %d)\n", uwsgi.mypid); 
    12951352                } 
    12961353                else { 
    1297                         fprintf(stderr, "spawned uWSGI master process (pid: %d)\n", uwsgi.mypid); 
     1354                        fprintf (stderr, "spawned uWSGI master process (pid: %d)\n", uwsgi.mypid); 
    12981355                } 
    12991356        } 
     
    13011358#ifdef UNBIT 
    13021359        if (single_app_mode == 1) { 
    1303                 wsgi_req.wsgi_script = getenv("UWSGI_SCRIPT"); 
     1360                wsgi_req.wsgi_script = getenv ("UWSGI_SCRIPT"); 
    13041361                if (wsgi_req.wsgi_script) { 
    1305                         wsgi_req.wsgi_script_len = strlen(wsgi_req.wsgi_script); 
     1362                        wsgi_req.wsgi_script_len = strlen (wsgi_req.wsgi_script); 
    13061363                } 
    13071364                else { 
    1308                         fprintf(stderr, "UWSGI_SCRIPT env var not set !\n"); 
    1309                         exit(1); 
    1310                 } 
    1311  
    1312                 init_uwsgi_app(NULL, NULL); 
     1365                        fprintf (stderr, "UWSGI_SCRIPT env var not set !\n"); 
     1366                        exit (1); 
     1367                } 
     1368 
     1369                init_uwsgi_app (NULL, NULL); 
    13131370        } 
    13141371#endif 
     
    13171374#ifndef PYTHREE 
    13181375        if (spool_dir != NULL) { 
    1319                 spooler_pid = spooler_start(serverfd, uwsgi_module); 
    1320         } 
    1321 #endif 
    1322 #endif 
    1323  
    1324         /* save the masterpid */ 
    1325         uwsgi.workers[0].pid = masterpid ; 
    1326  
    1327         uwsgi.workers[0].current_workers = uwsgi.numproc ; 
     1376                spooler_pid = spooler_start (serverfd, uwsgi_module); 
     1377        } 
     1378#endif 
     1379#endif 
     1380 
     1381#ifndef ROCK_SOLID 
     1382#ifndef PYTHREE 
     1383#ifndef UNBIT 
     1384        if (snmp != NULL) { 
     1385                //snmp_pid = snmp_start(snmp); 
     1386        } 
     1387#endif 
     1388#endif 
     1389#endif 
     1390 
     1391 
    13281392 
    13291393 
    13301394        if (!uwsgi.master_process && uwsgi.numproc == 1) { 
    1331                         fprintf(stderr, "spawned uWSGI worker 1 (and the only) (pid: %d)\n", masterpid); 
    1332                         uwsgi.workers[1].pid = masterpid ; 
    1333                         uwsgi.workers[1].id = 1 ; 
    1334                         uwsgi.workers[1].last_spawn = time(NULL) ; 
    1335                         uwsgi.workers[1].manage_next_request = 1 ; 
    1336                         uwsgi.mywid = 1; 
    1337                         gettimeofday(&last_respawn, NULL) ; 
    1338                         respawn_delta = last_respawn.tv_sec; 
     1395                fprintf (stderr, "spawned uWSGI worker 1 (and the only) (pid: %d)\n", masterpid); 
     1396                uwsgi.workers[1].pid = masterpid; 
     1397                uwsgi.workers[1].id = 1; 
     1398                uwsgi.workers[1].last_spawn = time (NULL); 
     1399                uwsgi.workers[1].manage_next_request = 1; 
     1400                uwsgi.mywid = 1; 
     1401                gettimeofday (&last_respawn, NULL); 
     1402                respawn_delta = last_respawn.tv_sec; 
    13391403        } 
    13401404        else { 
    1341                 for(i=1;i<uwsgi.numproc+1;i++) { 
     1405                for (i = 1; i < uwsgi.numproc + 1; i++) { 
    13421406                        /* let the worker know his worker_id (wid) */ 
    1343                         pid = fork(); 
    1344                         if (pid == 0 ) { 
    1345                                 uwsgi.mypid = getpid(); 
     1407                        pid = fork (); 
     1408                        if (pid == 0) { 
     1409                                uwsgi.mypid = getpid (); 
    13461410                                uwsgi.mywid = i; 
    13471411                                if (serverfd != 0 && uwsgi.master_process == 1) { 
    13481412                                        /* close STDIN for workers */ 
    1349                                         close(0); 
    1350                                 } 
    1351                                 break; 
    1352                         } 
    1353                         else if (pid < 1) { 
    1354                                 perror("fork()"); 
    1355                                 exit(1); 
    1356                         } 
    1357                         else { 
    1358                                 fprintf(stderr, "spawned uWSGI worker %d (pid: %d)\n", i, pid); 
    1359                                 uwsgi.workers[i].pid = pid ; 
    1360                                 uwsgi.workers[i].id = i ; 
    1361                                 uwsgi.workers[i].last_spawn = time(NULL) ; 
    1362                                 uwsgi.workers[i].manage_next_request = 1 ; 
    1363                                 gettimeofday(&last_respawn, NULL) ; 
     1413                                        close (0); 
     1414                                } 
     1415                                break; 
     1416                        } 
     1417                        else if (pid < 1) { 
     1418                                perror ("fork()"); 
     1419                                exit (1); 
     1420                        } 
     1421                        else { 
     1422                                fprintf (stderr, "spawned uWSGI worker %d (pid: %d)\n", i, pid); 
     1423                                uwsgi.workers[i].pid = pid; 
     1424                                uwsgi.workers[i].id = i; 
     1425                                uwsgi.workers[i].last_spawn = time (NULL); 
     1426                                uwsgi.workers[i].manage_next_request = 1; 
     1427                                gettimeofday (&last_respawn, NULL); 
    13641428                                respawn_delta = last_respawn.tv_sec; 
    1365                         } 
    1366                 } 
    1367         } 
    1368  
    1369         if (getpid() == masterpid && uwsgi.master_process == 1) { 
     1429                        } 
     1430                } 
     1431        } 
     1432 
     1433        if (getpid () == masterpid && uwsgi.master_process == 1) { 
    13701434                /* route signals to workers... */ 
    1371                 signal(SIGHUP, (void *) &grace_them_all); 
    1372                 signal(SIGTERM, (void *) &reap_them_all); 
    1373                 signal(SIGINT, (void *) &kill_them_all); 
    1374                 signal(SIGQUIT, (void *) &kill_them_all); 
     1435                signal (SIGHUP, (void *) &grace_them_all); 
     1436                signal (SIGTERM, (void *) &reap_them_all); 
     1437                signal (SIGINT, (void *) &kill_them_all); 
     1438                signal (SIGQUIT, (void *) &kill_them_all); 
    13751439                /* used only to avoid human-errors */ 
    13761440#ifndef ROCK_SOLID 
    13771441#ifndef UNBIT 
    1378                 signal(SIGUSR1, (void *) &stats); 
    1379 #endif 
    1380 #endif 
    1381                 for(;;) { 
     1442                signal (SIGUSR1, (void *) &stats); 
     1443#endif 
     1444#endif 
     1445 
     1446                if (udp_socket) { 
     1447                        udp_poll.fd = bind_to_udp (udp_socket); 
     1448                        if (udp_poll.fd < 0) { 
     1449                                fprintf (stderr, "unable to bind to udp socket. SNMP and cluster management services will be disabled.\n"); 
     1450                        } 
     1451                        else { 
     1452                                fprintf (stderr, "UDP server enabled.\n"); 
     1453                                udp_poll.events = POLLIN; 
     1454                        } 
     1455                } 
     1456                for (;;) { 
    13821457                        if (ready_to_die >= uwsgi.numproc && uwsgi.to_hell) { 
    13831458#ifndef ROCK_SOLID 
    13841459                                if (spool_dir && spooler_pid > 0) { 
    1385                                         kill(spooler_pid, SIGKILL); 
    1386                                 } 
    1387 #endif 
    1388                                 fprintf(stderr,"goodbye to uWSGI.\n"); 
    1389                                 exit(0); 
    1390                         }                
     1460                                        kill (spooler_pid, SIGKILL); 
     1461                                } 
     1462 
     1463                                if (snmp && snmp_pid > 0) { 
     1464                                        kill (snmp_pid, SIGKILL); 
     1465                                } 
     1466#endif 
     1467                                fprintf (stderr, "goodbye to uWSGI.\n"); 
     1468                                exit (0); 
     1469                        } 
    13911470                        if (ready_to_reload >= uwsgi.numproc && uwsgi.to_heaven) { 
    13921471#ifndef ROCK_SOLID 
    13931472                                if (spool_dir && spooler_pid > 0) { 
    1394                                         kill(spooler_pid, SIGKILL); 
    1395                                 } 
    1396 #endif 
    1397                                 fprintf(stderr,"binary reloading uWSGI...\n"); 
    1398                                 if (chdir(cwd)) { 
    1399                                         perror("chdir()"); 
    1400                                         exit(1); 
     1473                                        kill (spooler_pid, SIGKILL); 
     1474                                } 
     1475#endif 
     1476                                fprintf (stderr, "binary reloading uWSGI...\n"); 
     1477                                if (chdir (cwd)) { 
     1478                                        perror ("chdir()"); 
     1479                                        exit (1); 
    14011480                                } 
    14021481                                /* check fd table (a module can obviosly open some fd on initialization...) */ 
    1403                                 fprintf(stderr,"closing all fds > 2 (_SC_OPEN_MAX = %ld)...\n",sysconf(_SC_OPEN_MAX)); 
    1404                                 for(i=3;i<sysconf(_SC_OPEN_MAX);i++) { 
     1482                                fprintf (stderr, "closing all fds > 2 (_SC_OPEN_MAX = %ld)...\n", sysconf (_SC_OPEN_MAX)); 
     1483                                for (i = 3; i < sysconf (_SC_OPEN_MAX); i++) { 
    14051484                                        if (i == serverfd) { 
    1406                                                 continue ; 
     1485                                                continue; 
    14071486                                        } 
    1408                                         close(i); 
     1487                                        close (i); 
    14091488                                } 
    14101489                                if (serverfd != 3) { 
    1411                                         if (dup2(serverfd,3) < 0) { 
    1412                                                 perror("dup2()"); 
    1413                                                 exit(1); 
     1490                                        if (dup2 (serverfd, 3) < 0) { 
     1491                                                perror ("dup2()"); 
     1492                                                exit (1); 
    14141493                                        } 
    14151494                                } 
    1416                                 fprintf(stderr,"running %s\n", binary_path); 
    1417                                 strcpy(argv[0], binary_path); 
    1418                                 execve(binary_path, argv, envp); 
    1419                                 perror("execve()"); 
    1420                                 exit(1); 
    1421                         } 
    1422                         diedpid = waitpid(WAIT_ANY , &waitpid_status, WNOHANG) ; 
     1495                                fprintf (stderr, "running %s\n", binary_path); 
     1496                                strcpy (argv[0], binary_path); 
     1497                                execve (binary_path, argv, envp); 
     1498                                perror ("execve()"); 
     1499                                exit (1); 
     1500                        } 
     1501                        diedpid = waitpid (WAIT_ANY, &waitpid_status, WNOHANG); 
    14231502                        if (diedpid == -1) { 
    1424                                 perror("waitpid()"); 
     1503                                perror ("waitpid()"); 
    14251504                                /* here is better to reload all the uWSGI stack */ 
    1426                                 fprintf(stderr, "something horrible happened...\n"); 
    1427                                 reap_them_all(); 
    1428                                 exit(1); 
     1505                                fprintf (stderr, "something horrible happened...\n"); 
     1506                                reap_them_all (); 
     1507                                exit (1); 
    14291508                        } 
    14301509                        else if (diedpid == 0) { 
    14311510                                /* PLEASE, do not run python threads in the master process, you can potentially destroy the world, 
    1432                                  we support this for hyperultramegagodprogrammer and systems 
    1433                                 */ 
    1434 #ifndef ROCK_SOLID 
    1435                                 if (uwsgi.has_threads && uwsgi.options[UWSGI_OPTION_THREADS] == 1) { 
    1436                                         _save = PyEval_SaveThread(); 
    1437                                         i_have_gil = 0; 
    1438                                 } 
     1511                                   we support this for hyperultramegagodprogrammer and systems 
     1512                                 */ 
     1513#ifndef ROCK_SOLID 
     1514                                if (uwsgi.has_threads && uwsgi.options[UWSGI_OPTION_THREADS] == 1) { 
     1515                                        uwsgi._save = PyEval_SaveThread (); 
     1516                                        uwsgi.i_have_gil = 0; 
     1517                                } 
    14391518#endif 
    14401519                                /* all processes ok, doing status scan after N seconds */ 
    1441                                 check_interval.tv_sec = uwsgi.options[UWSGI_OPTION_MASTER_INTERVAL] ; 
    1442                                 if (!check_interval.tv_sec)      
     1520                                check_interval.tv_sec = uwsgi.options[UWSGI_OPTION_MASTER_INTERVAL]; 
     1521                                if (!check_interval.tv_sec) 
    14431522                                        check_interval.tv_sec = 1; 
    1444                                 select(0, NULL, NULL, NULL, &check_interval); 
    1445                                 working_workers = 0 ; 
    1446                                 blocking_workers = 0 ;   
    1447 #ifndef ROCK_SOLID 
    1448                                 if (uwsgi.has_threads && !i_have_gil) { 
    1449                                         PyEval_RestoreThread(_save); 
    1450                                         i_have_gil = 1 ; 
    1451                                 } 
    1452 #endif 
    1453                                 check_interval.tv_sec = uwsgi.options[UWSGI_OPTION_MASTER_INTERVAL] ; 
    1454                                 if (!check_interval.tv_sec)      
     1523                                if (udp_poll.fd >= 0) { 
     1524                                        rlen = poll (&udp_poll, 1, check_interval.tv_sec * 1000); 
     1525                                        if (rlen < 0) { 
     1526                                                perror ("poll()"); 
     1527                                        } 
     1528                                        else if (rlen > 0) { 
     1529                                                udp_len = sizeof (udp_client); 
     1530                                                rlen = recvfrom (udp_poll.fd, buffer, uwsgi.buffer_size, 0, (struct sockaddr *) &udp_client, &udp_len); 
     1531                                                if (rlen < 0) { 
     1532                                                        perror ("recvfrom()"); 
     1533                                                } 
     1534                                                else if (rlen > 0) { 
     1535                                                        memset (udp_client_addr, 0, 16); 
     1536                                                        if (inet_ntop (AF_INET, &udp_client, udp_client_addr, 16)) { 
     1537                                                                fprintf (stderr, "received udp packet of %d bytes from %s:%d\n", rlen, udp_client_addr, ntohs (udp_client.sin_port)); 
     1538                                                                if (buffer[0] == 0x30) { 
     1539                                                                        manage_snmp (udp_poll.fd, (uint8_t *) buffer, rlen, &udp_client); 
     1540                                                                } 
     1541                                                        } 
     1542                                                        else { 
     1543                                                                perror ("inet_ntop()"); 
     1544                                                        } 
     1545                                                } 
     1546                                        } 
     1547                                } 
     1548                                else { 
     1549                                        select (0, NULL, NULL, NULL, &check_interval); 
     1550                                } 
     1551                                working_workers = 0; 
     1552                                blocking_workers = 0; 
     1553#ifndef ROCK_SOLID 
     1554                                if (uwsgi.has_threads && !uwsgi.i_have_gil) { 
     1555                                        PyEval_RestoreThread (uwsgi._save); 
     1556                                        uwsgi.i_have_gil = 1; 
     1557                                } 
     1558#endif 
     1559                                check_interval.tv_sec = uwsgi.options[UWSGI_OPTION_MASTER_INTERVAL]; 
     1560                                if (!check_interval.tv_sec) 
    14551561                                        check_interval.tv_sec = 1; 
    1456                                 for(i=1;i<=uwsgi.workers[0].current_workers;i++) { 
     1562                                for (i = 1; i <= uwsgi.workers[0].current_workers; i++) { 
    14571563                                        /* first check for harakiri */ 
    1458                                         if (uwsgi.workers[i].harakiri > 0) { 
    1459                                                 if (uwsgi.workers[i].harakiri < time(NULL)) { 
     1564                                        if (uwsgi.workers[i].harakiri > 0) { 
     1565                                                if (uwsgi.workers[i].harakiri < time (NULL)) { 
    14601566                                                        /* first try to invoke the harakiri() custom handler */ 
    14611567                                                        /* TODO */ 
    14621568                                                        /* then brutally kill the worker */ 
    1463                                                         kill(uwsgi.workers[i].pid, SIGKILL); 
     1569                                                        kill (uwsgi.workers[i].pid, SIGKILL); 
    14641570                                                } 
    14651571                                        } 
     
    14701576                                        if (uwsgi.workers[i].blocking) 
    14711577                                                blocking_workers++; 
    1472                                         /* set the load */ 
    1473                                         if (uwsgi.workers[i].last_running_time > 0 && uwsgi.workers[i].running_time > 0) { 
    1474                                                 uwsgi.workers[i].load = (((uwsgi.workers[i].running_time-uwsgi.workers[i].last_running_time)/1000) * 100)/check_interval.tv_sec ; 
    1475                                         } 
    1476                                         uwsgi.workers[i].last_running_time = uwsgi.workers[i].running_time ; 
    1477                                 } 
     1578 
     1579                                        uwsgi.workers[i].last_running_time = uwsgi.workers[i].running_time; 
     1580                                } 
    14781581 
    14791582                                /* check if i need to add a subworker */ 
    1480                                 fprintf(stderr,"working workers: %d\n",  working_workers); 
     1583                                //fprintf(stderr,"working workers: %d\n",  working_workers); 
    14811584                                if (working_workers >= uwsgi.workers[0].current_workers) { 
    14821585                                        /* ok the system is overloaded, check if some worker is in blocking mode */ 
    14831586                                        if (blocking_workers > 0) { 
    14841587                                                /* check if is it safe to add a new worker (for now check only for enough free memory) */ 
    1485                                                 fprintf(stderr,"adding a new subworker as there are workers in blocking mode...\n"); 
    1486                                                 uwsgi.mywid = uwsgi.workers[0].current_workers+1; 
    1487                                                 pid = fork(); 
     1588                                                fprintf (stderr, "adding a new subworker as there are workers in blocking mode...\n"); 
     1589                                                uwsgi.mywid = uwsgi.workers[0].current_workers + 1; 
     1590                                                pid = fork (); 
    14881591                                                if (pid < 0) { 
    1489                                                         perror("fork()"); 
     1592                                                        perror ("fork()"); 
    14901593                                                } 
    14911594                                                else if (pid > 0) { 
    1492                                                         fprintf(stderr, "spawned uWSGI subworker (pid: %d)\n", pid); 
    1493                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].id = uwsgi.workers[0].current_workers+1 ; 
    1494                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].pid = pid ; 
    1495                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].harakiri = 0 ; 
    1496                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].requests = 0 ; 
    1497                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].failed_requests = 0 ; 
    1498                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].respawn_count++ ; 
    1499                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].last_spawn = time(NULL) ; 
    1500                                                         uwsgi.workers[uwsgi.workers[0].current_workers+1].manage_next_request = 1 ; 
    1501                                                         uwsgi.workers[0].current_workers++ ; 
     1595                                                        fprintf (stderr, "spawned uWSGI subworker (pid: %d)\n", pid); 
     1596                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].id = uwsgi.workers[0].current_workers + 1; 
     1597                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].pid = pid; 
     1598                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].harakiri = 0; 
     1599                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].requests = 0; 
     1600                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].failed_requests = 0; 
     1601                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].respawn_count++; 
     1602                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].last_spawn = time (NULL); 
     1603                                                        uwsgi.workers[uwsgi.workers[0].current_workers + 1].manage_next_request = 1; 
     1604                                                        uwsgi.workers[0].current_workers++; 
    15021605                                                } 
    15031606                                                else if (pid == 0) { 
    1504                                                         uwsgi.mypid = getpid(); 
    1505                                                         break; 
     1607                                                        uwsgi.mypid = getpid (); 
     1608                                                        break; 
    15061609                                                } 
    15071610                                        } 
     
    15091612                                else { 
    15101613                                        /* the system is no more overloaded, mark subworkers */ 
    1511                                         for(i=uwsgi.numproc+1;i<=uwsgi.workers[0].current_workers;i++) { 
    1512                                                 uwsgi.workers[i].manage_next_request = 0 ; 
    1513                                         }        
     1614                                        for (i = uwsgi.numproc + 1; i <= uwsgi.workers[0].current_workers; i++) { 
     1615                                                uwsgi.workers[i].manage_next_request = 0; 
     1616                                        } 
    15141617                                } 
    15151618 
    15161619                                /* now check for subworkers to remove */ 
    1517                                 for(i=uwsgi.workers[0].current_workers;i>uwsgi.numproc;i--) { 
     1620                                for (i = uwsgi.workers[0].current_workers; i > uwsgi.numproc; i--) { 
    15181621                                        if (uwsgi.workers[i].manage_next_request) 
    15191622                                                break; 
    15201623                                        /* reset this worker (no interest in killing it) */ 
    1521                                         memset(&uwsgi.workers[i], 0, sizeof(struct uwsgi_worker)); 
     1624                                        memset (&uwsgi.workers[i], 0, sizeof (struct uwsgi_worker)); 
    15221625                                        uwsgi.workers[0].current_workers--; 
    15231626                                } 
     
    15291632                        if (spool_dir && spooler_pid > 0) { 
    15301633                                if (diedpid == spooler_pid) { 
    1531                                         spooler_pid = spooler_start(serverfd, uwsgi_module); 
     1634                                        spooler_pid = spooler_start (serverfd, uwsgi_module); 
    15321635                                        continue; 
    15331636                                } 
    15341637                        } 
     1638#ifndef UNBIT 
     1639 
     1640                        /* reload the snmp server */ 
     1641                        if (snmp && snmp_pid > 0) { 
     1642                                if (diedpid == snmp_pid) { 
     1643                                        //snmp_pid = snmp_start(snmp); 
     1644                                        contin