1
0
mirror of https://git.FreeBSD.org/src.git synced 2025-01-24 16:10:11 +00:00

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
This commit is contained in:
Archie Cobbs 2000-06-20 18:51:38 +00:00
parent a42af91cba
commit 6ebb8ebbd1
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=61880
3 changed files with 70 additions and 45 deletions

View File

@ -52,7 +52,6 @@ static int DoCommand(int ac, char **av);
static int DoInteractive(void); static int DoInteractive(void);
static const struct ngcmd *FindCommand(const char *string); static const struct ngcmd *FindCommand(const char *string);
static int MatchCommand(const struct ngcmd *cmd, const char *s); 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 void Usage(const char *msg);
static int ReadCmd(int ac, char **av); static int ReadCmd(int ac, char **av);
static int HelpCmd(int ac, char **av); static int HelpCmd(int ac, char **av);
@ -229,48 +228,11 @@ DoInteractive(void)
} }
/* Display any incoming control message */ /* Display any incoming control message */
while (FD_ISSET(csock, &rfds)) { if (FD_ISSET(csock, &rfds))
u_char buf[2 * sizeof(struct ng_mesg) + 8192]; MsgRead();
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;
}
/* Display any incoming data packet */ /* Display any incoming data packet */
while (FD_ISSET(dsock, &rfds)) { if (FD_ISSET(dsock, &rfds)) {
u_char buf[8192]; u_char buf[8192];
char hook[NG_HOOKLEN + 1]; char hook[NG_HOOKLEN + 1];
int rl; int rl;
@ -278,14 +240,13 @@ DoInteractive(void)
/* Read packet from socket */ /* Read packet from socket */
if ((rl = NgRecvData(dsock, if ((rl = NgRecvData(dsock,
buf, sizeof(buf), hook)) < 0) buf, sizeof(buf), hook)) < 0)
err(EX_OSERR, "read(hook)"); err(EX_OSERR, "reading hook \"%s\"", hook);
if (rl == 0) if (rl == 0)
errx(EX_OSERR, "EOF from hook \"%s\"?", hook); errx(EX_OSERR, "EOF from hook \"%s\"?", hook);
/* Write packet to stdout */ /* Write packet to stdout */
printf("Rec'd data packet on hook \"%s\":\n", hook); printf("Rec'd data packet on hook \"%s\":\n", hook);
DumpAscii(buf, rl); DumpAscii(buf, rl);
break;
} }
/* Get any user input */ /* Get any user input */
@ -495,7 +456,7 @@ QuitCmd(int ac, char **av)
/* /*
* Dump data in hex and ASCII form * Dump data in hex and ASCII form
*/ */
static void void
DumpAscii(const u_char *buf, int len) DumpAscii(const u_char *buf, int len)
{ {
char ch, sbuf[100]; char ch, sbuf[100];

View File

@ -40,7 +40,7 @@
#include "ngctl.h" #include "ngctl.h"
#define BUF_SIZE 1024 #define BUF_SIZE 4096
static int MsgCmd(int ac, char **av); static int MsgCmd(int ac, char **av);
@ -80,7 +80,67 @@ MsgCmd(int ac, char **av)
return(CMDRTN_ERROR); 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 */ /* Done */
return(CMDRTN_OK); 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");
}

View File

@ -93,3 +93,7 @@ extern const struct ngcmd quit_cmd;
/* Data and control sockets */ /* Data and control sockets */
extern int csock, dsock; extern int csock, dsock;
/* Misc functions */
extern void MsgRead(void);
extern void DumpAscii(const u_char *buf, int len);