quartzStartup.c revision c8548ba8
1/**************************************************************
2 *
3 * Startup code for the Quartz Darwin X Server
4 * Copyright (c) 2008-2012 Apple Inc. All rights reserved.
5 * Copyright (c) 2001-2004 Torrey T. Lyons. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Except as contained in this notice, the name(s) of the above copyright
26 * holders shall not be used in advertising or otherwise to promote the sale,
27 * use or other dealings in this Software without prior written authorization.
28 */
29
30#include "sanitizedCarbon.h"
31
32#ifdef HAVE_DIX_CONFIG_H
33#include <dix-config.h>
34#endif
35
36#include <fcntl.h>
37#include <unistd.h>
38#include <CoreFoundation/CoreFoundation.h>
39#include "X11Controller.h"
40#include "darwin.h"
41#include "darwinEvents.h"
42#include "quartz.h"
43#include "opaque.h"
44#include "micmap.h"
45
46#include <assert.h>
47
48#include <pthread.h>
49
50int
51dix_main(int argc, char **argv, char **envp);
52
53struct arg {
54    int argc;
55    char **argv;
56    char **envp;
57};
58
59_X_NORETURN
60static void
61server_thread(void *arg)
62{
63    struct arg args = *((struct arg *)arg);
64    free(arg);
65    exit(dix_main(args.argc, args.argv, args.envp));
66}
67
68static pthread_t
69create_thread(void *func, void *arg)
70{
71    pthread_attr_t attr;
72    pthread_t tid;
73
74    pthread_attr_init(&attr);
75    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
76    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
77    pthread_create(&tid, &attr, func, arg);
78    pthread_attr_destroy(&attr);
79
80    return tid;
81}
82
83void
84QuartzInitServer(int argc, char **argv, char **envp)
85{
86    struct arg *args = (struct arg *)malloc(sizeof(struct arg));
87    if (!args)
88        FatalError("Could not allocate memory.\n");
89
90    args->argc = argc;
91    args->argv = argv;
92    args->envp = envp;
93
94    if (!create_thread(server_thread, args)) {
95        FatalError("can't create secondary thread\n");
96    }
97
98    /* Block signals on the AppKit thread that the X11 expects to handle on its thread */
99    sigset_t set;
100    sigemptyset(&set);
101    sigaddset(&set, SIGALRM);
102#ifdef BUSFAULT
103    sigaddset(&set, SIGBUS);
104#endif
105    pthread_sigmask(SIG_BLOCK, &set, NULL);
106}
107
108int
109server_main(int argc, char **argv, char **envp)
110{
111    int i;
112    int fd[2];
113
114    /* Unset CFProcessPath, so our children don't inherit this kludge we need
115     * to load our nib.  If an xterm gets this set, then it fails to
116     * 'open hi.txt' properly.
117     */
118    unsetenv("CFProcessPath");
119
120    // Make a pipe to pass events
121    assert(pipe(fd) == 0);
122    darwinEventReadFD = fd[0];
123    darwinEventWriteFD = fd[1];
124    fcntl(darwinEventReadFD, F_SETFL, O_NONBLOCK);
125
126    for (i = 1; i < argc; i++) {
127        // Display version info without starting Mac OS X UI if requested
128        if (!strcmp(argv[i],
129                    "-showconfig") || !strcmp(argv[i], "-version")) {
130            DarwinPrintBanner();
131            exit(0);
132        }
133    }
134
135    X11ControllerMain(argc, argv, envp);
136    exit(0);
137}
138