Commit 7b4b5515 authored by Manuel Stoeckl's avatar Manuel Stoeckl
Browse files

Clean up debug messages, rename variables

parent 5e627c2c
......@@ -64,7 +64,7 @@ static int run_client_child(int chanfd, const char *socket_path)
wp_log(WP_ERROR, "Failed to connect to a wayland server.\n");
return EXIT_FAILURE;
}
int displayfd = wl_display_get_fd(display);
int dispfd = wl_display_get_fd(display);
struct fd_translation_map fdtransmap = {
.local_sign = 1, .list = NULL, .max_local_id = 1};
......@@ -76,30 +76,27 @@ static int run_client_child(int chanfd, const char *socket_path)
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(chanfd, &readfds);
FD_SET(displayfd, &readfds);
FD_SET(dispfd, &readfds);
struct timespec timeout = {.tv_sec = 0, .tv_nsec = 700000000L};
int maxfd = chanfd > displayfd ? chanfd : displayfd;
int maxfd = chanfd > dispfd ? chanfd : dispfd;
int r = pselect(maxfd + 1, &readfds, NULL, NULL, &timeout,
NULL);
if (r == -1) {
wp_log(WP_ERROR, "Select failed, stopping\n");
break;
}
wp_log(WP_DEBUG, "Post select %d %d %d\n", r,
FD_ISSET(chanfd, &readfds),
FD_ISSET(displayfd, &readfds));
if (FD_ISSET(chanfd, &readfds)) {
wp_log(WP_DEBUG, "chanclient isset\n");
char *tmpbuf;
wp_log(WP_DEBUG, "Channel read begun\n");
ssize_t nbytes = read_size_then_buf(chanfd, &tmpbuf);
if (nbytes == 0) {
wp_log(WP_ERROR,
"channel read connection closed\n");
"Channel read connection closed\n");
break;
}
if (nbytes == -1) {
wp_log(WP_ERROR, "channel read failure: %s\n",
wp_log(WP_ERROR, "Channel read failure: %s\n",
strerror(errno));
break;
}
......@@ -114,31 +111,32 @@ static int run_client_child(int chanfd, const char *socket_path)
&waymsg, &nids, ids, &ntransfers,
transfers);
wp_log(WP_DEBUG,
"Read %ld byte msg, %d fds, %d transfers\n",
nbytes, nids, ntransfers);
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);
ssize_t wc = iovec_write(
dispfd, waymsg, waylen, fds, nids);
free(tmpbuf);
if (wc == -1) {
wp_log(WP_ERROR, "FD Write failure %d: %s\n",
wp_log(WP_ERROR,
"dispfd write failure %d: %s\n",
wc, strerror(errno));
break;
}
wp_log(WP_DEBUG, "client done\n");
}
if (FD_ISSET(displayfd, &readfds)) {
wp_log(WP_DEBUG, "displayfd isset\n");
if (FD_ISSET(dispfd, &readfds)) {
int fdbuf[28];
int nfds = 28;
int rc = iovec_read(displayfd, buffer, maxmsg, fdbuf,
&nfds);
ssize_t rc = iovec_read(
dispfd, buffer, maxmsg, fdbuf, &nfds);
if (rc == -1) {
wp_log(WP_ERROR, "CS Read failure %ld: %s\n",
rc, strerror(errno));
......@@ -158,19 +156,21 @@ static int run_client_child(int chanfd, const char *socket_path)
nfds, ids, ntransfers,
transfers);
wp_log(WP_DEBUG,
"Packed message size (%d fds): %ld\n",
nfds, msglen);
"Packed message size (%d fds, %d transfers): %ld\n",
nfds, ntransfers, msglen);
if (write(chanfd, msg, msglen) == -1) {
free(msg);
wp_log(WP_ERROR,
"CC msg write failure: %s\n",
"chanfd write failure: %s\n",
strerror(errno));
break;
}
free(msg);
wp_log(WP_DEBUG, "Channel write complete\n");
} else {
wp_log(WP_DEBUG, "the display shut down\n");
wp_log(WP_DEBUG, "The display shut down\n");
break;
}
}
......@@ -183,7 +183,7 @@ static int run_client_child(int chanfd, const char *socket_path)
wp_log(WP_DEBUG, "...\n");
wp_log(WP_DEBUG, "Closing client\n");
close(displayfd);
close(dispfd);
wl_display_disconnect(display);
......
......@@ -84,7 +84,7 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
int status;
struct sockaddr_un saddr;
int channelfd;
int chanfd;
if (strlen(socket_path) >= sizeof(saddr.sun_path)) {
wp_log(WP_ERROR,
......@@ -95,22 +95,21 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
saddr.sun_family = AF_UNIX;
strncpy(saddr.sun_path, socket_path, sizeof(saddr.sun_path) - 1);
channelfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (channelfd == -1) {
chanfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (chanfd == -1) {
wp_log(WP_ERROR, "Error creating socket: %s\n",
strerror(errno));
return EXIT_FAILURE;
}
if (connect(channelfd, (struct sockaddr *)&saddr, sizeof(saddr)) ==
-1) {
if (connect(chanfd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
wp_log(WP_ERROR, "Error connecting socket: %s\n",
strerror(errno));
close(channelfd);
close(chanfd);
return EXIT_FAILURE;
}
// A connection has already been established
int client_socket = csockpair[0];
int appfd = csockpair[0];
/** Main select loop:
* fd -> csockpair[0]
......@@ -128,10 +127,9 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
iter++;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(channelfd, &readfds);
FD_SET(client_socket, &readfds);
int maxfd = channelfd > client_socket ? channelfd
: client_socket;
FD_SET(chanfd, &readfds);
FD_SET(appfd, &readfds);
int maxfd = chanfd > appfd ? chanfd : appfd;
int r = pselect(maxfd + 1, &readfds, NULL, NULL, &timeout,
NULL);
if (r < 0) {
......@@ -140,21 +138,17 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
return EXIT_FAILURE;
}
wp_log(WP_DEBUG,
"Post select %d channelfd=%d client_socket=%d\n",
r, FD_ISSET(channelfd, &readfds),
FD_ISSET(client_socket, &readfds));
if (FD_ISSET(channelfd, &readfds)) {
wp_log(WP_DEBUG, "Readfd isset\n");
if (FD_ISSET(chanfd, &readfds)) {
wp_log(WP_DEBUG, "Channel read begun\n");
char *tmpbuf;
ssize_t nbytes = read_size_then_buf(channelfd, &tmpbuf);
ssize_t nbytes = read_size_then_buf(chanfd, &tmpbuf);
if (nbytes == 0) {
wp_log(WP_ERROR,
"channel read connection closed\n");
"Channel read connection closed\n");
break;
}
if (nbytes == -1) {
wp_log(WP_ERROR, "channel read failure: %s\n",
wp_log(WP_ERROR, "Channel read failure: %s\n",
strerror(errno));
break;
}
......@@ -169,6 +163,10 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
&waymsg, &nids, ids, &ntransfers,
transfers);
wp_log(WP_DEBUG,
"Read %ld byte msg, %d fds, %d transfers\n",
nbytes, nids, ntransfers);
apply_updates(&fdtransmap, ntransfers, transfers);
int fds[28];
......@@ -177,8 +175,8 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
wp_log(WP_DEBUG, "Read from conn %d = %d bytes\n",
nbytes, nbytes);
int wc = iovec_write(client_socket, waymsg, waylen, fds,
nids);
ssize_t wc = iovec_write(
appfd, waymsg, waylen, fds, nids);
free(tmpbuf);
if (wc == -1) {
wp_log(WP_ERROR, "FD Write failure %d: %s\n",
......@@ -186,15 +184,15 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
break;
}
}
if (FD_ISSET(client_socket, &readfds)) {
if (FD_ISSET(appfd, &readfds)) {
wp_log(WP_DEBUG, "client socket isset\n");
int fdbuf[28];
int nfds = 28;
int rc = iovec_read(client_socket, buffer, maxmsg,
fdbuf, &nfds);
ssize_t rc = iovec_read(
appfd, buffer, maxmsg, fdbuf, &nfds);
if (rc == -1) {
wp_log(WP_ERROR, "CS Read failure %ld: %s\n",
wp_log(WP_ERROR, "appfd read failure %ld: %s\n",
rc, strerror(errno));
break;
}
......@@ -215,14 +213,16 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
"Packed message size (%d fds): %ld\n",
nfds, msglen);
if (write(channelfd, msg, msglen) == -1) {
if (write(chanfd, msg, msglen) == -1) {
free(msg);
wp_log(WP_ERROR,
"CS msg write failure: %s\n",
"chanfd write failure: %s\n",
strerror(errno));
break;
}
free(msg);
wp_log(WP_DEBUG, "Channel write complete\n");
} else {
wp_log(WP_DEBUG, "the client shut down\n");
break;
......@@ -234,7 +234,7 @@ int run_server(const char *socket_path, int app_argc, char *const app_argv[])
}
}
cleanup_translation_map(&fdtransmap);
close(channelfd);
close(chanfd);
free(buffer);
// todo: scope manipulation, to ensure all cleanups are done
......
......@@ -7,9 +7,13 @@ program=`which weston-terminal`
#program=`which weston-flower`
#program=`which demo.py`
($root/waypipe -d client /tmp/socket-client 2>&1 | sed 's/.*/\x1b[33m&\x1b[0m/') &
debug=
debug=-d
($root/waypipe $debug client /tmp/socket-client 2>&1 | sed 's/.*/\x1b[33m&\x1b[0m/') &
# ssh-to-self; should have a local keypair set up
(ssh -R/tmp/socket-server:/tmp/socket-client localhost $root/waypipe server -d /tmp/socket-server -- $program) 2>&1 | sed 's/.*/\x1b[35m&\x1b[0m/'
(ssh -R/tmp/socket-server:/tmp/socket-client localhost $root/waypipe $debug server /tmp/socket-server -- $program) 2>&1 | sed 's/.*/\x1b[35m&\x1b[0m/'
kill %1
rm -f /tmp/socket-client
rm -f /tmp/socket-server
......@@ -54,7 +54,7 @@ const char *static_timestamp(void)
return msg;
}
int iovec_read(int conn, char *buf, size_t buflen, int *fds, int *numfds)
ssize_t iovec_read(int conn, char *buf, size_t buflen, int *fds, int *numfds)
{
char cmsgdata[(CMSG_LEN(28 * sizeof(int32_t)))];
struct iovec the_iovec;
......@@ -94,7 +94,7 @@ int iovec_read(int conn, char *buf, size_t buflen, int *fds, int *numfds)
}
return ret;
}
int iovec_write(int conn, const char *buf, size_t buflen, const int *fds,
ssize_t iovec_write(int conn, const char *buf, size_t buflen, const int *fds,
int numfds)
{
struct iovec the_iovec;
......@@ -254,7 +254,6 @@ 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];
......@@ -414,7 +413,6 @@ ssize_t read_size_then_buf(int fd, char **msg)
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) {
......
......@@ -30,8 +30,8 @@
#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,
ssize_t iovec_read(int socket, char *buf, size_t buflen, int *fds, int *numfds);
ssize_t iovec_write(int conn, const char *buf, size_t buflen, const int *fds,
int numfds);
typedef enum { WP_DEBUG = 1, WP_ERROR = 2 } log_cat_t;
......
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