1 /*
2  * Copyright (C) 2013-2019 Daniel Scharrer
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty.  In no event will the author(s) be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  *    claim that you wrote the original software. If you use this software
14  *    in a product, an acknowledgment in the product documentation would be
15  *    appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  *    misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #include "util/process.hpp"
22 
23 #include <sstream>
24 #include <iostream>
25 
26 #include "configure.hpp"
27 
28 #if defined(_WIN32)
29 
30 #include <string.h>
31 #include <windows.h>
32 
33 #include <boost/format.hpp>
34 #include <boost/algorithm/string.hpp>
35 
36 #elif INNOEXTRACT_HAVE_POSIX_SPAWNP || (INNOEXTRACT_HAVE_FORK && INNOEXTRACT_HAVE_EXECVP)
37 
38 #if INNOEXTRACT_HAVE_POSIX_SPAWNP
39 #include <spawn.h>
40 #if !INNOEXTRACT_HAVE_UNISTD_ENVIRON
41 extern "C" {
42 #if defined(__FreeBSD__) && defined(__GNUC__) && __GNUC__ >= 4
43 /*
44  * When combining -flto and -fvisibility=hidden we and up with a hidden
45  * 'environ' symbol in crt1.o on FreeBSD 9, which causes the link to fail.
46  */
47 extern char ** environ __attribute__((visibility("default")));
48 #else
49 extern char ** environ;
50 #endif
51 }
52 #endif
53 #endif
54 
55 #if INNOEXTRACT_HAVE_UNISTD_ENVIRON || (INNOEXTRACT_HAVE_FORK && INNOEXTRACT_HAVE_EXECVP)
56 #include <unistd.h>
57 #endif
58 
59 #if INNOEXTRACT_HAVE_WAITPID
60 #include <sys/wait.h>
61 #endif
62 
63 #else
64 
65 #include <cstdlib>
66 
67 #endif
68 
69 #include "util/encoding.hpp"
70 
71 namespace util {
72 
73 #if defined(_WIN32) || !(INNOEXTRACT_HAVE_POSIX_SPAWNP \
74                          || (INNOEXTRACT_HAVE_FORK && INNOEXTRACT_HAVE_EXECVP))
format_command_line(const char * const args[])75 static std::string format_command_line(const char * const args[]) {
76 
77 	std::ostringstream oss;
78 
79 	for(size_t i = 0; args[i]; i++) {
80 		if(i != 0) {
81 			oss << ' ';
82 		}
83 		oss << '"';
84 		for(const char * arg = args[i]; *arg; arg++) {
85 			char c = *arg;
86 			if(c == '\\' || c == '\"' || c == ' ' || c == '\'' || c == '$' || c == '!') {
87 				oss << '\\';
88 			}
89 			oss << c;
90 		}
91 		oss << '"';
92 	}
93 
94 	return oss.str();
95 }
96 #endif
97 
run(const char * const args[])98 int run(const char * const args[]) {
99 
100 	std::cout.flush();
101 	std::cerr.flush();
102 
103 #if defined(_WIN32)
104 
105 	// Format the command line arguments
106 	std::string exe;
107 	wtf8_to_utf16le(args[0], exe);
108 	exe.push_back('\0');
109 	std::string cmdline;
110 	wtf8_to_utf16le(format_command_line(args + 1), exe);
111 	cmdline.push_back('\0');
112 
113 	STARTUPINFO si;
114 	memset(&si, 0, sizeof(STARTUPINFO));
115 	si.cb = sizeof(STARTUPINFO);
116 
117 	PROCESS_INFORMATION pi;
118 	memset(&pi, 0, sizeof(PROCESS_INFORMATION));
119 
120 	bool success = (CreateProcessW(reinterpret_cast<LPCWSTR>(exe.c_str()),
121 	                               reinterpret_cast<LPWSTR>(&cmdline[0]), 0, 0, 0, 0, 0, 0, &si, &pi) != 0);
122 
123 	if(!success) {
124 		return -1; // Could not start process
125 	}
126 
127 	int status = int(WaitForSingleObject(pi.hProcess, INFINITE));
128 
129 	CloseHandle(pi.hProcess);
130 	CloseHandle(pi.hThread);
131 
132 	return status;
133 
134 #elif INNOEXTRACT_HAVE_POSIX_SPAWNP || (INNOEXTRACT_HAVE_FORK && INNOEXTRACT_HAVE_EXECVP)
135 
136 	char ** argv = const_cast<char **>(args);
137 
138 	pid_t pid = -1;
139 
140 	#if INNOEXTRACT_HAVE_POSIX_SPAWNP
141 
142 	// Fast POSIX implementation: posix_spawnp avoids unnecessary vm copies
143 
144 	// Run the executable in a new process
145 	(void)posix_spawnp(&pid, argv[0], NULL, NULL, argv, environ);
146 
147 	#else
148 
149 	// Compatibility POSIX implementation
150 
151 	// Start a new process
152 	pid = fork();
153 	if(pid == 0) {
154 
155 		// Run the executable
156 		(void)execvp(argv[0], argv);
157 
158 		exit(-1);
159 	}
160 
161 	#endif
162 
163 	if(pid < 0) {
164 		return -1;
165 	}
166 
167 	#if INNOEXTRACT_HAVE_WAITPID
168 	int status;
169 	(void)waitpid(pid, &status, 0);
170 	if(WIFEXITED(status) && WEXITSTATUS(status) < 127) {
171 		return WEXITSTATUS(status);
172 	} else if(WIFSIGNALED(status)) {
173 		return -WTERMSIG(status);
174 	} else {
175 		return -1;
176 	}
177 	#else
178 	# warning "Waiting for processes not supported on this system."
179 	#endif
180 
181 	return 0;
182 
183 #else
184 	return std::system(format_command_line(args).c_str());
185 #endif
186 
187 }
188 
189 } // namespace util
190