From 5ac0f77c5575dc06bfc0db383da984c96ce284b9 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 31 Dec 2016 00:43:13 -0800 Subject: It is *never* a good idea to throw away stderr :/. --- apt-pkg/contrib/fileutl.cc | 6 ------ 1 file changed, 6 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index affab956c..dd36ffa79 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1956,12 +1956,6 @@ public: dup2(compressed_fd,STDIN_FILENO); dup2(Pipe[1],STDOUT_FILENO); } - int const nullfd = open("/dev/null", O_WRONLY); - if (nullfd != -1) - { - dup2(nullfd,STDERR_FILENO); - close(nullfd); - } SetCloseExec(STDOUT_FILENO,false); SetCloseExec(STDIN_FILENO,false); -- cgit v1.2.3 From 2d71f8c24d490b5a3773821264124f0ed5c9a9d2 Mon Sep 17 00:00:00 2001 From: Jaywalker Date: Tue, 6 Feb 2018 23:51:41 -0600 Subject: Fixed system() using coolstar's patch and added other required patches --- apt-pkg/contrib/fileutl.cc | 165 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 164 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index dd36ffa79..e3fb1cc90 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -74,6 +75,14 @@ #endif #include + +//posix spawn +#include +#include +#include +#include +#include + /*}}}*/ using namespace std; @@ -81,6 +90,8 @@ using namespace std; /* Should be a multiple of the common page size (4096) */ static constexpr unsigned long long APT_BUFFER_SIZE = 64 * 1024; +extern char **environ; + // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ // --------------------------------------------------------------------- /* */ @@ -119,7 +130,7 @@ bool RunScripts(const char *Cnf) std::clog << "Running external script: '" << Opts->Value << "'" << std::endl; - if (system(Opts->Value.c_str()) != 0) + if (RunCmd(Opts->Value.c_str()) != 0) _exit(100+Count); } _exit(0); @@ -154,6 +165,158 @@ bool RunScripts(const char *Cnf) return true; } + +#define PROC_PIDPATHINFO_MAXSIZE (1024) +static int file_exist(const char *filename) { + struct stat buffer; + int r = stat(filename, &buffer); + return (r == 0); +} + +static char *searchpath(const char *binaryname){ + if (strstr(binaryname, "/") != NULL){ + if (file_exist(binaryname)){ + char *foundpath = malloc((strlen(binaryname) + 1) * (sizeof(char))); + strcpy(foundpath, binaryname); + return foundpath; + } else { + return NULL + } + } + + char *pathvar = getenv("PATH"); + + char *dir = strtok(pathvar,":"); + while (dir != NULL){ + char searchpth[PROC_PIDPATHINFO_MAXSIZE]; + strcpy(searchpth, dir); + strcat(searchpth, "/"); + strcat(searchpth, binaryname); + + if (file_exist(searchpth)){ + char *foundpath = malloc((strlen(searchpth) + 1) * (sizeof(char))); + strcpy(foundpath, searchpth); + return foundpath; + } + + dir = strtok(NULL, ":"); + } + return NULL; +} + +static bool isShellScript(const char *path){ + FILE *file = fopen(path, "r"); + uint8_t header[2]; + if (fread(header, sizeof(uint8_t), 2, file) == 2){ + if (header[0] == '#' && header[1] == '!'){ + fclose(file); + return true; + } + } + fclose(file); + return false; +} + +static char *getInterpreter(char *path){ + FILE *file = fopen(path, "r"); + char *interpreterLine = NULL; + unsigned long lineSize = 0; + getline(&interpreterLine, &lineSize, file); + + char *rawInterpreter = (interpreterLine+2); + rawInterpreter = strtok(rawInterpreter, " "); + rawInterpreter = strtok(rawInterpreter, "\n"); + + char *interpreter = malloc((strlen(rawInterpreter)+1) * sizeof(char)); + strcpy(interpreter, rawInterpreter); + + free(interpreterLine); + fclose(file); + return interpreter; +} + +static char *fixedCmd(const char *cmdStr){ + char *cmdCpy = malloc((strlen(cmdStr)+1) * sizeof(char)); + strcpy(cmdCpy, cmdStr); + + char *cmd = strtok(cmdCpy, " "); + + uint8_t size = strlen(cmd) + 1; + + char *args = cmdCpy + (size + 1); + if ((strlen(cmdStr) - strlen(cmd)) == 0) + args = NULL; + + char *abs_path = searchpath(cmd); + if (abs_path){ + bool isScript = isShellScript(abs_path); + if (isScript){ + char *interpreter = getInterpreter(abs_path); + + uint8_t commandSize = strlen(interpreter) + 1 + strlen(abs_path); + + if (args){ + commandSize += 1 + strlen(args); + } + + char *rawCommand = malloc(sizeof(char) * (commandSize + 1)); + strcpy(rawCommand, interpreter); + strcat(rawCommand, " "); + strcat(rawCommand, abs_path); + + if (args){ + strcat(rawCommand, " "); + strcat(rawCommand, args); + } + rawCommand[(commandSize)+1] = "\0"; + + free(interpreter); + free(abs_path); + free(cmdCpy); + + return rawCommand; + } else { + uint8_t commandSize = strlen(abs_path); + + if (args){ + commandSize += 1 + strlen(args); + } + + char *rawCommand = malloc(sizeof(char) * (commandSize + 1)); + strcat(rawCommand, abs_path); + + if (args){ + strcat(rawCommand, " "); + strcat(rawCommand, args); + } + rawCommand[(commandSize)+1] = "\0"; + + free(abs_path); + free(cmdCpy); + + return rawCommand; + } + } + return cmdCpy; +} + +int RunCmd(const char *cmd) { + pid_t pid; + char *rawCmd = fixedCmd(cmd); + char *argv[] = {"sh", "-c", (char*)rawCmd, NULL}; + int status; + status = posix_spawn(&pid, "/bin/sh", NULL, NULL, argv, environ); + if (status == 0) { + if (waitpid(pid, &status, 0) == -1) { + perror("waitpid"); + } + } else { + printf("posix_spawn: %s\n", strerror(status)); + } + free(rawCmd); + return status; +} + /*}}}*/ // CopyFile - Buffered copy of a file /*{{{*/ -- cgit v1.2.3 From a227e74c6eeeca16ecb5dd6faa933e6d8c297841 Mon Sep 17 00:00:00 2001 From: Jaywalker Date: Wed, 7 Feb 2018 00:15:24 -0600 Subject: Missed a semicolon --- apt-pkg/contrib/fileutl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index e3fb1cc90..383a9b7aa 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -180,7 +180,7 @@ static char *searchpath(const char *binaryname){ strcpy(foundpath, binaryname); return foundpath; } else { - return NULL + return NULL; } } -- cgit v1.2.3 From 2363847d2685a6abe4a23620d2a09296a80c47b3 Mon Sep 17 00:00:00 2001 From: Jaywalker Date: Wed, 7 Feb 2018 01:42:06 -0600 Subject: Fixed last few errors. APT is strict... --- apt-pkg/contrib/fileutl.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 383a9b7aa..25deec083 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -176,7 +176,7 @@ static int file_exist(const char *filename) { static char *searchpath(const char *binaryname){ if (strstr(binaryname, "/") != NULL){ if (file_exist(binaryname)){ - char *foundpath = malloc((strlen(binaryname) + 1) * (sizeof(char))); + char *foundpath = (char *)malloc((strlen(binaryname) + 1) * (sizeof(char))); strcpy(foundpath, binaryname); return foundpath; } else { @@ -194,7 +194,7 @@ static char *searchpath(const char *binaryname){ strcat(searchpth, binaryname); if (file_exist(searchpth)){ - char *foundpath = malloc((strlen(searchpth) + 1) * (sizeof(char))); + char *foundpath = (char *)malloc((strlen(searchpth) + 1) * (sizeof(char))); strcpy(foundpath, searchpth); return foundpath; } @@ -227,7 +227,7 @@ static char *getInterpreter(char *path){ rawInterpreter = strtok(rawInterpreter, " "); rawInterpreter = strtok(rawInterpreter, "\n"); - char *interpreter = malloc((strlen(rawInterpreter)+1) * sizeof(char)); + char *interpreter = (char *)malloc((strlen(rawInterpreter)+1) * sizeof(char)); strcpy(interpreter, rawInterpreter); free(interpreterLine); @@ -236,7 +236,7 @@ static char *getInterpreter(char *path){ } static char *fixedCmd(const char *cmdStr){ - char *cmdCpy = malloc((strlen(cmdStr)+1) * sizeof(char)); + char *cmdCpy = (char *)malloc((strlen(cmdStr)+1) * sizeof(char)); strcpy(cmdCpy, cmdStr); char *cmd = strtok(cmdCpy, " "); @@ -259,7 +259,7 @@ static char *fixedCmd(const char *cmdStr){ commandSize += 1 + strlen(args); } - char *rawCommand = malloc(sizeof(char) * (commandSize + 1)); + char *rawCommand = (char *)malloc(sizeof(char) * (commandSize + 1)); strcpy(rawCommand, interpreter); strcat(rawCommand, " "); strcat(rawCommand, abs_path); @@ -268,7 +268,7 @@ static char *fixedCmd(const char *cmdStr){ strcat(rawCommand, " "); strcat(rawCommand, args); } - rawCommand[(commandSize)+1] = "\0"; + rawCommand[(commandSize)+1] = '\0'; free(interpreter); free(abs_path); @@ -282,14 +282,14 @@ static char *fixedCmd(const char *cmdStr){ commandSize += 1 + strlen(args); } - char *rawCommand = malloc(sizeof(char) * (commandSize + 1)); + char *rawCommand = (char *)malloc(sizeof(char) * (commandSize + 1)); strcat(rawCommand, abs_path); if (args){ strcat(rawCommand, " "); strcat(rawCommand, args); } - rawCommand[(commandSize)+1] = "\0"; + rawCommand[(commandSize)+1] = '\0'; free(abs_path); free(cmdCpy); -- cgit v1.2.3 From 930007ca221e1578afa235cb7e42345f39f028db Mon Sep 17 00:00:00 2001 From: Jaywalker Date: Wed, 7 Feb 2018 17:01:37 -0600 Subject: Lets get those sizes right --- apt-pkg/contrib/fileutl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 25deec083..f3c7b6d3b 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -243,7 +243,7 @@ static char *fixedCmd(const char *cmdStr){ uint8_t size = strlen(cmd) + 1; - char *args = cmdCpy + (size + 1); + char *args = cmdCpy + size; if ((strlen(cmdStr) - strlen(cmd)) == 0) args = NULL; -- cgit v1.2.3