Commit Diff


commit - ddce9e236b5cbab80ad195840f427d4e19f39b97
commit + da2af3ac496403f12598bebd4b3474f4bad787e9
blob - 0a982b5cb1935a92641e2b017794a5c5bb1463f6
blob + 00d468e4f2977eda1ed8ea03f5c1659914056b36
--- carp.c
+++ carp.c
@@ -469,7 +469,13 @@ intcdev(char *ifname, int ifs, int argc, char **argv)
 	return (0);
 }
 
-void
+/* 
+ * Change the carp demotion counter.
+ * Return 1 if the counter was changed.
+ * Return 0 if the counter was already at or beyond the desired "lock" value.
+ * Return -1 on error.
+ */
+int
 carplock(int lock)
 {
 	int ifs;
@@ -481,12 +487,27 @@ carplock(int lock)
 	ifs = socket(AF_INET, SOCK_DGRAM, 0);
 	if (ifs == -1) {
 		printf("%% carplock: socket: %s\n", strerror(errno));
-		return;
+		return -1;
 	}
 
+	if (ioctl(ifs, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) {
+		printf("%% carplock: SIOCGIFGATTR %s: %s\n",
+		    ifgr.ifgr_name, strerror(errno));
+		close(ifs);
+		return -1;
+	}
+
+	if ((lock >= 0 && ifgr.ifgr_attrib.ifg_carp_demoted >= lock) ||
+	    (lock < 0 && ifgr.ifgr_attrib.ifg_carp_demoted <= lock)) {
+		close(ifs);
+		return 0; /* already "locked" */
+	}
+
 	ifgr.ifgr_attrib.ifg_carp_demoted = lock;
 
 	if (ioctl(ifs, SIOCSIFGATTR, (caddr_t)&ifgr) == -1)
 		printf("%% carplock: SIOCSIFGATTR: %s\n", strerror(errno));
 	close(ifs);
+
+	return 1;
 }
blob - 985ec7948a08869736e55a9f66fd385275c9cbf7
blob + e56534fea6e9c18e5ce36a0801be214dcae3f482
--- externs.h
+++ externs.h
@@ -463,7 +463,7 @@ int intcnode(char *, int, int, char **);
 int conf_carp(FILE *, int, char *);
 int carp_state(int, char *, FILE *);
 int intcdev(char *, int, int, char **);
-void carplock(int);
+int carplock(int);
 
 /* trunk.c */
 int inttrunkport(char *, int, int, char **);
blob - f1fe7d6526e5195906ff0082f8d42871d1680ec4
blob + d12f005cec3778de6c80aa47a81d64b27ae2ad63
--- main.c
+++ main.c
@@ -258,6 +258,8 @@ main(int argc, char *argv[])
 	}
 
 	if (iflag) {
+		int carp_locked;
+
 		/*
 		 * Interpret config file and exit
 		 */
@@ -270,14 +272,15 @@ main(int argc, char *argv[])
 		/*
 		 * Set carp group carpdemote to 128 during initialization
 		 */
-		carplock(128);
+		carp_locked = carplock(128);
 
 		cmdrc(rc);
 
 		/*
-		 * Initialization over
+		 * Initialization over; reset carpdemote if it was raised by us.
 		 */
-		carplock(-128);
+		if (carp_locked == 1)
+			carplock(-128);
 
 		exit(0);
 	}