@@ -494,8 +494,20 @@ main(int argc, char *argv[]) {
494494#include "ffunc.h"
495495#include <fcgiapp.h>
496496
497+
498+ #ifdef UNICODE
499+ #define FFUNC_SPRINTF swprintf
500+ #define FFUNC_PRINTF wprintf
501+ #else
502+ #define FFUNC_SPRINTF sprintf_s
503+ #define FFUNC_PRINTF printf
504+ #endif
505+ #define ffunc_spawn_child _spawnve
506+ #define pipename TEXT("\\\\.\\pipe\\LogPipe")
507+ static TCHAR * fullPipeName = NULL ;
508+ #define NAMED_PIPED_BUFF_SIZE 32
497509#define _get_param_ (KEY ) FCGX_GetParam(KEY, request->envp)
498- #define FFUNC_ERROR (errmsg ) fprintf(stderr, "%s - %d\n", errmsg, GetLastError() )
510+ #define FFUNC_ERROR (errmsg ) FFUNC_PRINTF(TEXT( "%s - %d\n"), TEXT( errmsg) , GetLastError() )
499511
500512int setenv (const char * name , const char * value , int overwrite ) {
501513 int errcode = 0 ;
@@ -745,9 +757,6 @@ console_ctrl_handler(DWORD ctrl) {
745757 }
746758}
747759
748- #define ffunc_print (...) printf ( __VA_ARGS__)
749- #define ffunc_spawn_child _spawnve
750-
751760/**Main Hook**/
752761int ffunc_hook (char * * argv , ffunc_config_t * conf , int bypassmaster ) {
753762 int sock_port = conf -> sock_port ;
@@ -756,25 +765,27 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
756765 char * * ffunc_nmap_func = conf -> ffunc_nmap_func ;
757766 size_t max_read_buffer = conf -> max_read_buffer ;
758767 char * exec_name = conf -> __exec_name ;
768+ int totalPipeLength ;
769+ TCHAR * pipenamebuff ;
759770
760771
761772 if (sock_port <= 0 ) {
762- ffunc_print ( "%s\n" , "sock_port has no defined..." );
773+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "sock_port has no defined..." ) );
763774 return 1 ;
764775 }
765776
766777 if (max_thread <= 0 ) {
767- ffunc_print ( "%s\n" , "max_thread has no defined..." );
778+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "max_thread has no defined..." ) );
768779 return 1 ;
769780 }
770781
771782 if (backlog <= 0 ) {
772- ffunc_print ( "%s\n" , "backlog has no defined..." );
783+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "backlog has no defined..." ) );
773784 return 1 ;
774785 }
775786
776787 if (!ffunc_nmap_func [0 ]) {
777- ffunc_print ( "%s\n" , "ffunc_nmap_func has no defined..." );
788+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "ffunc_nmap_func has no defined..." ) );
778789 return 1 ;
779790 }
780791
@@ -804,6 +815,32 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
804815 * max_thread_str ,
805816 * max_read_buffer_str ;
806817
818+ DWORD parentPid = GetCurrentProcessId ();
819+ HANDLE pipe ;
820+
821+ if (parentPid < 0 ) {
822+ FFUNC_PRINTF (TEXT ("%s\n" ), TEXT ("invalid pid" ));
823+ return 1 ;
824+ }
825+ else {
826+ totalPipeLength = ffunc_get_number_of_digit (parentPid ) + sizeof (pipename );
827+ pipenamebuff = (TCHAR * )calloc (totalPipeLength , sizeof (TCHAR ));
828+ FFUNC_SPRINTF (pipenamebuff , totalPipeLength , TEXT ("%s%d" ), pipename , parentPid );
829+ pipe = CreateNamedPipe (pipenamebuff , PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND , PIPE_WAIT , 1 ,
830+ NAMED_PIPED_BUFF_SIZE , NAMED_PIPED_BUFF_SIZE , 120 * 1000 , NULL );
831+ if (pipe == INVALID_HANDLE_VALUE ) {
832+ FFUNC_PRINTF (TEXT ("Error: %d" ), GetLastError ());
833+ }
834+ #ifdef UNICODE
835+ char * toCharPipe = calloc (totalPipeLength + 1 , sizeof (char ));
836+ size_t i ;
837+ wcstombs_s (& i , toCharPipe , totalPipeLength , pipenamebuff , totalPipeLength );
838+ FFUNC_SETENV ("_FFUNC_PID_NAMED_PIPE" , toCharPipe , 1 );
839+ #else
840+ FFUNC_SETENV ("_FFUNC_PID_NAMED_PIPE" , pipenamebuff , 1 );
841+ #endif
842+ }
843+
807844 if (sock_port != 0 ) {
808845 sockport_str = malloc ((ffunc_get_number_of_digit (sock_port ) + 1 ) * sizeof (char ));
809846 snprintf (sockport_str , ffunc_get_number_of_digit (sock_port ) + 1 , "%d" , sock_port );
@@ -844,7 +881,7 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
844881 func_str [func_str_sz ] = (char )0 ;
845882
846883 FFUNC_SETENV ("_FFUNC_ENV_FUNC_str" , func_str , 1 );
847- // ffunc_print ("func_str = %s\n", func_str);
884+ // FFUNC_PRINTF ("func_str = %s\n", func_str);
848885
849886 // Change command line to worker
850887 if ((strlen (exec_name ) + (sizeof (_FFUNC_WORKER_ ) - 1 )) <= strlen (argv [0 ])) {
@@ -855,7 +892,18 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
855892FFUNC_WORKER_RESTART :
856893 if (exec_name ) {
857894 FFUNC_EXECVE (_P_WAIT , exec_name , argv , environ );
858- goto FFUNC_WORKER_RESTART ;
895+ char data [NAMED_PIPED_BUFF_SIZE ];
896+ DWORD numRead ;
897+ //ConnectNamedPipe(pipe, NULL);
898+ if (PeekNamedPipe (pipe , NULL , 0 , NULL , & numRead , NULL )) {
899+ if (0 != numRead ) {
900+ ReadFile (pipe , data , 1024 , & numRead , NULL );
901+ goto FFUNC_WORKER_RESTART ;
902+ }
903+ }
904+
905+ CloseHandle (pipe );
906+
859907 }
860908 else {
861909 FFUNC_ERROR ("Error: Exec name not found " );
@@ -868,10 +916,10 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
868916
869917CONTINUE_CHILD_PROCESS :
870918
871- ffunc_print ( "%s\n" , "Service starting" );
872- ffunc_print ( "Socket port on hook %d\n" , sock_port );
873- ffunc_print ( "backlog=%d\n" , backlog );
874- ffunc_print ( "%d threads \n" , max_thread );
919+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "Service starting" ) );
920+ FFUNC_PRINTF ( TEXT ( "Socket port on hook %d\n" ) , sock_port );
921+ FFUNC_PRINTF ( TEXT ( "backlog=%d\n" ) , backlog );
922+ FFUNC_PRINTF ( TEXT ( "%d threads \n" ) , max_thread );
875923 fprintf (stderr , "%s\n" , "Press Ctrl-C to terminate the process...." );
876924 return hook_socket (sock_port , backlog , max_thread , ffunc_nmap_func );
877925}
@@ -883,15 +931,15 @@ ffunc_thread_worker(void* wrker) {
883931
884932 FCGX_Request request ;
885933 if (FCGX_InitRequest (& request , worker_t -> fcgi_func_socket , FCGI_FAIL_ACCEPT_ON_INTR ) != 0 ) {
886- ffunc_print ( "%s\n" , "Can not init request" );
934+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "Can not init request" ) );
887935 return 0 ;
888936 }
889937 while (1 ) {
890938 WaitForSingleObject (& worker_t -> accept_mutex , INFINITE );
891939 rc = FCGX_Accept_r (& request );
892940 ReleaseMutex (& worker_t -> accept_mutex );
893941 if (rc < 0 ) {
894- ffunc_print ( "%s\n" , "Cannot accept new request" );
942+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "Cannot accept new request" ) );
895943 FCGX_Finish_r (& request ); // this will free all the fcgiparams memory and request
896944 continue ;
897945 }
@@ -903,6 +951,32 @@ ffunc_thread_worker(void* wrker) {
903951
904952static int
905953hook_socket (int sock_port , int backlog , int max_thread , char * * ffunc_nmap_func ) {
954+ char * _pipeName ;
955+ // #define MAX_PIPE_SIZE 256
956+ // TCHAR *pipeName;
957+ HANDLE pipe ;
958+ if (! (_pipeName = FFUNC_GETENV ("_FFUNC_PID_NAMED_PIPE" ))) {
959+ FFUNC_PRINTF (TEXT ("%s\n" ), TEXT ("Failed getting _FFUNC_PID_NAMED_PIPE" ));
960+ return -1 ;
961+ }
962+
963+ /*
964+ #ifdef UNICODE
965+ size_t szi;
966+ pipeName = calloc(MAX_PIPE_SIZE, sizeof(TCHAR));
967+ int numConverted = mbstowcs_s(&szi, pipeName, MAX_PIPE_SIZE, _pipeName, MAX_PIPE_SIZE);
968+ #else
969+ pipeName = _pipeName;
970+ #endif
971+ */
972+
973+ //FFUNC_PRINTF(TEXT("PIPENAME IS %s"), pipeName);
974+ pipe = CreateFileA (_pipeName , GENERIC_READ | GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , 0 , NULL );
975+ if (pipe == INVALID_HANDLE_VALUE ) {
976+ FFUNC_ERROR ("Create file FAILED" );
977+ return -1 ;
978+ }
979+
906980 FCGX_Init ();
907981 if (!ffunc_init (ffunc_nmap_func )) {
908982 exit (1 );
@@ -925,15 +999,20 @@ hook_socket(int sock_port, int backlog, int max_thread, char** ffunc_nmap_func)
925999 worker_t -> fcgi_func_socket = FCGX_OpenSocket (port_str , 50 );
9261000 }
9271001 else {
928- ffunc_print ( "%s\n" , "argument wrong" );
1002+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "argument wrong" ) );
9291003 exit (1 );
9301004 }
9311005
9321006 if (worker_t -> fcgi_func_socket < 0 ) {
933- ffunc_print ( "%s\n" , "unable to open the socket" );
1007+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "unable to open the socket" ) );
9341008 exit (1 );
9351009 }
9361010
1011+ /* Release of successfully initialized */
1012+ DWORD numWritten ;
1013+ WriteFile (pipe , TEXT ("DONE" ), sizeof (TEXT ("DONE" )), & numWritten , NULL );
1014+ CloseHandle (pipe );
1015+
9371016 HANDLE * pth_workers = calloc (max_thread , sizeof (HANDLE ));
9381017 for (i = 0 ; i < max_thread ; i ++ ) {
9391018 unsigned thread_id ;
@@ -943,7 +1022,7 @@ hook_socket(int sock_port, int backlog, int max_thread, char** ffunc_nmap_func)
9431022 WaitForSingleObject (pth_workers [i ], INFINITE );
9441023 }
9451024 free (pth_workers );
946- ffunc_print ( "%s\n" , "Exiting" );
1025+ FFUNC_PRINTF ( TEXT ( "%s\n" ), TEXT ( "Exiting" ) );
9471026 return EXIT_SUCCESS ;
9481027}
9491028
@@ -1149,12 +1228,12 @@ mem_align(size_t size) //alignment => 16
11491228 int err ;
11501229 err = posix_memalign (& p , ALIGNMENT , size );
11511230 if (err ) {
1152- ffunc_print ("posix_memalign(%uz, %uz) failed \n" , ALIGNMENT , size );
1231+ // FFUNC_PRINTF ("posix_memalign(%uz, %uz) failed \n", ALIGNMENT, size);
11531232 p = NULL ;
11541233 }
1155- ffunc_print ("posix_memalign: %p:%uz @%uz \n" , p , size , ALIGNMENT );
1234+ // FFUNC_PRINTF ("posix_memalign: %p:%uz @%uz \n", p, size, ALIGNMENT);
11561235#else
1157- // ffunc_print ("%s\n", "Using Malloc");
1236+ // FFUNC_PRINTF ("%s\n", "Using Malloc");
11581237 p = malloc (size );
11591238
11601239#endif
0 commit comments