summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2018-05-02 08:58:48 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2018-05-08 06:50:17 +0200
commit8bd38d645ca2a3bc481a2c1dbfa78fbcb9882378 (patch)
tree8a3bd431075685a3aa212af743e17dcfe134ea00
parentrc_conf: Do not create directories used by dhcpcd (diff)
downloadrtems-libbsd-8bd38d645ca2a3bc481a2c1dbfa78fbcb9882378.tar.bz2
dhcpcd: Add rtems_dhcpcd_start()
Use it throughout to start the DHCP client (dhcpcd).
-rw-r--r--dhcpcd/dhcpcd.c70
-rw-r--r--dhcpcd/namespace.h4
-rw-r--r--rtemsbsd/include/machine/rtems-bsd-commands.h2
-rw-r--r--rtemsbsd/include/rtems/dhcpcd.h84
-rw-r--r--rtemsbsd/rtems/rtems-bsd-init-dhcp.c35
-rw-r--r--rtemsbsd/rtems/rtems-bsd-rc-conf-net.c57
-rw-r--r--rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c68
-rw-r--r--testsuite/include/rtems/bsd/test/default-network-init.h37
8 files changed, 254 insertions, 103 deletions
diff --git a/dhcpcd/dhcpcd.c b/dhcpcd/dhcpcd.c
index 8074f3fc..59562d63 100644
--- a/dhcpcd/dhcpcd.c
+++ b/dhcpcd/dhcpcd.c
@@ -1110,22 +1110,86 @@ signal_init(void (*func)(int, siginfo_t *, void *), sigset_t *oldset)
#endif
#ifdef __rtems__
+#include <rtems/dhcpcd.h>
#include <rtems/libio.h>
+#include <assert.h>
+
+rtems_recursive_mutex dhcpcd_mutex =
+ RTEMS_RECURSIVE_MUTEX_INITIALIZER("dhcpcd");
+
+static bool dhcpcd_initialized;
+
struct getopt_data dhcpcd_getopt_data;
static int
main(int argc, char **argv);
-int rtems_bsd_command_dhcpcd(int argc, char **argv)
+static void
+dhcpcd_task(rtems_task_argument arg)
{
+ char *default_argv[] = { "dhcpcd", NULL };
+ const rtems_dhcpcd_config *config;
+ int argc;
+ char **argv;
int exit_code;
- rtems_mkdir(DBDIR, S_IRWXU | S_IRWXG | S_IRWXO);
+ config = (const rtems_dhcpcd_config *)arg;
+
+ if (config != NULL) {
+ argc = config->argc;
+ argv = config->argv;
+ } else {
+ argc = RTEMS_BSD_ARGC(default_argv);
+ argv = default_argv;
+ }
+ if (config != NULL && config->prepare != NULL) {
+ (*config->prepare)(config, argc, argv);
+ }
+
+ rtems_mkdir(DBDIR, S_IRWXU | S_IRWXG | S_IRWXO);
exit_code = rtems_bsd_program_call_main("dhcpcd", main, argc, argv);
- return exit_code;
+ if (config != NULL && config->destroy != NULL) {
+ (*config->destroy)(config, exit_code);
+ }
+
+ rtems_task_delete(RTEMS_SELF);
+}
+
+rtems_status_code
+rtems_dhcpcd_start(const rtems_dhcpcd_config *config)
+{
+ rtems_status_code sc;
+
+ rtems_recursive_mutex_lock(&dhcpcd_mutex);
+
+ if (!dhcpcd_initialized) {
+ rtems_task_priority priority;
+ rtems_id id;
+
+ if (config != NULL && config->priority != 0) {
+ priority = config->priority;
+ } else {
+ priority = RTEMS_MAXIMUM_PRIORITY - 1;
+ }
+
+ sc = rtems_task_create(rtems_build_name('D', 'H', 'C', 'P'), priority,
+ 2 * RTEMS_MINIMUM_STACK_SIZE, RTEMS_DEFAULT_MODES,
+ RTEMS_FLOATING_POINT, &id);
+ if (sc == RTEMS_SUCCESSFUL) {
+ dhcpcd_initialized = true;
+
+ sc = rtems_task_start(id, dhcpcd_task, 0);
+ assert(sc == RTEMS_SUCCESSFUL);
+ }
+ } else {
+ sc = RTEMS_INCORRECT_STATE;
+ }
+
+ rtems_recursive_mutex_unlock(&dhcpcd_mutex);
+ return sc;
}
#endif /* __rtems__ */
diff --git a/dhcpcd/namespace.h b/dhcpcd/namespace.h
index 7299c7d0..efff909a 100644
--- a/dhcpcd/namespace.h
+++ b/dhcpcd/namespace.h
@@ -1,3 +1,7 @@
+#include <rtems/thread.h>
+
+extern rtems_recursive_mutex dhcpcd_mutex;
+
#define add_options dhcpcd_add_options
#define arp_announce dhcpcd_arp_announce
#define arp_probe dhcpcd_arp_probe
diff --git a/rtemsbsd/include/machine/rtems-bsd-commands.h b/rtemsbsd/include/machine/rtems-bsd-commands.h
index 32aba44c..7e72627d 100644
--- a/rtemsbsd/include/machine/rtems-bsd-commands.h
+++ b/rtemsbsd/include/machine/rtems-bsd-commands.h
@@ -60,8 +60,6 @@ int rtems_bsd_command_ping6(int argc, char **argv);
int rtems_bsd_command_route(int argc, char **argv);
-int rtems_bsd_command_dhcpcd(int argc, char **argv);
-
int rtems_bsd_command_wpa_supplicant(int argc, char **argv);
int rtems_bsd_command_wpa_supplicant_fork(int argc, char **argv);
diff --git a/rtemsbsd/include/rtems/dhcpcd.h b/rtemsbsd/include/rtems/dhcpcd.h
new file mode 100644
index 00000000..324c8a90
--- /dev/null
+++ b/rtemsbsd/include/rtems/dhcpcd.h
@@ -0,0 +1,84 @@
+/**
+ * @file
+ *
+ * @ingroup rtems_bsd
+ *
+ * @brief TODO.
+ */
+
+/*
+ * Copyright (c) 2018 embedded brains GmbH. All rights reserved.
+ *
+ * embedded brains GmbH
+ * Dornierstr. 4
+ * 82178 Puchheim
+ * Germany
+ * <rtems@embedded-brains.de>
+ *
+ * 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.
+ */
+
+#ifndef _RTEMS_DHCPCD_H_
+#define _RTEMS_DHCPCD_H_
+
+#include <rtems.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * @brief The DHCP client configuration (dhcpcd).
+ *
+ * Zero initialize the structure to allow future extensions.
+ */
+typedef struct rtems_dhcpcd_config {
+ rtems_task_priority priority;
+ int argc;
+ char **argv;
+ void (*prepare)(const struct rtems_dhcpcd_config *config,
+ int argc, char **argv);
+ void (*destroy)(const struct rtems_dhcpcd_config *config,
+ int exit_code);
+} rtems_dhcpcd_config;
+
+/**
+ * @brief Starts the DHCP client (dhcpcd).
+ *
+ * @param config The DHCP client configuration. Use NULL for a default
+ * configuration.
+ *
+ * @retval RTEMS_SUCCESSFUL Successful operation.
+ * @retval RTEMS_INCORRECT_STATE The DHCP client runs already.
+ * @retval RTEMS_TOO_MANY No task control block available.
+ * @retval RTEMS_UNSATISFIED Not enough resources to create task.
+ * @retval RTEMS_INVALID_PRIORITY Invalid task priority.
+ */
+rtems_status_code rtems_dhcpcd_start(const rtems_dhcpcd_config *config);
+
+/** @} */
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* _RTEMS_DHCPCD_H_ */
diff --git a/rtemsbsd/rtems/rtems-bsd-init-dhcp.c b/rtemsbsd/rtems/rtems-bsd-init-dhcp.c
index 1115e40a..e0a298f7 100644
--- a/rtemsbsd/rtems/rtems-bsd-init-dhcp.c
+++ b/rtemsbsd/rtems/rtems-bsd-init-dhcp.c
@@ -7,7 +7,7 @@
*/
/*
- * Copyright (c) 2009-2015 embedded brains GmbH. All rights reserved.
+ * Copyright (c) 2009, 2018 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
@@ -38,36 +38,17 @@
*/
#include <rtems/bsd/bsd.h>
+#include <rtems/dhcpcd.h>
-#include <assert.h>
#include <sysexits.h>
-#include <machine/rtems-bsd-commands.h>
-
#include <bsp.h>
-static void
-dhcpcd_task(rtems_task_argument arg)
-{
- int exit_code;
- char *dhcpcd[] = {
- "dhcpcd",
- NULL
- };
-
- (void)arg;
-
- exit_code = rtems_bsd_command_dhcpcd(RTEMS_BSD_ARGC(dhcpcd), dhcpcd);
- assert(exit_code == EX_OK);
- (void)exit_code;
-}
-
rtems_status_code
rtems_bsd_initialize_dhcp(void)
{
rtems_status_code sc;
int exit_code;
- rtems_id id;
sc = rtems_bsd_initialize();
if (sc != RTEMS_SUCCESSFUL) {
@@ -79,21 +60,11 @@ rtems_bsd_initialize_dhcp(void)
return (RTEMS_UNSATISFIED);
}
- sc = rtems_task_create(
- rtems_build_name('D', 'H', 'C', 'P'),
- RTEMS_MAXIMUM_PRIORITY - 1,
- 2 * RTEMS_MINIMUM_STACK_SIZE,
- RTEMS_DEFAULT_MODES,
- RTEMS_FLOATING_POINT,
- &id
- );
+ sc = rtems_dhcpcd_start(NULL);
if (sc != RTEMS_SUCCESSFUL) {
return (RTEMS_UNSATISFIED);
}
- sc = rtems_task_start(id, dhcpcd_task, 0);
- assert(sc == RTEMS_SUCCESSFUL);
-
return (RTEMS_SUCCESSFUL);
}
diff --git a/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c b/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
index 0a5b3aae..bedf3bc0 100644
--- a/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
+++ b/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
@@ -61,6 +61,7 @@
#include <machine/rtems-bsd-rc-conf-services.h>
#include <rtems/rtems-routes.h>
+#include <rtems/dhcpcd.h>
/*
* Default defaultroute_delay is 30seconds.
@@ -627,45 +628,38 @@ setup_vlans(rtems_bsd_rc_conf* rc_conf,
* memory if it exits.
*/
typedef struct dhcpcd_data {
+ rtems_dhcpcd_config config;
rtems_bsd_rc_conf_argc_argv* argc_argv;
bool verbose;
const char* name;
} dhcpcd_data;
static void
-dhcpcd_worker(rtems_task_argument arg)
+dhcpcd_prepare(const rtems_dhcpcd_config *config, int argc, char **argv)
{
- dhcpcd_data* dd = (dhcpcd_data*) arg;
- int argc;
- const char** argv;
- const char* dhcpcd_argv[] = { "dhcpcd", NULL };
- int r;
-
- if (dd->argc_argv->argc > 0) {
- argc = dd->argc_argv->argc;
- argv = dd->argc_argv->argv;
- }
- else {
- argc = 1;
- argv = dhcpcd_argv;
- }
+ const dhcpcd_data* dd = (const dhcpcd_data*) config;
if (dd->verbose) {
+ int r;
+
fprintf(stdout, "rc.conf: %s: dhcpcd ", dd->name);
for (r = 1; r < argc; ++r)
fprintf(stdout, "%s ", argv[r]);
fprintf(stdout, "\n");
}
+}
- r = rtems_bsd_command_dhcpcd(argc, argv);
- if (r != EX_OK)
+static void
+dhcpcd_destroy(const rtems_dhcpcd_config *config, int exit_code)
+{
+ dhcpcd_data* dd = (dhcpcd_data*) config;
+
+ if (exit_code != EX_OK)
fprintf(stderr, "error: dhcpcd: stopped\n");
free(dd->name);
rtems_bsd_rc_conf_argc_argv_destroy(dd->argc_argv);
free(dd);
-
- rtems_task_delete(RTEMS_SELF);
}
static int
@@ -673,8 +667,7 @@ run_dhcp(rtems_bsd_rc_conf* rc_conf, rtems_bsd_rc_conf_argc_argv* aa)
{
dhcpcd_data* dd;
rtems_status_code sc;
- rtems_id id;
- rtems_task_priority priority = RTEMS_MAXIMUM_PRIORITY - 1;
+ rtems_task_priority priority = 0;
char* end = NULL;
int r;
@@ -711,21 +704,23 @@ run_dhcp(rtems_bsd_rc_conf* rc_conf, rtems_bsd_rc_conf_argc_argv* aa)
if (r == 0) {
if (dd->argc_argv->argc == 2) {
priority = strtoul(dd->argc_argv->argv[1], &end, 10);
- if (priority == 0 || *end != '\0')
- priority = RTEMS_MAXIMUM_PRIORITY - 1;
+ if (*end != '\0')
+ priority = 0;
}
}
+ dd->config.priority = priority;
rtems_bsd_rc_conf_find(rc_conf, "dhcpcd_options", dd->argc_argv);
- sc = rtems_task_create(rtems_build_name('D', 'H', 'C', 'P'),
- priority,
- 2 * RTEMS_MINIMUM_STACK_SIZE,
- RTEMS_DEFAULT_MODES,
- RTEMS_FLOATING_POINT,
- &id);
- if (sc == RTEMS_SUCCESSFUL)
- sc = rtems_task_start(id, dhcpcd_worker, (rtems_task_argument) dd);
+ if (dd->argc_argv->argc > 0) {
+ dd->config.argc = dd->argc_argv->argc;
+ dd->config.argv = dd->argc_argv->argv;
+ }
+
+ dd->config.prepare = dhcpcd_prepare;
+ dd->config.destroy = dhcpcd_destroy;
+
+ sc = rtems_dhcpcd_start(&dd->config);
if (sc != RTEMS_SUCCESSFUL) {
fprintf(stderr,
"error: dhcpcd: thread create/start: %s\n", rtems_status_text(sc));
diff --git a/rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c b/rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c
index 24893748..d02e2f44 100644
--- a/rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c
+++ b/rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2013 embedded brains GmbH. All rights reserved.
+ * Copyright (c) 2013, 2018 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
@@ -30,11 +30,73 @@
*/
#include <rtems/netcmds-config.h>
-#include <machine/rtems-bsd-commands.h>
+
+#include <rtems.h>
+#include <rtems/dhcpcd.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct {
+ rtems_dhcpcd_config config;
+ char *argv[RTEMS_ZERO_LENGTH_ARRAY];
+} dhcpcd_command_config;
+
+static void
+dhcpcd_command_destroy_config(const rtems_dhcpcd_config *config, int exit_code)
+{
+ char **argv;
+
+ (void)exit_code;
+
+ argv = config->argv;
+ while (*argv != NULL) {
+ free(*argv);
+ ++argv;
+ }
+
+ free(RTEMS_DECONST(rtems_dhcpcd_config *, config));
+}
+
+static int
+dhcpcd_command(int argc, char **argv)
+{
+ dhcpcd_command_config *config;
+ rtems_status_code sc;
+ int i;
+
+ config = calloc(1, sizeof(*config) + (argc + 1) * sizeof(char *));
+ if (config == NULL) {
+ fprintf(stdout, "dhcpcd error: not enough memory\n");
+ return 1;
+ }
+
+ for (i = 0; i < argc; ++i) {
+ config->argv[i] = strdup(argv[i]);
+ if (config->argv[i] == NULL) {
+ dhcpcd_command_destroy_config(&config->config, 0);
+ fprintf(stdout, "dhcpcd error: not enough memory\n");
+ return 1;
+ }
+ }
+
+ config->config.argc = argc;
+ config->config.argv = &config->argv[0];
+ config->config.destroy = dhcpcd_command_destroy_config;
+
+ sc = rtems_dhcpcd_start(&config->config);
+ if (sc != RTEMS_SUCCESSFUL) {
+ dhcpcd_command_destroy_config(&config->config, 0);
+ fprintf(stdout, "dhcpcd start failed: %s\n", rtems_status_text(sc));
+ }
+
+ return 0;
+}
rtems_shell_cmd_t rtems_shell_DHCPCD_Command = {
.name = "dhcpcd",
.usage = "dhcpcd [args]",
.topic = "net",
- .command = rtems_bsd_command_dhcpcd
+ .command = dhcpcd_command
};
diff --git a/testsuite/include/rtems/bsd/test/default-network-init.h b/testsuite/include/rtems/bsd/test/default-network-init.h
index ee95d26e..304c7a96 100644
--- a/testsuite/include/rtems/bsd/test/default-network-init.h
+++ b/testsuite/include/rtems/bsd/test/default-network-init.h
@@ -48,6 +48,7 @@
#include <rtems/stackchk.h>
#include <rtems/bsd/bsd.h>
#include <rtems/bsd/modules.h>
+#include <rtems/dhcpcd.h>
#if defined(DEFAULT_NETWORK_DHCPCD_ENABLE) && \
!defined(DEFAULT_NETWORK_NO_STATIC_IFCONFIG)
@@ -126,22 +127,16 @@ default_network_route_hwif0(char *ifname)
}
#endif
-#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
static void
-default_network_dhcpcd_task(rtems_task_argument arg)
+default_network_dhcpcd(void)
{
+#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
static const char default_cfg[] = "clientid libbsd test client\n";
- int exit_code;
- char *dhcpcd[] = {
- "dhcpcd",
- NULL
- };
+ rtems_status_code sc;
int fd;
int rv;
ssize_t n;
- (void)arg;
-
fd = open("/etc/dhcpcd.conf", O_CREAT | O_WRONLY,
S_IRWXU | S_IRWXG | S_IRWXO);
assert(fd >= 0);
@@ -159,29 +154,7 @@ default_network_dhcpcd_task(rtems_task_argument arg)
rv = close(fd);
assert(rv == 0);
- exit_code = rtems_bsd_command_dhcpcd(RTEMS_BSD_ARGC(dhcpcd), dhcpcd);
- assert(exit_code == EXIT_SUCCESS);
-}
-#endif
-
-static void
-default_network_dhcpcd(void)
-{
-#ifdef DEFAULT_NETWORK_DHCPCD_ENABLE
- rtems_status_code sc;
- rtems_id id;
-
- sc = rtems_task_create(
- rtems_build_name('D', 'H', 'C', 'P'),
- RTEMS_MAXIMUM_PRIORITY - 1,
- 2 * RTEMS_MINIMUM_STACK_SIZE,
- RTEMS_DEFAULT_MODES,
- RTEMS_FLOATING_POINT,
- &id
- );
- assert(sc == RTEMS_SUCCESSFUL);
-
- sc = rtems_task_start(id, default_network_dhcpcd_task, 0);
+ sc = rtems_dhcpcd_start(NULL);
assert(sc == RTEMS_SUCCESSFUL);
#endif
}