1*4882a593Smuzhiyun /* Copyright (c) 2008-2012 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 <CoreServices/CoreServices.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
32*4882a593Smuzhiyun #include <dix-config.h>
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include <string.h>
36*4882a593Smuzhiyun #include <unistd.h>
37*4882a593Smuzhiyun #include <errno.h>
38*4882a593Smuzhiyun #include <asl.h>
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include <sys/socket.h>
41*4882a593Smuzhiyun #include <sys/un.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #define kX11AppBundleId BUNDLE_ID_PREFIX ".X11"
44*4882a593Smuzhiyun #define kX11AppBundlePath "/Contents/MacOS/X11"
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #include <mach/mach.h>
47*4882a593Smuzhiyun #include <mach/mach_error.h>
48*4882a593Smuzhiyun #include <servers/bootstrap.h>
49*4882a593Smuzhiyun #include "mach_startup.h"
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #include <signal.h>
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #include "launchd_fd.h"
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static char x11_path[PATH_MAX + 1];
56*4882a593Smuzhiyun static pid_t x11app_pid = 0;
57*4882a593Smuzhiyun aslclient aslc;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun static void
set_x11_path(void)60*4882a593Smuzhiyun set_x11_path(void)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun CFURLRef appURL = NULL;
63*4882a593Smuzhiyun OSStatus osstatus =
64*4882a593Smuzhiyun LSFindApplicationForInfo(kLSUnknownCreator, CFSTR(
65*4882a593Smuzhiyun kX11AppBundleId), nil, nil, &appURL);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun switch (osstatus) {
68*4882a593Smuzhiyun case noErr:
69*4882a593Smuzhiyun if (appURL == NULL) {
70*4882a593Smuzhiyun asl_log(
71*4882a593Smuzhiyun aslc, NULL, ASL_LEVEL_ERR,
72*4882a593Smuzhiyun "Xquartz: Invalid response from LSFindApplicationForInfo(%s)",
73*4882a593Smuzhiyun kX11AppBundleId);
74*4882a593Smuzhiyun exit(1);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (!CFURLGetFileSystemRepresentation(appURL, true,
78*4882a593Smuzhiyun (unsigned char *)x11_path,
79*4882a593Smuzhiyun sizeof(x11_path))) {
80*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
81*4882a593Smuzhiyun "Xquartz: Error resolving URL for %s",
82*4882a593Smuzhiyun kX11AppBundleId);
83*4882a593Smuzhiyun exit(3);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun strlcat(x11_path, kX11AppBundlePath, sizeof(x11_path));
87*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_INFO, "Xquartz: X11.app = %s", x11_path);
88*4882a593Smuzhiyun break;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun case kLSApplicationNotFoundErr:
91*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
92*4882a593Smuzhiyun "Xquartz: Unable to find application for %s",
93*4882a593Smuzhiyun kX11AppBundleId);
94*4882a593Smuzhiyun exit(10);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun default:
97*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
98*4882a593Smuzhiyun "Xquartz: Unable to find application for %s, error code = %d",
99*4882a593Smuzhiyun kX11AppBundleId,
100*4882a593Smuzhiyun (int)osstatus);
101*4882a593Smuzhiyun exit(11);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun static int
connect_to_socket(const char * filename)106*4882a593Smuzhiyun connect_to_socket(const char *filename)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun struct sockaddr_un servaddr_un;
109*4882a593Smuzhiyun struct sockaddr *servaddr;
110*4882a593Smuzhiyun socklen_t servaddr_len;
111*4882a593Smuzhiyun int ret_fd;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun /* Setup servaddr_un */
114*4882a593Smuzhiyun memset(&servaddr_un, 0, sizeof(struct sockaddr_un));
115*4882a593Smuzhiyun servaddr_un.sun_family = AF_UNIX;
116*4882a593Smuzhiyun strlcpy(servaddr_un.sun_path, filename, sizeof(servaddr_un.sun_path));
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun servaddr = (struct sockaddr *)&servaddr_un;
119*4882a593Smuzhiyun servaddr_len = sizeof(struct sockaddr_un) -
120*4882a593Smuzhiyun sizeof(servaddr_un.sun_path) + strlen(filename);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun ret_fd = socket(PF_UNIX, SOCK_STREAM, 0);
123*4882a593Smuzhiyun if (ret_fd == -1) {
124*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
125*4882a593Smuzhiyun "Xquartz: Failed to create socket: %s - %s", filename,
126*4882a593Smuzhiyun strerror(
127*4882a593Smuzhiyun errno));
128*4882a593Smuzhiyun return -1;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (connect(ret_fd, servaddr, servaddr_len) < 0) {
132*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
133*4882a593Smuzhiyun "Xquartz: Failed to connect to socket: %s - %d - %s",
134*4882a593Smuzhiyun filename, errno,
135*4882a593Smuzhiyun strerror(
136*4882a593Smuzhiyun errno));
137*4882a593Smuzhiyun close(ret_fd);
138*4882a593Smuzhiyun return -1;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun return ret_fd;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun static void
send_fd_handoff(int connected_fd,int launchd_fd)145*4882a593Smuzhiyun send_fd_handoff(int connected_fd, int launchd_fd)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun char databuf[] = "display";
148*4882a593Smuzhiyun struct iovec iov[1];
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun union {
151*4882a593Smuzhiyun struct cmsghdr hdr;
152*4882a593Smuzhiyun char bytes[CMSG_SPACE(sizeof(int))];
153*4882a593Smuzhiyun } buf;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun struct msghdr msg;
156*4882a593Smuzhiyun struct cmsghdr *cmsg;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun iov[0].iov_base = databuf;
159*4882a593Smuzhiyun iov[0].iov_len = sizeof(databuf);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun msg.msg_iov = iov;
162*4882a593Smuzhiyun msg.msg_iovlen = 1;
163*4882a593Smuzhiyun msg.msg_control = buf.bytes;
164*4882a593Smuzhiyun msg.msg_controllen = sizeof(buf);
165*4882a593Smuzhiyun msg.msg_name = 0;
166*4882a593Smuzhiyun msg.msg_namelen = 0;
167*4882a593Smuzhiyun msg.msg_flags = 0;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun cmsg = CMSG_FIRSTHDR(&msg);
170*4882a593Smuzhiyun cmsg->cmsg_level = SOL_SOCKET;
171*4882a593Smuzhiyun cmsg->cmsg_type = SCM_RIGHTS;
172*4882a593Smuzhiyun cmsg->cmsg_len = CMSG_LEN(sizeof(int));
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun msg.msg_controllen = cmsg->cmsg_len;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun *((int *)CMSG_DATA(cmsg)) = launchd_fd;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if (sendmsg(connected_fd, &msg, 0) < 0) {
179*4882a593Smuzhiyun asl_log(
180*4882a593Smuzhiyun aslc, NULL, ASL_LEVEL_ERR,
181*4882a593Smuzhiyun "Xquartz: Error sending $DISPLAY file descriptor over fd %d: %d -- %s",
182*4882a593Smuzhiyun connected_fd, errno, strerror(errno));
183*4882a593Smuzhiyun return;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_DEBUG,
187*4882a593Smuzhiyun "Xquartz: Message sent. Closing handoff fd.");
188*4882a593Smuzhiyun close(connected_fd);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun __attribute__((__noreturn__))
192*4882a593Smuzhiyun static void
signal_handler(int sig)193*4882a593Smuzhiyun signal_handler(int sig)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun if (x11app_pid)
196*4882a593Smuzhiyun kill(x11app_pid, sig);
197*4882a593Smuzhiyun _exit(0);
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun int
main(int argc,char ** argv,char ** envp)201*4882a593Smuzhiyun main(int argc, char **argv, char **envp)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun int envpc;
204*4882a593Smuzhiyun kern_return_t kr;
205*4882a593Smuzhiyun mach_port_t mp;
206*4882a593Smuzhiyun string_array_t newenvp;
207*4882a593Smuzhiyun string_array_t newargv;
208*4882a593Smuzhiyun size_t i;
209*4882a593Smuzhiyun int launchd_fd;
210*4882a593Smuzhiyun string_t handoff_socket_filename;
211*4882a593Smuzhiyun sig_t handler;
212*4882a593Smuzhiyun char *asl_sender;
213*4882a593Smuzhiyun char *asl_facility;
214*4882a593Smuzhiyun char *server_bootstrap_name = kX11AppBundleId;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun if (getenv("X11_PREFS_DOMAIN"))
217*4882a593Smuzhiyun server_bootstrap_name = getenv("X11_PREFS_DOMAIN");
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun asprintf(&asl_sender, "%s.stub", server_bootstrap_name);
220*4882a593Smuzhiyun assert(asl_sender);
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun asl_facility = strdup(server_bootstrap_name);
223*4882a593Smuzhiyun assert(asl_facility);
224*4882a593Smuzhiyun if (strcmp(asl_facility + strlen(asl_facility) - 4, ".X11") == 0)
225*4882a593Smuzhiyun asl_facility[strlen(asl_facility) - 4] = '\0';
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun assert(aslc = asl_open(asl_sender, asl_facility, ASL_OPT_NO_DELAY));
228*4882a593Smuzhiyun free(asl_sender);
229*4882a593Smuzhiyun free(asl_facility);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* We don't have a mechanism in place to handle this interrupt driven
232*4882a593Smuzhiyun * server-start notification, so just send the signal now, so xinit doesn't
233*4882a593Smuzhiyun * time out waiting for it and will just poll for the server.
234*4882a593Smuzhiyun */
235*4882a593Smuzhiyun handler = signal(SIGUSR1, SIG_IGN);
236*4882a593Smuzhiyun if (handler == SIG_IGN)
237*4882a593Smuzhiyun kill(getppid(), SIGUSR1);
238*4882a593Smuzhiyun signal(SIGUSR1, handler);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /* Pass on SIGs to X11.app */
241*4882a593Smuzhiyun signal(SIGINT, signal_handler);
242*4882a593Smuzhiyun signal(SIGTERM, signal_handler);
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun /* Get the $DISPLAY FD */
245*4882a593Smuzhiyun launchd_fd = launchd_display_fd();
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun kr = bootstrap_look_up(bootstrap_port, server_bootstrap_name, &mp);
248*4882a593Smuzhiyun if (kr != KERN_SUCCESS) {
249*4882a593Smuzhiyun pid_t child;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_WARNING,
252*4882a593Smuzhiyun "Xquartz: Unable to locate waiting server: %s",
253*4882a593Smuzhiyun server_bootstrap_name);
254*4882a593Smuzhiyun set_x11_path();
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* This forking is ugly and will be cleaned up later */
257*4882a593Smuzhiyun child = fork();
258*4882a593Smuzhiyun if (child == -1) {
259*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR, "Xquartz: Could not fork: %s",
260*4882a593Smuzhiyun strerror(
261*4882a593Smuzhiyun errno));
262*4882a593Smuzhiyun return EXIT_FAILURE;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun if (child == 0) {
266*4882a593Smuzhiyun char *_argv[3];
267*4882a593Smuzhiyun _argv[0] = x11_path;
268*4882a593Smuzhiyun _argv[1] = "--listenonly";
269*4882a593Smuzhiyun _argv[2] = NULL;
270*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_NOTICE,
271*4882a593Smuzhiyun "Xquartz: Starting X server: %s --listenonly",
272*4882a593Smuzhiyun x11_path);
273*4882a593Smuzhiyun return execvp(x11_path, _argv);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /* Try connecting for 10 seconds */
277*4882a593Smuzhiyun for (i = 0; i < 80; i++) {
278*4882a593Smuzhiyun usleep(250000);
279*4882a593Smuzhiyun kr = bootstrap_look_up(bootstrap_port, server_bootstrap_name, &mp);
280*4882a593Smuzhiyun if (kr == KERN_SUCCESS)
281*4882a593Smuzhiyun break;
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun if (kr != KERN_SUCCESS) {
285*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
286*4882a593Smuzhiyun "Xquartz: bootstrap_look_up(): %s", bootstrap_strerror(
287*4882a593Smuzhiyun kr));
288*4882a593Smuzhiyun return EXIT_FAILURE;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /* Get X11.app's pid */
293*4882a593Smuzhiyun request_pid(mp, &x11app_pid);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* Handoff the $DISPLAY FD */
296*4882a593Smuzhiyun if (launchd_fd != -1) {
297*4882a593Smuzhiyun size_t try, try_max;
298*4882a593Smuzhiyun int handoff_fd = -1;
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun for (try = 0, try_max = 5; try < try_max; try++) {
301*4882a593Smuzhiyun if (request_fd_handoff_socket(mp,
302*4882a593Smuzhiyun handoff_socket_filename) !=
303*4882a593Smuzhiyun KERN_SUCCESS) {
304*4882a593Smuzhiyun asl_log(
305*4882a593Smuzhiyun aslc, NULL, ASL_LEVEL_INFO,
306*4882a593Smuzhiyun "Xquartz: Failed to request a socket from the server to send the $DISPLAY fd over (try %d of %d)",
307*4882a593Smuzhiyun (int)try + 1, (int)try_max);
308*4882a593Smuzhiyun continue;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun handoff_fd = connect_to_socket(handoff_socket_filename);
312*4882a593Smuzhiyun if (handoff_fd == -1) {
313*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
314*4882a593Smuzhiyun "Xquartz: Failed to connect to socket (try %d of %d)",
315*4882a593Smuzhiyun (int)try + 1,
316*4882a593Smuzhiyun (int)try_max);
317*4882a593Smuzhiyun continue;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun asl_log(
321*4882a593Smuzhiyun aslc, NULL, ASL_LEVEL_INFO,
322*4882a593Smuzhiyun "Xquartz: Handoff connection established (try %d of %d) on fd %d, \"%s\". Sending message.",
323*4882a593Smuzhiyun (int)try + 1, (int)try_max, handoff_fd,
324*4882a593Smuzhiyun handoff_socket_filename);
325*4882a593Smuzhiyun send_fd_handoff(handoff_fd, launchd_fd);
326*4882a593Smuzhiyun close(handoff_fd);
327*4882a593Smuzhiyun break;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /* Count envp */
332*4882a593Smuzhiyun for (envpc = 0; envp[envpc]; envpc++) ;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /* We have fixed-size string lengths due to limitations in IPC,
335*4882a593Smuzhiyun * so we need to copy our argv and envp.
336*4882a593Smuzhiyun */
337*4882a593Smuzhiyun newargv = (string_array_t)calloc((1 + argc), sizeof(string_t));
338*4882a593Smuzhiyun newenvp = (string_array_t)calloc((1 + envpc), sizeof(string_t));
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun if (!newargv || !newenvp) {
341*4882a593Smuzhiyun /* Silence the clang static analyzer */
342*4882a593Smuzhiyun free(newargv);
343*4882a593Smuzhiyun free(newenvp);
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR,
346*4882a593Smuzhiyun "Xquartz: Memory allocation failure");
347*4882a593Smuzhiyun return EXIT_FAILURE;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun for (i = 0; i < argc; i++) {
351*4882a593Smuzhiyun strlcpy(newargv[i], argv[i], STRING_T_SIZE);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun for (i = 0; i < envpc; i++) {
354*4882a593Smuzhiyun strlcpy(newenvp[i], envp[i], STRING_T_SIZE);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun kr = start_x11_server(mp, newargv, argc, newenvp, envpc);
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun free(newargv);
360*4882a593Smuzhiyun free(newenvp);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun if (kr != KERN_SUCCESS) {
363*4882a593Smuzhiyun asl_log(aslc, NULL, ASL_LEVEL_ERR, "Xquartz: start_x11_server: %s",
364*4882a593Smuzhiyun mach_error_string(
365*4882a593Smuzhiyun kr));
366*4882a593Smuzhiyun return EXIT_FAILURE;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun return EXIT_SUCCESS;
369*4882a593Smuzhiyun }
370