|
| 1 | +//===- test_not.cpp - Test for enviroment variables in the not tool -------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include <cstdlib> |
| 10 | +#include <iostream> |
| 11 | +#include <sstream> |
| 12 | + |
| 13 | +#ifdef _WIN32 |
| 14 | +#define WIN32_LEAN_AND_MEAN |
| 15 | +#define NOMINMAX |
| 16 | +#include <windows.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#if defined(__unix__) || defined(__APPLE__) |
| 20 | +#include <spawn.h> |
| 21 | +#include <sys/wait.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +int main(int argc, char* const* argv) { |
| 25 | + ++argv; |
| 26 | + --argc; |
| 27 | + if (argc == 0) |
| 28 | + return EXIT_FAILURE; |
| 29 | + |
| 30 | +#ifdef _WIN32 |
| 31 | + SetEnvironmentVariableA("SET_IN_PARENT", "something"); |
| 32 | +#else |
| 33 | + setenv("SET_IN_PARENT", "something", 0); |
| 34 | +#endif |
| 35 | + |
| 36 | + int result; |
| 37 | + |
| 38 | +#if defined(__unix__) || defined(__APPLE__) |
| 39 | + pid_t pid; |
| 40 | + extern char** environ; |
| 41 | + if (posix_spawn(&pid, argv[0], NULL, NULL, argv, environ)) |
| 42 | + return EXIT_FAILURE; |
| 43 | + if (waitpid(pid, &result, WUNTRACED | WCONTINUED) == -1) |
| 44 | + return EXIT_FAILURE; |
| 45 | +#else |
| 46 | + std::stringstream ss; |
| 47 | + ss << argv[0]; |
| 48 | + for (int i = 1; i < argc; ++i) |
| 49 | + ss << " " << argv[i]; |
| 50 | + std::string cmd = ss.str(); |
| 51 | + result = std::system(cmd.c_str()); |
| 52 | +#endif |
| 53 | + |
| 54 | + int retcode = 0; |
| 55 | + int signal = 0; |
| 56 | + |
| 57 | +#ifdef _WIN32 |
| 58 | + // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka |
| 59 | + // unreachable, should be recognized as a crash. However, some binaries use |
| 60 | + // exit code 3 on non-crash failure paths, so only do this if we expect a |
| 61 | + // crash. |
| 62 | + if (errno) { |
| 63 | + // If the command interpreter was not found, errno will be set and 0 will |
| 64 | + // be returned. It is unlikely that this will happen in our use case, but |
| 65 | + // check anyway. |
| 66 | + retcode = 1; |
| 67 | + signal = 1; |
| 68 | + } else { |
| 69 | + // On Windows, result is the exit code, except for the special case above. |
| 70 | + retcode = result; |
| 71 | + signal = 0; |
| 72 | + } |
| 73 | +#elif defined(WIFEXITED) && defined(WEXITSTATUS) && defined(WIFSIGNALED) && \ |
| 74 | + defined(WTERMSIG) |
| 75 | + // On POSIX systems and Solaris, result is a composite value of the exit code |
| 76 | + // and, potentially, the signal that caused termination of the command. |
| 77 | + if (WIFEXITED(result)) |
| 78 | + retcode = WEXITSTATUS(result); |
| 79 | + if (WIFSIGNALED(result)) |
| 80 | + signal = WTERMSIG(result); |
| 81 | +#else |
| 82 | +#error "Unsupported system" |
| 83 | +#endif |
| 84 | + |
| 85 | + if (!signal && retcode == EXIT_SUCCESS) |
| 86 | + return EXIT_SUCCESS; |
| 87 | + return EXIT_FAILURE; |
| 88 | +} |
0 commit comments