Blame SOURCES/autofs-5.0.9-amd-lookup-add-selector-handling-functions.patch

6bbd11
autofs-5.0.999999999 - amd lookup add selector handling functions
6bbd11
6bbd11
From: Ian Kent <raven@themaw.net>
6bbd11
6bbd11
6bbd11
---
6bbd11
 include/parse_subs.h |   77 +++++++++++++++++++++++++++++++++
6bbd11
 lib/parse_subs.c     |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++
6bbd11
 2 files changed, 194 insertions(+)
6bbd11
6bbd11
diff --git a/include/parse_subs.h b/include/parse_subs.h
6bbd11
index 675411d..a416c59 100644
6bbd11
--- a/include/parse_subs.h
6bbd11
+++ b/include/parse_subs.h
6bbd11
@@ -25,6 +25,83 @@
6bbd11
 #define PROXIMITY_OTHER         0x0008
6bbd11
 #define PROXIMITY_UNSUPPORTED   0x0010
6bbd11
 
6bbd11
+#define SEL_ARCH		0x00000001
6bbd11
+#define SEL_KARCH		0x00000002
6bbd11
+#define SEL_OS			0x00000004
6bbd11
+#define SEL_OSVER		0x00000008
6bbd11
+#define SEL_FULL_OS		0x00000010
6bbd11
+#define SEL_VENDOR		0x00000020
6bbd11
+#define SEL_HOST		0x00000040
6bbd11
+#define SEL_HOSTD		0x00000080
6bbd11
+#define SEL_XHOST		0x00000100
6bbd11
+#define SEL_DOMAIN		0x00000200
6bbd11
+#define SEL_BYTE		0x00000400
6bbd11
+#define SEL_CLUSTER		0x00000800
6bbd11
+#define SEL_NETGRP		0x00001000
6bbd11
+#define SEL_NETGRPD		0x00002000
6bbd11
+#define SEL_IN_NETWORK		0x00004000
6bbd11
+#define SEL_UID			0x00008000
6bbd11
+#define SEL_GID			0x00010000
6bbd11
+#define SEL_KEY			0x00020000
6bbd11
+#define SEL_MAP			0x00040000
6bbd11
+#define SEL_PATH		0x00080000
6bbd11
+#define SEL_EXISTS		0x00100000
6bbd11
+#define SEL_AUTODIR		0x00200000
6bbd11
+#define SEL_DOLLAR		0x00400000
6bbd11
+#define SEL_TRUE		0x00800000
6bbd11
+#define SEL_FALSE		0x01000000
6bbd11
+
6bbd11
+#define SEL_COMP_NONE		0x0000
6bbd11
+#define SEL_COMP_EQUAL		0x0001
6bbd11
+#define SEL_COMP_NOTEQUAL	0x0002
6bbd11
+#define SEL_COMP_NOT		0x0004
6bbd11
+
6bbd11
+#define SEL_FLAG_MACRO		0x0001
6bbd11
+#define SEL_FLAG_FUNC1		0x0002
6bbd11
+#define SEL_FLAG_FUNC2		0x0004
6bbd11
+#define SEL_FLAG_STR		0x0100
6bbd11
+#define SEL_FLAG_NUM		0x0200
6bbd11
+#define SEL_FLAG_BOOL		0x0400
6bbd11
+
6bbd11
+#define SEL_FLAGS_TYPE_MASK	0x00FF
6bbd11
+#define SEL_FLAGS_VALUE_MASK	0xFF00
6bbd11
+#define SEL_FREE_VALUE_MASK	(SEL_FLAG_MACRO|SEL_FLAG_STR|SEL_FLAG_NUM)
6bbd11
+#define SEL_FREE_ARG1_MASK	(SEL_FLAG_FUNC1)
6bbd11
+#define SEL_FREE_ARG2_MASK	(SEL_FLAG_FUNC2)
6bbd11
+
6bbd11
+struct type_compare {
6bbd11
+	char	*value;
6bbd11
+};
6bbd11
+
6bbd11
+struct type_function {
6bbd11
+	char *arg1;
6bbd11
+	char *arg2;
6bbd11
+};
6bbd11
+
6bbd11
+struct sel {
6bbd11
+	unsigned long selector;
6bbd11
+	const char *name;
6bbd11
+	unsigned int flags;
6bbd11
+	struct sel *next;
6bbd11
+};
6bbd11
+
6bbd11
+struct selector {
6bbd11
+	struct sel *sel;
6bbd11
+	unsigned int compare;
6bbd11
+
6bbd11
+	union {
6bbd11
+		struct type_compare	comp;
6bbd11
+		struct type_function	func;
6bbd11
+	};
6bbd11
+
6bbd11
+	struct selector *next;
6bbd11
+};
6bbd11
+
6bbd11
+void sel_hash_init(void);
6bbd11
+struct sel *sel_lookup(const char *);
6bbd11
+struct selector *get_selector(char *);
6bbd11
+void free_selector(struct selector *);
6bbd11
+
6bbd11
 struct mapent;
6bbd11
 
6bbd11
 struct map_type_info {
6bbd11
diff --git a/lib/parse_subs.c b/lib/parse_subs.c
6bbd11
index 279f40e..f485a4c 100644
6bbd11
--- a/lib/parse_subs.c
6bbd11
+++ b/lib/parse_subs.c
6bbd11
@@ -45,6 +45,44 @@ static int volatile ifc_last_len = 0;
6bbd11
 #define EXPAND_LEADING_DOT	0x0004
6bbd11
 #define EXPAND_TRAILING_DOT	0x0008
6bbd11
 
6bbd11
+#define SELECTOR_HASH_SIZE	20
6bbd11
+
6bbd11
+static struct sel sel_table[] = {
6bbd11
+	{ SEL_ARCH,	  "arch",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_KARCH,	  "karch",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_OS,	  "os",		SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_OSVER,	  "osver",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_FULL_OS,	  "full_os",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_VENDOR,	  "vendor",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_HOST,	  "host",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_HOSTD,	  "hostd",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_XHOST,	  "xhost",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_DOMAIN,	  "domain",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_BYTE,	  "byte",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_CLUSTER,	  "cluster",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_NETGRP,	  "netgrp",	SEL_FLAG_FUNC2|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_NETGRPD,	  "netgrpd",	SEL_FLAG_FUNC2|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_IN_NETWORK, "in_network",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_IN_NETWORK, "netnumber",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_IN_NETWORK, "network",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_IN_NETWORK, "wire",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_UID,	  "uid",	SEL_FLAG_MACRO|SEL_FLAG_NUM, NULL },
6bbd11
+	{ SEL_GID,	  "gid",	SEL_FLAG_MACRO|SEL_FLAG_NUM, NULL },
6bbd11
+	{ SEL_KEY,	  "key",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_MAP,	  "map",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_PATH,	  "path",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_EXISTS,	  "exists",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_AUTODIR,	  "autodir",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_DOLLAR,	  "dollar",	SEL_FLAG_MACRO|SEL_FLAG_STR, NULL },
6bbd11
+	{ SEL_TRUE,	  "true",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+	{ SEL_FALSE,	  "false",	SEL_FLAG_FUNC1|SEL_FLAG_BOOL, NULL },
6bbd11
+};
6bbd11
+static unsigned int sel_count = sizeof(sel_table)/sizeof(struct sel);
6bbd11
+
6bbd11
+static struct sel *sel_hash[SELECTOR_HASH_SIZE];
6bbd11
+static unsigned int sel_hash_init_done = 0;
6bbd11
+static pthread_mutex_t sel_hash_mutex = PTHREAD_MUTEX_INITIALIZER;
6bbd11
+
6bbd11
 struct types {
6bbd11
 	char *type;
6bbd11
 	unsigned int len;
6bbd11
@@ -70,6 +108,85 @@ static struct types format_type[] = {
6bbd11
 };
6bbd11
 static unsigned int format_type_count = sizeof(format_type)/sizeof(struct types);
6bbd11
 
6bbd11
+static void sel_add(struct sel *sel)
6bbd11
+{
6bbd11
+	u_int32_t hval = hash(sel->name, SELECTOR_HASH_SIZE);
6bbd11
+	struct sel *old;
6bbd11
+
6bbd11
+	old = sel_hash[hval];
6bbd11
+	sel_hash[hval] = sel;
6bbd11
+	sel_hash[hval]->next = old;
6bbd11
+}
6bbd11
+
6bbd11
+void sel_hash_init(void)
6bbd11
+{
6bbd11
+	int i;
6bbd11
+
6bbd11
+	pthread_mutex_lock(&sel_hash_mutex);
6bbd11
+	if (sel_hash_init_done) {
6bbd11
+		pthread_mutex_unlock(&sel_hash_mutex);
6bbd11
+		return;
6bbd11
+	}
6bbd11
+	for (i = 0; i < SELECTOR_HASH_SIZE; i++)
6bbd11
+		sel_hash[i] = NULL;
6bbd11
+
6bbd11
+	for (i = 0; i < sel_count; i++)
6bbd11
+		sel_add(&sel_table[i]);
6bbd11
+
6bbd11
+	sel_hash_init_done = 1;
6bbd11
+	pthread_mutex_unlock(&sel_hash_mutex);
6bbd11
+}
6bbd11
+
6bbd11
+struct sel *sel_lookup(const char *name)
6bbd11
+{
6bbd11
+	u_int32_t hval = hash(name, SELECTOR_HASH_SIZE);
6bbd11
+	struct sel *sel;
6bbd11
+
6bbd11
+	pthread_mutex_lock(&sel_hash_mutex);
6bbd11
+	for (sel = sel_hash[hval]; sel != NULL; sel = sel->next) {
6bbd11
+		if (strcmp(name, sel->name) == 0) {
6bbd11
+			pthread_mutex_unlock(&sel_hash_mutex);
6bbd11
+			return sel;
6bbd11
+		}
6bbd11
+	}
6bbd11
+	pthread_mutex_unlock(&sel_hash_mutex);
6bbd11
+	return NULL;
6bbd11
+}
6bbd11
+
6bbd11
+struct selector *get_selector(char *name)
6bbd11
+{
6bbd11
+	struct sel *sel;
6bbd11
+
6bbd11
+	sel = sel_lookup(name);
6bbd11
+	if (sel) {
6bbd11
+		struct selector *new = malloc(sizeof(struct selector));
6bbd11
+		if (!new)
6bbd11
+			return NULL;
6bbd11
+		memset(new, 0, sizeof(*new));
6bbd11
+		new->sel = sel;
6bbd11
+		return new;
6bbd11
+	}
6bbd11
+	return NULL;
6bbd11
+}
6bbd11
+
6bbd11
+void free_selector(struct selector *selector)
6bbd11
+{
6bbd11
+	struct selector *s = selector;
6bbd11
+	struct selector *next = s;
6bbd11
+
6bbd11
+	while (s) {
6bbd11
+		next = s->next;
6bbd11
+		if (s->sel->flags & SEL_FREE_VALUE_MASK)
6bbd11
+			free(s->comp.value);
6bbd11
+		if (s->sel->flags & SEL_FREE_ARG1_MASK)
6bbd11
+			free(s->func.arg1);
6bbd11
+		if (s->sel->flags & SEL_FREE_ARG2_MASK)
6bbd11
+			free(s->func.arg2);
6bbd11
+		s = next;
6bbd11
+	}
6bbd11
+	return;
6bbd11
+}
6bbd11
+
6bbd11
 static unsigned int ipv6_mask_cmp(uint32_t *host, uint32_t *iface, uint32_t *mask)
6bbd11
 {
6bbd11
 	unsigned int ret = 1;