ifstated monitors interface state and the return state of invoked commands,
and takes action accordingly, all of which is managed with the help of a
finite state machine. That makes for a lot of "state" references in the code.
The following diff renames variables to make a distinction between link status
and command execution status (now referenced as "status"), and the various
states
within the state machine (referred to as "state").
As an example of the current confusion, the link_states() function in parse.y
actually links states within the finite state machine (as oppose to having
anything to do with interface link states).
This diff helps to make my head hurt less.
For your consideration.
Regards,
Rob
Index: ifstated.c
===================================================================
RCS file: /cvs/src/usr.sbin/ifstated/ifstated.c,v
retrieving revision 1.49
diff -u -p -r1.49 ifstated.c
--- ifstated.c 3 Jul 2017 18:45:34 -0000 1.49
+++ ifstated.c 3 Jul 2017 19:59:43 -0000
@@ -18,7 +18,7 @@
*/
/*
- * ifstated listens to link_state transitions on interfaces
+ * ifstated listens to link status transitions on interfaces
* and executes predefined commands.
*/
@@ -62,9 +62,9 @@ void external_handler(int, short, void
void external_exec(struct ifsd_external *, int);
void check_external_status(struct ifsd_state *);
void external_evtimer_setup(struct ifsd_state *, int);
-void scan_ifstate(int, int, int);
-int scan_ifstate_single(int, int, struct ifsd_state *);
-void fetch_state(void);
+void scan_ifstatus(int, int, int);
+int scan_ifstatus_single(int, int, struct ifsd_state *);
+void fetch_ifstatus(void);
__dead void usage(void);
void adjust_expressions(struct ifsd_expression_list *, int);
void adjust_external_expressions(struct ifsd_state *);
@@ -208,7 +208,7 @@ load_config(void)
clear_config(conf);
conf = newconf;
conf->initstate.entered = time(NULL);
- fetch_state();
+ fetch_ifstatus();
external_evtimer_setup(&conf->initstate, IFSD_EVTIMER_ADD);
adjust_external_expressions(&conf->initstate);
eval_state(&conf->initstate);
@@ -246,7 +246,7 @@ rt_msg_handler(int fd, short event, void
return;
memcpy(&ifm, rtm, sizeof(ifm));
- scan_ifstate(ifm.ifm_index, ifm.ifm_data.ifi_link_state, 1);
+ scan_ifstatus(ifm.ifm_index, ifm.ifm_data.ifi_link_state, 1);
}
void
@@ -419,37 +419,37 @@ external_evtimer_setup(struct ifsd_state
#define LINK_STATE_IS_DOWN(_s) (!LINK_STATE_IS_UP((_s)))
int
-scan_ifstate_single(int ifindex, int s, struct ifsd_state *state)
+scan_ifstatus_single(int ifindex, int s, struct ifsd_state *state)
{
- struct ifsd_ifstate *ifstate;
+ struct ifsd_ifstatus *ifstatus;
struct ifsd_expression_list expressions;
int changed = 0;
TAILQ_INIT(&expressions);
- TAILQ_FOREACH(ifstate, &state->interface_states, entries) {
- if (ifstate->ifindex == ifindex) {
- if (ifstate->prevstate != s &&
- (ifstate->prevstate != -1 || !opt_inhibit)) {
+ TAILQ_FOREACH(ifstatus, &state->interfaces_status, entries) {
+ if (ifstatus->ifindex == ifindex) {
+ if (ifstatus->prevstatus != s &&
+ (ifstatus->prevstatus != -1 || !opt_inhibit)) {
struct ifsd_expression *expression;
int truth;
truth =
- (ifstate->ifstate == IFSD_LINKUNKNOWN &&
+ (ifstatus->ifstatus == IFSD_LINKUNKNOWN &&
s == LINK_STATE_UNKNOWN) ||
- (ifstate->ifstate == IFSD_LINKDOWN &&
+ (ifstatus->ifstatus == IFSD_LINKDOWN &&
LINK_STATE_IS_DOWN(s)) ||
- (ifstate->ifstate == IFSD_LINKUP &&
+ (ifstatus->ifstatus == IFSD_LINKUP &&
LINK_STATE_IS_UP(s));
TAILQ_FOREACH(expression,
- &ifstate->expressions, entries) {
+ &ifstatus->expressions, entries) {
expression->truth = truth;
TAILQ_INSERT_TAIL(&expressions,
expression, eval);
changed = 1;
}
- ifstate->prevstate = s;
+ ifstatus->prevstatus = s;
}
}
}
@@ -460,15 +460,15 @@ scan_ifstate_single(int ifindex, int s,
}
void
-scan_ifstate(int ifindex, int s, int do_eval)
+scan_ifstatus(int ifindex, int s, int do_eval)
{
struct ifsd_state *state;
int cur_eval = 0;
- if (scan_ifstate_single(ifindex, s, &conf->initstate) && do_eval)
+ if (scan_ifstatus_single(ifindex, s, &conf->initstate) && do_eval)
eval_state(&conf->initstate);
TAILQ_FOREACH(state, &conf->states, entries) {
- if (scan_ifstate_single(ifindex, s, state) &&
+ if (scan_ifstatus_single(ifindex, s, state) &&
(do_eval && state == conf->curstate))
cur_eval = 1;
}
@@ -593,11 +593,8 @@ do_action(struct ifsd_action *action)
}
}
-/*
- * Fetch the current link states.
- */
void
-fetch_state(void)
+fetch_ifstatus(void)
{
struct ifaddrs *ifap, *ifa;
char *oname = NULL;
@@ -620,7 +617,7 @@ fetch_state(void)
if (ioctl(sock, SIOCGIFDATA, (caddr_t)&ifr) == -1)
continue;
- scan_ifstate(if_nametoindex(ifa->ifa_name),
+ scan_ifstatus(if_nametoindex(ifa->ifa_name),
ifrdat.ifi_link_state, 0);
}
freeifaddrs(ifap);
@@ -682,13 +679,13 @@ remove_expression(struct ifsd_expression
struct ifsd_state *state)
{
switch (expression->type) {
- case IFSD_OPER_IFSTATE:
- TAILQ_REMOVE(&expression->u.ifstate->expressions, expression,
+ case IFSD_OPER_IFSTATUS:
+ TAILQ_REMOVE(&expression->u.ifstatus->expressions, expression,
entries);
- if (--expression->u.ifstate->refcount == 0) {
- TAILQ_REMOVE(&state->interface_states,
- expression->u.ifstate, entries);
- free(expression->u.ifstate);
+ if (--expression->u.ifstatus->refcount == 0) {
+ TAILQ_REMOVE(&state->interfaces_status,
+ expression->u.ifstatus, entries);
+ free(expression->u.ifstatus);
}
break;
case IFSD_OPER_EXTERNAL:
Index: ifstated.h
===================================================================
RCS file: /cvs/src/usr.sbin/ifstated/ifstated.h,v
retrieving revision 1.15
diff -u -p -r1.15 ifstated.h
--- ifstated.h 2 Jul 2017 15:28:26 -0000 1.15
+++ ifstated.h 3 Jul 2017 19:59:43 -0000
@@ -32,14 +32,14 @@
struct ifsd_expression;
TAILQ_HEAD(ifsd_expression_list, ifsd_expression);
-struct ifsd_ifstate {
- TAILQ_ENTRY(ifsd_ifstate) entries;
+struct ifsd_ifstatus {
+ TAILQ_ENTRY(ifsd_ifstatus) entries;
struct ifsd_expression_list expressions;
- int ifstate;
+ int ifstatus;
#define IFSD_LINKUNKNOWN 0
#define IFSD_LINKDOWN 1
#define IFSD_LINKUP 2
- int prevstate;
+ int prevstatus;
u_int32_t refcount;
u_short ifindex;
};
@@ -87,7 +87,7 @@ struct ifsd_expression {
struct ifsd_expression *left;
struct ifsd_expression *right;
union {
- struct ifsd_ifstate *ifstate;
+ struct ifsd_ifstatus *ifstatus;
struct ifsd_external *external;
} u;
int depth;
@@ -96,16 +96,16 @@ struct ifsd_expression {
#define IFSD_OPER_OR 2
#define IFSD_OPER_NOT 3
#define IFSD_OPER_EXTERNAL 4
-#define IFSD_OPER_IFSTATE 5
+#define IFSD_OPER_IFSTATUS 5
u_int8_t truth;
};
-TAILQ_HEAD(ifsd_ifstate_list, ifsd_ifstate);
+TAILQ_HEAD(ifsd_ifstatus_list, ifsd_ifstatus);
TAILQ_HEAD(ifsd_external_list, ifsd_external);
struct ifsd_state {
struct event ev;
- struct ifsd_ifstate_list interface_states;
+ struct ifsd_ifstatus_list interfaces_status;
struct ifsd_external_list external_tests;
TAILQ_ENTRY(ifsd_state) entries;
struct ifsd_action *init;
Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/ifstated/parse.y,v
retrieving revision 1.43
diff -u -p -r1.43 parse.y
--- parse.y 2 Jul 2017 15:28:26 -0000 1.43
+++ parse.y 3 Jul 2017 19:59:43 -0000
@@ -85,7 +85,7 @@ struct ifsd_state *curstate = NULL;
void link_states(struct ifsd_action *);
void set_expression_depth(struct ifsd_expression *, int);
void init_state(struct ifsd_state *);
-struct ifsd_ifstate *new_ifstate(u_short, int);
+struct ifsd_ifstatus *new_ifstatus(u_short, int);
struct ifsd_external *new_external(char *, u_int32_t);
typedef struct {
@@ -96,7 +96,7 @@ typedef struct {
u_short interface;
struct ifsd_expression *expression;
- struct ifsd_ifstate *ifstate;
+ struct ifsd_ifstatus *ifstatus;
struct ifsd_external *external;
} v;
@@ -115,7 +115,7 @@ typedef struct {
%token <v.number> NUMBER
%type <v.string> string
%type <v.interface> interface
-%type <v.ifstate> if_test
+%type <v.ifstatus> if_test
%type <v.external> ext_test
%type <v.expression> expr term
%%
@@ -252,13 +252,13 @@ init : INIT {
;
if_test : interface '.' LINK '.' UP {
- $$ = new_ifstate($1, IFSD_LINKUP);
+ $$ = new_ifstatus($1, IFSD_LINKUP);
}
| interface '.' LINK '.' DOWN {
- $$ = new_ifstate($1, IFSD_LINKDOWN);
+ $$ = new_ifstatus($1, IFSD_LINKDOWN);
}
| interface '.' LINK '.' UNKNOWN {
- $$ = new_ifstate($1, IFSD_LINKUNKNOWN);
+ $$ = new_ifstatus($1, IFSD_LINKUNKNOWN);
}
;
@@ -277,8 +277,8 @@ term : if_test {
if (($$ = calloc(1, sizeof(*$$))) == NULL)
err(1, NULL);
curaction->act.c.expression = $$;
- $$->type = IFSD_OPER_IFSTATE;
- $$->u.ifstate = $1;
+ $$->type = IFSD_OPER_IFSTATUS;
+ $$->u.ifstatus = $1;
TAILQ_INSERT_TAIL(&$1->expressions, $$, entries);
}
| ext_test {
@@ -920,7 +920,7 @@ set_expression_depth(struct ifsd_express
void
init_state(struct ifsd_state *state)
{
- TAILQ_INIT(&state->interface_states);
+ TAILQ_INIT(&state->interfaces_status);
TAILQ_INIT(&state->external_tests);
if ((state->init = calloc(1, sizeof(*state->init))) == NULL)
@@ -934,10 +934,10 @@ init_state(struct ifsd_state *state)
TAILQ_INIT(&state->body->act.c.actions);
}
-struct ifsd_ifstate *
-new_ifstate(u_short ifindex, int s)
+struct ifsd_ifstatus *
+new_ifstatus(u_short ifindex, int s)
{
- struct ifsd_ifstate *ifstate = NULL;
+ struct ifsd_ifstatus *ifstatus = NULL;
struct ifsd_state *state;
if (curstate != NULL)
@@ -945,20 +945,20 @@ new_ifstate(u_short ifindex, int s)
else
state = &conf->initstate;
- TAILQ_FOREACH(ifstate, &state->interface_states, entries)
- if (ifstate->ifindex == ifindex && ifstate->ifstate == s)
+ TAILQ_FOREACH(ifstatus, &state->interfaces_status, entries)
+ if (ifstatus->ifindex == ifindex && ifstatus->ifstatus == s)
break;
- if (ifstate == NULL) {
- if ((ifstate = calloc(1, sizeof(*ifstate))) == NULL)
+ if (ifstatus == NULL) {
+ if ((ifstatus = calloc(1, sizeof(*ifstatus))) == NULL)
err(1, NULL);
- ifstate->ifindex = ifindex;
- ifstate->ifstate = s;
- TAILQ_INIT(&ifstate->expressions);
- TAILQ_INSERT_TAIL(&state->interface_states, ifstate, entries);
- }
- ifstate->prevstate = -1;
- ifstate->refcount++;
- return (ifstate);
+ ifstatus->ifindex = ifindex;
+ ifstatus->ifstatus = s;
+ TAILQ_INIT(&ifstatus->expressions);
+ TAILQ_INSERT_TAIL(&state->interfaces_status, ifstatus, entries);
+ }
+ ifstatus->prevstatus = -1;
+ ifstatus->refcount++;
+ return (ifstatus);
}
struct ifsd_external *