This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uwsgi.c
2822 lines (2394 loc) · 70.4 KB
/
uwsgi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*** uWSGI ***
Copyright (C) 2009-2010 Unbit S.a.s. <info@unbit.it>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
********* Note for Linux users *********
uWSGI supports UNIX socket on abstract namespace.
Use them if you have filesystem permission problems.
********* Note for Unbit users *********
Try to keep the same configuration on your unbit account (threading mode
in particular)
*/
#include "uwsgi.h"
struct uwsgi_server uwsgi;
extern char **environ;
static const char *app_slash = "/";
#ifdef UWSGI_SENDFILE
PyMethodDef uwsgi_sendfile_method[] = {{"uwsgi_sendfile", py_uwsgi_sendfile, METH_VARARGS, ""}};
#endif
int find_worker_id(pid_t pid) {
int i;
for (i = 1; i <= uwsgi.numproc; i++) {
/* uwsgi_log("%d of %d\n", pid, uwsgi.workers[i].pid); */
if (uwsgi.workers[i].pid == pid)
return i;
}
return -1;
}
PyMethodDef null_methods[] = {
{NULL, NULL},
};
#ifdef UNBIT
int save_to_disk = -1;
int tmp_dir_fd = -1;
char *tmp_filename;
int uri_to_hex(void);
int check_for_memory_errors = 0;
#endif
struct uwsgi_app *wi;
void warn_pipe() {
uwsgi_log("SIGPIPE: writing to a closed pipe/socket/fd !!!\n");
}
void gracefully_kill() {
uwsgi_log("Gracefully killing worker %d...\n", uwsgi.mypid);
if (UWSGI_IS_IN_REQUEST) {
uwsgi.workers[uwsgi.mywid].manage_next_request = 0;
}
else {
reload_me();
}
}
void reload_me() {
exit(UWSGI_RELOAD_CODE);
}
void end_me() {
exit(UWSGI_END_CODE);
}
void goodbye_cruel_world() {
uwsgi_log("...The work of process %d is done. Seeya!\n", getpid());
exit(0);
}
void kill_them_all() {
int i;
uwsgi.to_hell = 1;
uwsgi_log("SIGINT/SIGQUIT received...killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
kill(uwsgi.workers[i].pid, SIGINT);
}
}
void grace_them_all() {
int i;
uwsgi.to_heaven = 1;
uwsgi_log("...gracefully killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
kill(uwsgi.workers[i].pid, SIGHUP);
}
}
void reap_them_all() {
int i;
uwsgi_log("...brutally killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
kill(uwsgi.workers[i].pid, SIGTERM);
}
}
void harakiri() {
PyThreadState *_myself;
PyGILState_Ensure();
_myself = PyThreadState_Get();
if (wi) {
uwsgi_log("\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, uwsgi.wsgi_req->app_id, wi, wi->wsgi_harakiri, _myself, _myself->frame);
/*
// NEED TO FIND A SAFER WAY !!!
if (wi->wsgi_harakiri) {
PyEval_CallObject(wi->wsgi_harakiri, wi->wsgi_args);
if (PyErr_Occurred()) {
PyErr_Print();
}
}
*/
}
else {
uwsgi_log("\nF*CK !!! i must kill myself (pid: %d app_id: %d) thread_state: %p frame: %p...\n", uwsgi.mypid, uwsgi.wsgi_req->app_id, _myself, _myself->frame);
}
uwsgi_log("*** if you want your workers to be automatically respawned consider enabling the uWSGI master process ***\n");
Py_FatalError("HARAKIRI !\n");
}
#ifndef UNBIT
void stats() {
// fix this for better logging (this cause races)
struct uwsgi_app *ua = NULL;
int i;
uwsgi_log("*** pid %d stats ***\n", getpid());
uwsgi_log("\ttotal requests: %llu\n", uwsgi.workers[0].requests);
for (i = 0; i < uwsgi.wsgi_cnt; i++) {
ua = &uwsgi.wsgi_apps[i];
if (ua) {
uwsgi_log("\tapp %d requests: %d\n", i, ua->requests);
}
}
uwsgi_log("\n");
}
#endif
PyObject *wsgi_spitout;
PyMethodDef uwsgi_spit_method[] = { {"uwsgi_spit", py_uwsgi_spit, METH_VARARGS, ""} };
PyMethodDef uwsgi_write_method[] = { {"uwsgi_write", py_uwsgi_write, METH_VARARGS, ""} };
#ifdef UWSGI_ASYNC
PyMethodDef uwsgi_eventfd_read_method[] = { {"uwsgi_eventfd_read", py_eventfd_read, METH_VARARGS, ""}};
PyMethodDef uwsgi_eventfd_write_method[] = { {"uwsgi_eventfd_write", py_eventfd_write, METH_VARARGS, ""}};
#endif
// process manager is now (20090725) available on Unbit
pid_t masterpid;
pid_t diedpid;
int waitpid_status;
struct timeval last_respawn;
time_t respawn_delta;
#ifdef UNBIT
int single_app_mode = 0;
#endif
static int unconfigured_hook(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
uwsgi_log("-- unavailable modifier requested: %d --\n", wsgi_req->uh.modifier1);
return -1;
}
static void unconfigured_after_hook(struct uwsgi_server *uwsgi, struct wsgi_request *wsgi_req) {
return;
}
int main(int argc, char *argv[], char *envp[]) {
uint64_t master_cycles = 0;
struct timeval check_interval = {.tv_sec = 1,.tv_usec = 0 };
#ifdef PYTHREE
wchar_t *pyargv[MAX_PYARGV];
#else
char *pyargv[MAX_PYARGV];
#endif
int pyargc = 1;
int i;
int rlen;
int uwsgi_will_starts = 0;
#ifdef UWSGI_ASYNC
int current_async_timeout = 0;
#endif
struct pollfd uwsgi_poll;
struct sockaddr_in udp_client;
socklen_t udp_len;
char udp_client_addr[16];
pid_t pid;
int no_server = 0;
#ifndef UNBIT
FILE *pidfile;
#endif
int working_workers = 0;
int blocking_workers = 0;
char *cwd = NULL;
int ready_to_reload = 0;
int ready_to_die = 0;
char *env_reloads;
unsigned int reloads = 0;
char env_reload_buf[11];
#ifdef UNBIT
struct uidsec_struct us;
#endif
int socket_type = 0;
socklen_t socket_type_len;
/* anti signal bombing */
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, SIG_IGN);
memset(&uwsgi, 0, sizeof(struct uwsgi_server));
/* generic shared area */
uwsgi.shared = (struct uwsgi_shared *) mmap(NULL, sizeof(struct uwsgi_shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if (!uwsgi.shared) {
uwsgi_error("mmap()");
exit(1);
}
memset(uwsgi.shared, 0, sizeof(struct uwsgi_shared));
for (i = 0; i <= 0xFF; i++) {
uwsgi.shared->hooks[i] = unconfigured_hook;
uwsgi.shared->after_hooks[i] = unconfigured_after_hook;
}
uwsgi.wsgi_cnt = 1;
uwsgi.default_app = -1;
uwsgi.buffer_size = 4096;
uwsgi.numproc = 1;
uwsgi.async = 1;
#ifndef UNBIT
uwsgi.listen_queue = 64;
#endif
uwsgi.max_vars = MAX_VARS;
uwsgi.vec_size = 4 + 1 + (4 * MAX_VARS);
uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] = 4;
uwsgi.shared->options[UWSGI_OPTION_LOGGING] = 1;
#ifndef UNBIT
int option_index = 0;
struct option long_options[] = {
{"socket", required_argument, 0, 's'},
{"processes", required_argument, 0, 'p'},
{"harakiri", required_argument, 0, 't'},
#ifdef UWSGI_XML
{"xmlconfig", required_argument, 0, 'x'},
#endif
{"daemonize", required_argument, 0, 'd'},
{"listen", required_argument, 0, 'l'},
{"optimize", required_argument, 0, 'O'},
{"max-vars", required_argument, 0, 'v'},
{"buffer-size", required_argument, 0, 'b'},
{"memory-report", no_argument, 0, 'm'},
{"cgi-mode", no_argument, 0, 'c'},
{"abstract-socket", no_argument, 0, 'a'},
{"chmod-socket", no_argument, 0, 'C'},
#ifdef UWSGI_THREADING
{"enable-threads", no_argument, 0, 'T'},
#endif
{"profiler", no_argument, 0, 'P'},
{"single-interpreter", no_argument, 0, 'i'},
{"master", no_argument, 0, 'M'},
{"help", no_argument, 0, 'h'},
{"reaper", no_argument, 0, 'r'},
{"max-requests", required_argument, 0, 'R'},
{"socket-timeout", required_argument, 0, 'z'},
{"module", required_argument, 0, 'w'},
{"test", required_argument, 0, 'j'},
{"home", required_argument, 0, 'H'},
{"sharedarea", required_argument, 0, 'A'},
#ifdef UWSGI_SPOOLER
{"spooler", required_argument, 0, 'Q'},
#endif
{"disable-logging", no_argument, 0, 'L'},
{"pidfile", required_argument, 0, LONG_ARGS_PIDFILE},
{"chroot", required_argument, 0, LONG_ARGS_CHROOT},
{"gid", required_argument, 0, LONG_ARGS_GID},
{"uid", required_argument, 0, LONG_ARGS_UID},
{"pythonpath", required_argument, 0, LONG_ARGS_PYTHONPATH},
{"python-path", required_argument, 0, LONG_ARGS_PYTHONPATH},
{"pyargv", required_argument, 0, LONG_ARGS_PYARGV},
{"paste", required_argument, 0, LONG_ARGS_PASTE},
{"no-server", no_argument, &no_server, 1},
{"no-defer-accept", no_argument, &uwsgi.no_defer_accept, 1},
{"limit-as", required_argument, 0, LONG_ARGS_LIMIT_AS},
{"limit-post", required_argument, 0, LONG_ARGS_LIMIT_POST},
{"prio", required_argument, 0, LONG_ARGS_PRIO},
{"post-buffering", required_argument, 0, LONG_ARGS_POST_BUFFERING},
{"post-buffering-bufsize", required_argument, 0, LONG_ARGS_POST_BUFFERING_SIZE},
#ifdef UWSGI_UDP
{"udp", required_argument, 0, LONG_ARGS_UDP},
#endif
#ifdef UWSGI_MULTICAST
{"multicast", required_argument, 0, LONG_ARGS_MULTICAST},
#endif
#ifdef UWSGI_SNMP
{"snmp", no_argument, 0, LONG_ARGS_SNMP},
{"snmp-community", required_argument, 0, LONG_ARGS_SNMP_COMMUNITY},
#endif
{"check-interval", required_argument, 0, LONG_ARGS_CHECK_INTERVAL},
#ifdef UWSGI_ERLANG
{"erlang", required_argument, 0, LONG_ARGS_ERLANG},
{"erlang-cookie", required_argument, 0, LONG_ARGS_ERLANG_COOKIE},
#endif
#ifdef UWSGI_NAGIOS
{"nagios", no_argument, &uwsgi.nagios, 1},
#endif
{"binary-path", required_argument, 0, LONG_ARGS_BINARY_PATH},
#ifdef UWSGI_PROXY
{"proxy", required_argument, 0, LONG_ARGS_PROXY},
{"proxy-node", required_argument, 0, LONG_ARGS_PROXY_NODE},
{"proxy-max-connections", required_argument, 0, LONG_ARGS_PROXY_MAX_CONNECTIONS},
#endif
{"wsgi-file", required_argument, 0, LONG_ARGS_WSGI_FILE},
{"file", required_argument, 0, LONG_ARGS_FILE_CONFIG},
#ifdef UWSGI_ASYNC
{"async", required_argument, 0, LONG_ARGS_ASYNC},
#endif
#ifdef UWSGI_STACKLESS
{"stackless", no_argument, &uwsgi.stackless, 1},
#endif
#ifdef UWSGI_UGREEN
{"ugreen", no_argument, &uwsgi.ugreen, 1},
{"ugreen-stacksize", required_argument, 0, LONG_ARGS_UGREEN_PAGES},
#endif
{"logto", required_argument, 0, LONG_ARGS_LOGTO},
{"version", no_argument, 0, LONG_ARGS_VERSION},
{0, 0, 0, 0}
};
#endif
gettimeofday(&uwsgi.start_tv, NULL);
setlinebuf(stdout);
uwsgi.rl.rlim_cur = 0;
uwsgi.rl.rlim_max = 0;
env_reloads = getenv("UWSGI_RELOADS");
if (env_reloads) {
// convert env value to int
reloads = atoi(env_reloads);
reloads++;
// convert reloads to string
rlen = snprintf(env_reload_buf, 10, "%u", reloads);
if (rlen > 0) {
env_reload_buf[rlen] = 0;
if (setenv("UWSGI_RELOADS", env_reload_buf, 1)) {
uwsgi_error("setenv()");
}
}
}
else {
if (setenv("UWSGI_RELOADS", "0", 1)) {
uwsgi_error("setenv()");
}
}
socket_type_len = sizeof(int);
if (!getsockopt(3, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) {
if (socket_type == SOCK_STREAM && reloads > 0) {
uwsgi_log("...fd 3 is a socket, i suppose this is a graceful reload of uWSGI, i will try to do my best...\n");
uwsgi.is_a_reload = 1;
#ifdef UNBIT
/* discard the 3'th fd as we will use the fd 0 */
close(3);
#else
uwsgi.serverfd = 3;
#endif
}
}
uwsgi.binary_path = argv[0];
#ifndef UNBIT
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) {
#else
while ((i = getopt(argc, argv, "p:t:mTPiv:b:rMR:Sz:w:C:j:H:A:EQ:L")) != -1) {
#endif
manage_opt(i, optarg);
}
if (optind < argc) {
char *lazy = argv[optind];
if (!strcmp(lazy+strlen(lazy)-3, ".py")) {
uwsgi.file_config = lazy;
}
}
#ifdef UWSGI_XML
if (uwsgi.xml_config != NULL) {
uwsgi_xml_config(uwsgi.wsgi_req, long_options);
}
#endif
//parse environ
parse_sys_envs(environ, long_options);
if (uwsgi.binary_path == argv[0]) {
cwd = uwsgi_get_cwd();
uwsgi.binary_path = malloc(strlen(argv[0]) + 1);
if (uwsgi.binary_path == NULL) {
uwsgi_error("malloc()");
exit(1);
}
strlcpy(uwsgi.binary_path, argv[0], strlen(argv[0]) + 1);
}
#ifndef UNBIT
if (uwsgi.shared->options[UWSGI_OPTION_CGI_MODE] == 0) {
#endif
if (uwsgi.test_module == NULL) {
uwsgi_log("*** Starting uWSGI %s (%dbit) on [%.*s] ***\n", UWSGI_VERSION, (int) (sizeof(void *)) * 8, 24, ctime((const time_t *) &uwsgi.start_tv.tv_sec));
}
#ifndef UNBIT
}
else {
uwsgi_log("*** 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));
}
#endif
uwsgi_log("compiled with version: %s\n", __VERSION__);
#ifdef __BIG_ENDIAN__
uwsgi_log("*** big endian arch detected ***\n");
#endif
#ifdef PYTHREE
uwsgi_log("*** Warning Python3.x support is experimental, do not use it in production environment ***\n");
#endif
uwsgi_log("Python version: %s\n", Py_GetVersion());
#ifndef UNBIT
uwsgi_as_root();
#endif
if (!uwsgi.master_process) {
uwsgi_log(" *** WARNING: you are running uWSGI without its master process manager ***\n");
}
#ifndef __OpenBSD__
#ifndef UNBIT
if (uwsgi.rl.rlim_max > 0) {
uwsgi_log("limiting address space of processes...\n");
if (setrlimit(RLIMIT_AS, &uwsgi.rl)) {
uwsgi_error("setrlimit()");
}
}
#endif
if (uwsgi.prio != 0) {
if (setpriority(PRIO_PROCESS, 0, uwsgi.prio)) {
uwsgi_error("setpriority()");
}
else {
uwsgi_log("scheduler priority set to %d\n", uwsgi.prio);
}
}
if (!getrlimit(RLIMIT_AS, &uwsgi.rl)) {
#ifndef UNBIT
// check for overflow
if (uwsgi.rl.rlim_max != RLIM_INFINITY) {
#endif
uwsgi_log("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);
#ifndef UNBIT
}
#endif
}
#endif
uwsgi.page_size = getpagesize();
uwsgi_log("your memory page size is %d bytes\n", uwsgi.page_size);
if (uwsgi.buffer_size > 65536) {
uwsgi_log("invalid buffer size.\n");
exit(1);
}
sanitize_args(&uwsgi);
if (uwsgi.async > 1) {
if (!getrlimit(RLIMIT_NOFILE, &uwsgi.rl)) {
if (uwsgi.rl.rlim_cur < uwsgi.async) {
uwsgi_log("- your current max open files limit is %lu, this is lower than requested async cores !!! -\n", (unsigned long) uwsgi.rl.rlim_cur);
if (uwsgi.rl.rlim_cur < uwsgi.rl.rlim_max && uwsgi.rl.rlim_max > uwsgi.async) {
unsigned long tmp_nofile = (unsigned long) uwsgi.rl.rlim_cur ;
uwsgi.rl.rlim_cur = uwsgi.async;
if (!setrlimit(RLIMIT_NOFILE, &uwsgi.rl)) {
uwsgi_log("max open files limit reset to %lu\n", (unsigned long) uwsgi.rl.rlim_cur);
uwsgi.async = uwsgi.rl.rlim_cur;
}
else {
uwsgi.async = (int) tmp_nofile ;
}
}
else {
uwsgi.async = uwsgi.rl.rlim_cur;
}
uwsgi_log("- async cores set to %d -\n", uwsgi.async);
}
}
}
// allocate more wsgi_req for async mode
uwsgi.wsgi_requests = malloc(sizeof(struct wsgi_request) * uwsgi.async);
if (uwsgi.wsgi_requests == NULL) {
uwsgi_log("unable to allocate memory for requests.\n");
exit(1);
}
memset(uwsgi.wsgi_requests, 0, sizeof(struct wsgi_request) * uwsgi.async);
uwsgi.async_buf = malloc( sizeof(char *) * uwsgi.async);
if (!uwsgi.async_buf) {
uwsgi_error("malloc()");
exit(1);
}
if (uwsgi.post_buffering > 0) {
uwsgi.async_post_buf = malloc( sizeof(char *) * uwsgi.async);
if (!uwsgi.async_post_buf) {
uwsgi_error("malloc()");
exit(1);
}
if (!uwsgi.post_buffering_bufsize) {
uwsgi.post_buffering_bufsize = 8192 ;
}
}
for(i=0;i<uwsgi.async;i++) {
uwsgi.async_buf[i] = malloc(uwsgi.buffer_size);
if (!uwsgi.async_buf[i]) {
uwsgi_error("malloc()");
exit(1);
}
if (uwsgi.post_buffering > 0) {
uwsgi.async_post_buf[i] = malloc(uwsgi.post_buffering_bufsize);
if (!uwsgi.async_post_buf[i]) {
uwsgi_error("malloc()");
exit(1);
}
}
}
// by default set wsgi_req to the first slot
uwsgi.wsgi_req = uwsgi.wsgi_requests ;
uwsgi_log("allocated %llu bytes (%llu KB) for %d request's buffer.\n", (uint64_t) (sizeof(struct wsgi_request) * uwsgi.async),
(uint64_t)( (sizeof(struct wsgi_request) * uwsgi.async ) / 1024),
uwsgi.async);
if (uwsgi.pyhome != NULL) {
uwsgi_log("Setting PythonHome to %s...\n", uwsgi.pyhome);
#ifdef PYTHREE
wchar_t *wpyhome;
wpyhome = malloc((sizeof(wchar_t) * strlen(uwsgi.pyhome)) + 2);
if (!wpyhome) {
uwsgi_error("malloc()");
exit(1);
}
mbstowcs(wpyhome, uwsgi.pyhome, strlen(uwsgi.pyhome));
Py_SetPythonHome(wpyhome);
free(wpyhome);
#else
Py_SetPythonHome(uwsgi.pyhome);
#endif
}
#ifdef PYTHREE
wchar_t pname[6];
mbstowcs(pname, "uWSGI", 6);
Py_SetProgramName(pname);
#else
Py_SetProgramName("uWSGI");
#endif
Py_Initialize();
#ifdef PYTHREE
mbstowcs(pname, "uwsgi", 6);
pyargv[0] = pname;
#else
pyargv[0] = "uwsgi";
#endif
if (uwsgi.pyargv != NULL) {
#ifdef PYTHREE
wchar_t *wcargv = malloc( sizeof( wchar_t ) * strlen(uwsgi.pyargv));
if (!wcargv) {
uwsgi_error("malloc()");
exit(1);
}
wchar_t *wa;
#endif
char *ap;
#ifdef __sun__
// FIX THIS !!!
ap = strtok(uwsgi.pyargv, " ");
while ((ap = strtok(NULL, " ")) != NULL) {
#else
while ((ap = strsep(&uwsgi.pyargv, " \t")) != NULL) {
#endif
if (*ap != '\0') {
#ifdef PYTHREE
wa = (wchar_t *) ( (ap-uwsgi.pyargv) * sizeof(wchar_t) );
mbstowcs(wa, ap, strlen(ap));
pyargv[pyargc] = wa;
#else
pyargv[pyargc] = ap;
#endif
pyargc++;
}
if (pyargc + 1 > MAX_PYARGV)
break;
}
}
PySys_SetArgv(pyargc, pyargv);
uwsgi.py_apps = PyDict_New();
if (!uwsgi.py_apps) {
PyErr_Print();
exit(1);
}
wsgi_spitout = PyCFunction_New(uwsgi_spit_method, NULL);
uwsgi.wsgi_writeout = PyCFunction_New(uwsgi_write_method, NULL);
#ifdef UWSGI_EMBEDDED
if (uwsgi.sharedareasize > 0) {
#ifndef __OpenBSD__
uwsgi.sharedareamutex = mmap(NULL, sizeof(pthread_mutexattr_t) + sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if (!uwsgi.sharedareamutex) {
uwsgi_error("mmap()");
exit(1);
}
#else
uwsgi_log("***WARNING*** the sharedarea on OpenBSD is not SMP-safe. Beware of race conditions !!!\n");
#endif
uwsgi.sharedarea = mmap(NULL, uwsgi.page_size * uwsgi.sharedareasize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if (uwsgi.sharedarea) {
uwsgi_log("shared area mapped at %p, you can access it with uwsgi.sharedarea* functions.\n", uwsgi.sharedarea);
#ifdef __APPLE__
memset(uwsgi.sharedareamutex, 0, sizeof(OSSpinLock));
#else
#if !defined(__OpenBSD__) && !defined(__NetBSD__)
if (pthread_mutexattr_init((pthread_mutexattr_t *) uwsgi.sharedareamutex)) {
uwsgi_log("unable to allocate mutexattr structure\n");
exit(1);
}
if (pthread_mutexattr_setpshared((pthread_mutexattr_t *) uwsgi.sharedareamutex, PTHREAD_PROCESS_SHARED)) {
uwsgi_log("unable to share mutex\n");
exit(1);
}
if (pthread_mutex_init((pthread_mutex_t *) uwsgi.sharedareamutex + sizeof(pthread_mutexattr_t), (pthread_mutexattr_t *) uwsgi.sharedareamutex)) {
uwsgi_log("unable to initialize mutex\n");
exit(1);
}
#endif
#endif
}
else {
uwsgi_error("mmap()");
exit(1);
}
}
init_uwsgi_embedded_module();
#endif
Py_OptimizeFlag = uwsgi.py_optimize;
uwsgi.main_thread = PyThreadState_Get();
#ifdef UWSGI_NAGIOS
if (uwsgi.nagios) {
nagios(&uwsgi);
// never here
}
#endif
#ifdef UWSGI_UGREEN
if (uwsgi.ugreen) {
u_green_init(&uwsgi);
}
#endif
#ifdef UWSGI_THREADING
if (uwsgi.has_threads) {
PyEval_InitThreads();
uwsgi_log("threads support enabled\n");
}
#endif
if (!no_server) {
#ifndef UNBIT
if (uwsgi.socket_name != NULL && !uwsgi.is_a_reload) {
char *tcp_port = strchr(uwsgi.socket_name, ':');
if (tcp_port == NULL) {
uwsgi.serverfd = bind_to_unix(uwsgi.socket_name, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket);
}
else {
uwsgi.serverfd = bind_to_tcp(uwsgi.socket_name, uwsgi.listen_queue, tcp_port);
}
if (uwsgi.serverfd < 0) {
uwsgi_log("unable to create the server socket.\n");
exit(1);
}
}
#endif
socket_type_len = sizeof(int);
if (!getsockopt(uwsgi.serverfd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) {
uwsgi_will_starts = 1;
}
else {
uwsgi.numproc = 0 ;
}
#ifdef UWSGI_PROXY
if (uwsgi.proxy_socket_name) {
uwsgi.shared->proxy_pid = proxy_start(uwsgi.master_process);
uwsgi_will_starts = 1;
}
#endif
#ifdef UWSGI_UDP
if (uwsgi.udp_socket) {
uwsgi_will_starts = 1;
}
#endif
}
if (!uwsgi_will_starts && !no_server) {
uwsgi_log( "The -s/--socket option is missing and stdin is not a socket.\n");
exit(1);
}
#ifdef UWSGI_ASYNC
if (uwsgi.async > 1) {
#ifdef __linux__
uwsgi.async_events = malloc( sizeof(struct epoll_event) * uwsgi.async ) ;
#elif defined(__sun__)
uwsgi.async_events = malloc( sizeof(struct pollfd) * uwsgi.async ) ;
#else
uwsgi.async_events = malloc( sizeof(struct kevent) * uwsgi.async ) ;
#endif
if (!uwsgi.async_events) {
uwsgi_error("malloc()");
exit(1);
}
}
#endif
#ifndef UNBIT
uwsgi_log( "your server socket listen backlog is limited to %d connections\n", uwsgi.listen_queue);
#endif
if (uwsgi.single_interpreter == 1) {
init_uwsgi_vars();
}
memset(uwsgi.wsgi_apps, 0, sizeof(uwsgi.wsgi_apps));
/* shared area for workers */
uwsgi.workers = (struct uwsgi_worker *) mmap(NULL, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
if (!uwsgi.workers) {
uwsgi_error("mmap()");
exit(1);
}
memset(uwsgi.workers, 0, sizeof(struct uwsgi_worker) * uwsgi.numproc + 1);
uwsgi.mypid = getpid();
masterpid = uwsgi.mypid;
#ifndef UNBIT
if (uwsgi.pidfile) {
uwsgi_log( "writing pidfile to %s\n", uwsgi.pidfile);
pidfile = fopen(uwsgi.pidfile, "w");
if (!pidfile) {
uwsgi_error("fopen");
exit(1);
}
if (fprintf(pidfile, "%d\n", masterpid) < 0) {
uwsgi_log( "could not write pidfile.\n");
}
fclose(pidfile);
}
#endif
/* save the masterpid */
uwsgi.workers[0].pid = masterpid;
uwsgi_log( "initializing hooks...");
uwsgi.shared->hooks[0] = uwsgi_request_wsgi;
uwsgi.shared->after_hooks[0] = uwsgi_after_request_wsgi;
uwsgi.shared->hooks[UWSGI_MODIFIER_ADMIN_REQUEST] = uwsgi_request_admin; //10
#ifdef UWSGI_SPOOLER
uwsgi.shared->hooks[UWSGI_MODIFIER_SPOOL_REQUEST] = uwsgi_request_spooler; //17
#endif
uwsgi.shared->hooks[UWSGI_MODIFIER_EVAL] = uwsgi_request_eval; //22
uwsgi.shared->hooks[UWSGI_MODIFIER_FASTFUNC] = uwsgi_request_fastfunc; //26
uwsgi.shared->hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_request_wsgi; // 30
uwsgi.shared->after_hooks[UWSGI_MODIFIER_MANAGE_PATH_INFO] = uwsgi_after_request_wsgi; // 30
uwsgi.shared->hooks[UWSGI_MODIFIER_MESSAGE_MARSHAL] = uwsgi_request_marshal; //33
uwsgi.shared->hooks[UWSGI_MODIFIER_PING] = uwsgi_request_ping; //100
uwsgi_log( "done.\n");
#ifdef UWSGI_ERLANG
if (uwsgi.erlang_node) {
uwsgi.erlang_nodes = 1;
uwsgi.erlangfd = init_erlang(uwsgi.erlang_node, uwsgi.erlang_cookie);
}
#endif
#ifdef UWSGI_SNMP
if (uwsgi.snmp) {
snmp_init();
}
#endif
if (uwsgi.wsgi_config != NULL) {
uwsgi_wsgi_config(NULL);
}
else if (uwsgi.file_config != NULL) {
uwsgi_wsgi_config(uwsgi.file_config);
}
else if (uwsgi.wsgi_file != NULL) {
uwsgi_wsgi_file_config();
}
#ifdef UWSGI_XML
else if (uwsgi.xml_config != NULL) {
uwsgi_xml_config(uwsgi.wsgi_req, NULL);
}
#endif
#ifdef UWSGI_PASTE
else if (uwsgi.paste != NULL) {
uwsgi_paste_config();
}
#endif
if (uwsgi.test_module != NULL) {
if (PyImport_ImportModule(uwsgi.test_module)) {
exit(0);
}
exit(1);
}
#ifndef UNBIT
if (no_server) {
uwsgi_log( "no-server mode requested. Goodbye.\n");
exit(0);
}
#endif
// is this a proxy only worker ?
if (!uwsgi.master_process && uwsgi.numproc == 0) {
exit(0);
}
if (!uwsgi.single_interpreter) {
uwsgi_log( "*** uWSGI is running in multiple interpreter mode !!! ***\n");
}
/* preforking() */
if (uwsgi.master_process) {
if (uwsgi.is_a_reload) {
uwsgi_log( "gracefully (RE)spawned uWSGI master process (pid: %d)\n", uwsgi.mypid);
}
else {
uwsgi_log( "spawned uWSGI master process (pid: %d)\n", uwsgi.mypid);
}
}
#ifdef UNBIT
if (single_app_mode == 1) {
uwsgi.wsgi_req->wsgi_script = getenv("UWSGI_SCRIPT");
if (uwsgi.wsgi_req->wsgi_script) {
uwsgi.wsgi_req->wsgi_script_len = strlen(uwsgi.wsgi_req->wsgi_script);
}
else {
uwsgi_log( "UWSGI_SCRIPT env var not set !\n");
exit(1);
}
init_uwsgi_app(NULL, NULL);
}
#endif
#ifdef UWSGI_SPOOLER
if (uwsgi.spool_dir != NULL && uwsgi.numproc > 0) {
uwsgi.shared->spooler_pid = spooler_start(uwsgi.serverfd, uwsgi.embedded_dict);
}
#endif
#ifdef UWSGI_STACKLESS
if (uwsgi.stackless) {
stackless_init(&uwsgi);
}