14642e01fSmrg/**************************************************************
24642e01fSmrg *
34642e01fSmrg * Startup code for the Quartz Darwin X Server
435c4bbdfSmrg * Copyright (c) 2008-2012 Apple Inc. All rights reserved.
54642e01fSmrg * Copyright (c) 2001-2004 Torrey T. Lyons. All Rights Reserved.
64642e01fSmrg *
74642e01fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
84642e01fSmrg * copy of this software and associated documentation files (the "Software"),
94642e01fSmrg * to deal in the Software without restriction, including without limitation
104642e01fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
114642e01fSmrg * and/or sell copies of the Software, and to permit persons to whom the
124642e01fSmrg * Software is furnished to do so, subject to the following conditions:
134642e01fSmrg *
144642e01fSmrg * The above copyright notice and this permission notice shall be included in
154642e01fSmrg * all copies or substantial portions of the Software.
164642e01fSmrg *
174642e01fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
184642e01fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
194642e01fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
204642e01fSmrg * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
214642e01fSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
224642e01fSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
234642e01fSmrg * DEALINGS IN THE SOFTWARE.
244642e01fSmrg *
254642e01fSmrg * Except as contained in this notice, the name(s) of the above copyright
264642e01fSmrg * holders shall not be used in advertising or otherwise to promote the sale,
274642e01fSmrg * use or other dealings in this Software without prior written authorization.
284642e01fSmrg */
294642e01fSmrg
304642e01fSmrg#include "sanitizedCarbon.h"
314642e01fSmrg
324642e01fSmrg#ifdef HAVE_DIX_CONFIG_H
334642e01fSmrg#include <dix-config.h>
344642e01fSmrg#endif
354642e01fSmrg
364642e01fSmrg#include <fcntl.h>
374642e01fSmrg#include <unistd.h>
384642e01fSmrg#include <CoreFoundation/CoreFoundation.h>
394642e01fSmrg#include "X11Controller.h"
404642e01fSmrg#include "darwin.h"
414642e01fSmrg#include "darwinEvents.h"
424642e01fSmrg#include "quartz.h"
434642e01fSmrg#include "opaque.h"
444642e01fSmrg#include "micmap.h"
454642e01fSmrg
464642e01fSmrg#include <assert.h>
474642e01fSmrg
484642e01fSmrg#include <pthread.h>
494642e01fSmrg
5035c4bbdfSmrgint
5135c4bbdfSmrgdix_main(int argc, char **argv, char **envp);
524642e01fSmrg
534642e01fSmrgstruct arg {
544642e01fSmrg    int argc;
554642e01fSmrg    char **argv;
564642e01fSmrg    char **envp;
574642e01fSmrg};
584642e01fSmrg
5935c4bbdfSmrg_X_NORETURN
6035c4bbdfSmrgstatic void
6135c4bbdfSmrgserver_thread(void *arg)
6235c4bbdfSmrg{
634642e01fSmrg    struct arg args = *((struct arg *)arg);
644642e01fSmrg    free(arg);
6535c4bbdfSmrg    exit(dix_main(args.argc, args.argv, args.envp));
664642e01fSmrg}
674642e01fSmrg
6835c4bbdfSmrgstatic pthread_t
6935c4bbdfSmrgcreate_thread(void *func, void *arg)
7035c4bbdfSmrg{
714642e01fSmrg    pthread_attr_t attr;
724642e01fSmrg    pthread_t tid;
7335c4bbdfSmrg
7435c4bbdfSmrg    pthread_attr_init(&attr);
7535c4bbdfSmrg    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
7635c4bbdfSmrg    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
7735c4bbdfSmrg    pthread_create(&tid, &attr, func, arg);
7835c4bbdfSmrg    pthread_attr_destroy(&attr);
7935c4bbdfSmrg
804642e01fSmrg    return tid;
814642e01fSmrg}
824642e01fSmrg
8335c4bbdfSmrgvoid
8435c4bbdfSmrgQuartzInitServer(int argc, char **argv, char **envp)
8535c4bbdfSmrg{
8635c4bbdfSmrg    struct arg *args = (struct arg *)malloc(sizeof(struct arg));
8735c4bbdfSmrg    if (!args)
884642e01fSmrg        FatalError("Could not allocate memory.\n");
8935c4bbdfSmrg
904642e01fSmrg    args->argc = argc;
914642e01fSmrg    args->argv = argv;
924642e01fSmrg    args->envp = envp;
934642e01fSmrg
9435c4bbdfSmrg    if (!create_thread(server_thread, args)) {
954642e01fSmrg        FatalError("can't create secondary thread\n");
964642e01fSmrg    }
971b5d61b8Smrg
981b5d61b8Smrg    /* Block signals on the AppKit thread that the X11 expects to handle on its thread */
991b5d61b8Smrg    sigset_t set;
1001b5d61b8Smrg    sigemptyset(&set);
1011b5d61b8Smrg    sigaddset(&set, SIGALRM);
1021b5d61b8Smrg#ifdef BUSFAULT
1031b5d61b8Smrg    sigaddset(&set, SIGBUS);
1041b5d61b8Smrg#endif
1051b5d61b8Smrg    pthread_sigmask(SIG_BLOCK, &set, NULL);
1064642e01fSmrg}
1074642e01fSmrg
10835c4bbdfSmrgint
10935c4bbdfSmrgserver_main(int argc, char **argv, char **envp)
11035c4bbdfSmrg{
11135c4bbdfSmrg    int i;
11235c4bbdfSmrg    int fd[2];
1134642e01fSmrg
1144642e01fSmrg    /* Unset CFProcessPath, so our children don't inherit this kludge we need
1154642e01fSmrg     * to load our nib.  If an xterm gets this set, then it fails to
1164642e01fSmrg     * 'open hi.txt' properly.
1174642e01fSmrg     */
1184642e01fSmrg    unsetenv("CFProcessPath");
11935c4bbdfSmrg
1204642e01fSmrg    // Make a pipe to pass events
12135c4bbdfSmrg    assert(pipe(fd) == 0);
1224642e01fSmrg    darwinEventReadFD = fd[0];
1234642e01fSmrg    darwinEventWriteFD = fd[1];
1244642e01fSmrg    fcntl(darwinEventReadFD, F_SETFL, O_NONBLOCK);
1254642e01fSmrg
1264642e01fSmrg    for (i = 1; i < argc; i++) {
1274642e01fSmrg        // Display version info without starting Mac OS X UI if requested
12835c4bbdfSmrg        if (!strcmp(argv[i],
12935c4bbdfSmrg                    "-showconfig") || !strcmp(argv[i], "-version")) {
1304642e01fSmrg            DarwinPrintBanner();
1314642e01fSmrg            exit(0);
1324642e01fSmrg        }
1334642e01fSmrg    }
1344642e01fSmrg
1354642e01fSmrg    X11ControllerMain(argc, argv, envp);
1364642e01fSmrg    exit(0);
1374642e01fSmrg}
138