|
@@ -26,6 +26,7 @@
|
|
#include <apt-pkg/aptconfiguration.h>
|
|
#include <apt-pkg/aptconfiguration.h>
|
|
#include <apt-pkg/configuration.h>
|
|
#include <apt-pkg/configuration.h>
|
|
#include <apt-pkg/macros.h>
|
|
#include <apt-pkg/macros.h>
|
|
|
|
+#include <apt-pkg/endian.h>
|
|
|
|
|
|
#include <ctype.h>
|
|
#include <ctype.h>
|
|
#include <stdarg.h>
|
|
#include <stdarg.h>
|
|
@@ -74,6 +75,14 @@
|
|
#endif
|
|
#endif
|
|
|
|
|
|
#include <apti18n.h>
|
|
#include <apti18n.h>
|
|
|
|
+
|
|
|
|
+//posix spawn
|
|
|
|
+#include <stdint.h>
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <spawn.h>
|
|
|
|
+#include <sys/wait.h>
|
|
|
|
+
|
|
/*}}}*/
|
|
/*}}}*/
|
|
|
|
|
|
using namespace std;
|
|
using namespace std;
|
|
@@ -81,6 +90,8 @@ using namespace std;
|
|
/* Should be a multiple of the common page size (4096) */
|
|
/* Should be a multiple of the common page size (4096) */
|
|
static constexpr unsigned long long APT_BUFFER_SIZE = 64 * 1024;
|
|
static constexpr unsigned long long APT_BUFFER_SIZE = 64 * 1024;
|
|
|
|
|
|
|
|
+extern char **environ;
|
|
|
|
+
|
|
// RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
|
|
// RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
|
|
// ---------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------
|
|
/* */
|
|
/* */
|
|
@@ -119,7 +130,7 @@ bool RunScripts(const char *Cnf)
|
|
std::clog << "Running external script: '"
|
|
std::clog << "Running external script: '"
|
|
<< Opts->Value << "'" << std::endl;
|
|
<< Opts->Value << "'" << std::endl;
|
|
|
|
|
|
- if (system(Opts->Value.c_str()) != 0)
|
|
|
|
|
|
+ if (RunCmd(Opts->Value.c_str()) != 0)
|
|
_exit(100+Count);
|
|
_exit(100+Count);
|
|
}
|
|
}
|
|
_exit(0);
|
|
_exit(0);
|
|
@@ -154,6 +165,158 @@ bool RunScripts(const char *Cnf)
|
|
|
|
|
|
return true;
|
|
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 /*{{{*/
|
|
// CopyFile - Buffered copy of a file /*{{{*/
|