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) {
68 char scriptname[PATH_MAX];
71 snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
73 /* First check if there is a script */
76 if(!*scriptextension) {
77 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
78 size_t pathlen = strlen(pathext);
79 size_t scriptlen = strlen(scriptname);
80 char fullname[scriptlen + pathlen + 1];
81 char *ext = fullname + scriptlen;
82 strncpy(fullname, scriptname, sizeof fullname);
84 const char *p = pathext;
87 const char *q = strchr(p, ';');
89 memcpy(ext, p, q - p);
93 strncpy(ext, p, pathlen + 1);
95 if((found = !access(fullname, F_OK)))
104 if(access(scriptname, F_OK))
107 logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
109 /* Set environment */
111 for(int i = 0; envp[i]; i++)
114 if(scriptinterpreter)
115 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
117 xasprintf(&command, "\"%s\"", scriptname);
119 int status = system(command);
123 /* Unset environment */
125 for(int i = 0; envp[i]; i++)
130 if(WIFEXITED(status)) { /* Child exited by itself */
131 if(WEXITSTATUS(status)) {
132 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
133 name, WEXITSTATUS(status));
136 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
137 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
138 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
140 } else { /* Something strange happened */
141 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
146 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));