From f1f4c483fe069bb5205af783ab7286e199e13e50 Mon Sep 17 00:00:00 2001 From: archie Date: Tue, 20 Jun 2000 18:51:38 +0000 Subject: When the 'msg' command is used from the command line, check for a synchronous reply, and display it (if any) before exiting. Requested by: phk --- usr.sbin/ngctl/main.c | 49 ++++----------------------------------- usr.sbin/ngctl/msg.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++- usr.sbin/ngctl/ngctl.h | 4 ++++ 3 files changed, 70 insertions(+), 45 deletions(-) diff --git a/usr.sbin/ngctl/main.c b/usr.sbin/ngctl/main.c index 70080f9..67ffde3 100644 --- a/usr.sbin/ngctl/main.c +++ b/usr.sbin/ngctl/main.c @@ -52,7 +52,6 @@ static int DoCommand(int ac, char **av); static int DoInteractive(void); static const struct ngcmd *FindCommand(const char *string); static int MatchCommand(const struct ngcmd *cmd, const char *s); -static void DumpAscii(const u_char *buf, int len); static void Usage(const char *msg); static int ReadCmd(int ac, char **av); static int HelpCmd(int ac, char **av); @@ -229,48 +228,11 @@ DoInteractive(void) } /* Display any incoming control message */ - while (FD_ISSET(csock, &rfds)) { - u_char buf[2 * sizeof(struct ng_mesg) + 8192]; - struct ng_mesg *const m = (struct ng_mesg *)buf; - struct ng_mesg *const ascii = (struct ng_mesg *)m->data; - char path[NG_PATHLEN+1]; - - /* Get incoming message (in binary form) */ - if (NgRecvMsg(csock, m, sizeof(buf), path) < 0) { - warn("recv incoming message"); - break; - } - - /* Ask originating node to convert to ASCII */ - if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, - NGM_BINARY2ASCII, m, - sizeof(*m) + m->header.arglen) < 0 - || NgRecvMsg(csock, m, sizeof(buf), NULL) < 0) { - printf("Rec'd %s %d from \"%s\":\n", - (m->header.flags & NGF_RESP) != 0 ? - "response" : "command", - m->header.cmd, path); - if (m->header.arglen == 0) - printf("No arguments\n"); - else - DumpAscii(m->data, m->header.arglen); - break; - } - - /* Display message in ASCII form */ - printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", - (ascii->header.flags & NGF_RESP) != 0 ? - "response" : "command", - ascii->header.cmdstr, ascii->header.cmd, path); - if (*ascii->data != '\0') - printf("Args:\t%s\n", ascii->data); - else - printf("No arguments\n"); - break; - } + if (FD_ISSET(csock, &rfds)) + MsgRead(); /* Display any incoming data packet */ - while (FD_ISSET(dsock, &rfds)) { + if (FD_ISSET(dsock, &rfds)) { u_char buf[8192]; char hook[NG_HOOKLEN + 1]; int rl; @@ -278,14 +240,13 @@ DoInteractive(void) /* Read packet from socket */ if ((rl = NgRecvData(dsock, buf, sizeof(buf), hook)) < 0) - err(EX_OSERR, "read(hook)"); + err(EX_OSERR, "reading hook \"%s\"", hook); if (rl == 0) errx(EX_OSERR, "EOF from hook \"%s\"?", hook); /* Write packet to stdout */ printf("Rec'd data packet on hook \"%s\":\n", hook); DumpAscii(buf, rl); - break; } /* Get any user input */ @@ -495,7 +456,7 @@ QuitCmd(int ac, char **av) /* * Dump data in hex and ASCII form */ -static void +void DumpAscii(const u_char *buf, int len) { char ch, sbuf[100]; diff --git a/usr.sbin/ngctl/msg.c b/usr.sbin/ngctl/msg.c index 926024e..4ae5171 100644 --- a/usr.sbin/ngctl/msg.c +++ b/usr.sbin/ngctl/msg.c @@ -40,7 +40,7 @@ #include "ngctl.h" -#define BUF_SIZE 1024 +#define BUF_SIZE 4096 static int MsgCmd(int ac, char **av); @@ -80,7 +80,67 @@ MsgCmd(int ac, char **av) return(CMDRTN_ERROR); } + /* See if a synchronous reply awaits */ + { + struct timeval tv; + fd_set rfds; + + FD_ZERO(&rfds); + FD_SET(csock, &rfds); + memset(&tv, 0, sizeof(tv)); + switch (select(csock + 1, &rfds, NULL, NULL, &tv)) { + case -1: + err(EX_OSERR, "select"); + case 0: + break; + default: + MsgRead(); + break; + } + } + /* Done */ return(CMDRTN_OK); } +/* + * Read and display the next incoming control message + */ +void +MsgRead() +{ + u_char buf[2 * sizeof(struct ng_mesg) + BUF_SIZE]; + struct ng_mesg *const m = (struct ng_mesg *)buf; + struct ng_mesg *const ascii = (struct ng_mesg *)m->data; + char path[NG_PATHLEN+1]; + + /* Get incoming message (in binary form) */ + if (NgRecvMsg(csock, m, sizeof(buf), path) < 0) { + warn("recv incoming message"); + return; + } + + /* Ask originating node to convert message to ASCII */ + if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, + NGM_BINARY2ASCII, m, sizeof(*m) + m->header.arglen) < 0 + || NgRecvMsg(csock, m, sizeof(buf), NULL) < 0) { + printf("Rec'd %s %d from \"%s\":\n", + (m->header.flags & NGF_RESP) != 0 ? "response" : "command", + m->header.cmd, path); + if (m->header.arglen == 0) + printf("No arguments\n"); + else + DumpAscii(m->data, m->header.arglen); + return; + } + + /* Display message in ASCII form */ + printf("Rec'd %s \"%s\" (%d) from \"%s\":\n", + (ascii->header.flags & NGF_RESP) != 0 ? "response" : "command", + ascii->header.cmdstr, ascii->header.cmd, path); + if (*ascii->data != '\0') + printf("Args:\t%s\n", ascii->data); + else + printf("No arguments\n"); +} + diff --git a/usr.sbin/ngctl/ngctl.h b/usr.sbin/ngctl/ngctl.h index 1baced7..b66e74b 100644 --- a/usr.sbin/ngctl/ngctl.h +++ b/usr.sbin/ngctl/ngctl.h @@ -93,3 +93,7 @@ extern const struct ngcmd quit_cmd; /* Data and control sockets */ extern int csock, dsock; +/* Misc functions */ +extern void MsgRead(void); +extern void DumpAscii(const u_char *buf, int len); + -- cgit v1.1