summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthieu Herrb <matthieu.herrb@laas.fr>2011-06-05 12:04:24 +0200
committerMatthieu Herrb <matthieu.herrb@laas.fr>2011-06-05 12:04:24 +0200
commit65af3e3c88947ebfea2efde7fb65838151cb942c (patch)
treeb9a88a597ede306dbbf6513d75ba6640f11de062
parent9ac7c65e82543b18cf1bef52a626c54d3de34d26 (diff)
parent8e6cc68283518bba2820ff0d919aee9d49e3eb59 (diff)
Merge remote-tracking branch 'origin/master' into obsd
-rw-r--r--.gitignore1
-rw-r--r--Makefile.am14
-rw-r--r--configure.ac19
-rw-r--r--launchd/Makefile.am3
-rw-r--r--launchd/console_redirect.c418
-rw-r--r--launchd/console_redirect.h44
-rw-r--r--launchd/privileged_startx/.gitignore (renamed from privileged_startx/.gitignore)2
-rwxr-xr-xlaunchd/privileged_startx/10-tmpdirs.cpp (renamed from privileged_startx/10-tmpdirs.cpp)15
-rwxr-xr-xlaunchd/privileged_startx/20-font_cache.cpp (renamed from privileged_startx/20-font_cache.cpp)0
-rw-r--r--launchd/privileged_startx/Makefile.am (renamed from privileged_startx/Makefile.am)14
-rw-r--r--launchd/privileged_startx/client.c (renamed from privileged_startx/client.c)4
-rw-r--r--launchd/privileged_startx/privileged_startx.c (renamed from privileged_startx/privileged_startx.c)4
-rw-r--r--launchd/privileged_startx/privileged_startx.defs (renamed from privileged_startx/privileged_startx.defs)0
-rw-r--r--launchd/privileged_startx/privileged_startx.plist.cpp (renamed from privileged_startx/org.x.privileged_startx.plist.cpp)4
-rw-r--r--launchd/privileged_startx/privileged_startx_types.h (renamed from privileged_startx/privileged_startx_types.h)2
-rw-r--r--launchd/privileged_startx/server.c (renamed from privileged_startx/server.c)38
-rw-r--r--launchd/user_startx/.gitignore2
-rw-r--r--launchd/user_startx/Makefile.am49
-rw-r--r--launchd/user_startx/launchd_startx.c64
-rw-r--r--launchd/user_startx/startx.plist.cpp (renamed from org.x.startx.plist.cpp)5
-rw-r--r--startx.cpp2
21 files changed, 659 insertions, 45 deletions
diff --git a/.gitignore b/.gitignore
index 31e265f..ab58382 100644
--- a/.gitignore
+++ b/.gitignore
@@ -37,7 +37,6 @@ Makefile.in
mdate-sh
missing
mkinstalldirs
-*.startx.plist
*.pc
py-compile
stamp-h?
diff --git a/Makefile.am b/Makefile.am
index 3a9ab3c..1d1e4c6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -60,26 +60,20 @@ CPP_FILES_FLAGS = \
-DSHELL_CMD=$(SHELL_CMD) $(STARTX_COOKIE_FLAGS) \
-D__libexecdir__="$(libexecdir)" \
-D__bindir__="$(bindir)" \
- -DLAUNCHD_ID_PREFIX="$(launchdidprefix)"
+ -DBUNDLE_ID_PREFIX="$(bundleidprefix)"
if LAUNCHD
-$(launchdidprefix).startx.plist.cpp: org.x.startx.plist.cpp
- cp $< $@
-
-launchagents_PRE = $(launchdidprefix).startx.plist.cpp
-launchagents_DATA = $(launchagents_PRE:plist.cpp=plist)
-
-SUBDIRS = privileged_startx
+SUBDIRS = launchd
endif
-DIST_SUBDIRS = privileged_startx
+DIST_SUBDIRS = launchd
xinitrc_DATA = xinitrc
MAINTAINERCLEANFILES = ChangeLog INSTALL
CLEANFILES = xinitrc startx $(appman_DATA) $(launchagents_DATA)
-EXTRA_DIST = xinitrc.cpp startx.cpp org.x.startx.plist.cpp $(appman_PRE) \
+EXTRA_DIST = xinitrc.cpp startx.cpp $(appman_PRE) \
autogen.sh
.PHONY: ChangeLog INSTALL
diff --git a/configure.ac b/configure.ac
index c5b1a2b..c8439b2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -116,9 +116,11 @@ AC_ARG_WITH(launchagents-dir, AS_HELP_STRING([--with-launchagents-dir=PATH], [
AC_ARG_WITH(launchdaemons-dir, AS_HELP_STRING([--with-launchdaemons-dir=PATH], [Path to launchd's LaunchDaemonss directory (default: /Library/LaunchDaemons)]),
[ launchdaemonsdir="${withval}" ],
[ launchdaemonsdir="/Library/LaunchDaemons" ])
-AC_ARG_WITH(launchd-id-prefix, AS_HELP_STRING([--with-launchd-id-prefix=PATH], [Prefix to use for launchd identifiers (default: org.x)]),
- [ launchdidprefix="${withval}" ],
- [ launchdidprefix="org.x" ])
+AC_ARG_WITH(launchd-id-prefix, AS_HELP_STRING([--with-launchd-id-prefix=PATH], [Deprecated: Use --with-bundle-id-prefix.]),
+ [ bundleidprefix="${withval}" ],
+ [ bundleidprefix="org.x" ])
+AC_ARG_WITH(bundle-id-prefix, AS_HELP_STRING([--with-bundle-id-prefix=PATH], [Prefix to use for bundle identifiers (default: org.x)]),
+ [ bundleidprefix="${withval}" ])
if test "x$LAUNCHD" = "xauto"; then
unset LAUNCHD
@@ -133,15 +135,18 @@ if test "x$LAUNCHD" = "xyes" ; then
TIGER_LAUNCHD=yes
;;
esac
+ AC_CHECK_FUNC(dispatch_async,
+ AC_DEFINE([HAVE_LIBDISPATCH], 1, [Define to 1 if you have the libdispatch (GCD) available]),
+ [])
else
launchagentsdir=""
launchdaemonsdir=""
fi
-AC_DEFINE_UNQUOTED(LAUNCHD_ID_PREFIX, "$launchdidprefix", [Prefix to use for launchd identifiers])
+AC_DEFINE_UNQUOTED(BUNDLE_ID_PREFIX, "$bundleidprefix", [Prefix to use for launchd identifiers])
AC_SUBST([launchagentsdir])
AC_SUBST([launchdaemonsdir])
-AC_SUBST([launchdidprefix])
+AC_SUBST([bundleidprefix])
AM_CONDITIONAL(LAUNCHD, [test "x$LAUNCHD" = "xyes"])
AM_CONDITIONAL(TIGER_LAUNCHD, [test "x$TIGER_LAUNCHD" = "xyes"])
@@ -215,5 +220,7 @@ AC_SUBST(WM)
AC_SUBST(XCONSOLE)
AC_CONFIG_FILES([Makefile])
-AC_CONFIG_FILES([privileged_startx/Makefile])
+AC_CONFIG_FILES([launchd/Makefile])
+AC_CONFIG_FILES([launchd/privileged_startx/Makefile])
+AC_CONFIG_FILES([launchd/user_startx/Makefile])
AC_OUTPUT
diff --git a/launchd/Makefile.am b/launchd/Makefile.am
new file mode 100644
index 0000000..f8781ed
--- /dev/null
+++ b/launchd/Makefile.am
@@ -0,0 +1,3 @@
+SUBDIRS = privileged_startx user_startx
+
+EXTRA_DIST = console_redirect.h
diff --git a/launchd/console_redirect.c b/launchd/console_redirect.c
new file mode 100644
index 0000000..7ce3626
--- /dev/null
+++ b/launchd/console_redirect.c
@@ -0,0 +1,418 @@
+/* Copyright (c) 2011 Apple Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+ * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name(s) of the above
+ * copyright holders shall not be used in advertising or otherwise to
+ * promote the sale, use or other dealings in this Software without
+ * prior written authorization.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#else
+#define DEBUG_CONSOLE_REDIRECT 1
+#define HAVE_LIBDISPATCH 1
+#endif
+
+#include <assert.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/event.h>
+#include <asl.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "console_redirect.h"
+
+#define BUF_SIZE 512
+
+#ifdef HAVE_LIBDISPATCH
+#include <dispatch/dispatch.h>
+
+static dispatch_queue_t redirect_serial_q;
+static dispatch_group_t read_source_group;
+#else
+#include <pthread.h>
+
+static pthread_t redirect_pthread;
+static pthread_mutex_t redirect_fds_lock = PTHREAD_MUTEX_INITIALIZER;
+
+static int kq;
+
+/* Notifications to our reader thread */
+#define ASL_REDIRECT_TERMINATE ((void *)(uintptr_t)1)
+#endif
+
+typedef struct {
+ int level;
+ aslclient asl;
+ aslmsg msg;
+
+ /* Buffered reading */
+ char *buf;
+ char *w;
+
+#ifdef HAVE_LIBDISPATCH
+ dispatch_source_t read_source;
+#endif
+} asl_redirect;
+
+static asl_redirect *redirect_fds = NULL;
+static int n_redirect_fds = 0;
+
+/* Read from the FD until there is no more to read and redirect to ASL.
+ * Preconditions:
+ * 1: pthread_mutex_lock lock is held (pthreads) or called
+ * from the appropriate serial queue for operating on
+ * redirect_fds
+ * 2: fd corresponds to a valid entry in redirect_fds
+ *
+ * Return values:
+ * If the pipe is closed, EOF is returned regardless of how many bytes
+ * were processed. If the pipe is still open, the number of read bytes
+ * is returned.
+ */
+static inline int _read_redirect(int fd, int flush) {
+ int total_read = 0;
+ int nbytes;
+ asl_redirect *aslr = &redirect_fds[fd];
+
+ while((nbytes = read(fd, aslr->w, BUF_SIZE - (aslr->w - aslr->buf) - 1)) > 0) {
+ char *s, *p;
+
+ /* Increment our returned number read */
+ total_read += nbytes;
+
+ nbytes += (aslr->w - aslr->buf);
+ aslr->buf[nbytes] = '\0';
+
+ /* One line at a time */
+ for(p=aslr->buf; *p && (p - aslr->buf) < nbytes; p = s + 1) {
+ // Find null or \n
+ for(s=p; *s && *s != '\n'; s++);
+ if(*s == '\n') {
+ *s='\0';
+ asl_log(aslr->asl, aslr->msg, aslr->level, "%s", p);
+ } else if(aslr->buf != p) {
+ memmove(aslr->buf, p, BUF_SIZE - (p - aslr->buf));
+ aslr->w = aslr->buf + (s - p);
+ break;
+ } else if(nbytes == BUF_SIZE - 1) {
+ asl_log(aslr->asl, aslr->msg, aslr->level, "%s", p);
+ aslr->w = aslr->buf;
+ break;
+ }
+ }
+ }
+
+ /* Flush if requested or we're at EOF */
+ if(flush || nbytes == 0) {
+ if(aslr->w > aslr->buf) {
+ *aslr->w = '\0';
+ asl_log(aslr->asl, aslr->msg, aslr->level, "%s", aslr->buf);
+ }
+ }
+
+ if(nbytes == 0)
+ return EOF;
+ return total_read;
+}
+
+#ifdef HAVE_LIBDISPATCH
+static void read_from_source(void *_source) {
+ dispatch_source_t source = (dispatch_source_t)_source;
+ int fd = dispatch_source_get_handle(source);
+ if(_read_redirect(fd, 0) == EOF) {
+ dispatch_source_cancel(source);
+ }
+}
+
+static void cancel_source(void *_source) {
+ dispatch_source_t source = (dispatch_source_t)_source;
+ int fd = dispatch_source_get_handle(source);
+ asl_redirect *aslr = &redirect_fds[fd];
+
+ /* Flush the buffer */
+ _read_redirect(fd, 1);
+
+ close(fd);
+ free(aslr->buf);
+ memset(aslr, 0, sizeof(*aslr));
+ dispatch_release(source);
+ dispatch_group_leave(read_source_group);
+}
+
+#else /* !HAVE_LIBDISPATCH */
+static void *redirect_thread(void *ctx __unused) {
+ struct kevent ev;
+ int n;
+
+ while(1) {
+ n = kevent(kq, NULL, 0, &ev, 1, NULL);
+
+ /* Bail on errors */
+ if(n < 0) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "kevent failure: %s", strerror(errno));
+ break;
+ }
+
+ /* This should not happen */
+ if(n == 0)
+ continue;
+
+ switch(ev.filter) {
+ case EVFILT_READ:
+ pthread_mutex_lock(&redirect_fds_lock);
+ {
+ int fd = ev.ident;
+ int close_fd = 0;
+ asl_redirect *aslr = &redirect_fds[fd];
+
+ if(fd < 0 || fd >= n_redirect_fds || aslr->buf == NULL) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "Unexpected file descriptor: %d", fd);
+ goto next;
+ }
+
+ if(ev.flags & EV_EOF) {
+ close_fd = 1;
+ if(EOF != _read_redirect(fd, 1)) {
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "kevent reported EOF on %d, but read doesn't concur.", fd);
+ }
+ } else {
+ close_fd = (EOF == _read_redirect(fd, 0));
+ }
+
+ if(close_fd) {
+ EV_SET(&ev, fd, EVFILT_READ, EV_DELETE, 0, 0, 0);
+ kevent(kq, &ev, 1, NULL, 0, NULL);
+ close(fd);
+ free(aslr->buf);
+ memset(aslr, 0, sizeof(*aslr));
+ }
+ }
+ next:
+ pthread_mutex_unlock(&redirect_fds_lock);
+
+ case EVFILT_TIMER:
+ if(ev.udata == ASL_REDIRECT_TERMINATE)
+ return NULL;
+
+ default:
+ ;;
+ }
+ }
+
+ return NULL;
+}
+#endif
+
+static void redirect_atexit(void) {
+ /* stdout is linebuffered, so flush the buffer */
+ if(redirect_fds[STDOUT_FILENO].buf)
+ fflush(stdout);
+
+#ifdef HAVE_LIBDISPATCH
+ {
+ int i;
+
+ /* Cancel all of our dispatch sources, so they flush to ASL */
+ for(i=0; i < n_redirect_fds; i++)
+ if(redirect_fds[i].read_source)
+ dispatch_source_cancel(redirect_fds[i].read_source);
+
+ /* Wait at least three seconds for our sources to flush to ASL */
+ dispatch_group_wait(read_source_group, dispatch_time(DISPATCH_TIME_NOW, 3LL * NSEC_PER_SEC));
+ }
+#else
+ {
+ struct kevent ev;
+
+ /* Tell our reader thread it is time to pack up and go home */
+ EV_SET(&ev, 0, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 0, ASL_REDIRECT_TERMINATE);
+ kevent(kq, &ev, 1, NULL, 0, NULL);
+
+ pthread_join(redirect_pthread, NULL);
+ }
+#endif
+}
+
+#ifdef HAVE_LIBDISPATCH
+static void xi_asl_init(void *ctx __unused)
+#else
+static void xi_asl_init(void)
+#endif
+{
+ assert((redirect_fds = calloc(16, sizeof(*redirect_fds))) != NULL);
+ n_redirect_fds = 16;
+
+#ifdef HAVE_LIBDISPATCH
+ redirect_serial_q = dispatch_queue_create("com.apple.asl-redirect", NULL);
+ assert(redirect_serial_q != NULL);
+
+ read_source_group = dispatch_group_create();
+ assert(read_source_group != NULL);
+#else
+ assert((kq = kqueue()) != -1);
+ assert(pthread_create(&redirect_pthread, NULL, redirect_thread, NULL) == 0);
+#endif
+
+ atexit(redirect_atexit);
+}
+
+int xi_asl_log_fd(aslclient asl, aslmsg msg, int level, int fd) {
+#ifdef HAVE_LIBDISPATCH
+ int err __block = 0;
+ static dispatch_once_t once_control;
+ dispatch_once_f(&once_control, NULL, xi_asl_init);
+#else
+ int err = 0;
+ static pthread_once_t once_control = PTHREAD_ONCE_INIT;
+ assert(pthread_once(&once_control, xi_asl_init) == 0);
+#endif
+
+ if(fd < 0)
+ return EBADF;
+
+#ifdef HAVE_LIBDISPATCH
+#define BLOCK_DONE return
+ dispatch_sync(redirect_serial_q, ^
+#else
+#define BLOCK_DONE goto done
+ assert(pthread_mutex_lock(&redirect_fds_lock) == 0);
+#endif
+ {
+ /* Reallocate if we need more space */
+ if(fd >= n_redirect_fds) {
+ size_t new_n = 1 << (ffs(fd) + 1);
+ asl_redirect *new_array = realloc(redirect_fds, new_n * sizeof(*redirect_fds));
+ if(!new_array) {
+ err = errno;
+ BLOCK_DONE;
+ }
+ redirect_fds = new_array;
+ memset(redirect_fds + n_redirect_fds, 0, new_n - n_redirect_fds);
+ n_redirect_fds = new_n;
+ }
+
+ /* If we're already listening on it, return error. */
+ if(redirect_fds[fd].buf != NULL) {
+ err = EBADF;
+ BLOCK_DONE;
+ }
+
+ /* Initialize our buffer */
+ redirect_fds[fd].buf = (char *)malloc(BUF_SIZE);
+ if(redirect_fds[fd].buf == NULL) {
+ err = errno;
+ BLOCK_DONE;
+ }
+ redirect_fds[fd].w = redirect_fds[fd].buf;
+
+ /* Store our ASL settings */
+ redirect_fds[fd].level = level;
+ redirect_fds[fd].asl = asl;
+ redirect_fds[fd].msg = msg;
+
+ /* Don't block on reads from this fd */
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+
+ /* Start listening */
+#ifdef HAVE_LIBDISPATCH
+ {
+ dispatch_source_t read_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fd, 0, redirect_serial_q);
+ redirect_fds[fd].read_source = read_source;
+ dispatch_set_context(read_source, read_source);
+ dispatch_source_set_event_handler_f(read_source, read_from_source);
+ dispatch_source_set_cancel_handler_f(read_source, cancel_source);
+ dispatch_group_enter(read_source_group);
+ dispatch_resume(read_source);
+ }
+#else
+ {
+ struct kevent ev;
+ EV_SET(&ev, fd, EVFILT_READ, EV_ADD, 0, 0, 0);
+ kevent(kq, &ev, 1, NULL, 0, NULL);
+ }
+#endif
+ }
+#ifdef HAVE_LIBDISPATCH
+ );
+#else
+done:
+ assert(pthread_mutex_unlock(&redirect_fds_lock) == 0);
+#endif
+#undef BLOCK_DONE
+
+ return err;
+}
+
+int xi_asl_capture_fd(aslclient asl, aslmsg msg, int level, int fd) {
+ int pipepair[2];
+
+ /* Create pipe */
+ if(pipe(pipepair) == -1)
+ return errno;
+
+ /* Close the read fd but not the write fd on exec */
+ if(fcntl(pipepair[0], F_SETFD, FD_CLOEXEC) == -1)
+ return errno;
+
+ /* Replace the existing fd */
+ if(dup2(pipepair[1], fd) == -1) {
+ close(pipepair[0]);
+ close(pipepair[1]);
+ return errno;
+ }
+
+ /* If we capture STDOUT_FILENO, make sure we linebuffer stdout */
+ if(fd == STDOUT_FILENO)
+ setlinebuf(stdout);
+
+ /* Close the duplicate fds since they've been reassigned */
+ close(pipepair[1]);
+
+ /* Hand off the read end of our pipe to xi_asl_log_fd */
+ return xi_asl_log_fd(asl, msg, level, pipepair[0]);
+}
+
+#ifdef DEBUG_CONSOLE_REDIRECT
+int main(int argc __unused, char **argv __unused) {
+ xi_asl_capture_fd(NULL, NULL, ASL_LEVEL_NOTICE, STDOUT_FILENO);
+ xi_asl_capture_fd(NULL, NULL, ASL_LEVEL_ERR, STDERR_FILENO);
+
+ fprintf(stderr, "TEST ERR1\n");
+ fprintf(stdout, "TEST OUT1\n");
+ fprintf(stderr, "TEST ERR2\n");
+ fprintf(stdout, "TEST OUT2\n");
+ system("/bin/echo SYST OUT");
+ system("/bin/echo SYST ERR >&2");
+ fprintf(stdout, "TEST OUT3\n");
+ fprintf(stdout, "TEST OUT4\n");
+ fprintf(stderr, "TEST ERR3\n");
+ fprintf(stderr, "TEST ERR4\n");
+
+ exit(0);
+}
+#endif
diff --git a/launchd/console_redirect.h b/launchd/console_redirect.h
new file mode 100644
index 0000000..134def6
--- /dev/null
+++ b/launchd/console_redirect.h
@@ -0,0 +1,44 @@
+/* Copyright (c) 2011 Apple Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+ * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name(s) of the above
+ * copyright holders shall not be used in advertising or otherwise to
+ * promote the sale, use or other dealings in this Software without
+ * prior written authorization.
+ */
+
+#ifndef _XQUARTZ_CONSOLE_REDIRECT_H_
+#define _XQUARTZ_CONSOLE_REDIRECT_H_
+
+#include <asl.h>
+
+/* The given fd is replaced with a pipe. Anything written to it will will be
+ * logged to ASL.
+ */
+int xi_asl_capture_fd(aslclient asl, aslmsg msg, int level, int fd);
+
+/* The given fd is read from and passed along to ASL until all write ends of the
+ * pipe are closed. Once the last writer has closed the pipe, we close our end.
+ */
+int xi_asl_log_fd(aslclient asl, aslmsg msg, int level, int fd);
+
+#endif
diff --git a/privileged_startx/.gitignore b/launchd/privileged_startx/.gitignore
index 4bee5e8..c4a009d 100644
--- a/privileged_startx/.gitignore
+++ b/launchd/privileged_startx/.gitignore
@@ -1,6 +1,6 @@
10-tmpdirs
20-font_cache
-*.privileged_startx.plist
+*.privileged_startx.plist*
privileged_startx
privileged_startx.h
privileged_startxServer.c
diff --git a/privileged_startx/10-tmpdirs.cpp b/launchd/privileged_startx/10-tmpdirs.cpp
index e30abac..8012597 100755
--- a/privileged_startx/10-tmpdirs.cpp
+++ b/launchd/privileged_startx/10-tmpdirs.cpp
@@ -27,10 +27,19 @@ XCOMM promote the sale, use or other dealings in this Software without
XCOMM prior written authorization.
XCOMM Make sure these are owned by root
+
+XCOMM Our usage of mktemp fails with GNU, so prefer /usr/bin to hopefully
+XCOMM get BSD mktemp
+if [ -x /usr/bin/mktemp ] ; then
+ MKTEMP=/usr/bin/mktemp
+else
+ MKTEMP=mktemp
+fi
+
for dir in /tmp/.ICE-unix /tmp/.X11-unix /tmp/.font-unix ; do
- # Use mktemp rather than mkdir to avoid possible security issue
- # if $dir exists and is a symlink
- if mktemp -d ${dir} >& /dev/null ; then
+ XCOMM Use mktemp rather than mkdir to avoid possible security issue
+ XCOMM if $dir exists and is a symlink
+ if ${MKTEMP} -d ${dir} >& /dev/null ; then
chmod 1777 $dir
chown root:wheel $dir
fi
diff --git a/privileged_startx/20-font_cache.cpp b/launchd/privileged_startx/20-font_cache.cpp
index 6d43e10..6d43e10 100755
--- a/privileged_startx/20-font_cache.cpp
+++ b/launchd/privileged_startx/20-font_cache.cpp
diff --git a/privileged_startx/Makefile.am b/launchd/privileged_startx/Makefile.am
index 453f6e7..811cbdb 100644
--- a/privileged_startx/Makefile.am
+++ b/launchd/privileged_startx/Makefile.am
@@ -33,14 +33,15 @@ privstartxdir = $(xinitrcdir)/privileged_startx.d
xinitrc_PROGRAMS = privileged_startx
privstartx_SCRIPTS = 10-tmpdirs 20-font_cache
-AM_CPPFLAGS = -DXINITDIR=\"$(xinitrcdir)\" -DSCRIPTDIR=\"$(privstartxdir)\" -DBINDIR=\"$(bindir)\"
-CPP_FILES_FLAGS = -DXINITDIR="$(xinitrcdir)" -DSCRIPTDIR="$(privstartxdir)" -DBINDIR="$(bindir)" -DLAUNCHD_ID_PREFIX="$(launchdidprefix)"
+AM_CPPFLAGS = -I$(srcdir)/.. -DXINITDIR=\"$(xinitrcdir)\" -DSCRIPTDIR=\"$(privstartxdir)\" -DBINDIR=\"$(bindir)\"
+CPP_FILES_FLAGS = -DXINITDIR="$(xinitrcdir)" -DSCRIPTDIR="$(privstartxdir)" -DBINDIR="$(bindir)" -DBUNDLE_ID_PREFIX="$(bundleidprefix)"
if TIGER_LAUNCHD
CPP_FILES_FLAGS += -DTIGER_LAUNCHD
endif
dist_privileged_startx_SOURCES = \
+ $(srcdir)/../console_redirect.c \
server.c \
client.c \
privileged_startx.c
@@ -55,10 +56,10 @@ BUILT_SOURCES = \
privileged_startxServer.h \
privileged_startx.h
-$(launchdidprefix).privileged_startx.plist.cpp: org.x.privileged_startx.plist.cpp
+$(bundleidprefix).privileged_startx.plist.cpp: privileged_startx.plist.cpp
cp $< $@
-launchdaemons_PRE = $(launchdidprefix).privileged_startx.plist.cpp
+launchdaemons_PRE = $(bundleidprefix).privileged_startx.plist.cpp
launchdaemons_DATA = $(launchdaemons_PRE:plist.cpp=plist)
10-tmpdirs: 10-tmpdirs.cpp Makefile
@@ -67,12 +68,13 @@ launchdaemons_DATA = $(launchdaemons_PRE:plist.cpp=plist)
CLEANFILES = \
$(privstartx_SCRIPTS) \
$(BUILT_SOURCES) \
- $(launchdaemons_DATA)
+ $(launchdaemons_DATA) \
+ $(bundleidprefix).privileged_startx.plist.cpp
EXTRA_DIST = \
10-tmpdirs.cpp \
20-font_cache.cpp \
- org.x.privileged_startx.plist.cpp \
+ privileged_startx.plist.cpp \
privileged_startx.defs \
privileged_startx_types.h
diff --git a/privileged_startx/client.c b/launchd/privileged_startx/client.c
index 8e1a771..6c3a6d6 100644
--- a/privileged_startx/client.c
+++ b/launchd/privileged_startx/client.c
@@ -26,6 +26,10 @@
* prior written authorization.
*/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <servers/bootstrap.h>
diff --git a/privileged_startx/privileged_startx.c b/launchd/privileged_startx/privileged_startx.c
index 725a1b0..b71c30b 100644
--- a/privileged_startx/privileged_startx.c
+++ b/launchd/privileged_startx/privileged_startx.c
@@ -26,6 +26,10 @@
* prior written authorization.
*/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
#include <string.h>
#include <stdio.h>
diff --git a/privileged_startx/privileged_startx.defs b/launchd/privileged_startx/privileged_startx.defs
index 86f2b3a..86f2b3a 100644
--- a/privileged_startx/privileged_startx.defs
+++ b/launchd/privileged_startx/privileged_startx.defs
diff --git a/privileged_startx/org.x.privileged_startx.plist.cpp b/launchd/privileged_startx/privileged_startx.plist.cpp
index 02e1cce..533fc32 100644
--- a/privileged_startx/org.x.privileged_startx.plist.cpp
+++ b/launchd/privileged_startx/privileged_startx.plist.cpp
@@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>Label</key>
- <string>LAUNCHD_ID_PREFIX.privileged_startx</string>
+ <string>BUNDLE_ID_PREFIX.privileged_startx</string>
<key>ProgramArguments</key>
<array>
<string>XINITDIR/privileged_startx</string>
@@ -16,7 +16,7 @@
#else
<key>MachServices</key>
<dict>
- <key>LAUNCHD_ID_PREFIX.privileged_startx</key>
+ <key>BUNDLE_ID_PREFIX.privileged_startx</key>
<true/>
</dict>
<key>TimeOut</key>
diff --git a/privileged_startx/privileged_startx_types.h b/launchd/privileged_startx/privileged_startx_types.h
index 8928e64..a0d3439 100644
--- a/privileged_startx/privileged_startx_types.h
+++ b/launchd/privileged_startx/privileged_startx_types.h
@@ -5,6 +5,6 @@
#include <config.h>
#endif
-#define BOOTSTRAP_NAME LAUNCHD_ID_PREFIX".privileged_startx"
+#define BOOTSTRAP_NAME BUNDLE_ID_PREFIX".privileged_startx"
#endif
diff --git a/privileged_startx/server.c b/launchd/privileged_startx/server.c
index 4415943..cfbb623 100644
--- a/privileged_startx/server.c
+++ b/launchd/privileged_startx/server.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2008 Apple Inc.
+/* Copyright (c) 2008-2011 Apple Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
@@ -27,7 +27,7 @@
*/
#ifdef HAVE_CONFIG_H
-#include <config.h>
+# include "config.h"
#endif
#include <mach/mach.h>
@@ -46,6 +46,8 @@
#include <asl.h>
#include <errno.h>
+#include "console_redirect.h"
+
#include "privileged_startx.h"
#include "privileged_startxServer.h"
@@ -90,19 +92,19 @@ static mach_port_t checkin_or_register(char *bname) {
/* We probably were not started by launchd or the old mach_init */
kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
if (kr != KERN_SUCCESS) {
- fprintf(stderr, "mach_port_allocate(): %s\n", mach_error_string(kr));
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "mach_port_allocate(): %s", mach_error_string(kr));
exit(EXIT_FAILURE);
}
kr = mach_port_insert_right(mach_task_self(), mp, mp, MACH_MSG_TYPE_MAKE_SEND);
if (kr != KERN_SUCCESS) {
- fprintf(stderr, "mach_port_insert_right(): %s\n", mach_error_string(kr));
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "mach_port_insert_right(): %s", mach_error_string(kr));
exit(EXIT_FAILURE);
}
kr = bootstrap_register(bootstrap_port, bname, mp);
if (kr != KERN_SUCCESS) {
- fprintf(stderr, "bootstrap_register(): %s\n", mach_error_string(kr));
+ asl_log(NULL, NULL, ASL_LEVEL_ERR, "bootstrap_register(): %s", mach_error_string(kr));
exit(EXIT_FAILURE);
}
@@ -118,7 +120,10 @@ int server_main(const char *dir) {
long idle_timeout = DEFAULT_IDLE_TIMEOUT;
#endif
- launch_data_t config = NULL, checkin = NULL;
+ launch_data_t config = NULL, checkin = NULL, label = NULL;
+ const char *labelstr = BUNDLE_ID_PREFIX".privileged_startx";
+ aslclient aslc;
+
checkin = launch_data_new_string(LAUNCH_KEY_CHECKIN);
config = launch_msg(checkin);
if (!config || launch_data_get_type(config) == LAUNCH_DATA_ERRNO) {
@@ -132,6 +137,15 @@ int server_main(const char *dir) {
"script directory set: %s", script_dir);
}
+ label = launch_data_dict_lookup(config, LAUNCH_JOBKEY_LABEL);
+ if (label) {
+ labelstr = launch_data_get_string(label);
+ }
+
+ aslc = asl_open(labelstr, BUNDLE_ID_PREFIX, ASL_OPT_NO_DELAY);
+ xi_asl_capture_fd(aslc, NULL, ASL_LEVEL_INFO, STDOUT_FILENO);
+ xi_asl_capture_fd(aslc, NULL, ASL_LEVEL_NOTICE, STDERR_FILENO);
+
#ifdef LAUNCH_JOBKEY_MACHSERVICES
launch_data_t tmv;
tmv = launch_data_dict_lookup(config, LAUNCH_JOBKEY_TIMEOUT);
@@ -157,7 +171,7 @@ int server_main(const char *dir) {
mp = launch_data_get_machport(svc);
#else
- mp = checkin_or_register(LAUNCHD_ID_PREFIX".privileged_startx");
+ mp = checkin_or_register(BUNDLE_ID_PREFIX".privileged_startx");
#endif
if (mp == MACH_PORT_NULL) {
@@ -188,7 +202,7 @@ int server_main(const char *dir) {
kr = mach_msg_server(privileged_startx_server, mxmsgsz, mp, 0);
if (kr != KERN_SUCCESS) {
asl_log(NULL, NULL, ASL_LEVEL_ERR,
- "mach_msg_server(mp): %s\n", mach_error_string(kr));
+ "mach_msg_server(mp): %s", mach_error_string(kr));
exit(EXIT_FAILURE);
}
@@ -218,7 +232,7 @@ kern_return_t do_privileged_startx(mach_port_t test_port __attribute__((unused))
ftsp = fts_open((char * const *)path_argv, FTS_PHYSICAL, ftscmp);
if(!ftsp) {
asl_log(NULL, NULL, ASL_LEVEL_ERR,
- "do_privileged_startx: fts_open(%s): %s\n",
+ "do_privileged_startx: fts_open(%s): %s",
script_dir, strerror(errno));
return KERN_FAILURE;
}
@@ -227,7 +241,7 @@ kern_return_t do_privileged_startx(mach_port_t test_port __attribute__((unused))
ftsent = fts_read(ftsp);
if(!ftsent) {
asl_log(NULL, NULL, ASL_LEVEL_ERR,
- "do_privileged_startx: fts_read(): %s\n", strerror(errno));
+ "do_privileged_startx: fts_read(): %s", strerror(errno));
fts_close(ftsp);
return KERN_FAILURE;
}
@@ -236,7 +250,7 @@ kern_return_t do_privileged_startx(mach_port_t test_port __attribute__((unused))
ftsent = fts_children(ftsp, 0);
if(!ftsent) {
asl_log(NULL, NULL, ASL_LEVEL_ERR,
- "do_privileged_startx: fts_children(): %s\n", strerror(errno));
+ "do_privileged_startx: fts_children(): %s", strerror(errno));
fts_close(ftsp);
return KERN_FAILURE;
}
@@ -260,7 +274,7 @@ kern_return_t do_privileged_startx(mach_port_t test_port __attribute__((unused))
error_code = system(fn_buf);
if(error_code != 0) {
asl_log(NULL, NULL, ASL_LEVEL_ERR,
- "do_privileged_startx: %s: exited with status %d\n",
+ "do_privileged_startx: %s: exited with status %d",
fn_buf, error_code);
retval = KERN_FAILURE;
}
diff --git a/launchd/user_startx/.gitignore b/launchd/user_startx/.gitignore
new file mode 100644
index 0000000..b87dd68
--- /dev/null
+++ b/launchd/user_startx/.gitignore
@@ -0,0 +1,2 @@
+launchd_startx
+*.startx.plist*
diff --git a/launchd/user_startx/Makefile.am b/launchd/user_startx/Makefile.am
new file mode 100644
index 0000000..ce00e73
--- /dev/null
+++ b/launchd/user_startx/Makefile.am
@@ -0,0 +1,49 @@
+#
+# Copyright 2005 Red Hat, Inc.
+#
+# Permission to use, copy, modify, distribute, and sell this software and its
+# documentation for any purpose is hereby granted without fee, provided that
+# the above copyright notice appear in all copies and that both that
+# copyright notice and this permission notice appear in supporting
+# documentation, and that the name of Red Hat not be used in
+# advertising or publicity pertaining to distribution of the software without
+# specific, written prior permission. Red Hat makes no
+# representations about the suitability of this software for any purpose. It
+# is provided "as is" without express or implied warranty.
+#
+# RED HAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
+# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
+# EVENT SHALL RED HAT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
+# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
+# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+include $(top_srcdir)/cpprules.in
+
+xinitrcdir = $(XINITDIR)
+
+xinitrc_PROGRAMS = launchd_startx
+
+AM_CPPFLAGS = -I$(srcdir)/.. -DXINITDIR=\"$(xinitrcdir)\" -DBINDIR=\"$(bindir)\"
+
+dist_launchd_startx_SOURCES = \
+ $(srcdir)/../console_redirect.c \
+ launchd_startx.c
+
+CPP_FILES_FLAGS = \
+ -D__xinitrcdir__="$(xinitrcdir)" \
+ -D__bindir__="$(bindir)" \
+ -DBUNDLE_ID_PREFIX="$(bundleidprefix)"
+
+$(bundleidprefix).startx.plist.cpp: startx.plist.cpp
+ cp $< $@
+
+launchagents_PRE = $(bundleidprefix).startx.plist.cpp
+launchagents_DATA = $(launchagents_PRE:plist.cpp=plist)
+
+EXTRA_DIST = startx.plist.cpp
+
+CLEANFILES = \
+ $(launchagents_DATA) \
+ $(bundleidprefix).startx.plist.cpp
diff --git a/launchd/user_startx/launchd_startx.c b/launchd/user_startx/launchd_startx.c
new file mode 100644
index 0000000..e3fae76
--- /dev/null
+++ b/launchd/user_startx/launchd_startx.c
@@ -0,0 +1,64 @@
+/* Copyright (c) 2011 Apple Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
+ * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name(s) of the above
+ * copyright holders shall not be used in advertising or otherwise to
+ * promote the sale, use or other dealings in this Software without
+ * prior written authorization.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <asl.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <assert.h>
+#include <spawn.h>
+#include <sys/wait.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "console_redirect.h"
+
+int main(int argc, char **argv, char **envp) {
+ aslclient aslc;
+ pid_t child;
+ int pstat;
+
+ if(argc < 2 || strcmp(argv[1], "--help") == 0) {
+ fprintf(stderr, "Usage: %s prog [args...]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ aslc = asl_open(BUNDLE_ID_PREFIX".startx", BUNDLE_ID_PREFIX, ASL_OPT_NO_DELAY);
+
+ xi_asl_capture_fd(aslc, NULL, ASL_LEVEL_INFO, STDOUT_FILENO);
+ xi_asl_capture_fd(aslc, NULL, ASL_LEVEL_NOTICE, STDERR_FILENO);
+
+ assert(posix_spawnp(&child, argv[1], NULL, NULL, &argv[1], envp) == 0);
+
+ wait4(child, &pstat, 0, (struct rusage *)0);
+
+ return pstat;
+}
diff --git a/org.x.startx.plist.cpp b/launchd/user_startx/startx.plist.cpp
index bd4c9e9..9d9c1a4 100644
--- a/org.x.startx.plist.cpp
+++ b/launchd/user_startx/startx.plist.cpp
@@ -3,14 +3,15 @@
<plist version="1.0">
<dict>
<key>Label</key>
- <string>LAUNCHD_ID_PREFIX.startx</string>
+ <string>BUNDLE_ID_PREFIX.startx</string>
<key>ProgramArguments</key>
<array>
+ <string>__xinitrcdir__/launchd_startx</string>
<string>__bindir__/startx</string>
</array>
<key>Sockets</key>
<dict>
- <key>LAUNCHD_ID_PREFIX:0</key>
+ <key>BUNDLE_ID_PREFIX:0</key>
<dict>
<key>SecureSocketWithKey</key>
<string>DISPLAY</string>
diff --git a/startx.cpp b/startx.cpp
index 0f4cca5..9f6561d 100644
--- a/startx.cpp
+++ b/startx.cpp
@@ -83,7 +83,7 @@ serverargs=""
#ifdef __APPLE__
if [ "x$X11_PREFS_DOMAIN" = x ] ; then
- export X11_PREFS_DOMAIN=LAUNCHD_ID_PREFIX".X11"
+ export X11_PREFS_DOMAIN=BUNDLE_ID_PREFIX".X11"
fi
XCOMM Initialize defaults (this will cut down on "safe" error messages)