summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xlibbsd.py3
-rw-r--r--libbsd_waf.py12
-rw-r--r--rtemsbsd/include/machine/rtems-bsd-rc-conf-directives.h61
-rw-r--r--rtemsbsd/include/machine/rtems-bsd-rc-conf.h99
-rw-r--r--rtemsbsd/rtems/rtems-bsd-rc-conf-net.c303
-rw-r--r--rtemsbsd/rtems/rtems-bsd-rc-conf.c463
-rw-r--r--testsuite/rcconf01/test_main.c164
7 files changed, 1105 insertions, 0 deletions
diff --git a/libbsd.py b/libbsd.py
index a6a0ff0f..3d452290 100755
--- a/libbsd.py
+++ b/libbsd.py
@@ -63,6 +63,8 @@ def rtems(mm):
'rtems/rtems-bsd-get-mac-address.c',
'rtems/rtems-bsd-get-task-priority.c',
'rtems/rtems-bsd-get-task-stack-size.c',
+ 'rtems/rtems-bsd-rc-conf-net.c',
+ 'rtems/rtems-bsd-rc-conf.c',
'rtems/rtems-bsd-shell.c',
'rtems/rtems-bsd-shell-netcmds.c',
'rtems/rtems-bsd-syscall-api.c',
@@ -2449,6 +2451,7 @@ def tests(mm):
mod.addTest(mm.generator['test']('vlan01', ['test_main'], netTest = True))
mod.addTest(mm.generator['test']('lagg01', ['test_main'], netTest = True))
mod.addTest(mm.generator['test']('log01', ['test_main']))
+ mod.addTest(mm.generator['test']('rcconf01', ['test_main']))
return mod
#
diff --git a/libbsd_waf.py b/libbsd_waf.py
index df7a9ead..5cdcd5ea 100644
--- a/libbsd_waf.py
+++ b/libbsd_waf.py
@@ -977,6 +977,8 @@ def build(bld):
'rtemsbsd/rtems/rtems-bsd-get-mac-address.c',
'rtemsbsd/rtems/rtems-bsd-get-task-priority.c',
'rtemsbsd/rtems/rtems-bsd-get-task-stack-size.c',
+ 'rtemsbsd/rtems/rtems-bsd-rc-conf-net.c',
+ 'rtemsbsd/rtems/rtems-bsd-rc-conf.c',
'rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c',
'rtemsbsd/rtems/rtems-bsd-shell-netcmds.c',
'rtemsbsd/rtems/rtems-bsd-shell.c',
@@ -1281,6 +1283,16 @@ def build(bld):
lib = ["m", "z"],
install_path = None)
+ test_rcconf01 = ['testsuite/rcconf01/test_main.c']
+ bld.program(target = "rcconf01.exe",
+ features = "cprogram",
+ cflags = cflags,
+ includes = includes,
+ source = test_rcconf01,
+ use = ["bsd"],
+ lib = ["m", "z"],
+ install_path = None)
+
test_rwlock01 = ['testsuite/rwlock01/test_main.c']
bld.program(target = "rwlock01.exe",
features = "cprogram",
diff --git a/rtemsbsd/include/machine/rtems-bsd-rc-conf-directives.h b/rtemsbsd/include/machine/rtems-bsd-rc-conf-directives.h
new file mode 100644
index 00000000..5eaa2606
--- /dev/null
+++ b/rtemsbsd/include/machine/rtems-bsd-rc-conf-directives.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * The directives available with rc.conf. You define the list you wish
+ * to have available in your application and they will be available.
+ */
+
+#ifndef _RTEMS_BSP_RC_CONF_DIRECTIVES_h
+#define _RTEMS_BSP_RC_CONF_DIRECTIVES_h
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#include <sys/queue.h>
+#include <sys/kernel.h>
+
+/*
+ * Use the same SYSINT for all directives.
+ */
+#define RTEMS_BSD_RC_CONF_SYSINT(_n) \
+ SYSINIT(_n, SI_SUB_CREATE_INIT, SI_ORDER_ANY, _n ## _init, NULL)
+
+/*
+ * Decls for the handlers.
+ */
+void rc_conf_net_init(void* arg); /* Installed by default. */
+
+/*
+ * The user available directives.
+ */
+/* add here */
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif
diff --git a/rtemsbsd/include/machine/rtems-bsd-rc-conf.h b/rtemsbsd/include/machine/rtems-bsd-rc-conf.h
new file mode 100644
index 00000000..303d71a6
--- /dev/null
+++ b/rtemsbsd/include/machine/rtems-bsd-rc-conf.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Parse a FreeBSD /etc/rc.conf format file and execute the configuration
+ * options we want to support.
+ */
+
+#ifndef _RTEMS_BSP_RC_CONF_h
+#define _RTEMS_BSP_RC_CONF_h
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/*
+ * Maximum size of an rc.conf file.
+ */
+#define RTEMS_BSD_RC_CONF_MAX_SIZE (8 * 1024)
+
+/*
+ * Directive processing data. This data is opaque externally.
+ */
+typedef struct rtems_bsd_rc_conf_ rtems_bsd_rc_conf;
+
+/*
+ * A directive is a line in rc.conf and is 'name=value'. The handler is invoked
+ * if the name matches directive's regular expression.
+ */
+typedef int (*rtems_bsd_rc_conf_directive)(rtems_bsd_rc_conf* rc_conf,
+ int argc,
+ const char** argv);
+
+/*
+ * Register a directive handler.
+ */
+extern int rtems_bsd_rc_conf_directive_add(const char* dir_regex,
+ rtems_bsd_rc_conf_directive handler);
+
+/*
+ * Run an rc.conf script loaded into memory.
+ */
+extern int rtems_bsd_run_rc_conf_script(const char* name, const char* text, bool verbose);
+
+/*
+ * Run the rc.conf file.
+ */
+extern int rtems_bsd_run_rc_conf(const char* name, bool verbose);
+
+/*
+ * Run /etc/rc.conf.
+ */
+extern int rtems_bsd_run_etc_rc_conf(bool verbose);
+
+/*
+ * Return the name of the file being processed.
+ */
+extern const char* rtems_bsd_rc_conf_name(rtems_bsd_rc_conf* rc_conf);
+
+/*
+ * Return the line number being processed.
+ */
+extern int rtems_bsd_rc_conf_line(rtems_bsd_rc_conf* rc_conf);
+
+/*
+ * Print the argv list. Helper for verbose modes.
+ */
+extern void rtems_bsd_rc_conf_print_cmd(rtems_bsd_rc_conf* rc_conf,
+ const char* name,
+ int argc,
+ const char** argv);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif
diff --git a/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c b/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
new file mode 100644
index 00000000..3979e507
--- /dev/null
+++ b/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
@@ -0,0 +1,303 @@
+/*
+ * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Handle the networking directives found in rc.conf.
+ * - ifconfig_*
+ * - cloned_interfaces
+ * - autobridge_interfaces
+ * - autobridge_bridge*
+ * - defaultrouter
+ */
+
+#include <rtems/bsd/sys/param.h>
+#include <rtems/bsd/sys/types.h>
+
+#include <sys/queue.h>
+#include <sys/kernel.h>
+#include <sysexits.h>
+
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <rtems.h>
+#include <rtems/chain.h>
+
+#include <machine/rtems-bsd-commands.h>
+#include <machine/rtems-bsd-rc-conf.h>
+
+/*
+ * cloned_interfaces
+ *
+ * eg cloned_interfaces="vlan0 bridge0 tap1 tap2"
+ *
+ * See 'man rc.conf(5)' on FreeBSD.
+ */
+static int
+cloned_interfaces(rtems_bsd_rc_conf* rc_conf,
+ int argc,
+ const char** argv)
+{
+ int arg;
+ for (arg = 1; arg < argc; ++arg) {
+ const char* ifconfg_args[] = {
+ "ifconfig", argv[arg], "create", NULL
+ };
+ int r;
+ rtems_bsd_rc_conf_print_cmd(rc_conf, "cloning_interfaces", 3, ifconfg_args);
+ r = rtems_bsd_command_ifconfig(3, (char**) ifconfg_args);
+ if (r != EX_OK) {
+ errno = ECANCELED;
+ return -1;
+ }
+ }
+ return 0;
+}
+
+/*
+ * create_args_'interface'
+ *
+ * eg create_args_myvlan="vlan 102"
+ *
+ * See 'man rc.conf(5)' on FreeBSD.
+ */
+typedef struct {
+ rtems_chain_node node;
+ const char* label;
+ int argc;
+ const char** argv;
+} create_args_item;
+
+static RTEMS_CHAIN_DEFINE_EMPTY(create_args_items);
+
+static int
+create_args_(rtems_bsd_rc_conf* rc_conf,
+ int argc,
+ const char** argv)
+{
+ rtems_chain_node* node = rtems_chain_first(&create_args_items);
+ const rtems_chain_node* tail = rtems_chain_tail(&create_args_items);
+ const char* label = argv[0] + strlen("create_args_");
+ create_args_item* item;
+ int arg;
+
+ while (node != tail) {
+ item = (create_args_item*) node;
+ if (strcasecmp(item->label, label) == 0) {
+ fprintf(stderr, "error: %s:%d: duplicate create args entry: %s\n",
+ rtems_bsd_rc_conf_name(rc_conf),
+ rtems_bsd_rc_conf_line(rc_conf),
+ argv[0]);
+ errno = EEXIST;
+ return -1;
+ }
+ node = rtems_chain_next(node);
+ }
+
+ item = calloc(1, sizeof(*item));
+ if (item == NULL) {
+ errno = ENOMEM;
+ fprintf(stderr, "error: %s:%d: %s\n",
+ rtems_bsd_rc_conf_name(rc_conf),
+ rtems_bsd_rc_conf_line(rc_conf),
+ strerror(errno));
+ return -1;
+ }
+
+ item->argc = argc;
+
+ item->label = strdup(label);
+ if (item->label == NULL) {
+ free(item);
+ errno = ENOMEM;
+ fprintf(stderr, "error: %s:%d: %s\n",
+ rtems_bsd_rc_conf_name(rc_conf),
+ rtems_bsd_rc_conf_line(rc_conf),
+ strerror(errno));
+ return -1;
+ }
+
+ item->argv = calloc(argc + 1, sizeof(char*));
+ if (item->argv == NULL) {
+ free((void*) item->label);
+ free(item);
+ errno = ENOMEM;
+ fprintf(stderr, "error: %s:%d: %s\n",
+ rtems_bsd_rc_conf_name(rc_conf),
+ rtems_bsd_rc_conf_line(rc_conf),
+ strerror(errno));
+ return -1;
+ }
+
+ for (arg = 0; arg < argc; ++arg) {
+ item->argv[arg] = strdup(argv[0]);
+ if (item->argv[arg] == NULL) {
+ int a;
+ for (a = 0; a < arg; ++a)
+ free((void*) item->argv[a]);
+ free(item->argv);
+ free((void*) item->label);
+ free(item);
+ errno = ENOMEM;
+ fprintf(stderr, "error: %s:%d: %s\n",
+ rtems_bsd_rc_conf_name(rc_conf),
+ rtems_bsd_rc_conf_line(rc_conf),
+ strerror(errno));
+ return -1;
+ }
+ }
+
+ rtems_chain_append(&create_args_items, &item->node);
+
+ return 0;
+}
+
+/*
+ * ifconfig_'interface'
+ *
+ * eg ifconfig_em0="inet 10.10.5.33 netmask 255.255.255.0"
+ *
+ * See 'man rc.conf(5)' on FreeBSD.
+ */
+static int
+ifconfig_(rtems_bsd_rc_conf* rc_conf,
+ int argc,
+ const char** argv)
+{
+ const char** args;
+ int arg;
+ int r;
+
+ for (arg = 1; arg < argc; ++arg) {
+ if (strcasecmp(argv[arg], "NOAUTO") == 0)
+ return 0;
+ }
+
+ args = calloc(argc + 3, sizeof(char*));
+ if (args == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ args[0] = "ifconfig";
+ args[1] = argv[0] + strlen("ifconfig_");
+
+ for (arg = 1; arg < argc; ++arg)
+ args[arg + 1] = argv[arg];
+
+ args[argc + 1] = "up";
+
+ rtems_bsd_rc_conf_print_cmd(rc_conf, "ifconfig", argc + 2, args);
+
+ r = rtems_bsd_command_ifconfig(argc + 2, (char**) args);
+
+ free(args);
+
+ if (r != EX_OK) {
+ errno = ECANCELED;
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * hostname
+ *
+ * eg hostname="myhost"
+ *
+ * See 'man rc.conf(5)' on FreeBSD.
+ */
+static int
+hostname(rtems_bsd_rc_conf* rc_conf,
+ int argc,
+ const char** argv)
+{
+ if (argc > 2) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ rtems_bsd_rc_conf_print_cmd(rc_conf, "hostname", argc, argv);
+
+ return sethostname(argv[1], strlen(argv[1]));
+}
+
+/*
+ * defaultrouter
+ *
+ * eg defaultrouter="1.2.3.4"
+ *
+ * See 'man rc.conf(5)' on FreeBSD.
+ */
+static int
+defaultrouter(rtems_bsd_rc_conf* rc_conf,
+ int argc,
+ const char** argv)
+{
+ if (argc > 2) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (strcasecmp(argv[1], "NO") != 0) {
+ const char* args[] = {
+ "route", "add", "default", argv[1], NULL
+ };
+ int r;
+
+ rtems_bsd_rc_conf_print_cmd(rc_conf, "defaultrouter", 4, args);
+
+ r = rtems_bsd_command_route(4, (char**) args);
+ if (r != EX_OK) {
+ errno = ECANCELED;
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+static void
+add_directive(const char* name, rtems_bsd_rc_conf_directive handler)
+{
+ int r;
+ r = rtems_bsd_rc_conf_directive_add(name, handler);
+ if (r < 0)
+ fprintf(stderr, "error: cannot register rc.conf handler: %s\n", name);
+}
+
+void
+rc_conf_net_init(void* arg)
+{
+ add_directive("cloned_interfaces", cloned_interfaces);
+ add_directive("create_args_.*", create_args_);
+ add_directive("ifconfig_*", ifconfig_);
+ add_directive("hostname", hostname);
+ add_directive("defaultrouter", defaultrouter);
+}
diff --git a/rtemsbsd/rtems/rtems-bsd-rc-conf.c b/rtemsbsd/rtems/rtems-bsd-rc-conf.c
new file mode 100644
index 00000000..76f181d6
--- /dev/null
+++ b/rtemsbsd/rtems/rtems-bsd-rc-conf.c
@@ -0,0 +1,463 @@
+/*
+ * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/*
+ * Parse the /etc/rc.conf format file and execute the configuration options
+ * we want to support.
+ */
+
+
+#include <ctype.h>
+#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <regex.h>
+
+#include <rtems.h>
+#include <rtems/chain.h>
+
+#include <machine/rtems-bsd-rc-conf.h>
+
+/*
+ * Max line length.
+ */
+#define MAX_LINE_SIZE (512)
+
+/*
+ * Directive handler chain.
+ */
+struct rtems_bsd_rc_conf_ {
+ const char* name; /**< Name of the file. */
+ const char* data; /**< Pre-processed rc.conf data. */
+ size_t line_count; /**< Number of lines with text. */
+ const char** lines; /**< The lines in the file's text. */
+ size_t line; /**< The line being processed. */
+ bool verbose; /**< Verbose processing. */
+};
+
+/*
+ * Directive handler chain.
+ */
+typedef struct {
+ rtems_chain_node node;
+ const char* directive;
+ rtems_bsd_rc_conf_directive handler;
+} directive;
+
+/*
+ * Control of argc and argv.
+ */
+typedef struct {
+ char* command;
+ int argc;
+ const char** argv;
+} argc_argv;
+
+/*
+ * The chain of directives.
+ */
+static RTEMS_CHAIN_DEFINE_EMPTY(directives);
+
+static int
+argc_argv_create(const char* line, argc_argv* aa)
+{
+ char* c;
+ int arg;
+ char* brk;
+
+ if (strnlen(line, MAX_LINE_SIZE) >= MAX_LINE_SIZE) {
+ errno = EFBIG;
+ return -1;
+ }
+
+ aa->command = strdup(line);
+ if (aa->command == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ aa->argc = 0;
+ aa->argv = NULL;
+
+ /*
+ * Yes, the man page says do not use strtok. This is a local oopy, and the
+ * re-entrant version is being used.
+ */
+ c = aa->command;
+ while (true) {
+ c = strtok_r(c, " \t=\"", &brk);
+ if (c == NULL)
+ break;
+ ++aa->argc;
+ c = NULL;
+ }
+
+ /*
+ * Plus 1 for the trailing NULL present in argv lists.
+ */
+ aa->argv = calloc(aa->argc + 1, sizeof(char*));
+ if (aa->argv == NULL) {
+ free(aa->command);
+ errno = ENOMEM;
+ return -1;
+ }
+
+ aa->argv[0] = aa->command;
+ c = aa->command;
+
+ for (arg = 1; arg < aa->argc; ++arg) {
+ c += strlen(c) + 1;
+ while (*c != '\0' && (*c == '"' || isspace(*c)))
+ ++c;
+ aa->argv[arg] = c;
+ }
+
+ return 0;
+}
+
+static void
+argc_argv_destroy(argc_argv* aa)
+{
+ free(aa->argv);
+ free(aa->command);
+ aa->argv = NULL;
+ aa->command = NULL;
+}
+
+static int
+rc_conf_create(rtems_bsd_rc_conf* rc_conf,
+ const char* name,
+ const char* text,
+ bool verbose)
+{
+ size_t length;
+ char* line;
+ char* end;
+ char* copy;
+ char* marker;
+ const char** lines;
+ size_t line_count;
+
+ memset(rc_conf, 0, sizeof(*rc_conf));
+
+ /*
+ * Range check, this makes the rest safer in respect to buffer overflows.
+ */
+ length = strnlen(text, RTEMS_BSD_RC_CONF_MAX_SIZE);
+ if (length == RTEMS_BSD_RC_CONF_MAX_SIZE) {
+ errno = E2BIG;
+ return -1;
+ }
+
+ copy = strdup(text);
+ if (copy == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ end = copy + length;
+
+ /*
+ * Break the text up into lines.
+ */
+ line_count = 0;
+ for (line = copy; line != end; ++line) {
+ if (*line == '\n') {
+ line_count++;
+ *line = '\0';
+ }
+ }
+
+ lines = malloc(sizeof(char*) * line_count);
+ if (lines == NULL) {
+ free(copy);
+ errno = ENOMEM;
+ return -1;
+ }
+
+ memset(lines, 0, sizeof(char*) * line_count);
+
+ /*
+ * Extract all the lines.
+ */
+ line_count = 0;
+ for (marker = line = copy; line != end; ++line) {
+ if (*line == '\0' && line != end) {
+ /*
+ * Look for a comment.
+ */
+ char* comment = strchr(marker, '#');
+ if (comment != NULL)
+ *comment = '\0';
+ /*
+ * Remove leading whitespace.
+ */
+ length = strlen(marker);
+ while (*marker != '\0' && isspace(*marker))
+ memmove(marker, marker + 1, length--);
+ /*
+ * Remove trailing whitespace.
+ */
+ length = strlen(marker) - 1;
+ while (length > 0 && isspace(marker[length]))
+ marker[length--] = '\0';
+ /*
+ * Set the line.
+ */
+ lines[line_count] = marker;
+ ++line_count;
+ marker = line + 1;
+ }
+ }
+
+ rc_conf->name = name;
+ rc_conf->data = copy;
+ rc_conf->line_count = line_count;
+ rc_conf->lines = lines;
+ rc_conf->line = 0;
+ rc_conf->verbose = verbose;
+
+ return 0;
+}
+
+static void
+rc_conf_destroy(rtems_bsd_rc_conf* rc_conf)
+{
+ free((void*) rc_conf->data);
+ rc_conf->data = NULL;
+ rc_conf->name = NULL;
+}
+
+
+static int
+parse_line(rtems_bsd_rc_conf* rc_conf)
+{
+ const char* line;
+ rtems_chain_node* node = rtems_chain_first(&directives);
+ const rtems_chain_node* tail = rtems_chain_tail(&directives);
+ argc_argv aa;
+ int r;
+
+ line = rc_conf->lines[rc_conf->line];
+
+ if (*line == '\0')
+ return 0;
+
+ r = argc_argv_create(line, &aa);
+ if (r < 0) {
+ fprintf(stderr, "error: %s:%lu: creating argc/argv: %s\n",
+ rc_conf->name, rc_conf->line + 1, strerror(errno));
+ return r;
+ }
+
+ while (node != tail) {
+ directive* dir = (directive*) node;
+ regex_t rege;
+ #define MAX_MATCHES 1
+ regmatch_t matches[MAX_MATCHES];
+
+ r = regcomp(&rege, dir->directive, REG_EXTENDED);
+ if (r != 0) {
+ char rerror[128];
+ regerror(r, &rege, rerror, sizeof(rerror));
+ fprintf(stderr, "error: %s:%lu: %s\n",
+ rc_conf->name, rc_conf->line + 1, rerror);
+ argc_argv_destroy(&aa);
+ return -1;
+ }
+
+ r = regexec(&rege, aa.argv[0], MAX_MATCHES, matches, 0);
+ if (r != 0 && r != REG_NOMATCH) {
+ char rerror[128];
+ regerror(r, &rege, rerror, sizeof(rerror));
+ fprintf(stderr, "error: %s:%lu: %s\n",
+ rc_conf->name, rc_conf->line + 1, rerror);
+ regfree(&rege);
+ argc_argv_destroy(&aa);
+ return -1;
+ }
+
+ if (r == 0) {
+ r = dir->handler(rc_conf, aa.argc, aa.argv);
+ if (r < 0)
+ fprintf(stderr, "error: %s:%lu: runtime error: %s: %s\n",
+ rc_conf->name, rc_conf->line + 1, aa.argv[0], strerror(errno));
+ regfree(&rege);
+ argc_argv_destroy(&aa);
+ return r;
+ }
+
+ regfree(&rege);
+
+ node = rtems_chain_next(node);
+ }
+
+ errno = ENOSYS;
+
+ fprintf(stderr, "error: %s:%lu: %s: configuration name is not supported\n",
+ rc_conf->name, rc_conf->line + 1, aa.argv[0]);
+
+ argc_argv_destroy(&aa);
+
+ return -1;
+}
+
+int
+rtems_bsd_rc_conf_directive_add(const char* dir_regex,
+ rtems_bsd_rc_conf_directive handler)
+{
+ directive* dir;
+
+ dir = malloc(sizeof(*dir));
+ if (dir == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ memset(dir, 0, sizeof(*dir));
+
+ dir->directive = strdup(dir_regex);
+ if (dir->directive == NULL) {
+ free(dir);
+ errno = ENOMEM;
+ return -1;
+ }
+
+ dir->handler = handler;
+
+ rtems_chain_append(&directives, &dir->node);
+
+ return 0;
+}
+
+int
+rtems_bsd_run_rc_conf_script(const char* name, const char* text, bool verbose)
+{
+ rtems_bsd_rc_conf rc_conf;
+ int r;
+
+ if (verbose)
+ printf("rc.conf: processing: %s size:%lu\n", name, strlen(text));
+
+ r = rc_conf_create(&rc_conf, name, text, verbose);
+ if (r < 0) {
+ fprintf(stderr, "error: %s: parse error: %s\n",
+ name, strerror(errno));
+ return -1;
+ }
+
+ while (rc_conf.line < rc_conf.line_count) {
+ r = parse_line(&rc_conf);
+ if (r < 0)
+ break;
+ ++rc_conf.line;
+ }
+
+ rc_conf_destroy(&rc_conf);
+
+ return r;
+}
+
+int
+rtems_bsd_run_rc_conf(const char* name, bool verbose)
+{
+ struct stat sb;
+ int r;
+ char* rc_conf;
+ FILE* file;
+
+ r = stat(name, &sb);
+ if (r < 0)
+ return r;
+
+ rc_conf = malloc(sb.st_size);
+ if (rc_conf == NULL) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ if (verbose)
+ printf("rc.conf: loading: %s\n", name);
+
+ file = fopen(name, "r");
+ if (file == NULL) {
+ free(rc_conf);
+ return -1;
+ }
+
+ if (fread(rc_conf, 1, sb.st_size, file) != sb.st_size) {
+ fclose(file);
+ free(rc_conf);
+ return -1;
+ }
+
+ fclose(file);
+
+ r = rtems_bsd_run_rc_conf_script(name, rc_conf, verbose);
+
+ free(rc_conf);
+
+ return r;
+}
+
+int
+rtems_bsd_run_etc_rc_conf(bool verbose)
+{
+ return rtems_bsd_run_rc_conf("/etc/rc.conf", verbose);
+}
+
+const char*
+rtems_bsd_rc_conf_name(rtems_bsd_rc_conf* rc_conf)
+{
+ return rc_conf->name;
+}
+
+int
+rtems_bsd_rc_conf_line(rtems_bsd_rc_conf* rc_conf)
+{
+ return rc_conf->line + 1;
+}
+
+bool
+ rtems_bsd_rc_conf_verbose(rtems_bsd_rc_conf* rc_conf)
+{
+ return rc_conf->verbose;
+}
+
+void rtems_bsd_rc_conf_print_cmd(rtems_bsd_rc_conf* rc_conf,
+ const char* name,
+ int argc,
+ const char** argv)
+{
+ if (rc_conf->verbose) {
+ int arg;
+ printf("rc.conf: %s:%lu: %s:", rc_conf->name, rc_conf->line + 1, name);
+ for (arg = 0; arg < argc; ++arg)
+ printf(" %s", argv[arg]);
+ printf("\n");
+ }
+}
diff --git a/testsuite/rcconf01/test_main.c b/testsuite/rcconf01/test_main.c
new file mode 100644
index 00000000..df1b35fc
--- /dev/null
+++ b/testsuite/rcconf01/test_main.c
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2016 Chris Johns <chrisj@rtems.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <rtems/bsd/sys/param.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sysexits.h>
+
+#include <machine/rtems-bsd-rc-conf.h>
+
+#define TEST_NAME "LIBBSD RC.CONF 1"
+
+static const char* rc_conf_regex = \
+ "\n" /* empty line */ \
+ "#\n" /* comment then nothing */ \
+ "# comment\n" /* comment */
+ " # \n" /* whitespace (ws), commend, ws */ \
+ "test_regex_1=\n" /* name, empty value */ \
+ "test_regex_2=\"\"\n" /* name, quoted empty value */ \
+ "test_regex_3=\"1 2 3\"\n" /* name, 3 args */ \
+ " test_regex_4=\"04\"\n" /* ws, name, 1 arg */ \
+ "\ttest_regex_5=\"05\" \n" /* ws, name, 1 arg, ws */ \
+ "test_regex_6=\" 1 2\t 3 \"\n"; /* name then ws and 3 args */
+
+#define NUM_OF_TEST_REGEX_ 6
+static bool test_regex_results[NUM_OF_TEST_REGEX_];
+static int test_regex_last_num;
+
+static const char* rc_conf_not_found = \
+ "# invalid directive.\n" \
+ "abc_def_0=\"not found\"\n";
+
+static int
+test_regex_(rtems_bsd_rc_conf* rc_conf, int argc, const char** argv)
+{
+ int num;
+ int arg;
+
+ rtems_bsd_rc_conf_print_cmd(rc_conf, "test_regex_", argc, argv);
+
+ assert(strncasecmp(argv[0], "test_regex_", strlen("test_regex_")) == 0);
+ num = atoi(argv[0] + strlen("test_regex_"));
+ assert(num == (test_regex_last_num + 1));
+ assert((num - 1) < NUM_OF_TEST_REGEX_);
+ for (arg = 0; arg < argc; ++arg) {
+ const char* a = argv[arg];
+ size_t l = strlen(a);
+ if (l > 0) {
+ assert(!isspace(a[0]));
+ assert(!isspace(a[l - 1]));
+ assert(a[0] != '"');
+ assert(a[l - 1] != '"');
+ }
+ }
+ test_regex_results[num - 1] = true;
+ ++test_regex_last_num;
+
+ return 0;
+}
+
+static void
+make_rc_conf(const char* rc_conf)
+{
+ FILE* f;
+ unlink(rc_conf); /* Ignore any errors */
+ assert((f = fopen(rc_conf, "w")) != NULL);
+ assert(fwrite(rc_conf_regex, strlen(rc_conf_regex), 1, f) == 1);
+ assert(fclose(f) == 0);
+}
+
+static void
+test_regex_check(void)
+{
+ int i;
+ assert(test_regex_last_num == NUM_OF_TEST_REGEX_); /* all items found? */
+ for (i = 0; i < NUM_OF_TEST_REGEX_; ++i)
+ assert(test_regex_results[i]);
+}
+
+static void
+test_etc_rc_conf(void)
+{
+ memset(&test_regex_results[0], 0, sizeof(test_regex_results));
+ test_regex_last_num = 0;
+ make_rc_conf("/etc/rc.conf");
+ assert(rtems_bsd_run_etc_rc_conf(true) == 0);
+ test_regex_check();
+}
+
+static void
+test_rc_conf(void)
+{
+ memset(&test_regex_results[0], 0, sizeof(test_regex_results));
+ test_regex_last_num = 0;
+ make_rc_conf("/my_rc.conf");
+ assert(rtems_bsd_run_rc_conf("/my_rc.conf", true) == 0);
+ test_regex_check();
+}
+
+static void
+test_rc_conf_script(void)
+{
+ memset(&test_regex_results[0], 0, sizeof(test_regex_results));
+ test_regex_last_num = 0;
+ assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_regex, true) == 0);
+ test_regex_check();
+}
+
+static void
+test_rc_conf_script_not_found(void)
+{
+ assert(rtems_bsd_run_rc_conf_script("internal", rc_conf_not_found, true) < 0);
+}
+
+static void
+setup(void)
+{
+ struct stat sb;
+ mkdir("/etc", S_IRWXU | S_IRWXG | S_IRWXO); /* ignore errors, check the dir after. */
+ assert(stat("/etc", &sb) == 0);
+ assert(S_ISDIR(sb.st_mode));
+ assert(rtems_bsd_rc_conf_directive_add("test_regex_.*", test_regex_) == 0);
+}
+
+static void
+test_main(void)
+{
+ setup();
+
+ test_etc_rc_conf();
+ test_rc_conf();
+ test_rc_conf_script();
+ test_rc_conf_script_not_found();
+
+ exit(0);
+}
+
+#include <rtems/bsd/test/default-init.h>