|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +#include <cstdio> |
| 5 | +#include <cstdlib> |
| 6 | +#include <errno.h> |
| 7 | +#include <signal.h> |
| 8 | + |
| 9 | +#ifdef TARGET_WINDOWS |
| 10 | + |
| 11 | +#include <windows.h> |
| 12 | +#include <string> |
| 13 | + |
| 14 | +#else // !TARGET_WINDOWS |
| 15 | + |
| 16 | +#include <chrono> |
| 17 | +#include <sys/wait.h> |
| 18 | +#include <thread> |
| 19 | +#include <unistd.h> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#endif // TARGET_WINDOWS |
| 23 | + |
| 24 | +int run_timed_process(const long, const int, const char *[]); |
| 25 | + |
| 26 | +#ifdef TARGET_X86 |
| 27 | +int __cdecl main(const int argc, const char *argv[]) |
| 28 | +#else |
| 29 | +int main(const int argc, const char *argv[]) |
| 30 | +#endif |
| 31 | +{ |
| 32 | + if (argc < 3) |
| 33 | + { |
| 34 | + printf("There are missing arguments. Got %d instead of 3+ :(\n", argc); |
| 35 | + return EXIT_FAILURE; |
| 36 | + } |
| 37 | + |
| 38 | + const long timeout_sec = strtol(argv[1], nullptr, 10); |
| 39 | + int exit_code = run_timed_process(timeout_sec * 1000L, argc-2, &argv[2]); |
| 40 | + |
| 41 | + printf("App Exit Code: %d\n", exit_code); |
| 42 | + return exit_code; |
| 43 | +} |
| 44 | + |
| 45 | +int run_timed_process(const long timeout_ms, const int proc_argc, const char *proc_argv[]) |
| 46 | +{ |
| 47 | +#ifdef TARGET_WINDOWS |
| 48 | + std::string cmdline(proc_argv[0]); |
| 49 | + |
| 50 | + for (int i = 1; i < proc_argc; i++) |
| 51 | + { |
| 52 | + cmdline.append(" "); |
| 53 | + cmdline.append(proc_argv[i]); |
| 54 | + } |
| 55 | + |
| 56 | + STARTUPINFOA startup_info; |
| 57 | + PROCESS_INFORMATION proc_info; |
| 58 | + unsigned long exit_code; |
| 59 | + |
| 60 | + ZeroMemory(&startup_info, sizeof(startup_info)); |
| 61 | + startup_info.cb = sizeof(startup_info); |
| 62 | + ZeroMemory(&proc_info, sizeof(proc_info)); |
| 63 | + |
| 64 | + if (!CreateProcessA(NULL, &cmdline[0], NULL, NULL, FALSE, 0, NULL, NULL, |
| 65 | + &startup_info, &proc_info)) |
| 66 | + { |
| 67 | + int error_code = GetLastError(); |
| 68 | + printf("Process creation failed... Code %d.\n", error_code); |
| 69 | + return error_code; |
| 70 | + } |
| 71 | + |
| 72 | + WaitForSingleObject(proc_info.hProcess, timeout_ms); |
| 73 | + GetExitCodeProcess(proc_info.hProcess, &exit_code); |
| 74 | + |
| 75 | + CloseHandle(proc_info.hProcess); |
| 76 | + CloseHandle(proc_info.hThread); |
| 77 | + return exit_code; |
| 78 | + |
| 79 | +#else // !TARGET_WINDOWS |
| 80 | + |
| 81 | + const int check_interval_ms = 25; |
| 82 | + int check_count = 0; |
| 83 | + std::vector<const char*> args; |
| 84 | + |
| 85 | + pid_t child_pid; |
| 86 | + int child_status; |
| 87 | + int wait_code; |
| 88 | + |
| 89 | + for (int i = 0; i < proc_argc; i++) |
| 90 | + { |
| 91 | + args.push_back(proc_argv[i]); |
| 92 | + } |
| 93 | + args.push_back(NULL); |
| 94 | + |
| 95 | + child_pid = fork(); |
| 96 | + |
| 97 | + if (child_pid < 0) |
| 98 | + { |
| 99 | + // Fork failed. No memory remaining available :( |
| 100 | + printf("Fork failed... Returning ENOMEM.\n"); |
| 101 | + return ENOMEM; |
| 102 | + } |
| 103 | + else if (child_pid == 0) |
| 104 | + { |
| 105 | + // Instructions for child process! |
| 106 | + execv(args[0], const_cast<char* const*>(args.data())); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + do |
| 111 | + { |
| 112 | + // Instructions for the parent process! |
| 113 | + wait_code = waitpid(child_pid, &child_status, WNOHANG); |
| 114 | + |
| 115 | + if (wait_code == -1) |
| 116 | + return EINVAL; |
| 117 | + |
| 118 | + std::this_thread::sleep_for(std::chrono::milliseconds(check_interval_ms)); |
| 119 | + |
| 120 | + if (wait_code) |
| 121 | + { |
| 122 | + if (WIFEXITED(child_status)) |
| 123 | + return WEXITSTATUS(child_status); |
| 124 | + } |
| 125 | + check_count++; |
| 126 | + |
| 127 | + } while (check_count < (timeout_ms / check_interval_ms)); |
| 128 | + } |
| 129 | + |
| 130 | + printf("Child process took too long. Timed out... Exiting...\n"); |
| 131 | + kill(child_pid, SIGKILL); |
| 132 | + |
| 133 | +#endif // TARGET_WINDOWS |
| 134 | + return ETIMEDOUT; |
| 135 | +} |
| 136 | + |
0 commit comments