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