XQuartz: Some fd handoff cleanup.

(cherry picked from commit 9c20a4804d97e67a988f00f49866997209cce518)
This commit is contained in:
Jeremy Huddleston 2008-07-07 10:55:58 -07:00
parent 26d8030c38
commit 90dd2de845
5 changed files with 86 additions and 25 deletions

View File

@ -202,6 +202,12 @@ static void message_kit_thread (SEL selector, NSObject *arg) {
for_appkit = YES; for_appkit = YES;
for_x = YES; for_x = YES;
// fprintf(stderr, "fd_add_count: %d\n", fd_add_count);
if(fd_add_count) {
DarwinProcessFDAdditionQueue();
fprintf(stderr, "ran it - fd_add_count: %d\n", fd_add_count);
}
switch (type) { switch (type) {
case NSLeftMouseDown: case NSRightMouseDown: case NSOtherMouseDown: case NSLeftMouseDown: case NSRightMouseDown: case NSOtherMouseDown:
case NSLeftMouseUp: case NSRightMouseUp: case NSOtherMouseUp: case NSLeftMouseUp: case NSRightMouseUp: case NSOtherMouseUp:

View File

@ -79,6 +79,11 @@ void QuartzModeEQInit(void);
static int old_flags = 0; // last known modifier state static int old_flags = 0; // last known modifier state
#define FD_ADD_MAX 128
static int fd_add[FD_ADD_MAX];
int fd_add_count = 0;
static pthread_mutex_t fd_add_lock = PTHREAD_MUTEX_INITIALIZER;
static xEvent *darwinEvents = NULL; static xEvent *darwinEvents = NULL;
static pthread_mutex_t mieq_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mieq_lock = PTHREAD_MUTEX_INITIALIZER;
@ -232,15 +237,6 @@ static void DarwinSimulateMouseClick(
DarwinUpdateModifiers(KeyPress, modifierMask); DarwinUpdateModifiers(KeyPress, modifierMask);
} }
static void kXquartzListenOnOpenFDHandler(int screenNum, xEventPtr xe, DeviceIntPtr dev, int nevents) {
size_t i;
TA_SERVER();
for (i=0; i<nevents; i++) {
ListenOnOpenFD(xe[i].u.clientMessage.u.l.longs0);
}
}
/* Generic handler for Xquartz-specifc events. When possible, these should /* Generic handler for Xquartz-specifc events. When possible, these should
be moved into their own individual functions and set as handlers using be moved into their own individual functions and set as handlers using
mieqSetHandler. */ mieqSetHandler. */
@ -249,7 +245,7 @@ static void DarwinEventHandler(int screenNum, xEventPtr xe, DeviceIntPtr dev, in
int i; int i;
TA_SERVER(); TA_SERVER();
DEBUG_LOG("DarwinEventHandler(%d, %p, %p, %d)\n", screenNum, xe, dev, nevents); DEBUG_LOG("DarwinEventHandler(%d, %p, %p, %d)\n", screenNum, xe, dev, nevents);
for (i=0; i<nevents; i++) { for (i=0; i<nevents; i++) {
switch(xe[i].u.u.type) { switch(xe[i].u.u.type) {
@ -331,6 +327,35 @@ static void DarwinEventHandler(int screenNum, xEventPtr xe, DeviceIntPtr dev, in
} }
} }
void DarwinListenOnOpenFD(int fd) {
ErrorF("DarwinListenOnOpenFD: %d\n", fd);
pthread_mutex_lock(&fd_add_lock);
if(fd_add_count < FD_ADD_MAX)
fd_add[fd_add_count++] = fd;
else
ErrorF("FD Addition buffer at max. Dropping fd addition request.\n");
pthread_mutex_unlock(&fd_add_lock);
}
void DarwinProcessFDAdditionQueue() {
pthread_mutex_lock(&fd_add_lock);
while(fd_add_count) {
DarwinSendDDXEvent(kXquartzListenOnOpenFD, 1, fd_add[--fd_add_count]);
}
pthread_mutex_unlock(&fd_add_lock);
}
static void kXquartzListenOnOpenFDHandler(int screenNum, xEventPtr xe, DeviceIntPtr dev, int nevents) {
size_t i;
TA_SERVER();
for (i=0; i<nevents; i++) {
ListenOnOpenFD(xe[i].u.clientMessage.u.l.longs0);
}
}
Bool DarwinEQInit(void) { Bool DarwinEQInit(void) {
mieqInit(); mieqInit();
mieqSetHandler(kXquartzReloadKeymap, DarwinKeyboardReloadHandler); mieqSetHandler(kXquartzReloadKeymap, DarwinKeyboardReloadHandler);
@ -576,10 +601,6 @@ void DarwinUpdateModKeys(int flags) {
old_flags = flags; old_flags = flags;
} }
void DarwinListenOnOpenFD(int fd) {
DarwinSendDDXEvent(kXquartzListenOnOpenFD, 1, fd);
}
/* /*
* DarwinSendDDXEvent * DarwinSendDDXEvent
* Send the X server thread a message by placing it on the event queue. * Send the X server thread a message by placing it on the event queue.

View File

@ -41,6 +41,9 @@ void DarwinSendScrollEvents(float count_x, float count_y, int pointer_x, int poi
void DarwinUpdateModKeys(int flags); void DarwinUpdateModKeys(int flags);
void DarwinListenOnOpenFD(int fd); void DarwinListenOnOpenFD(int fd);
extern int fd_add_count;
void DarwinProcessFDAdditionQueue(void);
/* /*
* Special ddx events understood by the X server * Special ddx events understood by the X server
*/ */

View File

@ -57,6 +57,8 @@ extern int noPanoramiXExtension;
#define DEFAULT_STARTX "/usr/X11/bin/startx" #define DEFAULT_STARTX "/usr/X11/bin/startx"
#define DEFAULT_SHELL "/bin/sh" #define DEFAULT_SHELL "/bin/sh"
#define DEBUG 1
static int execute(const char *command); static int execute(const char *command);
static char *command_from_prefs(const char *key, const char *default_value); static char *command_from_prefs(const char *key, const char *default_value);
@ -198,8 +200,8 @@ static void socket_handoff_thread(void *arg) {
servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename); servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename);
handoff_fd = socket(AF_UNIX, SOCK_STREAM, 0); handoff_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if(handoff_fd == 0) { if(handoff_fd == -1) {
fprintf(stderr, "Failed to create socket: %s - %s\n", filename, strerror(errno)); fprintf(stderr, "X11.app: Failed to create socket: %d - %s\n", errno, strerror(errno));
data->retval = EXIT_FAILURE; data->retval = EXIT_FAILURE;
pthread_cond_broadcast(&data->cond); pthread_cond_broadcast(&data->cond);
@ -213,7 +215,7 @@ static void socket_handoff_thread(void *arg) {
pthread_mutex_unlock(&data->lock); pthread_mutex_unlock(&data->lock);
if(connect(handoff_fd, servaddr, servaddr_len) < 0) { if(connect(handoff_fd, servaddr, servaddr_len) < 0) {
fprintf(stderr, "Failed to connect to socket: %s - %s\n", filename, strerror(errno)); fprintf(stderr, "X11.app: Failed to connect to socket: %s - %d - %s\n", filename, errno, strerror(errno));
return; return;
} }
@ -226,6 +228,10 @@ static void socket_handoff_thread(void *arg) {
kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) { kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) {
handoff_data_t handoff_data; handoff_data_t handoff_data;
#ifdef DEBUG
fprintf(stderr, "X11.app: Prepping for fd handoff.\n");
#endif
/* Initialize our data */ /* Initialize our data */
pthread_mutex_init(&handoff_data.lock, NULL); pthread_mutex_init(&handoff_data.lock, NULL);
pthread_cond_init(&handoff_data.cond, NULL); pthread_cond_init(&handoff_data.cond, NULL);
@ -235,6 +241,10 @@ kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) {
create_thread(socket_handoff_thread, &handoff_data); create_thread(socket_handoff_thread, &handoff_data);
#ifdef DEBUG
fprintf(stderr, "X11.app: Thread created for handoff. Waiting on return value.\n");
#endif
/* Wait for our return value */ /* Wait for our return value */
pthread_cond_wait(&handoff_data.cond, &handoff_data.lock); pthread_cond_wait(&handoff_data.cond, &handoff_data.lock);
pthread_mutex_unlock(&handoff_data.lock); pthread_mutex_unlock(&handoff_data.lock);
@ -242,6 +252,10 @@ kern_return_t do_prep_fd_handoff(mach_port_t port, string_t socket_filename) {
/* Cleanup */ /* Cleanup */
pthread_cond_destroy(&handoff_data.cond); pthread_cond_destroy(&handoff_data.cond);
pthread_mutex_destroy(&handoff_data.lock); pthread_mutex_destroy(&handoff_data.lock);
#ifdef DEBUG
fprintf(stderr, "X11.app: Sending return value: %d\n", handoff_data.retval);
#endif
return handoff_data.retval; return handoff_data.retval;
} }

View File

@ -57,6 +57,8 @@
#define XSERVER_VERSION "?" #define XSERVER_VERSION "?"
#endif #endif
#define DEBUG 1
static char x11_path[PATH_MAX + 1]; static char x11_path[PATH_MAX + 1];
static void set_x11_path() { static void set_x11_path() {
@ -132,23 +134,27 @@ static int create_socket(char *filename_out) {
servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename_out); servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename_out);
ret_fd = socket(PF_UNIX, SOCK_STREAM, 0); ret_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(ret_fd == 0) { if(ret_fd == -1) {
fprintf(stderr, "Failed to create socket (try %d / %d): %s - %s\n", (int)try+1, (int)try_max, filename_out, strerror(errno)); fprintf(stderr, "Xquartz: Failed to create socket (try %d / %d): %s - %s\n", (int)try+1, (int)try_max, filename_out, strerror(errno));
continue; continue;
} }
if(bind(ret_fd, servaddr, servaddr_len) != 0) { if(bind(ret_fd, servaddr, servaddr_len) != 0) {
fprintf(stderr, "Failed to bind socket: %s - %s\n", filename_out, strerror(errno)); fprintf(stderr, "Xquartz: Failed to bind socket: %d - %s\n", errno, strerror(errno));
close(ret_fd); close(ret_fd);
return 0; return 0;
} }
if(listen(ret_fd, 10) != 0) { if(listen(ret_fd, 10) != 0) {
fprintf(stderr, "Failed to listen to socket: %s - %s\n", filename_out, strerror(errno)); fprintf(stderr, "Xquartz: Failed to listen to socket: %s - %d - %s\n", filename_out, errno, strerror(errno));
close(ret_fd); close(ret_fd);
return 0; return 0;
} }
#ifdef DEBUG
fprintf(stderr, "Xquartz: Listening on socket for fd handoff: %s\n", filename_out);
#endif
return ret_fd; return ret_fd;
} }
@ -186,19 +192,30 @@ static void send_fd_handoff(int handoff_fd, int launchd_fd) {
*((int*)CMSG_DATA(cmsg)) = launchd_fd; *((int*)CMSG_DATA(cmsg)) = launchd_fd;
#ifdef DEBUG
fprintf(stderr, "Xquartz: Waiting for fd handoff connection.\n");
#endif
connected_fd = accept(handoff_fd, NULL, NULL); connected_fd = accept(handoff_fd, NULL, NULL);
if(connected_fd == -1) { if(connected_fd == -1) {
fprintf(stderr, "Failed to accept incoming connection on socket: %s\n", strerror(errno)); fprintf(stderr, "Xquartz: Failed to accept incoming connection on socket: %s\n", strerror(errno));
return; return;
} }
#ifdef DEBUG
fprintf(stderr, "Xquartz: Handoff connection established. Sending message.\n");
#endif
if(sendmsg(connected_fd, &msg, 0) < 0) { if(sendmsg(connected_fd, &msg, 0) < 0) {
fprintf(stderr, "Error sending $DISPLAY file descriptor: %s\n", strerror(errno)); fprintf(stderr, "Xquartz: Error sending $DISPLAY file descriptor: %s\n", strerror(errno));
return; return;
} }
#ifdef DEBUG
fprintf(stderr, "Xquartz: Message sent. Closing.\n");
#endif
close(connected_fd); close(connected_fd);
fprintf(stderr, "send %d %d %d %s\n", handoff_fd, launchd_fd, errno, strerror(errno)); #ifdef DEBUG
fprintf(stderr, "Xquartz: end of send debug: %d %d %d %s\n", handoff_fd, launchd_fd, errno, strerror(errno));
#endif
} }
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp) {