-
Notifications
You must be signed in to change notification settings - Fork 9
/
ffunc.c
1383 lines (1195 loc) · 42.6 KB
/
ffunc.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
#if defined __GNUC__ || defined __CYGWIN__ || defined __MINGW32__ || defined __APPLE__
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <execinfo.h>
#include <unistd.h> /* needed to define getpid() */
#include <signal.h>
// #include <setjmp.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcgi_config.h>
#include <fcgiapp.h>
#include <dlfcn.h>
#include <pthread.h>
#include <sys/socket.h>
#include "ffunc.h"
#define _get_param_(KEY) FCGX_GetParam(KEY, request->envp)
#define ffunc_print(...) fprintf (stderr, __VA_ARGS__)
#define _FFUNC_MASTER_ENV "_ffunc-master_" __TIME__
#define FFUNC_SETENV setenv
#define FFUNC_GETENV getenv
#define FFUNC_EXECVE execve
#define _FFUNC_MASTER_ " ffunc-master"
#define _FFUNC_WORKER_ " ffunc-worker"
static void* mem_align(size_t size);
static int ffunc_get_number_of_digit(long long number);
static ffunc_pool* ffunc_recreate_pool(ffunc_pool *curr_p, size_t new_size);
static ffunc_str_t ffunc_proc_name;
typedef void(*h)(ffunc_session_t*);
struct ffunc_handler {
char* name;
size_t len;
h handle;
};
typedef struct {
int fcgi_func_socket;
pthread_mutex_t accept_mutex;
} ffunc_worker_t;
static int func_count = 0;
struct ffunc_handler *dyna_funcs = NULL;
static size_t max_std_input_buffer;
static int ffunc_init(char** ffunc_nmap_func);
static int ffunc_strpos(const char *haystack, const char *needle);
void *ffunc_thread_worker(void* wrker);
static int hook_socket(int sock_port, char* sock_port_str, int backlog, int max_thread, char** ffunc_nmap_func, int* procpip);
static void ffunc_add_signal_handler(void);
static void handle_request(FCGX_Request *request);
static size_t ffunc_read_body_limit(ffunc_session_t * csession, ffunc_str_t *content);
static size_t ffunc_read_body_nolimit(ffunc_session_t * csession, ffunc_str_t *content);
static void ffunc_default_direct_consume(ffunc_session_t *sess);
static int has_init_signal = 0;
static struct sigaction sa;
static void *usr_req_handle;
// extern declaration
size_t(*ffunc_read_body)(ffunc_session_t *, ffunc_str_t *);
void(*_ffunc_direct_consume)(ffunc_session_t *);
static int
ffunc_init(char** ffunc_nmap_func) {
int f = 0;
size_t szof_func_name;
usr_req_handle = NULL;
char *error;
usr_req_handle = dlopen(NULL, RTLD_LAZY | RTLD_NOW);
if (!usr_req_handle) {
ffunc_print("%s\n", dlerror());
ffunc_print("%s\n", "Module not found");
return 0;
}
for (func_count = 0; ffunc_nmap_func[func_count]; func_count++);
dyna_funcs = (struct ffunc_handler*) malloc(func_count * sizeof(struct ffunc_handler));
for (f = 0; f < func_count; f++) {
// szof_func_name = strlen(ffunc_nmap_func[f]);
dyna_funcs[f].name = ffunc_nmap_func[f]; //(char*) calloc(szof_func_name + 1, sizeof(char));
// memcpy(dyna_funcs[f].name, ffunc_nmap_func[f], szof_func_name);
dyna_funcs[f].len = strlen(ffunc_nmap_func[f]);
*(void **)(&dyna_funcs[f].handle) = dlsym(usr_req_handle, ffunc_nmap_func[f]);
if ((error = dlerror()) != NULL) {
ffunc_print("%s\n", error);
ffunc_print("%s\n", "Have you included -rdynamic in your compiler option, for e.g gcc ... -rdynamic");
return 0;
}
}
*(void **)(&_ffunc_direct_consume) = dlsym(usr_req_handle, "ffunc_direct_consume");
if ((error = dlerror()) != NULL) {
ffunc_print("%s\n", "direct consume disabled");
_ffunc_direct_consume = &ffunc_default_direct_consume;
}
else {
ffunc_print("%s\n", "direct consume enabled");
}
return 1;
}
/**To prevent -std=99 issue**/
char *
ffunc_strdup(ffunc_session_t * csession, const char *str, size_t len) {
if (len == 0) {
return NULL;
}
char *dupstr = (char*)_ffunc_alloc(&csession->pool, len + 1);
if (dupstr == NULL) {
return NULL;
}
memcpy(dupstr, str, len);
// memset(dupstr, 0, len + 1);
dupstr[len] = (char)0;
return dupstr;
}
static void
handle_request(FCGX_Request *request) {
char *value,
*qparam,
*query_string;
ffunc_pool *p = ffunc_create_pool(DEFAULT_BLK_SZ);
ffunc_session_t *_csession_ = (ffunc_session_t*)malloc(sizeof(ffunc_session_t));
if (p == NULL || _csession_ == NULL) {
fprintf(stderr, "error %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
_csession_->pool = p;
_csession_->query_str = NULL;
_csession_->request = request;
if ((value = FCGX_GetParam("FN_HANDLER", request->envp))) {
int i;
size_t vallen = strlen(value);
for (i = 0; i < func_count; i++) {
if (dyna_funcs[i].len == vallen && (strcmp(dyna_funcs[i].name, value) == 0)) {
if ((qparam = _get_param_("QUERY_STRING"))) {
query_string = ffunc_strdup(_csession_, qparam, strlen(qparam));
if (query_string && query_string[0]) {
_csession_->query_str = query_string;//parse_json_fr_args(query_string);
}
}
dyna_funcs[i].handle(_csession_);
goto RELEASE_POOL;
}
}
ffunc_default_direct_consume(_csession_);
}
else {
if ((qparam = _get_param_("QUERY_STRING"))) {
query_string = ffunc_strdup(_csession_, qparam, strlen(qparam));
if (query_string && query_string[0]) {
_csession_->query_str = query_string;//parse_json_fr_args(query_string);
}
}
_ffunc_direct_consume(_csession_);
}
RELEASE_POOL:
ffunc_destroy_pool(_csession_->pool);
free(_csession_);
}
static size_t
ffunc_read_body_nolimit(ffunc_session_t * csession, ffunc_str_t *content) {
FCGX_Request *request = csession->request;
char* clenstr = _get_param_("CONTENT_LENGTH");
FCGX_Stream *in = request->in;
size_t len;
if (clenstr && ((len = strtol(clenstr, &clenstr, 10)) != 0)) {
content->data = (char*)_ffunc_alloc(&csession->pool, len + 1);
memset(content->data, 0, len + 1);
content->len = FCGX_GetStr(content->data, len, in);
}
else {
content->data = NULL;
content->len = 0;
}
/* Chew up whats remaining (required by mod_fastcgi) */
while (FCGX_GetChar(in) != -1);
return len;
}
static size_t
ffunc_read_body_limit(ffunc_session_t * csession, ffunc_str_t *content) {
FCGX_Request *request = csession->request;
char* clenstr = _get_param_("CONTENT_LENGTH");
FCGX_Stream *in = request->in;
size_t len;
if (clenstr) {
// long int strtol (const char* str, char** endptr, int base);
// endptr -- Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
// This parameter can also be a null pointer, in which case it is not used.
len = strtol(clenstr, &clenstr, 10);
/* Don't read more than the predefined limit */
if (len > max_std_input_buffer) { len = max_std_input_buffer; }
content->data = (char*)_ffunc_alloc(&csession->pool, len + 1);
memset(content->data, 0, len + 1);
content->len = FCGX_GetStr(content->data, len, in);
}
/* Don't read if CONTENT_LENGTH is missing or if it can't be parsed */
else {
content->data = NULL;
content->len = 0;
}
/* Chew up whats remaining (required by mod_fastcgi) */
while (FCGX_GetChar(in) != -1);
return len;
}
#define FFUNC_APP_INITIALIZING "INIT"
#define FFUNC_APP_INITIALIZED "DONE"
#define FFUNC_APP_INIT_PIPE_BUF_SIZE sizeof(FFUNC_APP_INITIALIZED)
/**Main Hook**/
int
ffunc_hook(ffunc_config_t *conf) {
pid_t child_pid, daemon_pid;
int child_status;
int procpip[2];
char pipbuf[FFUNC_APP_INIT_PIPE_BUF_SIZE];
int sock_port = conf->sock_port;
char* sock_port_str = conf->sock_port_str;
int backlog = conf->backlog;
int max_thread = conf->max_thread;
char** ffunc_nmap_func = conf->ffunc_nmap_func;
size_t max_read_buffer = conf->max_read_buffer;
if (max_read_buffer > 0) {
max_std_input_buffer = max_read_buffer;
ffunc_read_body = &ffunc_read_body_limit;
}
else {
ffunc_read_body = &ffunc_read_body_nolimit;
}
if (sock_port <= 0 && sock_port_str == NULL) {
ffunc_print("%s\n", "sock_port has no defined...");
return 1;
}
if (max_thread <= 0) {
ffunc_print("%s\n", "max_thread has no defined...");
return 1;
}
if (backlog <= 0) {
ffunc_print("%s\n", "backlog has no defined...");
return 1;
}
if (!ffunc_nmap_func[0]) {
ffunc_print("%s\n", "ffunc_nmap_func has no defined...");
return 1;
}
ffunc_print("%s\n", "Service starting");
if (sock_port) {
ffunc_print("sock_port=%d, backlog=%d\n", sock_port, backlog);
} else if(sock_port_str) {
ffunc_print("sock_port=%s, backlog=%d\n", sock_port_str, backlog);
}
/** Do master pipe before fork **/
if (pipe(procpip) < 0)
exit(1);
FFUNC_WORKER_RESTART:
if (conf->daemon) {
daemon_pid = fork();
/* An error occurred */
if (daemon_pid < 0)
exit(EXIT_FAILURE);
/* Success: Let the parent terminate */
if (daemon_pid > 0)
exit(EXIT_SUCCESS);
/* On success: The child process becomes session leader */
if (setsid() < 0)
exit(EXIT_FAILURE);
}
if (!has_init_signal) {
ffunc_add_signal_handler();
}
write(procpip[1], FFUNC_APP_INITIALIZING, FFUNC_APP_INIT_PIPE_BUF_SIZE);
child_pid = fork();
if (child_pid >= 0) {// fork was successful
if (child_pid == 0) {// child process
char *swap_name = (ffunc_proc_name.data + ffunc_proc_name.len) - (sizeof(_FFUNC_MASTER_) - 1);
memcpy(swap_name, _FFUNC_WORKER_, (sizeof(_FFUNC_MASTER_) - 1));
if (conf->app_init_handler) {
conf->app_init_handler();
}
return hook_socket(sock_port, sock_port_str, backlog, max_thread, ffunc_nmap_func, procpip);
}
else { // Parent process
while (1) {
if (waitpid(child_pid, &child_status, WNOHANG) == child_pid) {
has_init_signal = 0;
if (read(procpip[0], pipbuf, FFUNC_APP_INIT_PIPE_BUF_SIZE) > 0) {
if (strncmp(pipbuf, FFUNC_APP_INITIALIZED, FFUNC_APP_INIT_PIPE_BUF_SIZE) == 0) {
goto FFUNC_WORKER_RESTART;
}
else {
printf("%s\n", "Failed while initializing the application... ");
close(procpip[0]);
close(procpip[1]);
return 1; // ERROR
}
}
}
sleep(1);
}
}
}
else {// fork failed
ffunc_print("%s\n", "Fork Child failed..");
return -1;
}
return 0;
// return hook_socket(sock_port, backlog, max_thread, ffunc_nmap_func, app_init_handler);
}
void *
ffunc_thread_worker(void* wrker) {
int rc;
ffunc_worker_t *worker_t = (ffunc_worker_t*)wrker;
FCGX_Request request;
if (FCGX_InitRequest(&request, worker_t->fcgi_func_socket, FCGI_FAIL_ACCEPT_ON_INTR) != 0) {
ffunc_print("%s\n", "Can not init request");
return NULL;
}
while (1) {
pthread_mutex_lock(&worker_t->accept_mutex);
rc = FCGX_Accept_r(&request);
pthread_mutex_unlock(&worker_t->accept_mutex);
if (rc < 0) {
ffunc_print("%s\n", "Cannot accept new request");
FCGX_Finish_r(&request); // this will free all the fcgiparams memory and request
continue;
}
handle_request(&request);
FCGX_Finish_r(&request); // this will free all the fcgiparams memory and request
}
pthread_exit(NULL);
}
static int
hook_socket(int sock_port, char *sock_port_str, int backlog, int max_thread, char** ffunc_nmap_func, int* procpip) {
char pipbuf[FFUNC_APP_INIT_PIPE_BUF_SIZE];
FCGX_Init();
if (!ffunc_init(ffunc_nmap_func)) {
exit(1);
}
char port_str[12];
ffunc_worker_t *worker_t = (ffunc_worker_t*)malloc(sizeof(ffunc_worker_t));
if (worker_t == NULL) {
perror("nomem");
}
worker_t->accept_mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; // this is lock for all threads
int i;
if (sock_port) {
sprintf(port_str, ":%d", sock_port);
if (backlog)
worker_t->fcgi_func_socket = FCGX_OpenSocket(port_str, backlog);
else
worker_t->fcgi_func_socket = FCGX_OpenSocket(port_str, 50);
} else if(sock_port_str) {
if (backlog)
worker_t->fcgi_func_socket = FCGX_OpenSocket(sock_port_str, backlog);
else
worker_t->fcgi_func_socket = FCGX_OpenSocket(sock_port_str, 50);
} else {
ffunc_print("%s\n", "argument wrong");
exit(1);
}
if (worker_t->fcgi_func_socket < 0) {
ffunc_print("%s\n", "unable to open the socket");
exit(1);
}
ffunc_print("%d threads \n", max_thread);
/** Release for success initialized **/
read(procpip[0], pipbuf, FFUNC_APP_INIT_PIPE_BUF_SIZE);
write(procpip[1], FFUNC_APP_INITIALIZED, FFUNC_APP_INIT_PIPE_BUF_SIZE);
close(procpip[0]);
close(procpip[1]);
pthread_t pth_workers[max_thread];
for (i = 0; i < max_thread; i++) {
pthread_create(&pth_workers[i], NULL, ffunc_thread_worker, worker_t);
// pthread_detach(pth_worker);
}
for (i = 0; i < max_thread; i++) {
pthread_join(pth_workers[i], NULL);
}
ffunc_print("%s\n", "Exiting");
return EXIT_SUCCESS;
}
static void
ffunc_signal_backtrace(int sfd) {
size_t i, ptr_size;
void *buffer[10];
char **strings;
ptr_size = backtrace(buffer, 1024);
fprintf(stderr, "backtrace() returned %zd addresses\n", ptr_size);
strings = backtrace_symbols(buffer, ptr_size);
if (strings == NULL) {
fprintf(stderr, "backtrace_symbols= %s", strerror(errno));
exit(EXIT_FAILURE);
}
for (i = 0; i < ptr_size; i++)
fprintf(stderr, "%s\n", strings[i]);
free(strings);
exit(EXIT_FAILURE);
}
static void
ffunc_add_signal_handler() {
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = ffunc_signal_backtrace;
sigemptyset(&sa.sa_mask);
sigaction(SIGABRT, &sa, NULL);
sigaction(SIGFPE, &sa, NULL);
sigaction(SIGILL, &sa, NULL);
sigaction(SIGIOT, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
#ifdef SIGBUS
sigaction(SIGBUS, &sa, NULL);
#endif
has_init_signal = 1;
}
int
main(int argc, char *argv[]) {
ffunc_config_t *conf;
char **new_argv, *env_ffunc_master;
unsigned int i;
int status;
if (FFUNC_GETENV(_FFUNC_MASTER_ENV)) {
conf = (ffunc_config_t*)calloc(1, sizeof(ffunc_config_t));
conf->sock_port_str = NULL;
ffunc_proc_name.data = argv[0];
ffunc_proc_name.len = strlen(argv[0]);
if ((status = ffunc_main(argc, argv, conf)) == 0) {
return ffunc_hook(conf);
}
else {
fprintf(stderr, "Error status %d, status return is not successful(0) \n", status);
return status;
}
}
else {
FFUNC_SETENV(_FFUNC_MASTER_ENV, argv[0], 1);
new_argv = (char **)calloc(argc + 1, sizeof(char*));
size_t arg$0sz = strlen(argv[0]);
new_argv[0] = (char *)calloc(arg$0sz + sizeof(_FFUNC_MASTER_), sizeof(char));
memcpy(new_argv[0], argv[0], arg$0sz);
memcpy(new_argv[0] + arg$0sz, _FFUNC_MASTER_, sizeof(_FFUNC_MASTER_) - 1);
for (i = 1; i < argc; i++) {
new_argv[i] = argv[i];
}
new_argv[i] = NULL;
extern char **environ;
FFUNC_EXECVE(argv[0], new_argv, environ);
}
return 0;
}
#else
#if defined _WIN32 || _WIN64 /*Windows*/
#include <process.h>
#include <stdio.h>
#include <windows.h>
#include <signal.h>
#include <time.h>
#include "ffunc.h"
#include <fcgiapp.h>
#ifdef UNICODE
#define FFUNC_SPRINTF swprintf
#define FFUNC_PRINTF wprintf
#else
#define FFUNC_SPRINTF sprintf_s
#define FFUNC_PRINTF printf
#endif
#define ffunc_spawn_child _spawnve
#define pipename TEXT("\\\\.\\pipe\\LogPipe")
static TCHAR *fullPipeName = NULL;
#define NAMED_PIPED_BUFF_SIZE 32
#define _get_param_(KEY) FCGX_GetParam(KEY, request->envp)
#define FFUNC_ERROR(errmsg) FFUNC_PRINTF(TEXT("%s - %d\n"), TEXT(errmsg), GetLastError() )
int setenv(const char *name, const char *value, int overwrite) {
int errcode = 0;
if (!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, name);
if (errcode || envsize) return errcode;
}
return _putenv_s(name, value);
}
char* getenv_secure(char const* _VarName) {
size_t envsize = 0;
char *exec_name = NULL;
if (getenv_s(&envsize, NULL, 0, _VarName) || envsize <= 0) {
return NULL;
}
exec_name = calloc(envsize + 1, sizeof(char));
if (getenv_s(&envsize, exec_name, envsize, _VarName) || envsize <= 0) {
return NULL;
}
return exec_name;
}
#define _FFUNC_MASTER_ENV "_ffunc-master_" __TIME__
#define FFUNC_SETENV setenv
#define FFUNC_GETENV(varname) getenv_secure(varname)
#define FFUNC_EXECVE _spawnve
#define _FFUNC_MASTER_ "_ffunc-master"
#define _FFUNC_WORKER_ "_ffunc-worker"
static void* mem_align(size_t size);
static int ffunc_get_number_of_digit(long long number);
static ffunc_pool* ffunc_recreate_pool(ffunc_pool *curr_p, size_t new_size);
typedef void(*h)(ffunc_session_t*);
typedef void(*inithandler)(void);
static ffunc_str_t ffunc_proc_name;
struct ffunc_handler {
char* name;
size_t len;
h handle;
};
typedef struct {
int fcgi_func_socket;
HANDLE accept_mutex;
} ffunc_worker_t;
static int func_count = 0;
struct ffunc_handler *dyna_funcs = NULL;
static size_t max_std_input_buffer;
static int ffunc_init(char** ffunc_nmap_func, char* init_handler_name);
static int ffunc_strpos(const char *haystack, const char *needle);
unsigned __stdcall ffunc_thread_worker(void *wrker);
static int hook_socket(int sock_port, char* sock_port_str, int backlog, int max_thread, char** ffunc_nmap_func, char* init_handler_name);
static void ffunc_add_signal_handler(void);
static void handle_request(FCGX_Request *request);
static size_t ffunc_read_body_limit(ffunc_session_t * csession, ffunc_str_t *content);
static size_t ffunc_read_body_nolimit(ffunc_session_t * csession, ffunc_str_t *content);
static void ffunc_default_direct_consume(ffunc_session_t *sess);
static int has_init_signal = 0;
static HINSTANCE usr_req_handle;
// extern declaration
size_t(*ffunc_read_body)(ffunc_session_t *, ffunc_str_t *);
h _ffunc_direct_consume;
// static void*
// read_t(void *origin) {
// ffunc_session_t *_csession_ = malloc(sizeof(ffunc_session_t));
// memcpy(_csession_, origin, sizeof(ffunc_session_t));
// return _csession_;
// }
// static void
// free_t(void* node) {
// free(node);
// }
static int
ffunc_init(char** ffunc_nmap_func, char* init_handler_name) {
int f;
SIZE_T szof_func_name;
usr_req_handle = GetModuleHandle(NULL);
// Pending FreeLibrary
if (!usr_req_handle) {
FFUNC_ERROR("GEGetModuleHandle");
return 0;
}
for (func_count = 0; ffunc_nmap_func[func_count]; func_count++);
dyna_funcs = malloc(func_count * sizeof(struct ffunc_handler));
for (f = 0; f < func_count; f++) {
szof_func_name = strlen(ffunc_nmap_func[f]);
dyna_funcs[f].name = calloc(szof_func_name + 1, sizeof(char));
memcpy(dyna_funcs[f].name, ffunc_nmap_func[f], szof_func_name);
dyna_funcs[f].len = szof_func_name;
dyna_funcs[f].handle = (h)GetProcAddress(usr_req_handle, ffunc_nmap_func[f]);
if (!dyna_funcs[f].handle) {
FFUNC_ERROR("could not locate the function");
return EXIT_FAILURE;
}
}
_ffunc_direct_consume = (h)GetProcAddress(usr_req_handle, "ffunc_direct_consume");
if (!_ffunc_direct_consume) {
FFUNC_PRINTF(TEXT("direct consume is disabled\n"));
_ffunc_direct_consume = &ffunc_default_direct_consume;
}
else {
FFUNC_PRINTF(TEXT("direct consume is enabled\n"));
}
if (init_handler_name) {
inithandler initHandler = (inithandler)GetProcAddress(usr_req_handle, init_handler_name);
if (initHandler) {
initHandler();
}
}
return 1;
}
/**To prevent -std=99 issue**/
char *
ffunc_strdup(ffunc_session_t * csession, const char *str, size_t len) {
if (len == 0) {
return NULL;
}
char *dupstr = (char*)_ffunc_alloc(&csession->pool, len + 1);
if (dupstr == NULL) {
return NULL;
}
memcpy(dupstr, str, len);
// memset(dupstr, 0, len + 1);
dupstr[len] = (char)0;
return dupstr;
}
static void
handle_request(FCGX_Request *request) {
char *value,
*qparam,
*query_string;
ffunc_pool *p = ffunc_create_pool(DEFAULT_BLK_SZ);
ffunc_session_t *_csession_ = (ffunc_session_t *)malloc(sizeof(ffunc_session_t));
if (p == NULL || _csession_ == NULL) {
char err_buff[50];
strerror_s(err_buff, sizeof err_buff, errno);
fprintf(stderr, "error %s\n", err_buff);
exit(EXIT_FAILURE);
}
_csession_->pool = p;
_csession_->query_str = NULL;
_csession_->request = request;
if ((value = FCGX_GetParam("FN_HANDLER", request->envp))) {
int i;
size_t vallen = strlen(value);
for (i = 0; i < func_count; i++) {
if (dyna_funcs[i].len == vallen && (strcmp(dyna_funcs[i].name, value) == 0)) {
if ((qparam = _get_param_("QUERY_STRING"))) {
query_string = ffunc_strdup(_csession_, qparam, strlen(qparam));
if (query_string && query_string[0]) {
_csession_->query_str = query_string;//parse_json_fr_args(query_string);
}
}
dyna_funcs[i].handle(_csession_);
goto RELEASE_POOL;
}
}
ffunc_default_direct_consume(_csession_);
}
else {
if ((qparam = _get_param_("QUERY_STRING"))) {
query_string = ffunc_strdup(_csession_, qparam, strlen(qparam));
if (query_string && query_string[0]) {
_csession_->query_str = query_string;//parse_json_fr_args(query_string);
}
}
_ffunc_direct_consume(_csession_);
}
RELEASE_POOL:
ffunc_destroy_pool(_csession_->pool);
free(_csession_);
}
static size_t
ffunc_read_body_nolimit(ffunc_session_t * csession, ffunc_str_t *content) {
FCGX_Request *request = csession->request;
char* clenstr = _get_param_("CONTENT_LENGTH");
FCGX_Stream *in = request->in;
size_t len;
if (clenstr && ((len = strtol(clenstr, &clenstr, 10)) != 0)) {
content->data = _ffunc_alloc(&csession->pool, len + 1);
memset(content->data, 0, len + 1);
content->len = FCGX_GetStr(content->data, len, in);
}
else {
content->data = NULL;
content->len = 0;
}
/* Chew up whats remaining (required by mod_fastcgi) */
while (FCGX_GetChar(in) != -1);
return len;
}
static size_t
ffunc_read_body_limit(ffunc_session_t * csession, ffunc_str_t *content) {
FCGX_Request *request = csession->request;
char* clenstr = _get_param_("CONTENT_LENGTH");
FCGX_Stream *in = request->in;
size_t len;
if (clenstr) {
// long int strtol (const char* str, char** endptr, int base);
// endptr -- Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value.
// This parameter can also be a null pointer, in which case it is not used.
len = strtol(clenstr, &clenstr, 10);
/* Don't read more than the predefined limit */
if (len > max_std_input_buffer) {
len = max_std_input_buffer;
}
content->data = _ffunc_alloc(&csession->pool, len + 1);
memset(content->data, 0, len + 1);
content->len = FCGX_GetStr(content->data, len, in);
}
/* Don't read if CONTENT_LENGTH is missing or if it can't be parsed */
else {
content->data = NULL;
content->len = 0;
}
/* Chew up whats remaining (required by mod_fastcgi) */
while (FCGX_GetChar(in) != -1);
return len;
}
static void
ffunc_interrupt_handler(intptr_t signal) {
ExitProcess(0);
}
BOOL WINAPI
console_ctrl_handler(DWORD ctrl) {
switch (ctrl)
{
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
return TRUE;
default:
return FALSE;
}
}
/**Main Hook**/
int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
int sock_port = conf->sock_port;
char* sock_port_str = conf->sock_port_str;
int backlog = conf->backlog;
int max_thread = conf->max_thread;
char** ffunc_nmap_func = conf->ffunc_nmap_func;
char* app_init_handler_name = conf->app_init_handler_name;
size_t max_read_buffer = conf->max_read_buffer;
char* exec_name = conf->__exec_name;
int totalPipeLength;
TCHAR *pipenamebuff;
if (sock_port <= 0 && sock_port_str == NULL) {
FFUNC_PRINTF(TEXT("%s\n"), TEXT("sock_port has no defined..."));
return 1;
}
if (max_thread <= 0) {
FFUNC_PRINTF(TEXT("%s\n"), TEXT("max_thread has no defined..."));
return 1;
}
if (backlog <= 0) {
FFUNC_PRINTF(TEXT("%s\n"), TEXT("backlog has no defined..."));
return 1;
}
if (!ffunc_nmap_func[0]) {
FFUNC_PRINTF(TEXT("%s\n"), TEXT("ffunc_nmap_func has no defined..."));
return 1;
}
if (max_read_buffer > 0) {
max_std_input_buffer = max_read_buffer;
ffunc_read_body = &ffunc_read_body_limit;
}
else {
ffunc_read_body = &ffunc_read_body_nolimit;
}
if (bypassmaster) {
goto CONTINUE_CHILD_PROCESS;
}
else {
goto SPAWN_CHILD_PROC;
}
SPAWN_CHILD_PROC:
// Setup a console control handler: We stop the server on CTRL-C
SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
signal(SIGINT, ffunc_interrupt_handler);
SIZE_T i;
char *sockport_str,
*backlog_str,
*max_thread_str,
*max_read_buffer_str;
DWORD parentPid = GetCurrentProcessId();
HANDLE pipe;
if (parentPid < 0) {
FFUNC_PRINTF(TEXT("%s\n"), TEXT("invalid pid"));
return 1;
}
else {
totalPipeLength = ffunc_get_number_of_digit(parentPid) + sizeof(pipename);
pipenamebuff = (TCHAR*)calloc(totalPipeLength, sizeof(TCHAR));
FFUNC_SPRINTF(pipenamebuff, totalPipeLength, TEXT("%s%d"), pipename, parentPid);
pipe = CreateNamedPipe(pipenamebuff, PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND, PIPE_WAIT, 1,
NAMED_PIPED_BUFF_SIZE, NAMED_PIPED_BUFF_SIZE, 120 * 1000, NULL);
if (pipe == INVALID_HANDLE_VALUE) {
FFUNC_PRINTF(TEXT("Error: %d"), GetLastError());
}
#ifdef UNICODE
char *toCharPipe = calloc(totalPipeLength + 1, sizeof(char));
size_t i;
wcstombs_s(&i, toCharPipe, totalPipeLength, pipenamebuff, totalPipeLength);
FFUNC_SETENV("_FFUNC_PID_NAMED_PIPE", toCharPipe, 1);
#else
FFUNC_SETENV("_FFUNC_PID_NAMED_PIPE", pipenamebuff, 1);
#endif
}
if (sock_port != 0) {
sockport_str = malloc((ffunc_get_number_of_digit(sock_port) + 1) * sizeof(char));
snprintf(sockport_str, ffunc_get_number_of_digit(sock_port) + 1, "%d", sock_port);
FFUNC_SETENV("_FFUNC_ENV_SOCK_PORT", sockport_str, 1);
} else if (sock_port_str != NULL) {
sockport_str = calloc((strlen(sock_port_str) + 1), sizeof(char));
memcpy(sockport_str, sock_port_str, strlen(sock_port_str));
FFUNC_SETENV("_FFUNC_ENV_SOCK_PORT_STR", sockport_str, 1);
}
if (app_init_handler_name) {
FFUNC_SETENV("_FFUNC_ENV_INIT_HANDLER_NAME", app_init_handler_name, 1);
}
if (backlog != 0) {
backlog_str = malloc((ffunc_get_number_of_digit(backlog) + 1) * sizeof(char));
snprintf(backlog_str, ffunc_get_number_of_digit(backlog) + 1, "%d", backlog);
FFUNC_SETENV("_FFUNC_ENV_BACKLOG", backlog_str, 1);
}
if (max_thread != 0) {
max_thread_str = malloc((ffunc_get_number_of_digit(max_thread) + 1) * sizeof(char));
snprintf(max_thread_str, ffunc_get_number_of_digit(max_thread) + 1, "%d", max_thread);
FFUNC_SETENV("_FFUNC_ENV_MAX_THREAD", max_thread_str, 1);
}
if (max_read_buffer != 0) {
max_read_buffer_str = malloc((ffunc_get_number_of_digit(max_read_buffer) + 1) * sizeof(char));
snprintf(max_read_buffer_str, ffunc_get_number_of_digit(max_read_buffer) + 1, "%d", max_read_buffer);
FFUNC_SETENV("_FFUNC_ENV_MAX_READ_BUF", max_read_buffer_str, 1);
}
SIZE_T func_str_sz = 0;
for (i = 0; ffunc_nmap_func[i]; i++) {
func_str_sz = func_str_sz + strlen(ffunc_nmap_func[i]) + sizeof("\",\" ") - 1;
}
char *func_str = malloc((func_str_sz + 1) * sizeof(char));
char* p = func_str;
for (i = 0; ffunc_nmap_func[i]; i++) {
p = (char*)(memcpy(p, ffunc_nmap_func[i], strlen(ffunc_nmap_func[i]))) + strlen(ffunc_nmap_func[i]);
p = (char*)(memcpy(p, "\",\" ", sizeof("\",\" ") - 1)) + (sizeof("\",\" ") - 1);
}
func_str[func_str_sz] = (char)0;
FFUNC_SETENV("_FFUNC_ENV_FUNC_str", func_str, 1);
// FFUNC_PRINTF("func_str = %s\n", func_str);
// Change command line to worker
if ((strlen(exec_name) + (sizeof(_FFUNC_WORKER_) - 1)) <= strlen(argv[0])) {
memmove(argv[0] + strlen(exec_name), _FFUNC_WORKER_, (sizeof(_FFUNC_WORKER_) - 1));
}
extern char **environ;
FFUNC_WORKER_RESTART:
if (exec_name) {
FFUNC_EXECVE(_P_WAIT, exec_name, argv, environ);
char data[NAMED_PIPED_BUFF_SIZE];
DWORD numRead;
//ConnectNamedPipe(pipe, NULL);
if (PeekNamedPipe(pipe, NULL, 0, NULL, &numRead, NULL)) {
if (0 != numRead) {
ReadFile(pipe, data, 1024, &numRead, NULL);
goto FFUNC_WORKER_RESTART;
}
}
CloseHandle(pipe);
}
else {
FFUNC_ERROR("Error: Exec name not found ");
}
FFUNC_ERROR("Error: ");
Sleep(3000);
ExitProcess(0);
CONTINUE_CHILD_PROCESS:
FFUNC_PRINTF(TEXT("%s\n"), TEXT("Service starting"));
if (sock_port) {
FFUNC_PRINTF(TEXT("Socket port on hook %d\n"), sock_port);
}
else if(sock_port_str){
FFUNC_PRINTF(TEXT("Socket port str on hook %s\n"), sock_port_str);
}
FFUNC_PRINTF(TEXT("backlog=%d\n"), backlog);
FFUNC_PRINTF(TEXT("%d threads \n"), max_thread);
fprintf(stderr, "%s\n", "Press Ctrl-C to terminate the process....");
return hook_socket(sock_port, sock_port_str, backlog, max_thread, ffunc_nmap_func, conf->app_init_handler_name);
}
unsigned __stdcall
ffunc_thread_worker(void* wrker) {
int rc;
ffunc_worker_t *worker_t = (ffunc_worker_t*)wrker;
FCGX_Request request;
if (FCGX_InitRequest(&request, worker_t->fcgi_func_socket, FCGI_FAIL_ACCEPT_ON_INTR) != 0) {
FFUNC_PRINTF(TEXT("%s\n"), TEXT("Can not init request"));