16f02d4e9Smrg/* Copyright (c) 2011-2012 Apple Inc.
2c81d8f5eSmrg *
3c81d8f5eSmrg * Permission is hereby granted, free of charge, to any person
4c81d8f5eSmrg * obtaining a copy of this software and associated documentation files
5c81d8f5eSmrg * (the "Software"), to deal in the Software without restriction,
6c81d8f5eSmrg * including without limitation the rights to use, copy, modify, merge,
7c81d8f5eSmrg * publish, distribute, sublicense, and/or sell copies of the Software,
8c81d8f5eSmrg * and to permit persons to whom the Software is furnished to do so,
9c81d8f5eSmrg * subject to the following conditions:
10c81d8f5eSmrg *
11c81d8f5eSmrg * The above copyright notice and this permission notice shall be
12c81d8f5eSmrg * included in all copies or substantial portions of the Software.
13c81d8f5eSmrg *
14c81d8f5eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15c81d8f5eSmrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16c81d8f5eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17c81d8f5eSmrg * NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
18c81d8f5eSmrg * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19c81d8f5eSmrg * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20c81d8f5eSmrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21c81d8f5eSmrg * DEALINGS IN THE SOFTWARE.
22c81d8f5eSmrg *
23c81d8f5eSmrg * Except as contained in this notice, the name(s) of the above
24c81d8f5eSmrg * copyright holders shall not be used in advertising or otherwise to
25c81d8f5eSmrg * promote the sale, use or other dealings in this Software without
26c81d8f5eSmrg * prior written authorization.
27c81d8f5eSmrg */
28c81d8f5eSmrg
29c81d8f5eSmrg#ifdef HAVE_CONFIG_H
30c81d8f5eSmrg# include "config.h"
31c81d8f5eSmrg#endif
32c81d8f5eSmrg
33c81d8f5eSmrg#include <asl.h>
34c81d8f5eSmrg#include <unistd.h>
35c81d8f5eSmrg#include <stdio.h>
36c81d8f5eSmrg#include <assert.h>
37c81d8f5eSmrg#include <sys/wait.h>
38c81d8f5eSmrg#include <string.h>
39c81d8f5eSmrg#include <stdlib.h>
4021212451Smrg#include <spawn.h>
4121212451Smrg
42c81d8f5eSmrgint main(int argc, char **argv, char **envp) {
43c81d8f5eSmrg    aslclient aslc;
44c81d8f5eSmrg    pid_t child;
45c81d8f5eSmrg    int pstat;
46c81d8f5eSmrg
47c81d8f5eSmrg    if(argc < 2 || strcmp(argv[1], "--help") == 0) {
48c81d8f5eSmrg        fprintf(stderr, "Usage: %s prog [args...]\n", argv[0]);
49c81d8f5eSmrg        exit(EXIT_FAILURE);
50c81d8f5eSmrg    }
51c81d8f5eSmrg
52c81d8f5eSmrg    aslc = asl_open(BUNDLE_ID_PREFIX".startx", BUNDLE_ID_PREFIX, ASL_OPT_NO_DELAY);
53c81d8f5eSmrg
547c5adda3Smrg    asl_log_descriptor(aslc, NULL, ASL_LEVEL_INFO, STDOUT_FILENO, ASL_LOG_DESCRIPTOR_WRITE);
557c5adda3Smrg    asl_log_descriptor(aslc, NULL, ASL_LEVEL_NOTICE, STDERR_FILENO, ASL_LOG_DESCRIPTOR_WRITE);
567c5adda3Smrg
577c5adda3Smrg    /* https://github.com/XQuartz/XQuartz/issues/114 */
587c5adda3Smrg    char const * const home = getenv("HOME");
597c5adda3Smrg    assert(home);
607c5adda3Smrg    chdir(home);
61c81d8f5eSmrg
62c81d8f5eSmrg    assert(posix_spawnp(&child, argv[1], NULL, NULL, &argv[1], envp) == 0);
63c81d8f5eSmrg    wait4(child, &pstat, 0, (struct rusage *)0);
64c81d8f5eSmrg
65c81d8f5eSmrg    return pstat;
66c81d8f5eSmrg}
67