Changeset 285:f98e391595d4 for uwsgi.c
Legend:
- Unmodified
- Added
- Removed
-
uwsgi.c
r284 r285 45 45 extern char **environ; 46 46 47 int find_worker_id (pid_t pid) {47 int find_worker_id(pid_t pid) { 48 48 int i; 49 49 for (i = 1; i <= uwsgi.numproc; i++) { … … 66 66 int tmp_dir_fd = -1; 67 67 char *tmp_filename; 68 int uri_to_hex (void);68 int uri_to_hex(void); 69 69 int check_for_memory_errors = 0; 70 70 #endif … … 75 75 struct uwsgi_app *wi; 76 76 77 void warn_pipe () {78 fprintf (stderr, "writing to a closed pipe/socket/fd !!!\n");77 void warn_pipe() { 78 fprintf(stderr, "writing to a closed pipe/socket/fd !!!\n"); 79 79 } 80 80 81 void gracefully_kill () {82 fprintf (stderr, "Gracefully killing worker %d...\n", uwsgi.mypid);81 void gracefully_kill() { 82 fprintf(stderr, "Gracefully killing worker %d...\n", uwsgi.mypid); 83 83 if (UWSGI_IS_IN_REQUEST) { 84 84 uwsgi.workers[uwsgi.mywid].manage_next_request = 0; 85 85 } 86 86 else { 87 reload_me ();87 reload_me(); 88 88 } 89 89 } 90 90 91 void reload_me () {92 exit (UWSGI_RELOAD_CODE);91 void reload_me() { 92 exit(UWSGI_RELOAD_CODE); 93 93 } 94 94 95 void end_me () {96 exit (UWSGI_END_CODE);95 void end_me() { 96 exit(UWSGI_END_CODE); 97 97 } 98 98 99 void goodbye_cruel_world () {100 fprintf (stderr, "...The work of process %d is done. Seeya!\n", getpid());101 exit (0);99 void goodbye_cruel_world() { 100 fprintf(stderr, "...The work of process %d is done. Seeya!\n", getpid()); 101 exit(0); 102 102 } 103 103 104 void kill_them_all () {104 void kill_them_all() { 105 105 int i; 106 106 uwsgi.to_hell = 1; 107 fprintf (stderr, "SIGINT/SIGQUIT received...killing workers...\n");107 fprintf(stderr, "SIGINT/SIGQUIT received...killing workers...\n"); 108 108 for (i = 1; i <= uwsgi.numproc; i++) { 109 kill (uwsgi.workers[i].pid, SIGINT);109 kill(uwsgi.workers[i].pid, SIGINT); 110 110 } 111 111 } 112 112 113 void grace_them_all () {113 void grace_them_all() { 114 114 int i; 115 115 uwsgi.to_heaven = 1; 116 fprintf (stderr, "...gracefully killing workers...\n");116 fprintf(stderr, "...gracefully killing workers...\n"); 117 117 for (i = 1; i <= uwsgi.numproc; i++) { 118 kill (uwsgi.workers[i].pid, SIGHUP);118 kill(uwsgi.workers[i].pid, SIGHUP); 119 119 } 120 120 } 121 121 122 void reap_them_all () {122 void reap_them_all() { 123 123 int i; 124 fprintf (stderr, "...brutally killing workers...\n");124 fprintf(stderr, "...brutally killing workers...\n"); 125 125 for (i = 1; i <= uwsgi.numproc; i++) { 126 kill (uwsgi.workers[i].pid, SIGTERM);126 kill(uwsgi.workers[i].pid, SIGTERM); 127 127 } 128 128 } 129 129 130 void harakiri () {130 void harakiri() { 131 131 132 132 PyThreadState *_myself; … … 139 139 end rock_solid */ 140 140 141 PyGILState_Ensure ();142 _myself = PyThreadState_Get ();141 PyGILState_Ensure(); 142 _myself = PyThreadState_Get(); 143 143 if (wi) { 144 144 /* … … 147 147 #else 148 148 */ 149 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);149 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); 150 150 151 151 if (wi->wsgi_harakiri) { 152 PyEval_CallObject (wi->wsgi_harakiri, wi->wsgi_args);153 if (PyErr_Occurred ()) {154 PyErr_Print ();155 } 156 } 157 } 158 Py_FatalError ("HARAKIRI !\n");152 PyEval_CallObject(wi->wsgi_harakiri, wi->wsgi_args); 153 if (PyErr_Occurred()) { 154 PyErr_Print(); 155 } 156 } 157 } 158 Py_FatalError("HARAKIRI !\n"); 159 159 } 160 160 161 161 #ifndef UNBIT 162 void stats () {162 void stats() { 163 163 struct uwsgi_app *ua = NULL; 164 164 int i; 165 165 166 fprintf (stderr, "*** pid %d stats ***\n", getpid());167 fprintf (stderr, "\ttotal requests: %llu\n", uwsgi.workers[0].requests);166 fprintf(stderr, "*** pid %d stats ***\n", getpid()); 167 fprintf(stderr, "\ttotal requests: %llu\n", uwsgi.workers[0].requests); 168 168 for (i = 0; i < uwsgi.wsgi_cnt; i++) { 169 169 ua = &uwsgi.wsgi_apps[i]; 170 170 if (ua) { 171 fprintf (stderr, "\tapp %d requests: %d\n", i, ua->requests);172 } 173 } 174 fprintf (stderr, "\n");171 fprintf(stderr, "\tapp %d requests: %d\n", i, ua->requests); 172 } 173 } 174 fprintf(stderr, "\n"); 175 175 } 176 176 #endif 177 177 178 void internal_server_error (int fd, char *message) {178 void internal_server_error(int fd, char *message) { 179 179 #ifndef UNBIT 180 180 if (uwsgi.shared->options[UWSGI_OPTION_CGI_MODE] == 0) { 181 181 #endif 182 wsgi_req.headers_size = write (fd, "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 63);182 wsgi_req.headers_size = write(fd, "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 63); 183 183 #ifndef UNBIT 184 184 } 185 185 else { 186 wsgi_req.headers_size = write (fd, "Status: 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 62);186 wsgi_req.headers_size = write(fd, "Status: 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n", 62); 187 187 } 188 188 wsgi_req.header_cnt = 2; 189 189 #endif 190 wsgi_req.response_size = write (fd, "<h1>uWSGI Error</h1>", 20);191 wsgi_req.response_size += write (fd, message, strlen(message));190 wsgi_req.response_size = write(fd, "<h1>uWSGI Error</h1>", 20); 191 wsgi_req.response_size += write(fd, message, strlen(message)); 192 192 } 193 193 194 PyObject *py_uwsgi_write (PyObject * self, PyObject * args) {194 PyObject *py_uwsgi_write(PyObject * self, PyObject * args) { 195 195 PyObject *data; 196 196 char *content; 197 197 int len; 198 data = PyTuple_GetItem (args, 0);199 if (PyString_Check (data)) {200 content = PyString_AsString (data);201 len = PyString_Size (data);198 data = PyTuple_GetItem(args, 0); 199 if (PyString_Check(data)) { 200 content = PyString_AsString(data); 201 len = PyString_Size(data); 202 202 203 203 #ifdef UWSGI_THREADING 204 204 if (uwsgi.has_threads && uwsgi.shared->options[UWSGI_OPTION_THREADS] == 1) { 205 Py_BEGIN_ALLOW_THREADS wsgi_req.response_size = write (uwsgi.poll.fd, content, len);205 Py_BEGIN_ALLOW_THREADS wsgi_req.response_size = write(uwsgi.poll.fd, content, len); 206 206 Py_END_ALLOW_THREADS} 207 207 else { 208 208 #endif 209 wsgi_req.response_size = write (uwsgi.poll.fd, content, len);209 wsgi_req.response_size = write(uwsgi.poll.fd, content, len); 210 210 #ifdef UNBIT 211 211 if (save_to_disk >= 0) { 212 if (write (save_to_disk, content, len) != len) {213 perror ("write()");214 close (save_to_disk);212 if (write(save_to_disk, content, len) != len) { 213 perror("write()"); 214 close(save_to_disk); 215 215 save_to_disk = -1; 216 216 } … … 223 223 #ifdef UNBIT 224 224 if (save_to_disk >= 0) { 225 close (save_to_disk);225 close(save_to_disk); 226 226 save_to_disk = -1; 227 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);228 } 229 #endif 230 Py_INCREF (Py_None);227 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); 228 } 229 #endif 230 Py_INCREF(Py_None); 231 231 return Py_None; 232 232 } … … 235 235 PyObject *wsgi_spitout; 236 236 237 PyObject *py_uwsgi_spit (PyObject * self, PyObject * args) {237 PyObject *py_uwsgi_spit(PyObject * self, PyObject * args) { 238 238 PyObject *headers, *head; 239 239 PyObject *h_key, *h_value; … … 249 249 250 250 251 head = PyTuple_GetItem (args, 0);251 head = PyTuple_GetItem(args, 0); 252 252 if (!head) { 253 253 goto clear; 254 254 } 255 255 256 if (!PyString_Check (head)) {257 fprintf (stderr, "http status must be a string !\n");256 if (!PyString_Check(head)) { 257 fprintf(stderr, "http status must be a string !\n"); 258 258 goto clear; 259 259 } … … 278 278 uwsgi.hvec[1].iov_len = 1; 279 279 #ifdef PYTHREE 280 uwsgi.hvec[2].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString(head));281 uwsgi.hvec[2].iov_len = strlen (uwsgi.hvec[2].iov_base);280 uwsgi.hvec[2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); 281 uwsgi.hvec[2].iov_len = strlen(uwsgi.hvec[2].iov_base); 282 282 #else 283 uwsgi.hvec[2].iov_base = PyString_AsString (head);284 uwsgi.hvec[2].iov_len = PyString_Size (head);285 #endif 286 wsgi_req.status = atoi (uwsgi.hvec[2].iov_base);283 uwsgi.hvec[2].iov_base = PyString_AsString(head); 284 uwsgi.hvec[2].iov_len = PyString_Size(head); 285 #endif 286 wsgi_req.status = atoi(uwsgi.hvec[2].iov_base); 287 287 uwsgi.hvec[3].iov_base = nl; 288 288 uwsgi.hvec[3].iov_len = NL_SIZE; … … 295 295 uwsgi.hvec[0].iov_len = 8; 296 296 #ifdef PYTHREE 297 uwsgi.hvec[1].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString(head));298 uwsgi.hvec[1].iov_len = strlen (uwsgi.hvec[1].iov_base);297 uwsgi.hvec[1].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(head)); 298 uwsgi.hvec[1].iov_len = strlen(uwsgi.hvec[1].iov_base); 299 299 #else 300 uwsgi.hvec[1].iov_base = PyString_AsString (head);301 uwsgi.hvec[1].iov_len = PyString_Size (head);302 #endif 303 wsgi_req.status = atoi (uwsgi.hvec[1].iov_base);300 uwsgi.hvec[1].iov_base = PyString_AsString(head); 301 uwsgi.hvec[1].iov_len = PyString_Size(head); 302 #endif 303 wsgi_req.status = atoi(uwsgi.hvec[1].iov_base); 304 304 uwsgi.hvec[2].iov_base = nl; 305 305 uwsgi.hvec[2].iov_len = NL_SIZE; … … 311 311 if (wsgi_req.unbit_flags & (unsigned long long) 1) { 312 312 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') { 313 save_to_disk = openat (tmp_dir_fd, tmp_filename, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP);314 } 315 } 316 #endif 317 318 319 headers = PyTuple_GetItem (args, 1);313 save_to_disk = openat(tmp_dir_fd, tmp_filename, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP); 314 } 315 } 316 #endif 317 318 319 headers = PyTuple_GetItem(args, 1); 320 320 if (!headers) { 321 321 goto clear; 322 322 } 323 if (!PyList_Check (headers)) {324 fprintf (stderr, "http headers must be in a python list\n");323 if (!PyList_Check(headers)) { 324 fprintf(stderr, "http headers must be in a python list\n"); 325 325 goto clear; 326 326 } 327 wsgi_req.header_cnt = PyList_Size (headers);327 wsgi_req.header_cnt = PyList_Size(headers); 328 328 329 329 … … 334 334 for (i = 0; i < wsgi_req.header_cnt; i++) { 335 335 j = (i * 4) + base; 336 head = PyList_GetItem (headers, i);336 head = PyList_GetItem(headers, i); 337 337 if (!head) { 338 338 goto clear; 339 339 } 340 if (!PyTuple_Check (head)) {341 fprintf (stderr, "http header must be defined in a tuple !\n");340 if (!PyTuple_Check(head)) { 341 fprintf(stderr, "http header must be defined in a tuple !\n"); 342 342 goto clear; 343 343 } 344 h_key = PyTuple_GetItem (head, 0);344 h_key = PyTuple_GetItem(head, 0); 345 345 if (!h_key) { 346 346 goto clear; 347 347 } 348 h_value = PyTuple_GetItem (head, 1);348 h_value = PyTuple_GetItem(head, 1); 349 349 if (!h_value) { 350 350 goto clear; 351 351 } 352 352 #ifdef PYTHREE 353 uwsgi.hvec[j].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString(h_key));354 uwsgi.hvec[j].iov_len = strlen (uwsgi.hvec[j].iov_base);353 uwsgi.hvec[j].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_key)); 354 uwsgi.hvec[j].iov_len = strlen(uwsgi.hvec[j].iov_base); 355 355 #else 356 uwsgi.hvec[j].iov_base = PyString_AsString (h_key);357 uwsgi.hvec[j].iov_len = PyString_Size (h_key);356 uwsgi.hvec[j].iov_base = PyString_AsString(h_key); 357 uwsgi.hvec[j].iov_len = PyString_Size(h_key); 358 358 #endif 359 359 uwsgi.hvec[j + 1].iov_base = h_sep; 360 360 uwsgi.hvec[j + 1].iov_len = H_SEP_SIZE; 361 361 #ifdef PYTHREE 362 uwsgi.hvec[j + 2].iov_base = PyBytes_AsString (PyUnicode_AsASCIIString(h_value));363 uwsgi.hvec[j + 2].iov_len = strlen (uwsgi.hvec[j + 2].iov_base);362 uwsgi.hvec[j + 2].iov_base = PyBytes_AsString(PyUnicode_AsASCIIString(h_value)); 363 uwsgi.hvec[j + 2].iov_len = strlen(uwsgi.hvec[j + 2].iov_base); 364 364 #else 365 uwsgi.hvec[j + 2].iov_base = PyString_AsString (h_value);366 uwsgi.hvec[j + 2].iov_len = PyString_Size (h_value);365 uwsgi.hvec[j + 2].iov_base = PyString_AsString(h_value); 366 uwsgi.hvec[j + 2].iov_len = PyString_Size(h_value); 367 367 #endif 368 368 uwsgi.hvec[j + 3].iov_base = nl; … … 375 375 if (save_to_disk >= 0) { 376 376 for (j = 0; j < i; j += 4) { 377 if (!strncasecmp (uwsgi.hvec[j].iov_base, "Set-Cookie", uwsgi.hvec[j].iov_len)) {378 close (save_to_disk);377 if (!strncasecmp(uwsgi.hvec[j].iov_base, "Set-Cookie", uwsgi.hvec[j].iov_len)) { 378 close(save_to_disk); 379 379 save_to_disk = -1; 380 380 break; … … 389 389 uwsgi.hvec[j].iov_len = NL_SIZE; 390 390 391 wsgi_req.headers_size = writev (uwsgi.poll.fd, uwsgi.hvec, j + 1);391 wsgi_req.headers_size = writev(uwsgi.poll.fd, uwsgi.hvec, j + 1); 392 392 if (wsgi_req.headers_size < 0) { 393 perror ("writev()");394 } 395 Py_INCREF (wsgi_writeout);393 perror("writev()"); 394 } 395 Py_INCREF(wsgi_writeout); 396 396 397 397 … … 400 400 clear: 401 401 402 Py_INCREF (Py_None);402 Py_INCREF(Py_None); 403 403 return Py_None; 404 404 } 405 405 406 406 #ifdef UWSGI_SENDFILE 407 PyObject *py_uwsgi_sendfile (PyObject * self, PyObject * args) {408 409 //PyObject *zero ;410 411 uwsgi.py_sendfile = PyTuple_GetItem(args, 0);407 PyObject *py_uwsgi_sendfile(PyObject * self, PyObject * args) { 408 409 //PyObject *zero ; 410 411 uwsgi.py_sendfile = PyTuple_GetItem(args, 0); 412 412 413 413 #ifdef PYTHREE 414 if ((wsgi_req.sendfile_fd = PyObject_AsFileDescriptor(uwsgi.py_sendfile)) >= 0) {415 Py_INCREF(uwsgi.py_sendfile);416 }414 if ((wsgi_req.sendfile_fd = PyObject_AsFileDescriptor(uwsgi.py_sendfile)) >= 0) { 415 Py_INCREF(uwsgi.py_sendfile); 416 } 417 417 #else 418 if (PyFile_Check(uwsgi.py_sendfile)) {419 //zero = PyFile_Name(uwsgi.py_sendfile) ;420 //fprintf(stderr,"->serving %s as static file...", PyString_AsString(zero));421 wsgi_req.sendfile_fd = PyObject_AsFileDescriptor(uwsgi.py_sendfile);422 Py_INCREF(uwsgi.py_sendfile);423 }424 #endif 425 426 427 return PyTuple_New(0);418 if (PyFile_Check(uwsgi.py_sendfile)) { 419 //zero = PyFile_Name(uwsgi.py_sendfile) ; 420 //fprintf(stderr,"->serving %s as static file...", PyString_AsString(zero)); 421 wsgi_req.sendfile_fd = PyObject_AsFileDescriptor(uwsgi.py_sendfile); 422 Py_INCREF(uwsgi.py_sendfile); 423 } 424 #endif 425 426 427 return PyTuple_New(0); 428 428 } 429 429 #endif … … 461 461 } 462 462 463 int main (int argc, char *argv[], char *envp[]) {464 465 uint64_t master_cycles = 0 ;463 int main(int argc, char *argv[], char *envp[]) { 464 465 uint64_t master_cycles = 0; 466 466 struct timeval check_interval = {.tv_sec = 1,.tv_usec = 0 }; 467 467 … … 474 474 475 475 struct sockaddr_un c_addr; 476 int c_len = sizeof (struct sockaddr_un);476 int c_len = sizeof(struct sockaddr_un); 477 477 int i; 478 478 int ret; … … 481 481 482 482 #ifdef UWSGI_NAGIOS 483 int nagios = 0 ;483 int nagios = 0; 484 484 #endif 485 485 … … 489 489 #endif 490 490 491 char *udp_socket = NULL;492 491 struct pollfd uwsgi_poll; 493 492 struct sockaddr_in udp_client; … … 509 508 int ready_to_die = 0; 510 509 511 char *env_reloads ;510 char *env_reloads; 512 511 unsigned int reloads = 0; 513 512 char env_reload_buf[11]; … … 522 521 523 522 /* anti signal bombing */ 524 signal (SIGHUP, SIG_IGN);525 signal (SIGTERM, SIG_IGN);526 527 memset (&uwsgi, 0, sizeof(struct uwsgi_server));523 signal(SIGHUP, SIG_IGN); 524 signal(SIGTERM, SIG_IGN); 525 526 memset(&uwsgi, 0, sizeof(struct uwsgi_server)); 528 527 529 528 /* generic shared area */ 530 uwsgi.shared = (struct uwsgi_shared *) mmap (NULL, sizeof(struct uwsgi_shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);529 uwsgi.shared = (struct uwsgi_shared *) mmap(NULL, sizeof(struct uwsgi_shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 531 530 if (!uwsgi.shared) { 532 perror ("mmap()");533 exit (1);534 } 535 memset (uwsgi.shared, 0, sizeof(struct uwsgi_shared));536 537 for (i=0;i<0xFF;i++) {531 perror("mmap()"); 532 exit(1); 533 } 534 memset(uwsgi.shared, 0, sizeof(struct uwsgi_shared)); 535 536 for (i = 0; i < 0xFF; i++) { 538 537 uwsgi.shared->hooks[i] = unconfigured_hook; 539 538 uwsgi.shared->after_hooks[i] = unconfigured_after_hook; … … 598 597 {"limit-as", required_argument, 0, LONG_ARGS_LIMIT_AS}, 599 598 {"udp", required_argument, 0, LONG_ARGS_UDP}, 599 {"snmp", no_argument, &uwsgi.snmp, 1}, 600 600 {"check-interval", required_argument, 0, LONG_ARGS_CHECK_INTERVAL}, 601 601 {"erlang", required_argument, 0, LONG_ARGS_ERLANG}, … … 613 613 614 614 615 gettimeofday (&uwsgi.start_tv, NULL);616 617 setlinebuf (stdout);615 gettimeofday(&uwsgi.start_tv, NULL); 616 617 setlinebuf(stdout); 618 618 619 619 uwsgi.rl.rlim_cur = 0; … … 627 627 reloads++; 628 628 // convert reloads to string 629 rlen = snprintf(env_reload_buf, 10, "%u", reloads) ;629 rlen = snprintf(env_reload_buf, 10, "%u", reloads); 630 630 if (rlen > 0) { 631 env_reload_buf[rlen] = 0 ;632 if (setenv("UWSGI_RELOADS", env_reload_buf, 1)) {631 env_reload_buf[rlen] = 0; 632 if (setenv("UWSGI_RELOADS", env_reload_buf, 1)) { 633 633 perror("setenv()"); 634 634 } … … 636 636 } 637 637 else { 638 if (setenv("UWSGI_RELOADS", "0", 1)) {638 if (setenv("UWSGI_RELOADS", "0", 1)) { 639 639 perror("setenv()"); 640 640 } 641 641 } 642 642 643 socket_type_len = sizeof (int);644 if (!getsockopt (3, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) {645 if (socket_type == SOCK_STREAM && reloads > 0) { 646 fprintf (stderr, "...fd 3 is a socket, i suppose this is a graceful reload of uWSGI, i will try to do my best...\n");643 socket_type_len = sizeof(int); 644 if (!getsockopt(3, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 645 if (socket_type == SOCK_STREAM && reloads > 0) { 646 fprintf(stderr, "...fd 3 is a socket, i suppose this is a graceful reload of uWSGI, i will try to do my best...\n"); 647 647 uwsgi.is_a_reload = 1; 648 648 #ifdef UNBIT 649 649 /* discard the 3'th fd as we will use the fd 0 */ 650 close (3);650 close(3); 651 651 #else 652 652 uwsgi.serverfd = 3; … … 655 655 } 656 656 657 uwsgi.binary_path = argv[0] ;657 uwsgi.binary_path = argv[0]; 658 658 659 659 #ifndef UNBIT 660 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) {660 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) { 661 661 #else 662 while ((i = getopt (argc, argv, "p:t:mTPiv:b:rMR:Sz:w:C:j:H:A:EQ:L")) != -1) {662 while ((i = getopt(argc, argv, "p:t:mTPiv:b:rMR:Sz:w:C:j:H:A:EQ:L")) != -1) { 663 663 #endif 664 664 manage_opt(i, optarg); … … 667 667 668 668 #ifdef UWSGI_XML 669 if (uwsgi.xml_config != NULL) {670 uwsgi_xml_config(&wsgi_req, long_options);671 }669 if (uwsgi.xml_config != NULL) { 670 uwsgi_xml_config(&wsgi_req, long_options); 671 } 672 672 #endif 673 673 674 674 if (uwsgi.binary_path == argv[0]) { 675 cwd = uwsgi_get_cwd ();676 uwsgi.binary_path = malloc (strlen(argv[0]) + 1);675 cwd = uwsgi_get_cwd(); 676 uwsgi.binary_path = malloc(strlen(argv[0]) + 1); 677 677 if (uwsgi.binary_path == NULL) { 678 perror ("malloc()");679 exit (1);680 } 681 strcpy (uwsgi.binary_path, argv[0]);678 perror("malloc()"); 679 exit(1); 680 } 681 strcpy(uwsgi.binary_path, argv[0]); 682 682 } 683 683 … … 686 686 #endif 687 687 if (uwsgi.test_module == NULL) { 688 fprintf (stderr, "*** Starting uWSGI %s (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof (void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec));688 fprintf(stderr, "*** Starting uWSGI %s (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof(void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec)); 689 689 } 690 690 #ifndef UNBIT 691 691 } 692 692 else { 693 fprintf (stderr, "*** Starting uWSGI %s (CGI mode) (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof (void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec));693 fprintf(stderr, "*** Starting uWSGI %s (CGI mode) (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof(void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec)); 694 694 } 695 695 #endif 696 696 697 697 #ifdef __BIG_ENDIAN__ 698 fprintf (stderr, "*** big endian arch detected ***\n");698 fprintf(stderr, "*** big endian arch detected ***\n"); 699 699 #endif 700 700 701 701 #ifdef PYTHREE 702 fprintf (stderr, "*** Warning Python3.x support is experimental, do not use it in production environment ***\n");703 #endif 704 705 fprintf (stderr, "Python version: %s\n", Py_GetVersion());702 fprintf(stderr, "*** Warning Python3.x support is experimental, do not use it in production environment ***\n"); 703 #endif 704 705 fprintf(stderr, "Python version: %s\n", Py_GetVersion()); 706 706 707 707 #ifndef UNBIT 708 if (!getuid ()) {709 fprintf (stderr, "uWSGI running as root, you can use --uid/--gid/--chroot options\n");708 if (!getuid()) { 709 fprintf(stderr, "uWSGI running as root, you can use --uid/--gid/--chroot options\n"); 710 710 if (uwsgi.chroot) { 711 fprintf (stderr, "chroot() to %s\n", uwsgi.chroot);712 if (chroot (uwsgi.chroot)) {713 perror ("chroot()");714 exit (1);711 fprintf(stderr, "chroot() to %s\n", uwsgi.chroot); 712 if (chroot(uwsgi.chroot)) { 713 perror("chroot()"); 714 exit(1); 715 715 } 716 716 #ifdef __linux__ 717 717 if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG]) { 718 fprintf (stderr, "*** Warning, on linux system you have to bind-mount the /proc fs in your chroot to get memory debug/report.\n");718 fprintf(stderr, "*** Warning, on linux system you have to bind-mount the /proc fs in your chroot to get memory debug/report.\n"); 719 719 } 720 720 #endif 721 721 } 722 722 if (uwsgi.gid) { 723 fprintf (stderr, "setgid() to %d\n", uwsgi.gid);724 if (setgid (uwsgi.gid)) {725 perror ("setgid()");726 exit (1);723 fprintf(stderr, "setgid() to %d\n", uwsgi.gid); 724 if (setgid(uwsgi.gid)) { 725 perror("setgid()"); 726 exit(1); 727 727 } 728 728 } 729 729 if (uwsgi.uid) { 730 fprintf (stderr, "setuid() to %d\n", uwsgi.uid);731 if (setuid (uwsgi.uid)) {732 perror ("setuid()");733 exit (1);730 fprintf(stderr, "setuid() to %d\n", uwsgi.uid); 731 if (setuid(uwsgi.uid)) { 732 perror("setuid()"); 733 exit(1); 734 734 } 735 735 } 736 736 737 737 if (!getuid()) { 738 fprintf(stderr, " *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** \n");738 fprintf(stderr, " *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** \n"); 739 739 } 740 740 } 741 741 else { 742 742 if (uwsgi.chroot) { 743 fprintf (stderr, "cannot chroot() as non-root user\n");744 exit (1);743 fprintf(stderr, "cannot chroot() as non-root user\n"); 744 exit(1); 745 745 } 746 746 if (uwsgi.gid) { 747 fprintf (stderr, "cannot setgid() as non-root user\n");748 exit (1);747 fprintf(stderr, "cannot setgid() as non-root user\n"); 748 exit(1); 749 749 } 750 750 if (uwsgi.uid) { 751 fprintf (stderr, "cannot setuid() as non-root user\n");752 exit (1);753 } 754 } 755 756 751 fprintf(stderr, "cannot setuid() as non-root user\n"); 752 exit(1); 753 } 754 } 755 756 757 757 758 758 #endif … … 760 760 #ifndef UNBIT 761 761 if (uwsgi.rl.rlim_max > 0) { 762 fprintf (stderr, "limiting address space of processes...\n");763 if (setrlimit (RLIMIT_AS, &uwsgi.rl)) {764 perror ("setrlimit()");765 } 766 } 767 #endif 768 769 if (!getrlimit (RLIMIT_AS, &uwsgi.rl)) {762 fprintf(stderr, "limiting address space of processes...\n"); 763 if (setrlimit(RLIMIT_AS, &uwsgi.rl)) { 764 perror("setrlimit()"); 765 } 766 } 767 #endif 768 769 if (!getrlimit(RLIMIT_AS, &uwsgi.rl)) { 770 770 #ifndef UNBIT 771 771 // check for overflow 772 if ((sizeof (void *) == 4 && (uint32_t) uwsgi.rl.rlim_max < UINT32_MAX) || (sizeof(void *) == 8 && (uint64_t) uwsgi.rl.rlim_max < UINT64_MAX)) {773 #endif 774 fprintf (stderr, "your process address space limit is %lld bytes (%lld MB)\n", (long long) uwsgi.rl.rlim_max, (long long) uwsgi.rl.rlim_max / 1024 / 1024);772 if ((sizeof(void *) == 4 && (uint32_t) uwsgi.rl.rlim_max < UINT32_MAX) || (sizeof(void *) == 8 && (uint64_t) uwsgi.rl.rlim_max < UINT64_MAX)) { 773 #endif 774 fprintf(stderr, "your process address space limit is %lld bytes (%lld MB)\n", (long long) uwsgi.rl.rlim_max, (long long) uwsgi.rl.rlim_max / 1024 / 1024); 775 775 #ifndef UNBIT 776 776 } … … 778 778 } 779 779 780 uwsgi.page_size = getpagesize ();781 fprintf (stderr, "your memory page size is %d bytes\n", uwsgi.page_size);780 uwsgi.page_size = getpagesize(); 781 fprintf(stderr, "your memory page size is %d bytes\n", uwsgi.page_size); 782 782 783 783 784 784 if (uwsgi.synclog) { 785 fprintf (stderr, "allocating a memory page for synced logging.\n");786 uwsgi.sync_page = malloc (uwsgi.page_size);785 fprintf(stderr, "allocating a memory page for synced logging.\n"); 786 uwsgi.sync_page = malloc(uwsgi.page_size); 787 787 if (!uwsgi.sync_page) { 788 perror ("malloc()");789 exit (1);788 perror("malloc()"); 789 exit(1); 790 790 } 791 791 } 792 792 793 793 if (uwsgi.pyhome != NULL) { 794 fprintf (stderr, "Setting PythonHome to %s...\n", uwsgi.pyhome);794 fprintf(stderr, "Setting PythonHome to %s...\n", uwsgi.pyhome); 795 795 #ifdef PYTHREE 796 796 wchar_t *wpyhome; 797 wpyhome = malloc ((sizeof (wchar_t) * strlen(uwsgi.pyhome)) + 2);797 wpyhome = malloc((sizeof(wchar_t) * strlen(uwsgi.pyhome)) + 2); 798 798 if (!wpyhome) { 799 perror ("malloc()");800 exit (1);801 } 802 mbstowcs (wpyhome, uwsgi.pyhome, strlen(uwsgi.pyhome));803 Py_SetPythonHome (wpyhome);804 free (wpyhome);799 perror("malloc()"); 800 exit(1); 801 } 802 mbstowcs(wpyhome, uwsgi.pyhome, strlen(uwsgi.pyhome)); 803 Py_SetPythonHome(wpyhome); 804 free(wpyhome); 805 805 #else 806 Py_SetPythonHome (uwsgi.pyhome);806 Py_SetPythonHome(uwsgi.pyhome); 807 807 #endif 808 808 } … … 813 813 #ifdef PYTHREE 814 814 wchar_t pname[6]; 815 mbstowcs (pname, "uWSGI", 6);816 Py_SetProgramName (pname);815 mbstowcs(pname, "uWSGI", 6); 816 Py_SetProgramName(pname); 817 817 #else 818 Py_SetProgramName ("uWSGI");819 #endif 820 821 822 Py_Initialize ();818 Py_SetProgramName("uWSGI"); 819 #endif 820 821 822 Py_Initialize(); 823 823 824 824 pyargv[0] = "uwsgi"; … … 826 826 if (uwsgi.pyargv != NULL) { 827 827 char *ap; 828 while ((ap = strsep (&uwsgi.pyargv, " \t")) != NULL) {828 while ((ap = strsep(&uwsgi.pyargv, " \t")) != NULL) { 829 829 if (*ap != '\0') { 830 830 pyargv[pyargc] = ap; … … 836 836 } 837 837 838 PySys_SetArgv (pyargc, pyargv);839 840 841 uwsgi.py_apps = PyDict_New ();838 PySys_SetArgv(pyargc, pyargv); 839 840 841 uwsgi.py_apps = PyDict_New(); 842 842 if (!uwsgi.py_apps) { 843 PyErr_Print ();844 exit (1);845 } 846 847 848 wsgi_spitout = PyCFunction_New (uwsgi_spit_method, NULL);849 wsgi_writeout = PyCFunction_New (uwsgi_write_method, NULL);843 PyErr_Print(); 844 exit(1); 845 } 846 847 848 wsgi_spitout = PyCFunction_New(uwsgi_spit_method, NULL); 849 wsgi_writeout = PyCFunction_New(uwsgi_write_method, NULL); 850 850 851 851 #ifdef UWSGI_EMBEDDED 852 uwsgi_module = Py_InitModule ("uwsgi", null_methods);852 uwsgi_module = Py_InitModule("uwsgi", null_methods); 853 853 if (uwsgi_module == NULL) { 854 fprintf (stderr, "could not initialize the uwsgi python module\n");855 exit (1);854 fprintf(stderr, "could not initialize the uwsgi python module\n"); 855 exit(1); 856 856 } 857 857 if (uwsgi.sharedareasize > 0) { 858 858 #ifndef __OpenBSD__ 859 uwsgi.sharedareamutex = mmap (NULL, sizeof (pthread_mutexattr_t) + sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);859 uwsgi.sharedareamutex = mmap(NULL, sizeof(pthread_mutexattr_t) + sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 860 860 if (!uwsgi.sharedareamutex) { 861 perror ("mmap()");862 exit (1);861 perror("mmap()"); 862 exit(1); 863 863 } 864 864 #else 865 fprintf (stderr, "***WARNING*** the sharedarea on OpenBSD is not SMP-safe. Beware of race conditions !!!\n");866 #endif 867 uwsgi.sharedarea = mmap (NULL, uwsgi.page_size * uwsgi.sharedareasize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);865 fprintf(stderr, "***WARNING*** the sharedarea on OpenBSD is not SMP-safe. Beware of race conditions !!!\n"); 866 #endif 867 uwsgi.sharedarea = mmap(NULL, uwsgi.page_size * uwsgi.sharedareasize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 868 868 if (uwsgi.sharedarea) { 869 fprintf (stderr, "shared area mapped at %p, you can access it with uwsgi.sharedarea* functions.\n", uwsgi.sharedarea);869 fprintf(stderr, "shared area mapped at %p, you can access it with uwsgi.sharedarea* functions.\n", uwsgi.sharedarea); 870 870 871 871 #ifdef __APPLE__ 872 memset (uwsgi.sharedareamutex, 0, sizeof(OSSpinLock));872 memset(uwsgi.sharedareamutex, 0, sizeof(OSSpinLock)); 873 873 #else 874 874 #ifndef __OpenBSD__ 875 if (pthread_mutexattr_init ((pthread_mutexattr_t *) uwsgi.sharedareamutex)) {876 fprintf (stderr, "unable to allocate mutexattr structure\n");877 exit (1);878 } 879 if (pthread_mutexattr_setpshared ((pthread_mutexattr_t *) uwsgi.sharedareamutex, PTHREAD_PROCESS_SHARED)) {880 fprintf (stderr, "unable to share mutex\n");881 exit (1);882 } 883 if (pthread_mutex_init ((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t), (pthread_mutexattr_t *) uwsgi.sharedareamutex)) {884 fprintf (stderr, "unable to initialize mutex\n");885 exit (1);875 if (pthread_mutexattr_init((pthread_mutexattr_t *) uwsgi.sharedareamutex)) { 876 fprintf(stderr, "unable to allocate mutexattr structure\n"); 877 exit(1); 878 } 879 if (pthread_mutexattr_setpshared((pthread_mutexattr_t *) uwsgi.sharedareamutex, PTHREAD_PROCESS_SHARED)) { 880 fprintf(stderr, "unable to share mutex\n"); 881 exit(1); 882 } 883 if (pthread_mutex_init((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t), (pthread_mutexattr_t *) uwsgi.sharedareamutex)) { 884 fprintf(stderr, "unable to initialize mutex\n"); 885 exit(1); 886 886 } 887 887 #endif … … 890 890 } 891 891 else { 892 perror ("mmap()");893 exit (1);894 } 895 896 } 897 898 init_uwsgi_embedded_module ();892 perror("mmap()"); 893 exit(1); 894 } 895 896 } 897 898 init_uwsgi_embedded_module(); 899 899 #endif 900 900 … … 904 904 905 905 906 wi = malloc (sizeof(struct uwsgi_app));906 wi = malloc(sizeof(struct uwsgi_app)); 907 907 if (wi == NULL) { 908 perror ("malloc()");909 exit (1);910 } 911 memset (wi, 0, sizeof(struct uwsgi_app));908 perror("malloc()"); 909 exit(1); 910 } 911 memset(wi, 0, sizeof(struct uwsgi_app)); 912 912 913 913 // load wsgi module/script 914 914 for (i = optind; i < argc; i++) { 915 init_uwsgi_vars ();916 wi->wsgi_module = PyImport_ImportModule (argv[i]);915 init_uwsgi_vars(); 916 wi->wsgi_module = PyImport_ImportModule(argv[i]); 917 917 if (!wi->wsgi_module) { 918 PyErr_Print ();919 exit (1);920 } 921 wi->wsgi_dict = PyModule_GetDict (wi->wsgi_module);918 PyErr_Print(); 919 exit(1); 920 } 921 wi->wsgi_dict = PyModule_GetDict(wi->wsgi_module); 922 922 if (!wi->wsgi_dict) { 923 PyErr_Print ();924 exit (1);925 } 926 wi->wsgi_callable = PyDict_GetItemString (wi->wsgi_dict, "application");923 PyErr_Print(); 924 exit(1); 925 } 926 wi->wsgi_callable = PyDict_GetItemString(wi->wsgi_dict, "application"); 927 927 if (!wi->wsgi_callable) { 928 PyErr_Print ();929 exit (1);930 } 931 wi->wsgi_environ = PyDict_New ();928 PyErr_Print(); 929 exit(1); 930 } 931 wi->wsgi_environ = PyDict_New(); 932 932 if (!wi->wsgi_environ) { 933 PyErr_Print ();934 exit (1);935 } 936 937 wi->wsgi_harakiri = PyDict_GetItemString (wi->wsgi_dict, "harakiri");938 939 wi->wsgi_args = PyTuple_New (2);933 PyErr_Print(); 934 exit(1); 935 } 936 937 wi->wsgi_harakiri = PyDict_GetItemString(wi->wsgi_dict, "harakiri"); 938 939 wi->wsgi_args = PyTuple_New(2); 940 940 if (!wi->wsgi_args) { 941 PyErr_Print ();942 exit (1);943 } 944 if (PyTuple_SetItem (wi->wsgi_args, 0, wi->wsgi_environ)) {945 PyErr_Print ();946 exit (1);947 } 948 if (PyTuple_SetItem (wi->wsgi_args, 1, wsgi_spitout)) {949 PyErr_Print ();950 exit (1);941 PyErr_Print(); 942 exit(1); 943 } 944 if (PyTuple_SetItem(wi->wsgi_args, 0, wi->wsgi_environ)) { 945 PyErr_Print(); 946 exit(1); 947 } 948 if (PyTuple_SetItem(wi->wsgi_args, 1, wsgi_spitout)) { 949 PyErr_Print(); 950 exit(1); 951 951 } 952 952 break; … … 954 954 955 955 if (!wi->wsgi_module) { 956 fprintf (stderr, "unable to find the wsgi script. Have you specified it ?\n");957 exit (1);956 fprintf(stderr, "unable to find the wsgi script. Have you specified it ?\n"); 957 exit(1); 958 958 } 959 959 #endif … … 961 961 Py_OptimizeFlag = uwsgi.py_optimize; 962 962 963 uwsgi.main_thread = PyThreadState_Get ();963 uwsgi.main_thread = PyThreadState_Get(); 964 964 965 965 966 966 #ifdef UWSGI_THREADING 967 967 if (uwsgi.has_threads) { 968 PyEval_InitThreads ();969 fprintf (stderr, "threads support enabled\n");968 PyEval_InitThreads(); 969 fprintf(stderr, "threads support enabled\n"); 970 970 } 971 971 … … 973 973 974 974 if (uwsgi.buffer_size > 65536) { 975 fprintf (stderr, "invalid buffer size.\n");976 exit (1);977 } 978 uwsgi.buffer = malloc (uwsgi.buffer_size);975 fprintf(stderr, "invalid buffer size.\n"); 976 exit(1); 977 } 978 uwsgi.buffer = malloc(uwsgi.buffer_size); 979 979 if (uwsgi.buffer == NULL) { 980 fprintf (stderr, "unable to allocate memory for buffer.\n");981 exit (1);982 } 983 984 fprintf (stderr, "request/response buffer (%d bytes) allocated.\n", uwsgi.buffer_size);980 fprintf(stderr, "unable to allocate memory for buffer.\n"); 981 exit(1); 982 } 983 984 fprintf(stderr, "request/response buffer (%d bytes) allocated.\n", uwsgi.buffer_size); 985 985 986 986 #ifdef UWSGI_NAGIOS … … 988 988 // connect and send 989 989 if (uwsgi.socket_name == NULL) { 990 fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n");990 fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n"); 991 991 exit(3); 992 992 } 993 char *tcp_port = strchr (uwsgi.socket_name, ':');994 if (tcp_port == NULL) {995 fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n");993 char *tcp_port = strchr(uwsgi.socket_name, ':'); 994 if (tcp_port == NULL) { 995 fprintf(stdout, "UWSGI UNKNOWN: you have specified an invalid socket\n"); 996 996 exit(3); 997 997 } 998 998 999 tcp_port[0] = 0 ;1000 1001 uwsgi_poll.fd = connect_to_tcp(uwsgi.socket_name, atoi(tcp_port +1), uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]);999 tcp_port[0] = 0; 1000 1001 uwsgi_poll.fd = connect_to_tcp(uwsgi.socket_name, atoi(tcp_port + 1), uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT]); 1002 1002 if (uwsgi_poll.fd < 0) { 1003 fprintf(stdout, "UWSGI CRITICAL: could not connect() to workers\n");1003 fprintf(stdout, "UWSGI CRITICAL: could not connect() to workers\n"); 1004 1004 exit(2); 1005 1005 } 1006 wsgi_req.modifier = UWSGI_MODIFIER_PING ;1007 wsgi_req.size = 0 ;1008 wsgi_req.modifier_arg = 0 ;1006 wsgi_req.modifier = UWSGI_MODIFIER_PING; 1007 wsgi_req.size = 0; 1008 wsgi_req.modifier_arg = 0; 1009 1009 if (write(uwsgi_poll.fd, &wsgi_req, 4) != 4) { 1010 1010 perror("write()"); 1011 fprintf(stdout, "UWSGI CRITICAL: could not send ping packet to workers\n");1011 fprintf(stdout, "UWSGI CRITICAL: could not send ping packet to workers\n"); 1012 1012 exit(2); 1013 1013 } 1014 uwsgi_poll.events = POLLIN ;1015 if (!uwsgi_parse_response (&uwsgi_poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) &wsgi_req, uwsgi.buffer)) {1016 fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n");1017 exit(2); 1014 uwsgi_poll.events = POLLIN; 1015 if (!uwsgi_parse_response(&uwsgi_poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) &wsgi_req, uwsgi.buffer)) { 1016 fprintf(stdout, "UWSGI CRITICAL: timed out waiting for response\n"); 1017 exit(2); 1018 1018 } 1019 1019 else { 1020 1020 if (wsgi_req.size > 0) { 1021 fprintf(stdout, "UWSGI WARNING: %.*s\n", wsgi_req.size, uwsgi.buffer);1021 fprintf(stdout, "UWSGI WARNING: %.*s\n", wsgi_req.size, uwsgi.buffer); 1022 1022 exit(1); 1023 1023 } 1024 1024 else { 1025 fprintf(stdout, "UWSGI OK: armed and ready\n");1025 fprintf(stdout, "UWSGI OK: armed and ready\n"); 1026 1026 exit(0); 1027 1027 } … … 1029 1029 1030 1030 // never here 1031 fprintf(stdout, "UWSGI UNKNOWN: probably you hit a bug of uWSGI !!!\n");1031 fprintf(stdout, "UWSGI UNKNOWN: probably you hit a bug of uWSGI !!!\n"); 1032 1032 exit(3); 1033 1033 1034 1034 } 1035 1035 #endif … … 1039 1039 if (uwsgi.socket_name != NULL && !uwsgi.is_a_reload) { 1040 1040 #ifdef UWSGI_SCTP 1041 if (!strncmp (uwsgi.socket_name, "sctp:", 5)) {1042 char *sctp_port = strchr (uwsgi.socket_name + 5, ':');1041 if (!strncmp(uwsgi.socket_name, "sctp:", 5)) { 1042 char *sctp_port = strchr(uwsgi.socket_name + 5, ':'); 1043 1043 if (sctp_port == NULL) { 1044 fprintf (stderr, "invalid SCTP port ! syntax: sctp:ip1,ip2,ipN:port\n");1045 exit (1);1044 fprintf(stderr, "invalid SCTP port ! syntax: sctp:ip1,ip2,ipN:port\n"); 1045 exit(1); 1046 1046 } 1047 uwsgi.serverfd = bind_to_sctp (uwsgi.socket_name + 5, uwsgi.listen_queue, sctp_port);1047 uwsgi.serverfd = bind_to_sctp(uwsgi.socket_name + 5, uwsgi.listen_queue, sctp_port); 1048 1048 i_am_sctp = 1; 1049 1049 } 1050 1050 else { 1051 1051 #endif 1052 char *tcp_port = strchr (uwsgi.socket_name, ':');1052 char *tcp_port = strchr(uwsgi.socket_name, ':'); 1053 1053 if (tcp_port == NULL) { 1054 uwsgi.serverfd = bind_to_unix (uwsgi.socket_name, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket);1054 uwsgi.serverfd = bind_to_unix(uwsgi.socket_name, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket); 1055 1055 } 1056 1056 else { 1057 uwsgi.serverfd = bind_to_tcp (uwsgi.socket_name, uwsgi.listen_queue, tcp_port);1057 uwsgi.serverfd = bind_to_tcp(uwsgi.socket_name, uwsgi.listen_queue, tcp_port); 1058 1058 } 1059 1059 1060 1060 if (uwsgi.serverfd < 0) { 1061 fprintf (stderr, "unable to create the server socket.\n");1062 exit (1);1061 fprintf(stderr, "unable to create the server socket.\n"); 1062 exit(1); 1063 1063 } 1064 1064 #ifdef UWSGI_SCTP … … 1068 1068 #endif 1069 1069 1070 socket_type_len = sizeof (int);1071 if (getsockopt (uwsgi.serverfd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) {1070 socket_type_len = sizeof(int); 1071 if (getsockopt(uwsgi.serverfd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 1072 1072 //perror ("getsockopt()"); 1073 uwsgi.numproc = 0 ;1073 uwsgi.numproc = 0; 1074 1074 } 1075 1075 … … 1089 1089 #endif 1090 1090 fprintf(stderr, "The -s/--socket option is missing and stdin is not a socket.\n"); 1091 exit (1);1091 exit(1); 1092 1092 } 1093 1093 … … 1095 1095 1096 1096 #ifndef UNBIT 1097 fprintf (stderr, "your server socket listen backlog is limited to %d connections\n", uwsgi.listen_queue);1097 fprintf(stderr, "your server socket listen backlog is limited to %d connections\n", uwsgi.listen_queue); 1098 1098 #endif 1099 1099 1100 1100 1101 1101 if (uwsgi.single_interpreter == 1) { 1102 init_uwsgi_vars ();1103 } 1104 1105 memset (uwsgi.wsgi_apps, 0, sizeof(uwsgi.wsgi_apps));1102 init_uwsgi_vars(); 1103 } 1104 1105 memset(uwsgi.wsgi_apps, 0, sizeof(uwsgi.wsgi_apps)); 1106 1106 1107 1107 … … 1109 1109 uwsgi.poll.events = POLLIN; 1110 1110 1111 memset (&wsgi_req, 0, sizeof(struct wsgi_request));1111 memset(&wsgi_req, 0, sizeof(struct wsgi_request)); 1112 1112 1113 1113 /* shared area for workers */ 1114 uwsgi.workers = (struct uwsgi_worker *) mmap (NULL, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);1114 uwsgi.workers = (struct uwsgi_worker *) mmap(NULL, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); 1115 1115 if (!uwsgi.workers) { 1116 perror ("mmap()");1117 exit (1);1118 } 1119 memset (uwsgi.workers, 0, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1);1120 1121 uwsgi.mypid = getpid ();1116 perror("mmap()"); 1117 exit(1); 1118 } 1119 memset(uwsgi.workers, 0, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1); 1120 1121 uwsgi.mypid = getpid(); 1122 1122 masterpid = uwsgi.mypid; 1123 1123 1124 1124 #ifndef UNBIT 1125 1125 if (uwsgi.pidfile) { 1126 fprintf (stderr, "writing pidfile to %s\n", uwsgi.pidfile);1127 pidfile = fopen (uwsgi.pidfile, "w");1126 fprintf(stderr, "writing pidfile to %s\n", uwsgi.pidfile); 1127 pidfile = fopen(uwsgi.pidfile, "w"); 1128 1128 if (!pidfile) { 1129 perror ("fopen");1130 exit (1);1131 } 1132 if (fprintf (pidfile, "%d\n", masterpid) < 0) {1133 fprintf (stderr, "could not write pidfile.\n");1134 } 1135 fclose (pidfile);1129 perror("fopen"); 1130 exit(1); 1131 } 1132 if (fprintf(pidfile, "%d\n", masterpid) < 0) { 1133 fprintf(stderr, "could not write pidfile.\n"); 1134 } 1135 fclose(pidfile); 1136 1136 } 1137 1137 #endif … … 1141 1141 uwsgi.workers[0].pid = masterpid; 1142 1142 1143 fprintf(stderr, "initializing hooks...");1144 1145 uwsgi.shared->hooks[0] = uwsgi_request_wsgi ;1146 uwsgi.shared->after_hooks[0] = uwsgi_after_request_wsgi ;1147 1148 uwsgi.shared->hooks[UWSGI_MODIFIER_ADMIN_REQUEST] = uwsgi_request_admin ;//101143 fprintf(stderr, "initializing hooks..."); 1144 1145 uwsgi.shared->hooks[0] = uwsgi_request_wsgi; 1146 uwsgi.shared->after_hooks[0] = uwsgi_after_request_wsgi; 1147 1148 uwsgi.shared->hooks[UWSGI_MODIFIER_ADMIN_REQUEST] = uwsgi_request_admin; //10 1149 1149 #ifdef UWSGI_SPOOLER 1150 uwsgi.shared->hooks[UWSGI_MODIFIER_SPOOL_REQUEST] = uwsgi_request_spooler ;//171151 #endif 1152 uwsgi.shared->hooks[UWSGI_MODIFIER_FASTFUNC] = uwsgi_request_fastfunc ;//261153 1154 uwsgi.shared->hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_request_wsgi ;// 301155 uwsgi.shared->after_hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_after_request_wsgi; // 301156 1157 uwsgi.shared->hooks[UWSGI_MODIFIER_MESSAGE_MARSHAL] = uwsgi_request_marshal ;//331158 uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping ;//1001159 1160 fprintf(stderr, "done.\n");1150 uwsgi.shared->hooks[UWSGI_MODIFIER_SPOOL_REQUEST] = uwsgi_request_spooler; //17 1151 #endif 1152 uwsgi.shared->hooks[UWSGI_MODIFIER_FASTFUNC] = uwsgi_request_fastfunc; //26 1153 1154 uwsgi.shared->hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_request_wsgi; // 30 1155 uwsgi.shared->after_hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_after_request_wsgi; // 30 1156 1157 uwsgi.shared->hooks[UWSGI_MODIFIER_MESSAGE_MARSHAL] = uwsgi_request_marshal; //33 1158 uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100 1159 1160 fprintf(stderr, "done.\n"); 1161 1161 1162 1162 #ifdef UWSGI_ERLANG … … 1170 1170 1171 1171 if (uwsgi.wsgi_config != NULL) { 1172 uwsgi_wsgi_config ();1172 uwsgi_wsgi_config(); 1173 1173 } 1174 1174 else if (uwsgi.wsgi_file != NULL) { 1175 uwsgi_wsgi_file_config();1176 }1175 uwsgi_wsgi_file_config(); 1176 } 1177 1177 #ifdef UWSGI_XML 1178 1178 else if (uwsgi.xml_config != NULL) { 1179 uwsgi_xml_config (&wsgi_req, NULL);1179 uwsgi_xml_config(&wsgi_req, NULL); 1180 1180 } 1181 1181 #endif … … 1183 1183 #ifdef UWSGI_PASTE 1184 1184 else if (uwsgi.paste != NULL) { 1185 uwsgi_paste_config ();1185 uwsgi_paste_config(); 1186 1186 } 1187 1187 #endif 1188 1188 1189 1189 if (uwsgi.test_module != NULL) { 1190 if (PyImport_ImportModule (uwsgi.test_module)) {1191 exit (0);1192 } 1193 exit (1);1190 if (PyImport_ImportModule(uwsgi.test_module)) { 1191 exit(0); 1192 } 1193 exit(1); 1194 1194 } 1195 1195 … … 1198 1198 #ifndef UNBIT 1199 1199 if (no_server) { 1200 fprintf (stderr, "no-server mode requested. Goodbye.\n");1201 exit (0);1200 fprintf(stderr, "no-server mode requested. Goodbye.\n"); 1201 exit(0); 1202 1202 } 1203 1203 #endif … … 1210 1210 1211 1211 if (!uwsgi.single_interpreter) { 1212 fprintf(stderr, "*** uWSGI is running in multiple interpreter mode !!! ***\n");1212 fprintf(stderr, "*** uWSGI is running in multiple interpreter mode !!! ***\n"); 1213 1213 } 1214 1214 … … 1216 1216 if (uwsgi.master_process) { 1217 1217 if (uwsgi.is_a_reload) { 1218 fprintf (stderr, "gracefully (RE)spawned uWSGI master process (pid: %d)\n", uwsgi.mypid);1218 fprintf(stderr, "gracefully (RE)spawned uWSGI master process (pid: %d)\n", uwsgi.mypid); 1219 1219 } 1220 1220 else { 1221 fprintf (stderr, "spawned uWSGI master process (pid: %d)\n", uwsgi.mypid);1221 fprintf(stderr, "spawned uWSGI master process (pid: %d)\n", uwsgi.mypid); 1222 1222 } 1223 1223 } … … 1225 1225 #ifdef UNBIT 1226 1226 if (single_app_mode == 1) { 1227 wsgi_req.wsgi_script = getenv ("UWSGI_SCRIPT");1227 wsgi_req.wsgi_script = getenv("UWSGI_SCRIPT"); 1228 1228 if (wsgi_req.wsgi_script) { 1229 wsgi_req.wsgi_script_len = strlen (wsgi_req.wsgi_script);1229 wsgi_req.wsgi_script_len = strlen(wsgi_req.wsgi_script); 1230 1230 } 1231 1231 else { 1232 fprintf (stderr, "UWSGI_SCRIPT env var not set !\n");1233 exit (1);1234 } 1235 1236 init_uwsgi_app (NULL, NULL);1232 fprintf(stderr, "UWSGI_SCRIPT env var not set !\n"); 1233 exit(1); 1234 } 1235 1236 init_uwsgi_app(NULL, NULL); 1237 1237 } 1238 1238 #endif … … 1240 1240 #ifdef UWSGI_SPOOLER 1241 1241 if (spool_dir != NULL && uwsgi.numproc > 0) { 1242 uwsgi.shared->spooler_pid = spooler_start (uwsgi.serverfd, uwsgi_module);1242 uwsgi.shared->spooler_pid = spooler_start(uwsgi.serverfd, uwsgi_module); 1243 1243 } 1244 1244 #endif … … 1247 1247 if (!uwsgi.master_process) { 1248 1248 if (uwsgi.numproc == 1) { 1249 fprintf (stderr, "spawned uWSGI worker 1 (and the only) (pid: %d)\n", masterpid);1249 fprintf(stderr, "spawned uWSGI worker 1 (and the only) (pid: %d)\n", masterpid); 1250 1250 } 1251 1251 else { 1252 fprintf (stderr, "spawned uWSGI worker 1 (pid: %d)\n", masterpid);1252 fprintf(stderr, "spawned uWSGI worker 1 (pid: %d)\n", masterpid); 1253 1253 } 1254 1254 uwsgi.workers[1].pid = masterpid; 1255 1255 uwsgi.workers[1].id = 1; 1256 uwsgi.workers[1].last_spawn = time (NULL);1256 uwsgi.workers[1].last_spawn = time(NULL); 1257 1257 uwsgi.workers[1].manage_next_request = 1; 1258 1258 #ifdef UWSGI_THREADING … … 1260 1260 #endif 1261 1261 uwsgi.mywid = 1; 1262 gettimeofday (&last_respawn, NULL);1262 gettimeofday(&last_respawn, NULL); 1263 1263 respawn_delta = last_respawn.tv_sec; 1264 1264 } 1265 1265 1266 1266 1267 for (i = 2-uwsgi.master_process; i < uwsgi.numproc + 1; i++) {1268 /* let the worker know his worker_id (wid) */1269 pid = fork();1270 if (pid == 0) {1271 uwsgi.mypid = getpid();1272 uwsgi.workers[i].pid = uwsgi.mypid;1273 uwsgi.workers[i].id = i;1274 uwsgi.workers[i].last_spawn = time(NULL);1275 uwsgi.workers[i].manage_next_request = 1;1267 for (i = 2 - uwsgi.master_process; i < uwsgi.numproc + 1; i++) { 1268 /* let the worker know his worker_id (wid) */ 1269 pid = fork(); 1270 if (pid == 0) { 1271 uwsgi.mypid = getpid(); 1272 uwsgi.workers[i].pid = uwsgi.mypid; 1273 uwsgi.workers[i].id = i; 1274 uwsgi.workers[i].last_spawn = time(NULL); 1275 uwsgi.workers[i].manage_next_request = 1; 1276 1276 #ifdef UWSGI_THREADING 1277 uwsgi.workers[i].i_have_gil = 1;1278 #endif 1279 uwsgi.mywid = i;1280 if (uwsgi.serverfd != 0 && uwsgi.master_process == 1) {1281 /* close STDIN for workers */1282 close(0);1283 }1284 break;1285 }1286 else if (pid < 1) {1287 perror("fork()");1288 exit(1);1289 }1290 else {1291 fprintf(stderr, "spawned uWSGI worker %d (pid: %d)\n", i, pid);1292 gettimeofday(&last_respawn, NULL);1293 respawn_delta = last_respawn.tv_sec;1294 }1295 }1296 1297 1298 if (getpid () == masterpid && uwsgi.master_process == 1) {1277 uwsgi.workers[i].i_have_gil = 1; 1278 #endif 1279 uwsgi.mywid = i; 1280 if (uwsgi.serverfd != 0 && uwsgi.master_process == 1) { 1281 /* close STDIN for workers */ 1282 close(0); 1283 } 1284 break; 1285 } 1286 else if (pid < 1) { 1287 perror("fork()"); 1288 exit(1); 1289 } 1290 else { 1291 fprintf(stderr, "spawned uWSGI worker %d (pid: %d)\n", i, pid); 1292 gettimeofday(&last_respawn, NULL); 1293 respawn_delta = last_respawn.tv_sec; 1294 } 1295 } 1296 1297 1298 if (getpid() == masterpid && uwsgi.master_process == 1) { 1299 1299 /* route signals to workers... */ 1300 signal (SIGHUP, (void *) &grace_them_all);1301 signal (SIGTERM, (void *) &reap_them_all);1302 signal (SIGINT, (void *) &kill_them_all);1303 signal (SIGQUIT, (void *) &kill_them_all);1300 signal(SIGHUP, (void *) &grace_them_all); 1301 signal(SIGTERM, (void *) &reap_them_all); 1302 signal(SIGINT, (void *) &kill_them_all); 1303 signal(SIGQUIT, (void *) &kill_them_all); 1304 1304 /* used only to avoid human-errors */ 1305 1305 #ifndef UNBIT 1306 signal (SIGUSR1, (void *) &stats);1307 #endif 1308 1309 if (u dp_socket) {1310 uwsgi_poll.fd = bind_to_udp (udp_socket);1306 signal(SIGUSR1, (void *) &stats); 1307 #endif 1308 1309 if (uwsgi.udp_socket) { 1310 uwsgi_poll.fd = bind_to_udp(uwsgi.udp_socket); 1311 1311 if (uwsgi_poll.fd < 0) { 1312 fprintf (stderr, "unable to bind to udp socket. SNMP and cluster management services will be disabled.\n");1312 fprintf(stderr, "unable to bind to udp socket. SNMP and cluster management services will be disabled.\n"); 1313 1313 } 1314 1314 else { 1315 fprintf (stderr, "UDP server enabled.\n");1315 fprintf(stderr, "UDP server enabled.\n"); 1316 1316 uwsgi_poll.events = POLLIN; 1317 1317 } … … 1321 1321 #ifdef UWSGI_SPOOLER 1322 1322 if (spool_dir && uwsgi.shared->spooler_pid > 0) { 1323 kill (uwsgi.shared->spooler_pid, SIGKILL);1324 fprintf(stderr, "killed the spooler with pid %d\n", uwsgi.shared->spooler_pid);1323 kill(uwsgi.shared->spooler_pid, SIGKILL); 1324 fprintf(stderr, "killed the spooler with pid %d\n", uwsgi.shared->spooler_pid); 1325 1325 } 1326 1326 … … 1329 1329 #ifdef UWSGI_PROXY 1330 1330 if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 1331 kill (uwsgi.shared->proxy_pid, SIGKILL);1332 fprintf(stderr, "killed proxy with pid %d\n", uwsgi.shared->proxy_pid);1331 kill(uwsgi.shared->proxy_pid, SIGKILL); 1332 fprintf(stderr, "killed proxy with pid %d\n", uwsgi.shared->proxy_pid); 1333 1333 } 1334 1334 #endif 1335 fprintf (stderr, "goodbye to uWSGI.\n");1336 exit (0);1335 fprintf(stderr, "goodbye to uWSGI.\n"); 1336 exit(0); 1337 1337 } 1338 1338 if (ready_to_reload >= uwsgi.numproc && uwsgi.to_heaven) { 1339 1339 #ifdef UWSGI_SPOOLER 1340 1340 if (spool_dir && uwsgi.shared->spooler_pid > 0) { 1341 kill (uwsgi.shared->spooler_pid, SIGKILL);1342 fprintf(stderr, "wait4() the spooler with pid %d...", uwsgi.shared->spooler_pid);1343 diedpid = waitpid(uwsgi.shared->spooler_pid, &waitpid_status, 0);1344 fprintf(stderr,"done.");1341 kill(uwsgi.shared->spooler_pid, SIGKILL); 1342 fprintf(stderr, "wait4() the spooler with pid %d...", uwsgi.shared->spooler_pid); 1343 diedpid = waitpid(uwsgi.shared->spooler_pid, &waitpid_status, 0); 1344 fprintf(stderr, "done."); 1345 1345 } 1346 1346 #endif … … 1348 1348 #ifdef UWSGI_PROXY 1349 1349 if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 1350 kill (uwsgi.shared->proxy_pid, SIGKILL);1351 fprintf(stderr, "wait4() the proxy with pid %d...", uwsgi.shared->proxy_pid);1352 diedpid = waitpid(uwsgi.shared->proxy_pid, &waitpid_status, 0);1353 fprintf(stderr,"done.");1350 kill(uwsgi.shared->proxy_pid, SIGKILL); 1351 fprintf(stderr, "wait4() the proxy with pid %d...", uwsgi.shared->proxy_pid); 1352 diedpid = waitpid(uwsgi.shared->proxy_pid, &waitpid_status, 0); 1353 fprintf(stderr, "done."); 1354 1354 } 1355 1355 #endif 1356 fprintf (stderr, "binary reloading uWSGI...\n");1356 fprintf(stderr, "binary reloading uWSGI...\n"); 1357 1357 if (cwd) { 1358 if (chdir (cwd)) {1359 perror ("chdir()");1360 exit (1);1358 if (chdir(cwd)) { 1359 perror("chdir()"); 1360 exit(1); 1361 1361 } 1362 1362 } 1363 1363 /* check fd table (a module can obviosly open some fd on initialization...) */ 1364 fprintf (stderr, "closing all fds > 2 (_SC_OPEN_MAX = %ld)...\n", sysconf(_SC_OPEN_MAX));1365 for (i = 3; i < sysconf (_SC_OPEN_MAX); i++) {1364 fprintf(stderr, "closing all fds > 2 (_SC_OPEN_MAX = %ld)...\n", sysconf(_SC_OPEN_MAX)); 1365 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) { 1366 1366 if (i == uwsgi.serverfd) { 1367 1367 continue; 1368 1368 } 1369 close (i);1369 close(i); 1370 1370 } 1371 1371 if (uwsgi.serverfd != 3) { 1372 if (dup2 (uwsgi.serverfd, 3) < 0) {1373 perror ("dup2()");1374 exit (1);1372 if (dup2(uwsgi.serverfd, 3) < 0) { 1373 perror("dup2()"); 1374 exit(1); 1375 1375 } 1376 1376 } 1377 fprintf (stderr, "running %s\n", uwsgi.binary_path);1378 argv[0] = uwsgi.binary_path ;1377 fprintf(stderr, "running %s\n", uwsgi.binary_path); 1378 argv[0] = uwsgi.binary_path; 1379 1379 //strcpy (argv[0], uwsgi.binary_path); 1380 execve (uwsgi.binary_path, argv, environ);1381 perror ("execve()");1380 execve(uwsgi.binary_path, argv, environ); 1381 perror("execve()"); 1382 1382 // never here 1383 exit (1);1384 } 1385 diedpid = waitpid (WAIT_ANY, &waitpid_status, WNOHANG);1383 exit(1); 1384 } 1385 diedpid = waitpid(WAIT_ANY, &waitpid_status, WNOHANG); 1386 1386 if (diedpid == -1) { 1387 perror ("waitpid()");1387 perror("waitpid()"); 1388 1388 /* here is better to reload all the uWSGI stack */ 1389 fprintf (stderr, "something horrible happened...\n");1390 reap_them_all ();1391 exit (1);1389 fprintf(stderr, "something horrible happened...\n"); 1390 reap_them_all(); 1391 exit(1); 1392 1392 } 1393 1393 else if (diedpid == 0) { … … 1397 1397 #ifdef UWSGI_THREADING 1398 1398 if (uwsgi.has_threads && uwsgi.shared->options[UWSGI_OPTION_THREADS] == 1) { 1399 uwsgi._save = PyEval_SaveThread ();1399 uwsgi._save = PyEval_SaveThread(); 1400 1400 uwsgi.workers[uwsgi.mywid].i_have_gil = 0; 1401 1401 } … … 1405 1405 if (!check_interval.tv_sec) 1406 1406 check_interval.tv_sec = 1; 1407 1408 if (u dp_socket && uwsgi_poll.fd >= 0) {1409 rlen = poll (&uwsgi_poll, 1, check_interval.tv_sec * 1000);1407 1408 if (uwsgi.udp_socket && uwsgi_poll.fd >= 0) { 1409 rlen = poll(&uwsgi_poll, 1, check_interval.tv_sec * 1000); 1410 1410 if (rlen < 0) { 1411 perror ("poll()");1411 perror("poll()"); 1412 1412 } 1413 1413 else if (rlen > 0) { 1414 udp_len = sizeof (udp_client);1415 rlen = recvfrom (uwsgi_poll.fd, uwsgi.buffer, uwsgi.buffer_size, 0, (struct sockaddr *) &udp_client, &udp_len);1414 udp_len = sizeof(udp_client); 1415 rlen = recvfrom(uwsgi_poll.fd, uwsgi.buffer, uwsgi.buffer_size, 0, (struct sockaddr *) &udp_client, &udp_len); 1416 1416 if (rlen < 0) { 1417 perror ("recvfrom()");1417 perror("recvfrom()"); 1418 1418 } 1419 1419 else if (rlen > 0) { 1420 memset (udp_client_addr, 0, 16);1421 if (inet_ntop (AF_INET, &udp_client.sin_addr.s_addr, udp_client_addr, 16)) {1422 fprintf (stderr, "received udp packet of %d bytes from %s:%d\n", rlen, udp_client_addr, ntohs(udp_client.sin_port));1420 memset(udp_client_addr, 0, 16); 1421 if (inet_ntop(AF_INET, &udp_client.sin_addr.s_addr, udp_client_addr, 16)) { 1422 fprintf(stderr, "received udp packet of %d bytes from %s:%d\n", rlen, udp_client_addr, ntohs(udp_client.sin_port)); 1423 1423 #ifdef UWSGI_SNMP 1424 if (uwsgi.buffer[0] == 0x30 ) {1425 manage_snmp (uwsgi_poll.fd, (uint8_t *) uwsgi.buffer, rlen, &udp_client);1424 if (uwsgi.buffer[0] == 0x30 && uwsgi.snmp) { 1425 manage_snmp(uwsgi_poll.fd, (uint8_t *) uwsgi.buffer, rlen, &udp_client); 1426 1426 } 1427 1427 #endif 1428 1428 } 1429 1429 else { 1430 perror ("inet_ntop()");1430 perror("inet_ntop()"); 1431 1431 } 1432 1432 } … … 1434 1434 } 1435 1435 else { 1436 select (0, NULL, NULL, NULL, &check_interval);1436 select(0, NULL, NULL, NULL, &check_interval); 1437 1437 } 1438 1438 master_cycles++; … … 1441 1441 #ifdef UWSGI_THREADING 1442 1442 if (uwsgi.has_threads && !uwsgi.workers[uwsgi.mywid].i_have_gil) { 1443 PyEval_RestoreThread (uwsgi._save);1443 PyEval_RestoreThread(uwsgi._save); 1444 1444 uwsgi.workers[uwsgi.mywid].i_have_gil = 1; 1445 1445 } … … 1451 1451 /* first check for harakiri */ 1452 1452 if (uwsgi.workers[i].harakiri > 0) { 1453 if (uwsgi.workers[i].harakiri < time (NULL)) {1453 if (uwsgi.workers[i].harakiri < time(NULL)) { 1454 1454 /* first try to invoke the harakiri() custom handler */ 1455 1455 /* TODO */ 1456 1456 /* then brutally kill the worker */ 1457 kill (uwsgi.workers[i].pid, SIGKILL);1457 kill(uwsgi.workers[i].pid, SIGKILL); 1458 1458 } 1459 1459 } … … 1469 1469 1470 1470 // check for cluster nodes 1471 for (i=0;i<MAX_CLUSTER_NODES;i++) {1471 for (i = 0; i < MAX_CLUSTER_NODES; i++) { 1472 1472 struct uwsgi_cluster_node *ucn = &uwsgi.shared->nodes[i]; 1473 1473 1474 1474 if (ucn->name[0] != 0 && ucn->status == UWSGI_NODE_FAILED) { 1475 1475 // should i retry ? 1476 1476 if (master_cycles % ucn->errors == 0) { 1477 1477 if (!uwsgi_ping_node(i, &wsgi_req)) { 1478 ucn->status = UWSGI_NODE_OK ;1479 fprintf(stderr, "re-enabled cluster node %d/%s\n", i, ucn->name);1478 ucn->status = UWSGI_NODE_OK; 1479 fprintf(stderr, "re-enabled cluster node %d/%s\n", i, ucn->name); 1480 1480 } 1481 1481 else { … … 1493 1493 if (spool_dir && uwsgi.shared->spooler_pid > 0) { 1494 1494 if (diedpid == uwsgi.shared->spooler_pid) { 1495 fprintf(stderr, "OOOPS the spooler is no more...trying respawn...\n");1496 uwsgi.shared->spooler_pid = spooler_start (uwsgi.serverfd, uwsgi_module);1495 fprintf(stderr, "OOOPS the spooler is no more...trying respawn...\n"); 1496 uwsgi.shared->spooler_pid = spooler_start(uwsgi.serverfd, uwsgi_module); 1497 1497 continue; 1498 1498 } … … 1504 1504 if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 1505 1505 if (diedpid == uwsgi.shared->proxy_pid) { 1506 if (WIFEXITED (waitpid_status)) {1507 if (WEXITSTATUS (waitpid_status) != UWSGI_END_CODE) {1508 fprintf(stderr, "OOOPS the proxy is no more...trying respawn...\n");1506 if (WIFEXITED(waitpid_status)) { 1507 if (WEXITSTATUS(waitpid_status) != UWSGI_END_CODE) { 1508 fprintf(stderr, "OOOPS the proxy is no more...trying respawn...\n"); 1509 1509 uwsgi.shared->spooler_pid = proxy_start(1); 1510 1510 continue; … … 1515 1515 #endif 1516 1516 /* check for reloading */ 1517 if (WIFEXITED (waitpid_status)) {1518 if (WEXITSTATUS (waitpid_status) == UWSGI_RELOAD_CODE && uwsgi.to_heaven) {1517 if (WIFEXITED(waitpid_status)) { 1518 if (WEXITSTATUS(waitpid_status) == UWSGI_RELOAD_CODE && uwsgi.to_heaven) { 1519 1519 ready_to_reload++; 1520 1520 continue; 1521 1521 } 1522 else if (WEXITSTATUS (waitpid_status) == UWSGI_END_CODE && uwsgi.to_hell) {1522 else if (WEXITSTATUS(waitpid_status) == UWSGI_END_CODE && uwsgi.to_hell) { 1523 1523 ready_to_die++; 1524 1524 continue; … … 1526 1526 } 1527 1527 1528 fprintf (stderr, "DAMN ! process %d died :( trying respawn ...\n", diedpid);1529 gettimeofday (&last_respawn, NULL);1528 fprintf(stderr, "DAMN ! process %d died :( trying respawn ...\n", diedpid); 1529 gettimeofday(&last_respawn, NULL); 1530 1530 if (last_respawn.tv_sec == respawn_delta) { 1531 fprintf (stderr, "worker respawning too fast !!! i have to sleep a bit...\n");1531 fprintf(stderr, "worker respawning too fast !!! i have to sleep a bit...\n"); 1532 1532 /* TODO, user configurable fork throttler */ 1533 sleep (2);1534 } 1535 gettimeofday (&last_respawn, NULL);1533 sleep(2); 1534 } 1535 gettimeofday(&last_respawn, NULL); 1536 1536 respawn_delta = last_respawn.tv_sec; 1537 uwsgi.mywid = find_worker_id (diedpid);1538 pid = fork ();1537 uwsgi.mywid = find_worker_id(diedpid); 1538 pid = fork(); 1539 1539 if (pid == 0) { 1540 uwsgi.mypid = getpid ();1540 uwsgi.mypid = getpid(); 1541 1541 uwsgi.workers[uwsgi.mywid].pid = uwsgi.mypid; 1542 1542 uwsgi.workers[uwsgi.mywid].harakiri = 0; … … 1544 1544 uwsgi.workers[uwsgi.mywid].failed_requests = 0; 1545 1545 uwsgi.workers[uwsgi.mywid].respawn_count++; 1546 uwsgi.workers[uwsgi.mywid].last_spawn = time (NULL);1546 uwsgi.workers[uwsgi.mywid].last_spawn = time(NULL); 1547 1547 uwsgi.workers[uwsgi.mywid].manage_next_request = 1; 1548 1548 uwsgi.workers[uwsgi.mywid].i_have_gil = 1; … … 1550 1550 } 1551 1551 else if (pid < 1) { 1552 perror ("fork()");1552 perror("fork()"); 1553 1553 } 1554 1554 else { 1555 fprintf (stderr, "Respawned uWSGI worker (new pid: %d)\n", pid);1555 fprintf(stderr, "Respawned uWSGI worker (new pid: %d)\n", pid); 1556 1556 #ifdef UWSGI_SPOOLER 1557 1557 if (uwsgi.mywid <= 0 && diedpid != uwsgi.shared->spooler_pid) { … … 1563 1563 if (diedpid != uwsgi.shared->proxy_pid) { 1564 1564 #endif 1565 fprintf (stderr, "warning the died pid was not in the workers list. Probably you hit a BUG of uWSGI\n");1565 fprintf(stderr, "warning the died pid was not in the workers list. Probably you hit a BUG of uWSGI\n"); 1566 1566 #ifdef UWSGI_PROXY 1567 1567 } … … 1575 1575 1576 1576 1577 uwsgi.hvec = malloc (sizeof(struct iovec) * uwsgi.vec_size);1577 uwsgi.hvec = malloc(sizeof(struct iovec) * uwsgi.vec_size); 1578 1578 if (uwsgi.hvec == NULL) { 1579 fprintf (stderr, "unable to allocate memory for iovec.\n");1580 exit (1);1579 fprintf(stderr, "unable to allocate memory for iovec.\n"); 1580 exit(1); 1581 1581 } 1582 1582 1583 1583 if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && !uwsgi.master_process) { 1584 signal (SIGALRM, (void *) &harakiri);1584 signal(SIGALRM, (void *) &harakiri); 1585 1585 } 1586 1586 1587 1587 /* gracefully reload */ 1588 signal (SIGHUP, (void *) &gracefully_kill);1588 signal(SIGHUP, (void *) &gracefully_kill); 1589 1589 /* close the process (useful for master INT) */ 1590 signal (SIGINT, (void *) &end_me);1590 signal(SIGINT, (void *) &end_me); 1591 1591 /* brutally reload */ 1592 signal (SIGTERM, (void *) &reload_me);1592 signal(SIGTERM, (void *) &reload_me); 1593 1593 1594 1594 1595 1595 #ifndef UNBIT 1596 signal (SIGUSR1, (void *) &stats);1597 #endif 1598 1599 1600 signal (SIGPIPE, (void *) &warn_pipe);1596 signal(SIGUSR1, (void *) &stats); 1597 #endif 1598 1599 1600 signal(SIGPIPE, (void *) &warn_pipe); 1601 1601 1602 1602 … … 1604 1604 if (uwsgi.erlang_nodes > 0) { 1605 1605 if (uwsgi.numproc <= uwsgi.erlang_nodes) { 1606 fprintf(stderr, "You do not have enough worker for Erlang. Please respawn with at least %d processes.\n", uwsgi.erlang_nodes+1);1607 } 1608 else if ( uwsgi.mywid > (uwsgi.numproc - uwsgi.erlang_nodes)) {1609 fprintf(stderr, "Erlang mode enabled for worker %d.\n", uwsgi.mywid);1606 fprintf(stderr, "You do not have enough worker for Erlang. Please respawn with at least %d processes.\n", uwsgi.erlang_nodes + 1); 1607 } 1608 else if (uwsgi.mywid > (uwsgi.numproc - uwsgi.erlang_nodes)) { 1609 fprintf(stderr, "Erlang mode enabled for worker %d.\n", uwsgi.mywid); 1610 1610 erlang_loop(); 1611 1611 // NEVER HERE … … 1620 1620 // release the GIL 1621 1621 if (uwsgi.has_threads) { 1622 uwsgi._save = PyEval_SaveThread ();1622 uwsgi._save = PyEval_SaveThread(); 1623 1623 uwsgi.workers[uwsgi.mywid].i_have_gil = 0; 1624 1624 } … … 1634 1634 #endif 1635 1635 // clear all status bits 1636 UWSGI_CLEAR_STATUS ;1637 1638 uwsgi.poll.fd = accept (uwsgi.serverfd, (struct sockaddr *) &c_addr, (socklen_t *) & c_len);1636 UWSGI_CLEAR_STATUS; 1637 1638 uwsgi.poll.fd = accept(uwsgi.serverfd, (struct sockaddr *) &c_addr, (socklen_t *) & c_len); 1639 1639 1640 1640 if (uwsgi.poll.fd < 0) { 1641 perror ("accept()");1641 perror("accept()"); 1642 1642 continue; 1643 1643 } … … 1646 1646 1647 1647 if (uwsgi.shared->options[UWSGI_OPTION_LOGGING]) 1648 gettimeofday (&wsgi_req.start_of_request, NULL);1649 1650 1648 gettimeofday(&wsgi_req.start_of_request, NULL); 1649 1650 1651 1651 1652 1652 #ifdef UWSGI_SCTP … … 1654 1654 // get stream id, and map it to uwsgi modifiers 1655 1655 struct sctp_status sstatus; 1656 memset (&sstatus, 0, sizeof(sstatus));1657 socklen_t slen = sizeof (sstatus);1656 memset(&sstatus, 0, sizeof(sstatus)); 1657 socklen_t slen = sizeof(sstatus); 1658 1658 sstatus.sstat_assoc_id = 1; 1659 if (getsockopt (uwsgi.poll.fd, IPPROTO_SCTP, SCTP_STATUS, &sstatus, &slen)) {1660 perror ("getsockopt()");1661 } 1662 1663 memset (&sctp_ss, 0, sizeof(sctp_ss));1664 1665 fprintf (stderr, "%d %d\n", sstatus.sstat_instrms, sstatus.sstat_outstrms);1659 if (getsockopt(uwsgi.poll.fd, IPPROTO_SCTP, SCTP_STATUS, &sstatus, &slen)) { 1660 perror("getsockopt()"); 1661 } 1662 1663 memset(&sctp_ss, 0, sizeof(sctp_ss)); 1664 1665 fprintf(stderr, "%d %d\n", sstatus.sstat_instrms, sstatus.sstat_outstrms); 1666 1666 1667 1667 i = 0; 1668 wsgi_req.size = sctp_recvmsg (uwsgi.poll.fd, uwsgi.buffer, uwsgi.buffer_size, 0, 0, &sctp_ss, 0);1668 wsgi_req.size = sctp_recvmsg(uwsgi.poll.fd, uwsgi.buffer, uwsgi.buffer_size, 0, 0, &sctp_ss, 0); 1669 1669 if (wsgi_req.size < 0) { 1670 perror ("sctp_recvmsg()");1671 } 1672 fprintf (stderr, "received uwsgi message of %d bytes on stream id %d flags %d\n", wsgi_req.size, ntohs(sctp_ss.sinfo_stream), i);1670 perror("sctp_recvmsg()"); 1671 } 1672 fprintf(stderr, "received uwsgi message of %d bytes on stream id %d flags %d\n", wsgi_req.size, ntohs(sctp_ss.sinfo_stream), i); 1673 1673 1674 1674 } 1675 1675 else { 1676 1676 #endif 1677 if (!uwsgi_parse_response (&uwsgi.poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) &wsgi_req, uwsgi.buffer)) {1677 if (!uwsgi_parse_response(&uwsgi.poll, uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT], (struct uwsgi_header *) &wsgi_req, uwsgi.buffer)) { 1678 1678 continue; 1679 1679 } … … 1687 1687 } 1688 1688 1689 ret = (*uwsgi.shared->hooks[wsgi_req.modifier]) (&uwsgi, &wsgi_req);1689 ret = (*uwsgi.shared->hooks[wsgi_req.modifier]) (&uwsgi, &wsgi_req); 1690 1690 // calculate execution time 1691 gettimeofday(&wsgi_req.end_of_request, NULL) ;1692 uwsgi.workers[uwsgi.mywid].running_time += (double) (( (double)(wsgi_req.end_of_request.tv_sec*1000000+wsgi_req.end_of_request.tv_usec)-(double)(wsgi_req.start_of_request.tv_sec*1000000+wsgi_req.start_of_request.tv_usec))/ (double)1000.0);1691 gettimeofday(&wsgi_req.end_of_request, NULL); 1692 uwsgi.workers[uwsgi.mywid].running_time += (double) (((double) (wsgi_req.end_of_request.tv_sec * 1000000 + wsgi_req.end_of_request.tv_usec) - (double) (wsgi_req.start_of_request.tv_sec * 1000000 + wsgi_req.start_of_request.tv_usec)) / (double) 1000.0); 1693 1693 1694 1694 1695 1695 // get memory usage 1696 1696 if (uwsgi.shared->options[UWSGI_OPTION_MEMORY_DEBUG] == 1) 1697 get_memusage();1697 get_memusage(); 1698 1698 1699 1699 // close the connection with the webserver … … 1703 1703 1704 1704 if (!ret) 1705 (*uwsgi.shared->after_hooks[wsgi_req.modifier]) (&uwsgi, &wsgi_req);1705 (*uwsgi.shared->after_hooks[wsgi_req.modifier]) (&uwsgi, &wsgi_req); 1706 1706 1707 1707 … … 1714 1714 // defunct process reaper 1715 1715 if (uwsgi.shared->options[UWSGI_OPTION_REAPER] == 1) { 1716 waitpid (-1, &waitpid_status, WNOHANG);1716 waitpid(-1, &waitpid_status, WNOHANG); 1717 1717 } 1718 1718 // reset request 1719 memset (&wsgi_req, 0, sizeof(struct wsgi_request));1719 memset(&wsgi_req, 0, sizeof(struct wsgi_request)); 1720 1720 #ifdef UNBIT 1721 1721 if (tmp_filename && tmp_dir_fd >= 0) { … … 1725 1725 1726 1726 if (uwsgi.shared->options[UWSGI_OPTION_MAX_REQUESTS] > 0 && uwsgi.workers[uwsgi.mywid].requests >= uwsgi.shared->options[UWSGI_OPTION_MAX_REQUESTS]) { 1727 goodbye_cruel_world ();1727 goodbye_cruel_world(); 1728 1728 } 1729 1729 1730 1730 #ifdef UNBIT 1731 1731 if (check_for_memory_errors) { 1732 if (syscall (357, &us, 0) > 0) {1732 if (syscall(357, &us, 0) > 0) { 1733 1733 if (us.memory_errors > 0) { 1734 fprintf (stderr, "Unbit Kernel found a memory allocation error for process %d.\n", uwsgi.mypid);1735 goodbye_cruel_world ();1734 fprintf(stderr, "Unbit Kernel found a memory allocation error for process %d.\n", uwsgi.mypid); 1735 goodbye_cruel_world(); 1736 1736 } 1737 1737 } … … 1742 1742 1743 1743 if (uwsgi.workers[uwsgi.mywid].manage_next_request == 0) { 1744 reload_me ();1744 reload_me(); 1745 1745 } 1746 1746 else { 1747 goodbye_cruel_world ();1747 goodbye_cruel_world(); 1748 1748 } 1749 1749 … … 1752 1752 } 1753 1753 1754 void init_uwsgi_vars () {1754 void init_uwsgi_vars() { 1755 1755 1756 1756 #ifndef UNBIT … … 1760 1760 1761 1761 /* add cwd to pythonpath */ 1762 pysys = PyImport_ImportModule ("sys");1762 pysys = PyImport_ImportModule("sys"); 1763 1763 if (!pysys) { 1764 PyErr_Print ();1765 exit (1);1766 } 1767 pysys_dict = PyModule_GetDict (pysys);1768 pypath = PyDict_GetItemString (pysys_dict, "path");1764 PyErr_Print(); 1765 exit(1); 1766 } 1767 pysys_dict = PyModule_GetDict(pysys); 1768 pypath = PyDict_GetItemString(pysys_dict, "path"); 1769 1769 if (!pypath) { 1770 PyErr_Print ();1771 exit (1);1772 } 1773 if (PyList_Insert (pypath, 0, PyString_FromString(".")) != 0) {1774 PyErr_Print ();1770 PyErr_Print(); 1771 exit(1); 1772 } 1773 if (PyList_Insert(pypath, 0, PyString_FromString(".")) != 0) { 1774 PyErr_Print(); 1775 1775 } 1776 1776 1777 1777 #ifndef UNBIT 1778 1778 for (i = 0; i < uwsgi.python_path_cnt; i++) { 1779 if (PyList_Insert (pypath, 0, PyString_FromString(uwsgi.python_path[i])) != 0) {1780 PyErr_Print ();1779 if (PyList_Insert(pypath, 0, PyString_FromString(uwsgi.python_path[i])) != 0) { 1780 PyErr_Print(); 1781 1781 } 1782 1782 else { 1783 fprintf (stderr, "added %s to pythonpath.\n", uwsgi.python_path[i]);1783 fprintf(stderr, "added %s to pythonpath.\n", uwsgi.python_path[i]); 1784 1784 } 1785 1785 } … … 1788 1788 } 1789 1789 1790 int init_uwsgi_app (PyObject * force_wsgi_dict, PyObject * my_callable) {1790 int init_uwsgi_app(PyObject * force_wsgi_dict, PyObject * my_callable) { 1791 1791 PyObject *wsgi_module, *wsgi_dict = NULL; 1792 1792 PyObject *pymain, *zero; … … 1797 1797 struct uwsgi_app *wi; 1798 1798 1799 memset (tmpstring, 0, 256);1799 memset(tmpstring, 0, 256); 1800 1800 1801 1801 1802 1802 if (wsgi_req.wsgi_script_len == 0 && ((wsgi_req.wsgi_module_len == 0 || wsgi_req.wsgi_callable_len == 0) && uwsgi.wsgi_config == NULL && my_callable == NULL)) { 1803 fprintf (stderr, "invalid application (%.*s). skip.\n", wsgi_req.script_name_len, wsgi_req.script_name);1803 fprintf(stderr, "invalid application (%.*s). skip.\n", wsgi_req.script_name_len, wsgi_req.script_name); 1804 1804 return -1; 1805 1805 } 1806 1806 1807 1807 if (uwsgi.wsgi_config && wsgi_req.wsgi_callable_len == 0 && my_callable == NULL) { 1808 fprintf (stderr, "invalid application (%.*s). skip.\n", wsgi_req.script_name_len, wsgi_req.script_name);1808 fprintf(stderr, "invalid application (%.*s). skip.\n", wsgi_req.script_name_len, wsgi_req.script_name); 1809 1809 return -1; 1810 1810 } 1811 1811 1812 1812 if (wsgi_req.wsgi_script_len > 255 || wsgi_req.wsgi_module_len > 255 || wsgi_req.wsgi_callable_len > 255) { 1813 fprintf (stderr, "invalid application's string size. skip.\n");1813 fprintf(stderr, "invalid application's string size. skip.\n"); 1814 1814 return -1; 1815 1815 } … … 1830 1830 1831 1831 1832 zero = PyString_FromStringAndSize (wsgi_req.script_name, wsgi_req.script_name_len);1832 zero = PyString_FromStringAndSize(wsgi_req.script_name, wsgi_req.script_name_len); 1833 1833 if (!zero) { 1834 Py_FatalError ("cannot get mountpoint python object !\n");1835 } 1836 1837 if (PyDict_GetItem (uwsgi.py_apps, zero) != NULL) {1838 Py_DECREF (zero);1839 fprintf (stderr, "mountpoint %.*s already configured. skip.\n", wsgi_req.script_name_len, wsgi_req.script_name);1834 Py_FatalError("cannot get mountpoint python object !\n"); 1835 } 1836 1837 if (PyDict_GetItem(uwsgi.py_apps, zero) != NULL) { 1838 Py_DECREF(zero); 1839 fprintf(stderr, "mountpoint %.*s already configured. skip.\n", wsgi_req.script_name_len, wsgi_req.script_name); 1840 1840 return -1; 1841 1841 } 1842 1842 1843 Py_DECREF (zero);1843 Py_DECREF(zero); 1844 1844 1845 1845 wi = &uwsgi.wsgi_apps[id]; 1846 1846 1847 memset (wi, 0, sizeof(struct uwsgi_app));1847 memset(wi, 0, sizeof(struct uwsgi_app)); 1848 1848 1849 1849 if (uwsgi.single_interpreter == 0) { 1850 wi->interpreter = Py_NewInterpreter ();1850 wi->interpreter = Py_NewInterpreter(); 1851 1851 if (!wi->interpreter) { 1852 fprintf (stderr, "unable to initialize the new interpreter\n");1853 exit (1);1854 } 1855 PyThreadState_Swap (wi->interpreter);1852 fprintf(stderr, "unable to initialize the new interpreter\n"); 1853 exit(1); 1854 } 1855 PyThreadState_Swap(wi->interpreter); 1856 1856 #ifndef PYTHREE 1857 init_uwsgi_embedded_module ();1858 #endif 1859 init_uwsgi_vars ();1860 fprintf (stderr, "interpreter for app %d initialized.\n", id);1857 init_uwsgi_embedded_module(); 1858 #endif 1859 init_uwsgi_vars(); 1860 fprintf(stderr, "interpreter for app %d initialized.\n", id); 1861 1861 } 1862 1862 1863 1863 if (uwsgi.paste) { 1864 1864 wi->wsgi_callable = my_callable; 1865 Py_INCREF (my_callable);1865 Py_INCREF(my_callable); 1866 1866 } 1867 1867 else if (uwsgi.wsgi_file) { 1868 wi->wsgi_callable = my_callable;1869 Py_INCREF(my_callable);1870 }1868 wi->wsgi_callable = my_callable; 1869 Py_INCREF(my_callable); 1870 } 1871 1871 else { 1872 1872 1873 1873 if (uwsgi.wsgi_config == NULL) { 1874 1874 if (wsgi_req.wsgi_script_len > 0) { 1875 memcpy (tmpstring, wsgi_req.wsgi_script, wsgi_req.wsgi_script_len);1876 wsgi_module = PyImport_ImportModule (tmpstring);1875 memcpy(tmpstring, wsgi_req.wsgi_script, wsgi_req.wsgi_script_len); 1876 wsgi_module = PyImport_ImportModule(tmpstring); 1877 1877 if (!wsgi_module) { 1878 PyErr_Print ();1878 PyErr_Print(); 1879 1879 if (uwsgi.single_interpreter == 0) { 1880 Py_EndInterpreter (wi->interpreter);1881 PyThreadState_Swap (uwsgi.main_thread);1880 Py_EndInterpreter(wi->interpreter); 1881 PyThreadState_Swap(uwsgi.main_thread); 1882 1882 } 1883 1883 return -1; … … 1887 1887 } 1888 1888 else { 1889 memcpy (tmpstring, wsgi_req.wsgi_module, wsgi_req.wsgi_module_len);1890 wsgi_module = PyImport_ImportModule (tmpstring);1889 memcpy(tmpstring, wsgi_req.wsgi_module, wsgi_req.wsgi_module_len); 1890 wsgi_module = PyImport_ImportModule(tmpstring); 1891 1891 if (!wsgi_module) { 1892 PyErr_Print ();1892 PyErr_Print(); 1893 1893 if (uwsgi.single_interpreter == 0) { 1894 Py_EndInterpreter (wi->interpreter);1895 PyThreadState_Swap (uwsgi.main_thread);1894 Py_EndInterpreter(wi->interpreter); 1895 PyThreadState_Swap(uwsgi.main_thread); 1896 1896 } 1897 1897 return -1; … … 1899 1899 } 1900 1900 1901 wsgi_dict = PyModule_GetDict (wsgi_module);1901 wsgi_dict = PyModule_GetDict(wsgi_module); 1902 1902 if (!wsgi_dict) { 1903 PyErr_Print ();1903 PyErr_Print(); 1904 1904 if (uwsgi.single_interpreter == 0) { 1905 Py_EndInterpreter (wi->interpreter);1906 PyThreadState_Swap (uwsgi.main_thread);1905 Py_EndInterpreter(wi->interpreter); 1906 PyThreadState_Swap(uwsgi.main_thread); 1907 1907 } 1908 1908 return -1; … … 1915 1915 1916 1916 1917 memset (tmpstring, 0, 256);1918 memcpy (tmpstring, wsgi_req.wsgi_callable, wsgi_req.wsgi_callable_len);1917 memset(tmpstring, 0, 256); 1918  
