Ngctl: support interactive mode.

This commit is contained in:
logwang 2017-11-01 18:53:07 +08:00
parent 3b2bd0f641
commit 555c848918
5 changed files with 44 additions and 8 deletions

View File

@ -1,4 +1,4 @@
SUBDIRS=compat libutil libmemstat libxo libnetgraph sysctl ifconfig route top netstat
SUBDIRS=compat libutil libmemstat libxo libnetgraph sysctl ifconfig route top netstat ngctl
all:
for d in $(SUBDIRS); do ( cd $$d; $(MAKE) all ) ; done

View File

@ -139,6 +139,19 @@ ipsec
For more details, see [Manual page](https://www.freebsd.org/cgi/man.cgi?netstat).
# ngctl
Usage:
```
ngctl -p <f-stack proc_id> [-d] [-f file] [-n name] [command ...]
```
About interactive mode:
- if you have `libedit` in your system, you can turn on `MK_HAVE_LIBEDIT` in `opts.mk`,
the interactive mode will support generic line editing, history functions.
For more details, see [Manual page](https://www.freebsd.org/cgi/man.cgi?ngctl).
# how to implement a custom tool for communicating with F-Stack process
Add a new FF_MSG_TYPE in ff_msg.h:

View File

@ -2,6 +2,7 @@
# $Whistle: Makefile,v 1.3 1999/01/16 00:10:11 archie Exp $
TOPDIR?=${CURDIR}/../..
include ${TOPDIR}/tools/opts.mk
PROG= ngctl
MAN= ngctl.8
@ -11,5 +12,10 @@ WARNS?= 3
LIBADD= netgraph
ifneq (${MK_HAVE_LIBEDIT},"no")
CFLAGS+= -DEDITLINE
LIBS+= -ledit
endif
include ${TOPDIR}/tools/prog.mk

View File

@ -79,9 +79,7 @@ static void ReadSockets(fd_set *);
#endif
static int DoParseCommand(const char *line);
static int DoCommand(int ac, char **av);
#ifndef FSTACK
static int DoInteractive(void);
#endif
static const struct ngcmd *FindCommand(const char *string);
static int MatchCommand(const struct ngcmd *cmd, const char *s);
static void Usage(const char *msg);
@ -89,10 +87,12 @@ static int ReadCmd(int ac, char **av);
static int HelpCmd(int ac, char **av);
static int QuitCmd(int ac, char **av);
#ifdef EDITLINE
#ifndef FSTACK
static volatile sig_atomic_t unblock;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
#endif
#endif
/* List of commands */
static const struct ngcmd *const cmds[] = {
@ -165,9 +165,7 @@ int
main(int ac, char *av[])
{
char name[NG_NODESIZ];
#ifndef FSTACK
int interactive = isatty(0) && isatty(1);
#endif
FILE *fp = NULL;
int ch, rtn = 0;
@ -216,10 +214,8 @@ main(int ac, char *av[])
if (ac == 0) {
if (fp != NULL) {
rtn = ReadFile(fp);
#ifndef FSTACK
} else if (interactive) {
rtn = DoInteractive();
#endif
} else
Usage("no command specified");
} else {
@ -262,8 +258,8 @@ ReadFile(FILE *fp)
return (CMDRTN_OK);
}
#ifndef FSTACK
#ifdef EDITLINE
#ifndef FSTACK
/* Signal handler for Monitor() thread. */
static void
Unblock(int signal __unused)
@ -309,6 +305,7 @@ Monitor(void *v __unused)
return (NULL);
}
#endif
static char *
Prompt(EditLine *el __unused)
@ -331,13 +328,17 @@ Prompt(EditLine *el __unused)
static int
DoInteractive(void)
{
#ifndef FSTACK
pthread_t monitor;
#endif
EditLine *el;
History *hist;
HistEvent hev = { 0, "" };
(*help_cmd.func)(0, NULL);
#ifndef FSTACK
pthread_create(&monitor, NULL, Monitor, NULL);
#endif
el = el_init(getprogname(), stdin, stdout, stderr);
if (el == NULL)
return (CMDRTN_ERROR);
@ -361,19 +362,27 @@ DoInteractive(void)
break;
}
history(hist, &hev, H_ENTER, buf);
#ifndef FSTACK
pthread_kill(monitor, SIGUSR1);
pthread_mutex_lock(&mutex);
#endif
if (DoParseCommand(buf) == CMDRTN_QUIT) {
#ifndef FSTACK
pthread_mutex_unlock(&mutex);
#endif
break;
}
#ifndef FSTACK
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
#endif
}
history_end(hist);
el_end(el);
#ifndef FSTACK
pthread_cancel(monitor);
#endif
return (CMDRTN_QUIT);
}
@ -395,8 +404,10 @@ DoInteractive(void)
/* See if any data or control messages are arriving */
FD_ZERO(&rfds);
#ifndef FSTACK
FD_SET(csock, &rfds);
FD_SET(dsock, &rfds);
#endif
memset(&tv, 0, sizeof(tv));
if (select(maxfd, &rfds, NULL, NULL, &tv) <= 0) {
@ -405,8 +416,10 @@ DoInteractive(void)
fflush(stdout);
FD_ZERO(&rfds);
FD_SET(0, &rfds);
#ifndef FSTACK
FD_SET(csock, &rfds);
FD_SET(dsock, &rfds);
#endif
if (select(maxfd, &rfds, NULL, NULL, NULL) < 0)
err(EX_OSERR, "select");
@ -415,7 +428,9 @@ DoInteractive(void)
printf("\n");
}
#ifndef FSTACK
ReadSockets(&rfds);
#endif
/* Get any user input */
if (FD_ISSET(0, &rfds)) {
@ -433,6 +448,7 @@ DoInteractive(void)
}
#endif /* !EDITLINE */
#ifndef FSTACK
/*
* Read and process data on netgraph control and data sockets.
*/

View File

@ -9,3 +9,4 @@ MK_LAGG_SUPPORT="no"
MK_JAIL="no"
MK_NETGRAPH_SUPPORT="no"
MK_IPSEC_SUPPORT="no"
MK_HAVE_LIBEDIT="no"