devd and caseful device ID matching on 6.0

Gary Palmer gpalmer at freebsd.org
Sat Jan 7 21:11:52 PST 2006


Sam Leffler wrote:

> At one point I added shorthand logic in devd for things like vendor, 
> device, subvendor, etc. that did numeric comparisons instead of 
> regex's.  It might be worthwhile to extend the grammar to have a 
> numeric match operator.


Something like the attached patch?  It seems to work in my test case, 
although I'm pretty sure its not as defensive as it could be against bad 
inputs.

(note: The patch is against 6.0-STABLE, not head)

Thanks,

Gary

-------------- next part --------------
Index: devd.cc
===================================================================
RCS file: /home/ncvs/src/sbin/devd/devd.cc,v
retrieving revision 1.22.2.5
diff -u -r1.22.2.5 devd.cc
--- devd.cc	19 Dec 2005 03:33:05 -0000	1.22.2.5
+++ devd.cc	8 Jan 2006 05:09:30 -0000
@@ -192,6 +192,25 @@
 	return retval;
 }
 
+equals::equals(config &c, const char *var, const char *val)
+	: _var(var)
+{
+	_val = strtol(val, NULL, 0);
+}
+
+bool
+equals::do_match(config &c)
+{
+	string value = c.get_variable(_var);
+	long val = strtol(value.c_str(), NULL, 0);
+
+	if (Dflag)
+		fprintf(stderr, "Testing %s=%s (0x%x) against 0x%x\n",
+			_var.c_str(), value.c_str(), val, _val);
+	
+	return(val == _val);
+}
+
 #include <sys/sockio.h>
 #include <net/if.h>
 #include <net/if_media.h>
@@ -850,6 +869,15 @@
 }
 
 eps *
+new_equals(const char *var, const char *val)
+{
+	eps *e = new equals(cfg, var, val);
+	free(const_cast<char *>(var));
+	free(const_cast<char *>(val));
+	return (e);
+}
+
+eps *
 new_media(const char *var, const char *re)
 {
 	eps *e = new media(cfg, var, re);
Index: devd.h
===================================================================
RCS file: /home/ncvs/src/sbin/devd/devd.h,v
retrieving revision 1.5
diff -u -r1.5 devd.h
--- devd.h	10 Jul 2005 03:37:15 -0000	1.5
+++ devd.h	8 Jan 2006 05:07:09 -0000
@@ -43,6 +43,7 @@
 void add_notify(int, struct event_proc *);
 struct event_proc *add_to_event_proc(struct event_proc *, struct eps *);
 struct eps *new_match(const char *, const char *);
+struct eps *new_equals(const char *, const char *);
 struct eps *new_media(const char *, const char *);
 struct eps *new_action(const char *);
 void set_pidfile(const char *);
Index: devd.hh
===================================================================
RCS file: /home/ncvs/src/sbin/devd/devd.hh,v
retrieving revision 1.3
diff -u -r1.3 devd.hh
--- devd.hh	10 Jul 2005 03:37:15 -0000	1.3
+++ devd.hh	8 Jan 2006 05:07:09 -0000
@@ -96,6 +96,21 @@
 };
 
 /**
+ * equal is the subclass used to match an individual variable using
+ * numeric comparitors instead of a regex.  Its actions are nops.
+ */
+class equals : public eps
+{
+public:
+	equals(config &, const char *var, const char *val);
+	virtual bool do_match(config &);
+	virtual bool do_action(config &) { return true; }
+private:
+	std::string _var;
+	long _val;
+};
+
+/**
  * media is the subclass used to match an individual variable.  Its
  * actions are nops.
  */
Index: parse.y
===================================================================
RCS file: /home/ncvs/src/sbin/devd/parse.y,v
retrieving revision 1.5
diff -u -r1.5 parse.y
--- parse.y	10 Jul 2005 03:37:15 -0000	1.5
+++ parse.y	8 Jan 2006 05:07:09 -0000
@@ -48,6 +48,7 @@
 %token <str> ID
 %token OPTIONS SET DIRECTORY PID_FILE DEVICE_NAME ACTION MATCH
 %token ATTACH DETACH NOMATCH NOTIFY MEDIA_TYPE CLASS SUBDEVICE
+%token EQUALS
 
 %type <eventproc> match_or_action_list
 %type <eps> match_or_action match action
@@ -135,6 +136,7 @@
 
 match
 	: MATCH STRING STRING SEMICOLON	{ $$ = new_match($2, $3); }
+	| EQUALS STRING STRING SEMICOLON	{ $$ = new_equals($2, $3); }
 	| DEVICE_NAME STRING SEMICOLON
 		{ $$ = new_match(strdup("device-name"), $2); }
 	| MEDIA_TYPE STRING SEMICOLON
Index: token.l
===================================================================
RCS file: /home/ncvs/src/sbin/devd/token.l,v
retrieving revision 1.6
diff -u -r1.6 token.l
--- token.l	10 Jul 2005 03:37:15 -0000	1.6
+++ token.l	8 Jan 2006 05:07:09 -0000
@@ -95,6 +95,7 @@
 subdevice		{ return SUBDEVICE; }
 action			{ return ACTION; }
 match			{ return MATCH; }
+equals			{ return EQUALS; }
 nomatch			{ return NOMATCH; }
 notify			{ return NOTIFY; }
 [A-Za-z][A-Za-z0-9_-]*	{


More information about the freebsd-stable mailing list