2 script.c -- call an external script
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2015 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 static void unputenv(const char *p) {
31 const char *e = strchr(p, '=');
37 // Windows requires putenv("FOO=") to unset %FOO%
47 // We must keep what we putenv() around in memory.
48 // To do this without memory leaks, keep things in a list and reuse if possible.
49 static list_t list = {};
50 for list_each(char, data, &list) {
51 if(!strcmp(data, var)) {
56 char *data = xstrdup(var);
57 list_insert_tail(&list, data);
62 static void putenv(const char *p) {}
63 static void unputenv(const char *p) {}
66 bool execute_script(const char *name, char **envp) {
67 char scriptname[PATH_MAX];
70 snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
72 /* First check if there is a script */
75 if(!*scriptextension) {
76 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
77 size_t pathlen = strlen(pathext);
78 size_t scriptlen = strlen(scriptname);
79 char fullname[scriptlen + pathlen + 1];
80 char *ext = fullname + scriptlen;
81 strncpy(fullname, scriptname, sizeof fullname);
83 const char *p = pathext;
86 const char *q = strchr(p, ';');
88 memcpy(ext, p, q - p);
92 strncpy(ext, p, pathlen + 1);
94 if((found = !access(fullname, F_OK)))
103 if(access(scriptname, F_OK))
106 logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
108 /* Set environment */
110 for(int i = 0; envp[i]; i++)
113 if(scriptinterpreter)
114 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
116 xasprintf(&command, "\"%s\"", scriptname);
118 int status = system(command);
122 /* Unset environment */
124 for(int i = 0; envp[i]; i++)
129 if(WIFEXITED(status)) { /* Child exited by itself */
130 if(WEXITSTATUS(status)) {
131 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
132 name, WEXITSTATUS(status));
135 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
136 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
137 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
139 } else { /* Something strange happened */
140 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
145 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));