]> git.meshlink.io Git - meshlink/blob - src/script.c
Remove support for Subnets.
[meshlink] / src / script.c
1 /*
2     script.c -- call an external script
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "logger.h"
25 #include "names.h"
26 #include "net.h"
27 #include "script.h"
28 #include "xalloc.h"
29
30 bool execute_script(const char *name, char **envp) {
31 #ifdef HAVE_SYSTEM
32         char *scriptname;
33         char *command;
34
35         xasprintf(&scriptname, "%s" SLASH "%s%s", confbase, name, scriptextension);
36
37         /* First check if there is a script */
38
39 #ifdef HAVE_MINGW
40         if(!*scriptextension) {
41                 const char *pathext = getenv("PATHEXT") ?: ".COM;.EXE;.BAT;.CMD";
42                 char fullname[strlen(scriptname) + strlen(pathext)];
43                 char *ext = fullname + strlen(scriptname);
44                 strcpy(fullname, scriptname);
45
46                 const char *p = pathext;
47                 bool found = false;
48                 while(p && *p) {
49                         const char *q = strchr(p, ';');
50                         if(q) {
51                                 memcpy(ext, p, q - p);
52                                 ext[q - p] = 0;
53                                 *q++;
54                         } else {
55                                 strcpy(ext, p);
56                         }
57                         if((found = !access(fullname, F_OK)))
58                                 break;
59                         p = q;
60                 }
61                 if(!found) {
62                         free(scriptname);
63                         return true;
64                 }
65         } else
66 #endif
67
68         if(access(scriptname, F_OK)) {
69                 free(scriptname);
70                 return true;
71         }
72
73         logger(DEBUG_STATUS, LOG_INFO, "Executing script %s", name);
74
75 #ifdef HAVE_PUTENV
76         /* Set environment */
77
78         for(int i = 0; envp[i]; i++)
79                 putenv(envp[i]);
80 #endif
81
82         if(scriptinterpreter)
83                 xasprintf(&command, "%s \"%s\"", scriptinterpreter, scriptname);
84         else
85                 xasprintf(&command, "\"%s\"", scriptname);
86
87         int status = system(command);
88
89         free(command);
90         free(scriptname);
91
92         /* Unset environment */
93
94         for(int i = 0; envp[i]; i++) {
95                 char *e = strchr(envp[i], '=');
96                 if(e) {
97                         char p[e - envp[i] + 1];
98                         strncpy(p, envp[i], e - envp[i]);
99                         p[e - envp[i]] = '\0';
100                         putenv(p);
101                 }
102         }
103
104         if(status != -1) {
105 #ifdef WEXITSTATUS
106                 if(WIFEXITED(status)) {          /* Child exited by itself */
107                         if(WEXITSTATUS(status)) {
108                                 logger(DEBUG_ALWAYS, LOG_ERR, "Script %s exited with non-zero status %d",
109                                            name, WEXITSTATUS(status));
110                                 return false;
111                         }
112                 } else if(WIFSIGNALED(status)) { /* Child was killed by a signal */
113                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s was killed by signal %d (%s)",
114                                    name, WTERMSIG(status), strsignal(WTERMSIG(status)));
115                         return false;
116                 } else {                         /* Something strange happened */
117                         logger(DEBUG_ALWAYS, LOG_ERR, "Script %s terminated abnormally", name);
118                         return false;
119                 }
120 #endif
121         } else {
122                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "system", strerror(errno));
123                 return false;
124         }
125 #endif
126         return true;
127 }