fixed race condition

This commit is contained in:
Pin
2022-03-06 23:41:46 -05:00
parent de1bac616b
commit fac85bb352
2 changed files with 6 additions and 6 deletions

View File

@@ -54,6 +54,7 @@ char *php_cgi(char *sPath, struct HTTPRequest *r) {
size_t bufLen = 1024;
buf = malloc(bufLen);
memset(buf, 0, bufLen);
pipe(phpPipe);
pid_t pid;
@@ -100,7 +101,7 @@ char *php_cgi(char *sPath, struct HTTPRequest *r) {
} else { // Parent fork
close(phpPipe[1]);
dup2(phpPipe2[0], STDIN_FILENO);
execlp("/usr/bin/php-cgi", "php-cgi", NULL);
execl("/usr/bin/php-cgi", "php-cgi", NULL);
}
} else {
queryString = malloc(strlen(r->requestVars) + 24);
@@ -109,11 +110,15 @@ char *php_cgi(char *sPath, struct HTTPRequest *r) {
putenv("REQUEST_METHOD=GET");
execl("/usr/bin/php-cgi", "php-cgi", NULL);
}
exit(EXIT_SUCCESS);
} else if (pid < 0) { // Error forking
printDebug("Error forking php exec");
} else { // Parent fork
size_t buffCount;
close(phpPipe[1]);
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
do {
buffCount = read(phpPipe[0], buf, BUFF_READ);
if (strlen(buf) == bufLen) {
@@ -121,9 +126,6 @@ char *php_cgi(char *sPath, struct HTTPRequest *r) {
buf = realloc(buf, bufLen);
}
} while (buffCount == 0);
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
close(phpPipe[0]);