- Support multiple profiles (instances) much like apache22

- Add `-p pidfile' option to support multiple profiles

PR:		ports/149727
Submitted by:	Jim Riggs <ports AT christianserving.org> (maintainer)
This commit is contained in:
Li-Wen Hsu 2010-08-22 01:15:32 +00:00
parent e716b53261
commit fdde528fec
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=259708
6 changed files with 210 additions and 46 deletions

View file

@ -7,7 +7,7 @@
PORTNAME= ifstated
PORTVERSION= 4.7
PORTREVISION= 1
PORTREVISION= 2
PORTEPOCH= 1
CATEGORIES= net
MASTER_SITES= http://christianserving.org/ports/net/ifstated/
@ -28,7 +28,7 @@ USE_RC_SUBR= ifstated
post-patch:
@${REINPLACE_CMD} -e 's|%%LOCALBASE%%|${LOCALBASE}|g' ${WRKSRC}/Makefile
@${REINPLACE_CMD} -e 's|/etc/|${PREFIX}/etc/|g' ${WRKSRC}/ifstated.c ${WRKSRC}/ifstated.8 ${WRKSRC}/ifstated.conf.5
@${REINPLACE_CMD} -e 's|/etc/|${PREFIX}/etc/|g' ${WRKSRC}/ifstated.conf.5
do-install:
${INSTALL_PROGRAM} ${WRKSRC}/ifstated ${PREFIX}/sbin

View file

@ -10,22 +10,95 @@
#
# Add the following lines to /etc/rc.conf to enable ifstated:
# ifstated_enable (bool): Set to "NO" by default.
# Set it to "YES" to enable ifstated
# ifstated_flags (str): Set to "-f %%PREFIX%%/etc/ifstated.conf" by default.
# Extra flags passed to start command
#
# ifstated_enable (bool): Set to "NO" by default.
# Set it to "YES" to enable ifstated.
# ifstated_flags (str): Set to "" by default.
# Extra flags passed to start command.
#
# With no profiles defined, the default configuration file will be
# used (%%PREFIX%%/etc/ifstated.conf).
#
# For profiles (separate ifstated intances):
#
# ifstated_profiles (str): Set to "" by default.
# Define profile names (e.g. "dns
# http ssh").
# ifstated_<name>_configfile (str):
# [Required] Path to the configuration
# file for profile <name>.
# ifstated_<name>_enable (bool): Set to ${ifstated_enable} by default.
# Set it to "YES" or "NO" to
# independently enable or disable
# profile <name>.
# ifstated_<name>_flags (str): Set to ${ifstated_flags} by default.
# Extra flags passed to start command
# for profile <name>.
#
. /etc/rc.subr
name="ifstated"
rcvar=`set_rcvar`
command="%%PREFIX%%/sbin/ifstated"
[ -z "$ifstated_enable" ] && ifstated_enable="NO"
[ -z "$ifstated_flags" ] && ifstated_flags="-f %%PREFIX%%/etc/ifstated.conf"
pidfile="/var/run/${name}.pid"
command_args="-p \"${pidfile}\""
load_rc_config $name
run_rc_command "$1"
: ${ifstated_enable:="NO"}
: ${ifstated_flags:=""}
: ${ifstated_profiles:=""}
if [ -n "$2" ]
then
profile="$2"
if [ "x${ifstated_profiles}" != "x" ]
then
pidfile="/var/run/${name}.${profile}.pid"
eval ifstated_configfile="\${ifstated_${profile}_configfile:-}"
if [ "x${ifstated_configfile}" = "x" ]
then
echo "You must define a configuration file (ifstated_${profile}_configile)." >&2
exit 1
fi
required_files="${ifstated_configfile}"
command_args="-f \"${ifstated_configfile}\" -p \"${pidfile}\""
eval ifstated_enable="\${ifstated_${profile}_enable:=${ifstated_enable}}"
eval ifstated_flags="\${ifstated_${profile}_flags:=${ifstated_flags}}"
else
echo "$0: extra argument ignored." >&2
fi
else
if [ "x${ifstated_profiles}" != "x" -a "x$1" != "x" ]
then
for profile in ${ifstated_profiles}
do
eval ifstated_enable_tmp="\${ifstated_${profile}_enable:=${ifstated_enable}}"
case "x${ifstated_enable_tmp}"
in
x|x[Nn][Oo])
continue
;;
x[Yy][Ee][Ss])
;;
*)
echo "Bad value \"${ifstated_enable_tmp}\" for ifstated_${profile}_enable. Profile ${profile} skipped." >&2
continue
;;
esac
echo "===> ifstated profile: ${profile}"
'%%PREFIX%%/etc/rc.d/ifstated' "$1" "${profile}"
done
exit 0
fi
fi
run_rc_command "$1"

View file

@ -0,0 +1,32 @@
--- ifstated.8.orig 2010-06-11 12:20:08.000000000 -0500
+++ ifstated.8 2010-07-30 22:15:08.518460671 -0500
@@ -29,6 +29,7 @@
.Ar macro Ns = Ns Ar value Oc
.Xc
.Op Fl f Ar file
+.Op Fl p Ar pidfile
.Ek
.Sh DESCRIPTION
The
@@ -61,6 +62,10 @@
Specify an alternate location,
.Ar file ,
for the configuration file.
+.It Fl p Ar pidfile
+Specify the location,
+.Ar pidfile ,
+for the PID file.
.It Fl h
Print help message.
.It Fl i
@@ -78,8 +83,8 @@
.Nm
reloads the configuration file.
.Sh FILES
-.Bl -tag -width "/etc/ifstated.conf" -compact
-.It Pa /etc/ifstated.conf
+.Bl -tag -width "/usr/local/etc/ifstated.conf" -compact
+.It Pa /usr/local/etc/ifstated.conf
.Nm
configuration file.
.El

View file

@ -1,5 +1,5 @@
--- ifstated.c.orig 2010-06-11 12:20:08.000000000 -0500
+++ ifstated.c 2010-06-15 13:49:50.785704080 -0500
+++ ifstated.c 2010-07-30 21:55:03.045444649 -0500
@@ -26,9 +26,11 @@
#include <sys/time.h>
#include <sys/ioctl.h>
@ -12,7 +12,17 @@
#include <net/route.h>
#include <netinet/in.h>
@@ -61,6 +63,8 @@
@@ -47,7 +49,8 @@
int opts = 0;
int opt_inhibit = 0;
-char *configfile = "/etc/ifstated.conf";
+char *configfile = "/usr/local/etc/ifstated.conf";
+char *pidfile = NULL;
struct event rt_msg_ev, sighup_ev, startup_ev, sigchld_ev;
void startup_handler(int, short, void *);
@@ -61,6 +64,8 @@
void external_evtimer_setup(struct ifsd_state *, int);
void scan_ifstate(int, int, int);
int scan_ifstate_single(int, int, struct ifsd_state *);
@ -21,7 +31,42 @@
void fetch_state(void);
void usage(void);
void adjust_expressions(struct ifsd_expression_list *, int);
@@ -159,7 +163,6 @@
@@ -70,13 +75,14 @@
void do_action(struct ifsd_action *);
void remove_action(struct ifsd_action *, struct ifsd_state *);
void remove_expression(struct ifsd_expression *, struct ifsd_state *);
+void remove_pidfile(int);
void
usage(void)
{
extern char *__progname;
- fprintf(stderr, "usage: %s [-dhinv] [-D macro=value] [-f file]\n",
+ fprintf(stderr, "usage: %s [-dhinv] [-D macro=value] [-f file] [-p pidfile]\n",
__progname);
exit(1);
}
@@ -90,7 +96,7 @@
log_init(1);
- while ((ch = getopt(argc, argv, "dD:f:hniv")) != -1) {
+ while ((ch = getopt(argc, argv, "dD:f:p:hniv")) != -1) {
switch (ch) {
case 'd':
debug = 1;
@@ -112,6 +118,9 @@
case 'i':
opt_inhibit = 1;
break;
+ case 'p':
+ pidfile = optarg;
+ break;
case 'v':
if (opts & IFSD_OPT_VERBOSE)
opts |= IFSD_OPT_VERBOSE2;
@@ -159,7 +168,6 @@
startup_handler(int fd, short event, void *arg)
{
int rt_fd;
@ -29,7 +74,7 @@
if ((rt_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
err(1, "no routing socket");
@@ -169,11 +172,6 @@
@@ -169,11 +177,24 @@
exit(1);
}
@ -38,10 +83,28 @@
- &rtfilter, sizeof(rtfilter)) == -1) /* not fatal */
- log_warn("startup_handler: setsockopt");
-
+ if (pidfile != NULL) {
+ FILE* file = fopen(pidfile, "w");
+
+ if (file == NULL) {
+ log_warnx("unable to open pidfile");
+ }
+ else {
+ fprintf(file, "%ld\n", (long)getpid());
+ fclose(file);
+ log_debug("wrote pidfile %s", pidfile);
+
+ signal(SIGINT, remove_pidfile);
+ signal(SIGQUIT, remove_pidfile);
+ signal(SIGABRT, remove_pidfile);
+ signal(SIGTERM, remove_pidfile);
+ }
+ }
+
event_set(&rt_msg_ev, rt_fd, EV_READ|EV_PERSIST, rt_msg_handler, NULL);
event_add(&rt_msg_ev, NULL);
@@ -406,6 +404,8 @@
@@ -406,6 +427,8 @@
}
}
@ -50,7 +113,7 @@
#define LINK_STATE_IS_DOWN(_s) \
(!LINK_STATE_IS_UP((_s)) && (_s) != LINK_STATE_UNKNOWN)
@@ -584,6 +584,44 @@
@@ -584,6 +607,44 @@
}
}
@ -95,7 +158,7 @@
/*
* Fetch the current link states.
*/
@@ -593,26 +631,31 @@
@@ -593,26 +654,31 @@
struct ifaddrs *ifap, *ifa;
char *oname = NULL;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
@ -135,3 +198,17 @@
}
freeifaddrs(ifap);
close(sock);
@@ -707,3 +773,13 @@
}
free(expression);
}
+
+void
+remove_pidfile(int code)
+{
+ if ((pidfile != NULL) && unlink(pidfile)) {
+ log_warnx("could not remove pidfile");
+ }
+
+ exit(code);
+}

View file

@ -1,16 +1,7 @@
*** ATTENTION ***
You must create a %%PREFIX%%/etc/ifstated.conf file (a sample is
provided). To run ifstated from startup, add the following to /etc/rc.conf:
ifstated_enable="YES"
Available variables you can set in /etc/rc.conf:
- ifstated_enable (bool): Set to "NO" by default.
Set it to "YES" to enable ifstated.
- ifstated_flags (str): Set to "-f %%PREFIX%%/etc/ifstated.conf" by default.
Extra flags passed to start command.
To use ifstated, you must create one or more configration files (a
sample is provided). This version of ifstated allows for multiple
"profiles" or instances, each with its own configuration. Please see
the comments in %%PREFIX%%/etc/rc.d/ifstated for the variables to set
in /etc/rc.conf.

View file

@ -1,15 +1,6 @@
This is a port of ifstated(8) from OpenBSD by Matthew George.
From the manpage:
***
The ifstated daemon runs commands in response to network state changes,
which it determines by monitoring interface link state or running exter-
nal tests. For example, it can be used with carp(4) to change running
services or to ensure that carp(4) interfaces stay in sync, or with pf(4)
to test server or link availability and modify translation or routing
rules.
***
It has been modified to use FreeBSD's kqueue/kevent mechanism and sysctl.
The ifstated daemon runs commands in response to network state
changes, which it determines by monitoring interface link state or
running external tests. For example, it can be used with carp(4) to
change running services or to ensure that carp(4) interfaces stay in
sync, or with pf(4) to test server or link availability and modify
translation or routing rules.