[Xquartz-changes] xserver: Branch 'master' - 2 commits

Jeremy Huddleston jeremyhu at freedesktop.org
Wed May 11 17:30:18 PDT 2011


 configure.ac                  |    3 
 hw/xquartz/Makefile.am        |    6 
 hw/xquartz/console_redirect.c |  392 ++++++++++++++++++++++++++++++++++++++++++
 hw/xquartz/console_redirect.h |   44 ++++
 4 files changed, 441 insertions(+), 4 deletions(-)

New commits:
commit cb9c2aee640ed74bacdbc248e2da07f958745dca
Author: Jeremy Huddleston <jeremyhu at apple.com>
Date:   Wed May 11 15:23:17 2011 -0700

    XQuartz: Add API which will allow redirection of stdout/stderr to ASL
    
    In order to improve logging in XQuartz, stdout and stderr should be redirected
    to ASL (syslog).  This code provides an API for doing that.
    
    Signed-off-by: Jeremy Huddleston <jeremyhu at apple.com>

diff --git a/hw/xquartz/Makefile.am b/hw/xquartz/Makefile.am
index 76f624d..a7cc012 100644
--- a/hw/xquartz/Makefile.am
+++ b/hw/xquartz/Makefile.am
@@ -33,7 +33,8 @@ libXquartz_la_SOURCES = \
 	quartzCocoa.m \
 	quartzKeyboard.c \
 	quartzStartup.c \
-	quartzRandR.c
+	quartzRandR.c \
+	console_redirect.c
 
 EXTRA_DIST = \
 	X11Application.h \
@@ -49,4 +50,5 @@ EXTRA_DIST = \
 	quartzKeyboard.h \
 	quartzRandR.h \
 	sanitizedCarbon.h \
-	sanitizedCocoa.h
+	sanitizedCocoa.h \
+	console_redirect.h
diff --git a/hw/xquartz/console_redirect.c b/hw/xquartz/console_redirect.c
new file mode 100644
index 0000000..957ac1f
--- /dev/null
+++ b/hw/xquartz/console_redirect.c
@@ -0,0 +1,392 @@
+/* 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_DIX_CONFIG_H
+#include <dix-config.h>
+#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>
+
+#ifdef HAVE_DISPATCH
+#include <dispatch/dispatch.h>
+#else
+#include <pthread.h>
+#endif
+
+#include "console_redirect.h"
+
+#define BUF_SIZE 512
+
+static int kq;
+
+typedef struct {
+    int level;
+    aslclient asl;
+    aslmsg msg;
+
+    /* Buffered reading */
+    char *buf;
+    char *w;
+} asl_redirect;
+
+static asl_redirect *redirect_fds = NULL;
+static int n_redirect_fds = 0;
+
+#ifdef HAVE_DISPATCH
+static dispatch_queue_t redirect_serial_q;
+#else
+static pthread_t redirect_pthread;
+static pthread_mutex_t redirect_fds_lock = PTHREAD_MUTEX_INITIALIZER;
+
+/* Notifications to our reader thread */
+#define ASL_REDIRECT_TERMINATE ((void *)(uintptr_t)1)
+#endif
+
+/* 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);
+                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_DISPATCH
+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];
+    
+    close(fd);
+    free(aslr->buf);
+    memset(aslr, 0, sizeof(*aslr));
+    dispatch_release(source);
+}
+
+#else /* !HAVE_DISPATCH */
+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_DISPATCH
+    {
+        dispatch_sync(redirect_serial_q, ^{});
+        dispatch_sync(redirect_serial_q, ^{});
+    }
+#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_DISPATCH
+static void xq_asl_init(void *ctx __unused)
+#else
+static void xq_asl_init(void)
+#endif
+{
+    assert((kq = kqueue()) != -1);
+    assert((redirect_fds = calloc(16, sizeof(*redirect_fds))) != NULL);
+    n_redirect_fds = 16;
+
+#ifdef HAVE_DISPATCH
+    redirect_serial_q = dispatch_queue_create("com.apple.asl-redirect", NULL);
+    assert(redirect_serial_q != NULL);
+#else
+    assert(pthread_create(&redirect_pthread, NULL, redirect_thread, NULL) == 0);
+#endif
+
+    atexit(redirect_atexit);
+}
+
+int xq_asl_log_fd(aslclient asl, aslmsg msg, int level, int fd) {
+#ifdef HAVE_DISPATCH
+    int err __block = 0;
+    static dispatch_once_t once_control;
+    dispatch_once_f(&once_control, NULL, xq_asl_init);
+#else
+    int err = 0;
+    static pthread_once_t once_control = PTHREAD_ONCE_INIT;
+    assert(pthread_once(&once_control, xq_asl_init) == 0);
+#endif
+    
+    if(fd < 0)
+        return EBADF;
+    
+#ifdef HAVE_DISPATCH
+    dispatch_sync(redirect_serial_q, ^
+#else
+    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;
+                goto 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;
+            goto done;
+        }
+        
+        /* Initialize our buffer */
+        redirect_fds[fd].buf = (char *)malloc(BUF_SIZE);
+        if(redirect_fds[fd].buf == NULL) {
+            err = errno;
+            goto 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_DISPATCH
+        {
+            dispatch_source_t fd_read_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fd, 0, redirect_serial_q);
+            dispatch_set_context(fd_read_source, fd_read_source);
+            dispatch_source_set_event_handler_f(fd_read_source, read_from_source);
+            dispatch_source_set_cancel_handler_f(fd_read_source, cancel_source);
+            dispatch_resume(fd_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
+        done: ;;
+    }
+#ifdef HAVE_DISPATCH
+    );
+#else
+    assert(pthread_mutex_unlock(&redirect_fds_lock) == 0);
+#endif
+    
+    return err;
+}
+
+int xq_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 xq_asl_log_fd */
+    return xq_asl_log_fd(asl, msg, level, pipepair[0]);
+}
+
+#ifdef DEBUG_CONSOLE_REDIRECT
+int main(int argc __unused, char **argv __unused) {
+    xq_asl_capture_fd(NULL, NULL, ASL_LEVEL_NOTICE, STDOUT_FILENO);
+    xq_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/hw/xquartz/console_redirect.h b/hw/xquartz/console_redirect.h
new file mode 100644
index 0000000..94520a3
--- /dev/null
+++ b/hw/xquartz/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 xq_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 xq_asl_log_fd(aslclient asl, aslmsg msg, int level, int fd);
+
+#endif
commit a558a66d249cd51d3d675b20cc3a76651fd87930
Author: Jeremy Huddleston <jeremyhu at apple.com>
Date:   Wed May 11 17:27:52 2011 -0700

    configure.ac: XQuartz: Fix support for the deprecated --with-launchd-id-prefix
    
    Signed-off-by: Jeremy Huddleston <jeremyhu at apple.com>

diff --git a/configure.ac b/configure.ac
index 61caaed..a474073 100644
--- a/configure.ac
+++ b/configure.ac
@@ -566,8 +566,7 @@ AC_ARG_WITH(launchd-id-prefix,  AS_HELP_STRING([--with-launchd-id-prefix=PATH],
                                 [ BUNDLE_ID_PREFIX="${withval}" ],
                                 [ BUNDLE_ID_PREFIX="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)]),
-                               [ BUNDLE_ID_PREFIX="${withval}" ],
-                               [ BUNDLE_ID_PREFIX="org.x" ])
+                               [ BUNDLE_ID_PREFIX="${withval}" ])
 AC_SUBST([BUNDLE_ID_PREFIX])
 AC_DEFINE_UNQUOTED(BUNDLE_ID_PREFIX, "$BUNDLE_ID_PREFIX", [Prefix to use for bundle identifiers])
 AC_ARG_ENABLE(sparkle,AS_HELP_STRING([--enable-sparkle], [Enable updating of X11.app using the Sparkle Framework (default: disabled)]),


More information about the Xquartz-changes mailing list