Changeset 218:76a04f491a6c for uwsgi.c
Legend:
- Unmodified
- Added
- Removed
-
uwsgi.c
r217 r218 30 30 31 31 32 #ifndef ROCK_SOLID33 #ifdef __linux__34 #include <sys/sendfile.h>35 #endif36 #endif37 32 38 33 #ifndef UNBIT … … 46 41 47 42 48 #include <sys/wait.h>49 50 51 43 #include "uwsgi.h" 52 44 53 45 #if PY_MINOR_VERSION < 5 54 #define Py_ssize_t int46 #define Py_ssize_t int 55 47 #endif 56 48 … … 58 50 59 51 static 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++) {52 static char *h_sep = ": "; 53 static const char *http_protocol = "HTTP/1.1"; 54 #ifndef ROCK_SOLID 55 static const char *app_slash = "/"; 56 #endif 57 58 int find_worker_id (pid_t pid) { 59 int i; 60 for (i = 1; i <= uwsgi.numproc; i++) { 69 61 /* fprintf(stderr,"%d of %d\n", pid, uwsgi.workers[i].pid); */ 70 62 if (uwsgi.workers[i].pid == pid) 71 return i ;72 } 73 74 return -1 ;63 return i; 64 } 65 66 return -1; 75 67 } 76 68 … … 78 70 79 71 PyMethodDef null_methods[] = { 80 {NULL, NULL},72 {NULL, NULL}, 81 73 }; 82 74 83 75 #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 ; 76 int save_to_disk = -1; 77 int tmp_dir_fd = -1; 78 char *tmp_filename; 79 int uri_to_hex (void); 80 int check_for_memory_errors = 0; 81 #endif 82 83 PyObject *wsgi_writeout; 84 95 85 96 86 #ifdef ROCK_SOLID … … 98 88 #endif 99 89 100 void warn_pipe () {101 fprintf (stderr,"writing to a closed pipe/socket/fd !!!\n");90 void warn_pipe () { 91 fprintf (stderr, "writing to a closed pipe/socket/fd !!!\n"); 102 92 } 103 93 104 void gracefully_kill () {105 fprintf (stderr, "Gracefully killing worker %d...\n", uwsgi.mypid);94 void gracefully_kill () { 95 fprintf (stderr, "Gracefully killing worker %d...\n", uwsgi.mypid); 106 96 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; 108 98 } 109 99 else { 110 reload_me ();100 reload_me (); 111 101 } 112 102 } 113 103 114 void reload_me () {115 exit (UWSGI_RELOAD_CODE);104 void reload_me () { 105 exit (UWSGI_RELOAD_CODE); 116 106 } 117 107 118 void end_me () {119 exit (UWSGI_END_CODE);108 void end_me () { 109 exit (UWSGI_END_CODE); 120 110 } 121 111 122 void goodbye_cruel_world () {123 fprintf (stderr, "...The work of process %d is done. Seeya!\n", getpid());124 exit (0);112 void goodbye_cruel_world () { 113 fprintf (stderr, "...The work of process %d is done. Seeya!\n", getpid ()); 114 exit (0); 125 115 } 126 116 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);117 void 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); 133 123 } 134 124 } 135 125 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);126 void 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); 142 132 } 143 133 } 144 134 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);135 void 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); 150 140 } 151 141 } 152 142 153 void harakiri () {143 void harakiri () { 154 144 155 145 PyThreadState *_myself; 156 146 #ifndef ROCK_SOLID 157 struct uwsgi_app *wi = NULL ;147 struct uwsgi_app *wi = NULL; 158 148 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 (); 164 154 if (wi) { 165 #ifdef ROCK_SOLID166 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 #else168 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 #endif155 #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 170 160 171 161 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"); 179 169 } 180 170 181 171 #ifndef UNBIT 182 172 #ifndef ROCK_SOLID 183 void stats () {173 void stats () { 184 174 struct uwsgi_app *ua = NULL; 185 175 int i; 186 176 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++) { 190 180 ua = &uwsgi.wsgi_apps[i]; 191 181 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"); 196 186 } 197 187 #endif 198 188 #endif 199 189 200 void internal_server_error (int fd, char *message) {190 void internal_server_error (int fd, char *message) { 201 191 #ifndef UNBIT 202 192 #ifndef ROCK_SOLID … … 204 194 #endif 205 195 #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); 207 197 #ifndef UNBIT 208 198 #ifndef ROCK_SOLID 209 199 } 210 200 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)); 218 208 } 219 209 220 210 #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);211 PyObject *py_uwsgi_sendfile (PyObject * self, PyObject * args) { 212 213 //PyObject *zero ; 214 215 uwsgi.py_sendfile = PyTuple_GetItem (args, 0); 226 216 227 217 #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); 230 220 } 231 221 #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); 242 232 } 243 233 #endif 244 234 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); 235 PyObject *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); 262 250 #ifdef UNBIT 263 251 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 } 275 263 #ifdef UNBIT 276 264 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; 284 272 } 285 273 … … 287 275 PyObject *wsgi_spitout; 288 276 289 PyObject *py_uwsgi_spit (PyObject *self, PyObject *args) {290 PyObject *headers, *head;291 PyObject *h_key, *h_value;292 int i,j;277 PyObject *py_uwsgi_spit (PyObject * self, PyObject * args) { 278 PyObject *headers, *head; 279 PyObject *h_key, *h_value; 280 int i, j; 293 281 #ifndef UNBIT 294 282 #ifdef ROCK_SOLID 295 int base = 4 ;283 int base = 4; 296 284 #else 297 int base = 0 ;285 int base = 0; 298 286 #endif 299 287 #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); 307 295 if (!head) { 308 296 goto clear; 309 297 } 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"); 313 301 goto clear; 314 302 } … … 318 306 #ifndef ROCK_SOLID 319 307 if (uwsgi.options[UWSGI_OPTION_CGI_MODE] == 0) { 320 base = 4 ;308 base = 4; 321 309 #endif 322 310 #endif … … 324 312 325 313 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; 328 316 } 329 317 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; 336 324 #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); 339 327 #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; 346 334 #ifndef UNBIT 347 335 #ifndef ROCK_SOLID … … 349 337 else { 350 338 // 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; 354 342 #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); 357 345 #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; 364 352 } 365 353 #endif … … 370 358 if (wsgi_req.unbit_flags & (unsigned long long) 1) { 371 359 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); 379 367 if (!headers) { 380 368 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"); 384 372 goto clear; 385 373 } 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); 396 384 if (!head) { 397 fprintf(stderr,"NON e' UNA LISTA\n");398 385 goto clear; 399 386 } 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"); 402 389 goto clear; 403 390 } 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 } 408 399 #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); 411 402 #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; 417 408 #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); 420 411 #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 } 428 419 429 420 430 421 #ifdef UNBIT 431 422 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; 436 427 break; 437 428 } … … 440 431 #endif 441 432 442 // \r\n443 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); 448 439 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); 459 450 return Py_None; 460 451 } 461 452 462 453 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, ""}} ; 454 PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} 455 }; 456 PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, METH_VARARGS, ""} 457 }; 458 459 #ifndef ROCK_SOLID 460 PyMethodDef uwsgi_sendfile_method[] = { {"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""} 461 }; 468 462 #endif 469 463 … … 479 473 #endif 480 474 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 475 char *spool_dir = NULL; 476 477 int main (int argc, char *argv[], char *envp[]) { 478 479 struct timeval check_interval = {.tv_sec = 1,.tv_usec = 0 }; 480 487 481 #ifndef PYTHREE 488 482 #ifndef ROCK_SOLID 489 483 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; 505 494 #ifndef ROCK_SOLID 506 495 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; 515 513 #ifndef ROCK_SOLID 516 514 FILE *pidfile; … … 519 517 520 518 #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; 530 531 int ready_to_reload = 0; 531 532 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; 545 539 546 540 #ifdef UNBIT … … 548 542 #endif 549 543 550 int socket_type; 551 socklen_t socket_type_len ; 552 char *path_info; 544 int socket_type; 545 socklen_t socket_type_len; 553 546 554 547 /* 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)); 559 556 560 557 /* 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); 562 559 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)); 567 564 568 565 #ifndef ROCK_SOLID … … 575 572 uwsgi.listen_queue = 64; 576 573 #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 585 586 586 587 #ifndef UNBIT … … 627 628 {"no-defer-accept", no_argument, &uwsgi.no_defer_accept, 1}, 628 629 {"limit-as", required_argument, 0, LONG_ARGS_LIMIT_AS}, 630 {"udp", required_argument, 0, LONG_ARGS_UDP}, 629 631 {"check-interval", required_argument, 0, LONG_ARGS_CHECK_INTERVAL}, 630 632 {0, 0, 0, 0} … … 634 636 635 637 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); 645 647 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; 655 657 #ifdef UNBIT 656 658 /* discard the 3'th fd as we will use the fd 0 */ 657 close (3);659 close (3); 658 660 #else 659 661 serverfd = 3; … … 662 664 663 665 #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) { 669 668 #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; 728 734 #endif 729 735 #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\ 837 843 \t-s|--socket <name>\t\tpath (or name) of UNIX/TCP socket to bind to\n\ 838 844 \t-l|--listen <num>\t\tset socket listen queue to <n> (default 64, maximum is system dependent)\n\ … … 873 879 \t--pyargv <args>\t\t\tassign args to python sys.argv\n\ 874 880 \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\ 875 882 \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 } 884 891 885 892 #ifndef UNBIT … … 889 896 #endif 890 897 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)); 892 899 } 893 900 #ifndef UNBIT … … 895 902 } 896 903 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)); 898 905 } 899 906 #endif … … 901 908 902 909 #ifdef __BIG_ENDIAN__ 903 fprintf (stderr,"*** big endian arch detected ***\n");910 fprintf (stderr, "*** big endian arch detected ***\n"); 904 911 #endif 905 912 906 913 #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"); 915 922 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); 920 927 } 921 928 #ifdef __linux__ 922 929 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 } 925 932 #endif 926 933 } 927 934 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); 932 939 } 933 940 } 934 941 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); 939 946 } 940 947 } 941 948 } 942 949 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 950 967 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 958 977 // 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); 969 991 #endif 970 992 971 993 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); 974 996 if (!uwsgi.sync_page) { 975 perror ("malloc()");976 exit (1);997 perror ("malloc()"); 998 exit (1); 977 999 } 978 1000 } 979 1001 980 1002 if (uwsgi.pyhome != NULL) { 981 fprintf(stderr,"Setting PythonHome to %s...\n", uwsgi.pyhome);1003 fprintf (stderr, "Setting PythonHome to %s...\n", uwsgi.pyhome); 982 1004 #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); 985 1007 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); 992 1014 #else 993 Py_SetPythonHome (uwsgi.pyhome);994 #endif 995 } 996 997 998 1015 Py_SetPythonHome (uwsgi.pyhome); 1016 #endif 1017 } 1018 1019 1020 999 1021 1000 1022 #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); 1004 1026 #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"; 1012 1034 1013 1035 if (uwsgi.pyargv != NULL) { 1014 1036 char *ap; 1015 while ( (ap = strsep(&uwsgi.pyargv, " \t")) != NULL) {1037 while ((ap = strsep (&uwsgi.pyargv, " \t")) != NULL) { 1016 1038 if (*ap != '\0') { 1017 pyargv[pyargc] = ap ;1039 pyargv[pyargc] = ap; 1018 1040 pyargc++; 1019 1041 } 1020 if (pyargc +1 > MAX_PYARGV)1042 if (pyargc + 1 > MAX_PYARGV) 1021 1043 break; 1022 1044 } 1023 1045 } 1024 1046 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); 1040 1062 1041 1063 #ifndef PYTHREE 1042 1064 #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); 1047 1069 } 1048 1070 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); 1051 1073 if (!uwsgi.sharedareamutex) { 1052 perror ("mmap()");1053 exit (1);1054 } 1055 #else1056 fprintf(stderr,"***WARNING*** the sharedarea on OpenBSD is not SMP-safe. Beware of race conditions !!!\n");1057 #endif1058 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); 1061 1083 1062 1084 #ifdef __APPLE__ 1063 memset (uwsgi.sharedareamutex,0, sizeof(OSSpinLock));1085 memset (uwsgi.sharedareamutex, 0, sizeof (OSSpinLock)); 1064 1086 #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 #endif1079 #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 1081 1103 } 1082 1104 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 (); 1090 1112 #endif 1091 1113 #endif … … 1096 1118 1097 1119 1098 wi = malloc (sizeof(struct uwsgi_app));1120 wi = malloc (sizeof (struct uwsgi_app)); 1099 1121 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)); 1104 1126 1105 1127 // load wsgi module/script 1106 1128 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]); 1109 1131 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); 1114 1136 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"); 1119 1141 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 (); 1124 1146 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); 1132 1154 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; 1145 1167 } 1146 1168 1147 1169 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); 1150 1172 } 1151 1173 #endif … … 1154 1176 Py_OptimizeFlag = uwsgi.py_optimize; 1155 1177 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 } 1163 1185 1164 1186 #endif … … 1166 1188 if (!no_server) { 1167 1189 #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 } 1189 1226 1190 1227 } … … 1194 1231 #ifndef ROCK_SOLID 1195 1232 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"); 1209 1308 1210 1309 1211 1310 #ifndef ROCK_SOLID 1212 1311 if (uwsgi.wsgi_config != NULL) { 1213 uwsgi_wsgi_config ();1312 uwsgi_wsgi_config (); 1214 1313 } 1215 1314 #endif … … 1218 1317 #ifndef ROCK_SOLID 1219 1318 else if (uwsgi.xml_config != NULL) { 1220 uwsgi_xml_config ();1319 uwsgi_xml_config (); 1221 1320 } 1222 1321 #endif … … 1225 1324 #ifndef ROCK_SOLID 1226 1325 else if (uwsgi.paste != NULL) { 1227 uwsgi_paste_config ();1326 uwsgi_paste_config (); 1228 1327 } 1229 1328 #endif 1230 1329 1231 1330 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 1281 1338 1282 1339 #ifndef UNBIT 1283 1340 #ifndef ROCK_SOLID 1284 1341 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() */ 1292 1349 if (uwsgi.master_process) { 1293 1350 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); 1295 1352 } 1296 1353 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); 1298 1355 } 1299 1356 } … … 1301 1358 #ifdef UNBIT 1302 1359 if (single_app_mode == 1) { 1303 wsgi_req.wsgi_script = getenv ("UWSGI_SCRIPT");1360 wsgi_req.wsgi_script = getenv ("UWSGI_SCRIPT"); 1304 1361 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); 1306 1363 } 1307 1364 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); 1313 1370 } 1314 1371 #endif … … 1317 1374 #ifndef PYTHREE 1318 1375 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 1328 1392 1329 1393 1330 1394 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; 1339 1403 } 1340 1404 else { 1341 for(i=1;i<uwsgi.numproc+1;i++) {1405 for (i = 1; i < uwsgi.numproc + 1; i++) { 1342 1406 /* 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 (); 1346 1410 uwsgi.mywid = i; 1347 1411 if (serverfd != 0 && uwsgi.master_process == 1) { 1348 1412 /* 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); 1364 1428 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) { 1370 1434 /* 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); 1375 1439 /* used only to avoid human-errors */ 1376 1440 #ifndef ROCK_SOLID 1377 1441 #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 (;;) { 1382 1457 if (ready_to_die >= uwsgi.numproc && uwsgi.to_hell) { 1383 1458 #ifndef ROCK_SOLID 1384 1459 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 } 1391 1470 if (ready_to_reload >= uwsgi.numproc && uwsgi.to_heaven) { 1392 1471 #ifndef ROCK_SOLID 1393 1472 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); 1401 1480 } 1402 1481 /* 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++) { 1405 1484 if (i == serverfd) { 1406 continue ;1485 continue; 1407 1486 } 1408 close (i);1487 close (i); 1409 1488 } 1410 1489 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); 1414 1493 } 1415 1494 } 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); 1423 1502 if (diedpid == -1) { 1424 perror ("waitpid()");1503 perror ("waitpid()"); 1425 1504 /* 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); 1429 1508 } 1430 1509 else if (diedpid == 0) { 1431 1510 /* PLEASE, do not run python threads in the master process, you can potentially destroy the world, 1432 we support this for hyperultramegagodprogrammer and systems1433 */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 } 1439 1518 #endif 1440 1519 /* 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) 1443 1522 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) 1455 1561 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++) { 1457 1563 /* 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)) { 1460 1566 /* first try to invoke the harakiri() custom handler */ 1461 1567 /* TODO */ 1462 1568 /* then brutally kill the worker */ 1463 kill (uwsgi.workers[i].pid, SIGKILL);1569 kill (uwsgi.workers[i].pid, SIGKILL); 1464 1570 } 1465 1571 } … … 1470 1576 if (uwsgi.workers[i].blocking) 1471 1577 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 } 1478 1581 1479 1582 /* 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); 1481 1584 if (working_workers >= uwsgi.workers[0].current_workers) { 1482 1585 /* ok the system is overloaded, check if some worker is in blocking mode */ 1483 1586 if (blocking_workers > 0) { 1484 1587 /* 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 (); 1488 1591 if (pid < 0) { 1489 perror ("fork()");1592 perror ("fork()"); 1490 1593 } 1491 1594 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++; 1502 1605 } 1503 1606 else if (pid == 0) { 1504 uwsgi.mypid = getpid ();1505 break;1607 uwsgi.mypid = getpid (); 1608 break; 1506 1609 } 1507 1610 } … … 1509 1612 else { 1510 1613 /* 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 } 1514 1617 } 1515 1618 1516 1619 /* 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--) { 1518 1621 if (uwsgi.workers[i].manage_next_request) 1519 1622 break; 1520 1623 /* 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)); 1522 1625 uwsgi.workers[0].current_workers--; 1523 1626 } … … 1529 1632 if (spool_dir && spooler_pid > 0) { 1530 1633 if (diedpid == spooler_pid) { 1531 spooler_pid = spooler_start (serverfd, uwsgi_module);1634 spooler_pid = spooler_start (serverfd, uwsgi_module); 1532 1635 continue; 1533 1636 } 1534 1637 } 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
