-/*
- Fork and execute the program pointed to by name.
-*/
-int execute_script(const char *name)
-{
- pid_t pid;
- int status;
-cp
- if((pid = fork()) < 0)
- {
- syslog(LOG_ERR, _("System call `%s' failed: %m"),
- "fork");
- return -1;
- }
-
- if(pid)
- {
- if(debug_lvl >= DEBUG_STATUS)
- syslog(LOG_INFO, _("Executing script %s"), name);
-
- if(waitpid(pid, &status, 0) == pid)
- {
- if(WIFEXITED(status)) /* Child exited by itself */
- {
- if(WEXITSTATUS(status))
- {
- syslog(LOG_ERR, _("Process %d (%s) exited with non-zero status %d"), pid, name, WEXITSTATUS(status));
- return -1;
- }
- else
- return 0;
- }
- else if(WIFSIGNALED(status)) /* Child was killed by a signal */
- {
- syslog(LOG_ERR, _("Process %d (%s) was killed by signal %d (%s)"),
- pid, name, WTERMSIG(status), strsignal(WTERMSIG(status)));
- return -1;
- }
- else /* Something strange happened */
- {
- syslog(LOG_ERR, _("Process %d (%s) terminated abnormally"), pid, name);
- return -1;
- }
- }
- else
- {
- syslog(LOG_ERR, _("System call `%s' failed: %m"), "waitpid");
- return -1;
- }
- }
-cp
- /* Child here */
-
- _execute_script(name);
+bool execute_script(const char *name, char **envp) {
+#ifdef HAVE_SYSTEM
+ char *scriptname;
+ char *interpreter = NULL;
+ config_t *cfg_interpreter;
+ int status, len, i;
+
+ cfg_interpreter = lookup_config(config_tree, "ScriptsInterpreter");
+#ifndef HAVE_MINGW
+ len = xasprintf(&scriptname, "\"%s/%s\"", confbase, name);
+#else
+
+ if(cfg_interpreter) {
+ len = xasprintf(&scriptname, "\"%s/%s\"", confbase, name);
+ } else {
+ len = xasprintf(&scriptname, "\"%s/%s.bat\"", confbase, name);
+ }
+
+#endif
+
+ if(len < 0) {
+ return false;
+ }
+
+ scriptname[len - 1] = '\0';
+
+ /* First check if there is a script */
+ if(access(scriptname + 1, F_OK)) {
+ free(scriptname);
+ return true;
+ }
+
+ // Custom scripts interpreter
+ if(get_config_string(cfg_interpreter, &interpreter)) {
+ // Force custom scripts interpreter allowing execution of scripts on android without execution flag (such as on /sdcard)
+ free(scriptname);
+ len = xasprintf(&scriptname, "%s \"%s/%s\"", interpreter, confbase, name);
+ free(interpreter);
+
+ if(len < 0) {
+ return false;
+ }
+ }
+
+ ifdebug(STATUS) logger(LOG_INFO, "Executing script %s", name);
+
+ /* Set environment */
+
+ for(i = 0; envp[i]; i++) {
+ putenv(envp[i]);
+ }
+
+ scriptname[len - 1] = '\"';
+ status = system(scriptname);
+
+ free(scriptname);
+
+ /* Unset environment */
+
+ for(i = 0; envp[i]; i++) {
+ unputenv(envp[i]);
+ }
+
+ if(status != -1) {
+#ifdef WEXITSTATUS
+
+ if(WIFEXITED(status)) { /* Child exited by itself */
+ if(WEXITSTATUS(status)) {
+ logger(LOG_ERR, "Script %s exited with non-zero status %d",
+ name, WEXITSTATUS(status));
+ return false;
+ }
+ } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
+ logger(LOG_ERR, "Script %s was killed by signal %d (%s)",
+ name, WTERMSIG(status), strsignal(WTERMSIG(status)));
+ return false;
+ } else { /* Something strange happened */
+ logger(LOG_ERR, "Script %s terminated abnormally", name);
+ return false;
+ }
+
+#endif
+ } else {
+ logger(LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
+ return false;
+ }
+
+#endif
+ return true;