[PATCH v3 1/2] dhcpcd: Add rtems_dhcpcd_start()
Sebastian Huber
sebastian.huber at embedded-brains.de
Tue May 8 04:51:46 UTC 2018
Use it throughout to start the DHCP client (dhcpcd).
---
dhcpcd/dhcpcd.c | 70 +++++++++++++++++-
dhcpcd/namespace.h | 4 ++
rtemsbsd/include/machine/rtems-bsd-commands.h | 2 -
rtemsbsd/include/rtems/dhcpcd.h | 84 ++++++++++++++++++++++
rtemsbsd/rtems/rtems-bsd-init-dhcp.c | 35 +--------
rtemsbsd/rtems/rtems-bsd-rc-conf-net.c | 57 +++++++--------
rtemsbsd/rtems/rtems-bsd-shell-dhcpcd.c | 68 +++++++++++++++++-
.../include/rtems/bsd/test/default-network-init.h | 37 ++--------
8 files changed, 254 insertions(+), 103 deletions(-)
create mode 100644 rtemsbsd/include/rtems/dhcpcd.h
diff --git a/dhcpcd/dhcpcd.c b/dhcpcd/dhcpcd.c
index 8074f3fc3..59562d635 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 7299c7d0b..efff909ad 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 32aba44c1..7e72627d1 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 000000000..324c8a90b
--- /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 at 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 1115e40a8..e0a298f7e 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 0a5b3aaec..bedf3bc06 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 248937489..d02e2f44a 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 ee95d26ef..304c7a966 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
}
--
2.12.3
More information about the devel
mailing list