2 script.c -- call an external script
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2017 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.
31 static void unputenv(const char *p) {
32 const char *e = strchr(p, '=');
38 // Windows requires putenv("FOO=") to unset %FOO%
48 // We must keep what we putenv() around in memory.
49 // To do this without memory leaks, keep things in a list and reuse if possible.
50 static list_t list = {};
51 for list_each(char, data, &list) {
52 if(!strcmp(data, var)) {
57 char *data = xstrdup(var);
58 list_insert_tail(&list, data);
63 static void putenv(const char *p) {}
64 static void unputenv(const char *p) {}
67 static const int min_env_size;
69 int environment_add(environment_t *env, const char *format, ...) {
70 if(env->n >= env->size) {
71 env->size = env->n ? env->n * 2 : min_env_size;
72 env->entries = xrealloc(env->entries, env->size * sizeof *env->entries);
78 vasprintf(&env->entries[env->n], format, ap);
81 env->entries[env->n] = NULL;
87 void environment_update(environment_t *env, int pos, const char *format, ...) {
88 free(env->entries[pos]);
91 vasprintf(&env->entries[pos], format, ap);
95 void environment_init(environment_t *env) {
97 env->size = min_env_size;
98 env->entries = 0; //xzalloc(env->size * sizeof *env->entries);
101 environment_add(env, "NETNAME=%s", netname);
103 environment_add(env, "NAME=%s", myname);
105 environment_add(env, "DEVICE=%s", device);
107 environment_add(env, "INTERFACE=%s", iface);
109 environment_add(env, "DEBUG=%d", debug_level);
112 void environment_exit(environment_t *env) {
113 for(int i = 0; i < env->n; i++)
114 free(env->entries[i]);
118 bool execute_script(const char *name, environment_t *env) {
119 char scriptname[PATH_MAX];
122 snprintf(scriptname, sizeof scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
124 /* First check if there is a script */
127 if(!*scriptextension) {
128 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
129 size_t pathlen = strlen(pathext);
130 size_t scriptlen = strlen(scriptname);
131 char fullname[scriptlen + pathlen + 1];
132 char *ext = fullname + scriptlen;
133 strncpy(fullname, scriptname, sizeof fullname);
135 const char *p = pathext;
138 const char *q = strchr(p, ';');
140 memcpy(ext, p, q - p);
144 strncpy(ext, p, pathlen + 1);
146 if((found = !access(fullname, F_OK)))
155 if(access(scriptname, F_OK))
158 logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
160 /* Set environment */
162 for(int i = 0; i < env->n; i++)
163 putenv(env->entries[i]);
165 if(scriptinterpreter)
166 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
168 xasprintf(&command, "\"%s\"", scriptname);
170 int status = system(command);
174 /* Unset environment */
176 for(int i = 0; i < env->n; i++)
177 unputenv(env->entries[i]);
181 if(WIFEXITED(status)) { /* Child exited by itself */
182 if(WEXITSTATUS(status)) {
183 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
184 name, WEXITSTATUS(status));
187 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
188 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
189 name, WTERMSIG(status), strsignal(WTERMSIG(status)));
191 } else { /* Something strange happened */
192 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
197 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));