summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReyk Floeter <reyk@cvs.openbsd.org>2007-02-01 21:01:11 +0000
committerReyk Floeter <reyk@cvs.openbsd.org>2007-02-01 21:01:11 +0000
commit38440244b554d891b09ea6f821edf556b581569a (patch)
treee4fc1a272c6dba42d7cd29e89b7848f270d59098
parent3a114d3a3f35f887545426f819f4a133b67e8aa1 (diff)
modify the imsg monitor to look even nicer and to use a more flexible
API (inspired by the ipsec pfkey monitor). ok pyr@ niallo@
-rw-r--r--usr.sbin/hoststatectl/hoststatectl.c122
-rw-r--r--usr.sbin/relayctl/relayctl.c122
2 files changed, 158 insertions, 86 deletions
diff --git a/usr.sbin/hoststatectl/hoststatectl.c b/usr.sbin/hoststatectl/hoststatectl.c
index 5c6abbd5074..8097ec01497 100644
--- a/usr.sbin/hoststatectl/hoststatectl.c
+++ b/usr.sbin/hoststatectl/hoststatectl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: hoststatectl.c,v 1.10 2007/02/01 20:03:38 pyr Exp $ */
+/* $OpenBSD: hoststatectl.c,v 1.11 2007/02/01 21:01:10 reyk Exp $ */
/*
* Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org>
@@ -44,11 +44,36 @@
__dead void usage(void);
int show_summary_msg(struct imsg *);
int show_command_output(struct imsg *);
-int monitor(struct imsg *);
char *print_service_status(int);
char *print_host_status(int, int);
char *print_table_status(int, int);
+struct imsgname {
+ int type;
+ char *name;
+ void (*func)(struct imsg *);
+};
+
+struct imsgname *monitor_lookup(u_int8_t);
+void monitor_host_status(struct imsg *);
+void monitor_id(struct imsg *);
+int monitor(struct imsg *);
+
+struct imsgname imsgs[] = {
+ { IMSG_HOST_STATUS, "host_status", monitor_host_status },
+ { IMSG_CTL_SERVICE_DISABLE, "ctl_disable_service", monitor_id },
+ { IMSG_CTL_SERVICE_ENABLE, "ctl_service_enable", monitor_id },
+ { IMSG_CTL_TABLE_DISABLE, "ctl_table_disable", monitor_id },
+ { IMSG_CTL_TABLE_ENABLE, "ctl_table_enable", monitor_id },
+ { IMSG_CTL_HOST_DISABLE, "ctl_host_disable", monitor_id },
+ { IMSG_CTL_HOST_ENABLE, "ctl_host_enable", monitor_id },
+ { IMSG_SYNC, "sync", NULL },
+ { 0, NULL, NULL }
+};
+struct imsgname imsgunknown = {
+ -1, "<unknown>", NULL
+};
+
struct imsgbuf *ibuf;
__dead void
@@ -184,54 +209,65 @@ main(int argc, char *argv[])
return (0);
}
-int
-monitor(struct imsg *imsg)
+struct imsgname *
+monitor_lookup(u_int8_t type)
{
- time_t now;
- int done;
- struct ctl_status cs;
- struct ctl_id id;
+ int i;
- done = 0;
- now = time(NULL);
- printf("got message of size %u on %s", imsg->hdr.len, ctime(&now));
- switch (imsg->hdr.type) {
- case IMSG_HOST_STATUS:
- memcpy(&cs, imsg->data, sizeof(cs));
- printf("HOST_STATUS: %u is in state %d\n", cs.id, cs.up);
- break;
- case IMSG_CTL_SERVICE_DISABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_SERVICE_DISABLE: %u\n", id.id);
- break;
- case IMSG_CTL_SERVICE_ENABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_SERVICE_ENABLE: %u\n", id.id);
- break;
- case IMSG_CTL_TABLE_DISABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_TABLE_DISABLE: %u\n", id.id);
- break;
- case IMSG_CTL_TABLE_ENABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_TABLE_ENABLE: %u\n", id.id);
- break;
- case IMSG_CTL_HOST_DISABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_HOST_DISABLE: %u\n", id.id);
- break;
- case IMSG_CTL_HOST_ENABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_HOST_ENABLE: %u\n", id.id);
+ for (i = 0; imsgs[i].name != NULL; i++)
+ if (imsgs[i].type == type)
+ return (&imsgs[i]);
+ return (&imsgunknown);
+}
+
+void
+monitor_host_status(struct imsg *imsg)
+{
+ struct ctl_status cs;
+
+ memcpy(&cs, imsg->data, sizeof(cs));
+ printf("\tid: %u\n", cs.id);
+ printf("\tstate: ");
+ switch (cs.up) {
+ case HOST_UP:
+ printf("up\n");
break;
- case IMSG_SYNC:
- printf("SYNC\n");
+ case HOST_DOWN:
+ printf("down\n");
break;
default:
- printf("INVALID\n");
- done = 1;
+ printf("unknown\n");
break;
}
+}
+
+void
+monitor_id(struct imsg *imsg)
+{
+ struct ctl_id id;
+
+ memcpy(&id, imsg->data, sizeof(id));
+ printf("\tid: %u\n", id.id);
+}
+
+int
+monitor(struct imsg *imsg)
+{
+ time_t now;
+ int done = 0;
+ struct imsgname *imn;
+
+ now = time(NULL);
+
+ imn = monitor_lookup(imsg->hdr.type);
+ printf("%s: imsg type %u len %u peerid %u pid %d\n", imn->name,
+ imsg->hdr.type, imsg->hdr.len, imsg->hdr.peerid, imsg->hdr.pid);
+ printf("\ttimestamp: %u, %s", now, ctime(&now));
+ if (imn->type == -1)
+ done = 1;
+ if (imn->func != NULL)
+ (*imn->func)(imsg);
+
return (done);
}
diff --git a/usr.sbin/relayctl/relayctl.c b/usr.sbin/relayctl/relayctl.c
index cfb796c43ce..d23ede5aff5 100644
--- a/usr.sbin/relayctl/relayctl.c
+++ b/usr.sbin/relayctl/relayctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: relayctl.c,v 1.10 2007/02/01 20:03:38 pyr Exp $ */
+/* $OpenBSD: relayctl.c,v 1.11 2007/02/01 21:01:10 reyk Exp $ */
/*
* Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org>
@@ -44,11 +44,36 @@
__dead void usage(void);
int show_summary_msg(struct imsg *);
int show_command_output(struct imsg *);
-int monitor(struct imsg *);
char *print_service_status(int);
char *print_host_status(int, int);
char *print_table_status(int, int);
+struct imsgname {
+ int type;
+ char *name;
+ void (*func)(struct imsg *);
+};
+
+struct imsgname *monitor_lookup(u_int8_t);
+void monitor_host_status(struct imsg *);
+void monitor_id(struct imsg *);
+int monitor(struct imsg *);
+
+struct imsgname imsgs[] = {
+ { IMSG_HOST_STATUS, "host_status", monitor_host_status },
+ { IMSG_CTL_SERVICE_DISABLE, "ctl_disable_service", monitor_id },
+ { IMSG_CTL_SERVICE_ENABLE, "ctl_service_enable", monitor_id },
+ { IMSG_CTL_TABLE_DISABLE, "ctl_table_disable", monitor_id },
+ { IMSG_CTL_TABLE_ENABLE, "ctl_table_enable", monitor_id },
+ { IMSG_CTL_HOST_DISABLE, "ctl_host_disable", monitor_id },
+ { IMSG_CTL_HOST_ENABLE, "ctl_host_enable", monitor_id },
+ { IMSG_SYNC, "sync", NULL },
+ { 0, NULL, NULL }
+};
+struct imsgname imsgunknown = {
+ -1, "<unknown>", NULL
+};
+
struct imsgbuf *ibuf;
__dead void
@@ -184,54 +209,65 @@ main(int argc, char *argv[])
return (0);
}
-int
-monitor(struct imsg *imsg)
+struct imsgname *
+monitor_lookup(u_int8_t type)
{
- time_t now;
- int done;
- struct ctl_status cs;
- struct ctl_id id;
+ int i;
- done = 0;
- now = time(NULL);
- printf("got message of size %u on %s", imsg->hdr.len, ctime(&now));
- switch (imsg->hdr.type) {
- case IMSG_HOST_STATUS:
- memcpy(&cs, imsg->data, sizeof(cs));
- printf("HOST_STATUS: %u is in state %d\n", cs.id, cs.up);
- break;
- case IMSG_CTL_SERVICE_DISABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_SERVICE_DISABLE: %u\n", id.id);
- break;
- case IMSG_CTL_SERVICE_ENABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_SERVICE_ENABLE: %u\n", id.id);
- break;
- case IMSG_CTL_TABLE_DISABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_TABLE_DISABLE: %u\n", id.id);
- break;
- case IMSG_CTL_TABLE_ENABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_TABLE_ENABLE: %u\n", id.id);
- break;
- case IMSG_CTL_HOST_DISABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_HOST_DISABLE: %u\n", id.id);
- break;
- case IMSG_CTL_HOST_ENABLE:
- memcpy(&id, imsg->data, sizeof(id));
- printf("CTL_HOST_ENABLE: %u\n", id.id);
+ for (i = 0; imsgs[i].name != NULL; i++)
+ if (imsgs[i].type == type)
+ return (&imsgs[i]);
+ return (&imsgunknown);
+}
+
+void
+monitor_host_status(struct imsg *imsg)
+{
+ struct ctl_status cs;
+
+ memcpy(&cs, imsg->data, sizeof(cs));
+ printf("\tid: %u\n", cs.id);
+ printf("\tstate: ");
+ switch (cs.up) {
+ case HOST_UP:
+ printf("up\n");
break;
- case IMSG_SYNC:
- printf("SYNC\n");
+ case HOST_DOWN:
+ printf("down\n");
break;
default:
- printf("INVALID\n");
- done = 1;
+ printf("unknown\n");
break;
}
+}
+
+void
+monitor_id(struct imsg *imsg)
+{
+ struct ctl_id id;
+
+ memcpy(&id, imsg->data, sizeof(id));
+ printf("\tid: %u\n", id.id);
+}
+
+int
+monitor(struct imsg *imsg)
+{
+ time_t now;
+ int done = 0;
+ struct imsgname *imn;
+
+ now = time(NULL);
+
+ imn = monitor_lookup(imsg->hdr.type);
+ printf("%s: imsg type %u len %u peerid %u pid %d\n", imn->name,
+ imsg->hdr.type, imsg->hdr.len, imsg->hdr.peerid, imsg->hdr.pid);
+ printf("\ttimestamp: %u, %s", now, ctime(&now));
+ if (imn->type == -1)
+ done = 1;
+ if (imn->func != NULL)
+ (*imn->func)(imsg);
+
return (done);
}