Commit 5688bc3f authored by Manuel Stoeckl's avatar Manuel Stoeckl
Browse files

Also replicate fds from 'server' to 'client'

parent 4cee0bda
......@@ -116,49 +116,47 @@ int run_client(const char *socket_path)
FD_ISSET(displayfd, &readfds));
if (FD_ISSET(chanclient, &readfds)) {
wp_log(WP_DEBUG, "client isset\n");
struct muxheader header;
int hr = read(chanclient, &header,
sizeof(struct muxheader));
if (hr >= 0 && hr < sizeof(struct muxheader)) {
wp_log(WP_DEBUG, "chanclient isset\n");
char *tmpbuf;
ssize_t nbytes =
read_size_then_buf(chanclient, &tmpbuf);
if (nbytes == 0) {
wp_log(WP_ERROR,
"channel client connection closed %d\n",
hr);
"channel read connection closed\n");
break;
}
if (hr == -1) {
wp_log(WP_ERROR, "FD header read failure: %s\n",
strerror(errno));
break;
}
char *tmpbuf = calloc(header.length, 1);
int nread = 0;
while (nread < header.length) {
int nr = read(chanclient, tmpbuf + nread,
header.length - nread);
if (nr <= 0) {
break;
}
nread += nr;
}
if (nread < header.length) {
wp_log(WP_ERROR,
"FD body read failure %ld/%ld: %s\n",
nread, header.length,
if (nbytes == -1) {
wp_log(WP_ERROR, "channel read failure: %s\n",
strerror(errno));
break;
}
wp_log(WP_DEBUG, "read bytes: %d = %d\n", nread,
header.length);
int wc = iovec_write(displayfd, tmpbuf, (size_t)nread,
NULL, 0);
char *waymsg;
int waylen;
int nids;
int ids[28];
int ntransfers;
struct transfer transfers[50];
unpack_pipe_message((size_t)nbytes, tmpbuf, &waylen,
&waymsg, &nids, ids, &ntransfers,
transfers);
apply_updates(&fdtransmap, ntransfers, transfers);
int fds[28];
memset(fds, 0, sizeof(fds));
untranslate_ids(&fdtransmap, nids, ids, fds);
wp_log(WP_DEBUG, "Read from conn %d = %d bytes\n",
nbytes, nbytes);
int wc = iovec_write(
displayfd, waymsg, waylen, fds, nids);
free(tmpbuf);
if (wc == -1) {
wp_log(WP_ERROR, "FD Write failure %ld: %s\n",
wp_log(WP_ERROR, "FD Write failure %d: %s\n",
wc, strerror(errno));
break;
}
free(tmpbuf);
wp_log(WP_DEBUG, "client done\n");
}
if (FD_ISSET(displayfd, &readfds)) {
......@@ -193,7 +191,7 @@ int run_client(const char *socket_path)
if (write(chanclient, msg, msglen) == -1) {
free(msg);
wp_log(WP_ERROR,
"CS msg write failure: %s\n",
"CC msg write failure: %s\n",
strerror(errno));
break;
}
......
......@@ -146,29 +146,19 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
FD_ISSET(client_socket, &readfds));
if (FD_ISSET(channelfd, &readfds)) {
wp_log(WP_DEBUG, "Readfd isset\n");
ssize_t nbytes;
if (read(channelfd, &nbytes, sizeof(ssize_t)) <
(ssize_t)sizeof(ssize_t)) {
wp_log(WP_ERROR, "FD header read failure: %s\n",
strerror(errno));
char *tmpbuf;
ssize_t nbytes = read_size_then_buf(channelfd, &tmpbuf);
if (nbytes == 0) {
wp_log(WP_ERROR,
"channel read connection closed\n");
break;
}
char *tmpbuf = calloc(nbytes, 1);
ssize_t nread = 0;
while (nread < nbytes) {
ssize_t nr = read(channelfd, tmpbuf + nread,
nbytes - nread);
if (nr <= 0) {
break;
}
nread += nr;
}
if (nread < nbytes) {
wp_log(WP_ERROR,
"FD body read failure %ld/%ld: %s\n",
nread, nbytes, strerror(errno));
if (nbytes == -1) {
wp_log(WP_ERROR, "channel read failure: %s\n",
strerror(errno));
break;
}
char *waymsg;
int waylen;
int nids;
......@@ -186,41 +176,53 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
untranslate_ids(&fdtransmap, nids, ids, fds);
wp_log(WP_DEBUG, "Read from conn %d = %d bytes\n",
nread, nbytes);
nbytes, nbytes);
int wc = iovec_write(client_socket, waymsg, waylen, fds,
nids);
free(tmpbuf);
if (wc == -1) {
wp_log(WP_ERROR, "FD Write failure %d: %s\n",
wc, strerror(errno));
break;
}
free(tmpbuf);
}
if (FD_ISSET(client_socket, &readfds)) {
wp_log(WP_DEBUG, "client socket isset\n");
int rc = iovec_read(client_socket, buffer, maxmsg, NULL,
NULL);
int fdbuf[28];
int nfds = 28;
int rc = iovec_read(client_socket, buffer, maxmsg,
fdbuf, &nfds);
if (rc == -1) {
wp_log(WP_ERROR, "CS Read failure %ld: %s\n",
rc, strerror(errno));
break;
}
if (rc > 0) {
struct muxheader header = {
.metadata = 0, .length = rc};
if (write(channelfd, &header, sizeof(header)) ==
-1) {
wp_log(WP_ERROR,
"CS write header failure: %s\n",
strerror(errno));
break;
}
if (write(channelfd, buffer, rc) == -1) {
int ids[28];
translate_fds(&fdtransmap, nfds, fdbuf, ids);
int ntransfers;
struct transfer transfers[50];
collect_updates(&fdtransmap, &ntransfers,
transfers);
char *msg = NULL;
size_t msglen;
pack_pipe_message(&msglen, &msg, rc, buffer,
nfds, ids, ntransfers,
transfers);
wp_log(WP_DEBUG,
"Packed message size (%d fds): %ld\n",
nfds, msglen);
if (write(channelfd, msg, msglen) == -1) {
free(msg);
wp_log(WP_ERROR,
"CS write body failure: %s\n",
"CS msg write failure: %s\n",
strerror(errno));
break;
}
free(msg);
} else {
wp_log(WP_DEBUG, "the client shut down\n");
break;
......
......@@ -2,9 +2,9 @@
root=`pwd`
#program=`which bash`
# program=`which weston-flower`
program=`which demo.py`
program=`which bash`
program=`which weston-flower`
#program=`which demo.py`
($root/waypipe -d client /tmp/socket-client 2>&1 | sed 's/.*/\x1b[33m&\x1b[0m/') &
# ssh-to-self; should have a local keypair set up
......
......@@ -99,7 +99,7 @@ int iovec_write(int conn, const char *buf, size_t buflen, const int *fds,
{
struct iovec the_iovec;
the_iovec.iov_len = buflen;
the_iovec.iov_base = buf;
the_iovec.iov_base = (char *)buf;
struct msghdr msg;
msg.msg_name = NULL;
msg.msg_namelen = 0;
......@@ -254,6 +254,7 @@ void pack_pipe_message(size_t *msglen, char **msg, int waylen,
void *data = calloc(size, 1);
size_t *cursor = data;
*cursor++ = size - sizeof(size_t); // size excluding this header
wp_log(WP_DEBUG, "Packing %ld bytes as tail\n", size - sizeof(size_t));
for (int i = 0; i < nids; i++) {
int *sd = (int *)cursor;
sd[0] = ids[i];
......@@ -285,6 +286,7 @@ void unpack_pipe_message(size_t msglen, const char *msg, int *waylen,
char **waymsg, int *nids, int ids[], int *ntransfers,
struct transfer transfers[])
{
(void)msglen;
int ni = 0, nt = 0;
size_t *cursor = (size_t *)msg;
while (true) {
......@@ -400,3 +402,32 @@ void apply_updates(struct fd_translation_map *map, int ntransfers,
apply_update(map, &transfers[i]);
}
}
ssize_t read_size_then_buf(int fd, char **msg)
{
*msg = NULL;
ssize_t nbytes = 0;
ssize_t nrc = read(fd, &nbytes, sizeof(ssize_t));
if (nrc == 0) {
return 0;
}
if (nrc < (ssize_t)sizeof(ssize_t)) {
return -1;
}
wp_log(WP_DEBUG, "rstb %ld\n", nbytes);
char *tmpbuf = calloc(nbytes, 1);
ssize_t nread = 0;
while (nread < nbytes) {
ssize_t nr = read(fd, tmpbuf + nread, nbytes - nread);
if (nr <= 0) {
break;
}
nread += nr;
}
if (nread < nbytes) {
free(tmpbuf);
return -1;
}
*msg = tmpbuf;
return nbytes;
}
......@@ -28,6 +28,7 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
int iovec_read(int socket, char *buf, size_t buflen, int *fds, int *numfds);
int iovec_write(int conn, const char *buf, size_t buflen, const int *fds,
......@@ -47,16 +48,6 @@ const char *static_timestamp(void);
fprintf(stderr, "%s [%s:%3d] " fmt, static_timestamp(), __FILE__, \
__LINE__, ##__VA_ARGS__)
// TODO: find a simple/fast binary protocol to communicate
// file data replacements; [Net-length;] [NFDS, [ID, LENGTH [DATA]], orig
// message] the 'net-length' makes the following read run straightforward. if
// sub-length > 0, then we are done.
struct muxheader {
int metadata;
int length;
};
struct fd_translation_map {
struct shadow_fd *list;
int max_local_id;
......@@ -109,4 +100,7 @@ void untranslate_ids(struct fd_translation_map *map, int nids, const int ids[],
void apply_updates(struct fd_translation_map *map, int ntransfers,
const struct transfer transfers[]);
/** Read the contents of a packed message into a newly allocated buffer */
ssize_t read_size_then_buf(int fd, char **msg);
#endif // WAYPIPE_UTIL_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment