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
3920 lines (3386 loc) · 101 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-2011 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.
*/
#include "uwsgi.h"
struct uwsgi_server uwsgi;
extern char **environ;
static char *short_options = NULL;
static char *base_short_options = "s:p:t:x:d:l:v:b:mcaCTiMhrR:z:A:Q:Ly:";
UWSGI_DECLARE_EMBEDDED_PLUGINS;
static struct option long_base_options[] = {
{"socket", required_argument, 0, 's'},
{"protocol", required_argument, 0, LONG_ARGS_PROTOCOL},
{"socket-protocol", required_argument, 0, LONG_ARGS_SOCKET_PROTOCOL},
{"shared-socket", required_argument, 0, LONG_ARGS_SHARED_SOCKET},
{"processes", required_argument, 0, 'p'},
{"workers", required_argument, 0, 'p'},
{"harakiri", required_argument, 0, 't'},
{"harakiri-verbose", no_argument, &uwsgi.harakiri_verbose, 1},
#ifdef UWSGI_XML
{"xmlconfig", required_argument, 0, 'x'},
{"xml", required_argument, 0, 'x'},
#endif
{"inherit", required_argument, 0, LONG_ARGS_INHERIT},
{"daemonize", required_argument, 0, 'd'},
{"listen", required_argument, 0, 'l'},
{"max-vars", required_argument, 0, 'v'},
{"buffer-size", required_argument, 0, 'b'},
{"memory-report", no_argument, 0, 'm'},
{"profiler", required_argument, 0, LONG_ARGS_PROFILER},
{"cgi-mode", no_argument, 0, 'c'},
{"abstract-socket", no_argument, 0, 'a'},
{"chmod-socket", optional_argument, 0, 'C'},
{"map-socket", required_argument, 0, LONG_ARGS_MAP_SOCKET},
{"chmod", optional_argument, 0, 'C'},
#ifdef UWSGI_THREADING
{"enable-threads", no_argument, 0, 'T'},
#endif
{"single-interpreter", no_argument, 0, 'i'},
{"master", no_argument, 0, 'M'},
{"emperor", required_argument, 0, LONG_ARGS_EMPEROR},
{"early-emperor", no_argument, &uwsgi.early_emperor, 1},
{"emperor-amqp-vhost", required_argument, 0, LONG_ARGS_EMPEROR_AMQP_VHOST},
{"emperor-amqp-username", required_argument, 0, LONG_ARGS_EMPEROR_AMQP_USERNAME},
{"emperor-amqp-password", required_argument, 0, LONG_ARGS_EMPEROR_AMQP_PASSWORD},
{"vassals-inherit", required_argument, 0, LONG_ARGS_VASSALS_INHERIT},
{"vassals-start-hook", required_argument, 0, LONG_ARGS_VASSALS_START_HOOK},
{"vassals-stop-hook", required_argument, 0, LONG_ARGS_VASSALS_STOP_HOOK},
{"auto-snapshot", optional_argument, 0, LONG_ARGS_AUTO_SNAPSHOT},
{"reload-mercy", required_argument, 0, LONG_ARGS_RELOAD_MERCY},
{"exit-on-reload", no_argument, &uwsgi.exit_on_reload, 1},
{"die-on-term", no_argument, &uwsgi.die_on_term, 1},
{"help", no_argument, 0, 'h'},
{"usage", no_argument, 0, 'h'},
{"reaper", no_argument, 0, 'r'},
{"max-requests", required_argument, 0, 'R'},
{"socket-timeout", required_argument, 0, 'z'},
{"no-fd-passing", no_argument, &uwsgi.no_fd_passing, 1},
{"sharedarea", required_argument, 0, 'A'},
{"cache", required_argument, 0, LONG_ARGS_CACHE},
{"cache-blocksize", required_argument, 0, LONG_ARGS_CACHE_BLOCKSIZE},
{"cache-store", required_argument, 0, LONG_ARGS_CACHE_STORE},
{"cache-store-sync", required_argument, 0, LONG_ARGS_CACHE_STORE_SYNC},
{"queue", required_argument, 0, LONG_ARGS_QUEUE},
{"queue-blocksize", required_argument, 0, LONG_ARGS_QUEUE_BLOCKSIZE},
{"queue-store", required_argument, 0, LONG_ARGS_QUEUE_STORE},
{"queue-store-sync", required_argument, 0, LONG_ARGS_QUEUE_STORE_SYNC},
#ifdef UWSGI_SPOOLER
{"spooler", required_argument, 0, 'Q'},
#endif
{"disable-logging", no_argument, 0, 'L'},
{"pidfile", required_argument, 0, LONG_ARGS_PIDFILE},
{"pidfile2", required_argument, 0, LONG_ARGS_PIDFILE2},
{"chroot", required_argument, 0, LONG_ARGS_CHROOT},
{"gid", required_argument, 0, LONG_ARGS_GID},
{"uid", required_argument, 0, LONG_ARGS_UID},
#ifdef UWSGI_INI
{"ini", required_argument, 0, LONG_ARGS_INI},
#endif
#ifdef UWSGI_YAML
{"yaml", required_argument, 0, 'y'},
{"yml", required_argument, 0, 'y'},
#endif
#ifdef UWSGI_JSON
{"json", required_argument, 0, 'j'},
#endif
#ifdef UWSGI_SQLITE3
{"sqlite3", required_argument, 0, LONG_ARGS_SQLITE3},
{"sqlite", required_argument, 0, LONG_ARGS_SQLITE3},
#endif
#ifdef UWSGI_ZEROMQ
{"zeromq", required_argument, 0, LONG_ARGS_ZEROMQ},
{"zmq", required_argument, 0, LONG_ARGS_ZEROMQ},
#endif
#ifdef UWSGI_LDAP
{"ldap", required_argument, 0, LONG_ARGS_LDAP},
{"ldap-schema", no_argument, 0, LONG_ARGS_LDAP_SCHEMA},
{"ldap-schema-ldif", no_argument, 0, LONG_ARGS_LDAP_SCHEMA_LDIF},
#endif
{"no-server", no_argument, &uwsgi.no_server, 1},
{"no-defer-accept", no_argument, &uwsgi.no_defer_accept, 1},
{"limit-as", required_argument, 0, LONG_ARGS_LIMIT_AS},
{"reload-on-as", required_argument, 0, LONG_ARGS_RELOAD_ON_AS},
{"reload-on-rss", required_argument, 0, LONG_ARGS_RELOAD_ON_RSS},
{"touch-reload", required_argument, 0, LONG_ARGS_TOUCH_RELOAD},
{"propagate-touch", no_argument, &uwsgi.propagate_touch, 1},
{"limit-post", required_argument, 0, LONG_ARGS_LIMIT_POST},
{"no-orphans", no_argument, &uwsgi.no_orphans, 1},
{"prio", required_argument, 0, LONG_ARGS_PRIO},
{"cpu-affinity", required_argument, 0, LONG_ARGS_CPU_AFFINITY},
{"post-buffering", required_argument, 0, LONG_ARGS_POST_BUFFERING},
{"post-buffering-bufsize", required_argument, 0, LONG_ARGS_POST_BUFFERING_SIZE},
{"upload-progress", required_argument, 0, LONG_ARGS_UPLOAD_PROGRESS},
{"no-default-app", no_argument, &uwsgi.no_default_app, 1},
{"manage-script-name", no_argument, &uwsgi.manage_script_name, 1},
#ifdef UWSGI_UDP
{"udp", required_argument, 0, LONG_ARGS_UDP},
#endif
#ifdef UWSGI_MULTICAST
{"multicast", required_argument, 0, LONG_ARGS_MULTICAST},
{"cluster", required_argument, 0, LONG_ARGS_CLUSTER},
{"cluster-nodes", required_argument, 0, LONG_ARGS_CLUSTER_NODES},
{"cluster-reload", required_argument, 0, LONG_ARGS_CLUSTER_RELOAD},
{"cluster-log", required_argument, 0, LONG_ARGS_CLUSTER_LOG},
#endif
{"subscribe-to", required_argument, 0, LONG_ARGS_SUBSCRIBE_TO},
#ifdef UWSGI_SNMP
{"snmp", optional_argument, 0, LONG_ARGS_SNMP},
{"snmp-community", required_argument, 0, LONG_ARGS_SNMP_COMMUNITY},
#endif
{"check-interval", required_argument, 0, LONG_ARGS_CHECK_INTERVAL},
{"binary-path", required_argument, 0, LONG_ARGS_BINARY_PATH},
#ifdef UWSGI_ASYNC
{"async", required_argument, 0, LONG_ARGS_ASYNC},
#endif
{"logto", required_argument, 0, LONG_ARGS_LOGTO},
{"logto2", required_argument, 0, LONG_ARGS_LOGTO2},
{"logfile-chown", no_argument, &uwsgi.logfile_chown, 1},
{"log-syslog", optional_argument, 0, LONG_ARGS_LOG_SYSLOG},
{"log-socket", required_argument, 0, LONG_ARGS_LOG_SOCKET},
#ifdef UWSGI_ZEROMQ
{"log-zeromq", required_argument, 0, LONG_ARGS_LOG_ZEROMQ},
#endif
{"log-master", no_argument, 0, LONG_ARGS_LOG_MASTER},
{"logdate", optional_argument, 0, LONG_ARGS_LOG_DATE},
{"log-date", optional_argument, 0, LONG_ARGS_LOG_DATE},
{"log-prefix", optional_argument, 0, LONG_ARGS_LOG_DATE},
{"log-zero", no_argument, 0, LONG_ARGS_LOG_ZERO},
{"log-slow", required_argument, 0, LONG_ARGS_LOG_SLOW},
{"log-4xx", no_argument, 0, LONG_ARGS_LOG_4xx},
{"log-5xx", no_argument, 0, LONG_ARGS_LOG_5xx},
{"log-big", required_argument, 0, LONG_ARGS_LOG_BIG},
{"log-sendfile", required_argument, 0, LONG_ARGS_LOG_SENDFILE},
{"log-micros", no_argument, &uwsgi.log_micros, 1},
{"master-as-root", no_argument, &uwsgi.master_as_root, 1},
{"chdir", required_argument, 0, LONG_ARGS_CHDIR},
{"chdir2", required_argument, 0, LONG_ARGS_CHDIR2},
{"lazy", no_argument, &uwsgi.lazy, 1},
{"cheap", no_argument, &uwsgi.cheap, 1},
{"idle", required_argument, 0, LONG_ARGS_IDLE},
{"mount", required_argument, 0, LONG_ARGS_MOUNT},
{"grunt", no_argument, &uwsgi.grunt, 1},
{"threads", required_argument, 0, LONG_ARGS_THREADS},
{"vhost", no_argument, &uwsgi.vhost, 1},
{"vhost-host", no_argument, 0, LONG_ARGS_VHOSTHOST},
#ifdef UWSGI_ROUTING
{"routing", no_argument, &uwsgi.routing, 1},
#endif
{"add-header", required_argument, 0, LONG_ARGS_ADD_HEADER},
{"check-static", required_argument, 0, LONG_ARGS_CHECK_STATIC},
{"static-map", required_argument, 0, LONG_ARGS_STATIC_MAP},
{"file-serve-mode", required_argument, 0, LONG_ARGS_FILE_SERVE_MODE},
{"check-cache", no_argument, &uwsgi.check_cache, 1},
{"close-on-exec", no_argument, &uwsgi.close_on_exec, 1},
{"mode", required_argument, 0, LONG_ARGS_MODE},
{"env", required_argument, 0, LONG_ARGS_ENV},
{"vacuum", no_argument, &uwsgi.vacuum, 1},
#ifdef __linux__
{"cgroup", required_argument, 0, LONG_ARGS_CGROUP},
{"cgroup-opt", required_argument, 0, LONG_ARGS_CGROUP_OPT},
{"namespace", required_argument, 0, LONG_ARGS_LINUX_NS},
{"ns", required_argument, 0, LONG_ARGS_LINUX_NS},
{"namespace-net", required_argument, 0, LONG_ARGS_LINUX_NS_NET},
{"ns-net", required_argument, 0, LONG_ARGS_LINUX_NS_NET},
#endif
{"reuse-port", no_argument, &uwsgi.reuse_port, 1},
{"zerg", required_argument, 0, LONG_ARGS_ZERG},
{"zerg-server", required_argument, 0, LONG_ARGS_ZERG_SERVER},
{"cron", required_argument, 0, LONG_ARGS_CRON},
{"loop", required_argument, 0, LONG_ARGS_LOOP},
{"worker-exec", required_argument, 0, LONG_ARGS_WORKER_EXEC},
{"attach-daemon", required_argument, 0, LONG_ARGS_ATTACH_DAEMON},
{"plugins", required_argument, 0, LONG_ARGS_PLUGINS},
{"autoload", no_argument, &uwsgi.autoload, 1},
{"allowed-modifiers", required_argument, 0, LONG_ARGS_ALLOWED_MODIFIERS},
{"remap-modifier", required_argument, 0, LONG_ARGS_REMAP_MODIFIER},
{"dump-options", no_argument, &uwsgi.dump_options, 1},
{"show-config", no_argument, &uwsgi.show_config, 1},
{"print", required_argument, 0, LONG_ARGS_PRINT},
{"version", no_argument, 0, LONG_ARGS_VERSION},
{0, 0, 0, 0}
};
void uwsgi_configure(void) {
struct option *lopt;
struct option *aopt;
char *val;
int i;
int is_retry;
int found;
// option must be processed in reverse (to have a consistent template system)
// but first load plugins...
for (i = 0; i < uwsgi.exported_opts_cnt; i++) {
if (uwsgi.exported_opts[i]->configured)
continue;
if (!strcmp("plugin", uwsgi.exported_opts[i]->key) || !strcmp("plugins", uwsgi.exported_opts[i]->key)) {
manage_opt(LONG_ARGS_PLUGINS, uwsgi.exported_opts[i]->value);
}
}
for (i = uwsgi.exported_opts_cnt-1; i >= 0; i--) {
#ifdef UWSGI_DEBUG
uwsgi_log("i = %d %p\n", i, uwsgi.exported_opts[i]);
#endif
if (uwsgi.exported_opts[i]->configured)
continue;
is_retry = 0;
retry:
found = 0;
lopt = uwsgi.long_options;;
while ((aopt = lopt)) {
if (!aopt->name)
break;
if (!strcmp(aopt->name, uwsgi.exported_opts[i]->key)) {
found = 1;
val = uwsgi.exported_opts[i]->value;
if (aopt->flag)
*aopt->flag = aopt->val;
else if (val) {
if (aopt->has_arg == optional_argument) {
if (!strcasecmp("true", val)) {
val = NULL;
}
}
if (aopt->has_arg == no_argument) {
if (!strcasecmp("false", val) || val[0] == '0') {
lopt++;
continue;
}
}
manage_opt(aopt->val, val);
}
}
lopt++;
}
if (!found && uwsgi.autoload && !is_retry) {
DIR *pdir;
struct dirent *dp;
pdir = opendir(UWSGI_PLUGIN_DIR);
if (!pdir) {
uwsgi_fatal_error("opendir()");
}
while ((dp = readdir(pdir)) != NULL) {
if (!strncmp("_plugin.so", dp->d_name + (strlen(dp->d_name) - 10), 19)) {
if (uwsgi_load_plugin(-1, dp->d_name, uwsgi.exported_opts[i]->key, 2)) {
uwsgi_log("option \"%s\" found in plugin %s\n", uwsgi.exported_opts[i]->key, dp->d_name);
found = 1;
break;
}
}
}
if (found) {
build_options();
closedir(pdir);
// avoid deadly loops...
is_retry = 1;
goto retry;
}
closedir(pdir);
}
}
}
void config_magic_table_fill(char *filename, char **magic_table) {
char *tmp = NULL;
magic_table['o'] = filename;
if (filename[0] == '/') {
magic_table['p'] = filename;
}
else {
magic_table['p'] = uwsgi_concat3(uwsgi.cwd, "/", filename);
}
magic_table['s'] = uwsgi_get_last_char(magic_table['p'], '/') + 1;
magic_table['d'] = uwsgi_concat2n(magic_table['p'], magic_table['s'] - magic_table['p'], "", 0);
if (magic_table['d'][strlen(magic_table['d'])-1] == '/') {
tmp = magic_table['d'] + (strlen(magic_table['d']) -1) ;
uwsgi_log("tmp = %c\n", *tmp);
*tmp = 0;
}
if (uwsgi_get_last_char(magic_table['d'], '/'))
magic_table['c'] = uwsgi_get_last_char(magic_table['d'], '/') + 1;
if (tmp) *tmp = '/';
if (uwsgi_get_last_char(filename, '.'))
magic_table['e'] = uwsgi_get_last_char(filename, '.') + 1;
if (uwsgi_get_last_char(magic_table['s'], '.'))
magic_table['n'] = uwsgi_concat2n(magic_table['s'], uwsgi_get_last_char(magic_table['s'], '.') - magic_table['s'], "", 0);
}
int find_worker_id(pid_t pid) {
int i;
for (i = 1; i <= uwsgi.numproc; i++) {
if (uwsgi.workers[i].pid == pid)
return i;
}
return -1;
}
void warn_pipe() {
struct wsgi_request *wsgi_req = current_wsgi_req();
if (uwsgi.threads < 2 && wsgi_req->uri_len > 0) {
uwsgi_log("SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request %.*s (ip %.*s) !!!\n", wsgi_req->uri_len, wsgi_req->uri, wsgi_req->remote_addr_len, wsgi_req->remote_addr);
}
else {
uwsgi_log("SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) !!!\n");
}
}
#ifdef UWSGI_THREADING
// in threading mode we need to use the cancel pthread subsystem
void wait_for_threads() {
int i, ret;
pthread_mutex_lock(&uwsgi.six_feet_under_lock);
for (i = 0; i < uwsgi.threads; i++) {
if (!pthread_equal(uwsgi.core[i]->thread_id, pthread_self())) {
pthread_cancel(uwsgi.core[i]->thread_id);
}
}
// wait for thread termination
for (i = 0; i < uwsgi.threads; i++) {
if (!pthread_equal(uwsgi.core[i]->thread_id, pthread_self())) {
ret = pthread_join(uwsgi.core[i]->thread_id, NULL);
if (ret) {
uwsgi_log("pthread_join() = %d\n", ret);
}
}
}
pthread_mutex_unlock(&uwsgi.six_feet_under_lock);
}
#endif
void gracefully_kill(int signum) {
struct wsgi_request *wsgi_req = current_wsgi_req();
uwsgi_log("Gracefully killing worker %d (pid: %d)...\n", uwsgi.mywid, uwsgi.mypid);
uwsgi.workers[uwsgi.mywid].manage_next_request = 0;
#ifdef UWSGI_THREADING
if (uwsgi.threads > 1) {
wait_for_threads();
if (!uwsgi.core[wsgi_req->async_id]->in_request) {
exit(UWSGI_RELOAD_CODE);
}
return;
// never here
}
#endif
// still not found a way to gracefully reload in async mode
if (uwsgi.async > 1) {
exit(UWSGI_RELOAD_CODE);
}
if (!uwsgi.core[0]->in_request) {
exit(UWSGI_RELOAD_CODE);
}
}
void end_me(int signum) {
exit(UWSGI_END_CODE);
}
void goodbye_cruel_world() {
#ifdef UWSGI_THREADING
if (uwsgi.threads > 1 && !uwsgi.to_hell) {
wait_for_threads();
}
#endif
uwsgi.workers[uwsgi.mywid].manage_next_request = 0;
uwsgi_log("...The work of process %d is done. Seeya!\n", getpid());
exit(0);
}
void kill_them_all(int signum) {
int i;
uwsgi.to_hell = 1;
if (uwsgi.reload_mercy > 0) {
uwsgi.master_mercy = time(NULL) + uwsgi.reload_mercy;
}
else {
uwsgi.master_mercy = time(NULL) + 5;
}
uwsgi_log("SIGINT/SIGQUIT received...killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
if (uwsgi.workers[i].pid > 0)
kill(uwsgi.workers[i].pid, SIGINT);
}
#ifdef UWSGI_SPOOLER
if (uwsgi.spool_dir && uwsgi.shared->spooler_pid > 0) {
kill(uwsgi.shared->spooler_pid, SIGKILL);
uwsgi_log("killed the spooler with pid %d\n", uwsgi.shared->spooler_pid);
}
#endif
if (uwsgi.emperor_pid >= 0) {
kill(uwsgi.emperor_pid, SIGKILL);
waitpid(uwsgi.emperor_pid, &i, 0);
uwsgi_log("killed the emperor with pid %d\n", uwsgi.emperor_pid);
}
for (i = 0; i < uwsgi.shared->daemons_cnt; i++) {
if (uwsgi.shared->daemons[i].pid > 0)
kill(-uwsgi.shared->daemons[i].pid, SIGKILL);
}
for (i = 0; i < uwsgi.gateways_cnt; i++) {
if (uwsgi.gateways[i].pid > 0)
kill(uwsgi.gateways[i].pid, SIGKILL);
}
}
void grace_them_all(int signum) {
int i;
int waitpid_status;
if (!uwsgi.lazy)
uwsgi.to_heaven = 1;
if (uwsgi.reload_mercy > 0) {
uwsgi.master_mercy = time(NULL) + uwsgi.reload_mercy;
}
#ifdef UWSGI_SPOOLER
if (uwsgi.spool_dir && uwsgi.shared->spooler_pid > 0) {
kill(uwsgi.shared->spooler_pid, SIGKILL);
uwsgi_log("killed the spooler with pid %d\n", uwsgi.shared->spooler_pid);
}
#endif
if (uwsgi.emperor_pid >= 0) {
kill(uwsgi.emperor_pid, SIGKILL);
waitpid(uwsgi.emperor_pid, &i, 0);
uwsgi_log("killed the emperor with pid %d\n", uwsgi.emperor_pid);
}
for (i = 0; i < uwsgi.shared->daemons_cnt; i++) {
if (uwsgi.shared->daemons[i].pid > 0)
kill(-uwsgi.shared->daemons[i].pid, SIGKILL);
}
for (i = 0; i < uwsgi.gateways_cnt; i++) {
if (uwsgi.gateways[i].pid > 0)
kill(uwsgi.gateways[i].pid, SIGKILL);
}
uwsgi_log("...gracefully killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
if (uwsgi.auto_snapshot) {
if (uwsgi.workers[i].snapshot > 0) {
kill(uwsgi.workers[i].snapshot, SIGKILL);
if (waitpid(uwsgi.workers[i].snapshot, &waitpid_status, 0) < 0) {
uwsgi_error("waitpid()");
}
}
if (uwsgi.workers[i].pid > 0) {
if (uwsgi.auto_snapshot > 0 && i > uwsgi.auto_snapshot) {
uwsgi.workers[i].snapshot = 0;
kill(uwsgi.workers[i].pid, SIGHUP);
}
else {
uwsgi.workers[i].snapshot = uwsgi.workers[i].pid;
kill(uwsgi.workers[i].pid, SIGURG);
}
}
}
else if (uwsgi.workers[i].pid > 0)
kill(uwsgi.workers[i].pid, SIGHUP);
}
if (uwsgi.auto_snapshot) {
uwsgi.respawn_workers = 1;
}
}
void uwsgi_nuclear_blast() {
if (!uwsgi.workers) {
reap_them_all(0);
}
else if (uwsgi.master_process) {
if (getpid() == uwsgi.workers[0].pid) {
reap_them_all(0);
}
}
exit(1);
}
void reap_them_all(int signum) {
int i;
if (!uwsgi.lazy)
uwsgi.to_heaven = 1;
for (i = 0; i < uwsgi.shared->daemons_cnt; i++) {
if (uwsgi.shared->daemons[i].pid > 0)
kill(-uwsgi.shared->daemons[i].pid, SIGKILL);
}
for (i = 0; i < uwsgi.gateways_cnt; i++) {
if (uwsgi.gateways[i].pid > 0)
kill(uwsgi.gateways[i].pid, SIGKILL);
}
if (uwsgi.emperor_pid >= 0) {
kill(uwsgi.emperor_pid, SIGKILL);
waitpid(uwsgi.emperor_pid, &i, 0);
uwsgi_log("killed the emperor with pid %d\n", uwsgi.emperor_pid);
}
if (!uwsgi.workers)
return;
uwsgi_log("...brutally killing workers...\n");
for (i = 1; i <= uwsgi.numproc; i++) {
if (uwsgi.workers[i].pid > 0)
kill(uwsgi.workers[i].pid, SIGTERM);
}
}
void harakiri() {
uwsgi_log("\nF*CK !!! i must kill myself (pid: %d app_id: %d)...\n", uwsgi.mypid, uwsgi.wsgi_req->app_id);
if (!uwsgi.master_process) {
uwsgi_log("*** if you want your workers to be automatically respawned consider enabling the uWSGI master process ***\n");
}
exit(0);
}
void snapshot_me(int signum) {
// wakeup !!!
if (uwsgi.snapshot) {
uwsgi.snapshot = 0;
return;
}
uwsgi.workers[uwsgi.mywid].manage_next_request = 0;
#ifdef UWSGI_THREADING
if (uwsgi.threads > 1) {
wait_for_threads();
}
#endif
uwsgi.snapshot = 1;
uwsgi_log("[snapshot] process %d taken\n", (int) getpid());
}
void stats(int signum) {
//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.apps_cnt; i++) {
ua = &uwsgi.apps[i];
if (ua) {
uwsgi_log("\tapp %d requests: %d\n", i, ua->requests);
}
}
uwsgi_log("\n");
}
void what_i_am_doing() {
struct wsgi_request *wsgi_req;
int i;
if (uwsgi.cores > 1) {
for (i = 0; i < uwsgi.cores; i++) {
wsgi_req = uwsgi.wsgi_requests[i];
if (wsgi_req->uri_len > 0) {
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < time(NULL)) {
uwsgi_log("HARAKIRI: --- uWSGI worker %d core %d (pid: %d) WAS managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, i, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) &wsgi_req->start_of_request.tv_sec));
}
else {
uwsgi_log("SIGUSR2: --- uWSGI worker %d core %d (pid: %d) is managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, i, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) &wsgi_req->start_of_request.tv_sec));
}
}
}
}
else {
wsgi_req = uwsgi.wsgi_requests[0];
if (wsgi_req->uri_len > 0) {
if (uwsgi.shared->options[UWSGI_OPTION_HARAKIRI] > 0 && uwsgi.workers[uwsgi.mywid].harakiri < time(NULL)) {
uwsgi_log("HARAKIRI: --- uWSGI worker %d (pid: %d) WAS managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) &wsgi_req->start_of_request.tv_sec));
}
else {
uwsgi_log("SIGUSR2: --- uWSGI worker %d (pid: %d) is managing request %.*s since %.*s ---\n", (int) uwsgi.mywid, (int) uwsgi.mypid, wsgi_req->uri_len, wsgi_req->uri, 24, ctime((const time_t *) &wsgi_req->start_of_request.tv_sec));
}
}
}
}
pid_t masterpid;
struct timeval last_respawn;
int unconfigured_hook(struct wsgi_request *wsgi_req) {
uwsgi_log("-- unavailable modifier requested: %d --\n", wsgi_req->uh.modifier1);
return -1;
}
static void unconfigured_after_hook(struct wsgi_request *wsgi_req) {
return;
}
struct uwsgi_plugin unconfigured_plugin = {
.name = "unconfigured",
.request = unconfigured_hook,
.after_request = unconfigured_after_hook,
};
static void vacuum(void) {
struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
if (uwsgi.vacuum) {
if (getpid() == masterpid) {
if (chdir(uwsgi.cwd)) {
uwsgi_error("chdir()");
}
if (uwsgi.pidfile && !uwsgi.uid) {
if (unlink(uwsgi.pidfile)) {
uwsgi_error("unlink()");
}
else {
uwsgi_log("VACUUM: pidfile removed.\n");
}
}
if (uwsgi.pidfile2) {
if (unlink(uwsgi.pidfile2)) {
uwsgi_error("unlink()");
}
else {
uwsgi_log("VACUUM: pidfile2 removed.\n");
}
}
if (uwsgi.chdir) {
if (chdir(uwsgi.chdir)) {
uwsgi_error("chdir()");
}
}
while (uwsgi_sock) {
if (uwsgi_sock->family == AF_UNIX) {
if (unlink(uwsgi_sock->name)) {
uwsgi_error("unlink()");
}
else {
uwsgi_log("VACUUM: unix socket %s removed.\n", uwsgi_sock->name);
}
}
uwsgi_sock = uwsgi_sock->next;
}
}
}
}
int main(int argc, char *argv[], char *envp[]) {
int i, j;
int rlen;
FILE *pidfile;
char *env_reloads;
char env_reload_buf[11];
char *plugins_requested;
#ifdef UNBIT
//struct uidsec_struct us;
#endif
#ifdef UWSGI_DEBUG
struct utsname uuts;
#endif
char *emperor_env;
char *magic_table[0xff];
char *optname;
signal(SIGHUP, SIG_IGN);
signal(SIGTERM, SIG_IGN);
init_magic_table(magic_table);
//initialize masterpid with a default value
masterpid = getpid();
memset(&uwsgi, 0, sizeof(struct uwsgi_server));
uwsgi.cwd = uwsgi_get_cwd();
atexit(vacuum);
#ifdef UWSGI_DEBUG
#ifdef __sun__
if (uname(&uuts) < 0) {
#else
if (uname(&uuts)) {
#endif
uwsgi_error("uname()");
}
else {
uwsgi_log("SYSNAME: %s\nNODENAME: %s\nRELEASE: %s\nVERSION: %s\nMACHINE: %s\n", uuts.sysname, uuts.nodename, uuts.release, uuts.version, uuts.machine);
}
#endif
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));
#ifdef UWSGI_SPOOLER
//set the spooler frequency to 30 seconds by default
uwsgi.shared->spooler_frequency = 30;
#endif
for (i = 0; i < 0xFF; i++) {
uwsgi.p[i] = &unconfigured_plugin;
}
uwsgi.master_queue = -1;
uwsgi.signal_socket = -1;
uwsgi.emperor_fd_config = -1;
uwsgi.emperor_pid = -1;
uwsgi.cluster_fd = -1;
uwsgi.cores = 1;
uwsgi.apps_cnt = 0;
uwsgi.default_app = -1;
uwsgi.buffer_size = 4096;
uwsgi.numproc = 1;
uwsgi.async = 1;
uwsgi.listen_queue = 100;
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;
gettimeofday(&uwsgi.start_tv, NULL);
setlinebuf(stdout);
uwsgi.rl.rlim_cur = 0;
uwsgi.rl.rlim_max = 0;
// are we under systemd ?
char *notify_socket = getenv("NOTIFY_SOCKET");
if (notify_socket) {
uwsgi_systemd_init(notify_socket);
}
uwsgi_notify("initializing uWSGI");
emperor_env = getenv("UWSGI_EMPEROR_FD");
if (emperor_env) {
uwsgi.has_emperor = 1;
uwsgi.emperor_fd = atoi(emperor_env);
uwsgi.master_process = 1;
uwsgi.no_orphans = 1;
uwsgi_log("*** has_emperor mode detected (fd: %d) ***\n", uwsgi.emperor_fd);
if (getenv("UWSGI_EMPEROR_FD_CONFIG")) {
uwsgi.emperor_fd_config = atoi(getenv("UWSGI_EMPEROR_FD_CONFIG"));
}
}
env_reloads = getenv("UWSGI_RELOADS");
if (env_reloads) {
//convert env value to int
uwsgi.reloads = atoi(env_reloads);
uwsgi.reloads++;
//convert reloads to string
rlen = snprintf(env_reload_buf, 10, "%u", uwsgi.reloads);
if (rlen > 0) {
env_reload_buf[rlen] = 0;
if (setenv("UWSGI_RELOADS", env_reload_buf, 1)) {
uwsgi_error("setenv()");
}
}
uwsgi.is_a_reload = 1;
}
else {
if (setenv("UWSGI_RELOADS", "0", 1)) {
uwsgi_error("setenv()");
}
}
uwsgi.binary_path = argv[0];
//initialize embedded plugins
UWSGI_LOAD_EMBEDDED_PLUGINS
// now a bit of magic, if the argv[0] basename contains a 'uwsgi_' string,
// try to automatically load a plugin
//uwsgi_log("executable name: %s\n", argv[0]);
char *p = strrchr(argv[0], '/');
if (p == NULL) p = argv[0];
p = strstr(p, "uwsgi_");
if (p != NULL) {
plugins_requested = strchr(p, '_');
if (plugins_requested != NULL && *(++plugins_requested) != '\0') {
uwsgi_log("plugin = %s\n", plugins_requested);
uwsgi_load_plugin(0, plugins_requested, NULL, 0);
}
}
plugins_requested = getenv("UWSGI_PLUGINS");
if (plugins_requested) {
char *p = strtok(plugins_requested, ",");
while (p != NULL) {
uwsgi_load_plugin(-1, p, NULL, 0);
p = strtok(NULL, ",");
}
}
build_options();
uwsgi.option_index = -1;
while ((i = getopt_long(argc, argv, short_options, uwsgi.long_options, &uwsgi.option_index)) != -1) {
if (uwsgi.option_index > -1) {
optname = (char *) uwsgi.long_options[uwsgi.option_index].name;
}
else {
optname = uwsgi_get_optname_by_index(i);
}
// is this a flagged option ?
if (i == 0) {
add_exported_option(optname, "1", 0);
}
else {
add_exported_option(optname, optarg, 1);
manage_opt(i, optarg);
}
uwsgi.option_index = -1;
}
if (optind < argc) {
char *lazy = argv[optind];
if (lazy[0] != '[') {
if (0) {
}
#ifdef UWSGI_XML
else if (!strcmp(lazy + strlen(lazy) - 4, ".xml")) {
uwsgi.xml_config = lazy;
}
#endif
#ifdef UWSGI_INI
else if (!strcmp(lazy + strlen(lazy) - 4, ".ini")) {
uwsgi.ini = lazy;
}
#endif
#ifdef UWSGI_YAML
else if (!strcmp(lazy + strlen(lazy) - 4, ".yml")) {
uwsgi.yaml = lazy;
}
else if (!strcmp(lazy + strlen(lazy) - 5, ".yaml")) {
uwsgi.yaml = lazy;
}
#endif
#ifdef UWSGI_JSON
else if (!strcmp(lazy + strlen(lazy) - 3, ".js")) {
uwsgi.json = lazy;
}
#endif
#ifdef UWSGI_SQLITE3
else if (!strcmp(lazy + strlen(lazy) - 3, ".db")) {
uwsgi.sqlite3 = lazy;
}
else if (!strcmp(lazy + strlen(lazy) - 7, ".sqlite")) {
uwsgi.sqlite3 = lazy;
}
else if (!strcmp(lazy + strlen(lazy) - 8, ".sqlite3")) {
uwsgi.sqlite3 = lazy;
}
#endif
// manage magic mountpoint
else if ((lazy[0] == '/' || strchr(lazy, '|')) && strchr(lazy, '=')) {
}
else {
int magic = 0;
for (i = 0; i < uwsgi.gp_cnt; i++) {
if (uwsgi.gp[i]->magic) {
if (uwsgi.gp[i]->magic(NULL, lazy)) {
magic = 1;
break;
}
}
}
if (!magic) {
for (i = 0; i < 0xFF; i++) {
if (uwsgi.p[i]->magic) {
if (uwsgi.p[i]->magic(NULL, lazy)) {
magic = 1;
break;
}
}
}
}
}
}
}
if (gethostname(uwsgi.hostname, 255)) {
uwsgi_error("gethostname()");
}
uwsgi.hostname_len = strlen(uwsgi.hostname);