Skip to content

Commit c8ca725

Browse files
committed
update
1 parent 8964f55 commit c8ca725

File tree

3 files changed

+78
-111
lines changed

3 files changed

+78
-111
lines changed

examples/profile_service.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ int ffunc_main(int argc, char *argv[], ffunc_config_t *ffunc_conf) {
4242
ffunc_conf->sock_port = 2005;
4343
ffunc_conf->backlog = 160;
4444
ffunc_conf->max_thread = 64;
45+
// ffunc_conf->daemon = 1;
4546
ffunc_parse_function(ffunc_conf, "getProfile", "postError", "postProfile");
4647
return 0;
4748
}

ffunc.c

Lines changed: 65 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ int
450450
main(int argc, char *argv[]) {
451451
ffunc_config_t *conf;
452452
char **new_argv, *env_ffunc_master;
453-
unsigned int i = 1;
453+
unsigned int i;
454454
int status;
455455

456456
if (FFUNC_GETENV(_FFUNC_MASTER_ENV)) {
@@ -473,8 +473,8 @@ main(int argc, char *argv[]) {
473473
memcpy(new_argv[0], argv[0], arg$0sz);
474474
memcpy(new_argv[0] + arg$0sz, _FFUNC_MASTER_, sizeof(_FFUNC_MASTER_) - 1);
475475

476-
while (argv[i]) {
477-
new_argv[i] = argv[i++];
476+
for (i = 1; i < argc; i++) {
477+
new_argv[i] = argv[i];
478478
}
479479

480480
new_argv[i] = NULL;
@@ -495,7 +495,7 @@ main(int argc, char *argv[]) {
495495
#include <fcgiapp.h>
496496

497497

498-
#ifdef UNICODE
498+
#ifdef UNICODE
499499
#define FFUNC_SPRINTF swprintf
500500
#define FFUNC_PRINTF wprintf
501501
#else
@@ -765,12 +765,12 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
765765
char** ffunc_nmap_func = conf->ffunc_nmap_func;
766766
size_t max_read_buffer = conf->max_read_buffer;
767767
char* exec_name = conf->__exec_name;
768-
int totalPipeLength;
769-
TCHAR *pipenamebuff;
768+
int totalPipeLength;
769+
TCHAR *pipenamebuff;
770770

771771

772772
if (sock_port <= 0) {
773-
FFUNC_PRINTF(TEXT("%s\n"), TEXT("sock_port has no defined..."));
773+
FFUNC_PRINTF(TEXT("%s\n"), TEXT("sock_port has no defined..."));
774774
return 1;
775775
}
776776

@@ -815,31 +815,31 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
815815
*max_thread_str,
816816
*max_read_buffer_str;
817817

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-
}
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+
}
834834
#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);
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);
839839
#else
840-
FFUNC_SETENV("_FFUNC_PID_NAMED_PIPE", pipenamebuff, 1);
840+
FFUNC_SETENV("_FFUNC_PID_NAMED_PIPE", pipenamebuff, 1);
841841
#endif
842-
}
842+
}
843843

844844
if (sock_port != 0) {
845845
sockport_str = malloc((ffunc_get_number_of_digit(sock_port) + 1) * sizeof(char));
@@ -892,17 +892,17 @@ int ffunc_hook(char **argv, ffunc_config_t *conf, int bypassmaster) {
892892
FFUNC_WORKER_RESTART:
893893
if (exec_name) {
894894
FFUNC_EXECVE(_P_WAIT, exec_name, argv, environ);
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-
}
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+
}
904904

905-
CloseHandle(pipe);
905+
CloseHandle(pipe);
906906

907907
}
908908
else {
@@ -951,31 +951,31 @@ ffunc_thread_worker(void* wrker) {
951951

952952
static int
953953
hook_socket(int sock_port, int backlog, int max_thread, char** ffunc_nmap_func) {
954-
char *_pipeName;
954+
char *_pipeName;
955955
// #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-
/*
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+
/*
964964
#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);
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);
968968
#else
969-
pipeName = _pipeName;
969+
pipeName = _pipeName;
970970
#endif
971-
*/
971+
*/
972972

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-
}
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+
}
979979

980980
FCGX_Init();
981981
if (!ffunc_init(ffunc_nmap_func)) {
@@ -1008,10 +1008,10 @@ hook_socket(int sock_port, int backlog, int max_thread, char** ffunc_nmap_func)
10081008
exit(1);
10091009
}
10101010

1011-
/* Release of successfully initialized */
1012-
DWORD numWritten;
1013-
WriteFile(pipe, TEXT("DONE"), sizeof(TEXT("DONE")), &numWritten, NULL);
1014-
CloseHandle(pipe);
1011+
/* Release of successfully initialized */
1012+
DWORD numWritten;
1013+
WriteFile(pipe, TEXT("DONE"), sizeof(TEXT("DONE")), &numWritten, NULL);
1014+
CloseHandle(pipe);
10151015

10161016
HANDLE *pth_workers = calloc(max_thread, sizeof(HANDLE));
10171017
for (i = 0; i < max_thread; i++) {
@@ -1160,41 +1160,6 @@ ffunc_get_query_param(ffunc_session_t * csession, const char *key, size_t len) {
11601160
return NULL;
11611161
}
11621162

1163-
void
1164-
ffunc_write_http_ok_status(ffunc_session_t * csession) {
1165-
ffunc_write_out(csession, "Status: 200 OK\r\n");
1166-
}
1167-
1168-
void
1169-
ffunc_write_http_not_found_status(ffunc_session_t * csession) {
1170-
ffunc_write_out(csession, "Status: 404 Not Found\r\n");
1171-
}
1172-
1173-
void
1174-
ffunc_write_http_internal_error_status(ffunc_session_t * csession) {
1175-
ffunc_write_out(csession, "Status: 500 Internal Server Error\r\n");
1176-
}
1177-
1178-
void
1179-
ffunc_write_http_no_content_status(ffunc_session_t * csession) {
1180-
ffunc_write_out(csession, "Status: 204 No Content\r\n");
1181-
}
1182-
1183-
void
1184-
ffunc_write_default_header(ffunc_session_t * csession) {
1185-
ffunc_write_out(csession, "Content-Type: application/x-www-form-urlencoded\r\n\r\n");
1186-
}
1187-
1188-
void
1189-
ffunc_write_jsonp_header(ffunc_session_t * csession) {
1190-
ffunc_write_out(csession, "Content-Type: application/javascript\r\n\r\n");
1191-
}
1192-
1193-
void
1194-
ffunc_write_json_header(ffunc_session_t * csession) {
1195-
ffunc_write_out(csession, "Content-Type: application/json\r\n\r\n");
1196-
}
1197-
11981163
void
11991164
ffunc_destroy_pool(ffunc_pool *p) {
12001165
if (p) {
@@ -1228,10 +1193,10 @@ mem_align(size_t size) //alignment => 16
12281193
int err;
12291194
err = posix_memalign(&p, ALIGNMENT, size);
12301195
if (err) {
1231-
// FFUNC_PRINTF("posix_memalign(%uz, %uz) failed \n", ALIGNMENT, size);
1196+
// FFUNC_PRINTF("posix_memalign(%uz, %uz) failed \n", ALIGNMENT, size);
12321197
p = NULL;
12331198
}
1234-
// FFUNC_PRINTF("posix_memalign: %p:%uz @%uz \n", p, size, ALIGNMENT);
1199+
// FFUNC_PRINTF("posix_memalign: %p:%uz @%uz \n", p, size, ALIGNMENT);
12351200
#else
12361201
// FFUNC_PRINTF("%s\n", "Using Malloc");
12371202
p = malloc(size);

ffunc.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ extern size_t ffunc_mem_left(ffunc_pool *p);
7272
extern size_t ffunc_mem_used(ffunc_pool *p);
7373
extern size_t ffunc_blk_size(ffunc_pool *p);
7474

75+
extern int ffunc_main(int argc, char *argv[], ffunc_config_t *ffunc_conf);
76+
extern char *ffunc_strdup(ffunc_session_t * csession, const char *str, size_t len);
77+
extern size_t(*ffunc_read_body)(ffunc_session_t *, ffunc_str_t *);
78+
extern void* ffunc_get_query_param(ffunc_session_t * csession, const char *key, size_t len);
79+
7580
#define ffunc_parse_function(ffconf_, ...) do { \
7681
const char *_ffunc_nmap_func[] = {__VA_ARGS__, NULL}; \
7782
unsigned int i=0, j; \
@@ -85,21 +90,17 @@ char *x =(char*) malloc(len); \
8590
(ffconf_)->ffunc_nmap_func[i] = NULL; \
8691
} while(0)
8792

88-
extern int ffunc_main(int argc, char *argv[], ffunc_config_t *ffunc_conf);
8993
#define ffunc_write_out(_csession, ...) FCGX_FPrintF(_csession->request->out, __VA_ARGS__)
9094
#define ffunc_get_fcgi_param(_csession, constkey) FCGX_GetParam(constkey, _csession->request->envp)
9195
#define ffunc_alloc(ffunc_session_t, sz) _ffunc_alloc(&ffunc_session_t->pool, sz)
92-
extern char *ffunc_strdup(ffunc_session_t * csession, const char *str, size_t len);
9396

94-
extern size_t(*ffunc_read_body)(ffunc_session_t *, ffunc_str_t *);
95-
extern void* ffunc_get_query_param(ffunc_session_t * csession, const char *key, size_t len);
96-
extern void ffunc_write_http_ok_status(ffunc_session_t * csession);
97-
extern void ffunc_write_http_not_found_status(ffunc_session_t * csession);
98-
extern void ffunc_write_http_internal_error_status(ffunc_session_t * csession);
99-
extern void ffunc_write_http_no_content_status(ffunc_session_t * csession);
100-
extern void ffunc_write_default_header(ffunc_session_t * csession);
101-
extern void ffunc_write_jsonp_header(ffunc_session_t * csession);
102-
extern void ffunc_write_json_header(ffunc_session_t * csession);
97+
#define ffunc_write_http_ok_status(_csession) ffunc_write_out(_csession, "Status: 200 OK\r\n")
98+
#define ffunc_write_http_not_found_status(_csession) ffunc_write_out(_csession, "Status: 404 Not Found\r\n")
99+
#define ffunc_write_http_internal_error_status(_csession) ffunc_write_out(_csession, "Status: 500 Internal Server Error\r\n")
100+
#define ffunc_write_http_no_content_status(_csession) ffunc_write_out(_csession, "Status: 204 No Content\r\n")
101+
#define ffunc_write_default_header(_csession) ffunc_write_out(_csession, "Content-Type: application/x-www-form-urlencoded\r\n\r\n")
102+
#define ffunc_write_jsonp_header(_csession) ffunc_write_out(_csession, "Content-Type: application/javascript\r\n\r\n")
103+
#define ffunc_write_json_header(_csession) ffunc_write_out(_csession, "Content-Type: application/json\r\n\r\n")
103104

104105
#ifdef __cplusplus
105106
}

0 commit comments

Comments
 (0)