From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: fasnacht@protonmail.ch
Subject: [PATCH nft 4/4] scanner: multi-level input file stack for glob
Date: Tue, 11 Feb 2020 21:23:08 +0100 [thread overview]
Message-ID: <20200211202308.90575-5-pablo@netfilter.org> (raw)
In-Reply-To: <20200211202308.90575-1-pablo@netfilter.org>
This patch updates indesc_list to become an array of stacks. Each stack
represents the files that have been included at this depth.
The scanner_add_indesc() function adds the indesc to this depth, this is
called in case the user specifies wildcards.
Otherwise, the scanner_push_indesc() function for regular inclusion.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
include/list.h | 30 +++++++++++++++++++++++++++++
include/parser.h | 3 +--
src/parser_bison.y | 5 ++++-
src/scanner.l | 55 ++++++++++++++++++++++++++++++++++++++++--------------
4 files changed, 76 insertions(+), 17 deletions(-)
diff --git a/include/list.h b/include/list.h
index 75d292124010..29f5ca739632 100644
--- a/include/list.h
+++ b/include/list.h
@@ -22,6 +22,17 @@ struct list_head {
struct list_head *next, *prev;
};
+/**
+ * list_is_first -- tests whether @list is the first entry in list @head
+ * @list: the entry to test
+ * @head: the head of the list
+ */
+static inline int list_is_first(const struct list_head *list,
+ const struct list_head *head)
+{
+ return list->prev == head;
+}
+
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
@@ -623,3 +634,22 @@ static inline void hlist_add_after(struct hlist_node *n,
pos = n)
#endif
+
+/**
+ * list_prev_entry - get the prev element in list
+ * @pos: the type * to cursor
+ * @member: the name of the list_head within the struct.
+ */
+#define list_prev_entry(pos, member) \
+ list_entry((pos)->member.prev, typeof(*(pos)), member)
+
+/**
+ * list_last_entry - get the last element from a list
+ * @ptr: the list head to take the element from.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_head within the struct.
+ *
+ * Note, that list is expected to be not empty.
+ */
+#define list_last_entry(ptr, type, member) \
+ list_entry((ptr)->prev, type, member)
diff --git a/include/parser.h b/include/parser.h
index 949284d9466c..d24133059203 100644
--- a/include/parser.h
+++ b/include/parser.h
@@ -15,9 +15,8 @@
struct parser_state {
struct input_descriptor *indesc;
- struct input_descriptor *indescs[MAX_INCLUDE_DEPTH];
unsigned int indesc_idx;
- struct list_head indesc_list;
+ struct list_head indesc_list[MAX_INCLUDE_DEPTH];
struct list_head *msgs;
unsigned int nerrs;
diff --git a/src/parser_bison.y b/src/parser_bison.y
index f5d7026a8574..216126472687 100644
--- a/src/parser_bison.y
+++ b/src/parser_bison.y
@@ -45,11 +45,14 @@ void parser_init(struct nft_ctx *nft, struct parser_state *state,
struct list_head *msgs, struct list_head *cmds,
struct scope *top_scope)
{
+ int i;
+
memset(state, 0, sizeof(*state));
state->msgs = msgs;
state->cmds = cmds;
state->scopes[0] = scope_init(top_scope, NULL);
- init_list_head(&state->indesc_list);
+ for (i = 0; i < MAX_INCLUDE_DEPTH; i++)
+ init_list_head(&state->indesc_list[i]);
}
static void yyerror(struct location *loc, struct nft_ctx *nft, void *scanner,
diff --git a/src/scanner.l b/src/scanner.l
index 9584f61c489c..9a6734dd4ec4 100644
--- a/src/scanner.l
+++ b/src/scanner.l
@@ -681,22 +681,44 @@ indesc_file_alloc(FILE *f, const char *filename, const struct location *loc)
return indesc;
}
+static inline struct list_head *indesc_list(struct parser_state *state)
+{
+ return &state->indesc_list[state->indesc_idx];
+}
+
static void scanner_push_indesc(struct parser_state *state,
struct input_descriptor *indesc)
{
- state->indescs[state->indesc_idx] = indesc;
- state->indesc = state->indescs[state->indesc_idx++];
- list_add_tail(&indesc->list, &state->indesc_list);
+ state->indesc = indesc;
+ list_add_tail(&indesc->list, indesc_list(state));
+ state->indesc_idx++;
+}
+
+static void scanner_add_indesc(struct parser_state *state,
+ struct input_descriptor *indesc)
+{
+ state->indesc = indesc;
+ list_add_tail(&indesc->list, indesc_list(state));
}
static void scanner_pop_indesc(struct parser_state *state)
{
- state->indesc_idx--;
+ if (list_empty(indesc_list(state)))
+ state->indesc_idx--;
- if (state->indesc_idx > 0)
- state->indesc = state->indescs[state->indesc_idx - 1];
- else
+ if (state->indesc_idx == 0) {
state->indesc = NULL;
+ return;
+ }
+
+ if (list_is_first(&state->indesc->list, indesc_list(state))) {
+ state->indesc_idx--;
+ state->indesc =
+ list_last_entry(indesc_list(state),
+ struct input_descriptor, list);
+ } else {
+ state->indesc = list_prev_entry(state->indesc, list);
+ }
}
static void scanner_pop_buffer(yyscan_t scanner)
@@ -816,8 +838,10 @@ static int include_glob(struct nft_ctx *nft, void *scanner, const char *pattern,
goto err;
indesc = scanner_push_file(nft, scanner, f, path, loc);
- scanner_push_indesc(state, indesc);
+ scanner_add_indesc(state, indesc);
}
+ if (glob_data.gl_pathc)
+ state->indesc_idx++;
globfree(&glob_data);
@@ -962,14 +986,17 @@ static void input_descriptor_destroy(const struct input_descriptor *indesc)
static void input_descriptor_list_destroy(struct parser_state *state)
{
struct input_descriptor *indesc, *next;
+ int i;
- list_for_each_entry_safe(indesc, next, &state->indesc_list, list) {
- if (indesc->f) {
- fclose(indesc->f);
- indesc->f = NULL;
+ for (i = 0; i < MAX_INCLUDE_DEPTH; i++) {
+ list_for_each_entry_safe(indesc, next, &state->indesc_list[i], list) {
+ if (indesc->f) {
+ fclose(indesc->f);
+ indesc->f = NULL;
+ }
+ list_del(&indesc->list);
+ input_descriptor_destroy(indesc);
}
- list_del(&indesc->list);
- input_descriptor_destroy(indesc);
}
}
--
2.11.0
next prev parent reply other threads:[~2020-02-11 20:23 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-11 20:23 [PATCH nft 0/4] glob and maximum number of includes Pablo Neira Ayuso
2020-02-11 20:23 ` [PATCH nft 1/4] scanner: call scanner_push_file() after scanner_push_file() Pablo Neira Ayuso
2020-02-11 20:23 ` [PATCH nft 2/4] scanner: add indesc_file_alloc() helper function Pablo Neira Ayuso
2020-02-11 20:23 ` [PATCH nft 3/4] scanner: call scanner_push_indesc() after scanner_push_file() Pablo Neira Ayuso
2020-02-11 20:23 ` Pablo Neira Ayuso [this message]
2020-02-12 20:44 ` [PATCH nft 0/4] glob and maximum number of includes Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200211202308.90575-5-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=fasnacht@protonmail.ch \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).