|
| 1 | +#include "sentry_process.h" |
| 2 | + |
| 3 | +#include "sentry_alloc.h" |
| 4 | +#include "sentry_logger.h" |
| 5 | +#include "sentry_string.h" |
| 6 | + |
| 7 | +#include <errno.h> |
| 8 | +#include <fcntl.h> |
| 9 | +#include <stdarg.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <string.h> |
| 12 | +#include <sys/types.h> |
| 13 | +#include <sys/wait.h> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | +static char ** |
| 17 | +argv_new(size_t len) |
| 18 | +{ |
| 19 | + char **argv = sentry_malloc((len + 1) * sizeof(char *)); |
| 20 | + if (argv) { |
| 21 | + argv[len] = NULL; |
| 22 | + } |
| 23 | + return argv; |
| 24 | +} |
| 25 | + |
| 26 | +static bool |
| 27 | +argv_set(char **argv, size_t index, const char *value) |
| 28 | +{ |
| 29 | + if (!argv || !value) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + argv[index] = sentry_malloc(strlen(value) + 1); |
| 34 | + if (!argv[index]) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + strcpy(argv[index], value); |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +static char * |
| 42 | +argv_to_string(char **argv) |
| 43 | +{ |
| 44 | + if (!argv) { |
| 45 | + return NULL; |
| 46 | + } |
| 47 | + |
| 48 | + size_t len = 0; |
| 49 | + for (int i = 0; argv[i]; i++) { |
| 50 | + len += sentry__guarded_strlen(argv[i]) + 1; |
| 51 | + } |
| 52 | + |
| 53 | + char *str = sentry_malloc(len); |
| 54 | + if (!str) { |
| 55 | + return NULL; |
| 56 | + } |
| 57 | + str[0] = '\0'; |
| 58 | + |
| 59 | + for (int i = 0; argv[i]; i++) { |
| 60 | + if (i > 0) { |
| 61 | + strcat(str, " "); |
| 62 | + } |
| 63 | + strcat(str, argv[i]); |
| 64 | + } |
| 65 | + return str; |
| 66 | +} |
| 67 | + |
| 68 | +static void |
| 69 | +argv_free(char **argv) |
| 70 | +{ |
| 71 | + if (!argv) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + for (int i = 0; argv[i]; i++) { |
| 76 | + sentry_free(argv[i]); |
| 77 | + } |
| 78 | + sentry_free(argv); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Spawns a new fully detached subprocess by double-forking. |
| 83 | + */ |
| 84 | +static void |
| 85 | +spawn_process(char **argv) |
| 86 | +{ |
| 87 | + pid_t pid1 = fork(); |
| 88 | + if (pid1 == -1) { |
| 89 | + SENTRY_ERRORF("first fork() failed: %s", strerror(errno)); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (pid1 == 0) { |
| 94 | + // first child process: create new session and process group to detach |
| 95 | + // from parent |
| 96 | + if (setsid() == -1) { |
| 97 | + SENTRY_ERRORF("setsid() failed: %s", strerror(errno)); |
| 98 | + _exit(1); |
| 99 | + } |
| 100 | + |
| 101 | + // second fork to ensure the process is not a session leader and cannot |
| 102 | + // acquire a controlling terminal |
| 103 | + pid_t pid2 = fork(); |
| 104 | + if (pid2 == -1) { |
| 105 | + SENTRY_ERRORF("second fork() failed: %s", strerror(errno)); |
| 106 | + _exit(1); |
| 107 | + } |
| 108 | + |
| 109 | + if (pid2 == 0) { |
| 110 | + // second child process: redirect stdin/out/err to /dev/null |
| 111 | + int dev_null = open("/dev/null", O_RDWR); |
| 112 | + if (dev_null != -1) { |
| 113 | + dup2(dev_null, STDIN_FILENO); |
| 114 | + dup2(dev_null, STDOUT_FILENO); |
| 115 | + dup2(dev_null, STDERR_FILENO); |
| 116 | + if (dev_null > STDERR_FILENO) { |
| 117 | + close(dev_null); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (strstr(argv[0], "/") != NULL) { |
| 122 | + execv(argv[0], argv); |
| 123 | + } else { |
| 124 | + execvp(argv[0], argv); |
| 125 | + } |
| 126 | + |
| 127 | + SENTRY_ERRORF("execv failed: %s", strerror(errno)); |
| 128 | + _exit(1); |
| 129 | + } else { |
| 130 | + // the first child exits immediately |
| 131 | + _exit(0); |
| 132 | + } |
| 133 | + } else { |
| 134 | + // parent process: wait for the first child to exit |
| 135 | + int status; |
| 136 | + if (waitpid(pid1, &status, 0) == -1) { |
| 137 | + SENTRY_ERRORF("waitpid() failed: %s", strerror(errno)); |
| 138 | + } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 139 | + SENTRY_ERRORF("child process failed with status %d", status); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +void |
| 145 | +sentry__process_spawn(const sentry_path_t *executable, const char *arg0, ...) |
| 146 | +{ |
| 147 | + if (!executable || !executable->path || strcmp(executable->path, "") == 0) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + int argc = 1; |
| 152 | +#ifdef SENTRY_PLATFORM_MACOS |
| 153 | + bool is_bundle = sentry__path_ends_with(executable, ".app"); |
| 154 | + if (is_bundle) { |
| 155 | + argc += 2; // /usr/bin/open -a <bundle> |
| 156 | + } |
| 157 | +#endif |
| 158 | + |
| 159 | + if (arg0) { |
| 160 | +#ifdef SENTRY_PLATFORM_MACOS |
| 161 | + if (is_bundle) { |
| 162 | + argc++; // --args |
| 163 | + } |
| 164 | +#endif |
| 165 | + argc++; |
| 166 | + va_list args; |
| 167 | + va_start(args, arg0); |
| 168 | + while (va_arg(args, const char *) != NULL) { |
| 169 | + argc++; |
| 170 | + } |
| 171 | + va_end(args); |
| 172 | + } |
| 173 | + |
| 174 | + int i = 0; |
| 175 | + char **argv = argv_new(argc); |
| 176 | + |
| 177 | +#ifdef SENTRY_PLATFORM_MACOS |
| 178 | + if (is_bundle |
| 179 | + && (!argv_set(argv, i++, "/usr/bin/open") |
| 180 | + || !argv_set(argv, i++, "-a"))) { |
| 181 | + argv_free(argv); |
| 182 | + return; |
| 183 | + } |
| 184 | +#endif |
| 185 | + |
| 186 | + if (!argv_set(argv, i++, executable->path)) { |
| 187 | + argv_free(argv); |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + if (arg0) { |
| 192 | +#ifdef SENTRY_PLATFORM_MACOS |
| 193 | + if (is_bundle && !argv_set(argv, i++, "--args")) { |
| 194 | + argv_free(argv); |
| 195 | + return; |
| 196 | + } |
| 197 | +#endif |
| 198 | + if (!argv_set(argv, i++, arg0)) { |
| 199 | + argv_free(argv); |
| 200 | + return; |
| 201 | + } |
| 202 | + va_list args; |
| 203 | + va_start(args, arg0); |
| 204 | + const char *argn; |
| 205 | + while ((argn = va_arg(args, const char *)) != NULL) { |
| 206 | + if (!argv_set(argv, i++, argn)) { |
| 207 | + va_end(args); |
| 208 | + argv_free(argv); |
| 209 | + return; |
| 210 | + } |
| 211 | + } |
| 212 | + va_end(args); |
| 213 | + } |
| 214 | + |
| 215 | + char *cli = argv_to_string(argv); |
| 216 | + SENTRY_DEBUGF("spawning %s", cli); |
| 217 | + sentry_free(cli); |
| 218 | + |
| 219 | + spawn_process(argv); |
| 220 | + argv_free(argv); |
| 221 | +} |
0 commit comments