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 <stdlib.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
|
||||||
|
@ -120,68 +121,140 @@ static pthread_t create_thread(void *func, void *arg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** $DISPLAY handoff ***/
|
/*** $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 */
|
/* From darwinEvents.c ... but don't want to pull in all the server cruft */
|
||||||
void DarwinListenOnOpenFD(int fd);
|
void DarwinListenOnOpenFD(int fd);
|
||||||
|
|
||||||
static void accept_fd_handoff(int connectedSocket) {
|
static void accept_fd_handoff(int connected_fd) {
|
||||||
int fd;
|
int launchd_fd;
|
||||||
return;
|
|
||||||
DarwinListenOnOpenFD(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
|
/* This thread loops accepting incoming connections and handing off the file
|
||||||
* descriptor for the new connection to accept_fd_handoff()
|
* descriptor for the new connection to accept_fd_handoff()
|
||||||
*/
|
*/
|
||||||
static void socket_handoff_thread(void *arg) {
|
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_un servaddr_un;
|
||||||
struct sockaddr *servaddr;
|
struct sockaddr *servaddr;
|
||||||
int handoff_fd;
|
socklen_t servaddr_len;
|
||||||
int 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 */
|
/* 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));
|
memset (&servaddr_un, 0, sizeof (struct sockaddr_un));
|
||||||
|
|
||||||
servaddr_un.sun_family = AF_UNIX;
|
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 = (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);
|
handoff_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if(handoff_fd == 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;
|
return;
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(bind(handoff_fd, servaddr, servaddr_len) != 0) {
|
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;
|
return;
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(listen(handoff_fd, 10) != 0) {
|
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;
|
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) {
|
connected_fd = accept(handoff_fd, NULL, NULL);
|
||||||
int connectedSocket = accept(handoff_fd, NULL, NULL);
|
|
||||||
|
/* We delete this temporary socket after we get the connection */
|
||||||
if(connectedSocket == -1) {
|
unlink(filename);
|
||||||
fprintf(stderr, "Failed to accept incoming connection on socket: %s - %s\n", display_handoff_socket, strerror(errno));
|
|
||||||
continue;
|
if(connected_fd == -1) {
|
||||||
}
|
fprintf(stderr, "Failed to accept incoming connection on socket: %s - %s\n", filename, strerror(errno));
|
||||||
accept_fd_handoff(connectedSocket);
|
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 ***/
|
/*** Server Startup ***/
|
||||||
|
@ -334,14 +407,6 @@ int main(int argc, char **argv, char **envp) {
|
||||||
return EXIT_FAILURE;
|
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
|
/* Check if we need to do something other than listen, and make another
|
||||||
* thread handle it.
|
* thread handle it.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -41,6 +41,6 @@ routine start_x11_server(
|
||||||
argv : string_array_t;
|
argv : string_array_t;
|
||||||
envp : string_array_t);
|
envp : string_array_t);
|
||||||
|
|
||||||
routine get_display_handoff_socket(
|
routine prep_fd_handoff(
|
||||||
port : mach_port_t;
|
port : mach_port_t;
|
||||||
filename : string_t);
|
socket_filename : string_t);
|
||||||
|
|
|
@ -37,6 +37,9 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/un.h>
|
||||||
|
|
||||||
#define kX11AppBundleId "org.x.X11"
|
#define kX11AppBundleId "org.x.X11"
|
||||||
#define kX11AppBundlePath "/Contents/MacOS/X11"
|
#define kX11AppBundlePath "/Contents/MacOS/X11"
|
||||||
|
|
||||||
|
@ -47,6 +50,13 @@
|
||||||
|
|
||||||
#include "launchd_fd.h"
|
#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 char x11_path[PATH_MAX + 1];
|
||||||
|
|
||||||
static void set_x11_path() {
|
static void set_x11_path() {
|
||||||
|
@ -103,12 +113,75 @@ static void set_x11_path() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef BUILD_DATE
|
static void send_fd_handoff(int connected_fd, int launchd_fd) {
|
||||||
#define BUILD_DATE "?"
|
char databuf[] = "display";
|
||||||
#endif
|
struct iovec iov[1];
|
||||||
#ifndef XSERVER_VERSION
|
|
||||||
#define XSERVER_VERSION "?"
|
iov[0].iov_base = databuf;
|
||||||
#endif
|
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) {
|
int main(int argc, char **argv, char **envp) {
|
||||||
#ifdef NEW_LAUNCH_METHOD
|
#ifdef NEW_LAUNCH_METHOD
|
||||||
|
@ -169,8 +242,12 @@ int main(int argc, char **argv, char **envp) {
|
||||||
|
|
||||||
/* Handoff the $DISPLAY FD */
|
/* Handoff the $DISPLAY FD */
|
||||||
if(launchd_fd != -1) {
|
if(launchd_fd != -1) {
|
||||||
get_display_handoff_socket(mp, handoff_socket);
|
tmpnam(handoff_socket);
|
||||||
fprintf(stderr, "Handoff socket: %s %d\n", handoff_socket, launchd_fd);
|
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 */
|
/* Count envp */
|
||||||
|
|
Loading…
Reference in New Issue