summaryrefslogtreecommitdiff
path: root/src/execute.cpp
blob: bbf9ceb77f14f284d116d21446728dc0aa08fff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (C) 2002 Andrew Tridgell
// Copyright (C) 2011-2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#include "execute.hpp"

#include "Config.hpp"
#include "Context.hpp"
#include "Fd.hpp"
#include "SignalHandler.hpp"
#include "Stat.hpp"
#include "Util.hpp"
#include "ccache.hpp"
#include "logging.hpp"

#ifdef _WIN32
#  include "win32compat.hpp"
#endif

using nonstd::string_view;

#ifdef _WIN32
int
execute(const char* const* argv, int fd_out, int fd_err, pid_t* /*pid*/)
{
  return win32execute(argv[0], argv, 1, fd_out, fd_err);
}

// Re-create a win32 command line string based on **argv.
// http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
char*
win32argvtos(const char* prefix, const char* const* argv, int* length)
{
  int i = 0;
  int k = 0;
  const char* arg = prefix ? prefix : argv[i++];
  do {
    int bs = 0;
    for (int j = 0; arg[j]; j++) {
      switch (arg[j]) {
      case '\\':
        bs++;
        break;
      case '"':
        bs = (bs << 1) + 1;
      // Fallthrough.
      default:
        k += bs + 1;
        bs = 0;
      }
    }
    k += (bs << 1) + 3;
  } while ((arg = argv[i++]));

  char* ptr = static_cast<char*>(malloc(k + 1));
  char* str = ptr;
  if (!str) {
    *length = 0;
    return NULL;
  }

  i = 0;
  arg = prefix ? prefix : argv[i++];
  do {
    int bs = 0;
    *ptr++ = '"';
    for (int j = 0; arg[j]; j++) {
      switch (arg[j]) {
      case '\\':
        bs++;
        break;
      // Fallthrough.
      case '"':
        bs = (bs << 1) + 1;
      // Fallthrough.
      default:
        while (bs && bs--) {
          *ptr++ = '\\';
        }
        *ptr++ = arg[j];
      }
    }
    bs <<= 1;
    while (bs && bs--) {
      *ptr++ = '\\';
    }
    *ptr++ = '"';
    *ptr++ = ' ';
  } while ((arg = argv[i++]));
  ptr[-1] = '\0';

  *length = ptr - str - 1;
  return str;
}

std::string
win32getshell(const char* path)
{
  const char* path_env = getenv("PATH");
  std::string sh;
  std::string ext = std::string(Util::get_extension(path));
  if (!ext.empty() && strcasecmp(ext.c_str(), ".sh") == 0 && path_env) {
    sh = find_executable_in_path("sh.exe", nullptr, path_env);
  }
  if (sh.empty() && getenv("CCACHE_DETECT_SHEBANG")) {
    // Detect shebang.
    File fp(path, "r");
    if (fp) {
      char buf[10] = {0};
      fgets(buf, sizeof(buf) - 1, fp.get());
      if (std::string(buf) == "#!/bin/sh" && path_env) {
        sh = find_executable_in_path("sh.exe", NULL, path_env);
      }
    }
  }

  return sh;
}

void
add_exe_ext_if_no_to_fullpath(char* full_path_win_ext,
                              size_t max_size,
                              const char* ext,
                              const char* path)
{
  if (!ext
      || (!str_eq(".exe", ext) && !str_eq(".sh", ext) && !str_eq(".bat", ext)
          && !str_eq(".EXE", ext) && !str_eq(".BAT", ext))) {
    snprintf(full_path_win_ext, max_size, "%s.exe", path);
  } else {
    snprintf(full_path_win_ext, max_size, "%s", path);
  }
}

int
win32execute(const char* path,
             const char* const* argv,
             int doreturn,
             int fd_stdout,
             int fd_stderr)
{
  PROCESS_INFORMATION pi;
  memset(&pi, 0x00, sizeof(pi));

  STARTUPINFO si;
  memset(&si, 0x00, sizeof(si));

  std::string sh = win32getshell(path);
  if (!sh.empty()) {
    path = sh.c_str();
  }

  si.cb = sizeof(STARTUPINFO);
  if (fd_stdout != -1) {
    si.hStdOutput = (HANDLE)_get_osfhandle(fd_stdout);
    si.hStdError = (HANDLE)_get_osfhandle(fd_stderr);
    si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    si.dwFlags = STARTF_USESTDHANDLES;
    if (si.hStdOutput == INVALID_HANDLE_VALUE
        || si.hStdError == INVALID_HANDLE_VALUE) {
      return -1;
    }
  } else {
    // Redirect subprocess stdout, stderr into current process.
    si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
    si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
    si.dwFlags = STARTF_USESTDHANDLES;
    if (si.hStdOutput == INVALID_HANDLE_VALUE
        || si.hStdError == INVALID_HANDLE_VALUE) {
      return -1;
    }
  }

  int length;
  const char* prefix = sh.empty() ? nullptr : sh.c_str();
  char* args = win32argvtos(prefix, argv, &length);
  const char* ext = strrchr(path, '.');
  char full_path_win_ext[MAX_PATH] = {0};
  add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext, path);
  BOOL ret = FALSE;
  if (length > 8192) {
    auto fd_and_path = Util::create_temp_fd(path);
    Fd fd(fd_and_path.first);
    const char* tmp_file = fd_and_path.second.c_str();
    if (!write_fd(*fd, args, length)) {
      cc_log("Error writing @file; this command will probably fail: %s", args);
    }
    std::string atfile = fmt::format("\"@{}\"", tmp_file);
    ret = CreateProcess(nullptr,
                        const_cast<char*>(atfile.c_str()),
                        nullptr,
                        nullptr,
                        1,
                        0,
                        nullptr,
                        nullptr,
                        &si,
                        &pi);
    Util::unlink_tmp(tmp_file);
  }
  if (!ret) {
    ret = CreateProcess(full_path_win_ext,
                        args,
                        nullptr,
                        nullptr,
                        1,
                        0,
                        nullptr,
                        nullptr,
                        &si,
                        &pi);
  }
  if (fd_stdout != -1) {
    close(fd_stdout);
    close(fd_stderr);
  }
  free(args);
  if (ret == 0) {
    DWORD error = GetLastError();
    std::string error_message = win32_error_message(error);
    cc_log("failed to execute %s: %s (%lu)",
           full_path_win_ext,
           win32_error_message(error).c_str(),
           error);
    return -1;
  }
  WaitForSingleObject(pi.hProcess, INFINITE);

  DWORD exitcode;
  GetExitCodeProcess(pi.hProcess, &exitcode);
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  if (!doreturn) {
    x_exit(exitcode);
  }
  return exitcode;
}

#else

// Execute a compiler backend, capturing all output to the given paths the full
// path to the compiler to run is in argv[0].
int
execute(const char* const* argv, int fd_out, int fd_err, pid_t* pid)
{
  cc_log_argv("Executing ", argv);

  {
    SignalHandlerBlocker signal_handler_blocker;
    *pid = fork();
  }

  if (*pid == -1) {
    fatal("Failed to fork: %s", strerror(errno));
  }

  if (*pid == 0) {
    // Child.
    dup2(fd_out, STDOUT_FILENO);
    close(fd_out);
    dup2(fd_err, STDERR_FILENO);
    close(fd_err);
    x_exit(execv(argv[0], const_cast<char* const*>(argv)));
  }

  close(fd_out);
  close(fd_err);

  int status;
  if (waitpid(*pid, &status, 0) != *pid) {
    fatal("waitpid failed: %s", strerror(errno));
  }

  {
    SignalHandlerBlocker signal_handler_blocker;
    *pid = 0;
  }

  if (WEXITSTATUS(status) == 0 && WIFSIGNALED(status)) {
    return -1;
  }

  return WEXITSTATUS(status);
}
#endif

// Find an executable by name in $PATH. Exclude any that are links to
// exclude_name.
std::string
find_executable(const Context& ctx, const char* name, const char* exclude_name)
{
  if (Util::is_absolute_path(name)) {
    return name;
  }

  const char* path = ctx.config.path().c_str();
  if (str_eq(path, "")) {
    path = getenv("PATH");
  }
  if (!path) {
    cc_log("No PATH variable");
    return "";
  }

  return find_executable_in_path(name, exclude_name, path);
}

std::string
find_executable_in_path(const char* name,
                        const char* exclude_name,
                        const char* path)
{
  if (!path) {
    return {};
  }

  // Search the path looking for the first compiler of the right name that
  // isn't us.
  for (const std::string& dir : Util::split_into_strings(path, PATH_DELIM)) {
#ifdef _WIN32
    char namebuf[MAX_PATH];
    int ret =
      SearchPath(dir.c_str(), name, nullptr, sizeof(namebuf), namebuf, nullptr);
    if (!ret) {
      std::string exename = fmt::format("{}.exe", name);
      ret = SearchPath(dir.c_str(),
                       exename.c_str(),
                       nullptr,
                       sizeof(namebuf),
                       namebuf,
                       nullptr);
    }
    (void)exclude_name;
    if (ret) {
      return std::string(namebuf);
    }
#else
    assert(exclude_name);
    std::string fname = fmt::format("{}/{}", dir, name);
    auto st1 = Stat::lstat(fname);
    auto st2 = Stat::stat(fname);
    // Look for a normal executable file.
    if (st1 && st2 && st2.is_regular() && access(fname.c_str(), X_OK) == 0) {
      if (st1.is_symlink()) {
        std::string real_path = Util::real_path(fname, true);
        if (Util::base_name(real_path) == exclude_name) {
          // It's a link to "ccache"!
          continue;
        }
      }

      // Found it!
      return fname;
    }
#endif
  }

  return "";
}