XQuartz: First stab at SCM_RIGHTS passing the $DISPLAY launchd fd from the stub to server
(cherry picked from commit cccee9cfc29f85cca635df3b8dd54199b45c4df8)
This commit is contained in:
parent
4b69d22bcb
commit
bb687465d0
|
@ -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;
|
||||
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;
|
||||
DarwinListenOnOpenFD(fd);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
/* Wipe ourselves clean */
|
||||
/* We need to save it since data dies after we pthread_cond_broadcast */
|
||||
strlcpy(filename, data->socket_filename, STRING_T_SIZE);
|
||||
|
||||
/* 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);
|
||||
}
|
||||
|
||||
while(true) {
|
||||
int connectedSocket = accept(handoff_fd, NULL, NULL);
|
||||
/* 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);
|
||||
|
||||
if(connectedSocket == -1) {
|
||||
fprintf(stderr, "Failed to accept incoming connection on socket: %s - %s\n", display_handoff_socket, strerror(errno));
|
||||
continue;
|
||||
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;
|
||||
}
|
||||
accept_fd_handoff(connectedSocket);
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue