1/* Copyright (c) 2021 Apple Inc.
2 *
3 * Permission is hereby granted, free of charge, to any person
4 * obtaining a copy of this software and associated documentation files
5 * (the "Software"), to deal in the Software without restriction,
6 * including without limitation the rights to use, copy, modify, merge,
7 * publish, distribute, sublicense, and/or sell copies of the Software,
8 * and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
18 * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Except as contained in this notice, the name(s) of the above
24 * copyright holders shall not be used in advertising or otherwise to
25 * promote the sale, use or other dealings in this Software without
26 * prior written authorization.
27 */
28
29#include <assert.h>
30#include <mach-o/dyld.h>
31#include <libgen.h>
32#include <spawn.h>
33#include <sys/syslimits.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37
38/* We wnt XQuartz.app to inherit a login shell environment.  This is handled by the X11.sh
39 * script which re-execs the main binary from a login shell environment.  However, recent
40 * versions of macOS require that the main executable of an app be Mach-O for full system
41 * fidelity.
42 *
43 * Failure to do so results in two problems:
44 *    1) bash is seen as the responsible executable for Security & Privacy, and the user doesn't
45 *       get prompted to allow filesystem access (https://github.com/XQuartz/XQuartz/issues/6).
46 *    2) The process is launched under Rosetta for compatability, which results in
47 *       the subsequent spawn of the real executable under Rosetta rather than natively.
48 *
49 * This trampoline provides the mach-o needed by LaunchServices and TCC to satisfy those
50 * needs and simply execs the startup script which then execs the main binary.
51 */
52
53static char *executable_path() {
54    uint32_t bufsize = PATH_MAX;
55    char *buf = calloc(1, bufsize);
56
57    if (_NSGetExecutablePath(buf, &bufsize) == -1) {
58        free(buf);
59        buf = calloc(1, bufsize);
60        assert(_NSGetExecutablePath(buf, &bufsize) == 0);
61    }
62
63    return buf;
64}
65
66int main(int argc, char **argv, char **envp) {
67    char const * const executable_directory = dirname(executable_path());
68    char *executable = NULL;
69
70    asprintf(&executable, "%s/X11.sh", executable_directory);
71    if (access(executable, X_OK) == -1) {
72        free(executable);
73        asprintf(&executable, "%s/X11", executable_directory);
74    }
75    assert(access(executable, X_OK) == 0);
76
77    argv[0] = executable;
78
79    posix_spawnattr_t attr;
80    assert(posix_spawnattr_init(&attr) == 0);
81    assert(posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETEXEC) == 0);
82
83    pid_t child_pid;
84    assert(posix_spawn(&child_pid, executable, NULL, &attr, argv, envp) == 0);
85
86    return EXIT_FAILURE;
87}
88