Skip to content

Commit

Permalink
Ngctl: support interactive mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
logwang committed Nov 1, 2017
1 parent 3b2bd0f commit 555c848
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tools/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 13 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions tools/ngctl/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,5 +12,10 @@ WARNS?= 3

LIBADD= netgraph

ifneq (${MK_HAVE_LIBEDIT},"no")
CFLAGS+= -DEDITLINE
LIBS+= -ledit
endif

include ${TOPDIR}/tools/prog.mk

30 changes: 23 additions & 7 deletions tools/ngctl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,20 @@ 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);
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[] = {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -309,6 +305,7 @@ Monitor(void *v __unused)

return (NULL);
}
#endif

static char *
Prompt(EditLine *el __unused)
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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) {

Expand All @@ -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");

Expand All @@ -415,7 +428,9 @@ DoInteractive(void)
printf("\n");
}

#ifndef FSTACK
ReadSockets(&rfds);
#endif

/* Get any user input */
if (FD_ISSET(0, &rfds)) {
Expand All @@ -433,6 +448,7 @@ DoInteractive(void)
}
#endif /* !EDITLINE */

#ifndef FSTACK
/*
* Read and process data on netgraph control and data sockets.
*/
Expand Down
1 change: 1 addition & 0 deletions tools/opts.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ MK_LAGG_SUPPORT="no"
MK_JAIL="no"
MK_NETGRAPH_SUPPORT="no"
MK_IPSEC_SUPPORT="no"
MK_HAVE_LIBEDIT="no"

0 comments on commit 555c848

Please sign in to comment.