diff options
author | Jeremy Huddleston <jeremyhu@freedesktop.org> | 2008-06-03 00:06:26 -0700 |
---|---|---|
committer | Jeremy Huddleston <jeremyhu@freedesktop.org> | 2008-06-04 12:24:04 -0700 |
commit | 2393dae6ffff7db8281a7977039450e8754cc19f (patch) | |
tree | c859de13366e695c4b4e40c71087e367008de13d /hw/xquartz/mach-startup/stub.c | |
parent | 45b661c67aca1a728f1793dd94d701488f1f196c (diff) |
XQuartz: Switched over to new startup path for testing. Cleaned it up a bit. Server still crashes when using the icon to launch =/
(cherry picked from commit 7f840e9dc180421eaa9b0ea3ab993fdd5b2466e5)
Diffstat (limited to 'hw/xquartz/mach-startup/stub.c')
-rw-r--r-- | hw/xquartz/mach-startup/stub.c | 99 |
1 files changed, 63 insertions, 36 deletions
diff --git a/hw/xquartz/mach-startup/stub.c b/hw/xquartz/mach-startup/stub.c index 854b71cbe..d48ae7477 100644 --- a/hw/xquartz/mach-startup/stub.c +++ b/hw/xquartz/mach-startup/stub.c @@ -113,9 +113,53 @@ static void set_x11_path() { } } +#ifdef NEW_LAUNCH_METHOD +static int create_socket(char *filename_out) { + struct sockaddr_un servaddr_un; + struct sockaddr *servaddr; + socklen_t servaddr_len; + int ret_fd; + size_t try, try_max; + + for(try=0, try_max=5; try < try_max; try++) { + tmpnam(filename_out); + + /* Setup servaddr_un */ + memset (&servaddr_un, 0, sizeof (struct sockaddr_un)); + servaddr_un.sun_family = AF_UNIX; + strlcpy(servaddr_un.sun_path, filename_out, sizeof(servaddr_un.sun_path)); + + servaddr = (struct sockaddr *) &servaddr_un; + servaddr_len = sizeof(struct sockaddr_un) - sizeof(servaddr_un.sun_path) + strlen(filename_out); + + ret_fd = socket(PF_UNIX, SOCK_STREAM, 0); + if(ret_fd == 0) { + fprintf(stderr, "Failed to create socket (try %d / %d): %s - %s\n", (int)try+1, (int)try_max, filename_out, strerror(errno)); + continue; + } + + if(bind(ret_fd, servaddr, servaddr_len) != 0) { + fprintf(stderr, "Failed to bind socket: %s - %s\n", filename_out, strerror(errno)); + close(ret_fd); + return 0; + } + + if(listen(ret_fd, 10) != 0) { + fprintf(stderr, "Failed to listen to socket: %s - %s\n", filename_out, strerror(errno)); + close(ret_fd); + return 0; + } + + return ret_fd; + } + + return 0; +} + static void send_fd_handoff(int handoff_fd, int launchd_fd) { char databuf[] = "display"; struct iovec iov[1]; + int connected_fd; iov[0].iov_base = databuf; iov[0].iov_len = sizeof(databuf); @@ -143,44 +187,21 @@ static void send_fd_handoff(int handoff_fd, int launchd_fd) { *((int*)CMSG_DATA(cmsg)) = launchd_fd; - if (sendmsg(handoff_fd, &msg, 0) < 0) { - fprintf(stderr, "Error sending $DISPLAY file descriptor: %s\n", strerror(errno)); + connected_fd = accept(handoff_fd, NULL, NULL); + if(connected_fd == -1) { + fprintf(stderr, "Failed to accept incoming connection on socket: %s\n", strerror(errno)); return; } - - fprintf(stderr, "send %d %d %d %s\n", handoff_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; - - /* 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; - } - - if(connect(handoff_fd, servaddr, servaddr_len) < 0) { - fprintf(stderr, "Failed to establish connection on socket: %s - %s\n", filename, strerror(errno)); + + if(sendmsg(connected_fd, &msg, 0) < 0) { + fprintf(stderr, "Error sending $DISPLAY file descriptor: %s\n", strerror(errno)); return; } - fprintf(stderr, "Socket: %s\n", filename); - - send_fd_handoff(handoff_fd, launchd_fd); - close(handoff_fd); + close(connected_fd); + fprintf(stderr, "send %d %d %d %s\n", handoff_fd, launchd_fd, errno, strerror(errno)); } +#endif int main(int argc, char **argv, char **envp) { #ifdef NEW_LAUNCH_METHOD @@ -191,7 +212,7 @@ int main(int argc, char **argv, char **envp) { string_array_t newargv; size_t i; int launchd_fd; - string_t handoff_socket; + string_t handoff_socket_filename; #endif if(argc == 2 && !strcmp(argv[1], "-version")) { @@ -241,9 +262,15 @@ int main(int argc, char **argv, char **envp) { /* Handoff the $DISPLAY FD */ if(launchd_fd != -1) { - tmpnam(handoff_socket); - if(prep_fd_handoff(mp, handoff_socket) == KERN_SUCCESS) { - handoff_fd(handoff_socket, launchd_fd); + int handoff_fd = create_socket(handoff_socket_filename); + + if((handoff_fd != 0) && + (prep_fd_handoff(mp, handoff_socket_filename) == KERN_SUCCESS)) { + send_fd_handoff(handoff_fd, launchd_fd); + + // Cleanup + close(handoff_fd); + unlink(handoff_socket_filename); } else { fprintf(stderr, "Unable to hand of $DISPLAY file descriptor\n"); } |