Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/ifwatchd New utility: ifwatchd.



details:   https://anonhg.NetBSD.org/src/rev/11d8d6bad89e
branches:  trunk
changeset: 517795:11d8d6bad89e
user:      martin <martin%NetBSD.org@localhost>
date:      Mon Nov 19 09:43:03 2001 +0000

description:
New utility: ifwatchd.
Monitors the routing socket for address changes of autonomous (kernel only)
interfaces (like PPPoE) and runs up/down scripts similar to what pppd
does for its interfaces.

diffstat:

 usr.sbin/ifwatchd/Makefile   |    6 +
 usr.sbin/ifwatchd/ifwatchd.8 |  126 +++++++++++++++++
 usr.sbin/ifwatchd/ifwatchd.c |  306 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 438 insertions(+), 0 deletions(-)

diffs (truncated from 450 to 300 lines):

diff -r 917744f83d8e -r 11d8d6bad89e usr.sbin/ifwatchd/Makefile
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ifwatchd/Makefile        Mon Nov 19 09:43:03 2001 +0000
@@ -0,0 +1,6 @@
+#      $NetBSD: Makefile,v 1.1 2001/11/19 09:43:03 martin Exp $
+
+PROG=ifwatchd
+MAN=ifwatchd.8
+
+.include <bsd.prog.mk>
diff -r 917744f83d8e -r 11d8d6bad89e usr.sbin/ifwatchd/ifwatchd.8
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ifwatchd/ifwatchd.8      Mon Nov 19 09:43:03 2001 +0000
@@ -0,0 +1,126 @@
+.\" Copyright (C) 2001 by Martin Husemann
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
+.\" OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+.\" DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
+.\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd September 2, 2001
+.Os
+.Dt IFWATCHD 8
+.Sh NAME
+.Nm ifwatchd
+.Nd watch for adresses added to or deleted from interfaces and call up/down scripts for them
+.Sh SYNOPSIS
+.Nm
+.Op Fl h
+.Op Fl v
+.Op Fl d Ar down-script
+.Op Fl u Ar up-script
+.Ar ifname(s)
+.Pp
+.Sh DESCRIPTION
+.Nm Ifwatchd
+is used to monitor dynamic interfaces (for example PPP interfaces) for address
+changes. Sometimes this interfaces are acompanied by a daemon program, which
+can take care of running any necesary scripts (like pppd or isdnd), but 
+sometimes the interfaces run completely autonomus (like pppoe).
+.Pp
+.Nm Ifwatchd
+provides a generic way to watch this type of changes. It works by monitoring
+the routing socket and interpreting
+.Ql RTM_NEWADDR
+.Pq address added
+and
+.Ql RTM_DELADDR
+.Pq address deleted
+messages. It does not need special privileges to do this. The scripts called
+for up or down events are run with the same user id as
+.Nm
+is run.
+.Pp
+The following options are available:
+.Bl -tag -width indent
+.It Fl h
+Show the synopsis.
+.It Fl v
+Output verbose progress messages and flag errors ignored during normal
+operation.
+.It Fl u Ar up-script
+Specify the command to invoke on
+.Dq interface up
+events (or: addition of an address to an interface).
+.It Fl d Ar down-script
+Specify the command to invoke on
+.Dq interface down
+events (or: deletion of an address from an interface).
+.It ifname(s)
+The name of the interface to watch. Multiple interfaces may be specified.
+Events for other interfaces are ignored.
+.El
+.Sh EXAMPLES
+Note: you may be able to run
+.Nm
+from 
+.Pa /etc/ifconfig.*
+files via the
+.Em !
+shell command syntax, but only if your /usr partition is already mounted
+(i.e. you either do not have a separate /usr partition or you have its
+mount point included in the critical_filesystems_beforenet /etc/rc.conf
+variable.)
+.Bd -literal
+# ifwatchd -u /etc/ppp/ip-up -d /etc/ppp/ip-down pppoe0
+.Ed
+.Sh PARAMETERS PASSED TO SCRIPTS
+The invoked scripts get three parameters passed:
+.Bl -tag -width indent
+.It Ar ifname
+The name of the interface this change is for (this allows to share the same
+script for multiple interfaces wathed and dispatching on the interface name
+in the script).
+.It Ar up/down
+Verbatim
+.Em up
+or
+.Em down ,
+which allows for the sharing of up and down scripts.
+.It Ar address
+the new address if this is an up event, or the no long valid old address
+if this is a down event.
+.Pp
+The format of the address depends on the address family, for IPv4 it is the
+usual dotted quad notation, for IPv6 the colon separated standard notation.
+.El
+.Sh SEE ALSO
+.Xr route 4 ,
+.Xr ifconfig.if 5 ,
+.Xr pppoe 8 ,
+.Xr rc.d 8 ,
+.Xr route 8
+.Sh HISTORY
+The 
+.Nm
+utility appeared in
+.Nx 1.6 .
+.Sh AUTHOR
+The program was written by
+.ie t Martin Husemann.
+.el Martin Husemann.
diff -r 917744f83d8e -r 11d8d6bad89e usr.sbin/ifwatchd/ifwatchd.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/ifwatchd/ifwatchd.c      Mon Nov 19 09:43:03 2001 +0000
@@ -0,0 +1,306 @@
+/*
+ * Copyright (c) 2001 Martin Husemann <martin%duskware.de@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. The name of the author may not be used to endorse or promote products
+ *    derived from this software withough specific prior written permission
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/file.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <sys/mbuf.h>
+#include <sys/sysctl.h>
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/route.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#include <sys/queue.h>
+
+/* local functions */
+static void usage(void);
+static void dispatch(void*, size_t);
+static void check_addrs(char *cp, int addrs, int is_up);
+static void invoke_script(struct sockaddr *sa, int is_up, int ifindex);
+static void list_interfaces(const char *ifnames);
+static void rescan_interfaces(void);
+static void free_interfaces(void);
+static int find_interface(int index);
+
+/* stolen from /sbin/route */
+#define ROUNDUP(a) \
+       ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
+#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
+
+/* global variables */
+static int verbose = 0;
+static const char *up_script = NULL;
+static const char *down_script = NULL;
+
+struct interface_data {
+       SLIST_ENTRY(interface_data) next;
+       int index;
+       char * ifname;
+};
+SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
+
+int
+main(int argc, char **argv)
+{
+       int c, s, n;
+       int errs = 0;
+       char msg[2048], *msgp;
+
+       while ((c = getopt(argc, argv, "vhu:d:")) != -1)
+               switch (c) {
+               case 'h':
+                       usage();
+                       return 0;
+               case 'v':
+                       verbose++;
+                       break;
+
+               case 'u':
+                       up_script = optarg;
+                       break;
+
+               case 'd':
+                       down_script = optarg;
+                       break;
+
+               default:
+                       errs++;
+                       break;
+               }
+
+       if (errs)
+               usage();
+
+       argv += optind;
+       argc -= optind;
+
+       if (argc <= 0) 
+               usage();
+
+       if (verbose) {
+               printf("up_script: %s\ndown_script: %s\n",
+                       up_script, down_script);
+               printf("verbosity = %d\n", verbose);
+       }
+
+       while (argc > 0) {
+           list_interfaces(argv[0]);
+           argv++; argc--;
+       }
+
+       if (!verbose)
+               daemon(0,0);
+
+       s = socket(PF_ROUTE, SOCK_RAW, 0);
+       if (s < 0) {
+               perror("open routing socket");
+               exit(1);
+       }
+
+       for (;;) {
+               n = read(s, msg, sizeof msg);
+               msgp = msg;
+               for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
+                       dispatch(msgp, n);
+                       
+               }
+       }
+
+       close(s);
+       free_interfaces();
+
+       exit(0);
+}
+
+static void
+usage()
+{
+       fprintf(stderr, 
+           "usage:\n"
+           "\tifwatchd [-h] [-v] [-u up-script] [-d down-script] ifname(s)\n"
+           "\twhere:\n"



Home | Main Index | Thread Index | Old Index