xserver: Branch 'master' - 2 commits

Jeremy Huddleston jeremyhu at kemper.freedesktop.org
Mon May 19 02:47:25 PDT 2008


 hw/xquartz/mach-startup/bundle-main.c     |  145 +++++++++++++++++++++---------
 hw/xquartz/mach-startup/mach_startup.defs |    4 
 hw/xquartz/mach-startup/stub.c            |   93 +++++++++++++++++--
 hw/xquartz/quartzKeyboard.c               |   32 +++---
 4 files changed, 209 insertions(+), 65 deletions(-)

New commits:
commit bb687465d0c5aef3cc1c865e6ea67c01a8b417fb
Author: Jeremy Huddleston <jeremyhu at freedesktop.org>
Date:   Mon May 19 02:45:47 2008 -0700

    XQuartz: First stab at SCM_RIGHTS passing the $DISPLAY launchd fd from the stub to server
    (cherry picked from commit cccee9cfc29f85cca635df3b8dd54199b45c4df8)

diff --git a/hw/xquartz/mach-startup/bundle-main.c b/hw/xquartz/mach-startup/bundle-main.c
index 4ddd82f..c975123 100644
--- a/hw/xquartz/mach-startup/bundle-main.c
+++ b/hw/xquartz/mach-startup/bundle-main.c
@@ -37,6 +37,7 @@
 #include <stdlib.h>
 #include <pthread.h>
 #include <stdbool.h>
+
 #include <sys/socket.h>
 #include <sys/un.h>
 
@@ -120,68 +121,140 @@ static pthread_t create_thread(void *func, void *arg) {
 }
 
 /*** $DISPLAY handoff ***/
-static char display_handoff_socket[PATH_MAX + 1];
-
-kern_return_t do_get_display_handoff_socket(mach_port_t port, string_t filename) {
-    strlcpy(filename, display_handoff_socket, STRING_T_SIZE);
-    fprintf(stderr, "Telling him the filename is %s = %s\n", filename, display_handoff_socket);
-    return KERN_SUCCESS;
-}
-
 /* From darwinEvents.c ... but don't want to pull in all the server cruft */
 void DarwinListenOnOpenFD(int fd);
 
-static void accept_fd_handoff(int connectedSocket) {
-    int fd;
-    return;
-    DarwinListenOnOpenFD(fd);
+static void accept_fd_handoff(int connected_fd) {
+    int launchd_fd;
+    
+    char databuf[] = "display";
+    struct iovec iov[1];
+    
+    iov[0].iov_base = databuf;
+    iov[0].iov_len  = sizeof(databuf);
+    
+    union {
+        struct cmsghdr hdr;
+        char bytes[CMSG_SPACE(sizeof(int))];
+    } buf;
+    
+    struct msghdr msg;
+    msg.msg_iov = iov;
+    msg.msg_iovlen = 1;
+    msg.msg_control = buf.bytes;
+    msg.msg_controllen = sizeof(buf);
+    msg.msg_name = 0;
+    msg.msg_namelen = 0;
+    msg.msg_flags = 0;
+    
+    struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+    cmsg->cmsg_level = SOL_SOCKET;
+    cmsg->cmsg_type = SCM_RIGHTS;
+    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+    
+    msg.msg_controllen = cmsg->cmsg_len;
+    
+    *((int*)CMSG_DATA(cmsg)) = -1;
+    
+    if(recvmsg(connected_fd, &msg, 0) < 0) {
+        fprintf(stderr, "Error receiving $DISPLAY file descriptor: %s\n", strerror(errno));
+        return;
+    }
+    
+    launchd_fd = *((int*)CMSG_DATA(cmsg));
+    
+    if(launchd_fd > 0)
+        DarwinListenOnOpenFD(launchd_fd);
+    else
+        fprintf(stderr, "Error receiving $DISPLAY file descriptor, no descriptor received? %d\n", launchd_fd);
 }
 
+typedef struct {
+    string_t socket_filename;
+    pthread_mutex_t lock;
+    pthread_cond_t cond;
+    kern_return_t retval;
+} handoff_data_t;
+
 /* This thread loops accepting incoming connections and handing off the file
  * descriptor for the new connection to accept_fd_handoff()
  */
 static void socket_handoff_thread(void *arg) {
+    handoff_data_t *data = (handoff_data_t *)arg;
+    string_t filename;
     struct sockaddr_un servaddr_un;
     struct sockaddr *servaddr;
-    int handoff_fd;
-    int servaddr_len;
+    socklen_t servaddr_len;
+    int handoff_fd, connected_fd;
+
+    /* We need to save it since data dies after we pthread_cond_broadcast */
+    strlcpy(filename, data->socket_filename, STRING_T_SIZE); 
     
-    /* Wipe ourselves clean */
-    memset (&servaddr_un, 0, sizeof (struct sockaddr_un));
+    /* Make sure we only run once the dispatch thread is waiting for us */
+    pthread_mutex_lock(&data->lock);
     
+    /* Setup servaddr_un */
+    memset (&servaddr_un, 0, sizeof (struct sockaddr_un));
     servaddr_un.sun_family  = AF_UNIX;
-    strcpy(servaddr_un.sun_path, display_handoff_socket);
+    strlcpy(servaddr_un.sun_path, filename, sizeof(servaddr_un.sun_path));
+
     servaddr = (struct sockaddr *) &servaddr_un;
-    servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(display_handoff_socket);
+    servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename);
     
     handoff_fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if(handoff_fd == 0) {
-        fprintf(stderr, "Failed to create socket: %s - %s\n", display_handoff_socket, strerror(errno));
+        fprintf(stderr, "Failed to create socket: %s - %s\n", filename, strerror(errno));
+        data->retval = EXIT_FAILURE;
         return;
-        exit(EXIT_FAILURE);
     }
     
     if(bind(handoff_fd, servaddr, servaddr_len) != 0) {
-        fprintf(stderr, "Failed to bind socket: %s - %s\n", display_handoff_socket, strerror(errno));
+        fprintf(stderr, "Failed to bind socket: %s - %s\n", filename, strerror(errno));
+        data->retval = EXIT_FAILURE;
         return;
-        exit(EXIT_FAILURE);
     }
     
     if(listen(handoff_fd, 10) != 0) {
-        fprintf(stderr, "Failed to listen to socket: %s - %s\n", display_handoff_socket, strerror(errno));
+        fprintf(stderr, "Failed to listen to socket: %s - %s\n", filename, strerror(errno));
+        data->retval = EXIT_FAILURE;
         return;
-        exit(EXIT_FAILURE);
     }
+
+    /* Let the dispatch thread now tell the caller that we're listening */
+    data->retval = EXIT_SUCCESS;
+    pthread_mutex_unlock(&data->lock);
+    pthread_cond_broadcast(&data->cond);
     
-    while(true) {
-        int connectedSocket = accept(handoff_fd, NULL, NULL);
-        
-        if(connectedSocket == -1) {
-            fprintf(stderr, "Failed to accept incoming connection on socket: %s - %s\n", display_handoff_socket, strerror(errno));
-            continue;
-        }
-        accept_fd_handoff(connectedSocket);    
+    connected_fd = accept(handoff_fd, NULL, NULL);
+    
+    /* We delete this temporary socket after we get the connection */
+    unlink(filename);
+
+    if(connected_fd == -1) {
+        fprintf(stderr, "Failed to accept incoming connection on socket: %s - %s\n", filename, strerror(errno));
+        return;
     }
+
+    /* Now actually get the passed file descriptor from this connection */
+    accept_fd_handoff(connected_fd);
+    
+    close(handoff_fd);
+}
+
+kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) {
+    handoff_data_t handoff_data;
+
+    pthread_mutex_init(&handoff_data.lock, NULL);
+    pthread_cond_init(&handoff_data.cond, NULL);
+    strlcpy(handoff_data.socket_filename, socket_filename, STRING_T_SIZE); 
+
+    pthread_mutex_lock(&handoff_data.lock);
+    
+    create_thread(socket_handoff_thread, &handoff_data);
+    
+    pthread_cond_wait(&handoff_data.cond, &handoff_data.lock);
+
+    return handoff_data.retval;
 }
 
 /*** Server Startup ***/
@@ -334,14 +407,6 @@ int main(int argc, char **argv, char **envp) {
         return EXIT_FAILURE;
     }
     
-    /* Figure out what our handoff socket will be
-     * TODO: cleanup on exit.
-     */
-    tmpnam(display_handoff_socket);
-    create_thread(socket_handoff_thread, NULL);
-    
-    fprintf(stderr, "Hi\n");
-    
     /* Check if we need to do something other than listen, and make another
      * thread handle it.
      */
diff --git a/hw/xquartz/mach-startup/mach_startup.defs b/hw/xquartz/mach-startup/mach_startup.defs
index 05bdf56..19c105c 100644
--- a/hw/xquartz/mach-startup/mach_startup.defs
+++ b/hw/xquartz/mach-startup/mach_startup.defs
@@ -41,6 +41,6 @@ routine start_x11_server(
     argv  : string_array_t;
     envp  : string_array_t);
 
-routine get_display_handoff_socket(
+routine prep_fd_handoff(
 	port  : mach_port_t;
-    filename  : string_t);
+    socket_filename  : string_t);
diff --git a/hw/xquartz/mach-startup/stub.c b/hw/xquartz/mach-startup/stub.c
index 5fdbad8..0a9ab66 100644
--- a/hw/xquartz/mach-startup/stub.c
+++ b/hw/xquartz/mach-startup/stub.c
@@ -37,6 +37,9 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include <sys/socket.h>
+#include <sys/un.h>
+
 #define kX11AppBundleId "org.x.X11"
 #define kX11AppBundlePath "/Contents/MacOS/X11"
 
@@ -47,6 +50,13 @@
 
 #include "launchd_fd.h"
 
+#ifndef BUILD_DATE
+#define BUILD_DATE "?"
+#endif
+#ifndef XSERVER_VERSION
+#define XSERVER_VERSION "?"
+#endif
+
 static char x11_path[PATH_MAX + 1];
 
 static void set_x11_path() {
@@ -103,12 +113,75 @@ static void set_x11_path() {
     }
 }
 
-#ifndef BUILD_DATE
-#define BUILD_DATE "?"
-#endif
-#ifndef XSERVER_VERSION
-#define XSERVER_VERSION "?"
-#endif
+static void send_fd_handoff(int connected_fd, int launchd_fd) {
+    char databuf[] = "display";
+    struct iovec iov[1];
+    
+    iov[0].iov_base = databuf;
+    iov[0].iov_len  = sizeof(databuf);
+
+    union {
+        struct cmsghdr hdr;
+        char bytes[CMSG_SPACE(sizeof(int))];
+    } buf;
+    
+    struct msghdr msg;
+    msg.msg_iov = iov;
+    msg.msg_iovlen = 1;
+    msg.msg_control = buf.bytes;
+    msg.msg_controllen = sizeof(buf);
+    msg.msg_name = 0;
+    msg.msg_namelen = 0;
+    msg.msg_flags = 0;
+
+    struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
+    cmsg->cmsg_level = SOL_SOCKET;
+    cmsg->cmsg_type = SCM_RIGHTS;
+    cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+
+    msg.msg_controllen = cmsg->cmsg_len;
+    
+    *((int*)CMSG_DATA(cmsg)) = launchd_fd;
+    
+    if (sendmsg(connected_fd, &msg, 0) < 0) {
+        fprintf(stderr, "Error sending $DISPLAY file descriptor: %s\n", strerror(errno));
+        return;
+    }
+
+    fprintf(stderr, "send %d %d %d %s\n", connected_fd, launchd_fd, errno, strerror(errno));
+}
+
+static void handoff_fd(const char *filename, int launchd_fd) {
+    struct sockaddr_un servaddr_un;
+    struct sockaddr *servaddr;
+    socklen_t servaddr_len;
+    int handoff_fd, connected_fd;
+
+    /* Setup servaddr_un */
+    memset (&servaddr_un, 0, sizeof (struct sockaddr_un));
+    servaddr_un.sun_family = AF_UNIX;
+    strlcpy(servaddr_un.sun_path, filename, sizeof(servaddr_un.sun_path));
+
+    servaddr = (struct sockaddr *) &servaddr_un;
+    servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename);
+
+    handoff_fd = socket(PF_UNIX, SOCK_STREAM, 0);
+    if(handoff_fd == 0) {
+        fprintf(stderr, "Failed to create socket: %s - %s\n", filename, strerror(errno));
+        return;
+    }
+
+    connected_fd = connect(handoff_fd, servaddr, servaddr_len);
+
+    if(connected_fd == -1) {
+        fprintf(stderr, "Failed to establish connection on socket: %s - %s\n", filename, strerror(errno));
+        return;
+    }
+
+    send_fd_handoff(connected_fd, launchd_fd);
+
+    close(handoff_fd);
+}
 
 int main(int argc, char **argv, char **envp) {
 #ifdef NEW_LAUNCH_METHOD
@@ -169,8 +242,12 @@ int main(int argc, char **argv, char **envp) {
     
     /* Handoff the $DISPLAY FD */
     if(launchd_fd != -1) {
-        get_display_handoff_socket(mp, handoff_socket);
-        fprintf(stderr, "Handoff socket: %s %d\n", handoff_socket, launchd_fd);
+        tmpnam(handoff_socket);
+        if(prep_fd_handoff(mp, handoff_socket) == KERN_SUCCESS) {
+            handoff_fd(handoff_socket, launchd_fd);
+        } else {
+            fprintf(stderr, "Unable to hand of $DISPLAY file descriptor\n");
+        }
     }
 
     /* Count envp */
commit 4b69d22bcb79210225379da2cefb0cd8d5ffa10e
Author: Jeremy Huddleston <jeremyhu at freedesktop.org>
Date:   Sun May 18 23:51:06 2008 -0700

    XQuartz: Fixed dropped code in the !XKB blocks, disable XKB support until we figure out a solution for xkeyboard-config
    (cherry picked from commit 2a72309c061f7060480d150791019ce232481462)

diff --git a/hw/xquartz/quartzKeyboard.c b/hw/xquartz/quartzKeyboard.c
index c75cd1d..5b8d32d 100644
--- a/hw/xquartz/quartzKeyboard.c
+++ b/hw/xquartz/quartzKeyboard.c
@@ -43,7 +43,7 @@
 // Define this to get a diagnostic output to stderr which is helpful
 // in determining how the X server is interpreting the Darwin keymap.
 #define DUMP_DARWIN_KEYMAP
-#define XQUARTZ_USE_XKB 
+//#define XQUARTZ_USE_XKB 
 #define HACK_MISSING 1
 #define HACK_KEYPAD 1
 
@@ -910,24 +910,26 @@ void DarwinKeyboardInit(DeviceIntPtr pDev) {
 
 
 void DarwinKeyboardReloadHandler(int screenNum, xEventPtr xe, DeviceIntPtr pDev, int nevents) {
-	if (pDev == NULL) pDev = darwinKeyboard;
-	
-	DEBUG_LOG("DarwinKeyboardReloadHandler(%p)\n", pDev);
+    if (pDev == NULL) pDev = darwinKeyboard;
+    
+    DEBUG_LOG("DarwinKeyboardReloadHandler(%p)\n", pDev);
 
 #ifdef XQUARTZ_USE_XKB
-	QuartzXkbUpdate(pDev);
+    QuartzXkbUpdate(pDev);
 #else
-	if (pDev->key) {
-		if (pDev->key->curKeySyms.map) xfree(pDev->key->curKeySyms.map);
-		if (pDev->key->modifierKeyMap) xfree(pDev->key->modifierKeyMap);
-		xfree(pDev->key);
-	}
-	
     KeySymsRec keySyms;
-	if (!InitKeyClassDeviceStruct(pDev, &keySyms, keyInfo.modMap)) {
-		DEBUG_LOG("InitKeyClassDeviceStruct failed\n");
-		return;
-	}
+    DarwinLoadKeyboardMapping(&keySyms);
+
+    if (pDev->key) {
+        if (pDev->key->curKeySyms.map) xfree(pDev->key->curKeySyms.map);
+        if (pDev->key->modifierKeyMap) xfree(pDev->key->modifierKeyMap);
+        xfree(pDev->key);
+    }
+    
+    if (!InitKeyClassDeviceStruct(pDev, &keySyms, keyInfo.modMap)) {
+        DEBUG_LOG("InitKeyClassDeviceStruct failed\n");
+        return;
+    }
 
     SendMappingNotify(MappingKeyboard, MIN_KEYCODE, NUM_KEYCODES, 0);
     SendMappingNotify(MappingModifier, 0, 0, 0);


More information about the xorg-commit mailing list